@@ -955,20 +955,21 @@ def calc_moments(timeseries_file, moment):
955
955
zero = (m2 == 0 )
956
956
return np .where (zero , 0 , m3 / m2 ** (moment / 2.0 ))
957
957
958
- < << << << HEAD
959
- class AddNoiseInputSpec (TraitedSpec ):
960
- in_file = File ( exists = True , mandatory = True ,
961
- desc = 'input image that will be corrupted with noise' )
962
-
963
- in_mask = File ( exists = True , desc = 'input mask, voxels outside this mask\
964
- will be considered background and corrupted with noise\
965
- with Rayleigh distribution' )
966
958
967
- snr = traits .Float ( 10.0 , desc = 'desired output SNR in dB' , usedefault = True )
959
+ class AddNoiseInputSpec (TraitedSpec ):
960
+ in_file = File (exists = True , mandatory = True ,
961
+ desc = 'input image that will be corrupted with noise' )
962
+ in_mask = File (exists = True , desc = ('input mask, voxels outside this mask '
963
+ 'will be considered background' ))
964
+ snr = traits .Float (10.0 , desc = 'desired output SNR in dB' , usedefault = True )
965
+ dist = traits .Enum ('normal' , usedefault = True , mandatory = True ,
966
+ desc = ('desired noise distribution, currently '
967
+ 'only normal is implemented' ))
968
+ bg_dist = traits .Enum ('normal' , 'rayleigh' , usedefault = True , mandatory = True ,
969
+ desc = ('desired noise distribution, currently '
970
+ 'only normal is implemented' ))
971
+ out_file = File (desc = 'desired output filename' )
968
972
969
- dist = traits .Enum ( 'normal' , desc = 'desired noise distribution, currently\
970
- only gaussian is implemented' )
971
- out_file = File ( desc = 'desired output filename' )
972
973
973
974
class AddNoiseOutputSpec (TraitedSpec ):
974
975
out_file = File (exists = True , desc = 'corrupted image' )
@@ -1001,9 +1002,10 @@ def _run_interface(self, runtime):
1001
1002
else :
1002
1003
in_mask = np .ones_like ( in_data )
1003
1004
1004
- result = self .gen_noise ( in_data , mask = in_mask , snr_db = snr )
1005
- res_im = nb .Nifti1Image ( result , in_image .get_affine (), in_image .get_header () )
1006
- nb .save ( res_im , self ._gen_output_filename () )
1005
+ result = self .gen_noise (in_data , mask = in_mask , snr_db = snr ,
1006
+ dist = self .inputs .dist , bg_dist = self .inputs .bg_dist )
1007
+ res_im = nb .Nifti1Image (result , in_image .get_affine (), in_image .get_header ())
1008
+ res_im .to_filename (self ._gen_output_filename ())
1007
1009
return runtime
1008
1010
1009
1011
def _gen_output_filename ( self ):
@@ -1020,28 +1022,32 @@ def _list_outputs(self):
1020
1022
outputs ['out_file' ] = self ._gen_output_filename ()
1021
1023
return outputs
1022
1024
1023
- def gen_noise ( self , image , mask = None , snr_db = 10.0 ):
1025
+ def gen_noise (self , image , mask = None , snr_db = 10.0 , dist = 'normal' , bg_dist = 'normal' ):
1024
1026
"""
1025
1027
Generates a copy of an image with a certain amount of
1026
1028
added gaussian noise (rayleigh for background in mask)
1027
1029
"""
1028
1030
from math import sqrt
1029
- snr = sqrt ( np .power ( 10.0 , snr_db / 10.0 ) )
1030
- noise = np .random .normal ( size = image .shape )
1031
+ snr = sqrt (np .power (10.0 , snr_db / 10.0 ))
1031
1032
1032
- if mask is None :
1033
- mask = np .ones_like ( image )
1033
+ if dist == 'normal' :
1034
+ noise = np .random .normal (size = image .shape )
1035
+ else :
1036
+ raise NotImplementedError ('Only normal distribution is supported' )
1034
1037
1038
+ if mask is None :
1039
+ mask = np .ones_like (image )
1035
1040
1036
- S = np .mean (image [mask > 0 ])
1041
+ signal = image [mask > 0 ].reshape (- 1 )
1042
+ signal = signal - signal .mean ()
1043
+ S = (signal .var ())** 2
1037
1044
1038
- if np .any ( mask == 0 ):
1039
- S = S - np . mean ( image [ mask == 0 ] )
1040
- bg_noise = np .random .rayleigh ( size = image .shape )
1041
- noise [mask == 0 ] = bg_noise [mask == 0 ]
1045
+ if np .any (mask == 0 ):
1046
+ if bg_dist == 'rayleigh' :
1047
+ bg_noise = np .random .rayleigh (size = image .shape )
1048
+ noise [mask == 0 ] = bg_noise [mask == 0 ]
1042
1049
1043
1050
im_noise = image + noise * (S / snr )
1044
-
1045
1051
return im_noise
1046
1052
1047
1053
0 commit comments