def test_snapshot(): from qcodes.instrument.base import InstrumentBase # We need to use `InstrumentBase` (not a bare mock) in order for # `snapshot` methods call resolution to work out mainframe = InstrumentBase(name='mainframe') mainframe.write = MagicMock() slot_nr = 1 smu = B1517A(parent=mainframe, name='B1517A', slot_nr=slot_nr) smu.use_high_speed_adc() smu.source_config(output_range=VOutputRange.AUTO) smu.i_measure_range_config(i_measure_range=IMeasRange.AUTO) smu.v_measure_range_config(v_measure_range=VMeasRange.AUTO) smu.timing_parameters(0.0, 0.123, 321) s = smu.snapshot() assert '_source_config' in s assert 'output_range' in s['_source_config'] assert isinstance(s['_source_config']['output_range'], VOutputRange) assert '_measure_config' in s assert 'v_measure_range' in s['_measure_config'] assert 'i_measure_range' in s['_measure_config'] assert isinstance(s['_measure_config']['v_measure_range'], VMeasRange) assert isinstance(s['_measure_config']['i_measure_range'], IMeasRange) assert '_timing_parameters' in s assert 'number' in s['_timing_parameters'] assert isinstance(s['_timing_parameters']['number'], int)
def add_parameter(self, name: str, parameter_class: type = Parameter, **kwargs: Any) -> None: existing_parameter = self.parameters.get(name, None) if isinstance(existing_parameter, AbstractParameter): # For abstract parameters, we define special behavior. existing_unit = getattr(existing_parameter, "unit", None) new_unit = kwargs.get("unit", None) if existing_unit and existing_unit != new_unit: raise AbstractParameterException( f"The unit of the parameter '{name}' is '{new_unit}', " f"which is inconsistent with the unit '{existing_unit}' " f"specified earlier. This is usually because a driver " f"is a subclass of a baseclass which defines a parameter " f"of the same name but with different units") param = parameter_class(name=name, instrument=self, **kwargs) self.parameters[name] = param else: # If it is a usual parameter, call the super class InstrumentBase.add_parameter(self, name, parameter_class, **kwargs)
def add_parameter_from_dict(instr: InstrumentBase, name: str, options: Dict[str, Any]) -> None: # keep the original dictionray intact for snapshot options = copy(options) param_type: type = _BaseParameter kwargs = {} if 'source' in options: param_type = DelegateParameter kwargs['source'] = resolve_parameter_identifier( instr.root_instrument, options['source']) options.pop('source') instr.add_parameter(name, param_type, **kwargs) setup_parameter_from_dict(instr.parameters[name], options)
def test_snapshot_and_meta_attrs(self): """Test snapshot of InstrumentBase contains _meta_attrs attributes""" instr = InstrumentBase('instr') self.assertEqual(instr.name, 'instr') self.assertTrue(hasattr(instr, '_meta_attrs')) self.assertListEqual(instr._meta_attrs, ['name']) snapshot = instr.snapshot() self.assertIn('name', snapshot) self.assertEqual('instr', snapshot['name']) self.assertIn('__class__', snapshot) self.assertIn('InstrumentBase', snapshot['__class__'])
def _singleInstrumentParametersToJson(instrument: InstrumentBase, get: bool = False, addPrefix: str = '', includeMeta: List[str] = [], excludeParameters: List[str] = [], simpleFormat: bool = True) -> Dict: """Create a dictionary that holds the parameters of an instrument.""" if "IDN" not in excludeParameters: excludeParameters.append("IDN") ret = {} snap = instrument.snapshot(update=get) for name, param in instrument.parameters.items(): if name not in excludeParameters: if len(includeMeta) == 0 and simpleFormat: ret[addPrefix + name] = snap['parameters'][name].get('value', None) else: ret[addPrefix + name] = dict() for k, v in snap['parameters'][name].items(): if k in (['value'] + includeMeta): ret[addPrefix + name][k] = v else: logger.debug(f"excluded: {addPrefix + name}") for name, submod in instrument.submodules.items(): ret.update(_singleInstrumentParametersToJson( submod, get=get, addPrefix=f"{addPrefix + name}.", simpleFormat=simpleFormat, includeMeta=includeMeta)) return ret
def test_snapshot_and_meta_attrs(): """Test snapshot of InstrumentBase contains _meta_attrs attributes""" instr = InstrumentBase('instr') assert instr.name == 'instr' assert hasattr(instr, '_meta_attrs') assert instr._meta_attrs == ['name'] snapshot = instr.snapshot() assert 'name' in snapshot assert 'instr' == snapshot['name'] assert '__class__' in snapshot assert 'InstrumentBase' in snapshot['__class__']
def test_instrumentbase_metadata(): metadatadict = {1: "data", "some": "data"} instrument = InstrumentBase('instr', metadata=metadatadict) assert instrument.metadata == metadatadict