-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tracks exporting to
zarr
, napari
, and trackmate
. Some bug f… (
#75) * Add tracks exporting to `zarr`, `napari`, and `trackmate`. Some bug fixes. * Address review comments * Refactor `export_tracks` and add tests for it * Fix circular import * add `recursive-include`
- Loading branch information
1 parent
8f10963
commit 182819f
Showing
13 changed files
with
195 additions
and
14 deletions.
There are no files selected for viewing
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,3 +1,3 @@ | ||
include README.md | ||
include LICENSE | ||
include ultrack/widgets/ultrackwidget/resources/*.json | ||
recursive-include ultrack/widgets/ultrackwidget/resources *.json |
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 |
---|---|---|
@@ -1,3 +1,2 @@ | ||
from ultrack.api.database import Experiment, ExperimentStatus | ||
from ultrack.api.main import start_server | ||
from ultrack.api.database import Experiment | ||
from ultrack.api.database import ExperimentStatus |
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,42 @@ | ||
from pathlib import Path | ||
|
||
from ultrack import MainConfig, export_tracks_by_extension | ||
|
||
|
||
def test_exporter(tracked_database_mock_data: MainConfig, tmp_path: Path) -> None: | ||
file_ext_list = [".xml", ".csv", ".zarr", ".dot", ".json"] | ||
last_modified_time = {} | ||
for file_ext in file_ext_list: | ||
tmp_file = tmp_path / f"tracks{file_ext}" | ||
export_tracks_by_extension(tracked_database_mock_data, tmp_file) | ||
|
||
# assert file exists | ||
assert (tmp_path / f"tracks{file_ext}").exists() | ||
# assert file size is not zero | ||
assert (tmp_path / f"tracks{file_ext}").stat().st_size > 0 | ||
|
||
# store last modified time | ||
last_modified_time[str(tmp_file)] = tmp_file.stat().st_mtime | ||
|
||
# loop again testing overwrite=False | ||
for file_ext in file_ext_list: | ||
tmp_file = tmp_path / f"tracks{file_ext}" | ||
try: | ||
export_tracks_by_extension( | ||
tracked_database_mock_data, tmp_file, overwrite=False | ||
) | ||
assert False, "FileExistsError should be raised" | ||
except FileExistsError: | ||
pass | ||
|
||
# loop again testing overwrite=True | ||
for file_ext in file_ext_list: | ||
tmp_file = tmp_path / f"tracks{file_ext}" | ||
export_tracks_by_extension(tracked_database_mock_data, tmp_file, overwrite=True) | ||
|
||
# assert file exists | ||
assert (tmp_path / f"tracks{file_ext}").exists() | ||
# assert file size is not zero | ||
assert (tmp_path / f"tracks{file_ext}").stat().st_size > 0 | ||
|
||
assert last_modified_time[str(tmp_file)] != tmp_file.stat().st_mtime |
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,74 @@ | ||
import json | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
import networkx as nx | ||
|
||
from ultrack.config import MainConfig | ||
from ultrack.core.export import ( | ||
to_networkx, | ||
to_trackmate, | ||
to_tracks_layer, | ||
tracks_to_zarr, | ||
) | ||
|
||
|
||
def export_tracks_by_extension( | ||
config: MainConfig, filename: Union[str, Path], overwrite: bool = False | ||
) -> None: | ||
""" | ||
Export tracks to a file given the file extension. | ||
Supported file extensions are .xml, .csv, .zarr, .dot, and .json. | ||
- `.xml` exports to a TrackMate compatible XML file. | ||
- `.csv` exports to a CSV file. | ||
- `.zarr` exports the tracks to dense segments in a `zarr` array format. | ||
- `.dot` exports to a Graphviz DOT file. | ||
- `.json` exports to a networkx JSON file. | ||
Parameters | ||
---------- | ||
filename : str or Path | ||
The name of the file to save the tracks to. | ||
config : MainConfig | ||
The configuration object. | ||
overwrite : bool, optional | ||
Whether to overwrite the file if it already exists, by default False. | ||
See Also | ||
-------- | ||
to_trackmate : | ||
Export tracks to a TrackMate compatible XML file. | ||
to_tracks_layer : | ||
Export tracks to a CSV file. | ||
tracks_to_zarr : | ||
Export tracks to a `zarr` array. | ||
to_networkx : | ||
Export tracks to a networkx graph. | ||
""" | ||
if Path(filename).exists() and not overwrite: | ||
raise FileExistsError( | ||
f"File {filename} already exists. Set `overwrite=True` to overwrite the file" | ||
) | ||
|
||
file_ext = Path(filename).suffix | ||
if file_ext.lower() == ".xml": | ||
to_trackmate(config, filename, overwrite=True) | ||
elif file_ext.lower() == ".csv": | ||
df, _ = to_tracks_layer(config, include_parents=True) | ||
df.to_csv(filename, index=False) | ||
elif file_ext.lower() == ".zarr": | ||
df, _ = to_tracks_layer(config) | ||
tracks_to_zarr(config, df, filename, overwrite=True) | ||
elif file_ext.lower() == ".dot": | ||
G = to_networkx(config) | ||
nx.drawing.nx_pydot.write_dot(G, filename) | ||
elif file_ext.lower() == ".json": | ||
G = to_networkx(config) | ||
json_data = nx.node_link_data(G) | ||
with open(filename, "w") as f: | ||
json.dump(json_data, f) | ||
else: | ||
raise ValueError( | ||
f"Unknown file extension: {file_ext}. Supported extensions are .xml, .csv, .zarr, .dot, and .json." | ||
) |
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
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