From 67dfecd863fba6163edbd61cf4c3e8aa5dbbce7b Mon Sep 17 00:00:00 2001 From: Jan Eglinger Date: Mon, 16 Oct 2023 14:54:37 +0200 Subject: [PATCH] Add simple test for mobie functionality Also remove 'compute_aggregate_table_values' as we do not use it. --- src/faim_hcs/mobie.py | 44 ----------------- tests/test_mobie.py | 109 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 44 deletions(-) create mode 100644 tests/test_mobie.py diff --git a/src/faim_hcs/mobie.py b/src/faim_hcs/mobie.py index 9ab1ec82..3cbe3130 100644 --- a/src/faim_hcs/mobie.py +++ b/src/faim_hcs/mobie.py @@ -226,50 +226,6 @@ def add_labels_view( wells_table.to_csv(wells_table_path, sep="\t", index=False) -def compute_aggregate_table_values( - dataset_folder, - table_suffix, - aggregation_dict=None, -): - """Aggregate all tables with given suffix, and write summarized values to wells table. - - :param dataset_folder: location of the MoBIE dataset to be modified - :param table_suffix: common suffix of all tables to be included (e.g. 'my_seg' for 'A01_my_seg' etc.) - :param aggregation_dict: mapping of column names to (one or multiple) aggregation methods - """ - if aggregation_dict is None: - aggregation_dict = {"area": ["mean", "min", "max"]} - - # read wells table and get list of wells (region_id) - wells_table_path = join(dataset_folder, "tables", "wells", "default.tsv") - wells_table = read_table(wells_table_path) - wells = wells_table["region_id"] - - # read and concatenate each table from segmentations - tables = [] - for well in wells: - well_table = read_table( - join(dataset_folder, "tables", f"{well}_{table_suffix}", "default.tsv") - ) - well_table["region_id"] = well - tables.append(well_table) - joined_table = pd.concat(tables) - - # aggregate given columns with given functions - summary = joined_table.groupby("region_id").aggregate(aggregation_dict) - - # flatten table index - summary.columns = ["_".join(headers) for headers in summary.columns.to_flat_index()] - # add suffix to column names - summary.columns = [f"{header}_{table_suffix}" for header in summary.columns] - print(summary) - - # join with original wells table - wells_table.join(summary, on="region_id").to_csv( - wells_table_path, sep="\t", index=False - ) - - def _add_channel_plate_overviews( dataset_folder, plate_hists, plate_colors, sources, view_name ): diff --git a/tests/test_mobie.py b/tests/test_mobie.py new file mode 100644 index 00000000..7a21964e --- /dev/null +++ b/tests/test_mobie.py @@ -0,0 +1,109 @@ +import json +from pathlib import Path + +import pytest +from mobie.metadata.dataset_metadata import ( + create_dataset_metadata, + create_dataset_structure, +) +from mobie.metadata.project_metadata import add_dataset, create_project_metadata +from mobie.validation import validate_project + +from faim_hcs.io.MolecularDevicesImageXpress import parse_single_plane_multi_fields +from faim_hcs.MetaSeriesUtils import get_well_image_CYX +from faim_hcs.mobie import add_wells_to_project +from faim_hcs.Zarr import build_zarr_scaffold, write_cyx_image_to_well + +ROOT_DIR = Path(__file__).parent.parent + + +@pytest.fixture +def files(): + return parse_single_plane_multi_fields(ROOT_DIR / "resources" / "Projection-Mix") + + +@pytest.fixture +def zarr_group(files, zarr_dir): + plate = build_zarr_scaffold( + root_dir=zarr_dir, + files=files, + name="", + layout=384, + order_name="", + barcode="", + ) + for well in ["E07", "E08"]: + img, hists, ch_metadata, metadata, roi_tables = get_well_image_CYX( + well_files=files[files["well"] == well], + channels=["w1"], + ) + field = plate[well[0]][str(int(well[1:]))][0] + write_cyx_image_to_well(img, hists, ch_metadata, metadata, field) + return plate + + +@pytest.fixture +def mobie_project_folder(tmp_path_factory): + return tmp_path_factory.mktemp("mobie") + + +@pytest.fixture +def zarr_dir(tmp_path_factory): + return tmp_path_factory.mktemp("zarr") + + +def test_add_wells_to_project(zarr_group, mobie_project_folder): + # setup OME-ZARR folder + # setup MoBIE project + dataset_name = "mobie_test_dataset" + create_project_metadata( + root=mobie_project_folder, + # description="MoBIE test project", + ) + create_dataset_structure( + root=mobie_project_folder, + dataset_name=dataset_name, + file_formats=["ome.zarr"], + ) + create_dataset_metadata( + dataset_folder=(mobie_project_folder / dataset_name), + description="MoBIE test dataset", + is2d=True, + ) + add_dataset( + root=mobie_project_folder, + dataset_name=dataset_name, + is_default=True, + ) + + # add_wells_to_project + add_wells_to_project( + plate=zarr_group, + dataset_folder=(mobie_project_folder / dataset_name), + well_group="0", + view_name="default", + ) + + # assert + validate_project( + root=mobie_project_folder, + ) + + dataset_json_path: Path = mobie_project_folder / dataset_name / "dataset.json" + + assert dataset_json_path.exists() + + with open(dataset_json_path) as f: + json_data = json.load(f) + assert "views" in json_data + assert "sourceDisplays" in json_data["views"]["default"] + assert "sourceTransforms" in json_data["views"]["default"] + + table_path: Path = ( + mobie_project_folder / dataset_name / "tables" / "wells" / "default.tsv" + ) + assert table_path.exists() + + +def test_add_labels_view(): + pass