Skip to content

Commit

Permalink
Add tests for XML writer
Browse files Browse the repository at this point in the history
  • Loading branch information
dstansby committed Mar 14, 2022
1 parent 707afb2 commit 90570b9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 27 deletions.
19 changes: 5 additions & 14 deletions brainglobe_napari_io/cellfinder/writer_multiple_xml.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
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 .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:
path: str, layer_data: List[FullLayerData]
) -> List[str]:
cells_to_save = []
for layer in layer_data:
data, state, type = layer
Expand All @@ -30,4 +21,4 @@ def write_multiple_points_to_xml(
if cells_to_save:
save_cells(cells_to_save, path)

return path
return [path]
15 changes: 4 additions & 11 deletions brainglobe_napari_io/cellfinder/writer_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,14 @@
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, data, metadata):
def write_points_to_xml(path: str, data: Any, attributes: dict) -> List[str]:
cells_to_save = []
if metadata["metadata"]["point_type"] == Cell.CELL:
if attributes["metadata"]["point_type"] == Cell.CELL:
cells_to_save.extend(convert_layer_to_cells(data))
elif metadata["metadata"]["point_type"] == Cell.UNKNOWN:
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]
5 changes: 3 additions & 2 deletions brainglobe_napari_io/napari.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ contributions:
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_multiple_xml:write_multiple_points_to_xml

readers:
- command: brainglobe-napari-io.brainreg_read_dir
filename_patterns:
Expand Down
32 changes: 32 additions & 0 deletions brainglobe_napari_io/tests/test_cellfinder_writer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pathlib

import numpy as np

from brainglobe_napari_io.cellfinder import (
reader_xml,
writer_xml,
writer_multiple_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)
layer = layers[0]

for layer in layers:
paths = writer_xml.write_points_to_xml(
str(tmpdir / "single.xml"), layer[0], layer[1]
)
assert len(paths) == 1
assert isinstance(paths[0], str)

paths = writer_multiple_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])

0 comments on commit 90570b9

Please sign in to comment.