Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DOC] add interactive bundle viz to OR example #861

Merged
merged 5 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions AFQ/api/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,10 @@ def montage(self, bundle_name, size, view, slice_pos=None):
If float, indicates the fractional position along the
perpendicular axis to the slice. Currently only works with plotly.
If None, no slice is displayed.

Returns
-------
list of filenames of montage images
"""
if view not in ["Sagittal", "Coronal", "Axial"]:
raise ValueError(
Expand All @@ -551,6 +555,7 @@ def montage(self, bundle_name, size, view, slice_pos=None):
clean_bundles_dict = self.export("clean_bundles", collapse=False)
best_scalar_dict = self.export(best_scalar, collapse=False)

all_fnames = []
self.logger.info("Generating Montage...")
for ii in tqdm(range(len(self.valid_ses_list))):
this_sub = self.valid_sub_list[ii]
Expand Down Expand Up @@ -648,10 +653,12 @@ def montage(self, bundle_name, size, view, slice_pos=None):
window.snapshot(figure, fname=this_fname, size=(600, 600))

def _save_file(curr_img, curr_file_num):
curr_img.save(op.abspath(op.join(
save_path = op.abspath(op.join(
self.afq_path,
(f"bundle-{bundle_name}_view-{view}"
f"_idx-{curr_file_num}_montage.png"))))
f"_idx-{curr_file_num}_montage.png")))
curr_img.save(save_path)
all_fnames.append(save_path)

this_img_trimmed = {}
max_height = 0
Expand Down Expand Up @@ -692,6 +699,7 @@ def _save_file(curr_img, curr_file_num):
(x_pos * max_width, y_pos * max_height))

_save_file(curr_img, curr_file_num)
return all_fnames

def combine_bundle(self, bundle_name):
"""
Expand Down
8 changes: 4 additions & 4 deletions AFQ/data/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,9 +588,9 @@ def read_templates(as_img=True, resample_to=False):
]

or_remote_fnames = [
"26831630",
"36514170",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just making sure I understand this: These are the new edited ROIs for thalamus?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes and this works well in test datasets

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we have a way to refer to different versions of this data on figshare?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am not sure what you mean by this. Note that the new OR ROIs are on my figshare but the old ones are not.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh! Gotcha. I think that means we are good to go here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I mean up to the sphinx stuff, which I can sort out. Let me pull this rebased thing down and fiddle with that.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh - I see you are sorting that out too in a8a12a1

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I still need to test that

"26831633",
"26831636",
"36514173",
"26831639",
"26831642",
"26831645",
Expand All @@ -605,9 +605,9 @@ def read_templates(as_img=True, resample_to=False):
]

or_md5_hashes = [
"c18f3f82c26f334dc26b96d21f026dd1",
"a45126a727c4b5d843b2f7aae181825f",
"ad996c67bf5cc59fc3a7b60255873b67",
"786fb4ba915599f746950acd980e5b03",
"7a75c3ddd25335277a099626dbc946ac",
"cc88fb4671311404eb9dfa8fa11a59e0",
"9cff03af586d9dd880750cef3e0bf63f",
"ff728ba3ffa5d1600bcd19fdef8182c4",
Expand Down
7 changes: 3 additions & 4 deletions AFQ/tasks/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def viz_indivBundle(base_fname,

bundle_names = bundle_dict.keys()

figures = {}
for bundle_name in bundle_names:
logger.info(f"Generating {bundle_name} visualization...")
figure = viz_backend.visualize_volume(
Expand Down Expand Up @@ -261,7 +262,7 @@ def viz_indivBundle(base_fname,

roi_dir = op.join(results_dir, 'viz_bundles')
os.makedirs(roi_dir, exist_ok=True)
fnames = []
figures[bundle_name] = figure
if "no_gif" not in viz_backend.backend:
fname = op.split(
get_fname(
Expand All @@ -273,7 +274,6 @@ def viz_indivBundle(base_fname,

fname = op.join(roi_dir, fname[1])
viz_backend.create_gif(figure, fname)
fnames.append(fname)
if "plotly" in viz_backend.backend:
roi_dir = op.join(results_dir, 'viz_bundles')
os.makedirs(roi_dir, exist_ok=True)
Expand All @@ -287,7 +287,6 @@ def viz_indivBundle(base_fname,

fname = op.join(roi_dir, fname[1])
figure.write_html(fname)
fnames.append(fname)

# also do the core visualizations when using the plotly backend
core_dir = op.join(results_dir, 'viz_core_bundles')
Expand Down Expand Up @@ -341,7 +340,7 @@ def viz_indivBundle(base_fname,
segmentation_params=segmentation_params)
meta = dict(Timing=time() - start_time)
write_json(meta_fname, meta)
return fnames
return {"indiv_bundles_figures": figures}


@pimms.calc("tract_profile_plots")
Expand Down
11 changes: 10 additions & 1 deletion examples/plot_optic_radiations.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"""

import os.path as op
from IPython.display import Image
import plotly

from AFQ.api.group import GroupAFQ
import AFQ.api.bundle_dict as abd
import AFQ.data.fetch as afd
Expand Down Expand Up @@ -76,7 +79,13 @@
"01"]).get_bundle("L_OR").streamlines) > 1:
# create bundle montage and bundle combination
# across subject/session in MNI
my_afq.montage("L_OR", (1, 1), "Axial")
montages = my_afq.montage("L_OR", (1, 1), "Axial")
my_afq.combine_bundle("L_OR")
montage_img = Image(filename=montages[0])
else:
raise ValueError("No L_OR found")

# open interactive bundle visualization
bundle_html = my_afq.export("indiv_bundles_figures")
bundle_figure = bundle_html["01"]["L_OR"]
plotly.io.show(bundle_figure)