Skip to content

Commit

Permalink
Add support for new maDLC labels format (#678)
Browse files Browse the repository at this point in the history
  • Loading branch information
talmo authored Mar 17, 2022
1 parent 3b20b6e commit 1c57287
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
11 changes: 9 additions & 2 deletions sleap/io/format/deeplabcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,17 @@ def read_frames(

# Check if this is in the new multi-animal format.
is_multianimal = data.columns[0][0] == "individuals"
is_new_format = data.columns[1][1].startswith("Unnamed")

if is_multianimal:
# Reload with additional header rows if using new format.
data = pd.read_csv(filename, header=[1, 2, 3])

# Pull out animal and node names from the columns.
start_col = 3 if is_new_format else 1
animal_names = []
node_names = []
for animal_name, node_name, _ in data.columns[1:][::2]:
for animal_name, node_name, _ in data.columns[start_col:][::2]:
if animal_name not in animal_names:
animal_names.append(animal_name)
if node_name not in node_names:
Expand All @@ -136,7 +138,12 @@ def read_frames(
skeleton.add_nodes(node_names)

# Get list of all images filenames.
img_files = data.iloc[:, 0]
if is_new_format:
# New format has folder name and filename in separate columns.
img_files = [f"{a}/{b}" for a, b in zip(data.iloc[:, 0], data.iloc[:, 2])]
else:
# Old format has filenames in a single column.
img_files = data.iloc[:, 0]

if full_video:
video = full_video
Expand Down
8 changes: 8 additions & 0 deletions tests/data/dlc/madlc_testdata_v2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
scorer,,,Scorer,Scorer,Scorer,Scorer,Scorer,Scorer,Scorer,Scorer,Scorer,Scorer,Scorer,Scorer
individuals,,,Animal1,Animal1,Animal1,Animal1,Animal1,Animal1,Animal2,Animal2,Animal2,Animal2,Animal2,Animal2
bodyparts,,,A,A,B,B,C,C,A,A,B,B,C,C
coords,,,x,y,x,y,x,y,x,y,x,y,x,y
labeled-data,species,img000.png,0,1,2,3,4,5,6,7,8,9,10,11
labeled-data,species,img001.png,12,13,,,15,16,17,18,,,20,21
labeled-data,species,img002.png,,,,,,,,,,,,
labeled-data,species,img003.png,22,23,24,25,26,27,,,,,,
8 changes: 6 additions & 2 deletions tests/io/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,13 @@ def test_matching_adaptor():
)


def test_madlc():
@pytest.mark.parametrize(
"test_data",
["tests/data/dlc/madlc_testdata.csv", "tests/data/dlc/madlc_testdata_v2.csv"],
)
def test_madlc(test_data):
labels = read(
"tests/data/dlc/madlc_testdata.csv",
test_data,
for_object="labels",
as_format="deeplabcut",
)
Expand Down

0 comments on commit 1c57287

Please sign in to comment.