ezmsg-peripheraldevice 0.2.0__tar.gz → 0.3.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 (23) hide show
  1. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/PKG-INFO +2 -2
  2. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/examples/mouse_poll.py +1 -1
  3. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/pyproject.toml +1 -1
  4. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/src/ezmsg/peripheraldevice/__init__.py +2 -2
  5. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/src/ezmsg/peripheraldevice/__version__.py +2 -2
  6. ezmsg_peripheraldevice-0.3.0/src/ezmsg/peripheraldevice/mouse.py +368 -0
  7. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/tests/test_mouse.py +93 -15
  8. ezmsg_peripheraldevice-0.2.0/src/ezmsg/peripheraldevice/mouse.py +0 -263
  9. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/.github/workflows/docs.yml +0 -0
  10. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/.github/workflows/python-publish.yml +0 -0
  11. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/.github/workflows/python-tests.yml +0 -0
  12. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/.gitignore +0 -0
  13. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/.pre-commit-config.yaml +0 -0
  14. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/LICENSE +0 -0
  15. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/README.md +0 -0
  16. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/docs/Makefile +0 -0
  17. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/docs/make.bat +0 -0
  18. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/docs/source/_templates/autosummary/module.rst +0 -0
  19. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/docs/source/api/index.rst +0 -0
  20. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/docs/source/conf.py +0 -0
  21. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/docs/source/index.md +0 -0
  22. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/examples/mouse_listen.py +0 -0
  23. {ezmsg_peripheraldevice-0.2.0 → ezmsg_peripheraldevice-0.3.0}/tests/test_example.py +0 -0
@@ -1,12 +1,12 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ezmsg-peripheraldevice
3
- Version: 0.2.0
3
+ Version: 0.3.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
9
+ Requires-Dist: ezmsg-baseproc>=1.2.1
10
10
  Requires-Dist: ezmsg>=3.6.0
11
11
  Requires-Dist: numpy>=1.26.0
12
12
  Requires-Dist: pynput>=1.8.1
@@ -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
 
@@ -9,7 +9,7 @@ readme = "README.md"
9
9
  requires-python = ">=3.10"
10
10
  dependencies = [
11
11
  "ezmsg>=3.6.0",
12
- "ezmsg-baseproc>=1.1.0",
12
+ "ezmsg-baseproc>=1.2.1",
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
  ]
@@ -28,7 +28,7 @@ version_tuple: VERSION_TUPLE
28
28
  commit_id: COMMIT_ID
29
29
  __commit_id__: COMMIT_ID
30
30
 
31
- __version__ = version = '0.2.0'
32
- __version_tuple__ = version_tuple = (0, 2, 0)
31
+ __version__ = version = '0.3.0'
32
+ __version_tuple__ = version_tuple = (0, 3, 0)
33
33
 
34
34
  __commit_id__ = commit_id = None
@@ -0,0 +1,368 @@
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, 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
+ "time": time_axis,
142
+ "ch": AxisArray.CoordinateAxis(
143
+ data=np.array(["x", "y"]),
144
+ dims=["ch"],
145
+ ),
146
+ },
147
+ key="mouse",
148
+ )
149
+
150
+ def _poll_loop(self) -> None:
151
+ """Background thread that polls mouse at poll_rate."""
152
+ interval = 1.0 / self._state.poll_rate
153
+ last_poll = time_module.perf_counter() - interval
154
+
155
+ while not self._state.stop_event.is_set():
156
+ sleep_time = (last_poll + interval) - time_module.perf_counter()
157
+ if sleep_time > 0:
158
+ time_module.sleep(sleep_time)
159
+ pos = self._state.controller.position
160
+ self._state.poll_buffer.append(pos)
161
+ self._state.last_position = pos
162
+ last_poll = time_module.perf_counter()
163
+
164
+ def _stop_poll_thread(self) -> None:
165
+ """Stop the background polling thread if running."""
166
+ if self._state.stop_event is not None:
167
+ self._state.stop_event.set()
168
+ if self._state.poll_thread is not None:
169
+ self._state.poll_thread.join(timeout=1.0)
170
+ self._state.poll_thread = None
171
+ self._state.stop_event = None
172
+ self._state.poll_buffer = None
173
+
174
+ def _produce(self, n_samples: int, time_axis: AxisArray.LinearAxis) -> AxisArray:
175
+ """Generate mouse position data."""
176
+ if self._state.use_thread and self._state.poll_buffer is not None:
177
+ # Get samples from buffer
178
+ positions = []
179
+ for _ in range(n_samples):
180
+ if self._state.poll_buffer:
181
+ pos = self._state.poll_buffer.popleft()
182
+ self._state.last_position = pos
183
+ else:
184
+ # Buffer empty - hold last known position
185
+ pos = self._state.last_position
186
+ positions.append([pos[0], pos[1]])
187
+ data = np.array(positions, dtype=np.float64)
188
+ else:
189
+ # Simple single-poll mode
190
+ pos = self._state.controller.position
191
+ data = np.array([[pos[0], pos[1]]], dtype=np.float64)
192
+
193
+ return replace(
194
+ self._state.template,
195
+ data=data,
196
+ axes={**self._state.template.axes, "time": time_axis},
197
+ )
198
+
199
+ def __del__(self) -> None:
200
+ """Stop polling thread on destruction."""
201
+ if hasattr(self, "_state"):
202
+ self._stop_poll_thread()
203
+
204
+
205
+ class MousePoller(BaseClockDrivenUnit[MousePollerSettings, MousePollerProducer]):
206
+ """
207
+ Unit for reading mouse position from Clock input.
208
+
209
+ Receives LinearAxis from Clock and outputs mouse positions.
210
+ Supports both simple polling (one sample per tick) and high-rate
211
+ background polling with buffering.
212
+ """
213
+
214
+ SETTINGS = MousePollerSettings
215
+
216
+
217
+ # =============================================================================
218
+ # Event-driven Mouse Listener Producer
219
+ # =============================================================================
220
+
221
+
222
+ class MouseListenerSettings(ez.Settings):
223
+ """Settings for MouseListenerProducer."""
224
+
225
+ pass
226
+
227
+
228
+ @processor_state
229
+ class MouseListenerState:
230
+ """State for MouseListenerProducer."""
231
+
232
+ listener: Listener | None = None
233
+ event_queue: queue.Queue | None = None
234
+ template: AxisArray | None = None
235
+
236
+
237
+ class MouseListenerProducer(BaseStatefulProducer[MouseListenerSettings, AxisArray, MouseListenerState]):
238
+ """
239
+ Produces mouse position events as they occur.
240
+
241
+ Uses pynput.mouse.Listener to capture mouse move events with timestamps.
242
+ Events are queued and emitted as AxisArray messages with irregular
243
+ CoordinateAxis timestamps.
244
+
245
+ Output: AxisArray with shape (n_events, 2) where n_events varies,
246
+ time axis is CoordinateAxis with actual event timestamps.
247
+ """
248
+
249
+ def _reset_state(self) -> None:
250
+ """Initialize listener and queue."""
251
+ self._state.event_queue = queue.Queue()
252
+
253
+ def on_move(x: int, y: int) -> None:
254
+ """Callback for mouse movement events."""
255
+ print(f"on_move called: ({x}, {y})") # DEBUG
256
+ self._state.event_queue.put((x, y, time.monotonic()))
257
+
258
+ # def on_click(x, y, button, pressed):
259
+ # print(f"{'Pressed' if pressed else 'Released'} at {(x, y)}")
260
+ # if not pressed:
261
+ # # Stop listener
262
+ # return False
263
+
264
+ self._state.listener = Listener(
265
+ on_move=on_move,
266
+ # on_click=on_click
267
+ )
268
+ self._state.listener.start()
269
+
270
+ # Check if process is trusted for input monitoring (macOS)
271
+ if hasattr(Listener, "IS_TRUSTED"):
272
+ if not Listener.IS_TRUSTED:
273
+ import warnings
274
+
275
+ warnings.warn(
276
+ "Process is not trusted for input monitoring. "
277
+ "On macOS, add your terminal to Accessibility clients: "
278
+ "System Settings > Privacy & Security > Accessibility. "
279
+ "Then fully restart your terminal (Cmd+Q).",
280
+ RuntimeWarning,
281
+ stacklevel=2,
282
+ )
283
+
284
+ # Pre-construct template AxisArray
285
+ self._state.template = AxisArray(
286
+ data=np.zeros((0, 2), dtype=np.float64),
287
+ dims=["time", "ch"],
288
+ axes={
289
+ "time": AxisArray.CoordinateAxis(
290
+ data=np.array([], dtype=np.float64),
291
+ dims=["time"],
292
+ unit="s",
293
+ ),
294
+ "ch": AxisArray.CoordinateAxis(
295
+ data=np.array(["x", "y"]),
296
+ dims=["ch"],
297
+ ),
298
+ },
299
+ key="mouse",
300
+ )
301
+
302
+ def _hash_message(self) -> int:
303
+ # Return constant - state persists across calls
304
+ return 0
305
+
306
+ def _drain_queue(self) -> tuple[list[float], list[float], list[float]]:
307
+ """Drain all events from queue."""
308
+ x_vals: list[float] = []
309
+ y_vals: list[float] = []
310
+ timestamps: list[float] = []
311
+
312
+ while True:
313
+ try:
314
+ x, y, t = self._state.event_queue.get_nowait()
315
+ x_vals.append(float(x))
316
+ y_vals.append(float(y))
317
+ timestamps.append(t)
318
+ except queue.Empty:
319
+ break
320
+
321
+ return x_vals, y_vals, timestamps
322
+
323
+ def _build_output(self, x_vals: list[float], y_vals: list[float], timestamps: list[float]) -> AxisArray | None:
324
+ """Build output AxisArray from collected events."""
325
+ if not timestamps:
326
+ return None
327
+
328
+ data = np.column_stack([x_vals, y_vals])
329
+ time_axis = replace(
330
+ self._state.template.axes["time"],
331
+ data=np.array(timestamps, dtype=np.float64),
332
+ )
333
+
334
+ return replace(
335
+ self._state.template,
336
+ data=data,
337
+ axes={"time": time_axis, "ch": self._state.template.axes["ch"]},
338
+ )
339
+
340
+ def __call__(self) -> AxisArray | None:
341
+ """Synchronous production - drain queue and return events."""
342
+ if self._hash == -1:
343
+ self._reset_state()
344
+ self._hash = 0
345
+
346
+ x_vals, y_vals, timestamps = self._drain_queue()
347
+ return self._build_output(x_vals, y_vals, timestamps)
348
+
349
+ async def _produce(self) -> AxisArray | None:
350
+ """Async production - drain queue and return events."""
351
+ x_vals, y_vals, timestamps = self._drain_queue()
352
+ return self._build_output(x_vals, y_vals, timestamps)
353
+
354
+ def __del__(self) -> None:
355
+ """Clean up listener on destruction."""
356
+ if hasattr(self, "_state") and self._state.listener is not None:
357
+ self._state.listener.stop()
358
+
359
+
360
+ class MouseListener(BaseProducerUnit[MouseListenerSettings, AxisArray, MouseListenerProducer]):
361
+ """
362
+ Unit for event-driven mouse position capture.
363
+
364
+ Produces AxisArray messages with mouse positions and their timestamps
365
+ as events occur. Time axis is an irregular CoordinateAxis.
366
+ """
367
+
368
+ SETTINGS = MouseListenerSettings
@@ -9,28 +9,29 @@ from ezmsg.util.messages.axisarray import AxisArray
9
9
  from ezmsg.peripheraldevice.mouse import (
10
10
  MouseListenerProducer,
11
11
  MouseListenerSettings,
12
+ MousePollerProducer,
12
13
  MousePollerSettings,
13
- MousePollerTransformer,
14
14
  )
15
15
 
16
16
 
17
- class TestMousePollerTransformer:
18
- """Tests for MousePollerTransformer."""
17
+ class TestMousePollerProducer:
18
+ """Tests for MousePollerProducer."""
19
19
 
20
20
  @patch("ezmsg.peripheraldevice.mouse.Controller")
21
- def test_basic_output(self, mock_controller_class):
22
- """Test that transformer produces valid output."""
21
+ def test_basic_output_simple_mode(self, mock_controller_class):
22
+ """Test that producer produces valid output in simple polling mode."""
23
23
  # Setup mock
24
24
  mock_controller = MagicMock()
25
25
  mock_controller.position = (100, 200)
26
26
  mock_controller_class.return_value = mock_controller
27
27
 
28
- transformer = MousePollerTransformer(MousePollerSettings())
28
+ # Use fs=10 to match clock rate (1/0.1 = 10 Hz) for simple mode
29
+ producer = MousePollerProducer(MousePollerSettings(fs=10.0, n_time=1))
29
30
 
30
31
  # Create a LinearAxis input (like what Clock produces)
31
32
  clock_tick = AxisArray.LinearAxis(gain=0.1, offset=0.0)
32
33
 
33
- result = transformer(clock_tick)
34
+ result = producer(clock_tick)
34
35
 
35
36
  # Check output shape
36
37
  assert result.data.shape == (1, 2), "Output should be (1, 2) for single sample with x, y"
@@ -59,11 +60,12 @@ class TestMousePollerTransformer:
59
60
  mock_controller.position = (500, 300)
60
61
  mock_controller_class.return_value = mock_controller
61
62
 
62
- transformer = MousePollerTransformer(MousePollerSettings())
63
+ # Use matching fs for simple mode
64
+ producer = MousePollerProducer(MousePollerSettings(fs=10.0, n_time=1))
63
65
 
64
66
  clock_tick = AxisArray.LinearAxis(gain=0.1, offset=0.0)
65
67
 
66
- result = transformer(clock_tick)
68
+ result = producer(clock_tick)
67
69
 
68
70
  # Check that values are finite numbers
69
71
  assert np.all(np.isfinite(result.data)), "Output should contain finite numbers"
@@ -75,11 +77,12 @@ class TestMousePollerTransformer:
75
77
  mock_controller.position = (0, 0)
76
78
  mock_controller_class.return_value = mock_controller
77
79
 
78
- transformer = MousePollerTransformer(MousePollerSettings())
80
+ # Use matching fs for simple mode
81
+ producer = MousePollerProducer(MousePollerSettings(fs=20.0, n_time=1))
79
82
 
80
83
  clock_tick = AxisArray.LinearAxis(gain=0.05, offset=1.5)
81
84
 
82
- result = transformer(clock_tick)
85
+ result = producer(clock_tick)
83
86
 
84
87
  # Check that time axis offset matches input
85
88
  time_axis = result.axes["time"]
@@ -87,12 +90,13 @@ class TestMousePollerTransformer:
87
90
  assert time_axis.offset == 1.5
88
91
 
89
92
  @patch("ezmsg.peripheraldevice.mouse.Controller")
90
- def test_multiple_calls(self, mock_controller_class):
91
- """Test that transformer works correctly across multiple calls."""
93
+ def test_multiple_calls_simple_mode(self, mock_controller_class):
94
+ """Test that producer works correctly across multiple calls in simple mode."""
92
95
  mock_controller = MagicMock()
93
96
  mock_controller_class.return_value = mock_controller
94
97
 
95
- transformer = MousePollerTransformer(MousePollerSettings())
98
+ # Use matching fs for simple mode (1/0.1 = 10 Hz)
99
+ producer = MousePollerProducer(MousePollerSettings(fs=10.0, n_time=1))
96
100
 
97
101
  for i in range(5):
98
102
  # Update mock position each call
@@ -100,7 +104,7 @@ class TestMousePollerTransformer:
100
104
 
101
105
  clock_tick = AxisArray.LinearAxis(gain=0.1, offset=i * 0.1)
102
106
 
103
- result = transformer(clock_tick)
107
+ result = producer(clock_tick)
104
108
 
105
109
  assert result.data.shape == (1, 2)
106
110
  assert np.all(np.isfinite(result.data))
@@ -109,6 +113,80 @@ class TestMousePollerTransformer:
109
113
  # Data should match mock position
110
114
  np.testing.assert_array_equal(result.data[0], [i * 10, i * 20])
111
115
 
116
+ @patch("ezmsg.peripheraldevice.mouse.Controller")
117
+ def test_threaded_mode_with_n_time_greater_than_1(self, mock_controller_class):
118
+ """Test that producer uses threaded mode when n_time > 1."""
119
+ mock_controller = MagicMock()
120
+ mock_controller.position = (100, 200)
121
+ mock_controller_class.return_value = mock_controller
122
+
123
+ # n_time > 1 triggers threaded mode
124
+ producer = MousePollerProducer(MousePollerSettings(fs=100.0, n_time=10))
125
+
126
+ clock_tick = AxisArray.LinearAxis(gain=0.1, offset=0.0)
127
+
128
+ # Give the thread a moment to poll
129
+ result = producer(clock_tick)
130
+ time_module.sleep(0.15) # Let thread poll a few times
131
+
132
+ result = producer(clock_tick)
133
+
134
+ # Should have n_time samples
135
+ assert result.data.shape == (10, 2)
136
+ assert np.all(np.isfinite(result.data))
137
+
138
+ # Clean up thread
139
+ del producer
140
+
141
+ @patch("ezmsg.peripheraldevice.mouse.Controller")
142
+ def test_threaded_mode_with_rate_mismatch(self, mock_controller_class):
143
+ """Test variable chunk mode (n_time=None) with fs != clock rate."""
144
+ mock_controller = MagicMock()
145
+ mock_controller.position = (50, 75)
146
+ mock_controller_class.return_value = mock_controller
147
+
148
+ # n_time=None, fs=100 != clock rate of 10 Hz triggers threaded mode
149
+ # n_samples = fs * clock.gain = 100 * 0.1 = 10 samples per tick
150
+ producer = MousePollerProducer(MousePollerSettings(fs=100.0, n_time=None))
151
+
152
+ clock_tick = AxisArray.LinearAxis(gain=0.1, offset=0.0)
153
+
154
+ # First call initializes, give thread time to poll
155
+ result = producer(clock_tick)
156
+ time_module.sleep(0.15) # Let thread poll ~15 times at 100 Hz
157
+
158
+ # Now get result with buffered data
159
+ result = producer(clock_tick)
160
+
161
+ # Should have n_samples = fs * gain = 100 * 0.1 = 10 samples
162
+ assert result.data.shape == (10, 2)
163
+ assert np.all(np.isfinite(result.data))
164
+
165
+ # Clean up thread
166
+ del producer
167
+
168
+ @patch("ezmsg.peripheraldevice.mouse.Controller")
169
+ def test_variable_chunk_mode_simple(self, mock_controller_class):
170
+ """Test variable chunk mode (n_time=None) with fs matching clock rate."""
171
+ mock_controller = MagicMock()
172
+ mock_controller.position = (123, 456)
173
+ mock_controller_class.return_value = mock_controller
174
+
175
+ # n_time=None, fs=10 matches clock rate of 10 Hz - no thread needed
176
+ # n_samples = fs * clock.gain = 10 * 0.1 = 1 sample per tick
177
+ producer = MousePollerProducer(MousePollerSettings(fs=10.0, n_time=None))
178
+
179
+ clock_tick = AxisArray.LinearAxis(gain=0.1, offset=0.0)
180
+
181
+ result = producer(clock_tick)
182
+
183
+ # Should have n_samples = 1 (simple mode, no thread)
184
+ assert result.data.shape == (1, 2)
185
+ np.testing.assert_array_equal(result.data[0], [123, 456])
186
+
187
+ # No thread should be running
188
+ assert producer._state.use_thread is False
189
+
112
190
 
113
191
  class TestMouseListenerProducer:
114
192
  """Tests for MouseListenerProducer."""
@@ -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