PyATCLI 2026.10.10__py3-none-any.whl

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.
Files changed (71) hide show
  1. PyATCLI/__init__.py +196 -0
  2. PyATCLI/__main__.py +7 -0
  3. PyATCLI/_version.py +47 -0
  4. PyATCLI/analyses.py +640 -0
  5. PyATCLI/analysis/__init__.py +81 -0
  6. PyATCLI/analysis/auc.py +79 -0
  7. PyATCLI/analysis/curve_fit.py +88 -0
  8. PyATCLI/analysis/event_peth.py +263 -0
  9. PyATCLI/analysis/fft.py +99 -0
  10. PyATCLI/analysis/group/__init__.py +318 -0
  11. PyATCLI/analysis/group/rebuild.py +59 -0
  12. PyATCLI/analysis/group/report.py +281 -0
  13. PyATCLI/analysis/group/run.py +164 -0
  14. PyATCLI/analysis/group/scan.py +92 -0
  15. PyATCLI/analysis/group/stats.py +451 -0
  16. PyATCLI/analysis/group/trials.py +188 -0
  17. PyATCLI/analysis/intervals.py +59 -0
  18. PyATCLI/analysis/peak_finder.py +155 -0
  19. PyATCLI/analysis/shared.py +100 -0
  20. PyATCLI/analysis/zscore_peth.py +107 -0
  21. PyATCLI/cli/__init__.py +105 -0
  22. PyATCLI/cli/commands.py +395 -0
  23. PyATCLI/cli/docs.py +246 -0
  24. PyATCLI/cli/parser.py +286 -0
  25. PyATCLI/cli/render.py +130 -0
  26. PyATCLI/engine/__init__.py +0 -0
  27. PyATCLI/engine/contract.py +124 -0
  28. PyATCLI/engine/server.py +169 -0
  29. PyATCLI/engine/session.py +40 -0
  30. PyATCLI/engine/store.py +222 -0
  31. PyATCLI/engine/wire.py +103 -0
  32. PyATCLI/fieldstudy/__init__.py +1 -0
  33. PyATCLI/fieldstudy/study.py +440 -0
  34. PyATCLI/fieldstudy/validation.py +443 -0
  35. PyATCLI/markers.py +71 -0
  36. PyATCLI/models.py +102 -0
  37. PyATCLI/numeric.py +35 -0
  38. PyATCLI/ops/__init__.py +274 -0
  39. PyATCLI/ops/analysis.py +136 -0
  40. PyATCLI/ops/common.py +93 -0
  41. PyATCLI/ops/compute.py +94 -0
  42. PyATCLI/ops/fieldstudy.py +52 -0
  43. PyATCLI/ops/files.py +42 -0
  44. PyATCLI/ops/group.py +144 -0
  45. PyATCLI/ops/recording.py +90 -0
  46. PyATCLI/ops/records.py +63 -0
  47. PyATCLI/ops/results.py +27 -0
  48. PyATCLI/ops/saved.py +98 -0
  49. PyATCLI/ops/system.py +31 -0
  50. PyATCLI/outputs.py +165 -0
  51. PyATCLI/progress.py +227 -0
  52. PyATCLI/provenance.py +336 -0
  53. PyATCLI/recording.py +602 -0
  54. PyATCLI/reproduce.py +278 -0
  55. PyATCLI/results.py +106 -0
  56. PyATCLI/saved_state.py +275 -0
  57. PyATCLI/sources/__init__.py +1 -0
  58. PyATCLI/sources/dataset.py +110 -0
  59. PyATCLI/sources/load.py +169 -0
  60. PyATCLI/sources/processing_TDT.py +810 -0
  61. PyATCLI/sources/processing_oxysoft.py +259 -0
  62. PyATCLI/sources/pt2.py +46 -0
  63. PyATCLI/sources/tables.py +306 -0
  64. PyATCLI/splice.py +196 -0
  65. PyATCLI/viewplan.py +181 -0
  66. pyatcli-2026.10.10.dist-info/METADATA +296 -0
  67. pyatcli-2026.10.10.dist-info/RECORD +71 -0
  68. pyatcli-2026.10.10.dist-info/WHEEL +5 -0
  69. pyatcli-2026.10.10.dist-info/entry_points.txt +2 -0
  70. pyatcli-2026.10.10.dist-info/licenses/LICENSE +661 -0
  71. pyatcli-2026.10.10.dist-info/top_level.txt +1 -0
@@ -0,0 +1,110 @@
1
+ """
2
+ sources/dataset.py
3
+ ------------------
4
+ The universal Dataset container returned by every loader, plus format
5
+ detection. No parsing logic lives here — that's in load.py and the readers beside it. No GUI
6
+ concerns either — folder/file selection is a caller responsibility.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import os
12
+ from dataclasses import dataclass, field
13
+ from enum import Enum, auto
14
+ from typing import Optional
15
+
16
+ import numpy as np
17
+
18
+ from ..numeric import as_float64
19
+
20
+
21
+ # ---------------------------------------------------------------------------
22
+ # Universal output struct
23
+ # ---------------------------------------------------------------------------
24
+
25
+ @dataclass
26
+ class Dataset:
27
+ """Universal container returned by every loader."""
28
+
29
+ source_format: str # 'TDT' | 'Oxysoft'
30
+ folder_path: str
31
+ folder_name: str
32
+
33
+ # timing
34
+ sample_rate: float = 0.0 # Hz
35
+ num_samples: int = 0
36
+ duration_s: float = 0.0 # seconds
37
+
38
+ # signals – shape (num_channels, num_samples)
39
+ signals: Optional[np.ndarray] = None
40
+
41
+ # channel metadata
42
+ channel_names: list[str] = field(default_factory=list)
43
+ num_channels: int = 0
44
+
45
+ # events – list of dicts with at least {'label': str, 'sample': int}
46
+ events: list[dict] = field(default_factory=list)
47
+
48
+ # raw header / metadata blob (format-specific)
49
+ metadata: dict = field(default_factory=dict)
50
+
51
+ def __post_init__(self):
52
+ # Whatever a loader read, the signals a Dataset holds are float64 (see numeric.py).
53
+ if self.signals is not None:
54
+ self.signals = as_float64(self.signals)
55
+
56
+
57
+ # ---------------------------------------------------------------------------
58
+ # Format detection
59
+ # ---------------------------------------------------------------------------
60
+
61
+ class DataFormat(Enum):
62
+ TDT = auto()
63
+ OXYSOFT = auto()
64
+ UNKNOWN = auto()
65
+
66
+
67
+ _TDT_EXTENSIONS = {'.tbk', '.tdx', '.tev', '.tsq', '.sev'}
68
+ _OXYSOFT_HEADER_MARKER = 'Datafile sample rate'
69
+
70
+
71
+ def detect_format(folder_path: str) -> DataFormat:
72
+ """
73
+ Inspect the contents of *folder_path* and return the matching DataFormat.
74
+ Priority: TDT first (proprietary extensions), then Oxysoft (.txt marker).
75
+ """
76
+ entries = os.listdir(folder_path)
77
+ extensions = {os.path.splitext(e)[1].lower() for e in entries}
78
+
79
+ if extensions & _TDT_EXTENSIONS:
80
+ return DataFormat.TDT
81
+
82
+ txt_files = [e for e in entries if e.lower().endswith('.txt')]
83
+ for fname in txt_files:
84
+ fpath = os.path.join(folder_path, fname)
85
+ try:
86
+ with open(fpath, 'r', encoding='utf-8', errors='replace') as fh:
87
+ for _ in range(30):
88
+ if _OXYSOFT_HEADER_MARKER in fh.readline():
89
+ return DataFormat.OXYSOFT
90
+ except OSError:
91
+ continue
92
+
93
+ return DataFormat.UNKNOWN
94
+
95
+
96
+ def detect_format_file(file_path: str) -> DataFormat:
97
+ """
98
+ Detect the format of a single file (as opposed to a folder).
99
+ Currently supports Oxysoft .txt exports.
100
+ """
101
+ if not file_path.lower().endswith('.txt'):
102
+ return DataFormat.UNKNOWN
103
+ try:
104
+ with open(file_path, 'r', encoding='utf-8', errors='replace') as fh:
105
+ for _ in range(30):
106
+ if _OXYSOFT_HEADER_MARKER in fh.readline():
107
+ return DataFormat.OXYSOFT
108
+ except OSError:
109
+ pass
110
+ return DataFormat.UNKNOWN
@@ -0,0 +1,169 @@
1
+ """
2
+ sources/load.py
3
+ ---------------
4
+ Reads a recording from disk into the universal Dataset: detects the format inside a selected folder or file, hands it to the reader for
5
+ that format, and packages what the reader returned.
6
+
7
+ - TDT (Tucker-Davis Technologies) – processing_TDT.py reads and corrects the tank; load_tdt() wraps the result
8
+ - Oxysoft / Artinis (Oxymon, OctaMon, PortaMon …) – processing_oxysoft.py reads the export; load_oxysoft() wraps the result
9
+
10
+ Format detection and the Dataset struct live in dataset.py. The .pt2 EFNMR/MRI image format is a standalone parser in pt2.py (it returns a
11
+ raw image array, not a Dataset).
12
+
13
+ Usage
14
+ -----
15
+ from PyATCLI import detect_format, load_dataset, DataFormat
16
+
17
+ fmt = detect_format(folder_path)
18
+ dataset = load_dataset(folder_path, fmt)
19
+ """
20
+
21
+ from __future__ import annotations
22
+
23
+ import os
24
+ from typing import Optional
25
+
26
+ import numpy as np
27
+
28
+ from . import processing_TDT, processing_oxysoft
29
+ from .dataset import Dataset, DataFormat, detect_format, detect_format_file
30
+
31
+
32
+ def load_dataset_file(file_path: str, progress=None) -> Dataset:
33
+ """Load a single file and return a Dataset.
34
+
35
+ progress : None, True or callable(fraction, message)
36
+ Progress reporting; see PyATCLI.progress.
37
+ """
38
+ fmt = detect_format_file(file_path)
39
+ if fmt == DataFormat.OXYSOFT:
40
+ return load_oxysoft_file(file_path, progress=progress)
41
+ raise ValueError(f"Unrecognised file format: {file_path}")
42
+
43
+
44
+ def load_dataset(folder_path: str, fmt: Optional[DataFormat] = None,
45
+ regression_method: str = "ols", progress=None) -> Dataset:
46
+ """
47
+ Load a recording folder and return a Dataset.
48
+ If *fmt* is None, detect_format() is called automatically.
49
+
50
+ regression_method : {"ransac", "huber", "ols"}
51
+ TDT-only — forwarded to load_tdt()/process_tdt_folder() for the
52
+ isosbestic-vs-signal motion-correction regression. Ignored for
53
+ Oxysoft, which has no such correction step.
54
+ progress : None, True or callable(fraction, message)
55
+ Progress reporting; see PyATCLI.progress.
56
+ """
57
+ folder_name = os.path.basename(folder_path.rstrip('/\\'))
58
+
59
+ if fmt is None:
60
+ fmt = detect_format(folder_path)
61
+
62
+ if fmt is DataFormat.TDT:
63
+ return load_tdt(folder_path, folder_name, regression_method=regression_method, progress=progress)
64
+ elif fmt is DataFormat.OXYSOFT:
65
+ return load_oxysoft(folder_path, folder_name, progress=progress)
66
+ else:
67
+ raise ValueError(
68
+ f"Could not identify a supported data format in: {folder_path}\n"
69
+ "Expected TDT proprietary files (.Tbk/.tev/…) or an Oxysoft .txt export."
70
+ )
71
+
72
+
73
+ # ---------------------------------------------------------------------------
74
+ # TDT
75
+ # ---------------------------------------------------------------------------
76
+
77
+ def load_tdt(folder_path: str, folder_name: str, regression_method: str = "ols", progress=None) -> Dataset:
78
+ """Load a TDT tank using the existing validate + process pipeline.
79
+
80
+ `progress` (None, True or callable(fraction, message); see
81
+ PyATCLI.progress) is handed to process_tdt_folder.
82
+ """
83
+ valid, msg = processing_TDT.validate_tdt_folder(folder_path)
84
+ if not valid:
85
+ raise ValueError(f"TDT validation failed: {msg}")
86
+
87
+ result = processing_TDT.process_tdt_folder(folder_path, regression_method=regression_method,
88
+ progress=progress)
89
+ signals = result["corr"]
90
+ signals_2d = signals[np.newaxis, :]
91
+ fs = float(result["fs"])
92
+ num_samp = signals.shape[0]
93
+
94
+ events = []
95
+ for marker in result["markers"]:
96
+ events.append({
97
+ "label": marker["label"],
98
+ "sample": int(round(marker["time"] * fs)),
99
+ })
100
+
101
+ return Dataset(
102
+ source_format = "TDT",
103
+ folder_path = folder_path,
104
+ folder_name = folder_name,
105
+ sample_rate = fs,
106
+ num_samples = num_samp,
107
+ duration_s = num_samp / fs if fs > 0 else 0.0,
108
+ signals = signals_2d,
109
+ channel_names = [result["store"]],
110
+ num_channels = 1,
111
+ events = events,
112
+ metadata = {
113
+ "f0": result["f0"],
114
+ "raw": result["raw"],
115
+ "x": result["x"],
116
+ },
117
+ )
118
+
119
+
120
+ # ---------------------------------------------------------------------------
121
+ # Oxysoft
122
+ # ---------------------------------------------------------------------------
123
+
124
+ def load_oxysoft(folder_path: str, folder_name: str, progress=None) -> Dataset:
125
+ """
126
+ Parse an Oxysoft TXT export folder into a Dataset.
127
+ Multiple .txt files are concatenated in alphabetical order.
128
+
129
+ `progress` (None, True or callable(fraction, message); see
130
+ PyATCLI.progress) is weighted by each file's size in bytes.
131
+ """
132
+ result = processing_oxysoft.process_oxysoft_folder(folder_path, progress=progress)
133
+ return _oxysoft_dataset(result, folder_path=folder_path, folder_name=folder_name)
134
+
135
+
136
+ def load_oxysoft_file(file_path: str, progress=None) -> Dataset:
137
+ """Parse a single Oxysoft .txt export into a Dataset.
138
+
139
+ `progress` (None, True or callable(fraction, message); see
140
+ PyATCLI.progress) follows the file as it is read.
141
+ """
142
+ result = processing_oxysoft.process_oxysoft_file(file_path, progress=progress)
143
+ return _oxysoft_dataset(result, folder_path=os.path.dirname(file_path),
144
+ folder_name=os.path.splitext(os.path.basename(file_path))[0])
145
+
146
+
147
+ def _oxysoft_dataset(result: dict, folder_path: str, folder_name: str) -> Dataset:
148
+ o2hb, hhb = result["o2hb"], result["hhb"]
149
+ sample_rate = result["sample_rate"]
150
+ n_ch, n_samp = o2hb.shape
151
+
152
+ # Stacked as (2 * n_channels, n_samples): first all O2Hb, then all HHb
153
+ signals = np.concatenate([o2hb, hhb], axis=0)
154
+
155
+ # Channel names: e.g. ['Tx1 O2Hb', 'Tx2 O2Hb', 'Tx3 O2Hb', 'Tx1 HHb', ...]
156
+ labels = result["channel_labels"]
157
+ return Dataset(
158
+ source_format = "Oxysoft",
159
+ folder_path = folder_path,
160
+ folder_name = folder_name,
161
+ sample_rate = sample_rate,
162
+ num_samples = n_samp,
163
+ duration_s = n_samp / sample_rate if sample_rate > 0 else 0.0,
164
+ signals = signals,
165
+ channel_names = [f"{l} O2Hb" for l in labels] + [f"{l} HHb" for l in labels],
166
+ num_channels = 2 * n_ch,
167
+ events = result["events"],
168
+ metadata = result["metadata"],
169
+ )