|
166 | 166 | """ |
167 | 167 |
|
168 | 168 | import argparse |
169 | | -import ants |
170 | | -import numpy as np |
171 | 169 | import sys |
172 | 170 | import os |
| 171 | + |
| 172 | +# Set threading environment variables BEFORE importing ants or other heavy libraries |
| 173 | +# This ensures they are picked up correctly during initialization |
| 174 | +if "--threads" in sys.argv: |
| 175 | + try: |
| 176 | + idx = sys.argv.index("--threads") |
| 177 | + if idx + 1 < len(sys.argv): |
| 178 | + threads = sys.argv[idx + 1] |
| 179 | + os.environ["ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS"] = str(threads) |
| 180 | + os.environ["OMP_NUM_THREADS"] = str(threads) |
| 181 | + os.environ["OPENBLAS_NUM_THREADS"] = str(threads) |
| 182 | + os.environ["MKL_NUM_THREADS"] = str(threads) |
| 183 | + os.environ["VECLIB_MAXIMUM_THREADS"] = str(threads) |
| 184 | + os.environ["NUMEXPR_NUM_THREADS"] = str(threads) |
| 185 | + except ValueError: |
| 186 | + pass |
| 187 | + |
| 188 | +import ants |
| 189 | +import numpy as np |
173 | 190 | from tqdm import tqdm |
174 | 191 | from colorama import init, Fore, Style |
175 | 192 | import scipy |
@@ -456,9 +473,12 @@ def run_motion_correction(dwi_path, input_bval_path, input_bvec_path, output_bve |
456 | 473 | # Check if inferred bval file exists |
457 | 474 | if not os.path.exists(input_bval_path): |
458 | 475 | raise FileNotFoundError(f"Could not find associated b-value file: {input_bval_path}") |
459 | | - # Set ANTs thread count (similar to lamar.py) |
| 476 | + |
| 477 | + # Environment variables are already set at the top of the script, |
| 478 | + # but we can reinforce them here just in case |
460 | 479 | os.environ["ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS"] = str(threads) |
461 | 480 | os.environ["OMP_NUM_THREADS"] = str(threads) |
| 481 | + |
462 | 482 | dataset = dmri.from_nii( |
463 | 483 | filename=dwi_path, |
464 | 484 | bvec_file=input_bvec_path, |
|
0 commit comments