From 01874bfdbd56481f559c43de003740f16569d981 Mon Sep 17 00:00:00 2001 From: 36000 Date: Wed, 3 Jan 2024 12:01:33 -0800 Subject: [PATCH 1/6] [ENH] more little tweaks to the AFQ viz utils panelfigure class --- AFQ/viz/utils.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/AFQ/viz/utils.py b/AFQ/viz/utils.py index 7c7c82e68..81b5ab6aa 100644 --- a/AFQ/viz/utils.py +++ b/AFQ/viz/utils.py @@ -148,7 +148,8 @@ class PanelFigure(): into subplots using matplotlib """ - def __init__(self, num_rows, num_cols, width, height): + def __init__(self, num_rows, num_cols, width, height, + panel_label_kwargs={}): """ Initialize PanelFigure. @@ -162,14 +163,26 @@ def __init__(self, num_rows, num_cols, width, height): Width of figure in inches height : int Height of figure in inches + panel_label_kwargs : dict + Additional arguments for matplotlib's text method, + which is used to add panel labels to each subplot """ self.fig = plt.figure(figsize=(width, height)) self.grid = plt.GridSpec(num_rows, num_cols, hspace=0, wspace=0) self.subplot_count = 0 + self.panel_label_kwargs = dict( + fontsize="medium", + verticalalignment="top", + fontfamily='serif', + bbox=dict( + facecolor='0.7', + edgecolor='none', + pad=3.0)) + self.panel_label_kwargs.update(panel_label_kwargs) def add_img(self, fname, x_coord, y_coord, reduct_count=1, subplot_label_pos=(0.1, 1.0), legend=None, legend_kwargs={}, - add_panel_label=True, panel_label_font_size="medium"): + add_panel_label=True): """ Add image from fname into figure as a panel. @@ -196,9 +209,6 @@ def add_img(self, fname, x_coord, y_coord, reduct_count=1, add_panel_label : bool Whether or not to add a panel label to the subplot Default: True - panel_label_font_size : str - Font size of panel label - Default: "medium" """ ax = self.fig.add_subplot(self.grid[y_coord, x_coord]) im1 = Image.open(fname) @@ -218,9 +228,7 @@ def add_img(self, fname, x_coord, y_coord, reduct_count=1, subplot_label_pos[0], subplot_label_pos[1], f"{chr(65+self.subplot_count)})", transform=ax.transAxes + trans, - fontsize=panel_label_font_size, verticalalignment="top", - fontfamily='serif', - bbox=dict(facecolor='0.7', edgecolor='none', pad=3.0)) + **self.panel_label_kwargs) ax.imshow(np.asarray(im1), aspect=1) ax.axis('off') self.subplot_count = self.subplot_count + 1 From 16feca08d621f5c78a0f495d7ad076191ebadbe8 Mon Sep 17 00:00:00 2001 From: 36000 Date: Wed, 3 Jan 2024 15:36:24 -0800 Subject: [PATCH 2/6] remove paran --- AFQ/viz/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AFQ/viz/utils.py b/AFQ/viz/utils.py index 81b5ab6aa..e645471cc 100644 --- a/AFQ/viz/utils.py +++ b/AFQ/viz/utils.py @@ -226,7 +226,7 @@ def add_img(self, fname, x_coord, y_coord, reduct_count=1, 10 / 72, -5 / 72, self.fig.dpi_scale_trans) ax.text( subplot_label_pos[0], subplot_label_pos[1], - f"{chr(65+self.subplot_count)})", + f"{chr(65+self.subplot_count)}", transform=ax.transAxes + trans, **self.panel_label_kwargs) ax.imshow(np.asarray(im1), aspect=1) From e11bd3f0c655260f18818973de26b27dbd29ff67 Mon Sep 17 00:00:00 2001 From: 36000 Date: Thu, 4 Jan 2024 15:41:09 -0800 Subject: [PATCH 3/6] update panel default --- AFQ/viz/utils.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/AFQ/viz/utils.py b/AFQ/viz/utils.py index e645471cc..76909822a 100644 --- a/AFQ/viz/utils.py +++ b/AFQ/viz/utils.py @@ -171,13 +171,14 @@ def __init__(self, num_rows, num_cols, width, height, self.grid = plt.GridSpec(num_rows, num_cols, hspace=0, wspace=0) self.subplot_count = 0 self.panel_label_kwargs = dict( - fontsize="medium", + fontfamily="Helvetica-Bold", + fontsize="xx-large", + color="white", + fontweight='bold', verticalalignment="top", - fontfamily='serif', bbox=dict( - facecolor='0.7', - edgecolor='none', - pad=3.0)) + facecolor='none', + edgecolor='none')) self.panel_label_kwargs.update(panel_label_kwargs) def add_img(self, fname, x_coord, y_coord, reduct_count=1, From 8027a9fc627a040e341bf7f6c782f0bd385926a8 Mon Sep 17 00:00:00 2001 From: 36000 Date: Tue, 9 Jan 2024 12:23:52 -0800 Subject: [PATCH 4/6] add example, force dpi=300 --- AFQ/viz/utils.py | 2 +- examples/tutorial_examples/plot_003_viz.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/AFQ/viz/utils.py b/AFQ/viz/utils.py index 76909822a..00eab170b 100644 --- a/AFQ/viz/utils.py +++ b/AFQ/viz/utils.py @@ -251,7 +251,7 @@ def format_and_save_figure(self, fname, trim_final=True): if trim_final: im1 = Image.open(fname) im1 = trim(im1) - im1.save(fname) + im1.save(fname, dpi=(300, 300)) def get_eye(view, direc): diff --git a/examples/tutorial_examples/plot_003_viz.py b/examples/tutorial_examples/plot_003_viz.py index 8412d3a57..9d3bf67e5 100644 --- a/examples/tutorial_examples/plot_003_viz.py +++ b/examples/tutorial_examples/plot_003_viz.py @@ -29,6 +29,7 @@ from fury.colormap import create_colormap import AFQ.data.fetch as afd +from AFQ.viz.utils import PanelFigure ############################################################################## # Get some data from HBN POD2 @@ -418,6 +419,21 @@ def slice_volume(data, x=None, y=None, z=None): window.record(scene, out_path='arc_cst4.png', size=(2400, 2400)) +############################################################################# +# Making a Figure out of many fury panels +# --------------------------------------- +# We can also make a figure that contains multiple panels, each of which +# contains a different visualization. This is useful for communicating the +# results of an analysis. Here, we will make a figure with four panels, using +# some of the visualizations we have already created. We will use some +# convenient methods from pyAFQ. + +pf = PanelFigure(3, 2, 6, 9) +pf.add_img(f'arc_cst1.png', 0, 0) +pf.add_img(f'arc_cst2.png', 1, 0) +pf.add_img(f'arc_cst3.png', 0, 1) +pf.add_img(f'arc_cst4.png', 1, 1) +pf.format_and_save_figure(f"arc_cst_fig.png") ############################################################################# # From fc57cc66643c0875f74f4652a6d0d244c1e2f601 Mon Sep 17 00:00:00 2001 From: 36000 Date: Tue, 9 Jan 2024 14:19:24 -0800 Subject: [PATCH 5/6] try to make this example work on CI --- examples/tutorial_examples/plot_003_viz.py | 29 ++++++++++++++++------ 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/examples/tutorial_examples/plot_003_viz.py b/examples/tutorial_examples/plot_003_viz.py index 9d3bf67e5..83971d461 100644 --- a/examples/tutorial_examples/plot_003_viz.py +++ b/examples/tutorial_examples/plot_003_viz.py @@ -250,7 +250,11 @@ def slice_volume(data, x=None, y=None, z=None): # image into a png file. We use a pretty high resolution here (2400 by 2400) so # that we get a nice crisp image. That also means the file is pretty large. -window.record(scene, out_path='arc_cst1.png', size=(2400, 2400)) +cwd = os.getcwd() +window.record( + scene, + out_path=op.join(cwd, 'arc_cst1.png'), + size=(2400, 2400)) ############################################################################ @@ -281,7 +285,10 @@ def slice_volume(data, x=None, y=None, z=None): for slicer in slicers: scene.add(slicer) -window.record(scene, out_path='arc_cst2.png', size=(2400, 2400)) +window.record( + scene, + out_path=op.join(cwd, 'arc_cst2.png'), + size=(2400, 2400)) ############################################################################# @@ -348,7 +355,10 @@ def slice_volume(data, x=None, y=None, z=None): scene.add(core_arc_actor) scene.add(core_cst_actor) -window.record(scene, out_path='arc_cst3.png', size=(2400, 2400)) +window.record( + scene, + out_path=op.join(cwd, 'arc_cst3.png'), + size=(2400, 2400)) ############################################################################# @@ -417,7 +427,10 @@ def slice_volume(data, x=None, y=None, z=None): scene.add(waypoint2_actor) -window.record(scene, out_path='arc_cst4.png', size=(2400, 2400)) +window.record( + scene, + out_path=op.join(cwd, 'arc_cst4.png'), + size=(2400, 2400)) ############################################################################# # Making a Figure out of many fury panels @@ -429,10 +442,10 @@ def slice_volume(data, x=None, y=None, z=None): # convenient methods from pyAFQ. pf = PanelFigure(3, 2, 6, 9) -pf.add_img(f'arc_cst1.png', 0, 0) -pf.add_img(f'arc_cst2.png', 1, 0) -pf.add_img(f'arc_cst3.png', 0, 1) -pf.add_img(f'arc_cst4.png', 1, 1) +pf.add_img(op.join(cwd, 'arc_cst1.png'), 0, 0) +pf.add_img(op.join(cwd, 'arc_cst2.png'), 1, 0) +pf.add_img(op.join(cwd, 'arc_cst3.png'), 0, 1) +pf.add_img(op.join(cwd, 'arc_cst4.png'), 1, 1) pf.format_and_save_figure(f"arc_cst_fig.png") ############################################################################# From ab8e7605f26835f3c0a0a2b2291a134fdbc5c381 Mon Sep 17 00:00:00 2001 From: 36000 Date: Tue, 9 Jan 2024 21:41:35 -0800 Subject: [PATCH 6/6] try this --- examples/tutorial_examples/plot_003_viz.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/examples/tutorial_examples/plot_003_viz.py b/examples/tutorial_examples/plot_003_viz.py index 83971d461..9d0f320c9 100644 --- a/examples/tutorial_examples/plot_003_viz.py +++ b/examples/tutorial_examples/plot_003_viz.py @@ -250,10 +250,11 @@ def slice_volume(data, x=None, y=None, z=None): # image into a png file. We use a pretty high resolution here (2400 by 2400) so # that we get a nice crisp image. That also means the file is pretty large. -cwd = os.getcwd() +out_folder = op.join(afd.afq_home, "VizExample") +os.makedirs(out_folder, exist_ok=True) window.record( scene, - out_path=op.join(cwd, 'arc_cst1.png'), + out_path=op.join(out_folder, 'arc_cst1.png'), size=(2400, 2400)) @@ -287,7 +288,7 @@ def slice_volume(data, x=None, y=None, z=None): window.record( scene, - out_path=op.join(cwd, 'arc_cst2.png'), + out_path=op.join(out_folder, 'arc_cst2.png'), size=(2400, 2400)) @@ -357,7 +358,7 @@ def slice_volume(data, x=None, y=None, z=None): window.record( scene, - out_path=op.join(cwd, 'arc_cst3.png'), + out_path=op.join(out_folder, 'arc_cst3.png'), size=(2400, 2400)) @@ -429,7 +430,7 @@ def slice_volume(data, x=None, y=None, z=None): window.record( scene, - out_path=op.join(cwd, 'arc_cst4.png'), + out_path=op.join(out_folder, 'arc_cst4.png'), size=(2400, 2400)) ############################################################################# @@ -442,10 +443,10 @@ def slice_volume(data, x=None, y=None, z=None): # convenient methods from pyAFQ. pf = PanelFigure(3, 2, 6, 9) -pf.add_img(op.join(cwd, 'arc_cst1.png'), 0, 0) -pf.add_img(op.join(cwd, 'arc_cst2.png'), 1, 0) -pf.add_img(op.join(cwd, 'arc_cst3.png'), 0, 1) -pf.add_img(op.join(cwd, 'arc_cst4.png'), 1, 1) +pf.add_img(op.join(out_folder, 'arc_cst1.png'), 0, 0) +pf.add_img(op.join(out_folder, 'arc_cst2.png'), 1, 0) +pf.add_img(op.join(out_folder, 'arc_cst3.png'), 0, 1) +pf.add_img(op.join(out_folder, 'arc_cst4.png'), 1, 1) pf.format_and_save_figure(f"arc_cst_fig.png") #############################################################################