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

Add the option for multiple views loading - take2 #346

Merged
merged 5 commits into from
Nov 22, 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
35 changes: 35 additions & 0 deletions movement/io/load_poses.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,41 @@ def from_dlc_file(
)


def from_multiview_files(
file_path_dict: dict[str, Path | str],
source_software: Literal["DeepLabCut", "SLEAP", "LightningPose"],
fps: float | None = None,
) -> xr.Dataset:
"""Load and merge pose tracking data from multiple views (cameras).

Parameters
----------
file_path_dict : dict[str, Union[Path, str]]
A dict whose keys are the view names and values are the paths to load.
source_software : {'LightningPose', 'SLEAP', 'DeepLabCut'}
The source software of the file.
fps : float, optional
The number of frames per second in the video. If None (default),
the `time` coordinates will be in frame numbers.

Returns
-------
xarray.Dataset
Dataset containing the pose tracks, confidence scores, and metadata,
with an additional views dimension.
vigji marked this conversation as resolved.
Show resolved Hide resolved

"""
views_list = list(file_path_dict.keys())
new_coord_views = xr.DataArray(views_list, dims="view")

dataset_list = [
from_file(f, source_software=source_software, fps=fps)
for f in file_path_dict.values()
]

return xr.concat(dataset_list, dim=new_coord_views)


def _ds_from_lp_or_dlc_file(
file_path: Path | str,
source_software: Literal["LightningPose", "DeepLabCut"],
Expand Down
17 changes: 17 additions & 0 deletions tests/test_unit/test_load_poses.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,20 @@ def test_from_numpy_valid(
source_software=source_software,
)
self.assert_dataset(ds, expected_source_software=source_software)

def test_from_multiview_files(self):
"""Test that the from_file() function delegates to the correct
loader function according to the source_software.
"""
view_names = ["view_0", "view_1"]
file_path_dict = {
view: DATA_PATHS.get("DLC_single-wasp.predictions.h5")
for view in view_names
}
multi_view_ds = load_poses.from_multiview_files(
file_path_dict, source_software="DeepLabCut"
)

assert isinstance(multi_view_ds, xr.Dataset)
assert "view" in multi_view_ds.dims
assert multi_view_ds.view.values.tolist() == view_names
Loading