Skip to content

Commit e67c68b

Browse files
committed
Fix AttributeError in torch_labels_to_contours when torch is not installed
- Use string literals for torch.Tensor type hints to prevent AttributeError - Add TYPE_CHECKING import for proper type hint handling - Add from __future__ import annotations for forward references
1 parent 86da402 commit e67c68b

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

src/napari_tmidas/processing_functions/torch_labels_to_contours.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,28 @@
1010
PyTorch's GPU acceleration instead of CuPy.
1111
"""
1212

13+
from __future__ import annotations
14+
1315
from pathlib import Path
14-
from typing import List, Optional, Sequence, Tuple, Union
16+
from typing import TYPE_CHECKING, List, Optional, Sequence, Tuple, Union
1517

1618
import numpy as np
1719
import zarr
1820
from tqdm import tqdm
1921

22+
if TYPE_CHECKING:
23+
import torch
24+
2025
try:
2126
import torch
2227
import torch.nn.functional as F
2328
TORCH_AVAILABLE = True
2429
except ImportError:
2530
TORCH_AVAILABLE = False
26-
torch = None
31+
torch = None # type: ignore
2732

2833

29-
def _find_boundaries_torch(labels: torch.Tensor, mode: str = "outer") -> torch.Tensor:
34+
def _find_boundaries_torch(labels: "torch.Tensor", mode: str = "outer") -> "torch.Tensor":
3035
"""
3136
PyTorch implementation of skimage.segmentation.find_boundaries.
3237
@@ -113,7 +118,7 @@ def _find_boundaries_torch(labels: torch.Tensor, mode: str = "outer") -> torch.T
113118
return boundary
114119

115120

116-
def _gaussian_filter_torch(input: torch.Tensor, sigma: float) -> torch.Tensor:
121+
def _gaussian_filter_torch(input: "torch.Tensor", sigma: float) -> "torch.Tensor":
117122
"""
118123
PyTorch implementation of Gaussian filtering.
119124

0 commit comments

Comments
 (0)