From 04f0a52a527294b41b5c4066dc0103fbae0b29c7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 22 Jan 2026 22:15:32 +0000 Subject: [PATCH 1/5] Initial plan From 2cab5792c8c4cbf8b48862e06fc8c679a411edff Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 22 Jan 2026 22:23:52 +0000 Subject: [PATCH 2/5] Fix mypy type checking error in plot method Co-authored-by: fepegar <12688084+fepegar@users.noreply.github.com> --- src/torchio/data/image.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/torchio/data/image.py b/src/torchio/data/image.py index 6afd748b..ad65064f 100644 --- a/src/torchio/data/image.py +++ b/src/torchio/data/image.py @@ -806,6 +806,7 @@ def plot(self, return_fig: bool = False, **kwargs) -> None | Figure: """Plot image.""" if self.is_2d(): self.as_pil().show() + return None else: from ..visualization import plot_volume # avoid circular import @@ -813,6 +814,7 @@ def plot(self, return_fig: bool = False, **kwargs) -> None | Figure: if return_fig: assert figure is not None return figure + return None def show(self, viewer_path: TypePath | None = None) -> None: """Open the image using external software. From 5aecc7fedfd60729d4cf15c7c40035d7e6f637f0 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 22 Jan 2026 22:26:10 +0000 Subject: [PATCH 3/5] Add docstring to _repr_html_ method Co-authored-by: fepegar <12688084+fepegar@users.noreply.github.com> --- src/torchio/data/image.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/torchio/data/image.py b/src/torchio/data/image.py index ad65064f..65bf2abc 100644 --- a/src/torchio/data/image.py +++ b/src/torchio/data/image.py @@ -211,6 +211,15 @@ def __repr__(self): return string def _repr_html_(self): + """Return HTML representation of the image for Jupyter notebooks. + + Generates an embedded base64-encoded PNG image using matplotlib. + If matplotlib is not installed, falls back to text representation. + + Returns: + HTML string with embedded image, or text representation if + matplotlib is unavailable. + """ try: from matplotlib import pyplot as plt from matplotlib.figure import Figure From 3605b5274efde1ac4e5206a779b93a5f993b58ca Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 22 Jan 2026 22:27:41 +0000 Subject: [PATCH 4/5] Replace assert with proper type check and add return type annotation Co-authored-by: fepegar <12688084+fepegar@users.noreply.github.com> --- src/torchio/data/image.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/torchio/data/image.py b/src/torchio/data/image.py index 65bf2abc..3c77a740 100644 --- a/src/torchio/data/image.py +++ b/src/torchio/data/image.py @@ -210,7 +210,7 @@ def __repr__(self): string = f'{self.__class__.__name__}({properties})' return string - def _repr_html_(self): + def _repr_html_(self) -> str: """Return HTML representation of the image for Jupyter notebooks. Generates an embedded base64-encoded PNG image using matplotlib. @@ -233,7 +233,9 @@ def _repr_html_(self): show=False, savefig_kwargs={'bbox_inches': 'tight'}, ) - assert isinstance(fig, Figure) + if not isinstance(fig, Figure): + message = f'Expected Figure, got {type(fig)}' + raise TypeError(message) plt.close(fig) buffer.seek(0) From e5be8a9b9cf4345479e2819943e8b67ef8321c22 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 24 Jan 2026 23:19:47 +0000 Subject: [PATCH 5/5] Revert changes to _repr_html_ and plot methods Co-authored-by: fepegar <12688084+fepegar@users.noreply.github.com> --- src/torchio/data/image.py | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/torchio/data/image.py b/src/torchio/data/image.py index 3c77a740..6afd748b 100644 --- a/src/torchio/data/image.py +++ b/src/torchio/data/image.py @@ -210,16 +210,7 @@ def __repr__(self): string = f'{self.__class__.__name__}({properties})' return string - def _repr_html_(self) -> str: - """Return HTML representation of the image for Jupyter notebooks. - - Generates an embedded base64-encoded PNG image using matplotlib. - If matplotlib is not installed, falls back to text representation. - - Returns: - HTML string with embedded image, or text representation if - matplotlib is unavailable. - """ + def _repr_html_(self): try: from matplotlib import pyplot as plt from matplotlib.figure import Figure @@ -233,9 +224,7 @@ def _repr_html_(self) -> str: show=False, savefig_kwargs={'bbox_inches': 'tight'}, ) - if not isinstance(fig, Figure): - message = f'Expected Figure, got {type(fig)}' - raise TypeError(message) + assert isinstance(fig, Figure) plt.close(fig) buffer.seek(0) @@ -817,7 +806,6 @@ def plot(self, return_fig: bool = False, **kwargs) -> None | Figure: """Plot image.""" if self.is_2d(): self.as_pil().show() - return None else: from ..visualization import plot_volume # avoid circular import @@ -825,7 +813,6 @@ def plot(self, return_fig: bool = False, **kwargs) -> None | Figure: if return_fig: assert figure is not None return figure - return None def show(self, viewer_path: TypePath | None = None) -> None: """Open the image using external software.