Access Values of an interface in python

93 Views Asked by At

I read in a fMRI image where I want to calculate a certain measurement.

        initial_data = os.path.join(str(subject_path)+'/derivatives/participant/fmriprep/sub-'+str(subject_label)+'/ses-'+str(k)+'/func/sub-'+str(subject_label)+'_ses-'+str(k)+'_task-rest_run-3_space-MNI152NLin6Asym_desc-smoothAROMAnonaggr_bold.nii.gz')


            mean_img = MeanImage(in_file=initial_data,dimension="T",nan2zeros=False,out_file=out_file_mean)
            mean_img.run()
            sd_img = StdImage(in_file=initial_data,dimension="T",nan2zeros=False,out_file=out_file_sd)
            sd_img.run()
            division = BinaryMaths(in_file=out_file_mean,operand_file=out_file_sd,operation="div",nan2zeros=False,out_file=out_file_dv)
            division.run()
            tsnr_value_calc = ImageStats(in_file=out_file_dv,op_string="-M")
            tsnr_result = tsnr_value_calc.run()
            print(tsnr_result)

outputs

220628-17:34:38,962 nipype.interface INFO:
     stdout 2022-06-28T17:34:38.962155:113.157831 

I want to extract the number (113.157831) in the end and save that into an array. How would I do that?

Converting the interface to a string with str() does not work. Printing the interface leads to:

print(tsnr_result)
<nipype.interfaces.base.support.InterfaceResult object at 0x7fd550078be0>

Here is the output of vars and tsnr_results.outputs would be the number I would need.

vars(tsnr_results)

1

There are 1 best solutions below

5
On

You can split the string in chunks separated with : and get the last element:

s = "220628-17:34:38,962 nipype.interface INFO: stdout 2022-06-28T17:34:38.962155:113.157831"

s = s.split(":")[-1]
print(s)

Output: You can convert it to float with float()

113.157831