-
Notifications
You must be signed in to change notification settings - Fork 11
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
Adding support for more .nwb training data to SLEAP #104
base: main
Are you sure you want to change the base?
Changes from all commits
70a34ed
f2fd6f5
6c062ec
f7d8a0c
23a5a83
cf4bcf7
8948e92
be6ccbc
0aabd86
85d47ab
1b8e08b
aab5a79
66edcd6
1b70d5c
c59f996
50da4a1
c8c173a
0742bdf
055e3e8
dcc3da8
0ba071e
0c7f048
6a6c38c
b15471e
dd522f9
22ed734
7d1737d
6f1a7d1
51d5623
e511efa
42da933
e246f8b
13d3a47
e579ebf
90f0ec4
24634a1
4819536
432c343
7cb04c4
a66cf8f
8f2271b
f510765
183cf55
7e2d138
846856b
f88ca17
0a8fc45
73b41b0
a9be0eb
1031247
68c7cfb
3bf7c8a
6d12f39
e94c14d
dac1eb5
593c139
68133e1
d66b858
0abaca4
4ba67ea
3bac601
9699be2
f01e03a
9a4422d
55ca394
c91e74c
63faf8b
56911d6
ac6bb23
0be5813
46f63c6
1cd6180
a2f96ab
30fbe6f
6a0cf27
52d45b8
bab0917
a268e1f
c57e572
e7b9fd3
e932340
6f3aabe
7a83046
1a7e58b
90fd397
d8294b7
c3b286e
dedc06d
141d1dd
67a62c2
bf7fadf
62dc540
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ dependencies: | |
- pydocstyle | ||
- toml | ||
- twine | ||
- build | ||
- python-build | ||
- pip | ||
- pip: | ||
- "--editable=.[dev]" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,21 +59,57 @@ def load_nwb(filename: str) -> Labels: | |
return nwb.read_nwb(filename) | ||
|
||
|
||
def save_nwb(labels: Labels, filename: str, append: bool = True): | ||
def save_nwb( | ||
labels: Labels, | ||
filename: str, | ||
as_training: bool = False, | ||
append: bool = True, | ||
frame_inds: Optional[list[int]] = None, | ||
frame_path: Optional[str] = None, | ||
image_format: str = "png", | ||
): | ||
Comment on lines
+62
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Refactor The function signature has been updated to include several new parameters. However, the function body can be refactored to reduce complexity and improve readability. Consider the following suggestions:
Here's a refactored version of the function: def save_nwb(
labels: Labels,
filename: str,
as_training: bool = False,
append: bool = True,
training_frame_indices: Optional[list[int]] = None,
training_frame_path: Optional[str] = None,
training_image_format: str = "png",
):
"""Save a SLEAP dataset to NWB format.
Args:
labels: A SLEAP `Labels` object (see `load_slp`).
filename: Path to NWB file to save to. Must end in `.nwb`.
as_training: If `True`, save the dataset as a training dataset.
append: If `True` (the default), append to existing NWB file. File will be created if it does not exist.
training_frame_indices: Optional list of labeled frame indices within the Labels to save when saving in training data format.
training_frame_path: The path to a folder to save the extracted frame images to when saving in training data format.
training_image_format: The image format to use when saving extracted frame images.
See also: nwb.write_nwb, nwb.append_nwb, nwb.append_nwb_training
"""
nwb_funcs = {
True: {
"append": nwb.append_nwb_training,
"write": nwb.write_nwb_training,
},
False: {
"append": nwb.append_nwb,
"write": nwb.write_nwb,
},
}
action = "append" if append and Path(filename).exists() else "write"
nwb_funcs[as_training][action](
labels,
filename,
frame_inds=training_frame_indices,
frame_path=training_frame_path,
image_format=training_image_format,
) |
||
"""Save a SLEAP dataset to NWB format. | ||
|
||
Args: | ||
labels: A SLEAP `Labels` object (see `load_slp`). | ||
filename: Path to NWB file to save to. Must end in `.nwb`. | ||
as_training: If `True`, save the dataset as a training dataset. This will use | ||
data structures from `ndx-pose` that do not assume the data is a continuous | ||
timeseries. This is useful for saving training data which includes frames | ||
from random videos. If `False` (the default), save the dataset as a | ||
timeseries, assuming the data is continuous. This is useful for saving | ||
tracked predictions that is used for downstream analysis. | ||
append: If `True` (the default), append to existing NWB file. File will be | ||
created if it does not exist. | ||
|
||
See also: nwb.write_nwb, nwb.append_nwb | ||
frame_inds: Optional list of labeled frame indices within the Labels to save | ||
when saving in training data format. If `None`, all labeled frames in the | ||
labels will be saved. No effect if `as_training` is `False`. | ||
frame_path: The path to a folder to save the extracted frame images to when | ||
saving in training data format. If `None`, the path is the NWB filename | ||
without the extension. No effect if `as_training` is `False`. | ||
image_format: The image format to use when saving extracted frame images. | ||
Defaults to "png". No effect if `as_training` is `False`. | ||
|
||
See also: nwb.write_nwb, nwb.append_nwb, nwb.append_nwb_training | ||
""" | ||
if append and Path(filename).exists(): | ||
nwb.append_nwb(labels, filename) | ||
nwb.append_nwb( | ||
labels, | ||
filename, | ||
as_training=as_training, | ||
frame_inds=frame_inds, | ||
frame_path=frame_path, | ||
image_format=image_format, | ||
) | ||
Comment on lines
+93
to
+103
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Refactor the The Here's a refactored version: nwb_funcs = {
True: {
"append": nwb.append_nwb_training,
"write": nwb.write_nwb_training,
},
False: {
"append": nwb.append_nwb,
"write": nwb.write_nwb,
},
}
action = "append" if append and Path(filename).exists() else "write"
nwb_funcs[as_training][action](
labels,
filename,
frame_inds=training_frame_indices,
frame_path=training_frame_path,
image_format=training_image_format,
) |
||
else: | ||
nwb.write_nwb(labels, filename) | ||
nwb.write_nwb( | ||
labels, | ||
filename, | ||
as_training=as_training, | ||
frame_inds=frame_inds, | ||
frame_path=frame_path, | ||
image_format=image_format, | ||
) | ||
|
||
|
||
def load_labelstudio( | ||
|
@@ -190,6 +226,8 @@ def load_file( | |
return load_jabs(filename, **kwargs) | ||
elif format == "video": | ||
return load_video(filename, **kwargs) | ||
else: | ||
raise ValueError(f"Unknown format '{format}' for filename: '{filename}'.") | ||
|
||
|
||
def save_file( | ||
|
@@ -219,8 +257,10 @@ def save_file( | |
|
||
if format == "slp": | ||
save_slp(labels, filename, **kwargs) | ||
elif format == "nwb": | ||
save_nwb(labels, filename, **kwargs) | ||
elif format in ("nwb", "nwb_predictions"): | ||
save_nwb(labels, filename, False) | ||
elif format == "nwb_training": | ||
save_nwb(labels, filename, True, frame_inds=kwargs.get("frame_inds", None)) | ||
Comment on lines
+260
to
+263
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Use a dictionary to map formats to save functions. The Here's a refactored version: format_map = {
"slp": save_slp,
"nwb": lambda l, f, **kw: save_nwb(l, f, False, **kw),
"nwb_training": lambda l, f, **kw: save_nwb(l, f, True, frame_inds=kw.pop("frame_inds", None), **kw),
"labelstudio": save_labelstudio,
"jabs": lambda l, f, **kw: save_jabs(l, kw.pop("pose_version", 5), f, **kw),
}
if format in format_map:
format_map[format](labels, filename, **kwargs)
else:
raise ValueError(f"Unknown format '{format}' for filename: '{filename}'.") |
||
elif format == "labelstudio": | ||
save_labelstudio(labels, filename, **kwargs) | ||
elif format == "jabs": | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
Dependency points to an outdated commit
The
ndx-pose
dependency is currently set to commita847ad4be75e60ef9e413b8cbfc99c616fc9fd05
, while the latest commit on the main branch is8017291ae9aee84d9f0b6f1d9426b56372cd715b
. To ensure you have the most recent updates and fixes, consider updating the dependency to the latest commit until version0.2.0
is released on PyPI.8017291ae9aee84d9f0b6f1d9426b56372cd715b
.ndx-pose>=0.2.0
and update the dependency accordingly.🔗 Analysis chain
Temporary dependency solution needs follow-up
The addition of the
ndx-pose
dependency from a specific GitHub commit is a temporary solution. While this allows for immediate use of the required features, it comes with some considerations:To ensure this temporary solution doesn't persist longer than necessary, let's check the current state of the
ndx-pose
package:Consider implementing a CI/CD process to automatically check for the availability of
ndx-pose>=0.2.0
on PyPI and create a pull request to update the dependency when it becomes available.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 518