diff --git a/dicom2nifti/convert_philips.py b/dicom2nifti/convert_philips.py index a033e34..ad5a917 100644 --- a/dicom2nifti/convert_philips.py +++ b/dicom2nifti/convert_philips.py @@ -259,8 +259,9 @@ def _multiframe_to_nifti(dicom_input, output_file): logger.info('Creating bval en bvec files') bval_file = '%s/%s.bval' % (base_path, base_name) bvec_file = '%s/%s.bvec' % (base_path, base_name) - bval, bvec, bval_file, bvec_file = _create_bvals_bvecs(multiframe_dicom, bval_file, bvec_file, nii_image, - output_file) + nii_image, bval, bvec, bval_file, bvec_file = _create_bvals_bvecs( + multiframe_dicom, bval_file, bvec_file, nii_image, output_file + ) return {'NII_FILE': output_file, 'BVAL_FILE': bval_file, @@ -433,7 +434,7 @@ def _create_bvals_bvecs(multiframe_dicom, bval_file, bvec_file, nifti, nifti_fil bvals = None bvecs = None - return bvals, bvecs, bval_file, bvec_file + return nifti, bvals, bvecs, bval_file, bvec_file def _fix_diffusion_images(bvals, bvecs, nifti, nifti_file): @@ -441,7 +442,7 @@ def _fix_diffusion_images(bvals, bvecs, nifti, nifti_file): This function will remove the last timepoint from the nifti, bvals and bvecs if the last vector is 0,0,0 This is sometimes added at the end by philips """ - # if all zero continue of if the last bvec is not all zero continue + # if all zero continue or if the last bvec is not all zero continue if numpy.count_nonzero(bvecs) == 0 or not numpy.count_nonzero(bvals[-1]) == 0: # nothing needs to be done here return nifti, bvals, bvecs diff --git a/tests/data/philips_enhanced/dti/adc/adc_multiframe.dcm b/tests/data/philips_enhanced/dti/adc/adc_multiframe.dcm new file mode 100644 index 0000000..ff8afee Binary files /dev/null and b/tests/data/philips_enhanced/dti/adc/adc_multiframe.dcm differ diff --git a/tests/data/philips_enhanced/dti/adc_ground_truth.bval b/tests/data/philips_enhanced/dti/adc_ground_truth.bval new file mode 100644 index 0000000..c210eef --- /dev/null +++ b/tests/data/philips_enhanced/dti/adc_ground_truth.bval @@ -0,0 +1 @@ +0 1000 1000 diff --git a/tests/data/philips_enhanced/dti/adc_ground_truth.bvec b/tests/data/philips_enhanced/dti/adc_ground_truth.bvec new file mode 100644 index 0000000..6b29172 --- /dev/null +++ b/tests/data/philips_enhanced/dti/adc_ground_truth.bvec @@ -0,0 +1,3 @@ +0.0 -0.9939979910850525 -0.0821085199713707 +0.0 0.08382682502269745 -0.9962577819824219 +0.0 -0.07029245793819427 -0.02699320949614048 diff --git a/tests/data/philips_enhanced/dti/adc_ground_truth.nii.gz b/tests/data/philips_enhanced/dti/adc_ground_truth.nii.gz new file mode 100644 index 0000000..d12e17f Binary files /dev/null and b/tests/data/philips_enhanced/dti/adc_ground_truth.nii.gz differ diff --git a/tests/test_data.py b/tests/test_data.py index 4935d1a..0bde51f 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -95,6 +95,9 @@ PHILIPS_ENHANCED_DTI_IMPLICIT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data', 'philips_enhanced', 'dti', '001_implicit') +PHILIPS_ENHANCED_DTI_ADC = os.path.join(os.path.dirname(os.path.abspath(__file__)), + 'data', 'philips_enhanced', 'dti', 'adc') + PHILIPS_ENHANCED_FMRI = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data', 'philips_enhanced', 'fmri', '001') diff --git a/tests/test_philips.py b/tests/test_philips.py index fe8d119..393293b 100644 --- a/tests/test_philips.py +++ b/tests/test_philips.py @@ -20,7 +20,7 @@ import dicom2nifti.settings as settings from dicom2nifti.common import read_dicom_directory from dicom2nifti.exceptions import ConversionError -from tests.test_tools import assert_compare_nifti, assert_compare_bval, assert_compare_bvec, ground_thruth_filenames +from tests.test_tools import assert_compare_nifti, assert_compare_bval, assert_compare_bvec, ground_thruth_filenames, assert_compare_length_of_nii_to_bval class TestConversionPhilips(unittest.TestCase): @@ -109,6 +109,22 @@ def test_diffusion_imaging(self): read_dicom_directory(test_data.PHILIPS_ENHANCED_DTI_IMPLICIT), os.path.join(tmp_output_dir, 'test.nii.gz')) + # check PHILIPS_ENHANCED_ADC + results = convert_philips.dicom_to_nifti(read_dicom_directory(test_data.PHILIPS_ENHANCED_DTI_ADC), + os.path.join(tmp_output_dir, 'test.nii.gz')) + assert_compare_nifti(results['NII_FILE'], + ground_thruth_filenames(test_data.PHILIPS_ENHANCED_DTI_ADC)[0]) + self.assertTrue(isinstance(results['NII'], nibabel.nifti1.Nifti1Image)) + assert_compare_bval(results['BVAL_FILE'], + ground_thruth_filenames(test_data.PHILIPS_ENHANCED_DTI_ADC)[2]) + self.assertTrue(isinstance(results['BVAL'], numpy.ndarray)) + assert_compare_bval(results['BVEC_FILE'], + ground_thruth_filenames(test_data.PHILIPS_ENHANCED_DTI_ADC)[3]) + self.assertTrue(isinstance(results['BVEC'], numpy.ndarray)) + # Also check the length of the BVAL & BVEC + assert_compare_length_of_nii_to_bval(results['NII_FILE'],results['BVAL_FILE']) + + finally: shutil.rmtree(tmp_output_dir) diff --git a/tests/test_tools.py b/tests/test_tools.py index ea91357..97b1b52 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -68,3 +68,11 @@ def assert_compare_bvec(bvec_file_1, bvec_file_2): equal = numpy.allclose(bvec_1, bvec_2) if not equal: raise Exception('bvecs not equal\n%s\n%s' %(numpy.array2string(bvec_1), numpy.array2string(bvec_2))) + +def assert_compare_length_of_nii_to_bval(nifti_file, bval_file): + nifti = nibabel.load(nifti_file) + nifti_num_of_directions = nifti.header['dim'][4] + bval_1 = numpy.loadtxt(bval_file) + bval_num_of_directions = len(bval_1) + if not nifti_num_of_directions == bval_num_of_directions: + raise Exception('BVAL not equal length to the NIfTI\n%s\n%s' %(bval_num_of_directions, nifti_num_of_directions))