From 413b7289d23149420064eb4a1f549b11a7564310 Mon Sep 17 00:00:00 2001 From: bjhardcastle Date: Fri, 2 Feb 2024 13:50:31 -0800 Subject: [PATCH] Add fn for getting sync file path --- pdm.lock | 16 +++++++++++++++- pyproject.toml | 1 + src/npc_sync/sync.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/pdm.lock b/pdm.lock index b4696cd..66e757b 100644 --- a/pdm.lock +++ b/pdm.lock @@ -5,7 +5,7 @@ groups = ["default", "dev", "docs"] strategy = ["cross_platform", "inherit_metadata"] lock_version = "4.4.1" -content_hash = "sha256:fa87f418fef904a0578d2e594c465918916d07bcf9ddf154213e6897c268f627" +content_hash = "sha256:bd99883dcd4fb17c0fdbaa5a345151637d5e16717da1c12ec72bde00a525d03c" [[package]] name = "aiobotocore" @@ -1511,6 +1511,20 @@ files = [ {file = "npc_io-0.1.20-py3-none-any.whl", hash = "sha256:2d773ab0cc155f8f3a30d5b22c5223c5e054e4d3c73fc779b873d721e8b4c141"}, ] +[[package]] +name = "npc-session" +version = "0.1.33" +requires_python = ">=3.9" +summary = "Basic tools for parsing subject, session, date and time associated with data from the Mindscope Neuropixels team, in the cloud." +groups = ["default"] +dependencies = [ + "typing-extensions>=4.7.1", +] +files = [ + {file = "npc_session-0.1.33-py3-none-any.whl", hash = "sha256:a26a2dfc5f88b5f0b0c5ef76fe6a7dbaa63c0eea2df8c7390e74e5e5d3364d6e"}, + {file = "npc_session-0.1.33.tar.gz", hash = "sha256:3f008c58e0f4a9b6ed76b413b91e8525e5ff6a0e8c9cc3960ab9e893443b8d52"}, +] + [[package]] name = "numpy" version = "1.26.3" diff --git a/pyproject.toml b/pyproject.toml index 0faf930..4219ee5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,7 @@ dependencies = [ "matplotlib>=3.8.2", "typing-extensions>=4.9.0", "npc-io>=0.1.20", + "npc-session>=0.1.33", ] version = "0.1.4" classifiers = [ diff --git a/src/npc_sync/sync.py b/src/npc_sync/sync.py index daea3c7..b2b8b0f 100644 --- a/src/npc_sync/sync.py +++ b/src/npc_sync/sync.py @@ -17,9 +17,11 @@ import h5py import npc_io +import npc_session import numpy as np import numpy.typing as npt from typing_extensions import Self, TypeAlias +import upath if TYPE_CHECKING: import matplotlib.axes @@ -41,6 +43,46 @@ def get_sync_data(sync_path_or_data: SyncPathOrDataset) -> SyncDataset: return sync_path_or_data return SyncDataset(sync_path_or_data) +def get_single_sync_path(dir_or_paths: npc_io.PathLike | Iterable[npc_io.PathLike], date: str | datetime.date | datetime.datetime | npc_session.DateRecord | npc_session.DatetimeRecord | None = None) -> upath.UPath: + """From an iterable of paths, return the one with the expected date and a + .sync or .h5 extension. + + >>> get_single_sync_path('s3://aind-ephys-data/ecephys_676909_2023-12-14_12-43-11/behavior_videos') + S3Path('s3://aind-ephys-data/ecephys_676909_2023-12-14_12-43-11/behavior_videos/20231214T124311.h5') + """ + if isinstance(dir_or_paths, str): + dir_or_paths = npc_io.from_pathlike(dir_or_paths) + if not isinstance(dir_or_paths, Iterable): + if (d := npc_io.from_pathlike(dir_or_paths)).is_dir(): + dir_or_paths = d.iterdir() + else: + dir_or_paths = tuple(dir_or_paths) + + if date is not None: + try: + date = npc_session.DatetimeRecord(date) + except ValueError: + date = npc_session.DateRecord(date) + + sync_files = [] + for p in (npc_io.from_pathlike(p) for p in dir_or_paths): + if p.suffix not in (".sync", ".h5"): + continue + if date is not None: + try: + if isinstance(date, npc_session.DatetimeRecord): + file_date = npc_session.DatetimeRecord(file_date) + else: + file_date = npc_session.DateRecord(file_date) + except ValueError: + continue + if file_date != date: + continue + sync_files.append(p) + + if not len(sync_files) == 1: + raise ValueError(f"Expected 1 sync file, found {sync_files = }") + return sync_files[0] def get_bit(uint_array: npt.NDArray, bit: int) -> npt.NDArray[np.uint8]: """