1

So I have code that completes an analysis for me on imaging data. This code is working fine except that the data is not being outputted into the correct folder. Instead, it is being outputted one folder too high. This is something I obviously want to fix in the long run, but as I am under certain time constraints I want to input code into my script that will simply move the files to the correct folder/directory that I have created. I have tried the mv and shutil commands but they don't seem to be working. I would be grateful if someone had a suggestion for how to fix/improve my method of moving these files to the correct location. If someone has a suggestion for why the files are being outputted to the wrong directory as well that would be awesome. I am relatively new to coding and no expert so please forgive any obvious mistakes. Thank you.

This is where I set up my directories

subject_dir = os.getcwd()
dti_dir = os.path.abspath( os.path.join(subject_dir, 'dti'))
dti_input_dir = os.path.abspath( os.path.join(dti_dir, 'input'))

This is where I entered a few shortcuts

eddy_prefix    = 'eddy'
input_path      = dti_input_dir
output_prefix   = 'dtifit'
output_path     = '../dti/dtifit'
output_basename = os.path.abspath(os.path.join(dti_dir, output_path))
infinite_path = os.path.join(os.getenv('INFINITE_PATH'), 'infinite')
dti30 = 'dti30.nii.gz'
dti30_brain = 'bet.b0.dti30.nii.gz'
dti30_mask = 'bet.b0.dti30_mask.nii.gz'

This is where I ran my test.

The test is being run, but my data is being outpputed in dti_dir and not output_basename (this is my second question)

dti = fsl.DTIFit()
dti.inputs.dwi = os.path.join(input_path, eddy_prefix + '.nii.gz')
dti.inputs.bvecs = os.path.join(input_path, eddy_prefix + '.eddy_rotated_bvecs')
dti.inputs.bvals = os.path.abspath(os.path.join(infinite_path, 'dti30.bval')) 
dti.inputs.base_name = output_basename
dti.inputs.mask = os.path.join(input_path, dti30_mask)
dti.cmdline

Creating output directory if doesn't exist.

This is working fine and the directory is created in the proper location.

if not os.path.exists(output_basename):
    os.makedirs(output_basename)

print('DTI Command line')
print(dti.cmdline)

res = dti.run()

exit()

print('DTIFIT Complete!')

Here I try to move the files and I get the error: IOError: [Errno 2] No such file or directory: even though I know the files exist

src = dti_dir
dst = output_basename

files = os.listdir(src)

for f in files:
    if (f.startswith("dtifit_")):
        shutil.move(f, dst)
Zong
  • 6,160
  • 5
  • 32
  • 46

1 Answers1

0

Your problem is most likely in the very first line. You may want to change that to be more exact, pointing to an exact directory rather than os.getcwd(). os.getcwd() will point to wherever the process is executing from, wherever you kicked off your python run, so it could change. You probably want to hard-code it somehow.

For example, you could use something like this, to point to the directory that the file lives in: Find current directory and file's directory

Another thing you could consider doing is printing out dst in your last few lines and seeing if it's what you expect. You could also use PDB to inspect that value live: https://pymotw.com/2/pdb/

Edit:

Your problem is using a relative path with shutil.move(). You should make sure that the path is absolute, please see this answer for more info:

stackoverflow.com/a/22230227/7299836

Community
  • 1
  • 1
  • Hi Christopher, Thanks for your quick response. For the first part of your suggestions, I forgot to mention one thing (very new to this). I have about 200 sets of data I run this code on so I create a .txt file with the proper files so that is where the os.getcwd() comes into play. I then run it in a for loop in my command window. It looks something like this: for ii in $(cat dtifit_test.txt); do cd ${INFINITE_MRI_DATA}/${ii}; nohup nipype_dtifit.py; done. For your second part, I did print the variables and they are what I want them to be. I will also look into PDB thank you for that! – The-Neuroscientist Apr 11 '17 at 20:37
  • Maybe check out this answer: http://stackoverflow.com/a/22230227/7299836 Basically, shutil expects a full path to the file, not just the file name that you're passing in! – Christopher Apple Apr 11 '17 at 21:03
  • Thank you!!! That link was great I was able to fix it. I didn't realize I needed that full path for the file as well. – The-Neuroscientist Apr 12 '17 at 15:19
  • I'll add it to the original answer. Please upvote and mark as accepted if it helped. Cheers! – Christopher Apple Apr 12 '17 at 15:28