def test_equality(self): x = 10 y = 10 z = 3 pixel_id = sitk.sitkUInt8 size = (x, y, z) direction = (0, 1, 0, 1, 0, 0, 0, 0, 1) image = sitk.Image([x, y, z], pixel_id) image.SetOrigin(size) image.SetSpacing(size) image.SetDirection(direction) dut1 = img.ImageProperties(image) dut2 = img.ImageProperties(image) self.assertTrue(dut1 == dut2) self.assertFalse(dut1 != dut2) image = sitk.Image([x, y, z], sitk.sitkInt8) image.SetOrigin(size) image.SetSpacing(size) image.SetDirection(direction) dut1 = img.ImageProperties(image) self.assertTrue(dut1 == dut2) self.assertFalse(dut1 != dut2) image = sitk.Image([x, y, z], sitk.sitkVectorUInt8, 2) image.SetOrigin(size) image.SetSpacing(size) image.SetDirection(direction) dut1 = img.ImageProperties(image) self.assertTrue(dut1 == dut2) self.assertFalse(dut1 != dut2)
def __call__(self, file_path: str, id_: str, category: str, subject_id: str ) -> typing.Tuple[np.ndarray, typing.Union[miapy_conv.ImageProperties, None]]: if category == 'images': img = sitk.ReadImage(file_path) return sitk.GetArrayFromImage(img), miapy_conv.ImageProperties(img) if category == 'labels': img = sitk.ReadImage(file_path, sitk.sitkUInt8) return sitk.GetArrayFromImage(img), miapy_conv.ImageProperties(img) if category == 'age' or category == 'resectionstatus' or category == 'survival': if self.csv_file is None or file_path != self.csv_file: self.csv_file = file_path self.csv_content = {} with open(self.csv_file) as f: reader = csv.reader(f) header = next(reader) for row in reader: subject = row[0] self.csv_content[subject] = {header[i]: row[i] for i in range(1, len(header))} if id_ == 'age': return np.asarray(float(self.csv_content[subject_id]['Age'])), None if id_ == 'resectionstatus': print("working...") status_class = ResectionStatusConversion.from_status(self.csv_content[subject_id]['ResectionStatus']) return np.asarray(status_class), None if id_ == 'survival': return np.asarray(int(self.csv_content[subject_id]['Survival'])), None raise ValueError('could not load {}'.format(file_path))
def test_non_equality(self): x = 10 y = 10 z = 3 pixel_id = sitk.sitkUInt8 size = (x, y, z) direction = (0, 1, 0, 1, 0, 0, 0, 0, 1) image = sitk.Image([x, y, z], pixel_id) image.SetOrigin(size) image.SetSpacing(size) image.SetDirection(direction) dut1 = img.ImageProperties(image) different_size = (x, y, 2) # non-equal size image = sitk.Image(different_size, pixel_id) image.SetOrigin(size) image.SetSpacing(size) image.SetDirection(direction) dut2 = img.ImageProperties(image) self.assertFalse(dut1 == dut2) self.assertTrue(dut1 != dut2) # non-equal origin image = sitk.Image(size, pixel_id) image.SetOrigin(different_size) image.SetSpacing(size) image.SetDirection(direction) dut2 = img.ImageProperties(image) self.assertFalse(dut1 == dut2) self.assertTrue(dut1 != dut2) # non-equal spacing different_size = (x, y, 2) image = sitk.Image(size, pixel_id) image.SetOrigin(size) image.SetSpacing(different_size) image.SetDirection(direction) dut2 = img.ImageProperties(image) self.assertFalse(dut1 == dut2) self.assertTrue(dut1 != dut2) # non-equal direction different_size = (x, y, 2) image = sitk.Image(size, pixel_id) image.SetOrigin(size) image.SetSpacing(size) image.SetDirection((1, 0, 0, 0, 1, 0, 0, 0, 1)) dut2 = img.ImageProperties(image) self.assertFalse(dut1 == dut2) self.assertTrue(dut1 != dut2)
def test_is_two_dimensional(self): x = 10 y = 10 image = sitk.Image([x, y], sitk.sitkUInt8) dut = img.ImageProperties(image) self.assertEqual(dut.is_two_dimensional(), True) self.assertEqual(dut.is_three_dimensional(), False) self.assertEqual(dut.is_vector_image(), False)
def test_is_vector_image(self): x = 10 y = 10 number_of_components_per_pixel = 3 image = sitk.Image([x, y], sitk.sitkVectorUInt8, number_of_components_per_pixel) dut = img.ImageProperties(image) self.assertEqual(dut.is_two_dimensional(), True) self.assertEqual(dut.is_three_dimensional(), False) self.assertEqual(dut.is_vector_image(), True)
def setUp(self): dim_x = 5 dim_y = 10 dim_z = 3 self.no_vector_components = 4 # some unordinary origins, spacings and directions self.origin_spacing_2d = (dim_x, dim_y) self.direction_2d = (0, 1, 1, 0) self.origin_spacing_3d = (dim_x, dim_y, dim_z) self.direction_3d = (1, 0, 0, 0, 1, 0, 0, 0, 1) # set up images image = sitk.Image((dim_x, dim_y, dim_z), sitk.sitkUInt8) image.SetOrigin(self.origin_spacing_3d) image.SetSpacing(self.origin_spacing_3d) image.SetDirection(self.direction_3d) self.properties_3d = img.ImageProperties(image) image = sitk.Image((dim_x, dim_y), sitk.sitkUInt8) image.SetOrigin(self.origin_spacing_2d) image.SetSpacing(self.origin_spacing_2d) image.SetDirection(self.direction_2d) self.properties_2d = img.ImageProperties(image) # set up numpy arrays (note the inverted order of the dimension) self.array_image_shape_2d = np.zeros((dim_y, dim_x), np.uint8) self.array_2d_vector = np.zeros( (dim_y * dim_x, self.no_vector_components), np.uint8) self.array_image_shape_2d_vector = np.zeros( (dim_y, dim_x, self.no_vector_components), np.uint8) self.array_image_shape_3d = np.zeros((dim_z, dim_y, dim_x), np.uint8) self.array_3d_vector = np.zeros( (dim_z * dim_y * dim_x, self.no_vector_components), np.uint8) self.array_image_shape_3d_vector = np.zeros( (dim_z, dim_y, dim_x, self.no_vector_components), np.uint8)
def test_properties(self): x = 10 y = 10 z = 3 pixel_id = sitk.sitkUInt8 size = (x, y, z) direction = (0, 1, 0, 1, 0, 0, 0, 0, 1) image = sitk.Image([x, y, z], pixel_id) image.SetOrigin(size) image.SetSpacing(size) image.SetDirection(direction) dut = img.ImageProperties(image) self.assertEqual(dut.size, size) self.assertEqual(dut.origin, size) self.assertEqual(dut.spacing, size) self.assertEqual(dut.direction, direction) self.assertEqual(dut.dimensions, z) self.assertEqual(dut.number_of_components_per_pixel, 1) self.assertEqual(dut.pixel_id, pixel_id)
def extract(self, reader: rd.Reader, params: dict, extracted: dict) -> None: subject_index_expr = expr.IndexExpression(params['subject_index']) shape = reader.read(df.INFO_SHAPE, subject_index_expr).tolist() direction = reader.read(df.INFO_DIRECTION, subject_index_expr).tolist() spacing = reader.read(df.INFO_SPACING, subject_index_expr).tolist() origin = reader.read(df.INFO_ORIGIN, subject_index_expr).tolist() # todo: everything in memory? image = sitk.Image(shape, sitk.sitkUInt8) image.SetDirection(direction) image.SetSpacing(spacing) image.SetOrigin(origin) # todo number_of_components_per_pixel and pixel_id img_properties = conv.ImageProperties(image) if self.do_pickle: # pickle to prevent from problems since own class img_properties = pickle.dumps(img_properties) extracted['properties'] = img_properties
def __call__(self, file_name: str, id_: str, category: str, subject_id: str) -> \ typing.Tuple[np.ndarray, typing.Union[conv.ImageProperties, None]]: if id_ == FileTypes.AGE.name: with open(file_name, 'r') as f: value = np.asarray([int(f.readline().split(':')[1].strip())]) return value, None if id_ == FileTypes.GPA.name: with open(file_name, 'r') as f: value = np.asarray([float(f.readlines()[1].split(':')[1].strip())]) return value, None if id_ == FileTypes.SEX.name: with open(file_name, 'r') as f: value = np.asarray([f.readlines()[2].split(':')[1].strip()]) return value, None if category == 'images': img = sitk.ReadImage(file_name, sitk.sitkFloat32) else: img = sitk.ReadImage(file_name, sitk.sitkUInt8) return sitk.GetArrayFromImage(img), conv.ImageProperties(img)
def main(hdf_file: str): extractor = extr.ComposeExtractor([ extr.NamesExtractor(), extr.DataExtractor(), extr.SelectiveDataExtractor(), extr.DataExtractor(('numerical', ), ignore_indexing=True), extr.DataExtractor(('sex', ), ignore_indexing=True), extr.DataExtractor(('mask', ), ignore_indexing=False), extr.SubjectExtractor(), extr.FilesExtractor(categories=('images', 'labels', 'mask', 'numerical', 'sex')), extr.IndexingExtractor(), extr.ImagePropertiesExtractor() ]) dataset = extr.ParameterizableDataset(hdf_file, extr.SliceIndexing(), extractor) for i in range(len(dataset)): item = dataset[i] index_expr = item['index_expr'] # type: miapy_data.IndexExpression root = item['file_root'] image = None # type: sitk.Image for i, file in enumerate(item['images_files']): image = sitk.ReadImage(os.path.join(root, file)) np_img = sitk.GetArrayFromImage(image).astype(np.float32) np_img = (np_img - np_img.mean()) / np_img.std() np_slice = np_img[index_expr.expression] if (np_slice != item['images'][..., i]).any(): raise ValueError('slice not equal') # for any image image_properties = conv.ImageProperties(image) if image_properties != item['properties']: raise ValueError('image properties not equal') for file in item['labels_files']: image = sitk.ReadImage(os.path.join(root, file)) np_img = sitk.GetArrayFromImage(image) np_img = np.expand_dims( np_img, axis=-1 ) # due to the convention of having the last dim as number of channels np_slice = np_img[index_expr.expression] if (np_slice != item['labels']).any(): raise ValueError('slice not equal') for file in item['mask_files']: image = sitk.ReadImage(os.path.join(root, file)) np_img = sitk.GetArrayFromImage(image) np_img = np.expand_dims( np_img, axis=-1 ) # due to the convention of having the last dim as number of channels np_slice = np_img[index_expr.expression] if (np_slice != item['mask']).any(): raise ValueError('slice not equal') for file in item['numerical_files']: with open(os.path.join(root, file), 'r') as f: lines = f.readlines() age = float(lines[0].split(':')[1].strip()) gpa = float(lines[1].split(':')[1].strip()) if age != item['numerical'][0][0] or gpa != item['numerical'][0][1]: raise ValueError('value not equal') for file in item['sex_files']: with open(os.path.join(root, file), 'r') as f: sex = f.readlines()[2].split(':')[1].strip() if sex != str(item['sex'][0]): raise ValueError('value not equal') print('All test passed!')
def __call__(self, file_name: str, id_: str, category: str) -> \ t.Tuple[np.ndarray, t.Union[conv.ImageProperties, None]]: img = sitk.ReadImage(file_name) return sitk.GetArrayFromImage(img), conv.ImageProperties(img)
def __call__( self, file_path: str, id_: str, category: str ) -> typing.Tuple[np.ndarray, typing.Union[miapy_conv.ImageProperties, None]]: if id_ == FileTypes.BRAIN_MRI.name: img = sitk.ReadImage(file_path) return sitk.GetArrayFromImage(img), miapy_conv.ImageProperties(img) if id_ == FileTypes.MORPHOMETRY_STATS.name: metrics = {} subject_name = file_path.split('/')[-2] print(subject_name) # aseg.stats with open(os.path.join(file_path, 'aseg.stats'), 'r') as f: for line in f.readlines(): # Parse header lines # Example: '# Measure TotalGray, TotalGrayVol, Total gray matter volume, 666685.704205, mm^3' matcher = re.match( '# Measure (\w+), \w+, [\w\s]+, ([0-9.]+),.*', line) if matcher: key = matcher.group(1) value = float(matcher.group(2)) metrics[key] = value # Parse body lines # Example: ' 13 18 1442 1442.0 Left-Amygdala 74.0193 6.1854 48.0000 94.0000 46.0000' matcher = re.match( '\s+[0-9.]+\s+[0-9.]+\s+[0-9.]+\s+([0-9.]+)\s+([\w\-_]+).*', line) if matcher: key = matcher.group(2) value = float(matcher.group(1)) metrics[key] = value # sum up Corpus Callosum metrics['CC_sum'] = sum( [v for k, v in metrics.items() if k.startswith('CC_')]) # old freesurfer versions only if 'IntraCranialVol' in metrics: metrics['EstimatedTotalIntraCranialVol'] = metrics.pop( 'IntraCranialVol') print('WARN: ' + file_path + '. Using IntraCranialVol') # lh.aparc.stats, rh.aparc.stats for hemi in ['lh', 'rh']: with open( os.path.join(file_path, '{}.aparc.stats'.format(hemi)), 'r') as f: for line in f.readlines(): # Parse body lines # StructName NumVert SurfArea GrayVol ThickAvg ThickStd MeanCurv GausCurv FoldInd CurvInd # Example: 'bankssts 1507 1003 2841 2.494 0.553 0.146 0.076 29 4.7' matcher = re.match( '(\w+)\s+[0-9.]+\s+[0-9.]+\s+[0-9.]+\s+([0-9.]+)\s+[0-9.]+\s+([0-9.]+).*', line) if matcher: key = matcher.group(1) thick_avg = float(matcher.group(2)) mean_curv = float(matcher.group(3)) metrics['{}-{}-ThickAvg'.format(hemi, key)] = thick_avg metrics['{}-{}-MeanCurv'.format(hemi, key)] = mean_curv # add additional meta data from dict (read from csv) if self.meta_dict: #and subject_name in self.meta_dict: meta = self.meta_dict[subject_name] for key in meta: metrics[key] = meta[key] if len(self.morphometry_column_names) == 0: self.morphometry_column_names = np.array( [key for key in metrics.keys()]) # if a structure has no voxels for a particular subject, entry is not available # in aparc.stats (e.g. entorhinal can be 0) for col in self.morphometry_column_names: if col not in metrics: print('No {} found for {}. Assuming 0.0'.format( col, subject_name)) metrics[col] = 0.0 return np.array([ metrics[value] for value in self.morphometry_column_names ]), None