Batch_processing

Hello @jdtournier ,
Suppose I have 5 data like this:
/User/study/patient_01/dti_01.nii.gz
/User/study/patient_02/dti_02.nii.gz
/User/study/patient_03/dti_03.nii.gz
/User/study/patient_04/dti_04.nii.gz
/User/study/patient_05/dti_05.nii.gz

Now, I would like to denoise these data using dwidenoise and I would like to obtain like this:
/User/study/patient_01/denoise_01.nii.gz
/User/study/patient_02/denoise_02.nii.gz
/User/study/patient_03/denoise_03.nii.gz
/User/study/patient_04/denoise_04.nii.gz
/User/study/patient_05/denoise_05.nii.gz

Would you please guide me through this?

Many Thanks,
Suren

In general, I would always recommend using identical names within subject folders, to avoid the kinds of bash gymnastics required to deal with a situation like this…

If you relied on the folder to identify the subject, and kept names within the folder consistent, like so:

β”œβ”€β”€ patient01
β”‚   └── dti.nii.gz
β”œβ”€β”€ patient02
β”‚   └── dti.nii.gz
β”œβ”€β”€ patient03
β”‚   └── dti.nii.gz
β”œβ”€β”€ patient04
β”‚   └── dti.nii.gz
└── patient05
    └── dti.nii.gz

then this would be a trivial matter of:

for_each patient* : dwidenoise IN/dti.nii.gz IN/denoise.nii.gz

or using regular bash:

for pat in patient*; do dwidenoise $pat/dti.nii.gz $pat/denoise.nii.gz ; done

But with the structure you’re suggesting, things get a bit trickier. I can’t think of a way to do this with our for_each command, but it can be done using regular bash – it’s just a bit of a kludge:

for pat in patient*; do 
  n=${pat#patient}; 
  dwidenoise $pat/dti_$n.nii.gz $pat/denoise_$n.nii.gz; 
done

which should give you the expected outcome:

β”œβ”€β”€ patient01
β”‚   β”œβ”€β”€ denoise_01.nii.gz
β”‚   └── dti_01.nii.gz
β”œβ”€β”€ patient02
β”‚   β”œβ”€β”€ denoise_02.nii.gz
β”‚   └── dti_02.nii.gz
β”œβ”€β”€ patient03
β”‚   β”œβ”€β”€ denoise_03.nii.gz
β”‚   └── dti_03.nii.gz
β”œβ”€β”€ patient04
β”‚   β”œβ”€β”€ denoise_04.nii.gz
β”‚   └── dti_04.nii.gz
└── patient05
    β”œβ”€β”€ denoise_05.nii.gz
    └── dti_05.nii.gz

Things can get easily get messier than this for more complex commands or even slightly different naming schemes though, so again, I would recommend using a simpler structure with identical names within folders – it really makes things a lot easier to handle down the line…

Cheers,
Donald.

1 Like