@@ -118,7 +118,9 @@ async def worker(
118
118
"""Create one worker and attach it to the execution loop."""
119
119
async with semaphore :
120
120
loop = asyncio .get_running_loop ()
121
- result = await loop .run_in_executor (None , func , data , coordinates , pe_info , hmc_xfm )
121
+ result = await loop .run_in_executor (
122
+ None , func , data , coordinates , pe_info , hmc_xfm
123
+ )
122
124
return result
123
125
124
126
@@ -204,14 +206,16 @@ async def unwarp_parallel(
204
206
xfm = None if xfms is None else xfms [volid ]
205
207
206
208
# IMPORTANT - the coordinates array must be copied every time anew per thread
207
- task = asyncio .create_task (worker (
208
- volume ,
209
- coordinates .copy (),
210
- pe_info [volid ],
211
- xfm ,
212
- func ,
213
- semaphore ,
214
- ))
209
+ task = asyncio .create_task (
210
+ worker (
211
+ volume ,
212
+ coordinates .copy (),
213
+ pe_info [volid ],
214
+ xfm ,
215
+ func ,
216
+ semaphore ,
217
+ )
218
+ )
215
219
tasks .append (task )
216
220
217
221
# Wait for all tasks to complete
@@ -295,15 +299,16 @@ def fit(
295
299
finest_coeffs = listify (self .coeffs )[- 1 ]
296
300
approx &= not np .allclose (
297
301
np .linalg .norm (
298
- np .cross (finest_coeffs .affine [:- 1 , :- 1 ].T , target_reference .affine [:- 1 , :- 1 ].T ),
302
+ np .cross (
303
+ finest_coeffs .affine [:- 1 , :- 1 ].T ,
304
+ target_reference .affine [:- 1 , :- 1 ].T ,
305
+ ),
299
306
axis = 1 ,
300
307
),
301
308
0 ,
302
309
atol = 1e-3 ,
303
310
)
304
311
305
-
306
-
307
312
weights = []
308
313
coeffs = []
309
314
if approx :
@@ -342,7 +347,9 @@ def fit(
342
347
from nitransforms .linear import Affine
343
348
344
349
_tmp_reference = nb .Nifti1Image (
345
- np .zeros (target_reference .shape [:3 ], dtype = target_reference .get_data_dtype ()),
350
+ np .zeros (
351
+ target_reference .shape [:3 ], dtype = target_reference .get_data_dtype ()
352
+ ),
346
353
target_reference .affine ,
347
354
target_reference .header ,
348
355
)
@@ -364,7 +371,7 @@ def apply(
364
371
mode : str = "constant" ,
365
372
cval : float = 0.0 ,
366
373
prefilter : bool = True ,
367
- output_dtype : str , np .dtype | None = None ,
374
+ output_dtype : str | np .dtype | None = None ,
368
375
num_threads : int = None ,
369
376
allow_negative : bool = False ,
370
377
):
@@ -478,16 +485,20 @@ def apply(
478
485
axis_flip = axcodes [pe_axis ] in ("LPI" )
479
486
pe_flip = pe_dir [volid ].endswith ("-" )
480
487
481
- pe_info .append ((
482
- pe_axis ,
483
- # Displacements are reversed if either is true (after ensuring positive cosines)
484
- - ro_time [volid ] if (axis_flip ^ pe_flip ) else ro_time [volid ]
485
- ))
488
+ pe_info .append (
489
+ (
490
+ pe_axis ,
491
+ # Displacements are reversed if either is true (after ensuring positive cosines)
492
+ - ro_time [volid ] if (axis_flip ^ pe_flip ) else ro_time [volid ],
493
+ )
494
+ )
486
495
487
496
# Reference image's voxel coordinates (in voxel units)
488
- voxcoords = nt .linear .Affine (
489
- reference = moving
490
- ).reference .ndindex .reshape ((ndim , * data .shape [:ndim ])).astype ("float32" )
497
+ voxcoords = (
498
+ nt .linear .Affine (reference = moving )
499
+ .reference .ndindex .reshape ((ndim , * data .shape [:ndim ]))
500
+ .astype ("float32" )
501
+ )
491
502
492
503
# Convert head-motion transforms to voxel-to-voxel:
493
504
if xfms is not None :
@@ -507,19 +518,21 @@ def apply(
507
518
)
508
519
509
520
# Resample
510
- resampled = asyncio .run (unwarp_parallel (
511
- data ,
512
- voxcoords ,
513
- self .mapped .get_fdata (dtype = "float32" ), # fieldmap in Hz
514
- pe_info ,
515
- xfms ,
516
- output_dtype = output_dtype ,
517
- order = order ,
518
- mode = mode ,
519
- cval = cval ,
520
- prefilter = prefilter ,
521
- max_concurrent = num_threads or min (os .cpu_count (), 12 ),
522
- ))
521
+ resampled = asyncio .run (
522
+ unwarp_parallel (
523
+ data ,
524
+ voxcoords ,
525
+ self .mapped .get_fdata (dtype = "float32" ), # fieldmap in Hz
526
+ pe_info ,
527
+ xfms ,
528
+ output_dtype = output_dtype ,
529
+ order = order ,
530
+ mode = mode ,
531
+ cval = cval ,
532
+ prefilter = prefilter ,
533
+ max_concurrent = num_threads or min (os .cpu_count (), 12 ),
534
+ )
535
+ )
523
536
524
537
if not allow_negative :
525
538
resampled [resampled < 0 ] = cval
0 commit comments