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 2 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
62 changes: 58 additions & 4 deletions ultrack/core/export/trackmate.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,68 @@ def tracks_layer_to_trackmate(
----------
tracks_df : pd.DataFrame
A DataFrame with columns `track_id, id, parent_id, t, z, y, x`. Cells that belong to the same track
have the same `track_id`.
have the same `track_id`. `tracks_df.index` is used as the spot ID in the TrackMate XML.

Returns
-------
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="0" SPOT_TARGET_ID="1" 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="0" SPOT_TARGET_ID="2" EDGE_TIME="0.5"/>
</Track>
</AllTracks>
<FilteredTracks>
<TrackID TRACK_ID="1"/>
<TrackID TRACK_ID="2"/>
</FilteredTracks>
<AllSpots>
<SpotsInFrame frame="0">
<Spot ID="0" QUALITY="1.0" VISIBILITY="1" name="0" FRAME="0" RADIUS="5.0" POSITION_X="49.0" POSITION_Y="49.0" POSITION_Z="12.0"/>
</SpotsInFrame>
<SpotsInFrame frame="1">
<Spot ID="1" QUALITY="1.0" VISIBILITY="1" name="1" FRAME="1" RADIUS="5.0" POSITION_X="32.0" POSITION_Y="49.0" POSITION_Z="12.0"/>
<Spot ID="2" QUALITY="1.0" VISIBILITY="1" name="2" 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>
"""
if not pd.api.types.is_integer_dtype(tracks_df.index.dtype):
raise ValueError("The DataFrame index will be TrackMate spot IDs and must be an integer type.")
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["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,12 +225,13 @@ 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
edge_elem = ET.SubElement(track_elem, "Edge")
edge_elem.set("SPOT_SOURCE_ID", str(parent_id))
parent_spot_id = tracks_df.index[tracks_df['id'] == parent_id][0] # uniqueness checked above
qin-yu marked this conversation as resolved.
Show resolved Hide resolved
edge_elem.set("SPOT_SOURCE_ID", str(parent_spot_id))
edge_elem.set("SPOT_TARGET_ID", str(spot_id))
edge_elem.set("EDGE_TIME", str(entry["t"] - 0.5))

Expand Down
Loading