Skip to content

Commit

Permalink
Remove old mpl-specific figure code and saving calls; now only uses p…
Browse files Browse the repository at this point in the history
…lotly
  • Loading branch information
mathieuboudreau committed Feb 13, 2024
1 parent a8ae719 commit 0d05160
Showing 1 changed file with 4 additions and 82 deletions.
86 changes: 4 additions & 82 deletions content/index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Make figure of CSF/SC signal ratio from T2starw scan"
"Prepare data for figure of CSF/SC signal ratio from T2starw scan"
]
},
{
Expand All @@ -607,8 +607,6 @@
"source": [
"%matplotlib inline\n",
"\n",
"# Make figure of CSF/SC signal ratio from T2starw scan\n",
"\n",
"# Go back to root data folder\n",
"os.chdir(path_data)\n",
"\n",
Expand Down Expand Up @@ -637,15 +635,6 @@
"# Number of subjects determines the number of rows in the subplot\n",
"n_rows = len(subjects)\n",
"\n",
"# Create a figure with multiple subplots\n",
"fig, axes = plt.subplots(n_rows, 1, figsize=(10, 6 * n_rows))\n",
"font_size = 18\n",
"line_width = 3\n",
"\n",
"# Check if axes is an array or a single object\n",
"if n_rows == 1:\n",
" axes = [axes]\n",
"\n",
"# Data storage for statistics\n",
"data_stats = []\n",
"\n",
Expand Down Expand Up @@ -687,11 +676,6 @@
" # If there's data for this shim method, plot it and compute stats\n",
" if method_data:\n",
" t2_data_plotly[subject][shim_mode]=method_data\n",
"\n",
" # Plotting each file's data separately\n",
" for resampled_data in method_data:\n",
" ax.plot(x_grid, resampled_data, label=f\"{shim_mode}\", linewidth=line_width)\n",
"\n",
" \n",
" # Compute stats on the non-resampled data (to avoid interpolation errors)\n",
" mean_data = np.mean(data_sc_csf_ratio)\n",
Expand All @@ -700,32 +684,8 @@
" else:\n",
" t2_data_plotly[subject][shim_mode]=None\n",
"\n",
" # Set x-ticks and labels for the bottom subplot\n",
" if i == n_rows - 1: # Check if it's the last subplot\n",
" # Create a secondary axis for vertebral level labels\n",
" secax = ax.secondary_xaxis('bottom')\n",
" secax.set_xticks(label_positions)\n",
" secax.set_xticklabels(vertebral_levels, fontsize=font_size-4)\n",
" secax.tick_params(axis='x', which='major', length=0) # Hide tick marks\n",
"\n",
" ax.set_xticks(custom_xticks)\n",
" ax.set_xticklabels([''] * len(custom_xticks))\n",
" ax.tick_params(axis='x', which='major', length=0) # Hide tick marks for the primary axis\n",
"\n",
" ax.set_title(f'{subject}', fontsize=font_size)\n",
" ax.set_ylabel('CSF/Cord T2starw signal ratio', fontsize=font_size)\n",
" ax.tick_params(axis='y', which='major', labelsize=font_size-4)\n",
"\n",
" # Add legend only to the first subplot\n",
" if i == 0:\n",
" ax.legend(fontsize=font_size)\n",
"\n",
" ax.grid(True)\n",
"\n",
"# Adjust the layout so labels and titles do not overlap\n",
"plt.tight_layout()\n",
"plt.savefig(os.path.join(path_results, 'fig_gre_csf-sc_ratio.png'), dpi=300, format='png')\n",
"plt.show()"
"\n"
]
},
{
Expand Down Expand Up @@ -950,7 +910,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Make figure of B1+ values along the spinal cord across shim methods"
"Prepare data for figure of B1+ values along the spinal cord across shim methods"
]
},
{
Expand Down Expand Up @@ -993,15 +953,6 @@
"# Number of subjects determines the number of rows in the subplot\n",
"n_rows = len(subjects)\n",
"\n",
"# Create a figure with multiple subplots\n",
"fig, axes = plt.subplots(n_rows, 1, figsize=(10, 6 * n_rows))\n",
"font_size = 18\n",
"line_width = 3\n",
"\n",
"# Check if axes is an array or a single object\n",
"if n_rows == 1:\n",
" axes = [axes]\n",
"\n",
"# Data storage for statistics\n",
"data_stats = []\n",
"\n",
Expand Down Expand Up @@ -1041,7 +992,6 @@
" # Plotting each file's data separately\n",
" for resampled_data in method_data:\n",
" b1_data_plotly[subject][shim_mode]=resampled_data\n",
" ax.plot(x_grid, resampled_data, label=f\"{shim_mode}\", linewidth=line_width)\n",
"\n",
" # Compute stats on the non-resampled data (to avoid interpolation errors)\n",
" mean_data = np.mean(wa_data)\n",
Expand All @@ -1050,34 +1000,7 @@
" else:\n",
" b1_data_plotly[subject][shim_mode]=None\n",
"\n",
"\n",
" # Set x-ticks and labels for the bottom subplot\n",
" if i == n_rows - 1: # Check if it's the last subplot\n",
" # Create a secondary axis for vertebral level labels\n",
" secax = ax.secondary_xaxis('bottom')\n",
" secax.set_xticks(label_positions)\n",
" secax.set_xticklabels(vertebral_levels, fontsize=font_size-4)\n",
" secax.tick_params(axis='x', which='major', length=0) # Hide tick marks\n",
"\n",
" ax.set_xticks(custom_xticks)\n",
" ax.set_xticklabels([''] * len(custom_xticks))\n",
" ax.tick_params(axis='x', which='major', length=0) # Hide tick marks for the primary axis\n",
"\n",
" ax.set_title(f'{subject}', fontsize=font_size)\n",
" ax.set_ylabel('B1+ efficiency [nT/V]', fontsize=font_size)\n",
" ax.tick_params(axis='y', which='major', labelsize=font_size-4)\n",
"\n",
" # Add legend only to the first subplot\n",
" if i == 0:\n",
" ax.legend(fontsize=font_size)\n",
"\n",
" ax.grid(True)\n",
"\n",
"# Adjust the layout so labels and titles do not overlap\n",
"plt.tight_layout()\n",
"plt.savefig(os.path.join(path_results, 'fig_b1plus.png'), dpi=300, format='png')\n",
"\n",
"plt.show()"
"\n"
]
},
{
Expand Down Expand Up @@ -1313,7 +1236,6 @@
"# cbar_ax = fig.add_axes([0.95, 0.5, 0.04, 0.4])\n",
"# cbar = plt.colorbar(im, cax=cbar_ax)\n",
"cbar_ax.set_title('nT/V', size=12)\n",
"plt.savefig(os.path.join(path_results, 'fig_b1plus_map.png'), dpi=300, format='png')\n",
"plt.show()\n"
]
},
Expand Down

7 comments on commit 0d05160

@jcohenadad
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not sure i understand this commit-- it removes code to generate the figure with MPL, but it does not add code to make the figure with plotly-- or am i missing somethig?

@jcohenadad
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also-- does it mean that the current notebook (ie the original one) does not generate the plots anymore?

@jcohenadad
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, i think i understand now-- the figs are generated AT THE END of the notebook:
image

@jcohenadad
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, this is quite different from the 'regular' notebook (where people see the fig right after the processing)-- but i understand this was done to merge the neurolibre and colab notebooks into a single notebook-- weighting pros/cons, i guess the merge is probably a better idea

@jcohenadad
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one problem with that, though, is that it makes the notebook much more complicated to read for newbies, and one of the purpose of the notebook was 'reusability'-- eg: the native version of the notebook (ie: the 'easy to understand one') is currently being reused for another project: spinal-cord-7t/coil-qc-code#7 (comment). So i'm not sure i want the 'neurolibre-like' notebook to be used as a template for spin off projects-- tagging @nstikov so he is aware of this collateral issue

@mathieuboudreau i'm not sure what's the best approach at this point-- maybe keeping two notebooks out of sync was the better approach... i dunno. we should discuss. Also tagging @evaalonsoortiz and @Kyota-exe so they are aware

@mathieuboudreau
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jcohenadad So, my old way, both were "generated" when the notebook is run, but only the plotly one is displayed in the HTML Jupyter Book. It is a bit more complicated as you noticed, as I took the data for the plot just before MPL plots it and saved it in the variables to be used with plotly later, so that if anything substantial (i.e. not cosmetic) changes in the data processing, both figures would get updated at tyhe same time.

So i'm not sure i want the 'neurolibre-like' notebook to be used as a template for spin off projects--

That's a good point, I guess the issue originates with the fact that you already had your paper-quality plots ready; could have just been skipped if you wanted in this case (or as we almost did, convert completely from MPL to Plotly).

and one of the purpose of the notebook was 'reusability'-- eg: the native version of the notebook (ie: the 'easy to understand one')

Yes I strongly agree with this point, in general with Plotly. Recall, the cNeuroMod book that Kiril had built also had issues with reusability / duplicate code, and it took me (which for the sake of neurolibre+plotly, we'll say I'm an expert) a considerable amount of time refactoring it into something that was fairly reusable/flexible (but still quite complicated code)

@mathieuboudreau
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, i think i understand now-- the figs are generated AT THE END of the notebook:

Yes, this was so that 1) the figures had the same "order" as for your figure, but also 2) because you were plotting the A and B columns in two completely different parts of the figure using MPL, and I felt you wanted one figure showing both next to each other., so this is the solution I decided on.

Please sign in to comment.