mesofield 0.3.2b0__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 (111) hide show
  1. docs/_static/custom.css +40 -0
  2. docs/_static/favicon.png +0 -0
  3. docs/_static/logo.png +0 -0
  4. docs/api/index.md +70 -0
  5. docs/conf.py +200 -0
  6. docs/developer_guide.md +303 -0
  7. docs/index.md +25 -0
  8. docs/tutorial.md +4 -0
  9. docs/user_guide.md +172 -0
  10. examples/teensy_pulse_generator.py +320 -0
  11. experiments/pipeline_demo/experiment.json +24 -0
  12. experiments/pipeline_demo/hardware.yaml +23 -0
  13. experiments/pipeline_demo/procedure.py +50 -0
  14. experiments/two_cam_demo/experiment.json +24 -0
  15. experiments/two_cam_demo/hardware.yaml +58 -0
  16. experiments/two_cam_demo/load_dataset.py +213 -0
  17. experiments/two_cam_demo/procedure.py +87 -0
  18. external/video-codecs/openh264-1.8.0-win64.dll +0 -0
  19. mesofield/__init__.py +45 -0
  20. mesofield/__main__.py +11 -0
  21. mesofield/_version.py +24 -0
  22. mesofield/base.py +750 -0
  23. mesofield/cli/__init__.py +57 -0
  24. mesofield/cli/_richhelp.py +100 -0
  25. mesofield/cli/acquire.py +254 -0
  26. mesofield/cli/datakit.py +165 -0
  27. mesofield/cli/process.py +376 -0
  28. mesofield/cli/rig.py +108 -0
  29. mesofield/cli/tools.py +347 -0
  30. mesofield/config.py +751 -0
  31. mesofield/data/__init__.py +23 -0
  32. mesofield/data/batch.py +633 -0
  33. mesofield/data/manager.py +388 -0
  34. mesofield/data/writer.py +289 -0
  35. mesofield/datakit/__init__.py +44 -0
  36. mesofield/datakit/__main__.py +35 -0
  37. mesofield/datakit/_utils/_logger.py +5 -0
  38. mesofield/datakit/_version.py +141 -0
  39. mesofield/datakit/config.py +50 -0
  40. mesofield/datakit/core.py +783 -0
  41. mesofield/datakit/datamodel.py +200 -0
  42. mesofield/datakit/discover.py +124 -0
  43. mesofield/datakit/explore.py +651 -0
  44. mesofield/datakit/notebooks/pupil_dlc.ipynb +2445 -0
  45. mesofield/datakit/profile.py +535 -0
  46. mesofield/datakit/shell.py +83 -0
  47. mesofield/datakit/sources/__init__.py +65 -0
  48. mesofield/datakit/sources/analysis/mesomap.py +194 -0
  49. mesofield/datakit/sources/analysis/mesoscope.py +77 -0
  50. mesofield/datakit/sources/analysis/pupil.py +246 -0
  51. mesofield/datakit/sources/behavior/__init__.py +0 -0
  52. mesofield/datakit/sources/behavior/dataqueue.py +281 -0
  53. mesofield/datakit/sources/behavior/psychopy.py +364 -0
  54. mesofield/datakit/sources/behavior/treadmill.py +323 -0
  55. mesofield/datakit/sources/behavior/wheel.py +277 -0
  56. mesofield/datakit/sources/camera/mesoscope.py +32 -0
  57. mesofield/datakit/sources/camera/metadata_json.py +130 -0
  58. mesofield/datakit/sources/camera/pupil.py +28 -0
  59. mesofield/datakit/sources/camera/suite2p.py +547 -0
  60. mesofield/datakit/sources/register.py +204 -0
  61. mesofield/datakit/sources/session/config.py +130 -0
  62. mesofield/datakit/sources/session/notes.py +63 -0
  63. mesofield/datakit/sources/session/timestamps.py +58 -0
  64. mesofield/datakit/timeline.py +306 -0
  65. mesofield/devices/__init__.py +42 -0
  66. mesofield/devices/base.py +498 -0
  67. mesofield/devices/base_camera.py +295 -0
  68. mesofield/devices/cameras.py +740 -0
  69. mesofield/devices/daq.py +151 -0
  70. mesofield/devices/encoder.py +384 -0
  71. mesofield/devices/mocks.py +275 -0
  72. mesofield/devices/psychopy_device.py +455 -0
  73. mesofield/devices/subprocesses/__init__.py +0 -0
  74. mesofield/devices/subprocesses/psychopy.py +133 -0
  75. mesofield/devices/treadmill.py +318 -0
  76. mesofield/engines.py +380 -0
  77. mesofield/gui/Mesofield_icon.png +0 -0
  78. mesofield/gui/__init__.py +76 -0
  79. mesofield/gui/config_wizard.py +724 -0
  80. mesofield/gui/controller.py +535 -0
  81. mesofield/gui/dynamic_controller.py +78 -0
  82. mesofield/gui/maingui.py +427 -0
  83. mesofield/gui/mdagui.py +285 -0
  84. mesofield/gui/qt_device_adapter.py +109 -0
  85. mesofield/gui/speedplotter.py +152 -0
  86. mesofield/gui/theme.py +445 -0
  87. mesofield/gui/tiff_viewer.py +1050 -0
  88. mesofield/gui/viewer.py +691 -0
  89. mesofield/hardware.py +549 -0
  90. mesofield/playback.py +1298 -0
  91. mesofield/processing/__init__.py +12 -0
  92. mesofield/processing/runner.py +237 -0
  93. mesofield/processors/__init__.py +13 -0
  94. mesofield/processors/base.py +287 -0
  95. mesofield/processors/frame_mean.py +19 -0
  96. mesofield/protocols.py +378 -0
  97. mesofield/scaffold/__init__.py +34 -0
  98. mesofield/scaffold/experiment.py +400 -0
  99. mesofield/scaffold/rigs.py +121 -0
  100. mesofield/signals.py +85 -0
  101. mesofield/utils/__init__.py +0 -0
  102. mesofield/utils/_logger.py +156 -0
  103. mesofield/utils/retrofit.py +309 -0
  104. mesofield/utils/utils.py +217 -0
  105. mesofield-0.3.2b0.dist-info/METADATA +178 -0
  106. mesofield-0.3.2b0.dist-info/RECORD +111 -0
  107. mesofield-0.3.2b0.dist-info/WHEEL +5 -0
  108. mesofield-0.3.2b0.dist-info/entry_points.txt +2 -0
  109. mesofield-0.3.2b0.dist-info/licenses/LICENSE +21 -0
  110. mesofield-0.3.2b0.dist-info/top_level.txt +6 -0
  111. scripts/bench_frame_processor.py +103 -0
@@ -0,0 +1,130 @@
1
+ """Shared JSON metadata loader for camera sources."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from pathlib import Path
6
+ from typing import Any
7
+
8
+ import json
9
+
10
+ import numpy as np
11
+ import pandas as pd
12
+
13
+ from mesofield.datakit.sources.register import LoadContext, TableSource
14
+
15
+
16
+ class MetadataJSON(TableSource):
17
+ """Load camera metadata JSON into a normalized table."""
18
+
19
+ json_entry_key = "p0"
20
+ allow_fallback_entry_key = False
21
+ metadata_column = "camera_metadata"
22
+ timestamp_preference = ("TimeReceivedByCore", "ElapsedTime-ms", "runner_time_ms")
23
+ millisecond_columns = {"ElapsedTime-ms", "runner_time_ms"}
24
+ drop_columns: tuple[str, ...] = ()
25
+ device_column = "camera_device"
26
+
27
+ def build_table(
28
+ self,
29
+ path: Path,
30
+ *,
31
+ context: LoadContext | None = None,
32
+ ) -> tuple[np.ndarray, pd.DataFrame, dict[str, Any]]:
33
+ """Normalize metadata rows and build a timeline."""
34
+ with open(path, "r") as handle:
35
+ data = json.load(handle)
36
+ entry_key, payload = self._resolve_entry(data, path)
37
+
38
+ df = pd.DataFrame(payload)
39
+
40
+ device_id = None
41
+ if self.device_column in df.columns and len(df):
42
+ device_id = str(df[self.device_column].iloc[0])
43
+
44
+ if self.metadata_column in df.columns:
45
+ camera_metadata_df = pd.json_normalize(df[self.metadata_column].tolist())
46
+ non_overlapping = [
47
+ col for col in camera_metadata_df.columns if col not in df.columns
48
+ ]
49
+ df = df.join(camera_metadata_df[non_overlapping])
50
+
51
+ existing_columns = [col for col in self.drop_columns if col in df.columns]
52
+ if existing_columns:
53
+ df = df.drop(columns=existing_columns)
54
+
55
+ t, absolute = self._resolve_timeline(df, path)
56
+ df = df.copy()
57
+ df["time_elapsed_s"] = t
58
+ if absolute is not None:
59
+ df["time_absolute"] = absolute
60
+
61
+ # Memory optimizations for full-length per-row arrays in the materialized
62
+ # output: downcast numeric counters and convert low-cardinality string
63
+ # columns to categoricals.
64
+ for col in ("ImageNumber",):
65
+ if col in df.columns:
66
+ numeric = pd.to_numeric(df[col], errors="coerce")
67
+ if numeric.notna().all() and (numeric >= 0).all() and numeric.max() < np.iinfo(np.uint32).max:
68
+ df[col] = numeric.astype(np.uint32)
69
+ for col in ("Temperature",):
70
+ if col in df.columns:
71
+ numeric = pd.to_numeric(df[col], errors="coerce")
72
+ if numeric.notna().any():
73
+ df[col] = numeric.astype(np.float32)
74
+ for col in ("Camera", self.device_column):
75
+ if col in df.columns:
76
+ df[col] = df[col].astype("category")
77
+
78
+ meta = {
79
+ "source_file": str(path),
80
+ "n_frames": int(len(df)),
81
+ "device_id": device_id,
82
+ "json_entry_key": entry_key,
83
+ }
84
+
85
+ return t.astype(np.float64), df, meta
86
+
87
+ def _resolve_entry(self, data: dict[str, Any], path: Path) -> tuple[str, Any]:
88
+ if not isinstance(data, dict):
89
+ raise ValueError(f"Unsupported metadata format in {path}")
90
+
91
+ payload = data.get(self.json_entry_key)
92
+ if payload is None and self.allow_fallback_entry_key:
93
+ if data:
94
+ entry_key, payload = next(iter(data.items()))
95
+ return entry_key, payload
96
+ if payload is None:
97
+ raise KeyError(
98
+ f"Missing expected entry '{self.json_entry_key}' in {path}; found keys: {list(data.keys())}"
99
+ )
100
+ return self.json_entry_key, payload
101
+
102
+ def _resolve_timeline(
103
+ self, df: pd.DataFrame, path: Path
104
+ ) -> tuple[np.ndarray, pd.Series | None]:
105
+ t: np.ndarray | None = None
106
+ absolute: pd.Series | None = None
107
+
108
+ for column in self.timestamp_preference:
109
+ if column not in df.columns:
110
+ continue
111
+ if column == self.timestamp_preference[0]:
112
+ timestamps = pd.to_datetime(df[column], errors="coerce", utc=True)
113
+ valid = timestamps.dropna()
114
+ if not valid.empty:
115
+ origin = valid.iloc[0]
116
+ t = (timestamps - origin).dt.total_seconds().to_numpy(dtype=np.float64)
117
+ # Keep as datetime64[ns, UTC] (8 B/value) rather than ISO strings (~30 B/value).
118
+ absolute = timestamps
119
+ else:
120
+ scale = 1000.0 if column in self.millisecond_columns else 1.0
121
+ values = pd.to_numeric(df[column], errors="coerce").to_numpy(dtype=np.float64)
122
+ if values.size:
123
+ t = (values - values[0]) / scale
124
+ if t is not None:
125
+ break
126
+
127
+ if t is None:
128
+ raise ValueError(f"No recognized timestamp column found in {path}")
129
+
130
+ return t.astype(np.float64), absolute
@@ -0,0 +1,28 @@
1
+ """Pupil camera metadata data source."""
2
+
3
+ from mesofield.datakit.sources.camera.metadata_json import MetadataJSON
4
+
5
+
6
+ class PupilMetadataSource(MetadataJSON):
7
+ """Load pupil camera metadata JSON files as a table."""
8
+ tag = "pupil_metadata"
9
+ patterns = ("**/*_pupil.mp4_frame_metadata.json",
10
+ "**/*_pupil.ome.tiff_frame_metadata.json")
11
+ camera_tag = "pupil_metadata"
12
+ flatten_payload = False
13
+ drop_columns = (
14
+ "camera_metadata",
15
+ "property_values",
16
+ "version",
17
+ "format",
18
+ "camera_device",
19
+ "pixel_size_um",
20
+ "images_remaining_in_buffer",
21
+ "PixelType",
22
+ "hardware_triggered",
23
+ # Redundant timestamp columns: time_elapsed_s + time_absolute already
24
+ # cover the same clock at smaller dtypes.
25
+ "ElapsedTime-ms",
26
+ "TimeReceivedByCore",
27
+ )
28
+ allow_fallback_entry_key = True