def assert_compare_nifti(nifti_file_1, nifti_file_2): logging.info("%s %s" % (nifti_file_1, nifti_file_2)) work_dir = tempfile.mkdtemp() try: tmp_nifti_file_1 = os.path.join(work_dir, os.path.basename(nifti_file_1)) tmp_nifti_file_2 = os.path.join(work_dir, os.path.basename(nifti_file_2)) image_reorientation.reorient_image(nifti_file_1, tmp_nifti_file_1) image_reorientation.reorient_image(nifti_file_2, tmp_nifti_file_2) nifti_1 = nibabel.load(tmp_nifti_file_1) nifti_2 = nibabel.load(tmp_nifti_file_2) # check the affine if not numpy.allclose(nifti_1.affine, nifti_2.affine): raise Exception('affine mismatch') # check the data if nifti_1.get_data_dtype() != nifti_2.get_data_dtype(): raise Exception('dtype mismatch') if not numpy.allclose(nifti_1.get_data(), nifti_2.get_data()): raise Exception('data mismatch') except: shutil.rmtree(work_dir)
def compare_nifti(nifti_file_1, nifti_file_2): result = True logging.info("%s %s" % (nifti_file_1, nifti_file_2)) work_dir = tempfile.mkdtemp() try: tmp_nifti_file_1 = os.path.join(work_dir, os.path.basename(nifti_file_1)) tmp_nifti_file_2 = os.path.join(work_dir, os.path.basename(nifti_file_2)) image_reorientation.reorient_image(nifti_file_1, tmp_nifti_file_1) image_reorientation.reorient_image(nifti_file_2, tmp_nifti_file_2) nifti_1 = nibabel.load(tmp_nifti_file_1) nifti_2 = nibabel.load(tmp_nifti_file_2) # check the affine if not numpy.allclose(nifti_1.affine, nifti_2.affine): logging.warning('affine mismatch') result = False # check the data if nifti_1.get_data_dtype() != nifti_2.get_data_dtype(): logging.warning('dtype mismatch') result = False if not (nifti_1.get_data() == nifti_2.get_data()).all(): logging.warning('data mismatch') result = False except: shutil.rmtree(work_dir) return result
def dicom_array_to_nifti(dicom_list, output_file, reorient_nifti=True): """ Converts dicom single series (see pydicom) to nifty, mimicking SPM Examples: See unit test will return a dictionary containing - the NIFTI under key 'NIFTI' - the NIFTI file path under 'NII_FILE' - the BVAL file path under 'BVAL_FILE' (only for dti) - the BVEC file path under 'BVEC_FILE' (only for dti) IMPORTANT: If no specific sequence type can be found it will default to anatomical and try to convert. You should check that the data you are trying to convert is supported by this code Inspired by http://nipy.sourceforge.net/nibabel/dicom/spm_dicom.html Inspired by http://code.google.com/p/pydicom/source/browse/source/dicom/contrib/pydicom_series.py :param reorient_nifti: if True the nifti affine and data will be updated so the data is stored LAS oriented :param output_file: file path to write to :param dicom_list: list with uncompressed dicom objects as read by pydicom """ # copy files so we can can modify without altering the original if not are_imaging_dicoms(dicom_list): raise ConversionValidationError('NON_IMAGING_DICOM_FILES') vendor = _get_vendor(dicom_list) if vendor == Vendor.GENERIC: results = convert_generic.dicom_to_nifti(dicom_list, output_file) elif vendor == Vendor.SIEMENS: results = convert_siemens.dicom_to_nifti(dicom_list, output_file) elif vendor == Vendor.GE: results = convert_ge.dicom_to_nifti(dicom_list, output_file) elif vendor == Vendor.PHILIPS: results = convert_philips.dicom_to_nifti(dicom_list, output_file) elif vendor == Vendor.HITACHI: results = convert_hitachi.dicom_to_nifti(dicom_list, output_file) else: raise ConversionValidationError("UNSUPPORTED_DATA") # do image reorientation if needed if reorient_nifti or settings.resample: image_reorientation.reorient_image(results['NII_FILE'], results['NII_FILE']) # resampling needs to be after reorientation if settings.resample: if not common.is_orthogonal_nifti(results['NII_FILE']): resample.resample_single_nifti(results['NII_FILE']) return results
def main(): for root, dir_names, _ in os.walk(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'dicom2nifti', 'tests', 'data')): settings.disable_validate_multiframe_implicit() # New directory for dir_name in dir_names: dir_path = os.path.join(root, dir_name) if subdir_count(dir_path) > 0: continue # not processing because not lowest level of directory print(dir_path) output_file = dir_path + '_ground_truth.nii.gz' reoriented_file = dir_path + '_ground_truth_reoriented.nii.gz' # noinspection PyBroadException try: dicom2nifti.dicom_series_to_nifti(dir_path, output_file, False) image_reorientation.reorient_image(output_file, reoriented_file) except: # explicitly capturing everything here pass
def assert_compare_nifti(nifti_file_1, nifti_file_2): logging.info("%s %s" % (nifti_file_1, nifti_file_2)) work_dir = tempfile.mkdtemp() try: tmp_nifti_file_1 = os.path.join(work_dir, os.path.basename(nifti_file_1)) tmp_nifti_file_2 = os.path.join(work_dir, os.path.basename(nifti_file_2)) image_reorientation.reorient_image(nifti_file_1, tmp_nifti_file_1) image_reorientation.reorient_image(nifti_file_2, tmp_nifti_file_2) nifti_1 = nibabel.load(tmp_nifti_file_1) nifti_2 = nibabel.load(tmp_nifti_file_2) # check the affine if not numpy.allclose(nifti_1.affine, nifti_2.affine): raise Exception('affine mismatch') # check the data if nifti_1.get_data_dtype() != nifti_2.get_data_dtype(): raise Exception('dtype mismatch') if not numpy.allclose(nifti_1.get_data(), nifti_2.get_data()): raise Exception('data mismatch') except: shutil.rmtree(work_dir) raise
def generate_ground_truth(dicom_directory, output_file, reoriented_file): if not os.path.isfile(output_file): dicom2nifti.dicom_series_to_nifti(dicom_directory, output_file, False) image_reorientation.reorient_image(output_file, reoriented_file)