Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for XML writer #13

Merged
merged 2 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions brainglobe_napari_io/cellfinder/writer_multiple_xml.py

This file was deleted.

36 changes: 22 additions & 14 deletions brainglobe_napari_io/cellfinder/writer_xml.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
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, 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 []
24 changes: 13 additions & 11 deletions brainglobe_napari_io/napari.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,47 @@ 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

- 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
title: Write Points
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:
- points
filename_extensions:
- .xml
display_name: points
- command: brainglobe-napari-io.cellfinder_write_multiple_xml
layer_types:
- "points+"
Expand Down
4 changes: 2 additions & 2 deletions brainglobe_napari_io/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions brainglobe_napari_io/tests/test_cellfinder_writer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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

paths = writer_xml.write_multiple_points_to_xml(
str(tmpdir / "multiple.xml"), layers
)
Comment on lines +19 to +21

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this method can't handle Path objects?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it should be able to. The file will eventually just be opened with open.

assert len(paths) == 1
assert isinstance(paths[0], str)
assert reader_xml.is_cellfinder_xml(paths[0])