neurodash 0.0.1__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.
- neurodash-0.0.1/LICENSE +201 -0
- neurodash-0.0.1/PKG-INFO +43 -0
- neurodash-0.0.1/README.md +19 -0
- neurodash-0.0.1/pyproject.toml +41 -0
- neurodash-0.0.1/src/neurodash/__init__.py +0 -0
- neurodash-0.0.1/src/neurodash/assets/logo/neurodash_logo.png +0 -0
- neurodash-0.0.1/src/neurodash/assets/logo/neurodash_logo_200.png +0 -0
- neurodash-0.0.1/src/neurodash/assets/logo/neurodash_logo_notext.png +0 -0
- neurodash-0.0.1/src/neurodash/assets/logo/neurodash_logo_trimmed.png +0 -0
- neurodash-0.0.1/src/neurodash/behavior_io.py +293 -0
- neurodash-0.0.1/src/neurodash/callbacks.py +429 -0
- neurodash-0.0.1/src/neurodash/config.py +29 -0
- neurodash-0.0.1/src/neurodash/file_picker.py +69 -0
- neurodash-0.0.1/src/neurodash/layout.py +244 -0
- neurodash-0.0.1/src/neurodash/neural_io.py +104 -0
- neurodash-0.0.1/src/neurodash/plot_utils.py +313 -0
- neurodash-0.0.1/src/neurodash/pyqtdash/__init__.py +1 -0
- neurodash-0.0.1/src/neurodash/pyqtdash/launch.py +33 -0
- neurodash-0.0.1/src/neurodash/pyqtdash/main_window.py +514 -0
- neurodash-0.0.1/src/neurodash/session.py +131 -0
- neurodash-0.0.1/src/neurodash/spectral_utils.py +42 -0
neurodash-0.0.1/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.
|
neurodash-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: neurodash
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Web dashboard neurobehavioral data explorer.
|
|
5
|
+
Author: EricThomson
|
|
6
|
+
Author-email: EricThomson <thomson.eric@gmail.com>
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: dash
|
|
10
|
+
Requires-Dist: plotly
|
|
11
|
+
Requires-Dist: neo
|
|
12
|
+
Requires-Dist: quantities
|
|
13
|
+
Requires-Dist: lspopt
|
|
14
|
+
Requires-Dist: numpy
|
|
15
|
+
Requires-Dist: scipy
|
|
16
|
+
Requires-Dist: pandas
|
|
17
|
+
Requires-Dist: openpyxl
|
|
18
|
+
Requires-Dist: imageio[ffmpeg,pyav]
|
|
19
|
+
Requires-Dist: pyqt6
|
|
20
|
+
Requires-Dist: pyqtgraph
|
|
21
|
+
Requires-Python: >=3.12
|
|
22
|
+
Project-URL: Repository, https://github.com/EricThomson/neurodash
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# neurodash
|
|
26
|
+
<img src="https://raw.githubusercontent.com/EricThomson/neurodash/main/src/neurodash/assets/logo/neurodash_logo_trimmed.png" alt="neurodash logo" align="right" width="250">
|
|
27
|
+
Lightweight dashboard for exploratory analysis of neurobehavioral data.
|
|
28
|
+
|
|
29
|
+
## Setting up and running
|
|
30
|
+
Make sure uv is installed.
|
|
31
|
+
|
|
32
|
+
uv sync
|
|
33
|
+
source .venv/Scripts/activate
|
|
34
|
+
|
|
35
|
+
Run with `python app.py` and explore away! Assumes you have neural data in plexon, and behavioral data in Excel.
|
|
36
|
+
|
|
37
|
+
## To do
|
|
38
|
+
- export csv/png
|
|
39
|
+
- docs
|
|
40
|
+
- basic how to use
|
|
41
|
+
- explain back-end stuff like channels/streaming neo/plx
|
|
42
|
+
- Testing, linting, etc
|
|
43
|
+
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# neurodash
|
|
2
|
+
<img src="https://raw.githubusercontent.com/EricThomson/neurodash/main/src/neurodash/assets/logo/neurodash_logo_trimmed.png" alt="neurodash logo" align="right" width="250">
|
|
3
|
+
Lightweight dashboard for exploratory analysis of neurobehavioral data.
|
|
4
|
+
|
|
5
|
+
## Setting up and running
|
|
6
|
+
Make sure uv is installed.
|
|
7
|
+
|
|
8
|
+
uv sync
|
|
9
|
+
source .venv/Scripts/activate
|
|
10
|
+
|
|
11
|
+
Run with `python app.py` and explore away! Assumes you have neural data in plexon, and behavioral data in Excel.
|
|
12
|
+
|
|
13
|
+
## To do
|
|
14
|
+
- export csv/png
|
|
15
|
+
- docs
|
|
16
|
+
- basic how to use
|
|
17
|
+
- explain back-end stuff like channels/streaming neo/plx
|
|
18
|
+
- Testing, linting, etc
|
|
19
|
+
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["uv_build>=0.11.26,<0.12.0"]
|
|
3
|
+
build-backend = "uv_build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "neurodash"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
description = "Web dashboard neurobehavioral data explorer."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
|
+
license = "Apache-2.0"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [
|
|
14
|
+
{ name = "EricThomson", email = "thomson.eric@gmail.com" }
|
|
15
|
+
]
|
|
16
|
+
dependencies = [
|
|
17
|
+
# Core UI
|
|
18
|
+
"dash",
|
|
19
|
+
"plotly",
|
|
20
|
+
# Neural I/O
|
|
21
|
+
"neo",
|
|
22
|
+
"quantities",
|
|
23
|
+
# Signal processing
|
|
24
|
+
"lspopt",
|
|
25
|
+
"numpy",
|
|
26
|
+
"scipy",
|
|
27
|
+
# Behavioral I/O
|
|
28
|
+
"pandas",
|
|
29
|
+
"openpyxl",
|
|
30
|
+
# Video I/O (pyqtdash)
|
|
31
|
+
"imageio[ffmpeg,pyav]",
|
|
32
|
+
# pyqtdash viewer
|
|
33
|
+
"pyqt6",
|
|
34
|
+
"pyqtgraph",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Repository = "https://github.com/EricThomson/neurodash"
|
|
39
|
+
|
|
40
|
+
[tool.uv.build-backend]
|
|
41
|
+
module-root = "src"
|
|
File without changes
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
"""
|
|
2
|
+
behavior_io.py — EthoVision behavioral data loading for neurodash.
|
|
3
|
+
|
|
4
|
+
Loads EthoVision Excel exports and provides position/motion extraction
|
|
5
|
+
and neural sync utilities.
|
|
6
|
+
|
|
7
|
+
EthoVision Excel format:
|
|
8
|
+
- Row 0: header row count (n_header)
|
|
9
|
+
- Rows 1..n_header-3: key/value metadata pairs
|
|
10
|
+
- Row n_header-2: column names
|
|
11
|
+
- Row n_header-1: units
|
|
12
|
+
- Row n_header onward: tracking data (~30 Hz)
|
|
13
|
+
|
|
14
|
+
Key columns: Recording time, X/Y center/nose/tail (cm), Velocity (cm/s),
|
|
15
|
+
Direction (deg), Distance moved (cm).
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
import re
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
import numpy as np
|
|
22
|
+
import pandas as pd
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
# ---------------------------------------------------------------------------
|
|
26
|
+
# Loading
|
|
27
|
+
# ---------------------------------------------------------------------------
|
|
28
|
+
|
|
29
|
+
def load_behavior_file(path):
|
|
30
|
+
"""Load an EthoVision Excel export.
|
|
31
|
+
|
|
32
|
+
Parameters
|
|
33
|
+
----------
|
|
34
|
+
path : str or Path
|
|
35
|
+
|
|
36
|
+
Returns
|
|
37
|
+
-------
|
|
38
|
+
metadata : dict
|
|
39
|
+
Key/value pairs from the header block.
|
|
40
|
+
data : pd.DataFrame
|
|
41
|
+
Tracking data, one row per frame. All numeric where possible;
|
|
42
|
+
non-numeric EthoVision markers become np.nan.
|
|
43
|
+
data.attrs['units'] holds a {column: unit_string} dict.
|
|
44
|
+
"""
|
|
45
|
+
raw = pd.read_excel(path, header=None)
|
|
46
|
+
n_header = int(raw.iloc[0, 1])
|
|
47
|
+
|
|
48
|
+
metadata = {}
|
|
49
|
+
for i in range(1, n_header - 2):
|
|
50
|
+
key = str(raw.iloc[i, 0]).strip()
|
|
51
|
+
val = raw.iloc[i, 1]
|
|
52
|
+
if key and key != 'nan':
|
|
53
|
+
metadata[key] = val if not _is_blank(val) else None
|
|
54
|
+
|
|
55
|
+
col_names = raw.iloc[n_header - 2].tolist()
|
|
56
|
+
units = raw.iloc[n_header - 1].tolist()
|
|
57
|
+
|
|
58
|
+
data = raw.iloc[n_header:].copy()
|
|
59
|
+
data.columns = col_names
|
|
60
|
+
data = data.reset_index(drop=True)
|
|
61
|
+
data.attrs['units'] = {col: str(u) for col, u in zip(col_names, units)}
|
|
62
|
+
|
|
63
|
+
for col in data.columns:
|
|
64
|
+
data[col] = pd.to_numeric(data[col], errors='coerce')
|
|
65
|
+
|
|
66
|
+
return metadata, data
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _is_blank(val):
|
|
70
|
+
if val is None:
|
|
71
|
+
return True
|
|
72
|
+
try:
|
|
73
|
+
return pd.isna(val)
|
|
74
|
+
except (TypeError, ValueError):
|
|
75
|
+
return str(val).strip() == ''
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# ---------------------------------------------------------------------------
|
|
79
|
+
# Metadata helpers
|
|
80
|
+
# ---------------------------------------------------------------------------
|
|
81
|
+
|
|
82
|
+
def get_recording_delay(metadata):
|
|
83
|
+
"""Parse 'Recording after' delay from EthoVision metadata.
|
|
84
|
+
|
|
85
|
+
Returns the number of seconds between trial start and when video
|
|
86
|
+
recording began. Used to compute frame_offset = round(delay * fps).
|
|
87
|
+
|
|
88
|
+
Parameters
|
|
89
|
+
----------
|
|
90
|
+
metadata : dict
|
|
91
|
+
Output of load_behavior_file.
|
|
92
|
+
|
|
93
|
+
Returns
|
|
94
|
+
-------
|
|
95
|
+
delay_seconds : float
|
|
96
|
+
"""
|
|
97
|
+
recording_after = str(metadata.get('Recording after', '+ 00:00:00.000'))
|
|
98
|
+
time_part = recording_after.strip().lstrip('+ ').strip()
|
|
99
|
+
h, m, s = time_part.split(':')
|
|
100
|
+
return int(h) * 3600 + int(m) * 60 + float(s)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def get_display_metadata(metadata):
|
|
104
|
+
"""Extract the subset of metadata fields useful for sidebar display.
|
|
105
|
+
|
|
106
|
+
Parameters
|
|
107
|
+
----------
|
|
108
|
+
metadata : dict
|
|
109
|
+
Output of load_behavior_file.
|
|
110
|
+
|
|
111
|
+
Returns
|
|
112
|
+
-------
|
|
113
|
+
dict with keys:
|
|
114
|
+
mouse_id, experiment, trial_name, start_time, recording_duration,
|
|
115
|
+
arena_name, arena_id, video_stem,
|
|
116
|
+
missed_samples_pct, subject_not_found_pct, interpolated_pct
|
|
117
|
+
"""
|
|
118
|
+
def _duration_str(val):
|
|
119
|
+
"""Strip leading '+ ' from EthoVision duration strings."""
|
|
120
|
+
if val is None:
|
|
121
|
+
return None
|
|
122
|
+
return str(val).strip().lstrip('+ ').strip()
|
|
123
|
+
|
|
124
|
+
def _pct_float(val):
|
|
125
|
+
"""Parse '0.0 %' → 0.0."""
|
|
126
|
+
if val is None:
|
|
127
|
+
return None
|
|
128
|
+
m = re.search(r'[\d.]+', str(val))
|
|
129
|
+
return float(m.group()) if m else None
|
|
130
|
+
|
|
131
|
+
video_path = metadata.get('Video file')
|
|
132
|
+
video_filename = Path(video_path).name if video_path else None
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
'mouse_id': metadata.get('mouse ID'),
|
|
136
|
+
'experiment': metadata.get('Experiment'),
|
|
137
|
+
'trial_name': metadata.get('Trial name'),
|
|
138
|
+
'start_time': metadata.get('Start time'),
|
|
139
|
+
'recording_duration': _duration_str(metadata.get('Recording duration')),
|
|
140
|
+
'arena_name': metadata.get('Arena name'),
|
|
141
|
+
'arena_id': metadata.get('Arena ID'),
|
|
142
|
+
'video_filename': video_filename,
|
|
143
|
+
'missed_samples_pct': _pct_float(metadata.get('Missed samples')),
|
|
144
|
+
'subject_not_found_pct': _pct_float(metadata.get('Subject not found')),
|
|
145
|
+
'interpolated_pct': _pct_float(metadata.get('Interpolated samples')),
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
# ---------------------------------------------------------------------------
|
|
150
|
+
# Position
|
|
151
|
+
# ---------------------------------------------------------------------------
|
|
152
|
+
|
|
153
|
+
def extract_position(data, point='center'):
|
|
154
|
+
"""Extract x/y position time series for a tracked body point.
|
|
155
|
+
|
|
156
|
+
Parameters
|
|
157
|
+
----------
|
|
158
|
+
data : pd.DataFrame
|
|
159
|
+
Output of load_behavior_file.
|
|
160
|
+
point : {'center', 'nose', 'tail'}
|
|
161
|
+
|
|
162
|
+
Returns
|
|
163
|
+
-------
|
|
164
|
+
t : np.ndarray — Recording time (s)
|
|
165
|
+
x : np.ndarray — X position (cm)
|
|
166
|
+
y : np.ndarray — Y position (cm)
|
|
167
|
+
"""
|
|
168
|
+
point_map = {
|
|
169
|
+
'center': ('X center', 'Y center'),
|
|
170
|
+
'nose': ('X nose', 'Y nose'),
|
|
171
|
+
'tail': ('X tail', 'Y tail'),
|
|
172
|
+
}
|
|
173
|
+
if point not in point_map:
|
|
174
|
+
raise ValueError(f"point must be one of {list(point_map)}, got '{point}'")
|
|
175
|
+
x_col, y_col = point_map[point]
|
|
176
|
+
t = data['Recording time'].to_numpy(dtype=float)
|
|
177
|
+
x = data[x_col].to_numpy(dtype=float)
|
|
178
|
+
y = data[y_col].to_numpy(dtype=float)
|
|
179
|
+
return t, x, y
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def estimate_position_pixels(x, y, video_width, video_height,
|
|
183
|
+
scale_x=1.0, scale_y=1.0,
|
|
184
|
+
offset_x=0.0, offset_y=0.0,
|
|
185
|
+
padding=0.1):
|
|
186
|
+
"""Map EthoVision cm coordinates to video pixel coordinates.
|
|
187
|
+
|
|
188
|
+
Uses data bounds + padding as a proxy for arena extent (uncalibrated).
|
|
189
|
+
Pass scale_x/y, offset_x/y from a saved arena calibration to refine.
|
|
190
|
+
|
|
191
|
+
Y is flipped: EthoVision Y increases upward, image Y increases downward.
|
|
192
|
+
|
|
193
|
+
Parameters
|
|
194
|
+
----------
|
|
195
|
+
x, y : np.ndarray — position in cm
|
|
196
|
+
video_width, video_height : int — frame dimensions in pixels
|
|
197
|
+
scale_x, scale_y : float — calibration scale factors
|
|
198
|
+
offset_x, offset_y : float — calibration pixel offsets
|
|
199
|
+
padding : float — fractional padding around data bounds
|
|
200
|
+
|
|
201
|
+
Returns
|
|
202
|
+
-------
|
|
203
|
+
px, py : np.ndarray — pixel coordinates
|
|
204
|
+
"""
|
|
205
|
+
x_min, x_max = np.nanmin(x), np.nanmax(x)
|
|
206
|
+
y_min, y_max = np.nanmin(y), np.nanmax(y)
|
|
207
|
+
x_pad = (x_max - x_min) * padding
|
|
208
|
+
y_pad = (y_max - y_min) * padding
|
|
209
|
+
x_min -= x_pad; x_max += x_pad
|
|
210
|
+
y_min -= y_pad; y_max += y_pad
|
|
211
|
+
|
|
212
|
+
px = ((x - x_min) / (x_max - x_min) * video_width) * scale_x + offset_x
|
|
213
|
+
py = ((1.0 - (y - y_min) / (y_max - y_min)) * video_height) * scale_y + offset_y
|
|
214
|
+
return px, py
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
# ---------------------------------------------------------------------------
|
|
218
|
+
# Motion
|
|
219
|
+
# ---------------------------------------------------------------------------
|
|
220
|
+
|
|
221
|
+
def compute_motion(data, point='center'):
|
|
222
|
+
"""Compute frame-to-frame displacement and speed from tracked position.
|
|
223
|
+
|
|
224
|
+
Re-derives from x/y coordinates rather than EthoVision's 'Distance moved'
|
|
225
|
+
column, so the calculation is transparent and reproducible.
|
|
226
|
+
|
|
227
|
+
Returns
|
|
228
|
+
-------
|
|
229
|
+
t : np.ndarray — Recording time (s)
|
|
230
|
+
displacement : np.ndarray — distance moved since previous frame (cm); first value NaN
|
|
231
|
+
speed : np.ndarray — instantaneous speed (cm/s); first value NaN
|
|
232
|
+
"""
|
|
233
|
+
t, x, y = extract_position(data, point=point)
|
|
234
|
+
dx = np.diff(x)
|
|
235
|
+
dy = np.diff(y)
|
|
236
|
+
dt = np.diff(t)
|
|
237
|
+
displacement = np.sqrt(dx**2 + dy**2)
|
|
238
|
+
speed = displacement / dt
|
|
239
|
+
displacement = np.concatenate([[np.nan], displacement])
|
|
240
|
+
speed = np.concatenate([[np.nan], speed])
|
|
241
|
+
return t, displacement, speed
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
# ---------------------------------------------------------------------------
|
|
245
|
+
# Neural sync
|
|
246
|
+
# ---------------------------------------------------------------------------
|
|
247
|
+
|
|
248
|
+
def get_recording_delay_from_data(data):
|
|
249
|
+
"""Return the offset between trial start and behavioral t=0.
|
|
250
|
+
|
|
251
|
+
Alias for get_recording_delay that works directly from the data DataFrame
|
|
252
|
+
when metadata is not available separately.
|
|
253
|
+
"""
|
|
254
|
+
# Recording time in the DataFrame starts at 0 when video recording began.
|
|
255
|
+
# The delay relative to trial start is already baked into the time axis.
|
|
256
|
+
return float(data['Recording time'].iloc[0])
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def sync_to_neural(data, neural_t_offset=0.0):
|
|
260
|
+
"""Align behavioral Recording time to a shared neural time axis.
|
|
261
|
+
|
|
262
|
+
Parameters
|
|
263
|
+
----------
|
|
264
|
+
data : pd.DataFrame
|
|
265
|
+
neural_t_offset : float
|
|
266
|
+
Seconds to add to behavioral Recording time to align with neural time.
|
|
267
|
+
|
|
268
|
+
Returns
|
|
269
|
+
-------
|
|
270
|
+
t_neural : np.ndarray
|
|
271
|
+
"""
|
|
272
|
+
t_behavior = data['Recording time'].to_numpy(dtype=float)
|
|
273
|
+
return t_behavior + neural_t_offset
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def get_behavior_at_neural_times(data, query_times, column, neural_t_offset=0.0):
|
|
277
|
+
"""Interpolate a behavioral variable onto arbitrary neural timepoints.
|
|
278
|
+
|
|
279
|
+
Parameters
|
|
280
|
+
----------
|
|
281
|
+
data : pd.DataFrame
|
|
282
|
+
query_times : array-like — neural timepoints (s)
|
|
283
|
+
column : str — column name (e.g. 'Velocity', 'X center')
|
|
284
|
+
neural_t_offset : float
|
|
285
|
+
|
|
286
|
+
Returns
|
|
287
|
+
-------
|
|
288
|
+
values : np.ndarray — interpolated values; NaN outside behavioral range
|
|
289
|
+
"""
|
|
290
|
+
t_neural = sync_to_neural(data, neural_t_offset=neural_t_offset)
|
|
291
|
+
values = data[column].to_numpy(dtype=float)
|
|
292
|
+
query_times = np.asarray(query_times, dtype=float)
|
|
293
|
+
return np.interp(query_times, t_neural, values, left=np.nan, right=np.nan)
|