From d81debac1c0929d377f6bc5e06527ad5c36bb49a Mon Sep 17 00:00:00 2001 From: rtmalikian Date: Thu, 18 Jun 2026 09:09:34 -0700 Subject: [PATCH] fix: resolve Python 3.12+ syntax errors in type annotations and escape sequences MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #228 — invalid escape sequence '\*' in glob pattern on line 44 of segmentation_train.py. Changed to forward slash '*/*.nii.gz' for proper pathlib glob matching. Fixes #216 — 'np.ndarray or torch.tensor' type annotations on lines 1764 and 1831 of unet.py. Replaced with Union[np.ndarray, torch.tensor] which is the correct typing syntax. Also fixed invalid escape sequences in dpm_solver.py docstring (LaTeX notation \hat, \sqrt) by converting to raw docstring (r"""). All Python files now compile cleanly on Python 3.12 with -W error. --- guided_diffusion/dpm_solver.py | 2 +- guided_diffusion/unet.py | 4 ++-- scripts/segmentation_train.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/guided_diffusion/dpm_solver.py b/guided_diffusion/dpm_solver.py index ce91f1f3..d09348fa 100644 --- a/guided_diffusion/dpm_solver.py +++ b/guided_diffusion/dpm_solver.py @@ -13,7 +13,7 @@ def __init__( continuous_beta_1=20., dtype=torch.float32, ): - """Create a wrapper class for the forward SDE (VP type). + r"""Create a wrapper class for the forward SDE (VP type). *** Update: We support discrete-time diffusion models by implementing a picewise linear interpolation for log_alpha_t. We recommend to use schedule='discrete' for the discrete-time diffusion models, especially for high-resolution images. diff --git a/guided_diffusion/unet.py b/guided_diffusion/unet.py index ddcc39fb..99969615 100644 --- a/guided_diffusion/unet.py +++ b/guided_diffusion/unet.py @@ -1761,7 +1761,7 @@ def _internal_predict_3D_3Dconv(self, x: np.ndarray, min_size: Tuple[int, ...], def _internal_maybe_mirror_and_pred_3D(self, x: Union[np.ndarray, torch.tensor], mirror_axes: tuple, do_mirroring: bool = True, - mult: np.ndarray or torch.tensor = None) -> torch.tensor: + mult: Union[np.ndarray, torch.tensor] = None) -> torch.tensor: assert len(x.shape) == 5, 'x must be (b, c, x, y, z)' # if cuda available: @@ -1828,7 +1828,7 @@ def _internal_maybe_mirror_and_pred_3D(self, x: Union[np.ndarray, torch.tensor], def _internal_maybe_mirror_and_pred_2D(self, x: Union[np.ndarray, torch.tensor], mirror_axes: tuple, do_mirroring: bool = True, - mult: np.ndarray or torch.tensor = None) -> torch.tensor: + mult: Union[np.ndarray, torch.tensor] = None) -> torch.tensor: # if cuda available: # everything in here takes place on the GPU. If x and mult are not yet on GPU this will be taken care of here # we now return a cuda tensor! Not numpy array! diff --git a/scripts/segmentation_train.py b/scripts/segmentation_train.py index a7ca2635..4e4c35b5 100644 --- a/scripts/segmentation_train.py +++ b/scripts/segmentation_train.py @@ -41,7 +41,7 @@ def main(): ds = BRATSDataset3D(args.data_dir, transform_train, test_flag=False) args.in_ch = 5 - elif any(Path(args.data_dir).glob("*\*.nii.gz")): + elif any(Path(args.data_dir).glob("*/*.nii.gz")): tran_list = [transforms.Resize((args.image_size,args.image_size)),] transform_train = transforms.Compose(tran_list) print("Your current directory : ",args.data_dir)