pymeasure icon indicating copy to clipboard operation
pymeasure copied to clipboard

Run experiment in console/batch mode

Open msmttchr opened this issue 4 years ago • 12 comments

I think it can be extremely useful to be able to write script that can run both in GUI and console batch mode. The example provided in pymeasure shows that the same Procedure class can be run both in GUI mode and in console mode, but in console mode all the parameters have to be provided inside the script.

My idea would be to create a sort of ConsoleScript class which is the console version of ManagedWindow. ConsoleScript will take care of creating an argparse initialization to parse the users's input parameters via command line.

Does anyone thought about this ? Anyone who started such pull request ? Any other comment ?

msmttchr avatar Sep 12 '21 14:09 msmttchr

I haven't thought about this yet, but I can imagine that if you want to run an experiment from the console, you don't want to 'hardcode' the parameters in the script but want to be able to change them upon calling the experiment. I don't know of any such PRs, but maybe it would be a useful feature.

CasperSchippers avatar Sep 22 '21 15:09 CasperSchippers

Indeed, my idea was to make the parameters available as command line options to be able to configure them. Obviously the graphic part will not be available, but log messages will be available in the console and the same results files (.csv) will be generated as the regular experiment in GUI mode.

msmttchr avatar Sep 22 '21 15:09 msmttchr

Sounds like it could be useful indeed. One thing to keep in mind is to ensure that interrupting the measurement (e.g Keyboard Interrupt) works as an abort, and hence also runs the shutdown method of the procedure.

CasperSchippers avatar Oct 06 '21 12:10 CasperSchippers

I like the idea.:+1: These are two points which I would like o add to the discussion:

  1. instead/in parallel to offering the parameters as command line options also offer the possibility to use a "config"-file[^1]
  2. use something like progressbar to visualize the progress (if neccessary/possible/wanted)

[^1]: some easy flavor

LongnoseRob avatar Oct 06 '21 13:10 LongnoseRob

I like the idea.👍 These are two points which I would like o add to the discussion:

  1. instead/in parallel to offering the parameters as command line options also offer the possibility to use a "config"-file1
  2. use something like progressbar to visualize the progress (if neccessary/possible/wanted)

Footnotes

  1. some easy flavor

I think they are both good proposals, I would try to draft a PR which includes the features you suggested and hopefully get some feedback from you.

msmttchr avatar Oct 07 '21 08:10 msmttchr

Sounds like it could be useful indeed. One thing to keep in mind is to ensure that interrupting the measurement (e.g Keyboard Interrupt) works as an abort, and hence also runs the shutdown method of the procedure.

God hint, I will take this into account in a possible PR. Thanks

msmttchr avatar Oct 07 '21 08:10 msmttchr

@LongnoseRob, if you have time, could You please give a try to PR #500 ? It is still draft, but pretty much usable. I would be interested in your preliminary feedback

msmttchr avatar Oct 20 '21 11:10 msmttchr

@msmttchr I will try to test it. I have recently updated my desktop here and still have to do some things to get running (e.g. get GPIB running again) Bbut at the weekend I think it should be possible.

LongnoseRob avatar Oct 20 '21 13:10 LongnoseRob

@msmttchr Here are some first points:

  • I cloned your repo to a local forlder and switched to the add_console_mode branch
  • setup a venv and installed pymeasure from the folder above
  • changed to pymeasure/examples/Basic and ran python console.py:
Traceback (most recent call last):
  File "/home/robby/builds/temp/pymeasure/examples/Basic/console.py", line 16, in <module>
    import pyqtgraph as pg
  File "/home/robby/builds/temp/test-env/lib/python3.9/site-packages/pyqtgraph/__init__.py", line 13, in <module>
    from .Qt import QtCore, QtGui, mkQApp
  File "/home/robby/builds/temp/test-env/lib/python3.9/site-packages/pyqtgraph/Qt/__init__.py", line 48, in <module>
    raise Exception("PyQtGraph requires one of PyQt5, PyQt6, PySide2 or PySide6; none of these packages could be imported.")
Exception: PyQtGraph requires one of PyQt5, PyQt6, PySide2 or PySide6; none of these packages could be imported.
  • install PyQt5 with pip in the venv
  • ran console.py again:
ModuleNotFoundError: No module named 'progressbar'
  • install progressbar2 & try again: A QT window of the example pops up.
  • try again with python console.py --iterations 10: success:
08:34:28 PM: Created worker for procedure TestProcedure (pymeasure.display.console, INFO)
08:34:28 PM: Worker thread started (root, INFO)
08:34:28 PM: Worker started running an instance of 'TestProcedure' (root, INFO)
08:34:28 PM: Setting up random number generator (root, INFO)
08:34:28 PM: Starting to generate numbers (root, INFO)
08:34:30 PM: Finished (root, INFO)                                                                                           
08:34:30 PM: Monitor caught stop command (pymeasure.display.listeners, INFO)                                                 
Finished: 100% (100 of 100) |##########################################################| Elapsed Time: 0:00:02 Time:  0:00:02

From this, I would say it looks very promising. next steps could be:

  • [ ] add progressbar2 to the requirements for installation
  • [ ] discuss if we should always offer console mode if pyqt or pyside is not available (could also be done in a separate PR)
  • [ ] test with real hardware

LongnoseRob avatar Oct 23 '21 11:10 LongnoseRob

Thanks @LongnoseRob , your comments are really appreciated. Indeed, my original aim was to get rid of all Qt stuff, but this required deeper changes to the architecture. I think leaving Qt in could be ok, but if people find a way, as you said, a new PR can be done. For the dependency, I have updated the file pymeasure.yml, and the automatic build system did not complain. I have tried this with real hardware and looks good to me.

Have you tried some of the other command line options (e.g. to set parameter from log file)?

msmttchr avatar Oct 23 '21 13:10 msmttchr

Indeed, my original aim was to get rid of all Qt stuff, but this required deeper changes to the architecture. I think leaving Qt in could be ok, but if people find a way, as you said, a new PR can be done.

Exactly, I think offering a qt-less option micht be a bit beyond the scope of this PR/

Have you tried some of the other command line options (e.g. to set parameter from log file)?

not yet, I will try to do more of these tests with porting some of my measurement procedures to ths feature-set.

LongnoseRob avatar Oct 23 '21 14:10 LongnoseRob

It looks very promising indeed; I tried some things (not yet all) and most works quite well and looks promising

Have you tried some of the other command line options (e.g. to set parameter from log file)?

There is a small problem when using the --use-log-file option, see my remark in the PR.

I will continue testing and try to review asap

CasperSchippers avatar Nov 02 '21 20:11 CasperSchippers

Completed as PR #500 is merged. Thanks to all contributors and reviewers, especially @CasperSchippers, @LongnoseRob, @mcdo0486

msmttchr avatar Aug 12 '23 18:08 msmttchr