-
Notifications
You must be signed in to change notification settings - Fork 3
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
assert len(paths) == 1 | ||
assert isinstance(paths[0], str) | ||
assert reader_xml.is_cellfinder_xml(paths[0]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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
.