def migrate_source_metadata(name, source, dataset_folder, menu_name): source_type = source["type"] xml_locations = source["storage"] xml_path = os.path.join(dataset_folder, "images", xml_locations["local"]) if source_type in ("image", "mask"): view = metadata.get_default_view( "image", name, menu_name=menu_name, color=source["color"], contrastLimits=source["contrastLimits"]) new_source = metadata.get_image_metadata(dataset_folder, xml_path) source_type = "image" else: assert source_type == "segmentation" if "tableFolder" in source: table_location = os.path.join(dataset_folder, source["tableFolder"]) assert os.path.exists(table_location), table_location else: table_location = None seg_color = source["color"] seg_color = "glasbey" if seg_color == "randomFromGlasbey" else seg_color if table_location is None: view = metadata.get_default_view("segmentation", name, menu_name=menu_name, lut=seg_color) else: view = metadata.get_default_view("segmentation", name, menu_name=menu_name, lut=seg_color, tables=["default.tsv"]) new_source = metadata.get_segmentation_metadata( dataset_folder, xml_path, table_location=table_location) if "remote" in xml_locations: remote_xml = os.path.join("images", xml_locations["remote"]) new_source[source_type]["imageData"]["bdv.n5.s3"] = { "relativePath": remote_xml } has_remote = True else: has_remote = False return new_source, view, has_remote
def get_dataset_metadata(self): dataset_metadata = { "is2D": False, "description": "My dataset.", "sources": { "image1": metadata.get_image_metadata("image1", "/images/image1.xml", file_format="bdv.n5"), "seg1": metadata.get_segmentation_metadata("seg1", "/images/seg1.xml", file_format="bdv.n5") }, "views": { "default": metadata.get_default_view("image", "image1") } } return dataset_metadata
def require_dataset_and_view(root, dataset_name, file_format, source_type, source_name, menu_name, view, is_default_dataset, contrast_limits=None): ds_exists = require_dataset(root, dataset_name, file_format) dataset_folder = os.path.join(root, dataset_name) if view is None: kwargs = { "contrastLimits": contrast_limits } if source_type == "image" else {} view = metadata.get_default_view(source_type, source_name, menu_name=menu_name, **kwargs) else: update_view = {} if menu_name is not None: update_view["uiSelectionGroup"] = menu_name if source_type == "image" and contrast_limits is None: update_view["contrastLimits"] = contrast_limits if update_view: view.update(update_view) validate_view_metadata(view, sources=[source_name]) if not ds_exists: metadata.create_dataset_structure(root, dataset_name, [file_format]) default_view = deepcopy(view) default_view.update({"uiSelectionGroup": "bookmark"}) metadata.create_dataset_metadata(dataset_folder, views={"default": default_view}) metadata.add_dataset(root, dataset_name, is_default_dataset) return view
def get_source(client, bucket, container, internal_path, endpoint, dataset_folder, source_name, view, menu_name): source_object = os.path.join(container, internal_path, 'attributes.json') attrs_file = s3_utils.download_file(client, bucket, source_object) with open(attrs_file) as f: attrs = json.load(f) name = attrs['name'] if source_name is None else source_name # for now we hard-code the source type to image. # in the future it would be nice to infer this somehow from the attributes source_type = 'image' # get the mobie source metadata address = os.path.join(endpoint, bucket, container, internal_path) if source_type == 'image': source = metadata.get_image_metadata(dataset_folder, address, file_format='openOrganelle.s3') else: source = metadata.get_segmentation_metadata( dataset_folder, address, file_format='openOrganelle.s3') # get the mobie view metadata # if the menu-name was not specified, we infer it from the root of the source name if menu_name is None: menu_name = os.path.split(internal_path)[0] menu_name = name if menu_name == "" else menu_name if view is None: view = metadata.get_default_view(source_type, name, menu_name=menu_name) else: view.update({"uiSelectionGroup": menu_name}) validate_view_metadata(view, sources=[name]) return name, source, view
[5.098000335693359, 0.0, 0.0, 39024.47988128662, 0.0, 5.098000335693359, 0.0, 44361.50386505127, 0.0, 0.0, 5.098000335693359, 0.0] ] # add the two tomograms for name, trafo in zip(tomo_names, transformations): im_name = f"em-{os.path.splitext(name)[0]}" im_path = os.path.join(example_input_data, name) # Here, we set the default contrast limits that will # be applied in mobie to the min / max value of the data. im = imageio.volread(im_path) min_val, max_val = im.min(), im.max() view = metadata.get_default_view("image", im_name, source_transform={'parameters': trafo}, contrastLimits=[min_val, max_val]) mobie.add_image( input_path=im_path, input_key="", root=mobie_project_folder, dataset_name=dataset_name, image_name=im_name, resolution=resolution, scale_factors=scale_factors, transformation=trafo, chunks=chunks, target=target, max_jobs=max_jobs, view=view,
def test_viewer_transform(self): from mobie.metadata import get_default_view trafos = [ { "timepoint": 0 }, { "affine": np.random.rand(12).tolist() }, { "affine": np.random.rand(12).tolist(), "timepoint": 0 }, { "normalizedAffine": np.random.rand(12).tolist() }, { "normalizedAffine": np.random.rand(12).tolist(), "timepoint": 1 }, { "position": np.random.rand(3).tolist() }, { "position": np.random.rand(3).tolist(), "timepoint": 2 }, { "normalVector": np.random.rand(3).tolist() }, ] for trafo in trafos: view = get_default_view("image", "my-image", viewer_transform=trafo) validate_with_schema(view, "view") # test missing fields invalid_trafos = [{"foo": "bar"}] for trafo in invalid_trafos: view = get_default_view("image", "my-image", viewer_transform=trafo) with self.assertRaises(ValidationError): validate_with_schema(view, "view") # test invalid fields invalid_trafos = [{ "timepoint": 0, "foo": "bar" }, { "affine": np.random.rand(12).tolist(), "foo": "bar" }, { "normalizedAffine": np.random.rand(12).tolist(), "x": "y" }, { "position": np.random.rand(3).tolist(), "a": 3 }] for trafo in invalid_trafos: view = get_default_view("image", "my-image", viewer_transform=trafo) with self.assertRaises(ValidationError): validate_with_schema(view, "view") # test invalid values invalid_trafos = [{ "timepoint": -1 }, { "affine": np.random.rand(11).tolist() }, { "normalizedAffine": np.random.rand(13).tolist() }, { "position": np.random.rand(4).tolist() }] for trafo in invalid_trafos: view = get_default_view("image", "my-image", viewer_transform=trafo) with self.assertRaises(ValidationError): validate_with_schema(view, "view")
def test_image_view(self): from mobie.metadata import get_default_view # test the default view view = get_default_view("image", "my-image") validate_with_schema(view, "view") # test custom image settings custom_kwargs = [ { "contrastLimits": [0., 255.], "color": "white" }, { "contrastLimits": [0., 2000.], "color": "red" }, { "contrastLimits": [-10., 20000000.] }, { "showImagesIn3d": True }, { "showImagesIn3d": True, "resolution3dView": [10., 10., 12.] }, { "blendingMode": "sumOccluding" }, ] for kwargs in custom_kwargs: view = get_default_view("image", "my-image", **kwargs) validate_with_schema(view, "view") # test missing fields view = get_default_view("image", "my-image") view["sourceDisplays"][0]["imageDisplay"].pop("color") with self.assertRaises(ValidationError): validate_with_schema(view, "view") # test invalid fields view = get_default_view("image", "my-image") view["foo"] = "bar" with self.assertRaises(ValidationError): validate_with_schema(view, "view") view = get_default_view("image", "my-image") view["sourceDisplays"][0]["imageDisplay"]["foo"] = "bar" with self.assertRaises(ValidationError): validate_with_schema(view, "view") # test invalid values invalid_kwargs = [{ "uiSelectionGroup": "abc a" }, { "uiSelectionGroup": "abc/a" }, { "uiSelectionGroup": "abc;" }, { "color": "foobar" }, { "color": "r=1,g=2,b=3,a=4,z=5" }, { "contrastLimits": [1., 2., 3.] }, { "showImagesIn3d": "foobar" }, { "resolution3dView": [1., 2., 3., 4.] }, { "blendingMode": "summe" }] for kwargs in invalid_kwargs: if "uiSelectionGroup" in kwargs: menu_name = kwargs.pop("uiSelectionGroup") view = get_default_view("image", "my-image", menu_name=menu_name, **kwargs) with self.assertRaises(ValidationError): validate_with_schema(view, "view")
def test_segmentation_view(self): from mobie.metadata import get_default_view # test the default view view = get_default_view("segmentation", "my-seg") validate_with_schema(view, "view") # test custom segmentation settings custom_kwargs = [{ "opacity": 0.5, "lut": "glasbey" }, { "opacity": 0.9, "lut": "viridis", "colorByColumn": "colname", "showSelectedSegmentsIn3d": True, "tables": ["a.csv", "b.tsv"], "valueLimits": [0, 2500] }, { "selectedSegmentIds": ["my-seg;0;1", "my-seg;0;2", "my-seg;1;10"] }, { "showAsBoundaries": True, "boundaryThickness": 12 }] for kwargs in custom_kwargs: view = get_default_view("segmentation", "my-seg", **kwargs) validate_with_schema(view, "view") # test missing fields view = get_default_view("segmentation", "my-seg") view["sourceDisplays"][0]["segmentationDisplay"].pop("opacity") with self.assertRaises(ValidationError): validate_with_schema(view, "view") # test invalid fields view = get_default_view("segmentation", "my-seg") view["sourceDisplays"][0]["segmentationDisplay"]["foo"] = "bar" with self.assertRaises(ValidationError): validate_with_schema(view, "view") # test invalid values invalid_kwargs = [{ "opacity": 10 }, { "lut": "red" }, { "lut": "foobar" }, { "selectedSegmentIds": ["my-seg,0,2"] }, { "selectedSegmentIds": ["my-seg/abc;0;2"] }, { "selectedSegmentIds": ["my-segc;abba;2"] }] for kwargs in invalid_kwargs: view = get_default_view("segmentation", "my-seg", **kwargs) with self.assertRaises(ValidationError): validate_with_schema(view, "view") # test view with source transformations trafos = [{ "parameters": np.random.rand(12).tolist() }, { "parameters": np.random.rand(12).tolist(), "timepoints": [0] }, { "parameters": np.random.rand(12).tolist(), "timepoints": [1, 2, 3] }] for trafo in trafos: view = get_default_view("image", "my-image", source_transform=trafo) validate_with_schema(view, "view") # test invalid values invalid_trafos = [{ "parameters": np.random.rand(12).tolist(), "timepoints": [-1] }, { "parameters": np.random.rand(12).tolist(), "timepoints": [-1, 2, 3] }] for trafo in invalid_trafos: view = get_default_view("image", "my-image", source_transform=trafo) with self.assertRaises(ValidationError): validate_with_schema(view, "view")