Writing output_file as optional in reorient_image #121
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Relates to issue #33
Problem:
If I try to do something like
nii = dicom2nifti.dicom_series_to_nifti(dcm_path, output_file=None)
, I'll get aImageFileError: Filespec "None" does not look right for class <class 'nibabel.nifti1.Nifti1Image'>
error (for the complete error, see below).Solution:
The error comes from the lack of a simple check in the
dicom2nifti.image_reorientation.reorient_image
function.The provided solution adds this simple check wheather to overwrite the
output_file
.Full error:
TypesFilenamesError Traceback (most recent call last)
File ~/miniconda3/envs/brain/lib/python3.8/site-packages/nibabel/filebasedimages.py:299, in FileBasedImage.filespec_to_file_map(klass, filespec)
298 try:
--> 299 filenames = types_filenames(
300 filespec, klass.files_types,
301 trailing_suffixes=klass._compressed_suffixes)
302 except TypesFilenamesError:
File ~/miniconda3/envs/brain/lib/python3.8/site-packages/nibabel/filename_parser.py:114, in types_filenames(template_fname, types_exts, trailing_suffixes, enforce_extensions, match_case)
113 if not isinstance(template_fname, str):
--> 114 raise TypesFilenamesError('Need file name as input '
115 'to set_filenames')
116 if template_fname.endswith('.'):
TypesFilenamesError: Need file name as input to set_filenames
During handling of the above exception, another exception occurred:
ImageFileError Traceback (most recent call last)
Input In [16], in <cell line: 1>()
----> 1 nii["NII"].to_filename(None)
File ~/miniconda3/envs/brain/lib/python3.8/site-packages/nibabel/filebasedimages.py:332, in FileBasedImage.to_filename(self, filename)
318 def to_filename(self, filename):
319 """ Write image to files implied by filename string
320
321 Parameters
(...)
330 None
331 """
--> 332 self.file_map = self.filespec_to_file_map(filename)
333 self.to_file_map()
File ~/miniconda3/envs/brain/lib/python3.8/site-packages/nibabel/filebasedimages.py:303, in FileBasedImage.filespec_to_file_map(klass, filespec)
299 filenames = types_filenames(
300 filespec, klass.files_types,
301 trailing_suffixes=klass._compressed_suffixes)
302 except TypesFilenamesError:
--> 303 raise ImageFileError(
304 f'Filespec "{filespec}" does not look right for class {klass}')
305 file_map = {}
306 for key, fname in filenames.items():
ImageFileError: Filespec "None" does not look right for class <class 'nibabel.nifti1.Nifti1Image'>