Skip to content

Commit b79aa4e

Browse files
updated threading for motion correction
1 parent 0d8c4c8 commit b79aa4e

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

micaflow/scripts/motion_correction.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,27 @@
166166
"""
167167

168168
import argparse
169-
import ants
170-
import numpy as np
171169
import sys
172170
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
173190
from tqdm import tqdm
174191
from colorama import init, Fore, Style
175192
import scipy
@@ -456,9 +473,12 @@ def run_motion_correction(dwi_path, input_bval_path, input_bvec_path, output_bve
456473
# Check if inferred bval file exists
457474
if not os.path.exists(input_bval_path):
458475
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
460479
os.environ["ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS"] = str(threads)
461480
os.environ["OMP_NUM_THREADS"] = str(threads)
481+
462482
dataset = dmri.from_nii(
463483
filename=dwi_path,
464484
bvec_file=input_bvec_path,

0 commit comments

Comments
 (0)