@@ -661,42 +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
- dmin , dmax = data .min (), data .max ()
668
+ # Rescale to [0, 2*pi]
669
+ data = (data - data .min ()) * (2 * np .pi / (data .max () - data .min ()))
670
670
671
- # Find the mode ignoring outliers on the far max/min, to allow for junk outside the FoV
672
- fudge = 0.05 * (dmax - dmin )
673
- mode = stats .mode (data [(data > dmin + fudge ) & (data < dmax - fudge )])[0 ][0 ]
674
-
675
- # Center data around 0.0
676
- data -= mode
677
-
678
- # Provide a less opaque error if we still can't figure it out
679
- neg = data < 0
680
- pos = data > 0
681
- if not (neg .any () and pos .any ()):
682
- raise ValueError ("Could not find an appropriate mode to center phase values around" )
683
-
684
- # Scale lower tail
685
- data [neg ] = - np .pi * data [neg ] / data [neg ].min ()
686
-
687
- # Scale upper tail
688
- data [pos ] = np .pi * data [pos ] / data [pos ].max ()
689
-
690
- # Offset to 0 - 2pi
691
- data += np .pi
692
-
693
- # Clip
694
- 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 )
695
673
696
674
hdr .set_data_dtype (np .float32 )
697
675
hdr .set_xyzt_units ('mm' )
698
676
out_file = fname_presuffix (in_file , suffix = '_rads' , newpath = newpath )
699
- nb .Nifti1Image (data , im . affine , hdr ).to_filename (out_file )
677
+ nb .Nifti1Image (data , None , hdr ).to_filename (out_file )
700
678
return out_file
701
679
702
680
0 commit comments