Skip to content

Commit

Permalink
Add DIY title for visualize
Browse files Browse the repository at this point in the history
  • Loading branch information
HowcanoeWang committed Jan 25, 2023
1 parent da7a73d commit 3fe9848
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
16 changes: 13 additions & 3 deletions easyidp/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import matplotlib.patches as pts
from matplotlib.collections import PatchCollection
from tqdm import tqdm
import warnings


def _view_poly2mask(poly, mask, pix_all, pix_in):
Expand Down Expand Up @@ -205,7 +206,7 @@ def draw_polygon_on_img(img_name, img_path, poly_coord, corrected_poly_coord=Non
del fig, ax, img_array


def draw_backward_one_roi(proj, result_dict, buffer=40, save_as=None, show=False, title=None, color='red', alpha=0.5, dpi=72):
def draw_backward_one_roi(proj, result_dict, buffer=40, title=None, save_as=None, show=False, color='red', alpha=0.5, dpi=72):
"""Plot one ROI results on all available images.
Parameters
Expand All @@ -217,6 +218,8 @@ def draw_backward_one_roi(proj, result_dict, buffer=40, save_as=None, show=False
| e.g. ``{"IMG_2345": np.array([...]), "IMG_2346": np.array([])}``
buffer : int, optional
The pixel buffer number around the backward ROI results, by default 40
title : str, optional
The image title displayed on the top, by default None -> ``Projection on [img_name]``
save_as : str, optional
file path to save the output figure, by default None
show : bool, optional
Expand Down Expand Up @@ -256,6 +259,13 @@ def draw_backward_one_roi(proj, result_dict, buffer=40, save_as=None, show=False
:alt: draw_backward_one_roi.png'
"""
title_list = ['ROI Positions on Original Images', 'Enlarge Detail View']
if title is not None:
if isinstance(title, list) and len(title) == 2:
title_list = title
else:
warnings.warn(f"Expected title like ['title1', 'title2'], not given '{title}', using default title instead")

img_num = len(result_dict) + 1
grid_w = np.ceil(np.sqrt(img_num)).astype(int)

Expand Down Expand Up @@ -316,13 +326,13 @@ def draw_backward_one_roi(proj, result_dict, buffer=40, save_as=None, show=False

# add subfigure title
plt.text(.25, 0.99,
'ROI Positions on Original Images',
title_list[0],
transform=fig.transFigure,
horizontalalignment='center',
verticalalignment='top',
size='xx-large')
plt.text(.75, 0.99,
'Enlarge Detail View',
title_list[1],
transform=fig.transFigure,
horizontalalignment='center',
verticalalignment='top',
Expand Down
17 changes: 10 additions & 7 deletions tests/test_visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_visualize_one_roi_on_img_p4d():
save_as=test_data.vis.out / "p4d_show_roi_on_img_diy.png")

out = p4d.show_roi_on_img(
img_dict_p4d, 'N1W2', title="AAAA", color='green', alpha=0.5, show=False,
img_dict_p4d, 'N1W2', show=False, title=["AAAA", "BBBB"], color='green', alpha=0.5, show=False,
save_as=test_data.vis.out / "p4d_show_one_roi_all.png")


Expand All @@ -85,7 +85,8 @@ def test_visualize_one_roi_on_img_ms():
save_as=test_data.vis.out / "ms_show_roi_on_img_diy.png")

out = ms.show_roi_on_img(
img_dict_ms, 'N1W2', color='green', alpha=0.5, show=False,
img_dict_ms, 'N1W2', color='green', alpha=0.5,
show=False, title=["AAAA", "BBBB"],
save_as=test_data.vis.out / "ms_show_one_roi_all.png")


Expand All @@ -99,8 +100,10 @@ def test_draw_backward_one_roi():

img_dict_ms = roi.back2raw(ms)

idp.visualize.draw_backward_one_roi(
ms, img_dict_ms['N1W1'], buffer=40,
save_as=test_data.vis.out / "draw_backward_one_roi.png",
color='blue', show=False
)
with pytest.warns(UserWarning, match=re.escape(
"Expected title like ['title1', 'title2'], not given 'sdedf', using default title instead")):
idp.visualize.draw_backward_one_roi(
ms, img_dict_ms['N1W1'], buffer=40, title='sdedf',
save_as=test_data.vis.out / "draw_backward_one_roi.png",
color='blue', show=False
)

0 comments on commit 3fe9848

Please sign in to comment.