def test_multi_setup(self): from pybdv import make_bdv from pybdv.metadata import get_affine shape = (64,) * 3 n_views = 2 data_dict = {} affine_dict = {} for vid in range(n_views): data = np.random.rand(*shape).astype('float32') affine = {'trafo1': [round(aff, 4) for aff in np.random.rand(12)], 'trafo2': [round(aff, 4) for aff in np.random.rand(12)]} make_bdv(data, self.out_path, setup_id=vid, affine=affine) data_dict[vid] = data affine_dict[vid] = affine # check implicit setup id data = np.random.rand(*shape).astype('float32') make_bdv(data, self.out_path) data_dict[n_views] = data for vid in range(n_views + 1): expected_key = get_key(self.is_h5, timepoint=0, setup_id=vid, scale=0) with open_file(self.out_path, 'r') as f: self.assertTrue(expected_key in f) data = f[expected_key][:] exp_data = data_dict[vid] self.assertTrue(np.allclose(data, exp_data)) # check affine trafos (only for explicit setup-ids) for vid in range(n_views): affine = affine_dict[vid] affine_out = get_affine(self.xml_path, vid) self.assertEqual(affine, affine_out)
def test_affine(self): from pybdv.metadata import get_affine shape = (100, 100, 100) affine = np.random.rand(12).tolist() affine = [round(aff, 4) for aff in affine] self._test_simple(shape, affine) affine_out = get_affine(self.xml_path, 0)['affine0'] self.assertEqual(affine, affine_out)
def _view_and_trafo_from_xml(xml_path, setup_id, timepoint, source_name, menu_name, trafos_for_mobie): def to_color(bdv_color): color = bdv_color.split() return f"r={color[0]},g={color[1]},b={color[2]},a={color[3]}" def to_blending_mode(bdv_blending): if bdv_blending == "Average": warnings.warn( "Blending mode average is not supported by MoBIE, using sum instead." ) mode = "sum" elif bdv_blending == "Sum": mode = "sum" else: raise RuntimeError( f"Bdv blending mode {bdv_blending} is not supported") return mode # try to parse the display settings from bdv attributes attributes = bdv_metadata.get_attributes(xml_path, setup_id) bdv_settings = attributes.get("displaysettings", None) if bdv_settings is None: display_settings = {} else: display_settings = { "contrastLimits": [bdv_settings["min"], bdv_settings["max"]], "color": to_color(bdv_settings["color"]) } bdv_blending = bdv_settings.get("Projection_Mode", None) if bdv_blending is not None: display_settings["blendingMode"] = to_blending_mode(bdv_blending) display_settings = [display_settings] # get the transforms and divide them into mobie and bdv metadata bdv_trafos = bdv_metadata.get_affine(xml_path, setup_id, timepoint) transforms = {} # the transforms to be written into bdv metadata mobie_transforms = [] # the transforms to be written into mobie metadata for trafo_name, params in bdv_trafos.items(): if trafos_for_mobie is not None and trafo_name in trafos_for_mobie: mobie_transforms.append({ "affine": { "parameters": params, "sources": [source_name], "name": trafo_name } }) else: transforms[trafo_name] = params menu_name = "images" if menu_name is None else menu_name view = metadata.get_view([source_name], ["image"], [[source_name]], display_settings, is_exclusive=False, menu_name=menu_name, source_transforms=mobie_transforms) return view, transforms
def _check(exp_data, exp_sf, exp_attrs, exp_affine): key = get_key(self.is_h5, timepoint=0, setup_id=0, scale=0) with open_file(self.out_path, 'r') as f: data = f[key][:] self.assertTrue(np.allclose(data, exp_data)) sf = get_scale_factors(self.out_path, setup_id=0) sf = absolute_to_relative_scale_factors(sf) self.assertEqual(sf, [[1, 1, 1]] + exp_sf) attrs = get_attributes(self.xml_path, setup_id=0) self.assertEqual(attrs, exp_attrs) affine = get_affine(self.xml_path, setup_id=0, timepoint=0)['affine0'] self.assertTrue(np.allclose(np.array(affine), np.array(exp_affine), atol=1e-4))
def get_transformation(source_metadata, dataset_folder, to_affine_matrix=True, resolution=None): data_format, image_metadata = _load_image_metadata(source_metadata, dataset_folder) if data_format.startswith("bdv"): transform = bdv_metadata.get_affine(image_metadata, setup_id=0) if to_affine_matrix: # TODO if resolution is None: pass transform = _bdv_transform_to_affine_matrix(transform, resolution) elif data_format.startswith("ome.zarr"): if to_affine_matrix: transform = trafo_utils.ngff_to_native(image_metadata) else: raise ValueError(f"Unsupported data format {data_format}") return transform