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

Bug fix and improvement in TrackMate export #90

Merged
merged 3 commits into from
Feb 29, 2024
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
6 changes: 5 additions & 1 deletion ultrack/core/export/_test/test_trackmate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
pytrackmate = pytest.importorskip("pytrackmate")


def test_trackmate_writer(tmp_path: Path) -> None:
def test_trackmate_export_spot_match(tmp_path: Path) -> None:
"""Check if the spots (objects) match between the tracks and the exported trackmate xml file.

This test cannot check if the exported tracking is valid.
"""
tracks_outpath = tmp_path / "tracks.xml"

tracks_df = pd.DataFrame(
Expand Down
57 changes: 55 additions & 2 deletions ultrack/core/export/trackmate.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def tracks_layer_to_trackmate(
) -> str:
"""
Convert a pandas DataFrame representation of Napari track layer to TrackMate XML format.
`<ImageData/>` need to be set manually in the output XML.

Parameters
----------
Expand All @@ -32,8 +33,61 @@ def tracks_layer_to_trackmate(
-------
str
A string representation of the XML in the TrackMate format.

Examples
--------
>>> tracks_df = pd.DataFrame(
... [[1,0,12.0,49.0,49.0,1000001,-1,-1],
... [1,1,12.0,49.0,32.0,2000001,-1,1000001],
... [2,1,12.0,49.0,66.0,2000002,-1,1000001]],
... columns=['track_id','t','z','y','x','id','parent_track_id','parent_id']
... )
>>> print(tracks_df)
track_id t z y x id parent_track_id parent_id
0 1 0 12.0 49.0 49.0 1000001 -1 -1
1 1 1 12.0 49.0 32.0 2000001 -1 1000001
2 2 1 12.0 49.0 66.0 2000002 -1 1000001
>>> tracks_layer_to_trackmate(tracks_df)
<?xml version="1.0" ?>
<TrackMate version="7.11.1">
<Model spatialunits="pixels" timeunits="frames">
<AllTracks>
<Track TRACK_ID="1" NUMBER_SPOTS="2" NUMBER_GAPS="0" TRACK_START="0" TRACK_STOP="1" name="Track_1">
<Edge SPOT_SOURCE_ID="1000001" SPOT_TARGET_ID="2000001" EDGE_TIME="0.5"/>
</Track>
<Track TRACK_ID="2" NUMBER_SPOTS="1" NUMBER_GAPS="0" TRACK_START="1" TRACK_STOP="1" name="Track_2">
<Edge SPOT_SOURCE_ID="1000001" SPOT_TARGET_ID="2000002" EDGE_TIME="0.5"/>
</Track>
</AllTracks>
<FilteredTracks>
<TrackID TRACK_ID="1"/>
<TrackID TRACK_ID="2"/>
</FilteredTracks>
<AllSpots>
<SpotsInFrame frame="0">
<Spot ID="1000001" QUALITY="1.0" VISIBILITY="1" name="1000001" FRAME="0" RADIUS="5.0" POSITION_X="49.0" POSITION_Y="49.0" POSITION_Z="12.0"/>
</SpotsInFrame>
<SpotsInFrame frame="1">
<Spot ID="2000001" QUALITY="1.0" VISIBILITY="1" name="2000001" FRAME="1" RADIUS="5.0" POSITION_X="32.0" POSITION_Y="49.0" POSITION_Z="12.0"/>
<Spot ID="2000002" QUALITY="1.0" VISIBILITY="1" name="2000002" FRAME="1" RADIUS="5.0" POSITION_X="66.0" POSITION_Y="49.0" POSITION_Z="12.0"/>
</SpotsInFrame>
</AllSpots>
<FeatureDeclarations>
...
</FeatureDeclarations>
</Model>
<Settings>
<InitialSpotFilter feature="QUALITY" value="0.0" isabove="true"/>
<SpotFilterCollection/>
<TrackFilterCollection/>
<ImageData filename="None" folder="None" width="0" height="0" depth="0" nslices="1" nframes="2" pixelwidth="1.0" pixelheight="1.0" voxeldepth="1.0" timeinterval="1.0"/>
</Settings>
</TrackMate>
"""
tracks_df["id"] = tracks_df["id"].astype(int)
if not tracks_df["id"].is_unique:
raise ValueError("The 'id' column must be unique.")
tracks_df.set_index("id", inplace=True)
tracks_df["parent_id"] = tracks_df["parent_id"].astype(int)
qin-yu marked this conversation as resolved.
Show resolved Hide resolved
tracks_df["track_id"] = tracks_df["track_id"].astype(int)

Expand Down Expand Up @@ -101,7 +155,6 @@ def tracks_layer_to_trackmate(
elem.set("dimension", dimension)
elem.set("isint", isint)

# Create edge features
# Create edge features
edge_features_elem = ET.SubElement(features_elem, "EdgeFeatures")
edge_features = [
Expand Down Expand Up @@ -172,7 +225,7 @@ def tracks_layer_to_trackmate(

ET.SubElement(filtered_tracks_elem, "TrackID").set("TRACK_ID", str(track_id))

for spot_id, entry in group.iterrows():
for spot_id, entry in group.iterrows(): # spot_id is the DataFrame row index
parent_id = int(entry["parent_id"])
if parent_id == NO_PARENT:
continue
Expand Down
Loading