def test_generate_entities_and_relationships(self):
     with open(TEST_IMAGE_PATH, "rb") as dcm:
         instance = ImportImage(dcm)
         instance.generate_entities_and_relationships()
     self.assertIsInstance(instance.image, Image)
     self.assertIsInstance(instance.image.series, Series)
     self.assertIsInstance(instance.image.series.study, Study)
     self.assertIsInstance(instance.image.series.patient, Patient)
 def test_create_image(self):
     # Create image
     with open(TEST_IMAGE_PATH, "rb") as dcm:
         instance = ImportImage(dcm)
         image = instance.create_image()
     # Test return type
     self.assertIsInstance(image, Image)
     # Test that the Image instance was initialized with a valid path
     image_points_to_real_file = os.path.isfile(image.dcm.path)
     self.assertTrue(image_points_to_real_file)
 def test_run_with_different_patient_and_different_study(self):
     with open(TEST_IMAGE_PATH, "rb") as dcm:
         image, _ = ImportImage(dcm).run()
     with open(TEST_DIFFERENT_PATIENT_IMAGE_PATH, "rb") as dcm:
         second_image, _ = ImportImage(dcm).run()
     self.assertNotEqual(image, second_image)
     self.assertNotEqual(image.series, second_image.series)
     self.assertNotEqual(image.series.study, second_image.series.study)
     self.assertNotEqual(image.series.patient, second_image.series.patient)
     shutil.rmtree(IMPORTED_DIR)
 def test_get_image_destination(self):
     with open(TEST_IMAGE_PATH, "rb") as dcm:
         instance = ImportImage(dcm)
         instance.image = instance.create_image()
     # The get_image_destination method used the Image instance number field
     # in order to create the file name.
     instance.image.number = 1
     destination = instance.get_image_destination()
     expected = "MRI/304848286/1.3.12.2.1107.5.2.43.66024.2018050112250992296484473.0.0.0/DICOM/1.dcm"
     self.assertEqual(destination, expected)
 def test_get_or_create_entity_with_existing_entities(self):
     # Create image
     with open(TEST_IMAGE_PATH, "rb") as dcm:
         instance = ImportImage(dcm)
         instance.image = instance.create_image()
     # Tests entities are returned and are not created
     for Entity in (Series, Study, Patient):
         entity, created = instance.get_or_create_entity(Entity)
         self.assertFalse(created)
         self.assertIsInstance(entity, Entity)
 def test_move_image_to_destination(self):
     # Create an image with updated fields
     with open(TEST_IMAGE_PATH, "rb") as dcm:
         instance = ImportImage(dcm)
         instance.image = instance.create_image()
         instance.image.update_fields_from_header(instance.image.header)
     # Test that the returned relative path is a valid path under MEDIA_ROOT
     relative_destination = instance.move_image_to_destination()
     destination = os.path.join(settings.MEDIA_ROOT, relative_destination)
     self.assertTrue(os.path.isfile(destination))
     shutil.rmtree(IMPORTED_DIR)
 def test_store_file(self):
     # Store file
     with open(TEST_IMAGE_PATH, "rb") as dcm:
         instance = ImportImage(dcm)
         temporary_name = instance.store_file()
     # Test that the returned path is valid and has the dcm extension
     temporary_path = os.path.join(settings.MEDIA_ROOT, temporary_name)
     is_file = os.path.isfile(temporary_path)
     has_correct_extension = temporary_name.endswith(
         DjangoDicomConfig.data_extension)
     self.assertTrue(is_file)
     self.assertTrue(has_correct_extension)
 def test_run_with_integrity_error_handler(self):
     # Normal run
     with open(TEST_IMAGE_PATH, "rb") as dcm:
         image, created = ImportImage(dcm).run()
     self.assertIsInstance(image, Image)
     self.assertTrue(created)
     # Adding the same instance should invoke the handle_integrity_error method
     with open(TEST_IMAGE_PATH, "rb") as dcm:
         same_image, created_again = ImportImage(dcm).run()
     self.assertEqual(image, same_image)
     self.assertFalse(created_again)
     shutil.rmtree(IMPORTED_DIR)
 def test_run_with_different_study_and_same_patient(self):
     # Create image
     with open(TEST_IMAGE_PATH, "rb") as dcm:
         image, _ = ImportImage(dcm).run()
     # Create image from same patient and study but different series
     with open(TEST_DIFFERENT_STUDY_IMAGE_PATH, "rb") as dcm:
         later_image, created = ImportImage(dcm).run()
     self.assertIsInstance(later_image, Image)
     self.assertTrue(created)
     # Test new series and study but existing patientwere related
     self.assertNotEqual(later_image.series, image.series)
     self.assertNotEqual(later_image.series.study, image.series.study)
     self.assertEqual(later_image.series.patient, image.series.patient)
     shutil.rmtree(IMPORTED_DIR)
 def test_get_or_create_entity_with_missing_entities(self):
     # Create image
     with open(TEST_DIFFERENT_PATIENT_IMAGE_PATH, "rb") as dcm:
         instance = ImportImage(dcm)
         instance.image = instance.create_image()
     # Test entities are returned and are not created
     for Entity in (Series, Study, Patient):
         # We are not saving to the database in order to avoid the required
         # relationships between the entites, as they are not managed here.
         entity, created = instance.get_or_create_entity(Entity, save=False)
         self.assertTrue(created)
         self.assertIsInstance(entity, Entity)
         # Make sure the created entity has updated fields
         self.assertIsNotNone(entity.uid)
 def test_run_with_different_series_but_same_patient_and_study(self):
     # Create image
     with open(TEST_IMAGE_PATH, "rb") as dcm:
         image, _ = ImportImage(dcm).run()
     # Create image from same patient and study but different series
     with open(TEST_DWI_IMAGE_PATH, "rb") as dcm:
         dwi, created = ImportImage(dcm).run()
     self.assertIsInstance(dwi, Image)
     self.assertTrue(created)
     # Test new series and existing patient and study were related
     self.assertEqual(dwi.series.uid, TEST_DWI_SERIES_FIELDS["uid"])
     self.assertNotEqual(dwi.series, image.series)
     self.assertEqual(dwi.series.study, image.series.study)
     self.assertEqual(dwi.series.patient, image.series.patient)
     shutil.rmtree(IMPORTED_DIR)
Beispiel #12
0
    def import_local_zip_archive(cls, path: str, verbose: bool = True) -> None:
        """
        Iterates over the files within a ZIP archive and imports any "*.dcm*" files.
        
        Parameters
        ----------
        path : str
            Local ZIP archive path.
        verbose : bool, optional
            Show a progressbar (default to True).
        """

        counter = {"created": 0, "existing": 0}
        zip_name = os.path.basename(path)
        if verbose:
            print(f"Reading {path}...")
        with zipfile.ZipFile(path, "r") as archive:
            for file_name in tqdm(archive.namelist(), disable=not verbose):
                if file_name.endswith(".dcm"):
                    with archive.open(file_name) as dcm_buffer:
                        _, created = ImportImage(dcm_buffer).run()
                        if created:
                            counter["created"] += 1
                        else:
                            counter["existing"] += 1
        msg = f"Successfully imported {counter['created']} new images from {zip_name}!"
        if counter["existing"]:
            msg += (
                f" ({counter['existing']} were found to already exist in the database)"
            )
        if verbose:
            print(msg)
Beispiel #13
0
 def import_dicom_image(self):
     dicom_image, _ = ImportImage(self.scan_file).run()
     scan, created = Scan.objects.get_or_create(dicom=dicom_image.series)
     if created:
         scan.update_fields_from_dicom()
     scan.subject = self.subject
     scan.save()
     return scan, created
 def test_run_with_existing_related_entities(self):
     # Create image
     with open(TEST_IMAGE_PATH, "rb") as dcm:
         image, created = ImportImage(dcm).run()
     self.assertIsInstance(image, Image)
     self.assertTrue(created)
     # Get the expected values
     series = Series.objects.get(uid=TEST_SERIES_FIELDS["uid"])
     study = Study.objects.get(uid=TEST_STUDY_FIELDS["uid"])
     patient = Patient.objects.get(uid=TEST_PATIENT_FIELDS["uid"])
     # Test equality
     self.assertEqual(image.series, series)
     self.assertEqual(image.series.study, study)
     self.assertEqual(image.series.patient, patient)
     shutil.rmtree(IMPORTED_DIR)
 def test_get_entity_uid_from_header(self):
     # Create image
     with open(TEST_IMAGE_PATH, "rb") as dcm:
         instance = ImportImage(dcm)
         instance.image = instance.create_image()
     # Get the expected values
     series_uid = instance.image.header.get_value("SeriesInstanceUID")
     study_uid = instance.image.header.get_value("StudyInstanceUID")
     patient_uid = instance.image.header.get_value("PatientID")
     # Get the returned values
     series_result = instance.get_entity_uid_from_header(Series)
     study_result = instance.get_entity_uid_from_header(Study)
     patient_result = instance.get_entity_uid_from_header(Patient)
     # Test equality
     self.assertEqual(series_result, series_uid)
     self.assertEqual(study_result, study_uid)
     self.assertEqual(patient_result, patient_uid)
Beispiel #16
0
    def import_local_dcm(cls, path: str) -> Image:
        """
        Reads the local DICOM image into an :class:`io.BufferedReader` and uses
        :class:`~django_dicom.data_import.import_image.ImportImage` to create an
        :class:`~django_dicom.models.Image` from it.
        
        Parameters
        ----------
        path : str
            The local path of the DICOM image.
        
        Returns
        -------
        Image
            The resulting :class:`~django_dicom.models.Image` instance.
        """

        with open(path, "rb") as dcm_buffer:
            return ImportImage(dcm_buffer).run()