0

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)

Dr. S
  • 41
  • 1
  • 1
  • 7
  • What do you mean by "does not work"? You need to provide a [mre]. For more tips, like how to write a good title, please read [ask]. – wjandrea Jun 28 '22 at 15:57
  • what library is that? you obviously need to reach into the `tsnr_result` object to get that information -- you did not specify that this is `nipype` -- please post `vars(tsnr_result)` ([edit] your question) – Christoph Rackwitz Jun 28 '22 at 18:09

1 Answers1

0

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
Cardstdani
  • 4,999
  • 3
  • 12
  • 31
  • 1
    I doubt this is a solution since OP wrote "Converting the interface to sth like a string does not work", though it's not clear what they mean by "does not work". Hypothetically, maybe it's being printed directly to stdout and they need to capture it, or maybe it's a bytestring and they don't know how to decode it. – wjandrea Jun 28 '22 at 16:00
  • @wjandrea Well, it would be great if he adds more information to the question. But thanks for the advice – Cardstdani Jun 28 '22 at 16:02
  • The problem is, I dont know how to get to s = "220628-17:34:38,962 nipype.interface INFO: stdout 2022-06-28T17:34:38.962155:113.157831", since I cannot convert the output to a string. – Dr. S Jun 28 '22 at 16:07
  • @dr-s Please add more details about your code by editing the question – Cardstdani Jun 28 '22 at 16:12
  • does this help more? – Dr. S Jun 28 '22 at 17:15