def setUp(self): self.instrument = DummyInstrument('dummy_holder') self.instrument.add_parameter( "a", set_cmd=None, get_cmd=None )
def __set_qcodes(self): initialise_or_create_database_at(self.__database) self.station = Station() #### instruments needs change # A dummy instrument dac with two parameters ch1 and ch2 self.dac = DummyInstrument('dac', gates=['amp']) # A dummy instrument dmm with two parameters ch1 and Ch2 self.dmm = DummyInstrument('dmm', gates=['v1']) #These are the parameters which come ready to use from the intruments drivers #dac.add_parameter('amp',label='Amplitude', unit="V", get_cmd=None, set_cmd=None) self.dac.add_parameter('freq', label='Frequency', unit="Hz", get_cmd=None, set_cmd=None) #puts current time in a string to facilitate control of the samples #makes the sample name now = datetime.now() now = now.strftime("%Y-%m-%d_%H-%M-%S") print(now) #the experiment is a unit of data inside the database it's made #out self.exp = load_or_create_experiment(experiment_name=self.__exp_name, sample_name=now) self.dmm.v1 = dmm_parameter('dmm_v1', self.dac)
class TestSetContextManager(TestCase): def setUp(self): self.instrument = DummyInstrument('dummy_holder') self.instrument.add_parameter( "a", set_cmd=None, get_cmd=None ) def tearDown(self): self.instrument.close() del self.instrument def test_none_value(self): with self.instrument.a.set_to(3): assert self.instrument.a.get() == 3 assert self.instrument.a.get() is None def test_context(self): self.instrument.a.set(2) with self.instrument.a.set_to(3): assert self.instrument.a.get() == 3 assert self.instrument.a.get() == 2
def setUp(self): self.instrument = DummyInstrument('dummy_holder') self.instrument.add_parameter("a", set_cmd=None, get_cmd=None) # These two parameters mock actual instrument parameters; when first # connecting to the instrument, they have the _latest["value"] None. # We must call get() on them to get a valid value that we can set # them to in the __exit__ method of the context manager self.instrument.add_parameter("validated_param", set_cmd=self._vp_setter, get_cmd=self._vp_getter, vals=vals.Enum("foo", "bar")) self.instrument.add_parameter("parsed_param", set_cmd=self._pp_setter, get_cmd=self._pp_getter, set_parser=int) # A parameter that counts the number of times it has been set self.instrument.add_parameter("counting_parameter", set_cmd=self._cp_setter, get_cmd=self._cp_getter) # the mocked instrument state values of validated_param and # parsed_param self._vp_value = "foo" self._pp_value = 42 # the counter value for counting_parameter self._cp_counter = 0 self._cp_get_counter = 0
def test_plot_by_id_line_and_heatmap(experiment): """ Test that line plots and heatmaps can be plotted together """ inst = DummyInstrument('dummy', gates=['s1', 'm1', 's2', 'm2']) inst.m1.get = np.random.randn inst.m2.get = lambda: np.random.randint(0, 5) meas = Measurement() meas.register_parameter(inst.s1) meas.register_parameter(inst.s2) meas.register_parameter(inst.m2, setpoints=(inst.s1, inst.s2)) meas.register_parameter(inst.m1, setpoints=(inst.s1, )) with meas.run() as datasaver: for outer in range(10): datasaver.add_result((inst.s1, outer), (inst.m1, inst.m1())) for inner in range(10): datasaver.add_result((inst.s1, outer), (inst.s2, inner), (inst.m2, inst.m2())) dataid = datasaver.run_id plot_by_id(dataid) plot_by_id(dataid, cmap='bone')
def _make_dummy_inst(): inst = DummyInstrument("dummy_dmm") inst.dac1.get = lambda: 0.001 inst.dac2.get = lambda: 1.4 try: yield inst finally: inst.close()
def test_get_instr(self): a = DummyInstrument('dummy_holder') d = DummyInstrument('dummy') a.add_parameter('test', parameter_class=InstrumentRefParameter) a.test.set(d.name) self.assertEqual(a.test.get(), d.name) self.assertEqual(a.test.get_instr(), d)
def inst(): """ Dummy instrument for testing, ensuring that it's instance gets closed and removed from the global register of instruments, which, if not done, make break other tests """ inst = DummyInstrument('inst', gates=['back', 'plunger', 'cutter']) yield inst inst.close()
def _make_inst_and_monitor(): instr = DummyInstrument("MonitorDummy") param = Parameter("DummyParam", unit="V", get_cmd=None, set_cmd=None) param(1) monitor_parameters = tuple(instr.parameters.values())[1:] my_monitor = monitor.Monitor(*monitor_parameters, param, interval=0.1) try: yield instr, my_monitor, monitor_parameters, param finally: my_monitor.stop() instr.close()
def setUp(self): """ Create a dummy instrument for use in monitor tests, and hook it into a monitor """ self.instr = DummyInstrument("MonitorDummy") self.param = Parameter("DummyParam", unit="V", get_cmd=None, set_cmd=None) self.param(1) self.monitor_parameters = tuple(self.instr.parameters.values())[1:] self.monitor = monitor.Monitor(*self.monitor_parameters, self.param, interval=0.1)
def test_add_component(): bob = DummyInstrument('bob', gates=['one']) station = Station() station.add_component(bob, 'bob') assert ['bob'] == list(station.components.keys()) assert bob == station.components['bob']
def test_station_getitem(): bob = DummyInstrument('bob', gates=['one']) station = Station(bob) assert bob == station['bob'] with pytest.raises(KeyError, match='bobby'): _ = station.components['bobby']
def SpectrumAnalyzer(): """ Yields a DummyInstrument that holds ArrayParameters returning different types """ class Spectrum(ArrayParameter): def __init__(self, name, instrument): super().__init__( name=name, shape=(1, ), # this attribute should be removed label='Flower Power Spectrum', unit='V/sqrt(Hz)', setpoint_names=('Frequency', ), setpoint_units=('Hz', )) self.npts = 100 self.start = 0 self.stop = 2e6 self._instrument = instrument def get_raw(self): # This is how it should be: the setpoints are generated at the # time we get. But that will of course not work with the old Loop self.setpoints = (tuple( np.linspace(self.start, self.stop, self.npts)), ) # not the best SA on the market; it just returns noise... return np.random.randn(self.npts) class ListSpectrum(Spectrum): def get_raw(self): output = super().get_raw() return list(output) class TupleSpectrum(Spectrum): def get_raw(self): output = super().get_raw() return tuple(output) SA = DummyInstrument('dummy_SA') SA.add_parameter('spectrum', parameter_class=Spectrum) SA.add_parameter('listspectrum', parameter_class=ListSpectrum) SA.add_parameter('tuplespectrum', parameter_class=TupleSpectrum) yield SA SA.close()
def test_snapshot(): station = Station() empty_snapshot = station.snapshot() assert { 'instruments': {}, 'parameters': {}, 'components': {}, 'config': None, } == empty_snapshot instrument = DummyInstrument('instrument', gates=['one']) station.add_component(instrument) instrument_snapshot = instrument.snapshot() parameter = Parameter('parameter', label='Label', unit='m') station.add_component(parameter) parameter_snapshot = parameter.snapshot() excluded_parameter = Parameter('excluded_parameter', snapshot_exclude=True) station.add_component(excluded_parameter) component = DumyPar('component') component.metadata['smth'] = 'in the way she moves' station.add_component(component) component_snapshot = component.snapshot() snapshot = station.snapshot() assert isinstance(snapshot, dict) assert [ 'instruments', 'parameters', 'components', 'config', ] == list(snapshot.keys()) assert ['instrument'] == list(snapshot['instruments'].keys()) assert instrument_snapshot == snapshot['instruments']['instrument'] # the list should not contain the excluded parameter assert ['parameter'] == list(snapshot['parameters'].keys()) assert parameter_snapshot == snapshot['parameters']['parameter'] assert ['component'] == list(snapshot['components'].keys()) assert component_snapshot == snapshot['components']['component']
def test_station(): bob = DummyInstrument('bob', gates=['one']) station = Station(bob) assert ['bob'] == list(station.components.keys()) assert bob == station.components['bob'] assert station == station.default assert station == Station.default
def test_snapshot(): station = Station() empty_snapshot = station.snapshot() assert {'instruments': {}, 'parameters': {}, 'components': {}, 'config': None, 'default_measurement': [] } == empty_snapshot instrument = DummyInstrument('instrument', gates=['one']) station.add_component(instrument) instrument_snapshot = instrument.snapshot() parameter = Parameter('parameter', label='Label', unit='m') station.add_component(parameter) parameter_snapshot = parameter.snapshot() component = DumyPar('component') component.metadata['smth'] = 'in the way she moves' station.add_component(component) component_snapshot = component.snapshot() snapshot = station.snapshot() assert isinstance(snapshot, dict) assert ['instruments', 'parameters', 'components', 'config', 'default_measurement' ] == list(snapshot.keys()) assert ['instrument'] == list(snapshot['instruments'].keys()) assert instrument_snapshot == snapshot['instruments']['instrument'] assert ['parameter'] == list(snapshot['parameters'].keys()) assert parameter_snapshot == snapshot['parameters']['parameter'] assert ['component'] == list(snapshot['components'].keys()) assert component_snapshot == snapshot['components']['component'] assert [] == snapshot['default_measurement']
def test_station_delegated_attributes(): bob = DummyInstrument('bob', gates=['one']) station = Station(bob) assert bob == station.bob with pytest.raises(AttributeError, match="'Station' object and its " "delegates have no attribute " "'bobby'"): _ = station.bobby
def test_station_after_instrument_is_closed(): """ Test that station is aware of the fact that its components could be removed within the lifetime of the station. Here we instantiate an instrument, add it to a station, then close the instrument, and then perform an action on the station to ensure that the closed instrument does not break the work of the station object. """ bob = DummyInstrument('bob', gates=['one']) station = Station(bob) assert bob == station['bob'] bob.close() # 'bob' is closed, but it is still part of the station assert bob == station['bob'] # check that snapshot method executes without exceptions snapshot = station.snapshot() # check that 'bob's snapshot is not here (because 'bob' is closed, # hence it was ignored, and even removed from the station by # `snapshot_base` method) assert { 'instruments': {}, 'parameters': {}, 'components': {}, 'config': None, 'default_measurement': [] } == snapshot # check that 'bob' has been removed from the station with pytest.raises(KeyError, match='bob'): _ = station.components['bob'] # check that 'bob' has been removed from the station, again with pytest.raises(KeyError, match='Component bob is not part of the ' 'station'): station.remove_component('bob')
class TestUnsafeThreading(TestCase): def setUp(self): self.inst1 = DummyInstrument(name='inst1', gates=['v1', 'v2']) self.inst2 = DummyInstrument(name='inst2', gates=['v1', 'v2']) def tearDown(self): self.inst1.close() self.inst2.close() del self.inst1 del self.inst2 gc.collect() def test_unsafe_exception(self): to_meas = (self.inst1.v1, self.inst1.v2) loop = Loop(self.inst2.v1.sweep(0, 1, num=10)).each(*to_meas) with self.assertRaises(UnsafeThreadingException): loop.run(use_threads=True)
def test_add_component_without_specifying_name(): """ Test that station looks for 'name' attribute in the component and uses it """ bob = DummyInstrument('bob', gates=['one']) assert hasattr(bob, 'name') assert 'bob' == bob.name station = Station() station.add_component(bob) assert ['bob'] == list(station.components.keys()) assert bob == station.components['bob']
class TestManualParameterValMapping(TestCase): def setUp(self): self.instrument = DummyInstrument('dummy_holder') def tearDown(self): self.instrument.close() del self.instrument def test_val_mapping(self): self.instrument.add_parameter('myparameter', set_cmd=None, get_cmd=None, val_mapping={'A': 0, 'B': 1}) self.instrument.myparameter('A') assert self.instrument.myparameter() == 'A' assert self.instrument.myparameter() == 'A' assert self.instrument.myparameter.raw_value == 0
def test_remove_component(): bob = DummyInstrument('bob', gates=['one']) station = Station() station.add_component(bob, 'bob') assert ['bob'] == list(station.components.keys()) assert bob == station.components['bob'] bob2 = station.remove_component('bob') with pytest.raises(KeyError, match='bob'): _ = station.components['bob'] assert bob == bob2 with pytest.raises(KeyError, match='Component bobby is not part of the ' 'station'): _ = station.remove_component('bobby')
class TestInstrumentRefParameter(TestCase): def setUp(self): self.a = DummyInstrument('dummy_holder') self.d = DummyInstrument('dummy') def test_get_instr(self): self.a.add_parameter('test', parameter_class=InstrumentRefParameter) self.a.test.set(self.d.name) self.assertEqual(self.a.test.get(), self.d.name) self.assertEqual(self.a.test.get_instr(), self.d) def tearDown(self): self.a.close() self.d.close() del self.a del self.d
import socket import threading import qcodes as qc import inspect from collections import defaultdict Pyro4.config.SERIALIZER = 'pickle' Pyro4.config.SERIALIZERS_ACCEPTED = ['json', 'marshal', 'serpent', 'pickle'] from qcodes import Instrument ## Add all the instrument you want to use to the station in the same way the DummyInstrument is added from qcodes.tests.instrument_mocks import DummyInstrument test_instrument = DummyInstrument('test_name') station = qc.Station() station.add_component(test_instrument) @Pyro4.expose class QcodesRemoteServer(object): def __init__(self): print('Starting Pyro4 server') self.locks = defaultdict(lambda: threading.Lock()) def ins_get(self, instrument_name: str, parameter: str): '''Get the value of a parameter inside the instrument''' with self.locks['instrument']: value = station.components[instrument_name].get(parameter)
def dac(): dac = DummyInstrument('dummy_dac', gates=['ch1', 'ch2']) yield dac dac.close()
def instrument_a(): a = DummyInstrument('dummy_holder') try: yield a finally: a.close()
def setUp(self): self.instrument = DummyInstrument('dummy_holder')
def setUp(self): self.a = DummyInstrument('dummy_holder') self.d = DummyInstrument('dummy')
def dmm(): dmm = DummyInstrument('dummy_dmm', gates=['v1', 'v2']) yield dmm dmm.close()
def instrument_d(): d = DummyInstrument('dummy') try: yield d finally: d.close()