toolsforexperiments / plottr

A flexible plotting and data analysis tool.
https://github.com/toolsforexperiments/plottr
MIT License
46 stars 54 forks source link

Qcodes Autoplot crash with categorical data #78

Open mgunyho opened 4 years ago

mgunyho commented 4 years ago

Using a dataset created with the following code:

import numpy as np
import qcodes as qc

qc.config.core.db_location = "./test.db"
qc.initialise_database()
exp = qc.load_or_create_experiment("categorical_data", sample_name="no sample")

m = qc.Measurement(exp=exp)
m.register_custom_parameter('x')
m.register_custom_parameter('label', paramtype='text')
m.register_custom_parameter('y', setpoints=('x', 'label'))

def f(x, label):
    if label == 'trig':
        return np.sin(x * 2 * np.pi)
    elif label == 'gauss':
        return np.exp(-x**2)

with m.run() as datasaver:
    for x in np.linspace(-1, 1, 10):
        for label in ['trig', 'gauss']:
            datasaver.add_result(('x', x),
                                 ('label', label),
                                 ('y', f(x, label)))

When opening the dataset with Autoplot, it crashes with the following stack trace:

Traceback (most recent call last):
  File "plottr/plottr/apps/inspectr.py", line 425, in plotRun
    fc, win = autoplotQcodesDataset(pathAndId=(self.filepath, runId))
  File "plottr/plottr/apps/autoplot.py", line 237, in autoplotQcodesDataset
    win = QCAutoPlotMainWindow(fc, pathAndId=pathAndId)
  File "plottr/plottr/apps/autoplot.py", line 213, in __init__
    self.setDefaults()
  File "plottr/plottr/apps/autoplot.py", line 182, in setDefaults
    self.fc.nodes()['Dimension assignment'].dimensionRoles = drs
  File "plottr/plottr/node/node.py", line 40, in wrap
    self.ui.optSetters[optName](val)
  File "plottr/plottr/node/dim_reducer.py", line 627, in setRoles
    self.widget.setRole(dimName, role)
  File "plottr/plottr/node/dim_reducer.py", line 336, in setRole
    super().setRole(dim, role, **kw)
  File "plottr/plottr/node/dim_reducer.py", line 276, in setRole
    super().setRole(dim, role)
  File "plottr/plottr/node/dim_reducer.py", line 202, in setRole
    self.choices[dim]['roleSelectionWidget'].setCurrentText(role)
KeyError: 'label'

The (or at least one) correct behavior would be to add the categorical dimension as a slider, which works if the data has at least one 'numerical' dimension (if not, then I'm not sure what should happen).

wpfff commented 4 years ago

Very interesting case -- i really hadn't considered non-numerical data. i'll think about how to solve it with a reasonable level of generality. (might be not be quick, though -- to me it looks like this needs some work already on the data backend)