diff --git a/examples/create_mobie_project.ipynb b/examples/create_mobie_project.ipynb index 0954e51..b54b65a 100644 --- a/examples/create_mobie_project.ipynb +++ b/examples/create_mobie_project.ipynb @@ -30,12 +30,12 @@ "# location of the input data. \n", "# the example data used in this notebook is available via this link:\n", "# https://oc.embl.de/index.php/s/IV1709ZlcUB1k99\n", - "example_input_data = \"/home/pape/Work/data/mobie/mobie-example-data\"\n", + "example_input_data = \"./mobie-example-data\"\n", "\n", "# the location of the mobie project that will be created\n", "# we recommend that the mobie project folders have the structure \n", "# the folder 'data' will contain the sub-folders for individual datasets\n", - "mobie_project_folder = \"/home/pape/Work/data/mobie/mobie_example_project/data\"\n", + "mobie_project_folder = \"./mobie_example_project/data\"\n", "\n", "# name of the dataset that will be created.\n", "# one project can contain multiple datasets\n", @@ -93,17 +93,15 @@ "# - resolution: the size of one voxel in the physical unit, this needs to be a tuple/list of length 3,\n", "# specifying the size for each of the 3 spatial dimensions\n", "# - chunks: the size of the chunks (in voxels) that are used to store the output file.\n", - "# good choices are usually (1, 512, 512) for 2d data and (64, 64, 64) for 3d data\n", + "# good choices are usually (512, 512) for 2d data and (64, 64, 64) for 3d data\n", "# - scale_factors: the scale factors used for downsampling the input when creating the image pyramid\n", - "# this needs to be a list, where each entry specifies the scale factors for the 3 axes.\n", - "# Note that axes are listed in the order ZYX for the resolution, chunks and scale factors\n", + "# this needs to be a list, where each entry specifies the scale factors for the 2 or 3 axes.\n", + "# Note that axes are listed in the order YX / ZYX for the resolution, chunks and scale factors\n", "# (in the java implementation of mobie / big-data-viewer the axis convention is XYZ).\n", - "# Also note that the values for all three axes (ZYX) need to be specified. In the case of 2d data, the value\n", - "# for Z should be set to 1.\n", "unit = \"nanometer\"\n", - "resolution = (1., 10., 10.)\n", - "chunks = (1, 512, 512)\n", - "scale_factors = 4 * [[1, 2, 2]]\n", + "resolution = (10., 10.)\n", + "chunks = (512, 512)\n", + "scale_factors = 4 * [[2, 2]]\n", "\n", "mobie.add_image(\n", " input_path=input_file, \n", @@ -215,7 +213,8 @@ " target=target,\n", " max_jobs=max_jobs,\n", " view=view,\n", - " unit=unit\n", + " unit=unit,\n", + " file_format=\"bdv.n5\", # We need to use bdv.n5 as file format because we specify transformations in the metadata.\n", " )" ] }, @@ -234,11 +233,10 @@ "# We choose 'lm' as menu name, because this is a lightmicroscopy source\n", "menu_name = \"lm\"\n", "\n", - "# This is again a 2d image, so we set all values for Z to 1.\n", "unit = \"nanometer\"\n", - "resolution = [1., 100., 100.]\n", - "scale_factors = [[1, 2, 2], [1, 2, 2], [1, 2, 2]]\n", - "chunks = (1, 512, 512)\n", + "resolution = [100., 100.]\n", + "scale_factors = [[2, 2], [2, 2], [2, 2]]\n", + "chunks = (512, 512)\n", "\n", "# we set the default display color to green.\n", "view = mobie.metadata.get_default_view(\"image\", im_name, color=\"green\")\n", @@ -272,9 +270,9 @@ "\n", "# again, the mask is 2d\n", "unit = \"nanometer\"\n", - "chunks = [1, 256, 256]\n", - "resolution = [1., 160., 160.]\n", - "scale_factors = [[1, 2, 2]]\n", + "chunks = [256, 256]\n", + "resolution = [160., 160.]\n", + "scale_factors = [[2, 2]]\n", "\n", "mobie.add_image(\n", " input_path=input_path,\n", @@ -304,9 +302,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "scrolled": false - }, + "metadata": {}, "outputs": [], "source": [ "# we add a segmentation for several of the cells visible in the em-overview image\n", @@ -314,9 +310,9 @@ "segmentation_name = \"cells\"\n", "\n", "unit = \"nanometer\"\n", - "resolution = [1., 30., 30.]\n", - "chunks = [1, 256, 256]\n", - "scale_factors = [[1, 2, 2], [1, 2, 2], [1, 2, 2], [1, 2, 2]]\n", + "resolution = [30., 30.]\n", + "chunks = [256, 256]\n", + "scale_factors = [[2, 2], [2, 2], [2, 2], [2, 2]]\n", "\n", "mobie.add_segmentation(\n", " input_path=input_path,\n", @@ -328,7 +324,8 @@ " resolution=resolution,\n", " chunks=chunks,\n", " scale_factors=scale_factors,\n", - " add_default_table=True # add the default table with the properties mobie needs to interact with table and segmentation\n", + " is_2d=True, # We need to specify that this is a 2d segmentation.\n", + " add_default_table=True, # add the default table with the properties mobie needs to interact with table and segmentation\n", ")" ] }, @@ -451,7 +448,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.7" + "version": "3.11.7" } }, "nbformat": 4, diff --git a/mobie/__version__.py b/mobie/__version__.py index 5d6a35c..76c632b 100644 --- a/mobie/__version__.py +++ b/mobie/__version__.py @@ -1,2 +1,2 @@ -__version__ = "0.5.1" +__version__ = "0.5.2" SPEC_VERSION = "0.3.0" diff --git a/mobie/metadata/source_metadata.py b/mobie/metadata/source_metadata.py index 732eaa9..d8526ad 100644 --- a/mobie/metadata/source_metadata.py +++ b/mobie/metadata/source_metadata.py @@ -235,9 +235,10 @@ def add_source_to_dataset( overwrite=True, channel=None, suppress_warnings=False, + is_2d=None, **kwargs, ): - """ Add source metadata to a MoBIE dataset. + """Add source metadata to a MoBIE dataset. Arguments: dataset_folder [str] - path to the dataset folder. @@ -254,6 +255,7 @@ def add_source_to_dataset( channel [int] - the channel to load from the data. Currently only supported for the ome.zarr format (default: None) suppress_warnings [bool] - a flag to suppress warnings raised by the metadata validation (default: False) + is_2d [bool] - whether this is a 2d source. (default: None) kwargs - additional keyword arguments for spot source """ dataset_metadata = read_dataset_metadata(dataset_folder) @@ -284,7 +286,8 @@ def add_source_to_dataset( else: raise ValueError(f"Invalid source type: {source_type}, expect one of 'image', 'segmentation' or 'spots'") - is_2d = dataset_metadata.get("is2D", False) + if is_2d is None: + is_2d = dataset_metadata.get("is2D", False) validate_source_metadata(source_name, source_metadata, dataset_folder, is_2d=is_2d) sources_metadata[source_name] = source_metadata dataset_metadata["sources"] = sources_metadata diff --git a/mobie/segmentation.py b/mobie/segmentation.py index bb160bf..d83d37c 100644 --- a/mobie/segmentation.py +++ b/mobie/segmentation.py @@ -23,7 +23,7 @@ def add_segmentation(input_path, input_key, max_jobs=multiprocessing.cpu_count(), add_default_table=True, view=None, postprocess_config=None, unit="micrometer", - is_default_dataset=False, description=None): + is_default_dataset=False, description=None, is_2d=None): """ Add segmentation source to MoBIE dataset. Arguments: @@ -53,6 +53,7 @@ def add_segmentation(input_path, input_key, is_default_dataset [bool] - whether to set new dataset as default dataset. Only applies if the dataset is being created. (default: False) description [str] - description for this segmentation (default: None) + is_2d [bool] - whether this is a 2d segmentation (default: None) """ if isinstance(input_path, np.ndarray): input_path, input_key = mobie.utils.save_temp_input(input_path, tmp_folder, segmentation_name) @@ -96,14 +97,16 @@ def add_segmentation(input_path, input_key, source_name=segmentation_name, file_format=file_format) + if is_2d is None: + is_2d = mobie.metadata.read_dataset_metadata(dataset_folder).get("is2D", False) # we initialize with an already computed default table if isinstance(add_default_table, (str, pd.DataFrame)): table_folder = os.path.join(dataset_folder, "tables", segmentation_name) table_path = os.path.join(table_folder, "default.tsv") os.makedirs(table_folder, exist_ok=True) input_table = add_default_table - is_2d = mobie.metadata.read_dataset_metadata(dataset_folder).get("is2D", False) check_and_copy_default_table(input_table, table_path, is_2d) + # compute the default segmentation table elif add_default_table: table_folder = os.path.join(dataset_folder, "tables", segmentation_name) @@ -119,7 +122,7 @@ def add_segmentation(input_path, input_key, # add the segmentation to the dataset metadata mobie.metadata.add_source_to_dataset(dataset_folder, "segmentation", segmentation_name, image_metadata_path, - table_folder=table_folder, view=view) + table_folder=table_folder, view=view, is_2d=is_2d) def main():