From 2425a01c4af4fefc2714f16970391ffc7e40d42d Mon Sep 17 00:00:00 2001 From: David Stansby Date: Wed, 16 Mar 2022 11:15:12 +0000 Subject: [PATCH 1/2] Refactor XML writer --- .../cellfinder/writer_multiple_xml.py | 33 ----------------- brainglobe_napari_io/cellfinder/writer_xml.py | 36 ++++++++++++------- brainglobe_napari_io/napari.yaml | 17 +++++++-- brainglobe_napari_io/plugins.py | 4 +-- .../tests/test_cellfinder_writer.py | 34 ++++++++++++++++++ 5 files changed, 75 insertions(+), 49 deletions(-) delete mode 100644 brainglobe_napari_io/cellfinder/writer_multiple_xml.py create mode 100644 brainglobe_napari_io/tests/test_cellfinder_writer.py diff --git a/brainglobe_napari_io/cellfinder/writer_multiple_xml.py b/brainglobe_napari_io/cellfinder/writer_multiple_xml.py deleted file mode 100644 index cdc02d2..0000000 --- a/brainglobe_napari_io/cellfinder/writer_multiple_xml.py +++ /dev/null @@ -1,33 +0,0 @@ -from typing import List, Tuple, Dict, Any -from imlib.IO.cells import save_cells -from imlib.cells.cells import Cell - -from .utils import convert_layer_to_cells - - -def cellfinder_write_multiple_xml(path: str, layer_types: list): - if ( - isinstance(path, str) - and path.endswith(".xml") - and all(item == "points" for item in layer_types) - ): - return write_multiple_points_to_xml - else: - return None - - -def write_multiple_points_to_xml( - path: str, layer_data: List[Tuple[Any, Dict, str]] -) -> str: - cells_to_save = [] - for layer in layer_data: - data, state, type = layer - if state["metadata"]["point_type"] == Cell.CELL: - cells_to_save.extend(convert_layer_to_cells(data)) - elif state["metadata"]["point_type"] == Cell.UNKNOWN: - cells_to_save.extend(convert_layer_to_cells(data, cells=False)) - - if cells_to_save: - save_cells(cells_to_save, path) - - return path diff --git a/brainglobe_napari_io/cellfinder/writer_xml.py b/brainglobe_napari_io/cellfinder/writer_xml.py index 834854a..428cfe3 100644 --- a/brainglobe_napari_io/cellfinder/writer_xml.py +++ b/brainglobe_napari_io/cellfinder/writer_xml.py @@ -1,25 +1,37 @@ from typing import List, Tuple, Dict, Any + from imlib.IO.cells import save_cells from imlib.cells.cells import Cell +from napari.types import FullLayerData +from napari.utils.notifications import show_info from .utils import convert_layer_to_cells -def cellfinder_write_xml(path, data, meta): - if isinstance(path, str) and path.endswith(".xml"): - return write_points_to_xml(path, data, meta) - else: - return None +def write_points_to_xml(path: str, data: Any, attributes: dict) -> List[str]: + return write_multiple_points_to_xml(path, [(data, attributes, "points")]) -def write_points_to_xml(path, data, metadata): +def write_multiple_points_to_xml( + path: str, layer_data: List[FullLayerData] +) -> List[str]: cells_to_save = [] - if metadata["metadata"]["point_type"] == Cell.CELL: - cells_to_save.extend(convert_layer_to_cells(data)) - elif metadata["metadata"]["point_type"] == Cell.UNKNOWN: - cells_to_save.extend(convert_layer_to_cells(data, cells=False)) + for layer in layer_data: + data, attributes, type = layer + if "point_type" not in attributes["metadata"]: + # Not a points layer loaded by brainglobe_napari_io + name = attributes["name"] + show_info( + f'Did not find point type in metadata for "{name}" layer, ' + "not saving to file." + ) + elif attributes["metadata"]["point_type"] == Cell.CELL: + cells_to_save.extend(convert_layer_to_cells(data)) + elif attributes["metadata"]["point_type"] == Cell.UNKNOWN: + cells_to_save.extend(convert_layer_to_cells(data, cells=False)) if cells_to_save: save_cells(cells_to_save, path) - - return path + return [path] + else: + return [] diff --git a/brainglobe_napari_io/napari.yaml b/brainglobe_napari_io/napari.yaml index 0c06179..efe8249 100644 --- a/brainglobe_napari_io/napari.yaml +++ b/brainglobe_napari_io/napari.yaml @@ -5,38 +5,50 @@ contributions: - id: brainglobe-napari-io.brainreg_read_dir title: Brainreg Read Directory python_name: brainglobe_napari_io.brainreg.reader_dir:brainreg_read_dir + - id: brainglobe-napari-io.brainreg_read_dir_standard_space title: Brainreg Read Directory Standard Space python_name: brainglobe_napari_io.brainreg.reader_dir_standard_space:brainreg_read_dir_standard_space + - id: brainglobe-napari-io.cellfinder_read_dir title: Cellfinder Read Directory python_name: brainglobe_napari_io.cellfinder.reader_dir:cellfinder_read_dir + - id: brainglobe-napari-io.cellfinder_read_xml title: Cellfinder Read XML python_name: brainglobe_napari_io.cellfinder.reader_xml:cellfinder_read_xml + - id: brainglobe-napari-io.cellfinder_write_xml title: Write Points - python_name: brainglobe_napari_io.cellfinder.writer_xml:cellfinder_write_xml + python_name: brainglobe_napari_io.cellfinder.writer_xml:write_points_to_xml + - id: brainglobe-napari-io.cellfinder_write_multiple_xml title: Write Multiple Points - python_name: brainglobe_napari_io.cellfinder.writer_multiple_xml:cellfinder_write_multiple_xml + python_name: brainglobe_napari_io.cellfinder.writer_xml:write_multiple_points_to_xml + + readers: - command: brainglobe-napari-io.brainreg_read_dir filename_patterns: - '*.tiff' accepts_directories: true + - command: brainglobe-napari-io.brainreg_read_dir_standard_space filename_patterns: - '*.tiff' accepts_directories: true + - command: brainglobe-napari-io.cellfinder_read_dir filename_patterns: - '*.tif' accepts_directories: true + - command: brainglobe-napari-io.cellfinder_read_xml filename_patterns: - '*.xml' accepts_directories: false + + writers: - command: brainglobe-napari-io.cellfinder_write_xml layer_types: @@ -44,6 +56,7 @@ contributions: filename_extensions: - .xml display_name: points + - command: brainglobe-napari-io.cellfinder_write_multiple_xml layer_types: - "points+" diff --git a/brainglobe_napari_io/plugins.py b/brainglobe_napari_io/plugins.py index df90037..78cfd7c 100644 --- a/brainglobe_napari_io/plugins.py +++ b/brainglobe_napari_io/plugins.py @@ -3,7 +3,7 @@ from brainglobe_napari_io.brainreg.reader_dir import brainreg_read_dir -from brainglobe_napari_io.cellfinder.writer_multiple_xml import ( +from brainglobe_napari_io.cellfinder.writer_xml import ( + cellfinder_write_xml, cellfinder_write_multiple_xml, ) -from brainglobe_napari_io.cellfinder.writer_xml import cellfinder_write_xml diff --git a/brainglobe_napari_io/tests/test_cellfinder_writer.py b/brainglobe_napari_io/tests/test_cellfinder_writer.py new file mode 100644 index 0000000..fcdaca7 --- /dev/null +++ b/brainglobe_napari_io/tests/test_cellfinder_writer.py @@ -0,0 +1,34 @@ +import pathlib + +import numpy as np + +from brainglobe_napari_io.cellfinder import ( + reader_xml, + writer_xml, +) + +test_data_dir = pathlib.Path(__file__) / ".." / "data" + + +def test_xml_roundrip(tmpdir): + # Check that a read in XML file can also be written back out + xml_file = test_data_dir / "cell_classification.xml" + layers = reader_xml.xml_reader(xml_file) + assert len(layers) == 2 + # First layer doesn't have any data, so use second layer + layer = layers[1] + + # Test single writing + paths = writer_xml.write_points_to_xml( + str(tmpdir / "single.xml"), layer[0], layer[1] + ) + assert len(paths) == 1 + assert isinstance(paths[0], str) + + # Test multiple writing + paths = writer_xml.write_multiple_points_to_xml( + str(tmpdir / "multiple.xml"), layers + ) + assert len(paths) == 1 + assert isinstance(paths[0], str) + assert reader_xml.is_cellfinder_xml(paths[0]) From a094f125241fcc4f92be5c6b8cbd19ce4684a630 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Wed, 16 Mar 2022 13:53:38 +0000 Subject: [PATCH 2/2] Remove single writer --- brainglobe_napari_io/cellfinder/writer_xml.py | 4 ---- brainglobe_napari_io/napari.yaml | 13 +------------ .../tests/test_cellfinder_writer.py | 10 ---------- 3 files changed, 1 insertion(+), 26 deletions(-) diff --git a/brainglobe_napari_io/cellfinder/writer_xml.py b/brainglobe_napari_io/cellfinder/writer_xml.py index 428cfe3..4a902f3 100644 --- a/brainglobe_napari_io/cellfinder/writer_xml.py +++ b/brainglobe_napari_io/cellfinder/writer_xml.py @@ -8,10 +8,6 @@ from .utils import convert_layer_to_cells -def write_points_to_xml(path: str, data: Any, attributes: dict) -> List[str]: - return write_multiple_points_to_xml(path, [(data, attributes, "points")]) - - def write_multiple_points_to_xml( path: str, layer_data: List[FullLayerData] ) -> List[str]: diff --git a/brainglobe_napari_io/napari.yaml b/brainglobe_napari_io/napari.yaml index efe8249..a27b279 100644 --- a/brainglobe_napari_io/napari.yaml +++ b/brainglobe_napari_io/napari.yaml @@ -18,12 +18,8 @@ contributions: title: Cellfinder Read XML python_name: brainglobe_napari_io.cellfinder.reader_xml:cellfinder_read_xml - - id: brainglobe-napari-io.cellfinder_write_xml - title: Write Points - python_name: brainglobe_napari_io.cellfinder.writer_xml:write_points_to_xml - - id: brainglobe-napari-io.cellfinder_write_multiple_xml - title: Write Multiple Points + title: Write Points python_name: brainglobe_napari_io.cellfinder.writer_xml:write_multiple_points_to_xml @@ -50,13 +46,6 @@ contributions: writers: - - command: brainglobe-napari-io.cellfinder_write_xml - layer_types: - - points - filename_extensions: - - .xml - display_name: points - - command: brainglobe-napari-io.cellfinder_write_multiple_xml layer_types: - "points+" diff --git a/brainglobe_napari_io/tests/test_cellfinder_writer.py b/brainglobe_napari_io/tests/test_cellfinder_writer.py index fcdaca7..183c0df 100644 --- a/brainglobe_napari_io/tests/test_cellfinder_writer.py +++ b/brainglobe_napari_io/tests/test_cellfinder_writer.py @@ -15,17 +15,7 @@ def test_xml_roundrip(tmpdir): xml_file = test_data_dir / "cell_classification.xml" layers = reader_xml.xml_reader(xml_file) assert len(layers) == 2 - # First layer doesn't have any data, so use second layer - layer = layers[1] - # Test single writing - paths = writer_xml.write_points_to_xml( - str(tmpdir / "single.xml"), layer[0], layer[1] - ) - assert len(paths) == 1 - assert isinstance(paths[0], str) - - # Test multiple writing paths = writer_xml.write_multiple_points_to_xml( str(tmpdir / "multiple.xml"), layers )