Public
Edited
Sep 22, 2023
Insert cell
Insert cell
Insert cell
Plot.plot({
marks: [
Plot.line(less_than_seven, { x: "date", y: "all" }),
Plot.line(less_than_seven, { x: "date", y: "cook" })
]
})
Insert cell
data.filter(
(d) =>
(d.admission > new Date("2016-06-01")) & (d.sentencing_county === "Cook")
).length / data.filter((d) => d.sentencing_county === "Cook").length
Insert cell
data.filter((d) => d.msr === null || d.msr > new Date("2030-03-31"))
Insert cell
data = raw_data
.map((d) => ({
admission: new Date(d["Current Admission Date"]),
msr: d["Projected Mandatory Supervised Release (MSR) Date"]
? new Date(d["Projected Mandatory Supervised Release (MSR) Date"])
: null,
discharge: d["Projected Discharge Date"]
? new Date(d["Projected Discharge Date"])
: null,
sentence_type: isNaN(d["Sentence Years"])
? d["Sentence Years"].toLowerCase()
: "term",
sentence_years: d["Sentence Years"],
sentencing_county: d["Sentencing County"]
}))
.map((d) => ({
...d,
sentence: d.sentence_type === "term" ? d.msr - d.admission : null
}))
Insert cell
raw_data = FileAttachment("June-2023-Prison@2.csv").csv()
Insert cell
isNaN("030")
Insert cell
async function fetchExcel(url, skipRow) {
const response = await fetch(
`https://corsproxy.bunkum.us/corsproxy/?apiurl=${url}`
);
const buffer = await response.arrayBuffer();
const workbook = xlsx.read(buffer, { type: "array", cellDates: true });
const sheet = workbook.Sheets[workbook.SheetNames[0]];
let range = xlsx.utils.decode_range(sheet["!ref"]);
range.s.r = skipRow;
sheet["!ref"] = xlsx.utils.encode_range(range);
return xlsx.utils.sheet_to_json(sheet);
}
Insert cell
less_than_seven = censuses.map((census) => {
const cutoff = new Date(census.date - time_to_census);
return {
date: census.date,
all:
census.data.filter((d) => d["Current Admission Date"] > cutoff).length /
census.data.length,
cook:
census.data.filter(
(d) =>
(d["Current Admission Date"] > cutoff) &
(d["Sentencing County"] === "Cook")
).length /
census.data.filter((d) => d["Sentencing County"] === "Cook").length,
all_total: census.data.length,
cook_total: census.data.filter((d) => d["Sentencing County"] === "Cook")
.length
};
})
Insert cell
censuses = Promise.all(
sources.map(async (d) => {
const raw = await fetchExcel(d.url, 5);
return {
date: d.date,
data: raw.map((d) => ({
...d,
"Current Admission Date":
d["Current Admission Date"] instanceof Date
? d["Current Admission Date"]
: d["Current Admission Date"]
? parseDate(d["Current Admission Date"])
: d["Current Admission Date"]
}))
};
})
)
Insert cell
sources = [
{
date: new Date("2023-06-30"),
url: "https://idoc.illinois.gov/content/dam/soi/en/web/idoc/reportsandstatistics/documents/June-2023-Prison.xls"
},
{
date: new Date("2022-06-30"),
url: "https://idoc.illinois.gov/content/dam/soi/en/web/idoc/reportsandstatistics/documents/june-2022-prison-population.xls"
},
{
date: new Date("2021-06-30"),
url: "https://idoc.illinois.gov/content/dam/soi/en/web/idoc/reportsandstatistics/documents/june-2021-prison-stock.xls"
},
{
date: new Date("2020-06-30"),
url: "https://idoc.illinois.gov/content/dam/soi/en/web/idoc/reportsandstatistics/documents/june-2020-prison-stock-pop.xls"
},
{
date: new Date("2019-06-30"),
url: "https://idoc.illinois.gov/content/dam/soi/en/web/idoc/reportsandstatistics/documents/june-2019-prison-stock.xls"
},
{
date: new Date("2018-06-30"),
url: "https://idoc.illinois.gov/content/dam/soi/en/web/idoc/reportsandstatistics/documents/june-2018-prison-internet-data-set.xls"
},
{
date: new Date("2017-06-30"),
url: "https://idoc.illinois.gov/content/dam/soi/en/web/idoc/reportsandstatistics/documents/June-2017-Prison-Population.xls"
},
{
date: new Date("2015-06-30"),
url: "https://idoc.illinois.gov/content/dam/soi/en/web/idoc/reportsandstatistics/documents/june-2015-prison-stock-internet-variables.xls"
},

{
date: new Date("2014-06-30"),
url: "https://idoc.illinois.gov/content/dam/soi/en/web/idoc/reportsandstatistics/documents/june-2014-prison-stock-internet-variables.xls"
},
{
date: new Date("2013-06-30"),
url: "https://idoc.illinois.gov/content/dam/soi/en/web/idoc/reportsandstatistics/documents/june-2013-prison-stock-internet-variables.xls"
},
{
date: new Date("2012-06-30"),
url: "https://idoc.illinois.gov/content/dam/soi/en/web/idoc/reportsandstatistics/documents/june-2012-prison-stock-internet-variables.xls"
},
{
date: new Date("2011-06-30"),
url: "https://idoc.illinois.gov/content/dam/soi/en/web/idoc/reportsandstatistics/documents/june-2011-prison-stock-pop.xls"
},
{
date: new Date("2010-06-30"),
url: "https://idoc.illinois.gov/content/dam/soi/en/web/idoc/reportsandstatistics/documents/june-2010-stock-pop.xls"
},
{
date: new Date("2009-06-30"),
url: "https://idoc.illinois.gov/content/dam/soi/en/web/idoc/reportsandstatistics/documents/june-2009-stock-pop.xls"
},
{
date: new Date("2008-06-30"),
url: "https://idoc.illinois.gov/content/dam/soi/en/web/idoc/reportsandstatistics/documents/june-2008-stock-pop.xls"
},
{
date: new Date("2007-06-30"),
url: "https://idoc.illinois.gov/content/dam/soi/en/web/idoc/reportsandstatistics/documents/june-2007-stock-pop.xls"
},
{
date: new Date("2006-06-30"),
url: "https://idoc.illinois.gov/content/dam/soi/en/web/idoc/reportsandstatistics/documents/june-2006-stock-pop.xls"
},
{
date: new Date("2005-06-30"),
url: "https://idoc.illinois.gov/content/dam/soi/en/web/idoc/reportsandstatistics/documents/june-2005-stock-pop.xls"
}
]
Insert cell
xlsx = require("xlsx")
Insert cell
census_day = new Date("2030-04-04")
Insert cell
time_to_census = census_day - new Date()
Insert cell
isNaN(census_day)
Insert cell
function parseDate(dateString) {
// Extract day, month, and year components from the string
const month = parseInt(dateString.substr(0, 2), 10) - 1;
const day = parseInt(dateString.substr(2, 2), 10);
const year = parseInt(dateString.substr(4, 4), 10);

// Create a Date object using the extracted components
const date = new Date(year, month, day);

return date;
}
Insert cell
11221 instanceof Date
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more