ezmsg-peripheraldevice 0.2.0__tar.gz → 0.4.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.
Files changed (25) hide show
  1. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/PKG-INFO +4 -4
  2. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/docs/source/_templates/autosummary/module.rst +1 -0
  3. ezmsg_peripheraldevice-0.4.0/docs/source/_templates/navbar-logo.html +18 -0
  4. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/docs/source/conf.py +4 -1
  5. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/examples/mouse_poll.py +1 -1
  6. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/pyproject.toml +2 -2
  7. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/src/ezmsg/peripheraldevice/__init__.py +2 -2
  8. ezmsg_peripheraldevice-0.4.0/src/ezmsg/peripheraldevice/__version__.py +24 -0
  9. ezmsg_peripheraldevice-0.4.0/src/ezmsg/peripheraldevice/mouse.py +371 -0
  10. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/tests/test_mouse.py +155 -16
  11. ezmsg_peripheraldevice-0.2.0/src/ezmsg/peripheraldevice/__version__.py +0 -34
  12. ezmsg_peripheraldevice-0.2.0/src/ezmsg/peripheraldevice/mouse.py +0 -263
  13. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/.github/workflows/docs.yml +0 -0
  14. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/.github/workflows/python-publish.yml +0 -0
  15. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/.github/workflows/python-tests.yml +0 -0
  16. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/.gitignore +0 -0
  17. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/.pre-commit-config.yaml +0 -0
  18. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/LICENSE +0 -0
  19. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/README.md +0 -0
  20. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/docs/Makefile +0 -0
  21. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/docs/make.bat +0 -0
  22. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/docs/source/api/index.rst +0 -0
  23. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/docs/source/index.md +0 -0
  24. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/examples/mouse_listen.py +0 -0
  25. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.4.0}/tests/test_example.py +0 -0
@@ -1,13 +1,13 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: ezmsg-peripheraldevice
3
- Version: 0.2.0
3
+ Version: 0.4.0
4
4
  Summary: Modules to stream data from peripheral devices into ezmsg
5
5
  Author-email: Chadwick Boulay <chadwick.boulay@gmail.com>
6
6
  License-Expression: MIT
7
7
  License-File: LICENSE
8
8
  Requires-Python: >=3.10
9
- Requires-Dist: ezmsg-baseproc>=1.1.0
10
- Requires-Dist: ezmsg>=3.6.0
9
+ Requires-Dist: ezmsg-baseproc>=1.17.0
10
+ Requires-Dist: ezmsg>=3.10.0b7
11
11
  Requires-Dist: numpy>=1.26.0
12
12
  Requires-Dist: pynput>=1.8.1
13
13
  Description-Content-Type: text/markdown
@@ -1,6 +1,7 @@
1
1
  {{ fullname | escape | underline}}
2
2
 
3
3
  .. automodule:: {{ fullname }}
4
+ :no-members:
4
5
 
5
6
  {% block attributes %}
6
7
  {% if attributes %}
@@ -0,0 +1,18 @@
1
+ {# Override of pydata-sphinx-theme's navbar-logo component.
2
+ Splits the brand into two links: the logo image goes to the ezmsg.org
3
+ landing page (``logo.link``) and the title text goes to this site's root. #}
4
+ {% set org_href = theme_logo.get("link") or pathto(root_doc) %}
5
+ {% set alt = theme_logo.get("alt_text", "ezmsg.org") %}
6
+ {% if "light" in theme_logo["image_relative"] %}
7
+ {% if default_mode is undefined or default_mode == "auto" %}
8
+ {% set default_mode = "light" %}
9
+ {% endif %}
10
+ {% set js_mode = "light" if default_mode == "dark" else "dark" %}
11
+ <a class="navbar-brand logo" href="{{ org_href }}" title="ezmsg.org" style="margin-right: 0.75rem;">
12
+ <img src="{{ theme_logo['image_relative'][default_mode] }}" class="logo__image only-{{ default_mode }}" alt="{{ alt }}"/>
13
+ <img src="{{ theme_logo['image_relative'][js_mode] }}" class="logo__image only-{{ js_mode }} pst-js-only" alt="{{ alt }}"/>
14
+ </a>
15
+ {% endif %}
16
+ <a class="navbar-brand logo" href="{{ pathto(root_doc) }}">
17
+ <p class="title logo__title">{{ theme_logo.get("text") or docstitle }}</p>
18
+ </a>
@@ -85,7 +85,10 @@ html_baseurl = "https://www.ezmsg.org/ezmsg-peripheraldevice/"
85
85
  html_theme_options = {
86
86
  "logo": {
87
87
  "text": f"ezmsg.peripheraldevice {version}",
88
- "link": "https://ezmsg.org",
88
+ "link": "https://www.ezmsg.org",
89
+ "image_light": "https://www.ezmsg.org/_static/_images/ezmsg_logo.png",
90
+ "image_dark": "https://www.ezmsg.org/_static/_images/ezmsg_logo.png",
91
+ "alt_text": "ezmsg.org",
89
92
  },
90
93
  "header_links_before_dropdown": 4,
91
94
  "navbar_start": ["navbar-logo"],
@@ -43,7 +43,7 @@ class MousePollSystem(ez.Collection):
43
43
 
44
44
  def network(self) -> ez.NetworkDefinition:
45
45
  return (
46
- (self.CLOCK.OUTPUT_SIGNAL, self.MOUSE.INPUT_SIGNAL),
46
+ (self.CLOCK.OUTPUT_SIGNAL, self.MOUSE.INPUT_CLOCK),
47
47
  (self.MOUSE.OUTPUT_SIGNAL, self.LOG.INPUT),
48
48
  )
49
49
 
@@ -8,8 +8,8 @@ license = "MIT"
8
8
  readme = "README.md"
9
9
  requires-python = ">=3.10"
10
10
  dependencies = [
11
- "ezmsg>=3.6.0",
12
- "ezmsg-baseproc>=1.1.0",
11
+ "ezmsg>=3.10.0b7",
12
+ "ezmsg-baseproc>=1.17.0",
13
13
  "numpy>=1.26.0",
14
14
  "pynput>=1.8.1",
15
15
  ]
@@ -5,9 +5,9 @@ from .mouse import (
5
5
  MouseListenerSettings,
6
6
  MouseListenerState,
7
7
  MousePoller,
8
+ MousePollerProducer,
8
9
  MousePollerSettings,
9
10
  MousePollerState,
10
- MousePollerTransformer,
11
11
  )
12
12
 
13
13
  __all__ = [
@@ -16,8 +16,8 @@ __all__ = [
16
16
  "MouseListenerSettings",
17
17
  "MouseListenerState",
18
18
  "MouseListener",
19
+ "MousePollerProducer",
19
20
  "MousePollerSettings",
20
21
  "MousePollerState",
21
- "MousePollerTransformer",
22
22
  "MousePoller",
23
23
  ]
@@ -0,0 +1,24 @@
1
+ # file generated by vcs-versioning
2
+ # don't change, don't track in version control
3
+ from __future__ import annotations
4
+
5
+ __all__ = [
6
+ "__version__",
7
+ "__version_tuple__",
8
+ "version",
9
+ "version_tuple",
10
+ "__commit_id__",
11
+ "commit_id",
12
+ ]
13
+
14
+ version: str
15
+ __version__: str
16
+ __version_tuple__: tuple[int | str, ...]
17
+ version_tuple: tuple[int | str, ...]
18
+ commit_id: str | None
19
+ __commit_id__: str | None
20
+
21
+ __version__ = version = '0.4.0'
22
+ __version_tuple__ = version_tuple = (0, 4, 0)
23
+
24
+ __commit_id__ = commit_id = None
@@ -0,0 +1,371 @@
1
+ """Mouse input via pynput."""
2
+
3
+ import queue
4
+ import threading
5
+ import time
6
+ import time as time_module
7
+ from collections import deque
8
+
9
+ import ezmsg.core as ez
10
+ import numpy as np
11
+ from ezmsg.baseproc import (
12
+ BaseClockDrivenProducer,
13
+ BaseClockDrivenUnit,
14
+ BaseProducerUnit,
15
+ BaseStatefulProducer,
16
+ ClockDrivenSettings,
17
+ ClockDrivenState,
18
+ processor_state,
19
+ )
20
+ from ezmsg.util.messages.axisarray import AxisArray, compute_config_id, replace
21
+ from pynput.mouse import Controller, Listener
22
+
23
+ # =============================================================================
24
+ # Polled Mouse Producer (takes LinearAxis from Clock)
25
+ # =============================================================================
26
+
27
+
28
+ class MousePollerSettings(ClockDrivenSettings):
29
+ """Settings for MousePollerProducer."""
30
+
31
+ fs: float = 60.0
32
+ """
33
+ Output sample rate in Hz (only used when n_time is None).
34
+
35
+ When n_time is specified, the effective poll rate is derived from the clock:
36
+ poll_rate = n_time / clock.gain (i.e., n_time samples per tick).
37
+
38
+ When n_time is None, fs determines both the poll rate and output sample rate,
39
+ with n_samples = fs * clock.gain per tick.
40
+ """
41
+
42
+ n_time: int | None = None
43
+ """
44
+ Samples per block.
45
+ - If specified: fixed chunk size, poll rate derived from clock timing
46
+ - If None: derived from fs * clock.gain, poll rate is fs
47
+ """
48
+
49
+
50
+ @processor_state
51
+ class MousePollerState(ClockDrivenState):
52
+ """State for MousePollerProducer."""
53
+
54
+ controller: Controller | None = None
55
+ template: AxisArray | None = None
56
+
57
+ # Background polling state
58
+ poll_thread: threading.Thread | None = None
59
+ poll_buffer: deque | None = None
60
+ stop_event: threading.Event | None = None
61
+ last_position: tuple[float, float] = (0.0, 0.0)
62
+ use_thread: bool = False
63
+ poll_rate: float = 0.0
64
+
65
+
66
+ class MousePollerProducer(BaseClockDrivenProducer[MousePollerSettings, MousePollerState]):
67
+ """
68
+ Reads mouse position, optionally with high-rate background polling.
69
+
70
+ Takes LinearAxis input (from Clock) and outputs mouse positions.
71
+
72
+ Behavior depends on settings:
73
+
74
+ **Fixed chunk mode (n_time is set):**
75
+ - Each tick produces exactly n_time samples
76
+ - Poll rate = n_time * clock_rate (derived from clock timing)
77
+ - settings.fs is ignored
78
+ - Thread used when n_time > 1
79
+
80
+ **Variable chunk mode (n_time is None):**
81
+ - Each tick produces fs * clock.gain samples (with fractional tracking)
82
+ - Poll rate = fs (from settings)
83
+ - Thread used when fs != clock_rate
84
+
85
+ Input: LinearAxis (from Clock - provides timing info)
86
+ Output: AxisArray with shape (n_samples, 2) - x, y channels
87
+ """
88
+
89
+ def _hash_message(self, message: AxisArray.LinearAxis) -> int:
90
+ """
91
+ Hash based on clock gain to detect rate changes.
92
+
93
+ Returns different hash when clock rate changes significantly,
94
+ triggering state reset and potential thread restart.
95
+ """
96
+ # Quantize gain to avoid floating point noise triggering resets
97
+ quantized_gain = round(message.gain * 1e6)
98
+ return hash(quantized_gain)
99
+
100
+ def _reset_state(self, time_axis: AxisArray.LinearAxis) -> None:
101
+ """Initialize mouse controller and optionally start polling thread."""
102
+ # Stop any existing polling thread
103
+ self._stop_poll_thread()
104
+
105
+ self._state.controller = Controller()
106
+ self._state.last_position = self._state.controller.position
107
+
108
+ clock_rate = 1.0 / time_axis.gain if time_axis.gain > 0 else float("inf")
109
+
110
+ if self.settings.n_time is not None:
111
+ # Fixed chunk mode: poll rate derived from clock timing
112
+ # Need n_time samples per tick, so poll at n_time * clock_rate
113
+ self._state.poll_rate = self.settings.n_time * clock_rate
114
+ need_thread = self.settings.n_time > 1
115
+ else:
116
+ # Variable chunk mode: poll rate is fs from settings
117
+ # n_samples = fs * clock.gain (handled by base class)
118
+ self._state.poll_rate = self.settings.fs
119
+ # Need thread if fs != clock_rate (meaning n_samples != 1)
120
+ need_thread = not np.isclose(self.settings.fs, clock_rate, rtol=0.01)
121
+
122
+ self._state.use_thread = need_thread
123
+
124
+ if self._state.use_thread:
125
+ # Start background polling thread
126
+ buffer_size = max(int(self._state.poll_rate * 10), 1000)
127
+ self._state.poll_buffer = deque(maxlen=buffer_size)
128
+ self._state.stop_event = threading.Event()
129
+ self._state.poll_thread = threading.Thread(
130
+ target=self._poll_loop,
131
+ daemon=True,
132
+ )
133
+ self._state.poll_thread.start()
134
+
135
+ # Pre-construct template AxisArray (shape will be updated in _produce)
136
+ n_time_for_template = self.settings.n_time if self.settings.n_time is not None else 1
137
+ self._state.template = AxisArray(
138
+ data=np.zeros((n_time_for_template, 2), dtype=np.float64),
139
+ dims=["time", "ch"],
140
+ axes={
141
+ # Not `time_axis`, the clock tick: the outputs' time axis is 1 / fs (BaseClockDrivenProducer),
142
+ # and config_id digests its gain. The poll timing above still follows the tick.
143
+ "time": AxisArray.TimeAxis(fs=self.settings.fs),
144
+ "ch": AxisArray.CoordinateAxis(
145
+ data=np.array(["x", "y"]),
146
+ dims=["ch"],
147
+ ),
148
+ },
149
+ key="mouse",
150
+ segment=0 if self._state.template is None else self._state.template.segment + 1,
151
+ )
152
+ self._state.template.config_id = compute_config_id(self._state.template)
153
+
154
+ def _poll_loop(self) -> None:
155
+ """Background thread that polls mouse at poll_rate."""
156
+ interval = 1.0 / self._state.poll_rate
157
+ last_poll = time_module.perf_counter() - interval
158
+
159
+ while not self._state.stop_event.is_set():
160
+ sleep_time = (last_poll + interval) - time_module.perf_counter()
161
+ if sleep_time > 0:
162
+ time_module.sleep(sleep_time)
163
+ pos = self._state.controller.position
164
+ self._state.poll_buffer.append(pos)
165
+ self._state.last_position = pos
166
+ last_poll = time_module.perf_counter()
167
+
168
+ def _stop_poll_thread(self) -> None:
169
+ """Stop the background polling thread if running."""
170
+ if self._state.stop_event is not None:
171
+ self._state.stop_event.set()
172
+ if self._state.poll_thread is not None:
173
+ self._state.poll_thread.join(timeout=1.0)
174
+ self._state.poll_thread = None
175
+ self._state.stop_event = None
176
+ self._state.poll_buffer = None
177
+
178
+ def _produce(self, n_samples: int, time_axis: AxisArray.LinearAxis) -> AxisArray:
179
+ """Generate mouse position data."""
180
+ if self._state.use_thread and self._state.poll_buffer is not None:
181
+ # Get samples from buffer
182
+ positions = []
183
+ for _ in range(n_samples):
184
+ if self._state.poll_buffer:
185
+ pos = self._state.poll_buffer.popleft()
186
+ self._state.last_position = pos
187
+ else:
188
+ # Buffer empty - hold last known position
189
+ pos = self._state.last_position
190
+ positions.append([pos[0], pos[1]])
191
+ data = np.array(positions, dtype=np.float64)
192
+ else:
193
+ # Simple single-poll mode
194
+ pos = self._state.controller.position
195
+ data = np.array([[pos[0], pos[1]]], dtype=np.float64)
196
+
197
+ return replace(
198
+ self._state.template,
199
+ data=data,
200
+ axes={**self._state.template.axes, "time": time_axis},
201
+ )
202
+
203
+ def __del__(self) -> None:
204
+ """Stop polling thread on destruction."""
205
+ if hasattr(self, "_state"):
206
+ self._stop_poll_thread()
207
+
208
+
209
+ class MousePoller(BaseClockDrivenUnit[MousePollerSettings, MousePollerProducer]):
210
+ """
211
+ Unit for reading mouse position from Clock input.
212
+
213
+ Receives LinearAxis from Clock and outputs mouse positions.
214
+ Supports both simple polling (one sample per tick) and high-rate
215
+ background polling with buffering.
216
+ """
217
+
218
+ SETTINGS = MousePollerSettings
219
+
220
+
221
+ # =============================================================================
222
+ # Event-driven Mouse Listener Producer
223
+ # =============================================================================
224
+
225
+
226
+ class MouseListenerSettings(ez.Settings):
227
+ """Settings for MouseListenerProducer."""
228
+
229
+ pass
230
+
231
+
232
+ @processor_state
233
+ class MouseListenerState:
234
+ """State for MouseListenerProducer."""
235
+
236
+ listener: Listener | None = None
237
+ event_queue: queue.Queue | None = None
238
+ template: AxisArray | None = None
239
+
240
+
241
+ class MouseListenerProducer(BaseStatefulProducer[MouseListenerSettings, AxisArray, MouseListenerState]):
242
+ """
243
+ Produces mouse position events as they occur.
244
+
245
+ Uses pynput.mouse.Listener to capture mouse move events with timestamps.
246
+ Events are queued and emitted as AxisArray messages with irregular
247
+ CoordinateAxis timestamps.
248
+
249
+ Output: AxisArray with shape (n_events, 2) where n_events varies,
250
+ time axis is CoordinateAxis with actual event timestamps.
251
+ """
252
+
253
+ def _reset_state(self) -> None:
254
+ """Initialize listener and queue."""
255
+ self._state.event_queue = queue.Queue()
256
+
257
+ def on_move(x: int, y: int) -> None:
258
+ """Callback for mouse movement events."""
259
+ print(f"on_move called: ({x}, {y})") # DEBUG
260
+ self._state.event_queue.put((x, y, time.monotonic()))
261
+
262
+ # def on_click(x, y, button, pressed):
263
+ # print(f"{'Pressed' if pressed else 'Released'} at {(x, y)}")
264
+ # if not pressed:
265
+ # # Stop listener
266
+ # return False
267
+
268
+ self._state.listener = Listener(
269
+ on_move=on_move,
270
+ # on_click=on_click
271
+ )
272
+ self._state.listener.start()
273
+
274
+ # Check if process is trusted for input monitoring (macOS)
275
+ if hasattr(Listener, "IS_TRUSTED"):
276
+ if not Listener.IS_TRUSTED:
277
+ import warnings
278
+
279
+ warnings.warn(
280
+ "Process is not trusted for input monitoring. "
281
+ "On macOS, add your terminal to Accessibility clients: "
282
+ "System Settings > Privacy & Security > Accessibility. "
283
+ "Then fully restart your terminal (Cmd+Q).",
284
+ RuntimeWarning,
285
+ stacklevel=2,
286
+ )
287
+
288
+ # Pre-construct template AxisArray
289
+ self._state.template = AxisArray(
290
+ data=np.zeros((0, 2), dtype=np.float64),
291
+ dims=["time", "ch"],
292
+ axes={
293
+ "time": AxisArray.CoordinateAxis(
294
+ data=np.array([], dtype=np.float64),
295
+ dims=["time"],
296
+ unit="s",
297
+ ),
298
+ "ch": AxisArray.CoordinateAxis(
299
+ data=np.array(["x", "y"]),
300
+ dims=["ch"],
301
+ ),
302
+ },
303
+ key="mouse",
304
+ # Events have no regular rate, so only a restarted listener breaks the stream.
305
+ segment=0 if self._state.template is None else self._state.template.segment + 1,
306
+ )
307
+ self._state.template.config_id = compute_config_id(self._state.template)
308
+
309
+ def _drain_queue(self) -> tuple[list[float], list[float], list[float]]:
310
+ """Drain all events from queue."""
311
+ x_vals: list[float] = []
312
+ y_vals: list[float] = []
313
+ timestamps: list[float] = []
314
+
315
+ while True:
316
+ try:
317
+ x, y, t = self._state.event_queue.get_nowait()
318
+ x_vals.append(float(x))
319
+ y_vals.append(float(y))
320
+ timestamps.append(t)
321
+ except queue.Empty:
322
+ break
323
+
324
+ return x_vals, y_vals, timestamps
325
+
326
+ def _build_output(self, x_vals: list[float], y_vals: list[float], timestamps: list[float]) -> AxisArray | None:
327
+ """Build output AxisArray from collected events."""
328
+ if not timestamps:
329
+ return None
330
+
331
+ data = np.column_stack([x_vals, y_vals])
332
+ time_axis = replace(
333
+ self._state.template.axes["time"],
334
+ data=np.array(timestamps, dtype=np.float64),
335
+ )
336
+
337
+ return replace(
338
+ self._state.template,
339
+ data=data,
340
+ axes={"time": time_axis, "ch": self._state.template.axes["ch"]},
341
+ )
342
+
343
+ def __call__(self) -> AxisArray | None:
344
+ """Synchronous production - drain queue and return events."""
345
+ if self._hash == -1:
346
+ self._reset_state()
347
+ self._hash = 0
348
+
349
+ x_vals, y_vals, timestamps = self._drain_queue()
350
+ return self._build_output(x_vals, y_vals, timestamps)
351
+
352
+ async def _produce(self) -> AxisArray | None:
353
+ """Async production - drain queue and return events."""
354
+ x_vals, y_vals, timestamps = self._drain_queue()
355
+ return self._build_output(x_vals, y_vals, timestamps)
356
+
357
+ def __del__(self) -> None:
358
+ """Clean up listener on destruction."""
359
+ if hasattr(self, "_state") and self._state.listener is not None:
360
+ self._state.listener.stop()
361
+
362
+
363
+ class MouseListener(BaseProducerUnit[MouseListenerSettings, AxisArray, MouseListenerProducer]):
364
+ """
365
+ Unit for event-driven mouse position capture.
366
+
367
+ Produces AxisArray messages with mouse positions and their timestamps
368
+ as events occur. Time axis is an irregular CoordinateAxis.
369
+ """
370
+
371
+ SETTINGS = MouseListenerSettings
@@ -1,36 +1,39 @@
1
1
  """Tests for mouse input module."""
2
2
 
3
+ import logging
3
4
  import time as time_module
4
5
  from unittest.mock import MagicMock, patch
5
6
 
7
+ import ezmsg.baseproc.stateful
6
8
  import numpy as np
7
- from ezmsg.util.messages.axisarray import AxisArray
9
+ from ezmsg.util.messages.axisarray import AxisArray, compute_config_id
8
10
 
9
11
  from ezmsg.peripheraldevice.mouse import (
10
12
  MouseListenerProducer,
11
13
  MouseListenerSettings,
14
+ MousePollerProducer,
12
15
  MousePollerSettings,
13
- MousePollerTransformer,
14
16
  )
15
17
 
16
18
 
17
- class TestMousePollerTransformer:
18
- """Tests for MousePollerTransformer."""
19
+ class TestMousePollerProducer:
20
+ """Tests for MousePollerProducer."""
19
21
 
20
22
  @patch("ezmsg.peripheraldevice.mouse.Controller")
21
- def test_basic_output(self, mock_controller_class):
22
- """Test that transformer produces valid output."""
23
+ def test_basic_output_simple_mode(self, mock_controller_class):
24
+ """Test that producer produces valid output in simple polling mode."""
23
25
  # Setup mock
24
26
  mock_controller = MagicMock()
25
27
  mock_controller.position = (100, 200)
26
28
  mock_controller_class.return_value = mock_controller
27
29
 
28
- transformer = MousePollerTransformer(MousePollerSettings())
30
+ # Use fs=10 to match clock rate (1/0.1 = 10 Hz) for simple mode
31
+ producer = MousePollerProducer(MousePollerSettings(fs=10.0, n_time=1))
29
32
 
30
33
  # Create a LinearAxis input (like what Clock produces)
31
34
  clock_tick = AxisArray.LinearAxis(gain=0.1, offset=0.0)
32
35
 
33
- result = transformer(clock_tick)
36
+ result = producer(clock_tick)
34
37
 
35
38
  # Check output shape
36
39
  assert result.data.shape == (1, 2), "Output should be (1, 2) for single sample with x, y"
@@ -59,11 +62,12 @@ class TestMousePollerTransformer:
59
62
  mock_controller.position = (500, 300)
60
63
  mock_controller_class.return_value = mock_controller
61
64
 
62
- transformer = MousePollerTransformer(MousePollerSettings())
65
+ # Use matching fs for simple mode
66
+ producer = MousePollerProducer(MousePollerSettings(fs=10.0, n_time=1))
63
67
 
64
68
  clock_tick = AxisArray.LinearAxis(gain=0.1, offset=0.0)
65
69
 
66
- result = transformer(clock_tick)
70
+ result = producer(clock_tick)
67
71
 
68
72
  # Check that values are finite numbers
69
73
  assert np.all(np.isfinite(result.data)), "Output should contain finite numbers"
@@ -75,11 +79,12 @@ class TestMousePollerTransformer:
75
79
  mock_controller.position = (0, 0)
76
80
  mock_controller_class.return_value = mock_controller
77
81
 
78
- transformer = MousePollerTransformer(MousePollerSettings())
82
+ # Use matching fs for simple mode
83
+ producer = MousePollerProducer(MousePollerSettings(fs=20.0, n_time=1))
79
84
 
80
85
  clock_tick = AxisArray.LinearAxis(gain=0.05, offset=1.5)
81
86
 
82
- result = transformer(clock_tick)
87
+ result = producer(clock_tick)
83
88
 
84
89
  # Check that time axis offset matches input
85
90
  time_axis = result.axes["time"]
@@ -87,12 +92,13 @@ class TestMousePollerTransformer:
87
92
  assert time_axis.offset == 1.5
88
93
 
89
94
  @patch("ezmsg.peripheraldevice.mouse.Controller")
90
- def test_multiple_calls(self, mock_controller_class):
91
- """Test that transformer works correctly across multiple calls."""
95
+ def test_multiple_calls_simple_mode(self, mock_controller_class):
96
+ """Test that producer works correctly across multiple calls in simple mode."""
92
97
  mock_controller = MagicMock()
93
98
  mock_controller_class.return_value = mock_controller
94
99
 
95
- transformer = MousePollerTransformer(MousePollerSettings())
100
+ # Use matching fs for simple mode (1/0.1 = 10 Hz)
101
+ producer = MousePollerProducer(MousePollerSettings(fs=10.0, n_time=1))
96
102
 
97
103
  for i in range(5):
98
104
  # Update mock position each call
@@ -100,7 +106,7 @@ class TestMousePollerTransformer:
100
106
 
101
107
  clock_tick = AxisArray.LinearAxis(gain=0.1, offset=i * 0.1)
102
108
 
103
- result = transformer(clock_tick)
109
+ result = producer(clock_tick)
104
110
 
105
111
  assert result.data.shape == (1, 2)
106
112
  assert np.all(np.isfinite(result.data))
@@ -109,6 +115,110 @@ class TestMousePollerTransformer:
109
115
  # Data should match mock position
110
116
  np.testing.assert_array_equal(result.data[0], [i * 10, i * 20])
111
117
 
118
+ @patch("ezmsg.peripheraldevice.mouse.Controller")
119
+ def test_threaded_mode_with_n_time_greater_than_1(self, mock_controller_class):
120
+ """Test that producer uses threaded mode when n_time > 1."""
121
+ mock_controller = MagicMock()
122
+ mock_controller.position = (100, 200)
123
+ mock_controller_class.return_value = mock_controller
124
+
125
+ # n_time > 1 triggers threaded mode
126
+ producer = MousePollerProducer(MousePollerSettings(fs=100.0, n_time=10))
127
+
128
+ clock_tick = AxisArray.LinearAxis(gain=0.1, offset=0.0)
129
+
130
+ # Give the thread a moment to poll
131
+ result = producer(clock_tick)
132
+ time_module.sleep(0.15) # Let thread poll a few times
133
+
134
+ result = producer(clock_tick)
135
+
136
+ # Should have n_time samples
137
+ assert result.data.shape == (10, 2)
138
+ assert np.all(np.isfinite(result.data))
139
+
140
+ # Clean up thread
141
+ del producer
142
+
143
+ @patch("ezmsg.peripheraldevice.mouse.Controller")
144
+ def test_threaded_mode_with_rate_mismatch(self, mock_controller_class):
145
+ """Test variable chunk mode (n_time=None) with fs != clock rate."""
146
+ mock_controller = MagicMock()
147
+ mock_controller.position = (50, 75)
148
+ mock_controller_class.return_value = mock_controller
149
+
150
+ # n_time=None, fs=100 != clock rate of 10 Hz triggers threaded mode
151
+ # n_samples = fs * clock.gain = 100 * 0.1 = 10 samples per tick
152
+ producer = MousePollerProducer(MousePollerSettings(fs=100.0, n_time=None))
153
+
154
+ clock_tick = AxisArray.LinearAxis(gain=0.1, offset=0.0)
155
+
156
+ # First call initializes, give thread time to poll
157
+ result = producer(clock_tick)
158
+ time_module.sleep(0.15) # Let thread poll ~15 times at 100 Hz
159
+
160
+ # Now get result with buffered data
161
+ result = producer(clock_tick)
162
+
163
+ # Should have n_samples = fs * gain = 100 * 0.1 = 10 samples
164
+ assert result.data.shape == (10, 2)
165
+ assert np.all(np.isfinite(result.data))
166
+
167
+ # Clean up thread
168
+ del producer
169
+
170
+ @patch("ezmsg.peripheraldevice.mouse.Controller")
171
+ def test_variable_chunk_mode_simple(self, mock_controller_class):
172
+ """Test variable chunk mode (n_time=None) with fs matching clock rate."""
173
+ mock_controller = MagicMock()
174
+ mock_controller.position = (123, 456)
175
+ mock_controller_class.return_value = mock_controller
176
+
177
+ # n_time=None, fs=10 matches clock rate of 10 Hz - no thread needed
178
+ # n_samples = fs * clock.gain = 10 * 0.1 = 1 sample per tick
179
+ producer = MousePollerProducer(MousePollerSettings(fs=10.0, n_time=None))
180
+
181
+ clock_tick = AxisArray.LinearAxis(gain=0.1, offset=0.0)
182
+
183
+ result = producer(clock_tick)
184
+
185
+ # Should have n_samples = 1 (simple mode, no thread)
186
+ assert result.data.shape == (1, 2)
187
+ np.testing.assert_array_equal(result.data[0], [123, 456])
188
+
189
+ # No thread should be running
190
+ assert producer._state.use_thread is False
191
+
192
+ @patch("ezmsg.peripheraldevice.mouse.compute_config_id", wraps=compute_config_id)
193
+ @patch("ezmsg.peripheraldevice.mouse.Controller")
194
+ def test_config_id_and_segment(self, mock_controller_class, mock_compute, monkeypatch, caplog):
195
+ """One declared id per layout; a new segment per reset; the tick's gain stays out of the id."""
196
+ mock_controller_class.return_value = MagicMock(position=(1, 2))
197
+ # The output check must find nothing to correct.
198
+ monkeypatch.setattr(ezmsg.baseproc.stateful, "_warned_unannounced", set())
199
+
200
+ # One sample per tick (no thread) from a 10 Hz clock, labelled 60 Hz as before.
201
+ producer = MousePollerProducer(MousePollerSettings(fs=60.0, n_time=1))
202
+ with caplog.at_level(logging.WARNING, logger="ezmsg.baseproc.stateful"):
203
+ first = [producer(AxisArray.LinearAxis(gain=0.1, offset=i * 0.1)) for i in range(3)]
204
+ # A new clock rate resets (restarts polling): new segment, same layout.
205
+ faster = [producer(AxisArray.LinearAxis(gain=0.05, offset=1 + i * 0.05)) for i in range(3)]
206
+ producer.update_settings(MousePollerSettings(fs=120.0, n_time=1))
207
+ relabelled = [producer(AxisArray.LinearAxis(gain=0.05, offset=2 + i * 0.05)) for i in range(3)]
208
+ assert not caplog.records
209
+
210
+ for msgs, fs in ((first, 60.0), (faster, 60.0), (relabelled, 120.0)):
211
+ assert msgs[0].axes["time"].gain == 1 / fs
212
+ assert len({m.config_id for m in msgs}) == len({m.segment for m in msgs}) == 1
213
+ assert all(m.config_id and m.config_id == compute_config_id(m) for m in msgs)
214
+ assert faster[0].config_id == first[0].config_id
215
+ assert relabelled[0].config_id != first[0].config_id
216
+ assert [first[0].segment, faster[0].segment, relabelled[0].segment] == [0, 1, 2]
217
+ assert mock_compute.call_count == 3 # once per reset, not per message
218
+
219
+ other = MousePollerProducer(MousePollerSettings(fs=60.0, n_time=1))
220
+ assert other(AxisArray.LinearAxis(gain=0.1, offset=0.0)).segment == 0 # a fresh instance starts at 0
221
+
112
222
 
113
223
  class TestMouseListenerProducer:
114
224
  """Tests for MouseListenerProducer."""
@@ -258,3 +368,32 @@ class TestMouseListenerProducer:
258
368
  assert event2[0] == 160
259
369
  assert event2[1] == 260
260
370
  assert isinstance(event2[2], float) # timestamp
371
+
372
+ @patch("ezmsg.peripheraldevice.mouse.compute_config_id", wraps=compute_config_id)
373
+ @patch("ezmsg.peripheraldevice.mouse.Listener")
374
+ def test_config_id_and_segment(self, mock_listener_class, mock_compute):
375
+ """Every message declares the template's id; a restarted listener starts a new segment."""
376
+ mock_listener_class.return_value = MagicMock()
377
+ producer = MouseListenerProducer(MouseListenerSettings())
378
+ producer()
379
+
380
+ msgs = []
381
+ for i in range(3):
382
+ producer._state.event_queue.put((float(i), float(i), time_module.monotonic() + i))
383
+ producer._state.event_queue.put((float(i), float(i), time_module.monotonic() + i + 0.5))
384
+ msgs.append(producer())
385
+ assert len({m.config_id for m in msgs}) == len({m.segment for m in msgs}) == 1
386
+ assert all(m.config_id and m.config_id == compute_config_id(m) for m in msgs)
387
+ assert mock_compute.call_count == 1
388
+
389
+ producer._request_reset()
390
+ producer()
391
+ producer._state.event_queue.put((0.0, 0.0, time_module.monotonic()))
392
+ restarted = producer()
393
+ assert msgs[0].segment == 0
394
+ assert restarted.config_id == msgs[0].config_id and restarted.segment == 1
395
+
396
+ other = MouseListenerProducer(MouseListenerSettings())
397
+ other()
398
+ other._state.event_queue.put((0.0, 0.0, time_module.monotonic()))
399
+ assert other().segment == 0 # a fresh instance starts at 0
@@ -1,34 +0,0 @@
1
- # file generated by setuptools-scm
2
- # don't change, don't track in version control
3
-
4
- __all__ = [
5
- "__version__",
6
- "__version_tuple__",
7
- "version",
8
- "version_tuple",
9
- "__commit_id__",
10
- "commit_id",
11
- ]
12
-
13
- TYPE_CHECKING = False
14
- if TYPE_CHECKING:
15
- from typing import Tuple
16
- from typing import Union
17
-
18
- VERSION_TUPLE = Tuple[Union[int, str], ...]
19
- COMMIT_ID = Union[str, None]
20
- else:
21
- VERSION_TUPLE = object
22
- COMMIT_ID = object
23
-
24
- version: str
25
- __version__: str
26
- __version_tuple__: VERSION_TUPLE
27
- version_tuple: VERSION_TUPLE
28
- commit_id: COMMIT_ID
29
- __commit_id__: COMMIT_ID
30
-
31
- __version__ = version = '0.2.0'
32
- __version_tuple__ = version_tuple = (0, 2, 0)
33
-
34
- __commit_id__ = commit_id = None
@@ -1,263 +0,0 @@
1
- """Mouse input via pynput."""
2
-
3
- import queue
4
- import time
5
-
6
- import ezmsg.core as ez
7
- import numpy as np
8
- from ezmsg.baseproc import (
9
- BaseProducerUnit,
10
- BaseStatefulProducer,
11
- BaseStatefulTransformer,
12
- BaseTransformerUnit,
13
- processor_state,
14
- )
15
- from ezmsg.util.messages.axisarray import AxisArray, replace
16
- from pynput.mouse import Controller, Listener
17
-
18
- # =============================================================================
19
- # Polled Mouse Transformer (takes LinearAxis from Clock, like Counter)
20
- # =============================================================================
21
-
22
-
23
- class MousePollerSettings(ez.Settings):
24
- """Settings for MousePollerTransformer."""
25
-
26
- pass
27
-
28
-
29
- @processor_state
30
- class MousePollerState:
31
- """State for MousePollerTransformer."""
32
-
33
- controller: Controller | None = None
34
- template: AxisArray | None = None
35
-
36
-
37
- class MousePollerTransformer(
38
- BaseStatefulTransformer[
39
- MousePollerSettings,
40
- AxisArray.LinearAxis,
41
- AxisArray,
42
- MousePollerState,
43
- ]
44
- ):
45
- """
46
- Reads current mouse position when triggered by clock tick.
47
-
48
- Takes LinearAxis input (from Clock) and outputs the current mouse position
49
- as a single sample with the clock's timestamp.
50
-
51
- Input: LinearAxis (from Clock - provides timing info)
52
- Output: AxisArray with shape (1, 2) - single sample with x, y channels
53
- """
54
-
55
- def _reset_state(self, message: AxisArray.LinearAxis) -> None:
56
- """Initialize mouse controller."""
57
- self._state.controller = Controller()
58
-
59
- # Pre-construct template AxisArray
60
- self._state.template = AxisArray(
61
- data=np.zeros((1, 2), dtype=np.float64),
62
- dims=["time", "ch"],
63
- axes={
64
- "time": AxisArray.LinearAxis(
65
- unit="s",
66
- gain=message.gain,
67
- offset=message.offset,
68
- ),
69
- "ch": AxisArray.CoordinateAxis(
70
- data=np.array(["x", "y"]),
71
- dims=["ch"],
72
- ),
73
- },
74
- key="mouse",
75
- )
76
-
77
- def _process(self, message: AxisArray.LinearAxis) -> AxisArray:
78
- """Read current mouse position and return as AxisArray."""
79
- pos = self._state.controller.position
80
-
81
- # Create output with single sample
82
- data = np.array([[pos[0], pos[1]]], dtype=np.float64)
83
- time_axis = replace(
84
- self._state.template.axes["time"],
85
- offset=message.offset,
86
- )
87
-
88
- return replace(
89
- self._state.template,
90
- data=data,
91
- axes={"time": time_axis, "ch": self._state.template.axes["ch"]},
92
- )
93
-
94
-
95
- class MousePoller(
96
- BaseTransformerUnit[
97
- MousePollerSettings,
98
- AxisArray.LinearAxis,
99
- AxisArray,
100
- MousePollerTransformer,
101
- ]
102
- ):
103
- """
104
- Unit for reading mouse position from Clock input.
105
-
106
- Receives LinearAxis from Clock and outputs current mouse position.
107
- """
108
-
109
- SETTINGS = MousePollerSettings
110
-
111
-
112
- # =============================================================================
113
- # Event-driven Mouse Listener Producer
114
- # =============================================================================
115
-
116
-
117
- class MouseListenerSettings(ez.Settings):
118
- """Settings for MouseListenerProducer."""
119
-
120
- pass
121
-
122
-
123
- @processor_state
124
- class MouseListenerState:
125
- """State for MouseListenerProducer."""
126
-
127
- listener: Listener | None = None
128
- event_queue: queue.Queue | None = None
129
- template: AxisArray | None = None
130
-
131
-
132
- class MouseListenerProducer(BaseStatefulProducer[MouseListenerSettings, AxisArray, MouseListenerState]):
133
- """
134
- Produces mouse position events as they occur.
135
-
136
- Uses pynput.mouse.Listener to capture mouse move events with timestamps.
137
- Events are queued and emitted as AxisArray messages with irregular
138
- CoordinateAxis timestamps.
139
-
140
- Output: AxisArray with shape (n_events, 2) where n_events varies,
141
- time axis is CoordinateAxis with actual event timestamps.
142
- """
143
-
144
- def _reset_state(self) -> None:
145
- """Initialize listener and queue."""
146
- self._state.event_queue = queue.Queue()
147
-
148
- def on_move(x: int, y: int) -> None:
149
- """Callback for mouse movement events."""
150
- print(f"on_move called: ({x}, {y})") # DEBUG
151
- self._state.event_queue.put((x, y, time.monotonic()))
152
-
153
- # def on_click(x, y, button, pressed):
154
- # print(f"{'Pressed' if pressed else 'Released'} at {(x, y)}")
155
- # if not pressed:
156
- # # Stop listener
157
- # return False
158
-
159
- self._state.listener = Listener(
160
- on_move=on_move,
161
- # on_click=on_click
162
- )
163
- self._state.listener.start()
164
-
165
- # Check if process is trusted for input monitoring (macOS)
166
- if hasattr(Listener, "IS_TRUSTED"):
167
- if not Listener.IS_TRUSTED:
168
- import warnings
169
-
170
- warnings.warn(
171
- "Process is not trusted for input monitoring. "
172
- "On macOS, add your terminal to Accessibility clients: "
173
- "System Settings > Privacy & Security > Accessibility. "
174
- "Then fully restart your terminal (Cmd+Q).",
175
- RuntimeWarning,
176
- stacklevel=2,
177
- )
178
-
179
- # Pre-construct template AxisArray
180
- self._state.template = AxisArray(
181
- data=np.zeros((0, 2), dtype=np.float64),
182
- dims=["time", "ch"],
183
- axes={
184
- "time": AxisArray.CoordinateAxis(
185
- data=np.array([], dtype=np.float64),
186
- dims=["time"],
187
- unit="s",
188
- ),
189
- "ch": AxisArray.CoordinateAxis(
190
- data=np.array(["x", "y"]),
191
- dims=["ch"],
192
- ),
193
- },
194
- key="mouse",
195
- )
196
-
197
- def _hash_message(self) -> int:
198
- # Return constant - state persists across calls
199
- return 0
200
-
201
- def _drain_queue(self) -> tuple[list[float], list[float], list[float]]:
202
- """Drain all events from queue."""
203
- x_vals: list[float] = []
204
- y_vals: list[float] = []
205
- timestamps: list[float] = []
206
-
207
- while True:
208
- try:
209
- x, y, t = self._state.event_queue.get_nowait()
210
- x_vals.append(float(x))
211
- y_vals.append(float(y))
212
- timestamps.append(t)
213
- except queue.Empty:
214
- break
215
-
216
- return x_vals, y_vals, timestamps
217
-
218
- def _build_output(self, x_vals: list[float], y_vals: list[float], timestamps: list[float]) -> AxisArray | None:
219
- """Build output AxisArray from collected events."""
220
- if not timestamps:
221
- return None
222
-
223
- data = np.column_stack([x_vals, y_vals])
224
- time_axis = replace(
225
- self._state.template.axes["time"],
226
- data=np.array(timestamps, dtype=np.float64),
227
- )
228
-
229
- return replace(
230
- self._state.template,
231
- data=data,
232
- axes={"time": time_axis, "ch": self._state.template.axes["ch"]},
233
- )
234
-
235
- def __call__(self) -> AxisArray | None:
236
- """Synchronous production - drain queue and return events."""
237
- if self._hash == -1:
238
- self._reset_state()
239
- self._hash = 0
240
-
241
- x_vals, y_vals, timestamps = self._drain_queue()
242
- return self._build_output(x_vals, y_vals, timestamps)
243
-
244
- async def _produce(self) -> AxisArray | None:
245
- """Async production - drain queue and return events."""
246
- x_vals, y_vals, timestamps = self._drain_queue()
247
- return self._build_output(x_vals, y_vals, timestamps)
248
-
249
- def __del__(self) -> None:
250
- """Clean up listener on destruction."""
251
- if hasattr(self, "_state") and self._state.listener is not None:
252
- self._state.listener.stop()
253
-
254
-
255
- class MouseListener(BaseProducerUnit[MouseListenerSettings, AxisArray, MouseListenerProducer]):
256
- """
257
- Unit for event-driven mouse position capture.
258
-
259
- Produces AxisArray messages with mouse positions and their timestamps
260
- as events occur. Time axis is an irregular CoordinateAxis.
261
- """
262
-
263
- SETTINGS = MouseListenerSettings