labscript-suite / blacs

๐—ฏ๐—น๐—ฎ๐—ฐ๐˜€ supervises the execution of experiments controlled by the ๐˜ญ๐˜ข๐˜ฃ๐˜ด๐˜ค๐˜ณ๐˜ช๐˜ฑ๐˜ต ๐˜ด๐˜ถ๐˜ช๐˜ต๐˜ฆ. It manages experiment queuing and hardware-timed execution, and provides manual control over devices between shots.
http://labscriptsuite.org
Other
4 stars 48 forks source link

Add plugin lock monitor #88

Open zakv opened 3 years ago

zakv commented 3 years ago

This PR would add a plugin called "lock monitor" to blacs. Since plugins are able to be distributed separately from blacs, adding this to the official blacs repo definitely isn't necessary. However since laser locks are ubiquitous among labscript users I thought I'd see what you all thought about including it with blacs? Including it might be particularly helpful for folks who don't have a developer install of blacs, as I'm not sure if they can easily add plugins.

image

A lot of details about the plugin are provided in the readme. Briefly the plugin is designed to call user-written code to check if lasers are in lock and optionally call user-written code to lock a laser when it is found to be out of lock. The user-written code is called during some of the plugin callbacks in blacs.experiment_queue.QueueManager.manage(). The plugin itself sandboxes user code in worker processes and handles aborting and re-queueing shots when lasers go out of lock. It also pauses the shot queue when a laser cannot be locked. Controls for lockers are provided in a blacs plugin tab which also includes an output box for displaying log messages from the lockers. A settings tab is provided for adding/removing lockers.

A readme is provided, as well as base classes for the locker classes that the user must write. Dummy locker classes are also provided. They don't control any real hardware but they do demonstrate how to write locker classes and can be used with lock monitor like real lockers.

Note that running this plugin requires https://github.com/labscript-suite/blacs/pull/87 due to https://github.com/labscript-suite/blacs/issues/86. Also, thanks to @philipstarkey for pointing me to some good references for writing blacs plugins!

zakv commented 3 years ago

A few questions and points maybe worth mentioning...

For reference, the error raised when using setup_logging() in a jupyter notebook (or importing a module that uses setup_logging() into a jupyter notebook) is included below. It has something to do with the way jupyter handles stdout and stderr.

Jupyter Error ```python UnsupportedOperation Traceback (most recent call last) in 1 from labscript_utils.setup_logging import setup_logging 2 ----> 3 setup_logging('jupyter_test') c:\users\username\software\labscript\labscript-utils\labscript_utils\setup_logging.py in setup_logging(program_name, log_level, terminal_level, maxBytes, backupCount) 53 handler.setLevel(log_level) 54 logger.addHandler(handler) ---> 55 if sys.stdout is not None and sys.stdout.fileno() >= 0: 56 stdout_handler = logging.StreamHandler(sys.stdout) 57 stdout_handler.setFormatter(formatter) UnsupportedOperation: fileno ```
matth2peters commented 1 year ago

I'm having an issue with this code. It seems to work for the DummyLocker except for when I shut down blacs. I then get the error: Traceback (most recent call last): File "c:\users\matth\labscript-suite\python_38\lib\site-packages\qtutils\invoke_in_main.py", line 46, in event result = event.fn(*event.args, **event.kwargs) File "c:\users\matth\labscript-suite\python_38\custom_python_packages\zak_blacs\blacs\blacs\__main__.py", line 605, in on_save_exit tab.shutdown_workers() File "c:\users\matth\labscript-suite\python_38\custom_python_packages\zak_blacs\blacs\blacs\tab_base_classes.py", line 212, in f self.event_queue.put(allowed_modes,queue_state_indefinitely,delete_stale_states,[function,[args,kwargs]]) AttributeError: 'LockMonitorTab' object has no attribute 'event_queue'

It seems like in the tab_base_classes.py file the PluginTab class (which LockMonitorTab inherits from) has no shutdown_workers method. Should PluginTab inherit from the Tab class, or is there something else that I'm missing?

zakv commented 1 year ago

It seems like in the tab_base_classes.py file the PluginTab class (which LockMonitorTab inherits from) has no shutdown_workers method. Should PluginTab inherit from the Tab class, or is there something else that I'm missing?

I did run into an issue like that (https://github.com/labscript-suite/blacs/issues/86) when developing this plugin, and made a separate PR (https://github.com/labscript-suite/blacs/pull/87) to fix it. I don't think that the branch for this PR includes the fix from that other PR though, which may be why you're seeing this issue. Could you try rebasing this branch on master in your local copy of the blacs repo? If that does work, then I can update this PR.

That said, the traceback you provided looks slightly different than I would expect if the issue were just that the LockMontiorTab class is missing a shutdown_workers() method though (seems like it's missing an event_queue attribute). So I'm not 100% sure this will fix it, but fingers crossed

matth2peters commented 1 year ago

That fixed it, thanks!

zakv commented 1 year ago

That fixed it, thanks!

Perfect! Thanks for checking, I just updated this PR by rebasing its branch on master