Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recovering stdout/stderr from python wrapper #773

Closed
nbeliy opened this issue Jan 2, 2024 · 4 comments · Fixed by #774
Closed

Recovering stdout/stderr from python wrapper #773

nbeliy opened this issue Jan 2, 2024 · 4 comments · Fixed by #774

Comments

@nbeliy
Copy link

nbeliy commented Jan 2, 2024

In current implementation of the Python wrapper, the output of the dcm2niix is printed directly to stdout.
However it make it more difficult to recover output, if dcm2niix is run inside the pipeline with a logging into a file.

I would propose to add parameters which will be forwarded directly to subprocess.run, something like:

def main(args=None, **run_params):
    if args is None:
        import sys
        args = sys.argv[1:]
    from subprocess import run
    return run(["dcm2niix"] + args, **run_params)

It would allow to capture output of the dcm2niix:

from dcm2niix import main
res = main([args] + [path],
                    capture_output=True, text=True)
print(res.stdout)

It will also allow to recover the exit code and detect if conversion was successful.

@neurolabusc
Copy link
Collaborator

This repository does not include any reference Python wrappers, though the home page links to other wrappers that depend on dcm2niix. Therefore, I think your message is related to a different repository.

I am not a Python expert, but it is unclear what feature you need that does not already exist with the Python subprocess methods, for example, this ChatGPT generated script appears to intercept the outputs of dcm2niix nicely:

import subprocess

def run_dcm2niix(dcm2niix_path, dicom_path):
    # Command to call dcm2niix with specified arguments
    command = [dcm2niix_path, '-f', '%s_%p', dicom_path]
    try:
        # Run the command and capture the output
        result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)
        # Split the output into a list of strings
        output_lines = result.stdout.split('\n')
        return output_lines
    except subprocess.CalledProcessError as e:
        # Handle any errors that occur during the execution
        print(f"Error running dcm2niix: {e}")
        return []

# Example usage
dcm2niix_path = '/path/to/dcm2niix'  # Replace with the actual path to dcm2niix executable
dicom_path = '/path/to/DICOM'          # Replace with the actual path to your DICOM directory

output = run_dcm2niix(dcm2niix_path, dicom_path)

# Print the captured output
for line in output:
    print(line)

@nbeliy
Copy link
Author

nbeliy commented Jan 2, 2024

Hi @neurolabusc, I was refering to __init__.py file, which allow to call dcm2niix from inside the python (that's why I called it wrapper).

@neurolabusc
Copy link
Collaborator

@casperdcl do you want to modify this file to respond to this issue?

casperdcl added a commit to casperdcl/dcm2niix that referenced this issue Jan 3, 2024
casperdcl added a commit to casperdcl/dcm2niix that referenced this issue Jan 3, 2024
@nbeliy
Copy link
Author

nbeliy commented Jan 4, 2024

Thanks for the fast modifications)

@nbeliy nbeliy closed this as completed Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants