w2t-bkin 0.0.9__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- w2t_bkin-0.0.9/LICENSE +201 -0
- w2t_bkin-0.0.9/PKG-INFO +298 -0
- w2t_bkin-0.0.9/README.md +260 -0
- w2t_bkin-0.0.9/pyproject.toml +56 -0
- w2t_bkin-0.0.9/src/w2t_bkin/__init__.py +89 -0
- w2t_bkin-0.0.9/src/w2t_bkin/cli.py +344 -0
- w2t_bkin-0.0.9/src/w2t_bkin/config.py +642 -0
- w2t_bkin-0.0.9/src/w2t_bkin/core/__init__.py +0 -0
- w2t_bkin-0.0.9/src/w2t_bkin/core/pipeline.py +931 -0
- w2t_bkin-0.0.9/src/w2t_bkin/core/session.py +564 -0
- w2t_bkin-0.0.9/src/w2t_bkin/core/validate.py +88 -0
- w2t_bkin-0.0.9/src/w2t_bkin/exceptions.py +426 -0
- w2t_bkin-0.0.9/src/w2t_bkin/ingest/__init__.py +0 -0
- w2t_bkin-0.0.9/src/w2t_bkin/ingest/behavior.py +1080 -0
- w2t_bkin-0.0.9/src/w2t_bkin/ingest/bpod.py +536 -0
- w2t_bkin-0.0.9/src/w2t_bkin/ingest/pose.py +706 -0
- w2t_bkin-0.0.9/src/w2t_bkin/ingest/ttl.py +569 -0
- w2t_bkin-0.0.9/src/w2t_bkin/processors/__init__.py +0 -0
- w2t_bkin-0.0.9/src/w2t_bkin/processors/dlc.py +573 -0
- w2t_bkin-0.0.9/src/w2t_bkin/processors/facemap.py +0 -0
- w2t_bkin-0.0.9/src/w2t_bkin/processors/transcode.py +0 -0
- w2t_bkin-0.0.9/src/w2t_bkin/sync/__init__.py +72 -0
- w2t_bkin-0.0.9/src/w2t_bkin/sync/core.py +666 -0
- w2t_bkin-0.0.9/src/w2t_bkin/sync/stats.py +168 -0
- w2t_bkin-0.0.9/src/w2t_bkin/sync/timebase.py +301 -0
- w2t_bkin-0.0.9/src/w2t_bkin/sync/ttl.py +254 -0
- w2t_bkin-0.0.9/src/w2t_bkin/sync/ttl_mock.py +477 -0
- w2t_bkin-0.0.9/src/w2t_bkin/tasks/__init__.py +26 -0
- w2t_bkin-0.0.9/src/w2t_bkin/tasks/base.py +207 -0
- w2t_bkin-0.0.9/src/w2t_bkin/tasks/dlc.py +270 -0
- w2t_bkin-0.0.9/src/w2t_bkin/tasks/sleap.py +158 -0
- w2t_bkin-0.0.9/src/w2t_bkin/utils.py +1215 -0
w2t_bkin-0.0.9/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
w2t_bkin-0.0.9/PKG-INFO
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: w2t_bkin
|
|
3
|
+
Version: 0.0.9
|
|
4
|
+
Summary: Mouse wiskers body kinematics and behaviour
|
|
5
|
+
Author: Larkum Lab
|
|
6
|
+
Requires-Python: ~=3.10.0
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Requires-Dist: pydantic~=2.12.0
|
|
14
|
+
Requires-Dist: pydantic-settings~=2.11.0
|
|
15
|
+
Requires-Dist: tomli~=2.3.0
|
|
16
|
+
Requires-Dist: facemap~=1.0.0
|
|
17
|
+
Requires-Dist: deeplabcut[tf]~=2.3.0
|
|
18
|
+
Requires-Dist: pynwb~=3.1.0
|
|
19
|
+
Requires-Dist: hdmf~=4.1.0
|
|
20
|
+
Requires-Dist: ndx-pose~=0.2.0
|
|
21
|
+
Requires-Dist: ndx-events~=0.4.0
|
|
22
|
+
Requires-Dist: ndx-structured-behavior~=0.1.0
|
|
23
|
+
Requires-Dist: ffmpeg-python~=0.2.0
|
|
24
|
+
Requires-Dist: scipy~=1.15.0
|
|
25
|
+
Requires-Dist: pandas~=2.3.0
|
|
26
|
+
Requires-Dist: h5py~=3.15.0
|
|
27
|
+
Requires-Dist: tables~=3.8.0
|
|
28
|
+
Requires-Dist: nwbinspector~=0.6.5
|
|
29
|
+
Requires-Dist: typer~=0.20.0
|
|
30
|
+
Requires-Dist: rich~=14.2.0
|
|
31
|
+
Requires-Dist: black~=25.9.0 ; extra == "dev"
|
|
32
|
+
Requires-Dist: isort~=7.0.0 ; extra == "dev"
|
|
33
|
+
Requires-Dist: pytest~=9.0.0 ; extra == "dev"
|
|
34
|
+
Requires-Dist: matplotlib~=3.8.0 ; extra == "dev"
|
|
35
|
+
Requires-Dist: numpy~=1.26.0 ; extra == "dev"
|
|
36
|
+
Provides-Extra: dev
|
|
37
|
+
|
|
38
|
+
# W2T Body Kinematics Pipeline (w2t-bkin)
|
|
39
|
+
|
|
40
|
+
A modular, reproducible Python pipeline for processing multi-camera rodent behavior recordings. It integrates synchronization, pose estimation (DeepLabCut/SLEAP), facial metrics, and behavioral events into standardized **NWB (Neurodata Without Borders)** datasets.
|
|
41
|
+
|
|
42
|
+
## Key Features
|
|
43
|
+
|
|
44
|
+
- **NWB-First Architecture**: Produces NWB-native data structures directly, eliminating intermediate conversion layers.
|
|
45
|
+
- **Hierarchical Metadata**: Supports cascading configuration from global → subject → session levels for efficient metadata management.
|
|
46
|
+
- **Bpod Integration**: Parses Bpod `.mat` files and converts them to `ndx-structured-behavior` format.
|
|
47
|
+
- **Pose Estimation**: Imports and harmonizes data from DeepLabCut and SLEAP into `ndx-pose`.
|
|
48
|
+
- **Synchronization**: Robust alignment of behavioral data and video frames to a common timebase using TTL pulses.
|
|
49
|
+
- **Modular Design**: Distinct modules for behavior, pose, sync, and session management.
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
|
|
53
|
+
The project requires Python ~3.10.
|
|
54
|
+
|
|
55
|
+
1. **Install `ndx-structured-behavior`** (currently required from source):
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
git clone https://github.com/rly/ndx-structured-behavior.git
|
|
59
|
+
pip install -U ./ndx-structured-behavior
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
2. **Install `w2t-bkin`**:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pip install w2t-bkin
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Project Structure
|
|
69
|
+
|
|
70
|
+
```text
|
|
71
|
+
project/
|
|
72
|
+
├── config.toml # Pipeline configuration
|
|
73
|
+
├── data/
|
|
74
|
+
│ ├── raw/ # Raw data organized by subject/session
|
|
75
|
+
│ │ ├── metadata.toml # Optional: Global metadata (lab-wide defaults)
|
|
76
|
+
│ │ └── subject-001/
|
|
77
|
+
│ │ ├── subject.toml # Optional: Subject-specific metadata
|
|
78
|
+
│ │ └── session-001/
|
|
79
|
+
│ │ ├── session.toml # Session-specific NWB metadata
|
|
80
|
+
│ │ ├── Video/ # Raw video files
|
|
81
|
+
│ │ ├── TTLs/ # TTL pulse timestamps
|
|
82
|
+
│ │ └── Bpod/ # Bpod behavior files
|
|
83
|
+
│ ├── interim/ # Processed data (pose estimation, etc.)
|
|
84
|
+
│ │ └── subject-001/
|
|
85
|
+
│ │ └── session-001/
|
|
86
|
+
│ │ └── Pose/
|
|
87
|
+
│ └── processed/ # Final NWB output files
|
|
88
|
+
└── models/ # Pose estimation models (DLC/SLEAP)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Configuration
|
|
92
|
+
|
|
93
|
+
The pipeline uses TOML for configuration:
|
|
94
|
+
|
|
95
|
+
### Pipeline Configuration (`config.toml`)
|
|
96
|
+
|
|
97
|
+
Defines paths, timebase, and synchronization settings:
|
|
98
|
+
|
|
99
|
+
```toml
|
|
100
|
+
[project]
|
|
101
|
+
name = "my-experiment"
|
|
102
|
+
|
|
103
|
+
[paths]
|
|
104
|
+
raw_root = "data/raw"
|
|
105
|
+
intermediate_root = "data/interim"
|
|
106
|
+
output_root = "data/processed"
|
|
107
|
+
models_root = "models"
|
|
108
|
+
root_metadata = "data/raw/metadata.toml" # Optional global metadata
|
|
109
|
+
|
|
110
|
+
[synchronization]
|
|
111
|
+
strategy = "hardware_pulse"
|
|
112
|
+
reference_channel = "ttl_camera"
|
|
113
|
+
|
|
114
|
+
[synchronization.alignment]
|
|
115
|
+
method = "nearest"
|
|
116
|
+
tolerance_s = 0.001
|
|
117
|
+
|
|
118
|
+
[[bpod.sync.trial_types]]
|
|
119
|
+
trial_type = 1
|
|
120
|
+
sync_signal = "W2T_Audio"
|
|
121
|
+
sync_ttl = "ttl_cue"
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Hierarchical Metadata
|
|
125
|
+
|
|
126
|
+
Metadata is loaded and merged from multiple levels (later files override earlier ones):
|
|
127
|
+
|
|
128
|
+
1. **`root_metadata`** (optional): Lab/project-wide defaults
|
|
129
|
+
2. **`raw_root/metadata.toml`** (optional): Experiment-wide settings
|
|
130
|
+
3. **`raw_root/subject_id/subject.toml`** (optional): Subject-specific metadata
|
|
131
|
+
4. **`raw_root/subject_id/session_id/session.toml`**: Session-specific NWB metadata
|
|
132
|
+
|
|
133
|
+
Example `session.toml`:
|
|
134
|
+
|
|
135
|
+
```toml
|
|
136
|
+
session_description = "Behavioral training with pose tracking"
|
|
137
|
+
identifier = "session-001"
|
|
138
|
+
session_start_time = "2025-11-21T14:30:00Z"
|
|
139
|
+
experimenter = ["Esteban, Borja"]
|
|
140
|
+
institution = "My Lab"
|
|
141
|
+
lab = "Neuroscience Lab"
|
|
142
|
+
|
|
143
|
+
[subject]
|
|
144
|
+
subject_id = "subject-001"
|
|
145
|
+
species = "Mus musculus"
|
|
146
|
+
sex = "M"
|
|
147
|
+
age = "P90D"
|
|
148
|
+
|
|
149
|
+
# Camera configuration
|
|
150
|
+
[[cameras]]
|
|
151
|
+
id = "camera_0" # Must match a device name in [[devices]]
|
|
152
|
+
paths = "Video/cam0_*.avi" # Glob pattern for video files
|
|
153
|
+
fps = 150.0 # Acquisition frame rate (defaults to 30.0 if omitted)
|
|
154
|
+
ttl_id = "ttl_camera" # Associated TTL stream for synchronization
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Quick Start
|
|
158
|
+
|
|
159
|
+
### Using the High-Level Helper
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
from pathlib import Path
|
|
163
|
+
from w2t_bkin.config import load_config
|
|
164
|
+
from w2t_bkin.utils import load_session_metadata_and_nwb
|
|
165
|
+
|
|
166
|
+
# Load configuration
|
|
167
|
+
config = load_config("config.toml")
|
|
168
|
+
|
|
169
|
+
# Load hierarchical metadata and create NWBFile in one step
|
|
170
|
+
metadata, nwbfile = load_session_metadata_and_nwb(
|
|
171
|
+
config=config,
|
|
172
|
+
subject_id="subject-001",
|
|
173
|
+
session_id="session-001"
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
# Continue with your pipeline...
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Manual Approach
|
|
180
|
+
|
|
181
|
+
```python
|
|
182
|
+
from pathlib import Path
|
|
183
|
+
from w2t_bkin import config, sync
|
|
184
|
+
from w2t_bkin.core import session
|
|
185
|
+
from w2t_bkin.ingest import behavior, bpod, ttl
|
|
186
|
+
|
|
187
|
+
# 1. Load Configuration
|
|
188
|
+
settings = config.load_config("config.toml")
|
|
189
|
+
|
|
190
|
+
# 2. Build metadata paths and load hierarchically
|
|
191
|
+
metadata_paths = session.build_metadata_paths(
|
|
192
|
+
raw_root=settings.paths.raw_root,
|
|
193
|
+
subject_id="subject-001",
|
|
194
|
+
session_id="session-001",
|
|
195
|
+
root_metadata=settings.paths.root_metadata
|
|
196
|
+
)
|
|
197
|
+
metadata = session.load_metadata(metadata_paths)
|
|
198
|
+
|
|
199
|
+
# 3. Create NWBFile
|
|
200
|
+
nwbfile = session.create_nwb_file(metadata)
|
|
201
|
+
|
|
202
|
+
# 4. Get session directory
|
|
203
|
+
session_dir = settings.paths.raw_root / "subject-001" / "session-001"
|
|
204
|
+
|
|
205
|
+
# 5. Import TTL Signals
|
|
206
|
+
ttl_patterns = {
|
|
207
|
+
"ttl_camera": "TTLs/*.xa_7_0*.txt",
|
|
208
|
+
"ttl_cue": "TTLs/*.xia_3_0*.txt",
|
|
209
|
+
}
|
|
210
|
+
ttl_pulses = ttl.get_ttl_pulses(session_dir, ttl_patterns)
|
|
211
|
+
|
|
212
|
+
# 6. Parse Bpod Data
|
|
213
|
+
bpod_data = bpod.parse_bpod(
|
|
214
|
+
session_dir=session_dir,
|
|
215
|
+
pattern="Bpod/*.mat",
|
|
216
|
+
order="name_asc"
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
# 7. Synchronize Bpod to TTL
|
|
220
|
+
trial_offsets, warnings = sync.align_bpod_trials_to_ttl(
|
|
221
|
+
trial_type_configs=settings.bpod.sync.trial_types,
|
|
222
|
+
bpod_data=bpod_data,
|
|
223
|
+
ttl_pulses=ttl_pulses,
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
# 8. Extract Behavioral Data (NWB objects)
|
|
227
|
+
task, recording, trials = behavior.extract_behavioral_data(
|
|
228
|
+
bpod_data,
|
|
229
|
+
trial_offsets
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
# 9. Add to NWB
|
|
233
|
+
nwbfile.add_lab_meta_data(task)
|
|
234
|
+
nwbfile.add_acquisition(recording.states)
|
|
235
|
+
nwbfile.add_acquisition(recording.events)
|
|
236
|
+
nwbfile.add_acquisition(recording.actions)
|
|
237
|
+
nwbfile.trials = trials
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## Examples
|
|
241
|
+
|
|
242
|
+
The `examples/` directory contains complete working examples:
|
|
243
|
+
|
|
244
|
+
- **`bpod_camera_sync.py`**: Demonstrates Bpod-camera synchronization with TTL alignment
|
|
245
|
+
- **`pose_camera_nwb.py`**: Shows pose estimation data import and NWB file creation
|
|
246
|
+
- **`sync_recovery_demo.py`**: Robust sync recovery with missing TTL pulses
|
|
247
|
+
|
|
248
|
+
Run an example:
|
|
249
|
+
|
|
250
|
+
```bash
|
|
251
|
+
python examples/pose_camera_nwb.py
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
## Module Overview
|
|
255
|
+
|
|
256
|
+
| Module | Description |
|
|
257
|
+
| :------------------------- | :------------------------------------------------------------------------------------------------- |
|
|
258
|
+
| `w2t_bkin.ingest.behavior` | Converts Bpod data into `ndx-structured-behavior` classes (StatesTable, EventsTable, TrialsTable). |
|
|
259
|
+
| `w2t_bkin.ingest.bpod` | Low-level parsing and validation of Bpod `.mat` files. |
|
|
260
|
+
| `w2t_bkin.ingest.pose` | Imports pose estimation data (DLC/SLEAP) and builds `ndx-pose` objects (PoseEstimation, Skeleton). |
|
|
261
|
+
| `w2t_bkin.ingest.ttl` | Loads hardware TTL pulse timestamps and creates `ndx-events` tables. |
|
|
262
|
+
| `w2t_bkin.sync` | Handles timebase alignment, jitter calculation, and synchronization of video/behavior to TTLs. |
|
|
263
|
+
| `w2t_bkin.core.session` | Loads metadata hierarchically and assembles the root `NWBFile`. |
|
|
264
|
+
| `w2t_bkin.core.pipeline` | High-level orchestration of the entire workflow. |
|
|
265
|
+
| `w2t_bkin.utils` | Shared utilities including datetime parsing, dictionary merging, and helper functions. |
|
|
266
|
+
|
|
267
|
+
## CLI Utilities
|
|
268
|
+
|
|
269
|
+
The `scripts/` directory contains useful utilities:
|
|
270
|
+
|
|
271
|
+
- `mat2json.py`: Converts MATLAB `.mat` files to JSON, handling nested structures and arrays.
|
|
272
|
+
- `pose2ttl.py`: Generates mock TTL signals from DeepLabCut pose data (useful for testing or when hardware sync fails).
|
|
273
|
+
- `trials2df.py`: Converts NWB `TrialsTable` and `TaskRecording` objects into a flat pandas DataFrame for analysis.
|
|
274
|
+
|
|
275
|
+
## Testing
|
|
276
|
+
|
|
277
|
+
The project includes synthetic data generation for testing:
|
|
278
|
+
|
|
279
|
+
```python
|
|
280
|
+
from synthetic import build_raw_folder, build_interim_pose
|
|
281
|
+
|
|
282
|
+
# Generate synthetic session
|
|
283
|
+
session = build_raw_folder(
|
|
284
|
+
out_root=Path("output/test/raw"),
|
|
285
|
+
project_name="test-project",
|
|
286
|
+
subject_id="subject-001",
|
|
287
|
+
session_id="session-001",
|
|
288
|
+
camera_ids=["cam0", "cam1"],
|
|
289
|
+
ttl_ids=["ttl_camera", "ttl_bpod"],
|
|
290
|
+
n_frames=300,
|
|
291
|
+
n_trials=10,
|
|
292
|
+
)
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
## License
|
|
296
|
+
|
|
297
|
+
See `LICENSE` file for details.
|
|
298
|
+
|