Skip to content

First pass at #124 #126

Open
YagoDel wants to merge 22 commits intomasterfrom
issue124
Open

First pass at #124 #126
YagoDel wants to merge 22 commits intomasterfrom
issue124

Conversation

@YagoDel
Copy link
Contributor

@YagoDel YagoDel commented Oct 16, 2020

It solves my issue with the Andor (stops setting values in the Initialize method), but haven't tested it with other instruments and don't know if it's useful for anything else, so I'm not closing the #124 yet

@YagoDel YagoDel requested a review from eoinell October 16, 2020 01:11
@eoinell
Copy link
Contributor

eoinell commented Oct 18, 2020

yaml and pymsgbox aren't in the standard library, right? Would it be ok to use either stdlib file formats such as json or pickle, or our favourite .h5? And could the messagebox be done in plain pyqt? Just worried about introducing more dependencies, particularly for guis. Even tkinter is stdlib and has some simple msgbox options, even though it would be weird to mix pyqt and tkinter like that.

Since save_config is called through the console, can we just use a good old fashioned input()?

Really nice idea though, there are plenty of instruments that would benefit from this, I usually set default attributes in a meta 'lab' instrument which holds all the cooperative methods, and initialization functions for each instrument. This does extra work though for instruments with default parameters already.

@YagoDel
Copy link
Contributor Author

YagoDel commented Oct 18, 2020

I'm with you on the GUI dependencies. I'll see if I can figure out some simple PyQt ones (last time I tried I found it annoying to synchronise within the QtApp)

Really unsure what you mean with the input() comment

Pickle and HDF5 aren't human-readable, so I'm not a huge fan for config files. In terms of YAML vs JSON, I might have a quick go at how JSON handles general Python types. I think YAML more naturally handles things. For example, it uses indentation for hierarchy, which is neat for Python users (see the YAML I pushed to this branch). But I haven't played with JSON much and it might be good too.

@rwb27
Copy link
Contributor

rwb27 commented Oct 19, 2020

I've found JSON and YAML similar for basic types - YAML is slightly more pythonic in structure but both work fine. I don't think JSON has out of the box support for serialising python types, but TBH that's not so human readable in YAML either, so I tend to wrap things in conversion functions to make sure i only load/save simple types.

@YagoDel
Copy link
Contributor Author

YagoDel commented Oct 23, 2020

Got it working with JSON and PyQt. Not really sure there's much more to do in this PR.

Maybe integrating with the previous config code which seems to be used to save spectrometer/CCD background/references (and a couple of more things), but I'm gonna need someone to test it

@YagoDel
Copy link
Contributor Author

YagoDel commented Oct 28, 2020

Integrated with previous configuration code. I've tested it with my SeaBreeze spectrometer, and it seems the functionality is all good to go, but someone else testing it would be ideal

Added the possibility of saving all of an instruments parameters.
For super-basic stuff, it's possible to simply call
instrument.config_file = instrument.config to save and instrument.config = instrument.config_file to load

@YagoDel
Copy link
Contributor Author

YagoDel commented Feb 26, 2021

I am starting to need features I've developed in different branches, so I'm going to merge this next week

It would be nice to check that it doesn't break anything beforehand (I'm not sure who is currently actively working on nplab @eoinell), otherwise I'll just be active to help if it does break anything after

@eoinell
Copy link
Contributor

eoinell commented Feb 26, 2021 via email

@YagoDel
Copy link
Contributor Author

YagoDel commented Feb 26, 2021 via email

"""Create an instrument object."""
super(Instrument, self).__init__()
Instrument.instances_set().add(self) #keep track of instances (should this be in __new__?)
Instrument.instances_set().add(self) # keep track of instances (should this be in __new__?)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think using new is necessary

"""
instances = cls.get_instances()
if len(instances)>0:
if len(instances) > 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if instances:

@@ -0,0 +1 @@
{"ReadMode": 4, "AcquisitionMode": 1, "TriggerMode": 0, "Exposure": 0.01, "Shutter": [1, 0, 1, 1], "SetTemperature": -90, "CoolerMode": 0, "FanMode": 0, "cooler": 0} No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can exposure be 1s? most common value in our labs for Si referencing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'm not using the default_config anyway. In that case, do you know what your shutter speeds are?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, so probably setting it to (1,0,0,0) is most sensible..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that'll fail for most shutters. Which could be good, since it'll force people to actively set their own configurations), but could also be a pain in the ass

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, nvm, it seems that's what master does now anyway, so yeah Shutter=(1, 0, 0, 0) it is

"""

from builtins import str
from nplab.utils.thread_utils import locked_action_decorator, background_action_decorator
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

locked_action and background_action are nicer aliases IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could agree, not an issue for this branch :P (it's been like that since the start of nplab)

@YagoDel
Copy link
Contributor Author

YagoDel commented Mar 4, 2021

Do the above comments mean you have tested this branch?

@eoinell
Copy link
Contributor

eoinell commented Mar 22, 2021

I just mean use the console for input instead of a pyqt popup if it's easier - but you have this working now so nvm :)

Just tried it now (sorry for the delay, tbh I prefer to let the rest of nplab do a bit of involuntary testing once i'm pretty sure it'll work)
`
File "C:\Users\hera\Documents\GitHub\nplab\nplab\instrument_init_.py", line 315, in load_config
with open(filename, 'r') as config_file:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\hera\Documents\GitHub\nplab\nplab\instrument\spectrometer\default_config.json'`

This is for the andor - I guess it needs to handle the case where there isn't a default_config file?

Sorry this is a bit late in the game, but I can't help but feel that this would be better placed in the Instrument, where it would check for the existence of a default config file, and if it finds one attempt to set all the parameters in it. Otherwise it just acts as normal.

@eoinell
Copy link
Contributor

eoinell commented Mar 22, 2021

If these config files can't be implemented without having to change the respective instruments' code then they'll just become one more thing in the instrument base class that nobody knows how or why to use. Ideally calling instr.save_config() should be all that's needed to get going. This save call should also try to set all parameters, and remove any that fail. The properties to be set could be integrated with the metadata_property_names system possibly, but this is probably unnecessary.

@YagoDel
Copy link
Contributor Author

YagoDel commented Mar 22, 2021

This is useful stuff, thanks!

Sorry this is a bit late in the game, but I can't help but feel that this would be better placed in the Instrument, where it would check for the existence of a default config file, and if it finds one attempt to set all the parameters in it. Otherwise it just acts as normal.

So the config stuff all happesn in the Instrument base (same as it was happening before, but before it was only using HDF5, and I don't think that many instruments were using it)

Just tried it now (sorry for the delay, tbh I prefer to let the rest of nplab do a bit of involuntary testing once i'm pretty sure it'll work)
`
File "C:\Users\hera\Documents\GitHub\nplab\nplab\instrument__init__.py", line 315, in load_config
with open(filename, 'r') as config_file:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\hera\Documents\GitHub\nplab\nplab\instrument\spectrometer\default_config.json'`

This is for the andor - I guess it needs to handle the case where there isn't a default_config file?

I'll have a look to fix the issue of it failing (it should be simple enough). You got that error just from trying to create an Andor instance? It feels like a Trandor or Kandor instance

@YagoDel
Copy link
Contributor Author

YagoDel commented Mar 22, 2021

I very much appreciate the want for a tool that can be used by all users. I'm not sure I understand how an instrument instance can know what it's own "parameters" are though.

So specifically, when you say "set all parameters, and remove any that fails", I'm not sure how to do it, other than manually giving it a list of names (which in this case was config_property_names, in parallel with metadata_property_names, but I'm happy to look into either merging them into a single one, or having a hierarchy)

@eoinell
Copy link
Contributor

eoinell commented Mar 22, 2021

Hmm, yeah.. maybe a tuple of all settable parameter names similar to metadata_property_names but this requires some extra code. In camera.thorlabs.kiralux I implemented something where it would just add everything that has a getter and setter method, and remove any that fail but this may be a bit too general. You could explicitly filter properties that come from instrument..
Basically I'm pretty sure this will work but you may end up with too much stuff (like live_view in Camera for example) being in the config file.

A shamdor instance, yep. I guess there's a bit of a distinction between settings parameters (JSON is perfect here, and this should happen on every instantiation) and configs like background/reference spectra which are arguably better rendered with the h5 renderer and should be optionally loaded.

@eoinell
Copy link
Contributor

eoinell commented Mar 22, 2021 via email

@YagoDel
Copy link
Contributor Author

YagoDel commented Mar 22, 2021

Oh, I'll have a look at the kiralux thing, but you're probably right we'd end up with tons of stuff.

I guess I hadn't realised people were actually using the HDF5 config code (that is currently in the Instrument base) for spectrometer, but I had tried to keep it compatible. I guess it breaks down if it tries to do both things (read configuration parameters from a JSON, and config for a spectrometer)

Do you know if the current config code is used for anything else?

@eoinell
Copy link
Contributor

eoinell commented Mar 22, 2021 via email

@YagoDel
Copy link
Contributor Author

YagoDel commented Mar 28, 2021

Ok, so three things:

  • Can't find the <camera.thorlabs.kiralux> you mentioned
  • The reason opening a Shamdor failed for you is that it doesn't know where to look for the default parameters. The way I wrote the code is that if the specified path isn't absolute, it tries to make an absolute path from the location of the class. Since the Andor and the Shamdor are in different directories, it fails. I've corrected it in the latest commit, by forcing the Andor to always look in the Andor directory if a config_file path is not given on instantiation. So it's fixed, have a test again.

And finally, following what you said:

I guess there's a bit of a distinction between settings parameters (JSON is perfect here, and this should happen on every instantiation) and configs like background/reference spectra which are arguably better rendered with the h5 renderer and should be optionally loaded.

I foresee one possible issue. I tried to make the config code backwards compatible, so that instruments could either save to HDF5 or JSON and the rest of nplab would continue working as it was. But if we create an instrument that is a subclass of an instrument that does config in HDF5 and an instrument that does a config in JSON (for example a camera_with_location and and Andor), then I'm fairly certain it'll fail.The simplest fix is to just make camera_with_location save that value to a JSON instead.

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.

3 participants