Skip to content

Commit

Permalink
Fix issues for 2d ome zarr data (mobie#137)
Browse files Browse the repository at this point in the history
Fix issues for 2d ome zarr data
  • Loading branch information
constantinpape authored May 20, 2024
1 parent 2f5e0cd commit 79c67e9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
49 changes: 23 additions & 26 deletions examples/create_mobie_project.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <PROECJT_ROOT_FOLDER/data>\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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
" )"
]
},
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -304,19 +302,17 @@
{
"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",
"input_path = os.path.join(example_input_data, \"em_segmentation.tif\")\n",
"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",
Expand All @@ -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",
")"
]
},
Expand Down Expand Up @@ -451,7 +448,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
"version": "3.11.7"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion mobie/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.5.1"
__version__ = "0.5.2"
SPEC_VERSION = "0.3.0"
7 changes: 5 additions & 2 deletions mobie/metadata/source_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions mobie/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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():
Expand Down

0 comments on commit 79c67e9

Please sign in to comment.