Public
Edited
Apr 5, 2023
Insert cell
Insert cell
iCareBPC3Data = [
'https://raw.githubusercontent.com/jeyabbalas/py-icare/master/data/breast_cancer_covariate_model_formula.txt',
'https://raw.githubusercontent.com/jeyabbalas/py-icare/master/data/breast_cancer_model_log_odds_ratios.json',
'https://raw.githubusercontent.com/jeyabbalas/py-icare/master/data/reference_covariate_data.csv',
'https://raw.githubusercontent.com/jeyabbalas/py-icare/master/data/query_covariate_profile.csv',
'https://raw.githubusercontent.com/jeyabbalas/py-icare/master/data/age_specific_breast_cancer_incidence_rates.csv',
'https://raw.githubusercontent.com/jeyabbalas/py-icare/master/data/age_specific_all_cause_mortality_rates.csv'
];
Insert cell
async function fetchFilesAndWriteToPyodideFS(fileURLs) {
if (typeof pyodide === 'undefined') {
throw new Error('Pyodide is not loaded.');
}

async function fetchAndWriteFile(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch file from ${url}`);
}

const fileContent = await response.text();
const fileName = url.substring(url.lastIndexOf('/') + 1);
pyodide.FS.writeFile(fileName, fileContent);

console.log(`File ${fileName} successfully loaded to the Pyodide file system.`);
} catch (error) {
console.error(`Error fetching and writing file: ${error.message}`);
}
}

const promises = fileURLs.map(fetchAndWriteFile);
await Promise.all(promises);
}

Insert cell
async function loadICare() {
let response = await fetch('https://raw.githubusercontent.com/jeyabbalas/py-icare/master/dist/icare.zip')
let buffer = await response.arrayBuffer();
await pyodide.unpackArchive(buffer, 'zip');
await pyodide.loadPackage(['numpy', 'pandas', 'patsy']);
return pyodide.pyimport('icare');
}
Insert cell
icare = loadICare();
Insert cell
icare.absolute_risk_main.hello_world();
Insert cell
results = {
await fetchFilesAndWriteToPyodideFS(iCareBPC3Data);
return icare.compute_absolute_risk(
50, 30,
'age_specific_breast_cancer_incidence_rates.csv',
'age_specific_all_cause_mortality_rates.csv',
'breast_cancer_covariate_model_formula.txt',
'breast_cancer_model_log_odds_ratios.json',
'reference_covariate_data.csv',
null, null, null, 1,
'query_covariate_profile.csv',
null,
false, true
).toJs();
}
Insert cell
betaUsed = Array.from(results.get('beta_used').entries()).map(([feature, beta]) => ({feature, beta}));
Insert cell
betaUsed
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
profile = JSON.parse(results.get('profile'))
Insert cell
profile
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
results.get('reference_risks')[0]
Insert cell
populationRisks = results.get('reference_risks')[0].get('population_risks')
Insert cell
Plot.plot({
marks: [
Plot.rectY(populationRisks, Plot.binX({y: "count"})),
Plot.ruleY([0])
]
})
Insert cell
Insert cell
pyodide = {
let pyodide = await require("https://cdn.jsdelivr.net/pyodide/v0.23.0/full/pyodide.js");
return pyodide.loadPyodide();
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more