Skip to content

Commit

Permalink
enh: start drafting tutorial notebook
Browse files Browse the repository at this point in the history
enh: add data necessary to run the example
  • Loading branch information
celprov committed May 16, 2024
1 parent c6c859a commit 7e9132a
Show file tree
Hide file tree
Showing 8 changed files with 646 additions and 0 deletions.
151 changes: 151 additions & 0 deletions docs/notebooks/Tutorial.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Nireports"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nireports is a framework to easily combine what we call reportlets, that is data visualizations that help assess the quality of a particular processing step, into HTML visual report. In this notebook, we give you an example about how to build your own visual report using the nireports framework. The goal with this example visual report is to summarize some group properties of functional connectivity (FC) matrices computed from a single dataset in order to assess the quality of connectivity extraction.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first step is to generate the visualizations you would like and store them in a file with a BIDS-compatible name."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Load data\n",
"import pandas as pd\n",
"\n",
"# Define the path where the derivatives are stored\n",
"output = (\"/home/cprovins/derivatives/hcph/\")\n",
"# Load fMRI duration after censoring\n",
"good_timepoints_df = pd.read_csv(\"./data/fMRI_duration_after_censoring.csv\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A reportlet does not have to be stored in an image format, it is also possible to incorporate HTML chunks into the overall report. Let us look at an example together. The goal with the next reportlet is to verify whether each session has enough timepoints after censoring to reliably estimate FC. If not, that session needs to be identified and excluded. We use plotly to generate an interactive plot in which hovering on the points tells you the session it corresponds to. The interactivity would not be possible with an image that is why we store the reportlet in HTML format."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import plotly.offline as pyo\n",
"# The literature indicates that less than 5 minutes of fMRI signal after censoring is not enough to reliably estimate functional connectivity.\n",
"DURATION_CUT_OFF = 300\n",
"# Generate each reportlets\n",
"def group_report_censoring(good_timepoints_df, output) -> None:\n",
" \"\"\"\n",
" Generate a group report about censoring.\n",
"\n",
" This function generates an HTML report that includes an interactive scatterplot\n",
" showing the fMRI duration after censoring. The scatterplot includes\n",
" error bars for the confidence interval and a red line indicating a duration cutoff.\n",
"\n",
" Parameters:\n",
" -----------\n",
" good_timepoints_df: pd.Dataframe\n",
" A DataFrame containing information the fMRI duration after censoring.\n",
" output : str\n",
" Path to the output directory\n",
" \"\"\"\n",
" filenames = good_timepoints_df[\"filename\"]\n",
" durations = good_timepoints_df[\"duration\"]\n",
"\n",
" # Constructing the data for the plot\n",
" # Add jitter to x values\n",
" jitter = 0.2 # adjust this value to change the amount of jitter\n",
" x_values = [1 + np.random.uniform(-jitter, jitter) for _ in range(len(durations))]\n",
" data = [\n",
" {\n",
" \"x\": x_values,\n",
" \"y\": durations,\n",
" \"text\": filenames,\n",
" \"mode\": \"markers\",\n",
" \"type\": \"scatter\",\n",
" \"hoverinfo\": \"text\",\n",
" \"marker\": {\"opacity\": 0.5},\n",
" }\n",
" ]\n",
"\n",
" # Adding a red line at 5 minutes\n",
" red_line = {\n",
" \"type\": \"line\",\n",
" \"x0\": 0,\n",
" \"y0\": DURATION_CUT_OFF,\n",
" \"x1\": 1.5,\n",
" \"y1\": DURATION_CUT_OFF,\n",
" \"line\": {\"color\": \"red\", \"width\": 3, \"dash\": \"dashdot\"},\n",
" }\n",
"\n",
" # Layout settings\n",
" layout = {\n",
" \"hovermode\": \"closest\",\n",
" \"title\": \"Duration of fMRI signal after censoring\",\n",
" \"yaxis\": {\"title\": \"Duration [s]\"},\n",
" \"xaxis\": {\"showticklabels\": False, \"range\": [0.5, 1.5]},\n",
" \"shapes\": [red_line],\n",
" \"width\": 600,\n",
" \"height\": 600,\n",
" \"font\": {\"size\": 16},\n",
" \"annotations\": [\n",
" {\n",
" \"x\": 0.8,\n",
" \"y\": DURATION_CUT_OFF - DURATION_CUT_OFF / 55,\n",
" \"xref\": \"x\",\n",
" \"yref\": \"y\",\n",
" \"text\": f\"QC cutoff of {DURATION_CUT_OFF/60} min\",\n",
" \"showarrow\": False,\n",
" \"font\": {\"color\": \"red\"},\n",
" }\n",
" ],\n",
" }\n",
"\n",
" fig = {\"data\": data, \"layout\": layout}\n",
"\n",
" # Save the plot as an HTML file\n",
" pyo.plot(\n",
" fig,\n",
" filename=op.join(output, \"reportlets\", \"group_desc-censoring_bold.html\"),\n",
" auto_open=False,\n",
" )\n",
"group_report_censoring(good_timepoints_df, output)\n",
"group_reportlet_fc_dist(fc_matrices, output)\n",
"qc_fc_dict = group_reportlet_qc_fc(fc_matrices, iqms_df, output)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
16 changes: 16 additions & 0 deletions docs/notebooks/data/fMRI_duration_after_censoring.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
filename,duration
sub-001_ses-001_task-qct_dir-LR_part-mag_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,156.8
sub-001_ses-003_task-qct_dir-LR_part-mag_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,156.8
sub-001_ses-004_task-qct_dir-RL_part-mag_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,145.6
sub-001_ses-007_task-qct_dir-LR_part-mag_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,156.8
sub-001_ses-008_task-qct_dir-RL_part-mag_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,156.8
sub-001_ses-005_task-qct_dir-PA_part-mag_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,156.8
sub-001_ses-006_task-qct_dir-PA_part-mag_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,156.8
sub-001_ses-009_task-qct_dir-AP_part-mag_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,156.8
sub-001_ses-001_task-qct_dir-LR_part-mag_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,156.8
sub-001_ses-001_task-qct_dir-LR_part-mag_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,156.8
sub-001_ses-003_task-qct_dir-LR_part-mag_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,145.6
sub-001_ses-004_task-qct_dir-RL_part-mag_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,156.8
sub-001_ses-008_task-qct_dir-RL_part-mag_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,156.8
sub-001_ses-005_task-qct_dir-PA_part-mag_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,156.8
sub-001_ses-006_task-qct_dir-PA_part-mag_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz,156.8
Loading

0 comments on commit 7e9132a

Please sign in to comment.