fibphot 0.1.0__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.
- fibphot-0.1.0/LICENSE.md +21 -0
- fibphot-0.1.0/PKG-INFO +63 -0
- fibphot-0.1.0/README.md +16 -0
- fibphot-0.1.0/pyproject.toml +97 -0
- fibphot-0.1.0/setup.cfg +4 -0
- fibphot-0.1.0/src/fibphot/__init__.py +6 -0
- fibphot-0.1.0/src/fibphot/analysis/__init__.py +0 -0
- fibphot-0.1.0/src/fibphot/analysis/aggregate.py +257 -0
- fibphot-0.1.0/src/fibphot/analysis/auc.py +354 -0
- fibphot-0.1.0/src/fibphot/analysis/irls.py +350 -0
- fibphot-0.1.0/src/fibphot/analysis/peaks.py +1163 -0
- fibphot-0.1.0/src/fibphot/analysis/photobleaching.py +290 -0
- fibphot-0.1.0/src/fibphot/analysis/plotting.py +105 -0
- fibphot-0.1.0/src/fibphot/analysis/report.py +56 -0
- fibphot-0.1.0/src/fibphot/collection.py +207 -0
- fibphot-0.1.0/src/fibphot/fit/__init__.py +0 -0
- fibphot-0.1.0/src/fibphot/fit/regression.py +269 -0
- fibphot-0.1.0/src/fibphot/io/__init__.py +6 -0
- fibphot-0.1.0/src/fibphot/io/doric.py +435 -0
- fibphot-0.1.0/src/fibphot/io/excel.py +76 -0
- fibphot-0.1.0/src/fibphot/io/h5.py +321 -0
- fibphot-0.1.0/src/fibphot/misc.py +11 -0
- fibphot-0.1.0/src/fibphot/peaks.py +628 -0
- fibphot-0.1.0/src/fibphot/pipeline.py +14 -0
- fibphot-0.1.0/src/fibphot/plotting.py +594 -0
- fibphot-0.1.0/src/fibphot/stages/__init__.py +22 -0
- fibphot-0.1.0/src/fibphot/stages/base.py +101 -0
- fibphot-0.1.0/src/fibphot/stages/baseline.py +354 -0
- fibphot-0.1.0/src/fibphot/stages/control_dff.py +214 -0
- fibphot-0.1.0/src/fibphot/stages/filters.py +273 -0
- fibphot-0.1.0/src/fibphot/stages/normalisation.py +260 -0
- fibphot-0.1.0/src/fibphot/stages/regression.py +139 -0
- fibphot-0.1.0/src/fibphot/stages/smooth.py +442 -0
- fibphot-0.1.0/src/fibphot/stages/trim.py +141 -0
- fibphot-0.1.0/src/fibphot/state.py +309 -0
- fibphot-0.1.0/src/fibphot/tags.py +130 -0
- fibphot-0.1.0/src/fibphot/types.py +6 -0
- fibphot-0.1.0/src/fibphot.egg-info/PKG-INFO +63 -0
- fibphot-0.1.0/src/fibphot.egg-info/SOURCES.txt +40 -0
- fibphot-0.1.0/src/fibphot.egg-info/dependency_links.txt +1 -0
- fibphot-0.1.0/src/fibphot.egg-info/requires.txt +34 -0
- fibphot-0.1.0/src/fibphot.egg-info/top_level.txt +1 -0
fibphot-0.1.0/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Thomas Nicholas
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
fibphot-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fibphot
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Analysis pipeline for fibre photometry data
|
|
5
|
+
Author-email: Thomas Nicholas <tcnicholas@me.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/tcnicholas/fibphot
|
|
7
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
8
|
+
Classifier: Programming Language :: Python
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE.md
|
|
15
|
+
Requires-Dist: h5py>=3.15.1
|
|
16
|
+
Requires-Dist: matplotlib>=3.10.8
|
|
17
|
+
Requires-Dist: numpy>=2.4.1
|
|
18
|
+
Requires-Dist: openpyxl>=3.1.5
|
|
19
|
+
Requires-Dist: pandas>=2.3.3
|
|
20
|
+
Requires-Dist: pybaselines>=1.2.1
|
|
21
|
+
Requires-Dist: scipy>=1.17.0
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: ruff>=0.14.13; extra == "dev"
|
|
24
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
25
|
+
Requires-Dist: matplotlib>=3.10.8; extra == "dev"
|
|
26
|
+
Provides-Extra: publish
|
|
27
|
+
Requires-Dist: build; extra == "publish"
|
|
28
|
+
Requires-Dist: twine; extra == "publish"
|
|
29
|
+
Provides-Extra: docs
|
|
30
|
+
Requires-Dist: sphinx; extra == "docs"
|
|
31
|
+
Requires-Dist: pip; extra == "docs"
|
|
32
|
+
Requires-Dist: furo; extra == "docs"
|
|
33
|
+
Requires-Dist: nbsphinx; extra == "docs"
|
|
34
|
+
Requires-Dist: ipython; extra == "docs"
|
|
35
|
+
Requires-Dist: pyzmq; extra == "docs"
|
|
36
|
+
Requires-Dist: sphinx-autoapi; extra == "docs"
|
|
37
|
+
Requires-Dist: sphinx-autobuild; extra == "docs"
|
|
38
|
+
Requires-Dist: pydata_sphinx_theme; extra == "docs"
|
|
39
|
+
Requires-Dist: sphinxext-opengraph; extra == "docs"
|
|
40
|
+
Requires-Dist: sphinx-copybutton; extra == "docs"
|
|
41
|
+
Requires-Dist: sphinx-design; extra == "docs"
|
|
42
|
+
Requires-Dist: jupyter_sphinx; extra == "docs"
|
|
43
|
+
Requires-Dist: sphinx-togglebutton; extra == "docs"
|
|
44
|
+
Requires-Dist: sphinx_favicon; extra == "docs"
|
|
45
|
+
Requires-Dist: sphinx_sitemap; extra == "docs"
|
|
46
|
+
Dynamic: license-file
|
|
47
|
+
|
|
48
|
+
# FibPhot
|
|
49
|
+
|
|
50
|
+
<div align="center">
|
|
51
|
+
|
|
52
|
+

|
|
53
|
+
[](LICENSE.md)
|
|
54
|
+
</div>
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
## Install
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
## Relevant sources and related packages/code
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
### References
|
fibphot-0.1.0/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# FibPhot
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+
[](LICENSE.md)
|
|
7
|
+
</div>
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## Relevant sources and related packages/code
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### References
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "fibphot"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Analysis pipeline for fibre photometry data"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
authors = [{ name = "Thomas Nicholas", email = "tcnicholas@me.com" }]
|
|
11
|
+
license = { file = "LICENSE" }
|
|
12
|
+
classifiers = [
|
|
13
|
+
"License :: OSI Approved :: MIT License",
|
|
14
|
+
"Programming Language :: Python",
|
|
15
|
+
"Programming Language :: Python :: 3.10",
|
|
16
|
+
"Programming Language :: Python :: 3.11",
|
|
17
|
+
"Programming Language :: Python :: 3.12",
|
|
18
|
+
]
|
|
19
|
+
requires-python = ">=3.10"
|
|
20
|
+
dependencies = [
|
|
21
|
+
"h5py>=3.15.1",
|
|
22
|
+
"matplotlib>=3.10.8",
|
|
23
|
+
"numpy>=2.4.1",
|
|
24
|
+
"openpyxl>=3.1.5",
|
|
25
|
+
"pandas>=2.3.3",
|
|
26
|
+
"pybaselines>=1.2.1",
|
|
27
|
+
"scipy>=1.17.0",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[tool.setuptools]
|
|
31
|
+
include-package-data = true
|
|
32
|
+
|
|
33
|
+
[tool.setuptools.packages.find]
|
|
34
|
+
where = ["src"]
|
|
35
|
+
|
|
36
|
+
[tool.setuptools.package-data]
|
|
37
|
+
topo_metrics = ["RingStatistics/**/*"]
|
|
38
|
+
|
|
39
|
+
[project.urls]
|
|
40
|
+
Homepage = "https://github.com/tcnicholas/fibphot"
|
|
41
|
+
|
|
42
|
+
[project.optional-dependencies]
|
|
43
|
+
dev = [
|
|
44
|
+
"ruff>=0.14.13",
|
|
45
|
+
"pytest>=8.0",
|
|
46
|
+
"matplotlib>=3.10.8",
|
|
47
|
+
]
|
|
48
|
+
publish = ["build", "twine"]
|
|
49
|
+
docs = [
|
|
50
|
+
"sphinx",
|
|
51
|
+
"pip",
|
|
52
|
+
"furo",
|
|
53
|
+
"nbsphinx",
|
|
54
|
+
"ipython",
|
|
55
|
+
"pyzmq",
|
|
56
|
+
"sphinx-autoapi",
|
|
57
|
+
"sphinx-autobuild",
|
|
58
|
+
"pydata_sphinx_theme",
|
|
59
|
+
"sphinxext-opengraph",
|
|
60
|
+
"sphinx-copybutton",
|
|
61
|
+
"sphinx-design",
|
|
62
|
+
"jupyter_sphinx",
|
|
63
|
+
"sphinx-togglebutton",
|
|
64
|
+
"sphinx_favicon",
|
|
65
|
+
"sphinx_sitemap",
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
[tool.bumpver]
|
|
69
|
+
current_version = "0.1.0"
|
|
70
|
+
version_pattern = "MAJOR.MINOR.PATCH"
|
|
71
|
+
commit_message = "bump {old_version} -> {new_version}"
|
|
72
|
+
commit = true
|
|
73
|
+
tag = true
|
|
74
|
+
push = false
|
|
75
|
+
|
|
76
|
+
[tool.bumpver.file_patterns]
|
|
77
|
+
"pyproject.toml" = ['current_version = "{version}"', 'version = "{version}"']
|
|
78
|
+
"src/fibphot/__init__.py" = ["{version}"]
|
|
79
|
+
|
|
80
|
+
[tool.ruff]
|
|
81
|
+
line-length = 80
|
|
82
|
+
indent-width = 4
|
|
83
|
+
target-version = "py38"
|
|
84
|
+
extend-include = ["*.ipynb", "*.pyi", "*.toml"]
|
|
85
|
+
|
|
86
|
+
[tool.ruff.lint]
|
|
87
|
+
select = ["E", "F", "UP", "B", "SIM", "I"]
|
|
88
|
+
ignore = ["SIM300", "E402", "E703", "F722", "UP037", "F821", "B018", "E741"]
|
|
89
|
+
fixable = ["ALL"]
|
|
90
|
+
unfixable = []
|
|
91
|
+
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
|
|
92
|
+
|
|
93
|
+
[tool.ruff.format]
|
|
94
|
+
quote-style = "double"
|
|
95
|
+
indent-style = "space"
|
|
96
|
+
skip-magic-trailing-comma = false
|
|
97
|
+
line-ending = "auto"
|
fibphot-0.1.0/setup.cfg
ADDED
|
File without changes
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Sequence
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from typing import Literal
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
from ..state import PhotometryState
|
|
10
|
+
from ..types import FloatArray
|
|
11
|
+
|
|
12
|
+
AlignMode = Literal["intersection", "union"]
|
|
13
|
+
TimeRef = Literal["absolute", "start"]
|
|
14
|
+
InterpKind = Literal["linear", "nearest"]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass(frozen=True, slots=True)
|
|
18
|
+
class AlignedSignals:
|
|
19
|
+
"""
|
|
20
|
+
Container for a time-aligned stack of signals.
|
|
21
|
+
|
|
22
|
+
aligned has shape (n_states, n_channels, n_time).
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
time_seconds: FloatArray
|
|
26
|
+
aligned: FloatArray
|
|
27
|
+
channel_names: tuple[str, ...]
|
|
28
|
+
subjects: tuple[str | None, ...]
|
|
29
|
+
align_mode: AlignMode
|
|
30
|
+
time_ref: TimeRef
|
|
31
|
+
dt: float
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _as_relative_time(t: FloatArray) -> FloatArray:
|
|
35
|
+
t = np.asarray(t, dtype=float)
|
|
36
|
+
return t - float(t[0])
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _infer_dt(states: Sequence[PhotometryState]) -> float:
|
|
40
|
+
dts: list[float] = []
|
|
41
|
+
for s in states:
|
|
42
|
+
dt = np.diff(np.asarray(s.time_seconds, dtype=float))
|
|
43
|
+
if dt.size == 0:
|
|
44
|
+
continue
|
|
45
|
+
dts.append(float(np.nanmedian(dt)))
|
|
46
|
+
if not dts:
|
|
47
|
+
raise ValueError(
|
|
48
|
+
"Cannot infer dt from empty or degenerate time arrays."
|
|
49
|
+
)
|
|
50
|
+
return float(np.nanmedian(np.asarray(dts, dtype=float)))
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _time_window(
|
|
54
|
+
times: list[FloatArray],
|
|
55
|
+
mode: AlignMode,
|
|
56
|
+
) -> tuple[float, float]:
|
|
57
|
+
starts = [float(t[0]) for t in times]
|
|
58
|
+
ends = [float(t[-1]) for t in times]
|
|
59
|
+
|
|
60
|
+
if mode == "intersection":
|
|
61
|
+
t0 = max(starts)
|
|
62
|
+
t1 = min(ends)
|
|
63
|
+
elif mode == "union":
|
|
64
|
+
t0 = min(starts)
|
|
65
|
+
t1 = max(ends)
|
|
66
|
+
else:
|
|
67
|
+
raise ValueError(f"Unknown align mode: {mode!r}")
|
|
68
|
+
|
|
69
|
+
if not np.isfinite(t0) or not np.isfinite(t1) or t1 <= t0:
|
|
70
|
+
raise ValueError(
|
|
71
|
+
f"Invalid common window from mode={mode!r}: ({t0}, {t1})."
|
|
72
|
+
)
|
|
73
|
+
return t0, t1
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _make_grid(t0: float, t1: float, dt: float) -> FloatArray:
|
|
77
|
+
n = int(np.floor((t1 - t0) / dt)) + 1
|
|
78
|
+
if n < 2:
|
|
79
|
+
raise ValueError("Common time grid would have <2 points.")
|
|
80
|
+
return t0 + dt * np.arange(n, dtype=float)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _interp_1d(
|
|
84
|
+
x: FloatArray,
|
|
85
|
+
y: FloatArray,
|
|
86
|
+
x_new: FloatArray,
|
|
87
|
+
*,
|
|
88
|
+
kind: InterpKind,
|
|
89
|
+
fill: float,
|
|
90
|
+
) -> FloatArray:
|
|
91
|
+
x = np.asarray(x, dtype=float)
|
|
92
|
+
y = np.asarray(y, dtype=float)
|
|
93
|
+
x_new = np.asarray(x_new, dtype=float)
|
|
94
|
+
|
|
95
|
+
mask = np.isfinite(x) & np.isfinite(y)
|
|
96
|
+
x0 = x[mask]
|
|
97
|
+
y0 = y[mask]
|
|
98
|
+
if x0.size < 2:
|
|
99
|
+
return np.full_like(x_new, fill, dtype=float)
|
|
100
|
+
|
|
101
|
+
order = np.argsort(x0)
|
|
102
|
+
x0 = x0[order]
|
|
103
|
+
y0 = y0[order]
|
|
104
|
+
|
|
105
|
+
if kind == "linear":
|
|
106
|
+
return np.interp(x_new, x0, y0, left=fill, right=fill).astype(float)
|
|
107
|
+
|
|
108
|
+
if kind == "nearest":
|
|
109
|
+
idx = np.searchsorted(x0, x_new, side="left")
|
|
110
|
+
idx = np.clip(idx, 0, x0.size - 1)
|
|
111
|
+
left = np.clip(idx - 1, 0, x0.size - 1)
|
|
112
|
+
|
|
113
|
+
choose_left = np.abs(x_new - x0[left]) <= np.abs(x_new - x0[idx])
|
|
114
|
+
out = np.where(choose_left, y0[left], y0[idx]).astype(float)
|
|
115
|
+
|
|
116
|
+
out[(x_new < x0[0]) | (x_new > x0[-1])] = fill
|
|
117
|
+
return out
|
|
118
|
+
|
|
119
|
+
raise ValueError(f"Unknown interpolation kind: {kind!r}")
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def align_collection_signals(
|
|
123
|
+
states: Sequence[PhotometryState],
|
|
124
|
+
*,
|
|
125
|
+
channels: Sequence[str] | None = None,
|
|
126
|
+
align: AlignMode = "intersection",
|
|
127
|
+
time_ref: TimeRef = "start",
|
|
128
|
+
dt: float | None = None,
|
|
129
|
+
target_fs: float | None = None,
|
|
130
|
+
interpolation: InterpKind = "linear",
|
|
131
|
+
fill: float = float("nan"),
|
|
132
|
+
) -> AlignedSignals:
|
|
133
|
+
"""
|
|
134
|
+
Align a set of states to a common time axis via interpolation.
|
|
135
|
+
|
|
136
|
+
align="intersection":
|
|
137
|
+
Uses only the time interval present in all states (truncate behaviour).
|
|
138
|
+
align="union":
|
|
139
|
+
Uses the full time span across states and fills missing regions (pad behaviour).
|
|
140
|
+
|
|
141
|
+
time_ref="start":
|
|
142
|
+
Treats each state's time as relative to its own start time.
|
|
143
|
+
time_ref="absolute":
|
|
144
|
+
Uses each state's original time_seconds values.
|
|
145
|
+
"""
|
|
146
|
+
if not states:
|
|
147
|
+
raise ValueError("No states provided.")
|
|
148
|
+
|
|
149
|
+
if channels is None:
|
|
150
|
+
channel_names = states[0].channel_names
|
|
151
|
+
idxs = list(range(states[0].n_signals))
|
|
152
|
+
else:
|
|
153
|
+
channel_names = tuple(c.lower() for c in channels)
|
|
154
|
+
idxs = [states[0].idx(c) for c in channel_names]
|
|
155
|
+
|
|
156
|
+
for s in states[1:]:
|
|
157
|
+
for c in channel_names:
|
|
158
|
+
_ = s.idx(c)
|
|
159
|
+
|
|
160
|
+
if target_fs is not None:
|
|
161
|
+
if target_fs <= 0:
|
|
162
|
+
raise ValueError("target_fs must be > 0.")
|
|
163
|
+
dt_use = 1.0 / float(target_fs)
|
|
164
|
+
else:
|
|
165
|
+
dt_use = float(dt) if dt is not None else _infer_dt(states)
|
|
166
|
+
|
|
167
|
+
times: list[FloatArray] = []
|
|
168
|
+
for s in states:
|
|
169
|
+
t = np.asarray(s.time_seconds, dtype=float)
|
|
170
|
+
if time_ref == "start":
|
|
171
|
+
t = _as_relative_time(t)
|
|
172
|
+
times.append(t)
|
|
173
|
+
|
|
174
|
+
t0, t1 = _time_window(times, align)
|
|
175
|
+
grid = _make_grid(t0, t1, dt_use)
|
|
176
|
+
|
|
177
|
+
aligned = np.full(
|
|
178
|
+
(len(states), len(idxs), grid.size),
|
|
179
|
+
fill,
|
|
180
|
+
dtype=float,
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
for si, s in enumerate(states):
|
|
184
|
+
t = times[si]
|
|
185
|
+
for cj, idx in enumerate(idxs):
|
|
186
|
+
y = np.asarray(s.signals[idx], dtype=float)
|
|
187
|
+
aligned[si, cj] = _interp_1d(
|
|
188
|
+
t,
|
|
189
|
+
y,
|
|
190
|
+
grid,
|
|
191
|
+
kind=interpolation,
|
|
192
|
+
fill=fill,
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
subjects: tuple[str | None, ...] = tuple(
|
|
196
|
+
getattr(s, "subject", None) for s in states
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
return AlignedSignals(
|
|
200
|
+
time_seconds=grid,
|
|
201
|
+
aligned=aligned,
|
|
202
|
+
channel_names=tuple(channel_names),
|
|
203
|
+
subjects=subjects,
|
|
204
|
+
align_mode=align,
|
|
205
|
+
time_ref=time_ref,
|
|
206
|
+
dt=dt_use,
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def mean_aligned(
|
|
211
|
+
aligned: AlignedSignals,
|
|
212
|
+
) -> tuple[FloatArray, FloatArray, FloatArray, FloatArray]:
|
|
213
|
+
"""
|
|
214
|
+
Compute mean, std, sem, and n (per channel/time) from an aligned stack.
|
|
215
|
+
"""
|
|
216
|
+
x = np.asarray(aligned.aligned, dtype=float)
|
|
217
|
+
|
|
218
|
+
n = np.sum(np.isfinite(x), axis=0).astype(float)
|
|
219
|
+
mean = np.nanmean(x, axis=0)
|
|
220
|
+
std = np.nanstd(x, axis=0)
|
|
221
|
+
|
|
222
|
+
sem = std / np.sqrt(np.where(n <= 0, np.nan, n))
|
|
223
|
+
return mean, std, sem, n
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def mean_state_from_aligned(
|
|
227
|
+
aligned: AlignedSignals,
|
|
228
|
+
*,
|
|
229
|
+
name: str = "group_mean",
|
|
230
|
+
) -> PhotometryState:
|
|
231
|
+
"""
|
|
232
|
+
Return a PhotometryState whose signals are the group mean.
|
|
233
|
+
|
|
234
|
+
Additional statistics are stored in derived:
|
|
235
|
+
- derived["group_std"]
|
|
236
|
+
- derived["group_sem"]
|
|
237
|
+
- derived["group_n"]
|
|
238
|
+
"""
|
|
239
|
+
mean, std, sem, n = mean_aligned(aligned)
|
|
240
|
+
|
|
241
|
+
return PhotometryState(
|
|
242
|
+
time_seconds=aligned.time_seconds,
|
|
243
|
+
signals=mean,
|
|
244
|
+
channel_names=aligned.channel_names,
|
|
245
|
+
derived={
|
|
246
|
+
"group_std": std,
|
|
247
|
+
"group_sem": sem,
|
|
248
|
+
"group_n": n,
|
|
249
|
+
},
|
|
250
|
+
metadata={
|
|
251
|
+
"kind": name,
|
|
252
|
+
"align_mode": aligned.align_mode,
|
|
253
|
+
"time_ref": aligned.time_ref,
|
|
254
|
+
"dt": aligned.dt,
|
|
255
|
+
"subjects": list(aligned.subjects),
|
|
256
|
+
},
|
|
257
|
+
)
|