def test_local_instrument(self): # a local instrument should work in a foreground loop, but # not in a background loop (should give a RuntimeError) self.gates.close() # so we don't have two gates with same name gates_local = MockGates(model=self.model, server_name=None) self.gates = gates_local c1 = gates_local.chan1 loop_local = Loop(c1[1:5:1], 0.001).each(c1) # if spawn, pickle will happen if mp.get_start_method() == "spawn": with self.assertRaises(RuntimeError): loop_local.run(location=self.location, quiet=True, background=True) # allow for *nix # TODO(giulioungaretti) see what happens ? # what is the expected beavhiour ? # The RunimError will never be raised here, as the forkmethod # won't try to pickle anything at all. else: logging.error( "this should not be allowed, but for now we let it be") loop_local.run(location=self.location, quiet=True) data = loop_local.run(location=self.location2, background=False, quiet=True) self.check_loop_data(data)
def test_enqueue(self): c1 = self.gates.chan1 loop = Loop(c1[1:5:1], 0.01).each(c1) data1 = loop.run(location=self.location, quiet=True, background=True, data_manager=True) # second running of the loop should be enqueued, blocks until # the first one finishes. # TODO: check what it prints? data2 = loop.run(location=self.location2, quiet=True, background=True, data_manager=True) data1.sync() data2.sync() self.assertEqual(data1.gates_chan1.tolist(), [1, 2, 3, 4]) for v in data2.gates_chan1: self.assertTrue(np.isnan(v)) loop.process.join() data2.sync() self.assertEqual(data2.gates_chan1.tolist(), [1, 2, 3, 4]) # and while we're here, check that running a loop in the # foreground *after* the background clears its .process self.assertTrue(hasattr(loop, 'process')) loop.run_temp() self.assertFalse(hasattr(loop, 'process'))
def test_measurement_with_many_nans(self): loop = Loop(self.p1.sweep(0, 1, num=10), delay=0.05).each(self.p4_crazy) ds = loop.get_data_set() loop.run() # assert that both the snapshot and the datafile are there self.assertEqual(len(os.listdir(ds.location)), 2)
def test_halt(self): abort_after = 3 self.res = list(np.arange(0, abort_after-1, 1.)) [self.res.append(float('nan')) for i in range(0, abort_after-1)] p1 = AbortingGetter('p1', count=abort_after, vals=Numbers(-10, 10), set_cmd=None) loop = Loop(p1.sweep(0, abort_after, 1), 0.005).each(p1) # we want to test what's in data, so get it ahead of time # because loop.run will not return. data = loop.get_data_set(location=False) loop.run(quiet=True) self.assertEqual(repr(data.p1.tolist()), repr(self.res))
def testLoopCombinedParameterPrintTask(self, npoints, x_start_stop, y_start_stop, z_start_stop): x_set = np.linspace(x_start_stop[0], x_start_stop[1], npoints) y_set = np.linspace(y_start_stop[0], y_start_stop[1], npoints) z_set = np.linspace(z_start_stop[0], z_start_stop[1], npoints) setpoints = np.hstack( (x_set.reshape(npoints, 1), y_set.reshape(npoints, 1), z_set.reshape(npoints, 1))) parameters = [ Parameter(name, get_cmd=None, set_cmd=None) for name in ["X", "Y", "Z"] ] sweep_values = combine(*parameters, name="combined").sweep(setpoints) def ataskfunc(): a = 1 + 1 def btaskfunc(): b = 1 + 2 atask = Task(ataskfunc) btask = Task(btaskfunc) loop = Loop(sweep_values).each(atask, btask) data = loop.run(quiet=True) np.testing.assert_array_equal(data.arrays['X'].ndarray, x_set) np.testing.assert_array_equal(data.arrays['Y'].ndarray, y_set) np.testing.assert_array_equal(data.arrays['Z'].ndarray, z_set)
def testLoopCombinedParameterTwice(self, npoints, x_start_stop, y_start_stop, z_start_stop): x_set = np.linspace(x_start_stop[0], x_start_stop[1], npoints) y_set = np.linspace(y_start_stop[0], y_start_stop[1], npoints) z_set = np.linspace(z_start_stop[0], z_start_stop[1], npoints) setpoints = np.hstack( (x_set.reshape(npoints, 1), y_set.reshape(npoints, 1), z_set.reshape(npoints, 1))) parameters = [ Parameter(name, get_cmd=None, set_cmd=None) for name in ["X", "Y", "Z"] ] sweep_values = combine(*parameters, name="combined").sweep(setpoints) def wrapper(): counter = 0 def inner(): nonlocal counter counter += 1 return counter return inner self.dmm.voltage.get = wrapper() loop = Loop(sweep_values).each(self.dmm.voltage, self.dmm.voltage) data = loop.run(quiet=True) np.testing.assert_array_equal(data.arrays['X'].ndarray, x_set) np.testing.assert_array_equal(data.arrays['Y'].ndarray, y_set) np.testing.assert_array_equal(data.arrays['Z'].ndarray, z_set) np.testing.assert_array_equal(data.arrays['dmm_voltage_0'].ndarray, np.arange(1, npoints * 2, 2)) np.testing.assert_array_equal(data.arrays['dmm_voltage_1'].ndarray, np.arange(2, npoints * 2 + 1, 2))
def test_loop_measure_channels_by_name(dci, values): p1 = Parameter(name='p1', vals=Numbers(-10, 10), get_cmd=None, set_cmd=None) for i in range(4): dci.channels[i].temperature(values[i]) loc_fmt = 'data/{date}/#{counter}_{name}_{date}_{time}' rcd = {'name': 'channelsByName'} loc_provider = FormatLocation(fmt=loc_fmt, record=rcd) loop = Loop(p1.sweep(-10, 10, 1), 1e-6).each( dci.A.temperature, dci.B.temperature, dci.C.temperature, dci.D.temperature ) data = loop.run(location=loc_provider) assert data.p1_set.ndarray.shape == (21, ) for i, chan in enumerate(['A', 'B', 'C', 'D']): assert getattr( data, f'dci_Chan{chan}_temperature' ).ndarray.shape == (21,) assert getattr( data, f'dci_Chan{chan}_temperature' ).ndarray.max() == values[i] assert getattr( data, f'dci_Chan{chan}_temperature' ).ndarray.min() == values[i]
def testLoopCombinedParameterPrintTask(self, npoints, x_start_stop, y_start_stop, z_start_stop): x_set = np.linspace(x_start_stop[0], x_start_stop[1], npoints) y_set = np.linspace(y_start_stop[0], y_start_stop[1], npoints) z_set = np.linspace(z_start_stop[0], z_start_stop[1], npoints) setpoints = np.hstack((x_set.reshape(npoints, 1), y_set.reshape(npoints, 1), z_set.reshape(npoints, 1))) parameters = [Parameter(name, get_cmd=None, set_cmd=None) for name in ["X", "Y", "Z"]] sweep_values = combine(*parameters, name="combined").sweep(setpoints) def ataskfunc(): a = 1+1 def btaskfunc(): b = 1+2 atask = Task(ataskfunc) btask = Task(btaskfunc) loc_fmt = 'data/{date}/#{counter}_{name}_{date}_{time}' rcd = {'name': 'printTask'} loc_provider = FormatLocation(fmt=loc_fmt, record=rcd) loop = Loop(sweep_values).each(atask, btask) data = loop.run(location=loc_provider, quiet=True) np.testing.assert_array_equal(data.arrays['X'].ndarray, x_set) np.testing.assert_array_equal(data.arrays['Y'].ndarray, y_set) np.testing.assert_array_equal(data.arrays['Z'].ndarray, z_set)
def test_loop_measure_channels_by_name(self, values): p1 = Parameter(name='p1', vals=Numbers(-10, 10), get_cmd=None, set_cmd=None) for i in range(4): self.instrument.channels[i].temperature(values[i]) loc_fmt = 'data/{date}/#{counter}_{name}_{date}_{time}' rcd = {'name': 'channelsByName'} loc_provider = FormatLocation(fmt=loc_fmt, record=rcd) loop = Loop(p1.sweep(-10, 10, 1), 1e-6).each(self.instrument.A.temperature, self.instrument.B.temperature, self.instrument.C.temperature, self.instrument.D.temperature) data = loop.run(location=loc_provider) self.assertEqual(data.p1_set.ndarray.shape, (21, )) for i, chan in enumerate(['A', 'B', 'C', 'D']): self.assertEqual( getattr(data, 'testchanneldummy_Chan{}_temperature'.format( chan)).ndarray.shape, (21, )) self.assertEqual( getattr(data, 'testchanneldummy_Chan{}_temperature'.format( chan)).ndarray.max(), values[i]) self.assertEqual( getattr(data, 'testchanneldummy_Chan{}_temperature'.format( chan)).ndarray.min(), values[i])
def _QCoDeS_Loop(self, measured_parameter, sweeped_parameter, sweep_value=[0, 0, 0], **kw): Sweep_Values = sweeped_parameter[ sweep_value[0]:sweep_value[1]:sweep_value[2]] # Sweep_Values2 = sweeped_parameter2[sweep_value2[0]:sweep_value2[1]:sweep_value2[2]] LOOP = Loop(sweep_values=Sweep_Values).each(measured_parameter) data_set = LOOP.get_data_set( location=None, loc_record={ 'name': 'Chevron Pattern', 'label': 'frequency-burst_time' }, io=self.data_IO, ) data_set = LOOP.run() return data_set
def test_loop_measure_all_channels(self): p1 = Parameter(name='p1', vals=Numbers(-10, 10), get_cmd=None, set_cmd=None) loop = Loop(p1.sweep(-10, 10, 1), 1e-6).each(self.instrument.channels.temperature) data = loop.run() self.assertEqual(data.p1_set.ndarray.shape, (21, )) self.assertEqual(len(data.arrays), 7) for chan in ['A', 'B', 'C', 'D', 'E', 'F']: self.assertEqual(getattr(data, 'testchanneldummy_Chan{}_temperature'.format(chan)).ndarray.shape, (21,))
def test_halt(self): p1 = AbortingGetter('p1', count=2, vals=Numbers(-10, 10), msg=ActiveLoop.HALT_DEBUG) loop = Loop(p1[1:6:1], 0.005).each(p1) # we want to test what's in data, so get it ahead of time # because loop.run will not return. data = loop.get_data_set(location=False) p1.set_queue(loop.signal_queue) with self.assertRaises(_DebugInterrupt): # need to use explicit loop.run rather than run_temp # so we can avoid providing location=False twice, which # is an error. loop.run(background=False, data_manager=False, quiet=True) self.check_data(data)
def test_loop_simple(dci): loc_fmt = 'data/{date}/#{counter}_{name}_{date}_{time}' rcd = {'name': 'loopSimple'} loc_provider = FormatLocation(fmt=loc_fmt, record=rcd) loop = Loop(dci.channels[0].temperature.sweep(0, 300, 10), 0.001).each(dci.A.temperature) data = loop.run(location=loc_provider) assert_array_equal(data.dci_ChanA_temperature_set.ndarray, data.dci_ChanA_temperature.ndarray)
def test_loop_measure_channels_individually(self): p1 = ManualParameter(name='p1', vals=Numbers(-10, 10)) loop = Loop(p1.sweep(-10, 10, 1), 1e-6).each(self.instrument.channels[0].temperature, self.instrument.channels[1].temperature, self.instrument.channels[2].temperature, self.instrument.channels[3].temperature) data = loop.run() self.assertEqual(data.p1_set.ndarray.shape, (21, )) for chan in ['A', 'B', 'C', 'D']: self.assertEqual( getattr(data, 'testchanneldummy_Chan{}_temperature'.format( chan)).ndarray.shape, (21, ))
def test_loop_measure_channels_by_name(self, values): p1 = Parameter(name='p1', vals=Numbers(-10, 10), get_cmd=None, set_cmd=None) for i in range(4): self.instrument.channels[i].temperature(values[i]) loop = Loop(p1.sweep(-10, 10, 1), 1e-6).each(self.instrument.A.temperature, self.instrument.B.temperature, self.instrument.C.temperature, self.instrument.D.temperature) data = loop.run() self.assertEqual(data.p1_set.ndarray.shape, (21, )) for i, chan in enumerate(['A', 'B', 'C', 'D']): self.assertEqual(getattr(data, 'testchanneldummy_Chan{}_temperature'.format(chan)).ndarray.shape, (21,)) self.assertEqual(getattr(data, 'testchanneldummy_Chan{}_temperature'.format(chan)).ndarray.max(), values[i]) self.assertEqual(getattr(data, 'testchanneldummy_Chan{}_temperature'.format(chan)).ndarray.min(), values[i])
def test_loop_measure_all_channels(dci): p1 = Parameter(name='p1', vals=Numbers(-10, 10), get_cmd=None, set_cmd=None) loc_fmt = 'data/{date}/#{counter}_{name}_{date}_{time}' rcd = {'name': 'allChannels'} loc_provider = FormatLocation(fmt=loc_fmt, record=rcd) loop = Loop(p1.sweep(-10, 10, 1), 1e-6).\ each(dci.channels.temperature) data = loop.run(location=loc_provider) assert data.p1_set.ndarray.shape == (21, ) assert len(data.arrays) == 7 for chan in ['A', 'B', 'C', 'D', 'E', 'F']: assert getattr(data, f'dci_Chan{chan}_temperature').ndarray.shape == (21, )
def test_loop_measure_all_channels(self): p1 = Parameter(name='p1', vals=Numbers(-10, 10), get_cmd=None, set_cmd=None) loc_fmt = 'data/{date}/#{counter}_{name}_{date}_{time}' rcd = {'name': 'allChannels'} loc_provider = FormatLocation(fmt=loc_fmt, record=rcd) loop = Loop(p1.sweep(-10, 10, 1), 1e-6).\ each(self.instrument.channels.temperature) data = loop.run(location=loc_provider) self.assertEqual(data.p1_set.ndarray.shape, (21, )) self.assertEqual(len(data.arrays), 7) for chan in ['A', 'B', 'C', 'D', 'E', 'F']: self.assertEqual( getattr(data, 'testchanneldummy_Chan{}_temperature'.format( chan)).ndarray.shape, (21, ))
def test_loop_measure_channels_individually(dci): p1 = Parameter(name='p1', vals=Numbers(-10, 10), get_cmd=None, set_cmd=None) loc_fmt = 'data/{date}/#{counter}_{name}_{date}_{time}' rcd = {'name': 'channelsIndividually'} loc_provider = FormatLocation(fmt=loc_fmt, record=rcd) loop = Loop(p1.sweep(-10, 10, 1), 1e-6).each(dci.channels[0].temperature, dci.channels[1].temperature, dci.channels[2].temperature, dci.channels[3].temperature) data = loop.run(location=loc_provider) assert data.p1_set.ndarray.shape == (21, ) for chan in ['A', 'B', 'C', 'D']: assert getattr(data, f'dci_Chan{chan}_temperature').ndarray.shape == (21, )
def test_nested_loop_over_channels(self, loop_channels, measure_channel): channel_to_label = {0: 'A', 1: 'B', 2: 'C', 3: "D"} loc_fmt = 'data/{date}/#{counter}_{name}_{date}_{time}' rcd = {'name': 'nestedLoopOverChannels'} loc_provider = FormatLocation(fmt=loc_fmt, record=rcd) loop = Loop( self.instrument.channels[loop_channels[0]].temperature.sweep( 0, 10, 0.5)) loop = loop.loop( self.instrument.channels[loop_channels[1]].temperature.sweep( 50, 51, 0.1)) loop = loop.each(self.instrument.channels[measure_channel].temperature) data = loop.run(location=loc_provider) self.assertEqual( getattr( data, 'testchanneldummy_Chan{}_temperature_set'.format( channel_to_label[loop_channels[0]])).ndarray.shape, (21, )) self.assertEqual( getattr( data, 'testchanneldummy_Chan{}_temperature_set'.format( channel_to_label[loop_channels[1]])).ndarray.shape, ( 21, 11, )) self.assertEqual( getattr( data, 'testchanneldummy_Chan{}_temperature'.format( channel_to_label[measure_channel])).ndarray.shape, (21, 11)) assert_array_equal( getattr( data, 'testchanneldummy_Chan{}_temperature_set'.format( channel_to_label[loop_channels[0]])).ndarray, np.arange(0, 10.1, 0.5)) expected_array = np.repeat(np.arange(50, 51.01, 0.1).reshape(1, 11), 21, axis=0) array = getattr( data, 'testchanneldummy_Chan' '{}_temperature_set'.format( channel_to_label[loop_channels[1]])).ndarray assert_allclose(array, expected_array)
def test_nested_loop_over_channels(self, loop_channels, measure_channel): channel_to_label = {0: 'A', 1: 'B', 2: 'C', 3: "D"} loop = Loop( self.instrument.channels[loop_channels[0]].temperature.sweep( 0, 10, 0.5)) loop = loop.loop( self.instrument.channels[loop_channels[1]].temperature.sweep( 50, 51, 0.1)) loop = loop.each(self.instrument.channels[measure_channel].temperature) data = loop.run() self.assertEqual( getattr( data, 'testchanneldummy_Chan{}_temperature_set'.format( channel_to_label[loop_channels[0]])).ndarray.shape, (21, )) self.assertEqual( getattr( data, 'testchanneldummy_Chan{}_temperature_set'.format( channel_to_label[loop_channels[1]])).ndarray.shape, ( 21, 11, )) self.assertEqual( getattr( data, 'testchanneldummy_Chan{}_temperature'.format( channel_to_label[measure_channel])).ndarray.shape, (21, 11)) assert_array_equal( getattr( data, 'testchanneldummy_Chan{}_temperature_set'.format( channel_to_label[loop_channels[0]])).ndarray, np.arange(0, 10.1, 0.5)) expected_array = np.repeat(np.arange(50, 51.01, 0.1).reshape(1, 11), 21, axis=0) array = getattr( data, 'testchanneldummy_Chan' '{}_temperature_set'.format( channel_to_label[loop_channels[1]])).ndarray assert_allclose(array, expected_array)
def testLoopCombinedParameterAndMore(self, npoints, x_start_stop, y_start_stop, z_start_stop): x_set = np.linspace(x_start_stop[0], x_start_stop[1], npoints) y_set = np.linspace(y_start_stop[0], y_start_stop[1], npoints) z_set = np.linspace(z_start_stop[0], z_start_stop[1], npoints) setpoints = np.hstack( (x_set.reshape(npoints, 1), y_set.reshape(npoints, 1), z_set.reshape(npoints, 1))) parameters = [ Parameter(name, get_cmd=None, set_cmd=None) for name in ["X", "Y", "Z"] ] sweep_values = combine(*parameters, name="combined").sweep(setpoints) def wrapper(): counter = 0 def inner(): nonlocal counter counter += 1 return counter return inner self.dmm.voltage.get = wrapper() loc_fmt = 'data/{date}/#{counter}_{name}_{date}_{time}' rcd = {'name': 'parameterAndMore'} loc_provider = FormatLocation(fmt=loc_fmt, record=rcd) loop = Loop(sweep_values).each(self.dmm.voltage, self.dmm.somethingelse, self.dmm.voltage) data = loop.run(location=loc_provider, quiet=True) np.testing.assert_array_equal(data.arrays['X'].ndarray, x_set) np.testing.assert_array_equal(data.arrays['Y'].ndarray, y_set) np.testing.assert_array_equal(data.arrays['Z'].ndarray, z_set) np.testing.assert_array_equal(data.arrays['dmm_voltage_0'].ndarray, np.arange(1, npoints * 2, 2)) np.testing.assert_array_equal(data.arrays['dmm_somethingelse'].ndarray, np.ones(npoints)) np.testing.assert_array_equal(data.arrays['dmm_voltage_2'].ndarray, np.arange(2, npoints * 2 + 1, 2))
def test_loop_writing_2D(self): # pass station = Station() MockPar = MockParabola(name='Loop_writing_test_2D') station.add_component(MockPar) loop = Loop(MockPar.x[-100:100:20]).loop(MockPar.y[-50:50:10]).each( MockPar.skewed_parabola) data1 = loop.run(name='MockLoop_hdf5_test', formatter=self.formatter) data2 = DataSet(location=data1.location, formatter=self.formatter) data2.read() for key in data2.arrays.keys(): self.checkArraysEqual(data2.arrays[key], data1.arrays[key]) metadata_equal, err_msg = compare_dictionaries(data1.metadata, data2.metadata, 'original_metadata', 'loaded_metadata') self.assertTrue(metadata_equal, msg='\n' + err_msg) self.formatter.close_file(data1) self.formatter.close_file(data2)
def switch_sequence(seq_num, x): self.close() self.load_sequence(measurement='1') Count = StandardParameter(name='Count', set_cmd=self.function) Sweep_Count = Count[1:2:1] Loop = Loop(sweep_values=Sweep_Count).each(self.dig) DS = Loop.run() DSP = convert_to_probability(DS, 0.025, self.qubit_number, self.seq_repetition, 'cal', sweep_array=self.X_sweep_array) self.close() self.load_sequence(measurement='2') return True
def scan_outside_awg(name, set_parameter, measured_parameter, start, end, step): Sweep_Value = set_parameter[start:end:step] LOOP = Loop(sweep_values=Sweep_Value).each(measured_parameter) data_set = LOOP.get_data_set( location=None, loc_record={ 'name': name, 'label': 'Freq_sweep' }, io=data_IO, ) data_set = LOOP.run() awg.stop() awg2.stop() vsg.status('Off') vsg2.status('Off') return data_set
def test_loop_simple(self): loop = Loop(self.instrument.channels[0].temperature.sweep(0, 300, 10), 0.001).each(self.instrument.A.temperature) data = loop.run() assert_array_equal(data.testchanneldummy_ChanA_temperature_set.ndarray, data.testchanneldummy_ChanA_temperature.ndarray)
def _do_measurement(loop: Loop, set_params: tuple, meas_params: tuple, do_plots: Optional[bool]=True, use_threads: bool=True) -> Tuple[QtPlot, DataSet]: """ The function to handle all the auxiliary magic of the T10 users, e.g. their plotting specifications, the device image annotation etc. The input loop should just be a loop ready to run and perform the desired measurement. The two params tuple are used for plotting. Args: loop: The QCoDeS loop object describing the actual measurement set_params: tuple of tuples. Each tuple is of the form (param, start, stop) meas_params: tuple of parameters to measure do_plots: Whether to do a live plot use_threads: Whether to use threads to parallelise simultaneous measurements. If only one thing is being measured at the time in loop, this does nothing. Returns: (plot, data) """ try: parameters = [sp[0] for sp in set_params] + list(meas_params) _flush_buffers(*parameters) # startranges for _plot_setup try: startranges = {} for sp in set_params: minval = min(sp[1], sp[2]) maxval = max(sp[1], sp[2]) startranges[sp[0].full_name] = {'max': maxval, 'min': minval} except Exception: startranges = None interrupted = False data = loop.get_data_set() if do_plots: try: plot, _ = _plot_setup(data, meas_params, startranges=startranges) except (ClosedError, ConnectionError): log.warning('Remote process crashed png will not be saved') else: plot = None try: if do_plots: _ = loop.with_bg_task(plot.update).run(use_threads=use_threads) else: _ = loop.run(use_threads=use_threads) except KeyboardInterrupt: interrupted = True print("Measurement Interrupted") if do_plots: # Ensure the correct scaling before saving try: plot.autorange() plot.save() except (ClosedError, ConnectionError): log.warning('Remote process crashed png will not be saved') if 'pdf_subfolder' in CURRENT_EXPERIMENT or 'png_subfolder' in CURRENT_EXPERIMENT: _do_MatPlot(data,meas_params) if CURRENT_EXPERIMENT.get('device_image'): log.debug('Saving device image') save_device_image(tuple(sp[0] for sp in set_params)) # add the measurement ID to the logfile with open(CURRENT_EXPERIMENT['logfile'], 'a') as fid: print("#[QCoDeS]# Saved dataset to: {}".format(data.location), file=fid) if interrupted: raise KeyboardInterrupt except: log.exception("Exception in doND") raise return plot, data
def testLoopCombinedParameterInside(self, npoints, npoints_outer, x_start_stop, y_start_stop, z_start_stop): x_set = np.linspace(x_start_stop[0], x_start_stop[1], npoints_outer) y_set = np.linspace(y_start_stop[0], y_start_stop[1], npoints) z_set = np.linspace(z_start_stop[0], z_start_stop[1], npoints) setpoints = np.hstack((y_set.reshape(npoints, 1), z_set.reshape(npoints, 1))) parameters = [ Parameter(name, get_cmd=None, set_cmd=None) for name in ["X", "Y", "Z"] ] sweep_values = combine(parameters[1], parameters[2], name="combined").sweep(setpoints) def ataskfunc(): a = 1 + 1 def btaskfunc(): b = 1 + 2 atask = Task(ataskfunc) btask = Task(btaskfunc) def wrapper(): counter = 0 def inner(): nonlocal counter counter += 1 return counter return inner self.dmm.voltage.get = wrapper() loop = Loop( parameters[0].sweep(x_start_stop[0], x_start_stop[1], num=npoints_outer)).loop(sweep_values).each( self.dmm.voltage, atask, self.dmm.somethingelse, self.dmm.voltage, btask) data = loop.run(quiet=True) np.testing.assert_array_equal(data.arrays['X_set'].ndarray, x_set) np.testing.assert_array_equal( data.arrays['Y'].ndarray, np.repeat(y_set.reshape(1, npoints), npoints_outer, axis=0)) np.testing.assert_array_equal( data.arrays['Z'].ndarray, np.repeat(z_set.reshape(1, npoints), npoints_outer, axis=0)) np.testing.assert_array_equal( data.arrays['dmm_voltage_0'].ndarray, np.arange(1, npoints * npoints_outer * 2, 2).reshape(npoints_outer, npoints)) np.testing.assert_array_equal( data.arrays['dmm_voltage_3'].ndarray, np.arange(2, npoints * npoints_outer * 2 + 1, 2).reshape(npoints_outer, npoints)) np.testing.assert_array_equal(data.arrays['dmm_somethingelse'].ndarray, np.ones((npoints_outer, npoints)))
def _do_measurement(loop: Loop, set_params: tuple, meas_params: tuple, do_plots: Optional[bool]=True, use_threads: bool=True, auto_color_scale: Optional[bool]=None, cutoff_percentile: Optional[Union[Tuple[Number, Number], Number]]=None) -> Tuple[QtPlot, DataSet]: """ The function to handle all the auxiliary magic of the T10 users, e.g. their plotting specifications, the device image annotation etc. The input loop should just be a loop ready to run and perform the desired measurement. The two params tuple are used for plotting. Args: loop: The QCoDeS loop object describing the actual measurement set_params: tuple of tuples. Each tuple is of the form (param, start, stop) meas_params: tuple of parameters to measure do_plots: Whether to do a live plot use_threads: Whether to use threads to parallelise simultaneous measurements. If only one thing is being measured at the time in loop, this does nothing. auto_color_scale: if True, the colorscale of heatmap plots will be automatically adjusted to disregard outliers. cutoff_percentile: percentile of data that may maximally be clipped on both sides of the distribution. If given a tuple (a,b) the percentile limits will be a and 100-b. See also the plotting tuorial notebook. Returns: (plot, data) """ try: parameters = [sp[0] for sp in set_params] + list(meas_params) _flush_buffers(*parameters) # startranges for _plot_setup try: startranges = {} for sp in set_params: minval = min(sp[1], sp[2]) maxval = max(sp[1], sp[2]) startranges[sp[0].full_name] = {'max': maxval, 'min': minval} except Exception: startranges = None interrupted = False data = loop.get_data_set() if do_plots: try: plot, _ = _plot_setup(data, meas_params, startranges=startranges, auto_color_scale=auto_color_scale, cutoff_percentile=cutoff_percentile) except (ClosedError, ConnectionError): log.warning('Remote process crashed png will not be saved') # if remote process crashed continue without plots do_plots = False else: plot = None try: if do_plots: _ = loop.with_bg_task(plot.update).run(use_threads=use_threads) else: _ = loop.run(use_threads=use_threads) except KeyboardInterrupt: interrupted = True print("Measurement Interrupted") if do_plots: # Ensure the correct scaling before saving try: plot.autorange() plot.save() except (ClosedError, ConnectionError): log.warning('Remote process crashed png will not be saved') # if remote process crashed continue without plots do_plots = False if 'pdf_subfolder' in CURRENT_EXPERIMENT or 'png_subfolder' in CURRENT_EXPERIMENT: _do_MatPlot(data,meas_params, auto_color_scale=auto_color_scale, cutoff_percentile=cutoff_percentile) if CURRENT_EXPERIMENT.get('device_image'): log.debug('Saving device image') save_device_image(tuple(sp[0] for sp in set_params)) log.info("#[QCoDeS]# Saved dataset to: {}".format(data.location)) if interrupted: raise KeyboardInterrupt except: log.exception("Exception in doND") raise return plot, data
def _do_measurement(loop: Loop, set_params: tuple, meas_params: tuple, do_plots: bool = True, use_threads: bool = True) -> Tuple[QtPlot, DataSet]: """ The function to handle all the auxiliary magic of the T10 users, e.g. their plotting specifications, the device image annotation etc. The input loop should just be a loop ready to run and perform the desired measurement. The two params tuple are used for plotting. Args: loop: The QCoDeS loop object describing the actual measurement set_params: tuple of tuples. Each tuple is of the form (param, start, stop) meas_params: tuple of parameters to measure do_plots: Whether to do a live plot use_threads: Whether to use threads to parallelise simultaneous measurements. If only one thing is being measured at the time in loop, this does nothing. Returns: (plot, data) """ try: parameters = [sp[0] for sp in set_params] + list(meas_params) _flush_buffers(*parameters) # startranges for _plot_setup startranges = {} for sp in set_params: minval = min(sp[1], sp[2]) maxval = max(sp[1], sp[2]) startranges[sp[0].full_name] = {'max': maxval, 'min': minval} interrupted = False data = loop.get_data_set() if do_plots: try: plot, _ = _plot_setup(data, meas_params, startranges=startranges) except (ClosedError, ConnectionError): log.warning('Remote process crashed png will not be saved') else: plot = None try: if do_plots: _ = loop.with_bg_task(plot.update).run(use_threads=use_threads) else: _ = loop.run(use_threads=use_threads) except KeyboardInterrupt: interrupted = True print("Measurement Interrupted") if do_plots: # Ensure the correct scaling before saving try: plot.autorange() plot.save() except (ClosedError, ConnectionError): log.warning('Remote process crashed png will not be saved') plt.ioff() pdfplot, num_subplots = _plot_setup(data, meas_params, useQT=False) # pad a bit more to prevent overlap between # suptitle and title pdfplot.rescale_axis() pdfplot.fig.tight_layout(pad=3) title_list = plot.get_default_title().split(sep) title_list.insert(-1, CURRENT_EXPERIMENT['pdf_subfolder']) title = sep.join(title_list) pdfplot.save("{}.pdf".format(title)) if pdfdisplay['combined'] or (num_subplots == 1 and pdfdisplay['individual']): pdfplot.fig.canvas.draw() plt.show() else: plt.close(pdfplot.fig) if num_subplots > 1: _save_individual_plots(data, meas_params, pdfdisplay['individual']) plt.ion() if CURRENT_EXPERIMENT.get('device_image'): log.debug('Saving device image') save_device_image(tuple(sp[0] for sp in set_params)) # add the measurement ID to the logfile with open(CURRENT_EXPERIMENT['logfile'], 'a') as fid: print("#[QCoDeS]# Saved dataset to: {}".format(data.location), file=fid) if interrupted: raise KeyboardInterrupt except: log.exception("Exception in doND") raise return plot, data
class Measure(Metadatable): """ Create a DataSet from a single (non-looped) set of actions. Args: *actions (Any): sequence of actions to perform. Any action that is valid in a ``Loop`` can be used here. If an action is a gettable ``Parameter``, its output will be included in the DataSet. Scalars returned by an action will be saved as length-1 arrays, with a dummy setpoint for consistency with other DataSets. """ dummy_parameter = Parameter(name='single', label='Single Measurement', set_cmd=None, get_cmd=None) def __init__(self, *actions): super().__init__() self._dummyLoop = Loop(self.dummy_parameter[0]).each(*actions) def run_temp(self, **kwargs): """ Wrapper to run this measurement as a temporary data set """ return self.run(quiet=True, location=False, **kwargs) def get_data_set(self, *args, **kwargs): return self._dummyLoop.get_data_set(*args, **kwargs) def run(self, use_threads=False, quiet=False, station=None, **kwargs): """ Run the actions in this measurement and return their data as a DataSet Args: quiet (Optional[bool]): Set True to not print anything except errors. Default False. station (Optional[Station]): the ``Station`` this measurement pertains to. Defaults to ``Station.default`` if one is defined. Only used to supply metadata. use_threads (Optional[bool]): whether to parallelize ``get`` operations using threads. Default False. location (Optional[Union[str, bool]]): the location of the DataSet, a string whose meaning depends on formatter and io, or False to only keep in memory. May be a callable to provide automatic locations. If omitted, will use the default DataSet.location_provider name (Optional[str]): if location is default or another provider function, name is a string to add to location to make it more readable/meaningful to users formatter (Optional[Formatter]): knows how to read and write the file format. Default can be set in DataSet.default_formatter io (Optional[io_manager]): knows how to connect to the storage (disk vs cloud etc) location, name formatter and io are passed to ``data_set.new_data`` along with any other optional keyword arguments. returns: a DataSet object containing the results of the measurement """ data_set = self._dummyLoop.get_data_set(**kwargs) # set the DataSet to local for now so we don't save it, since # we're going to massage it afterward original_location = data_set.location data_set.location = False # run the measurement as if it were a Loop self._dummyLoop.run(use_threads=use_threads, station=station, quiet=True) # look for arrays that are unnecessarily nested, and un-nest them all_unnested = True for array in data_set.arrays.values(): if array.ndim == 1: if array.is_setpoint: dummy_setpoint = array else: # we've found a scalar - so keep the dummy setpoint all_unnested = False else: # The original return was an array, so take off the extra dim. # (This ensures the outer dim length was 1, otherwise this # will raise a ValueError.) array.ndarray.shape = array.ndarray.shape[1:] # TODO: DataArray.shape masks ndarray.shape, and a user *could* # change it, thinking they were reshaping the underlying array, # but this would a) not actually reach the ndarray right now, # and b) if it *did* and the array was reshaped, this array # would be out of sync with its setpoint arrays, so bad things # would happen. So we probably want some safeguards in place # against this array.shape = array.ndarray.shape array.set_arrays = array.set_arrays[1:] array.init_data() # Do we still need the dummy setpoint array at all? if all_unnested: del data_set.arrays[dummy_setpoint.array_id] if hasattr(data_set, 'action_id_map'): del data_set.action_id_map[dummy_setpoint.action_indices] # now put back in the DataSet location and save it data_set.location = original_location data_set.write() # metadata: ActiveLoop already provides station snapshot, but also # puts in a 'loop' section that we need to replace with 'measurement' # but we use the info from 'loop' to ensure consistency and avoid # duplication. LOOP_SNAPSHOT_KEYS = ['ts_start', 'ts_end', 'use_threads'] data_set.add_metadata({'measurement': { k: data_set.metadata['loop'][k] for k in LOOP_SNAPSHOT_KEYS }}) del data_set.metadata['loop'] # actions are included in self.snapshot() rather than in # LOOP_SNAPSHOT_KEYS because they are useful if someone just # wants a local snapshot of the Measure object data_set.add_metadata({'measurement': self.snapshot()}) data_set.save_metadata() if not quiet: print(repr(data_set)) print(datetime.now().strftime('acquired at %Y-%m-%d %H:%M:%S')) return data_set def snapshot_base(self, update: Optional[bool] = False, params_to_skip_update: Optional[Sequence[str]] = None): return { '__class__': full_class(self), 'actions': _actions_snapshot(self._dummyLoop.actions, update) }