Open clead6 opened 1 week ago
I attempted to use numpy.astype to convert uint16. The current attempt is successful. I hope this patch can be included in the package in the future.
dicom2nifti/common.py
else:
# Determine required range
data = data.astype(numpy.int16)
minimum_required, maximum_required = data.min(), data.max()
minimum_required = min([minimum_required, minimum_required * rescale_slope + rescale_intercept,
maximum_required * rescale_slope + rescale_intercept])
maximum_required = max([maximum_required, minimum_required * rescale_slope + rescale_intercept,
maximum_required * rescale_slope + rescale_intercept])
Another approach is to downgrade the numpy package. I try numpy==1.26.4 can solved this problem
Hi, I am getting this overflow error when converting my image from DICOM to NIFTI. I had quick look through your code, I think one of your conditions for converting to float must be missing when doing the scaling. Let me know if you can fix this.
OverflowError Traceback (most recent call last) Cell In[2], line 4 2 dicom_path = Path("data/practical1/CT-PET-VI-02/CT_for_PET/") 3 output_path = Path("data/practical1/ct_for_pet.nii.gz") ----> 4 dicom2nifti.dicom_series_to_nifti(dicom_path, output_path, reorient_nifti=False)
File c:\Users\clead\anaconda3\envs\dicom2nifti\Lib\site-packages\dicom2nifti\convert_dicom.py:79, in dicom_series_to_nifti(original_dicom_directory, output_file, reorient_nifti) 75 shutil.copytree(original_dicom_directory, dicom_directory) 77 dicom_input = common.read_dicom_directory(dicom_directory) ---> 79 return dicom_array_to_nifti(dicom_input, output_file, reorient_nifti) 81 except AttributeError as exception: 82 raise exception
File c:\Users\clead\anaconda3\envs\dicom2nifti\Lib\site-packages\dicom2nifti\convert_dicom.py:119, in dicom_array_to_nifti(dicom_list, output_file, reorient_nifti) 116 vendor = _get_vendor(dicom_list) 118 if vendor == Vendor.GENERIC: --> 119 results = convert_generic.dicom_to_nifti(dicom_list, output_file) 120 elif vendor == Vendor.SIEMENS: 121 results = convert_siemens.dicom_to_nifti(dicom_list, output_file)
File c:\Users\clead\anaconda3\envs\dicom2nifti\Lib\site-packages\dicom2nifti\convert_generic.py:274, in dicom_to_nifti(dicom_input, output_file) 270 nii_image, max_slice_increment = _convert_slice_incement_inconsistencies(dicom_input) 271 # do the normal conversion 272 else: 273 # Get data; originally z,y,x, transposed to x,y,z --> 274 data = common.get_volume_pixeldata(dicom_input) 276 affine, max_slice_increment = common.create_affine(dicom_input) 278 # Convert to nifti
File c:\Users\clead\anaconda3\envs\dicom2nifti\Lib\site-packages\dicom2nifti\common.py:313, in get_volume_pixeldata(sortedslices) 310 for i, slice in enumerate(sorted_slices): 311 # create copy so we don't load all pixel data on the original slice that is kept in memory 312 slicecopy = copy.deepcopy(slice) --> 313 slice_data = _get_slice_pixeldata(slice_copy) 314 del slice_copy 315 if combined_dtype is None:
File c:\Users\clead\anaconda3\envs\dicom2nifti\Lib\site-packages\dicom2nifti\common.py:355, in _get_slice_pixeldata(dicom_slice) 353 data[data > max_value] = numpy.bitwise_or(data[data > max_value], invert_value) 354 pass --> 355 return apply_scaling(data, dicom_slice)
File c:\Users\clead\anaconda3\envs\dicom2nifti\Lib\site-packages\dicom2nifti\common.py:509, in apply_scaling(data, dicom_headers) 500 rescale_intercept = dicom_headers.RescaleIntercept 501 # try: 502 # # this section can sometimes fail due to unknown private fields 503 # if private_scale_slope_tag in dicom_headers: (...) 507 # except: 508 # pass --> 509 return do_scaling(data, rescale_slope, rescale_intercept) 510 else: 511 return data
File c:\Users\clead\anaconda3\envs\dicom2nifti\Lib\site-packages\dicom2nifti\common.py:540, in do_scaling(data, rescale_slope, rescale_intercept, private_scale_slope, private_scale_intercept) 537 else: 538 # Determine required range 539 minimum_required, maximum_required = data.min(), data.max() --> 540 minimum_required = min([minimum_required, minimum_required rescale_slope + rescale_intercept, 541 maximum_required rescale_slope + rescale_intercept]) 542 maximum_required = max([maximum_required, minimum_required rescale_slope + rescale_intercept, 543 maximum_required rescale_slope + rescale_intercept]) 545 # Determine required datatype from that
OverflowError: Python integer -1024 out of bounds for uint16