@@ -661,46 +661,20 @@ def _delta_te(in_values, te1=None, te2=None):
661
661
662
662
def au2rads (in_file , newpath = None ):
663
663
"""Convert the input phase difference map in arbitrary units (a.u.) to rads."""
664
- from scipy import stats
665
664
im = nb .load (in_file )
666
- data = im .get_fdata (dtype = 'float32' )
665
+ data = im .get_fdata (caching = 'unchanged' ) # Read as float64 for safety
667
666
hdr = im .header .copy ()
668
667
669
- if np .allclose ((data .min (), data .max ()), (- np .pi , np .pi ), atol = 0.01 ):
670
- # Already in rads, but wrap to 0 - 2pi
671
- data [data < 0 ] += 2 * np .pi
672
- else :
673
- dmin , dmax = data .min (), data .max ()
668
+ # Rescale to [0, 2*pi]
669
+ data = (data - data .min ()) * (2 * np .pi / (data .max () - data .min ()))
674
670
675
- # Find the mode ignoring outliers on the far max/min, to allow for junk outside the FoV
676
- fudge = 0.05 * (dmax - dmin )
677
- mode = stats .mode (data [(data > dmin + fudge ) & (data < dmax - fudge )])[0 ][0 ]
678
-
679
- # Center data around 0.0
680
- data -= mode
681
-
682
- # Provide a less opaque error if we still can't figure it out
683
- neg = data < 0
684
- pos = data > 0
685
- if not (neg .any () and pos .any ()):
686
- raise ValueError ("Could not find an appropriate mode to center phase values around" )
687
-
688
- # Scale lower tail
689
- data [neg ] = - np .pi * data [neg ] / data [neg ].min ()
690
-
691
- # Scale upper tail
692
- data [pos ] = np .pi * data [pos ] / data [pos ].max ()
693
-
694
- # Offset to 0 - 2pi
695
- data += np .pi
696
-
697
- # Clip
698
- data = np .clip (data , 0.0 , 2 * np .pi )
671
+ # Round to float32 and clip
672
+ data = np .clip (np .float32 (data ), 0.0 , 2 * np .pi )
699
673
700
674
hdr .set_data_dtype (np .float32 )
701
675
hdr .set_xyzt_units ('mm' )
702
676
out_file = fname_presuffix (in_file , suffix = '_rads' , newpath = newpath )
703
- nb .Nifti1Image (data , im . affine , hdr ).to_filename (out_file )
677
+ nb .Nifti1Image (data , None , hdr ).to_filename (out_file )
704
678
return out_file
705
679
706
680
0 commit comments