Skip to content

Enhance Teensy serial handling and refactor code for clarity#330

Open
CeliaBenquet wants to merge 27 commits into
celia/register-processorsfrom
celia/fix-teensy-race
Open

Enhance Teensy serial handling and refactor code for clarity#330
CeliaBenquet wants to merge 27 commits into
celia/register-processorsfrom
celia/fix-teensy-race

Conversation

@CeliaBenquet

@CeliaBenquet CeliaBenquet commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

This pull request adds a new developer-facing documentation page that explains the experiment session lifecycle, and makes a series of style and clarity improvements to the DeepLabCut processor code. The documentation addition provides a clear, step-by-step overview of how the two main processes interact during a session, while the code changes focus on improved logging, exception handling, and minor refactoring for readability.

Race-condition fixes

TeensyLatency.close_serial():

  • added timeout=0.5 to the serial.Serial(...) constructor so readline() can never block indefinitely, and close_serial() now joins the reader thread before closing the port. Also done in Fix thread crash in TeensyLatency + prevent data-loss for dlc_inference_w_pd_sync #329.
  • same fix applied to Teensy.py (the main rig Teensy, not just the photodiode one) and to the two DLC-position socket clients, dlc_socket.py/dlc_deque_socket.py (conn.close() racing a blocked recv()), since they're the same failure shape.
    ActiveSensingTask.stop() now closes its dlcClient (and joins its thread) so the position-reading socket/thread don't leak across repeated task start/stop cycles in one GUI process.

Documentation Improvements:

  • Added a new page, docs/software_package/experiment_lifecycle.md, detailing the session lifecycle, process responsibilities, data flow, and failure handling for developers. This is now included in the table of contents (_toc.yml).

vr4mice GUI save/close safety (teensy_experiment.py)

  • _dump_data(): save failures now show an error dialog instead of an uncaught traceback.
  • Fixed a latent bug: check_close()'s "did you save?" prompt was a silent no-op once saved_ok was already True — the Close button did nothing in that case.
  • The window's [X] button and Ctrl+C in the terminal now route through the same save-check (previously both bypassed it entirely and skipped Teensy cleanup on interrupt).
  • New "unsaved data" warning on clicking "Ready" (which replaces self.task, discarding the old task's in-memory data) — not a hard requirement, since not saving can be intentional (test/aborted runs).
  • Fixed another latent bug found while adding that: self.saved_ok was never reset to False after a new task was created, so after the first save in a GUI session, every subsequent unsaved-data warning silently stopped firing.

CeliaBenquet and others added 26 commits July 1, 2026 16:51
Wrap processor initialization in a guarded try/except so partial startup failures trigger cleanup and re-raise as a clear RuntimeError. Add a dedicated stop() method (plus close() alias) to optionally save output and reliably release Teensy, socket connection, and listener resources, reducing leaked serial handles/ports after errors or shutdown.
Implement a dedicated `save()` method in `dlc_inference_w_pd_sync` that persists latency data to a pickle file, supports an optional explicit path, and falls back to `self.save_path` when no file is passed. The change also initializes `save_path` on construction, ensures parent directories are created, and adds warning-based error handling for missing paths or save failures.
Replaced `import datetime` with `from datetime import datetime` in `dlc_processor_socket_pd_sync.py` to align the import with direct `datetime` usage and avoid module/class ambiguity.
Add a `_video_prefix()` helper that derives the base name from discovered video files, falling back to `filename_stem` and then `recording`. Update `_db_compat_base()` to use this prefix instead of the mouse identifier when building the output path, improving alignment with recording/video naming.
Signed-off-by: Célia Benquet <32598028+CeliaBenquet@users.noreply.github.com>
Base automatically changed from celia/autocomplete-cameras to celia/register-processors July 24, 2026 11:04
Signed-off-by: Célia Benquet <32598028+CeliaBenquet@users.noreply.github.com>
@CeliaBenquet CeliaBenquet self-assigned this Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants