def test_non_existant_dicom_property(self): uri = '/images/LIDC-IDRI-0001/' \ '1.3.6.1.4.1.14519.5.2.1.6279.6001.298806137288633453246975630178/' \ '1.3.6.1.4.1.14519.5.2.1.6279.6001.179049373636438705059720603192' path = os.path.join(uri, '-132.500000.dcm') image_series, created = ImageSeries.get_or_create(uri) # Monkey patch ImageFile.DICOM_PROPERTIES.update({ 'RescaleSlope': lambda x: NotImplemented, }) # expected behavior is to log a warning that parsing for this # property fails with self.assertLogs(level='WARN') as cm: ImageFile(path=path, series=image_series).save() # just one warning self.assertEqual(len(cm.output), 1) self.assertIn('RescaleSlope', cm.output[0]) self.assertIn(path, cm.output[0]) # reset for other tests ImageFile.DICOM_PROPERTIES.update({ 'RescaleSlope': lambda x: { 'rescale_slope': int(x) }, })
def test_image_file_serializer(self): image_file = ImageFile(path='/my_path') serialized = ImageFileSerializer(image_file) # we construct the preview url, so test it here self.assertEqual(serialized['preview_url'].value, '/api/images/preview?dicom_location=/my_path') for s in serialized: self.assertIsNone(s.errors)
def get(self, request): ''' Get metadata of a DICOM image that is not yet imported into the database. This can be used to preview the DICOM images before creating the model (e.g., during selection). Example: .../api/images/metadata?dicom_location=FULL_PATH_TO_IMAGE --- parameters: - name: dicom_location description: full location of the image required: true type: string ''' path = request.GET['dicom_location'] try: return Response(ImageFile.load_dicom_data_from_disk(path, encode_image_data=True)) except IOError as err: raise NotFound(f"DICOM file not found on disk with path '{path}'")
def test_invalid_path_on_image_file_create(self): with self.assertRaises(PermissionDenied): ImageFile(path='/not_valid/path').save() with self.assertRaises(FileNotFoundError): ImageFile(path='/images/LIDC-IDRI-0001/not_valid/path').save()
def test_parsed_property_no_model_attribute(self): uri = '/images/LIDC-IDRI-0001/' \ '1.3.6.1.4.1.14519.5.2.1.6279.6001.298806137288633453246975630178/' \ '1.3.6.1.4.1.14519.5.2.1.6279.6001.179049373636438705059720603192' path = os.path.join(uri, '-132.500000.dcm') image_series, created = ImageSeries.get_or_create(uri) # CASE ONE, Dicom prop does not exist, attr is not field ImageFile.DICOM_PROPERTIES.update({ 'DoesNotExist1': 'no_attribute', }) # expected behavior is to log a warning with self.assertLogs(level='WARN') as cm: ImageFile(path=path, series=image_series).save() self.assertEqual(len(cm.output), 1) self.assertIn("'no_attribute' not a field", cm.output[0]) # reset ImageFile.DICOM_PROPERTIES.pop('DoesNotExist1') # CASE TWO, Dicom prop exists, attr is not field ImageFile.DICOM_PROPERTIES.update({ 'RescaleSlope': 'no_attribute2', }) # expected behavior is to log a warning with self.assertLogs(level='WARN') as cm: ImageFile(path=path, series=image_series).save() self.assertEqual(len(cm.output), 1) self.assertIn("'no_attribute2' not a field", cm.output[0]) ImageFile.DICOM_PROPERTIES.update({ 'RescaleSlope': lambda x: { 'rescale_slope': int(x) }, }) # CASE THREE, Dicom prop does not exist, field does exist ImageFile.DICOM_PROPERTIES.pop('Rows') ImageFile.DICOM_PROPERTIES.update({ 'DoesNotExist2': 'rows', }) # no warnings should be logged try: with self.assertLogs(level='WARN') as cm: img_file = ImageFile(path=path, series=image_series) img_file.save() except AssertionError as e: self.assertEqual( str(e), "no logs of level WARNING or higher triggered on root") self.assertIsNone(img_file.rows) # reset ImageFile.DICOM_PROPERTIES['Rows'] = 'rows' # CASE FOUR, Dicom prop does not exist, rows set multiple times ImageFile.DICOM_PROPERTIES.update({ 'DoesNotExist2': 'rows', }) # expected behavior is to log a warning with self.assertLogs(level='WARN') as cm: ImageFile(path=path, series=image_series).save() self.assertEqual(len(cm.output), 1) self.assertIn("Tried to set 'rows' more than once", cm.output[0]) ImageFile.DICOM_PROPERTIES.pop('DoesNotExist2')