Exemplo n.º 1
0
 def run(self):
     accession_number = self.data["accession_number"]
     patient_id = self.data["patient_id"]
     accession_dir = "work/results/{}/{}".format(patient_id, accession_number)
     if not os.path.exists(accession_dir):
         os.makedirs(accession_dir)
     dicom_series_to_nifti(self.data["images_dir"], accession_dir + "/source.nii.gz")
     with self.output().open("w") as outfile:
         outfile.write("Converted to nifti")
Exemplo n.º 2
0
    def test_resampling(self):
        tmp_output_dir = tempfile.mkdtemp()
        try:

            results = convert_dicom.dicom_series_to_nifti(
                test_data.FAILING_ORHTOGONAL,
                os.path.join(tmp_output_dir, 'test.nii.gz'), False)
            self.assertTrue(os.path.isfile(results['NII_FILE']))

        finally:
            shutil.rmtree(tmp_output_dir)
Exemplo n.º 3
0
    def test_anatomical(self):
        tmp_output_dir = tempfile.mkdtemp()
        try:
            results = convert_dicom.dicom_series_to_nifti(
                test_data.SIEMENS_ANATOMICAL,
                os.path.join(tmp_output_dir, 'test.nii.gz'), False)
            assert compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(
                    test_data.SIEMENS_ANATOMICAL)[0]) is True

            results = convert_dicom.dicom_series_to_nifti(
                test_data.SIEMENS_ANATOMICAL,
                os.path.join(tmp_output_dir, 'test.nii.gz'), True)
            assert compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(
                    test_data.SIEMENS_ANATOMICAL)[1]) is True

            results = convert_dicom.dicom_series_to_nifti(
                test_data.SIEMENS_ANATOMICAL_IMPLICIT,
                os.path.join(tmp_output_dir, 'test.nii.gz'), False)
            assert compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(
                    test_data.SIEMENS_ANATOMICAL_IMPLICIT)[0]) is True

            results = convert_dicom.dicom_series_to_nifti(
                test_data.SIEMENS_ANATOMICAL_IMPLICIT,
                os.path.join(tmp_output_dir, 'test.nii.gz'), True)
            assert compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(
                    test_data.SIEMENS_ANATOMICAL_IMPLICIT)[1]) is True

            results = convert_dicom.dicom_series_to_nifti(
                test_data.GENERIC_ANATOMICAL,
                os.path.join(tmp_output_dir, 'test.nii.gz'), False)
            assert compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(
                    test_data.GENERIC_ANATOMICAL)[0]) is True

            results = convert_dicom.dicom_series_to_nifti(
                test_data.GENERIC_ANATOMICAL,
                os.path.join(tmp_output_dir, 'test.nii.gz'), True)
            assert compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(
                    test_data.GENERIC_ANATOMICAL)[1]) is True

            results = convert_dicom.dicom_series_to_nifti(
                test_data.GENERIC_ANATOMICAL_IMPLICIT,
                os.path.join(tmp_output_dir, 'test.nii.gz'), False)
            assert compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(
                    test_data.GENERIC_ANATOMICAL_IMPLICIT)[0]) is True

            results = convert_dicom.dicom_series_to_nifti(
                test_data.GENERIC_ANATOMICAL_IMPLICIT,
                os.path.join(tmp_output_dir, 'test.nii.gz'), True)
            assert compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(
                    test_data.GENERIC_ANATOMICAL_IMPLICIT)[1]) is True

            results = convert_dicom.dicom_series_to_nifti(
                test_data.GENERIC_COMPRESSED,
                os.path.join(tmp_output_dir, 'test.nii.gz'), False)
            assert compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(
                    test_data.GENERIC_COMPRESSED)[0]) is True

            results = convert_dicom.dicom_series_to_nifti(
                test_data.GENERIC_COMPRESSED,
                os.path.join(tmp_output_dir, 'test.nii.gz'), True)
            assert compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(
                    test_data.GENERIC_COMPRESSED)[1]) is True

            results = convert_dicom.dicom_series_to_nifti(
                test_data.HITACHI_ANATOMICAL,
                os.path.join(tmp_output_dir, 'test.nii.gz'), True)
            assert compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(
                    test_data.HITACHI_ANATOMICAL)[1]) is True

        finally:
            shutil.rmtree(tmp_output_dir)
Exemplo n.º 4
0
    settings.disable_validate_slicecount()
    for key in inputFiles:
        dicomFolderPath = key
        outputFileName = inputFiles[ key ];
        if (outputFileName in mapOfFileNames):
            fileNo = mapOfFileNames[ outputFileName]
            mapOfFileNames[outputFileName] = fileNo + 1
        else:
            mapOfFileNames.setdefault(outputFileName, 1)

        outputFileName = outputFileName + "$$" + str(mapOfFileNames[ outputFileName])
        print('***', dicomFolderPath)
        print('--', outputDirectory, outputFileName)
        outputFileName = outputFileName + ".nii.gz"
        try:
            convert_dicom.dicom_series_to_nifti(key, os.path.join(outputDirectory, outputFileName),
                                              False)
        except dicom2nifti.exceptions.ConversionValidationError:
            print( " Error for file outputFileName", outputFileName )

    print( 'completed' )

def readFromTextFile( inputFilePath):
    inputFilesList = []
    file1 = open(inputFilePath, 'r')
    lines = file1.readlines()
    count = 0
    # Strips the newline character
    for line in lines:
        inputFilesList.append( line.strip() )

    return inputFilesList
Exemplo n.º 5
0
    def test_anatomical(self):
        tmp_output_dir = tempfile.mkdtemp()
        try:
            results = convert_dicom.dicom_series_to_nifti(
                test_data.SIEMENS_ANATOMICAL, None, False)
            self.assertTrue(results.get('NII_FILE') is None)
            self.assertTrue(
                isinstance(results['NII'], nibabel.nifti1.Nifti1Image))

            results = convert_dicom.dicom_series_to_nifti(
                test_data.SIEMENS_ANATOMICAL,
                os.path.join(tmp_output_dir, 'test.nii.gz'), False)
            assert_compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(test_data.SIEMENS_ANATOMICAL)[0])
            self.assertTrue(
                isinstance(results['NII'], nibabel.nifti1.Nifti1Image))

            results = convert_dicom.dicom_series_to_nifti(
                test_data.SIEMENS_ANATOMICAL,
                os.path.join(tmp_output_dir, 'test.nii.gz'), True)
            assert_compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(test_data.SIEMENS_ANATOMICAL)[1])
            self.assertTrue(
                isinstance(results['NII'], nibabel.nifti1.Nifti1Image))

            results = convert_dicom.dicom_series_to_nifti(
                test_data.SIEMENS_ANATOMICAL_IMPLICIT,
                os.path.join(tmp_output_dir, 'test.nii.gz'), False)
            assert_compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(
                    test_data.SIEMENS_ANATOMICAL_IMPLICIT)[0])
            self.assertTrue(
                isinstance(results['NII'], nibabel.nifti1.Nifti1Image))

            results = convert_dicom.dicom_series_to_nifti(
                test_data.SIEMENS_ANATOMICAL_IMPLICIT,
                os.path.join(tmp_output_dir, 'test.nii.gz'), True)
            assert_compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(
                    test_data.SIEMENS_ANATOMICAL_IMPLICIT)[1])
            self.assertTrue(
                isinstance(results['NII'], nibabel.nifti1.Nifti1Image))

            results = convert_dicom.dicom_series_to_nifti(
                test_data.GENERIC_ANATOMICAL,
                os.path.join(tmp_output_dir, 'test.nii.gz'), False)
            assert_compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(test_data.GENERIC_ANATOMICAL)[0])
            self.assertTrue(
                isinstance(results['NII'], nibabel.nifti1.Nifti1Image))

            results = convert_dicom.dicom_series_to_nifti(
                test_data.GENERIC_ANATOMICAL,
                os.path.join(tmp_output_dir, 'test.nii.gz'), True)
            assert_compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(test_data.GENERIC_ANATOMICAL)[1])
            self.assertTrue(
                isinstance(results['NII'], nibabel.nifti1.Nifti1Image))

            results = convert_dicom.dicom_series_to_nifti(
                test_data.GENERIC_ANATOMICAL_IMPLICIT,
                os.path.join(tmp_output_dir, 'test.nii.gz'), False)
            assert_compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(
                    test_data.GENERIC_ANATOMICAL_IMPLICIT)[0])
            self.assertTrue(
                isinstance(results['NII'], nibabel.nifti1.Nifti1Image))

            results = convert_dicom.dicom_series_to_nifti(
                test_data.GENERIC_ANATOMICAL_IMPLICIT,
                os.path.join(tmp_output_dir, 'test.nii.gz'), True)
            assert_compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(
                    test_data.GENERIC_ANATOMICAL_IMPLICIT)[1])
            self.assertTrue(
                isinstance(results['NII'], nibabel.nifti1.Nifti1Image))

            results = convert_dicom.dicom_series_to_nifti(
                test_data.GENERIC_NON_ISOTROPIC,
                os.path.join(tmp_output_dir, 'test.nii.gz'), False)
            assert_compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(test_data.GENERIC_NON_ISOTROPIC)[0])
            self.assertTrue(
                isinstance(results['NII'], nibabel.nifti1.Nifti1Image))

            results = convert_dicom.dicom_series_to_nifti(
                test_data.GENERIC_COMPRESSED,
                os.path.join(tmp_output_dir, 'test.nii.gz'), False)
            assert_compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(test_data.GENERIC_COMPRESSED)[0])
            self.assertTrue(
                isinstance(results['NII'], nibabel.nifti1.Nifti1Image))

            results = convert_dicom.dicom_series_to_nifti(
                test_data.GENERIC_COMPRESSED,
                os.path.join(tmp_output_dir, 'test.nii.gz'), True)
            assert_compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(test_data.GENERIC_COMPRESSED)[1])
            self.assertTrue(
                isinstance(results['NII'], nibabel.nifti1.Nifti1Image))

            results = convert_dicom.dicom_series_to_nifti(
                test_data.HITACHI_ANATOMICAL,
                os.path.join(tmp_output_dir, 'test.nii.gz'), True)
            assert_compare_nifti(
                results['NII_FILE'],
                ground_thruth_filenames(test_data.HITACHI_ANATOMICAL)[1])
            self.assertTrue(
                isinstance(results['NII'], nibabel.nifti1.Nifti1Image))

        finally:
            shutil.rmtree(tmp_output_dir)