8000 Import multiple DLC datasets from a folder · Pull Request #437 · talmolab/sleap · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Import multiple DLC datasets from a folder #437

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

Merged
merged 10 commits into from
Dec 24, 2020
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: 6 additions & 0 deletions sleap/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ def add_submenu_choices(menu, title, options, key):
"DeepLabCut dataset...",
self.commands.importDLC,
)
add_menu_item(
import_types_menu,
"import_dlc_folder",
"Multiple DeepLabCut datasets from folder...",
self.commands.importDLCFolder,
)
add_menu_item(
import_types_menu,
"import_dpk",
Expand Down
47 changes: 47 additions & 0 deletions sleap/gui/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class which inherits from `AppCommand` (or a more specialized class such as

from abc import ABC
from enum import Enum
from glob import glob
from pathlib import PurePath
from typing import Callable, Dict, Iterator, List, Optional, Type, Tuple

Expand Down Expand Up @@ -263,6 +264,10 @@ def importDLC(self):
"""Imports DeepLabCut datasets."""
self.execute(ImportDeepLabCut)

def importDLCFolder(self):
"""Imports multiple DeepLabCut datasets."""
self.execute(ImportDeepLabCutFolder)

def importLEAP(self):
"""Imports LEAP matlab datasets."""
self.execute(ImportLEAP)
Expand Down Expand Up @@ -721,6 +726,48 @@ def ask(context: "CommandContext", params: dict) -> bool:
return True


class ImportDeepLabCutFolder(AppCommand):
@staticmethod
def do_action(context: "CommandContext", params: dict):
csv_files = ImportDeepLabCutFolder.find_dlc_files_in_folder(params['folder_name'])
if csv_files:
win = MessageDialog(f"Importing {len(csv_files)} DeepLabCut datasets...", context.app)
merged_labels = ImportDeepLabCutFolder.import_labels_from_dlc_files(csv_files)
win.hide()

new_window = context.app.__class__()
new_window.showMaximized()
new_window.loadLabelsObject(labels=merged_labels)

@staticmethod
def ask(context: "CommandContext", params: dict) -> bool:
folder_name = FileDialog.openDir(
context.app,
dir=None,
caption="Select a folder with DeepLabCut datasets...",
)

if len(folder_name) == 0:
return False
params["folder_name"] = folder_name
return True

@staticmethod
def find_dlc_files_in_folder(folder_name: str) -> List[str]:
return glob(f"{folder_name}/*/*.csv")

@staticmethod
def import_labels_from_dlc_files(csv_files: List[str]) -> Labels:
merged_labels = None
for csv_file in csv_files:
labels = Labels.load_file(csv_file, as_format="deeplabcut")
if merged_labels is None:
merged_labels = labels
else:
merged_labels.extend_from(labels, unify=True)
return merged_labels


class ImportAnalysisFile(AppCommand):
@staticmethod
def do_action(context: "CommandContext", params: dict):
Expand Down
6 changes: 6 additions & 0 deletions tests/data/dlc_multiple_datasets/video1/dlc_dataset_1.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
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/video/img000.jpg,0,1,2,3,4,5,6,7,8,9,10,11
labeled-data/video/img001.jpg,12,13,,,15,16,17,18,,,20,21
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions tests/data/dlc_multiple_datasets/video2/dlc_dataset_2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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/video/img002.jpg,22,23,24,25,26,27,,,,,,
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 26 additions & 1 deletion tests/gui/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from sleap.gui.commands import CommandContext
from sleap.gui.commands import CommandContext, ImportDeepLabCutFolder
from sleap.io.pathutils import fix_path_separator


def test_delete_user_dialog(centered_pair_predictions):
Expand All @@ -15,3 +16,27 @@ def test_delete_user_dialog(centered_pair_predictions):

# Make sure we now have user instances
assert len(context.state["labeled_frame"].user_instances) == 2


def test_import_labels_from_dlc_folder():
csv_files = ImportDeepLabCutFolder.find_dlc_files_in_folder('tests/data/dlc_multiple_datasets')
assert set([fix_path_separator(f) for f in csv_files]) == {
'tests/data/dlc_multiple_datasets/video2/dlc_dataset_2.csv',
'tests/data/dlc_multiple_datasets/video1/dlc_dataset_1.csv',
}

labels = ImportDeepLabCutFolder.import_labels_from_dlc_files(csv_files)

assert len(labels) == 3
assert len(labels.videos) == 2
assert len(labels.skeletons) == 1
assert len(labels.nodes) == 3
assert len(labels.tracks) == 0

assert set([fix_path_separator(l.video.backend.filename) for l in labels.labeled_frames]) == {
'tests/data/dlc_multiple_datasets/video2/img002.jpg',
'tests/data/dlc_multiple_datasets/video1/img000.jpg',
'tests/data/dlc_multiple_datasets/video1/img000.jpg',
}

assert set([l.frame_idx for l in labels.labeled_frames]) == {0, 0, 1}
0