fix: resolve Python 3.12+ syntax errors in escape sequences and type annotations (Fixes #228, #216)#230
Open
rtmalikian wants to merge 1 commit into
Conversation
…e sequences Fixes ImprintLab#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 ImprintLab#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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #228
Fixes #216
Problem
Three Python 3.12+ compatibility issues prevent the codebase from running on modern Python versions:
A SyntaxError in scripts/segmentation_train.py while using Python3.12+ #228 —
scripts/segmentation_train.pyline 44 uses"*\*.nii.gz"glob pattern containing invalid escape sequence\*. Python 3.12+ raisesSyntaxErrorfor unrecognized escape sequences.Issue within the unet.py (model architecture)!! #216 —
guided_diffusion/unet.pylines 1764 and 1831 usenp.ndarray or torch.tensoras type annotations. Whileoris a runtime operator (not a type union), this is semantically incorrect and will break under stricter type checking.Bonus —
guided_diffusion/dpm_solver.pydocstring contains LaTeX notation (\hat,\sqrt) with invalid escape sequences.Solution
A SyntaxError in scripts/segmentation_train.py while using Python3.12+ #228: Changed
"*\*.nii.gz"to"*/*.nii.gz"— uses forward slash for proper pathlib glob matching of.nii.gzfiles in subdirectories.Issue within the unet.py (model architecture)!! #216: Replaced
np.ndarray or torch.tensorwithUnion[np.ndarray, torch.tensor](already imported in the file) for correct type annotation syntax.dpm_solver.py: Changed docstring from
"""tor"""(raw string) to preserve LaTeX backslash notation without triggering escape sequence warnings.Verification
About the Author: Raphael Malikian — Clinical AI Solutions Architect. I specialise in building and fixing AI/ML systems for healthcare, including vector databases, RAG pipelines, and clinical NLP. If you need help with your project or think I can add value to your organisation, feel free to reach out — I'd love to connect.
📧 rtmalikian@gmail.com
🔗 GitHub: https://github.com/rtmalikian
🔗 LinkedIn: http://www.linkedin.com/in/raphael-t-malikian-mbbs-bsc-hons-71075436a
Disclosure: This code was developed with assistance from mimo-2.5-pro (Xiaomi) via Hermes Agent (Nous Research). All changes were reviewed, tested against the actual codebase, and verified for correctness.