How to run dcm2bids from a python file

Hello all,
I am writing a small pipeline to process our raw data. Among other things, this pipeline uses dcm2bids. Until now, I have been using subprocess.run("[dcm2bids command]", shell = True) to run the command from a python file, but I am running into issues related to the conda environment when I do so. To get around this, I have attempted something akin to:

from dcm2bids import Dcm2bids
dcm_2_bids = Dcm2bids(dicom_dir = dicom_dir, 
                        participant = "001", 
                        config = config, 
                        output_dir = output, 
                        clobber = True, 
                        forceDcm2niix = True)
dcm_2_bids.run()

However the output of the program does not match the output given by using subprocess.run. Why is this? I’m not sure that this would be the best way to run dcm2bids from a python file, so I’m open to other ideas.

Hi,

It would help to know more about what the problems are with both approaches.

That being said, it might be easier if you prepare a separate bash script to run dcm2bids, and then call it in your python script, e.g.:

import subprocess
subprocess.check_call(["./script.sh", arg1, arg2, arg3], shell=True)

Best,
Steven

Thanks for the response!

It would help to know more about what the problems are with both approaches

At first, using os.system worked well, however now the program complains that I am using conda as the application (I am not):

ERROR: The install method you used for conda--probably either `pip install conda`
or `easy_install conda`--is not compatible with using conda as an application.
If your intention is to install conda as a standalone application, currently
supported install methods include the Anaconda installer and the miniconda
installer.  You can download the miniconda installer from
https://conda.io/miniconda.html.

I’m not sure what I changed to bring about this error. The script continues to run notwithstanding this error, however if I use subprocess.run() a la:

from subprocess import run
run('conda activate dcm2bids', shell = True)
run(f'dcm2bids --forceDcm2niix --clobber -d {args.dicom_dir} -c {dcm2bids}/config_{participant}.json -p {participant}', shell = True)

I run into this error:

subprocess.CalledProcessError: Command '['dcm2niix', '-b', 'y', '-ba', 'y', '-z', 'y', '-f', '%3s_%f_%p_%t', '-o', PosixPath('/home/darwin/fMRI_pp/SUDNU/tmp_dcm2bids/sub-001'), PosixPath('/home/darwin/fMRI_raw/SUDNU/001')]' returned non-zero exit status 7.

Using subprocess.check_call gives this similar but slightly different error:
subprocess.CalledProcessError: Command '['./home/darwin/.local/bin/pybids-converter_helper.sh', '/home/darwin/fMRI_raw/SUDNU/001', '001']' returned non-zero exit status 127.

Where pybids-converter_helper.sh looks like this:

. /home/darwin/miniconda3/etc/profile.d/conda.sh
conda activate dcm2bids
dcm2bids --forceDcm2niix --clobber -d $1 -c /home/darwin/dcm2bids/code/config_$2.json -p $2
conda deactivate

Running dcm2bids in the same way as the codeblock in the OP does not give an error, however there is no output suggesting that the program actually converts the files:

WARNING:dcm2bids.version:Your using dcm2bids version 2.1.7
WARNING:dcm2bids.version:A new version exists : 2.1.9
WARNING:dcm2bids.version:Check https://github.com/unfmontreal/Dcm2Bids
WARNING:dcm2bids.dcm2niix:Previous dcm2niix directory output found:
WARNING:dcm2bids.dcm2niix:/home/darwin/Me/tmp_dcm2bids/sub-001
WARNING:dcm2bids.dcm2niix:'force' argument is set to True
WARNING:dcm2bids.dcm2niix:Cleaning the previous directory and running dcm2niix

After spending much time trying to find the issue, I decided to simply reinstall dcm2bids. This seems to have fixed my problem, as I can now run dcm2bids as in the OP.