Skip to content

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
ImprintLab:masterfrom
rtmalikian:fix/issue-228-invalid-escape-sequence
Open

rtmalikian wants to merge 1 commit into
ImprintLab:masterfrom
rtmalikian:fix/issue-228-invalid-escape-sequence

Conversation

@rtmalikian

Copy link
Copy Markdown

Fixes #228
Fixes #216

Problem

Three Python 3.12+ compatibility issues prevent the codebase from running on modern Python versions:

  1. A SyntaxError in scripts/segmentation_train.py while using Python3.12+ #228 — scripts/segmentation_train.py line 44 uses "*\*.nii.gz" glob pattern containing invalid escape sequence \*. Python 3.12+ raises SyntaxError for unrecognized escape sequences.

  2. Issue within the unet.py (model architecture)!! #216 — guided_diffusion/unet.py lines 1764 and 1831 use np.ndarray or torch.tensor as type annotations. While or is a runtime operator (not a type union), this is semantically incorrect and will break under stricter type checking.

  3. Bonus — guided_diffusion/dpm_solver.py docstring contains LaTeX notation (\hat, \sqrt) with invalid escape sequences.

Solution

  1. 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.gz files in subdirectories.

  2. Issue within the unet.py (model architecture)!! #216: Replaced np.ndarray or torch.tensor with Union[np.ndarray, torch.tensor] (already imported in the file) for correct type annotation syntax.

  3. dpm_solver.py: Changed docstring from """ to r""" (raw string) to preserve LaTeX backslash notation without triggering escape sequence warnings.

Verification

# All Python files compile cleanly on Python 3.12 with -W error
$ python3.12 -W error -c "
import py_compile, os
for root, dirs, files in os.walk('.'):
    if '.git' in root: continue
    for f in files:
        if f.endswith('.py'):
            py_compile.compile(os.path.join(root, f), doraise=True)
print('All Python files compile cleanly on Python 3.12')
"
All Python files compile cleanly on Python 3.12

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.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant