Skip to content

Commit 558470c

Browse files
author
Benjamin Gorman
committed
Add type hints to monai/visualize/
- use 'if TYPE_CHECKING:' for optional imports - add type hints - remove docstring type hints optional_import does not enable correct type hinting so 'if TYPE_CHECKING:' is used. The type annotation must then be a forward reference i.e. it must be in quotes.
1 parent e1bf529 commit 558470c

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

monai/visualize/img2tensorboard.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
# See the License for the specific language governing permissions and
1010
# limitations under the License.
1111

12-
from typing import Optional, Sequence, Union
12+
from typing import Optional, Sequence, Union, TYPE_CHECKING
13+
14+
if TYPE_CHECKING:
15+
import torch.utils.tensorboard
1316

1417
import numpy as np
1518
import torch
@@ -97,13 +100,13 @@ def make_animated_gif_summary(
97100

98101

99102
def add_animated_gif(
100-
writer,
103+
writer: "torch.utils.tensorboard.SummaryWriter",
101104
tag: str,
102105
image_tensor: Union[np.ndarray, torch.Tensor],
103106
max_out: int,
104107
scale_factor: float,
105108
global_step: Optional[int] = None,
106-
):
109+
) -> None:
107110
"""Creates an animated gif out of an image tensor in 'CHWD' format and writes it with SummaryWriter.
108111
109112
Args:
@@ -124,13 +127,13 @@ def add_animated_gif(
124127

125128

126129
def add_animated_gif_no_channels(
127-
writer,
130+
writer: "torch.utils.tensorboard.SummaryWriter",
128131
tag: str,
129132
image_tensor: Union[np.ndarray, torch.Tensor],
130133
max_out: int,
131134
scale_factor: float,
132135
global_step: Optional[int] = None,
133-
):
136+
) -> None:
134137
"""Creates an animated gif out of an image tensor in 'HWD' format that does not have
135138
a channel dimension and writes it with SummaryWriter. This is similar to the "add_animated_gif"
136139
after inserting a channel dimension of 1.
@@ -155,22 +158,22 @@ def add_animated_gif_no_channels(
155158
def plot_2d_or_3d_image(
156159
data: Union[torch.Tensor, np.ndarray],
157160
step: int,
158-
writer,
161+
writer: "torch.utils.tensorboard.SummaryWriter",
159162
index: int = 0,
160163
max_channels: int = 1,
161164
max_frames: int = 64,
162165
tag: str = "output",
163-
):
166+
) -> None:
164167
"""Plot 2D or 3D image on the TensorBoard, 3D image will be converted to GIF image.
165168
166169
Note:
167170
Plot 3D or 2D image(with more than 3 channels) as separate images.
168171
169172
Args:
170-
data (Tensor or np.array): target data to be plotted as image on the TensorBoard.
173+
data: target data to be plotted as image on the TensorBoard.
171174
The data is expected to have 'NCHW[D]' dimensions, and only plot the first in the batch.
172175
step: current step to plot in a chart.
173-
writer (SummaryWriter): specify TensorBoard SummaryWriter to plot the image.
176+
writer: specify TensorBoard SummaryWriter to plot the image.
174177
index: plot which element in the input data batch, default is the first element.
175178
max_channels: number of channels to plot.
176179
max_frames: number of frames for 2D-t plot.

0 commit comments

Comments
 (0)