def testInvalidName(parameters): x_vals = np.linspace(1, 1, 2) y_vals = np.linspace(1, 1, 2) z_vals = np.linspace(1, 1, 2) with pytest.raises(ValueError): combine(*parameters, name="combined with spaces").sweep(x_vals, y_vals, z_vals)
def testLen(parameters): x_vals = np.linspace(1, 1, 2) y_vals = np.linspace(1, 1, 2) z_vals = np.linspace(1, 0, 2) sweep_values = combine(*parameters, name="combined").sweep(x_vals, y_vals, z_vals) assert len(x_vals) == len(sweep_values.setpoints)
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 testMutable(parameters): setpoints = np.array([[1, 1, 1], [1, 1, 1]]) sweep_values = combine(*parameters, name="combined") a = sweep_values.sweep(setpoints) setpoints = np.array([[2, 1, 1], [1, 1, 1]]) b = sweep_values.sweep(setpoints) assert a != b
def testSet(parameters, mocker): setpoints = np.array([[1, 1, 1], [1, 1, 1]]) sweep_values = combine(*parameters, name="combined").sweep(setpoints) mock_method = mocker.patch.object(sweep_values, 'set') for i in sweep_values: sweep_values.set(i) mock_method.assert_has_calls([mocker.call(0), mocker.call(1)])
def testSweep(parameters): setpoints = np.array([[1, 1, 1], [1, 1, 1]]) sweep_values = combine(*parameters, name="combined").sweep(setpoints) res = [] for i in sweep_values: value = sweep_values.set(i) res.append([i, value]) expected = [[0, [1, 1, 1]], [1, [1, 1, 1]]] assert res == expected
def testArrays(parameters): x_vals = np.linspace(1, 1, 2) y_vals = np.linspace(1, 1, 2) z_vals = np.linspace(1, 1, 2) sweep_values = combine(*parameters, name="combined").sweep(x_vals, y_vals, z_vals) res = [] for i in sweep_values: value = sweep_values.set(i) res.append([i, value]) expected = [[0, [1, 1, 1]], [1, [1, 1, 1]]] assert res == expected
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 testMeta(parameters): name = "combined" label = "Linear Combination" unit = "a.u" aggregator = linear sweep_values = combine(*parameters, name=name, label=label, unit=unit, aggregator=aggregator) snap = sweep_values.snapshot() out = OrderedDict() out['__class__'] = full_class(sweep_values) out["unit"] = unit out["label"] = label out["full_name"] = name out["aggregator"] = repr(linear) for param in sweep_values.parameters: out[param.full_name] = {} assert out == snap
def testAggregator(parameters, npoints, x_start_stop, y_start_stop, z_start_stop): x_set = np.linspace(x_start_stop[0], x_start_stop[1], npoints).reshape(npoints, 1) y_set = np.linspace(y_start_stop[0], y_start_stop[1], npoints).reshape(npoints, 1) z_set = np.linspace(z_start_stop[0], z_start_stop[1], npoints).reshape(npoints, 1) setpoints = np.hstack((x_set, y_set, z_set)) expected_results = [linear(*set) for set in setpoints] sweep_values = combine(*parameters, name="combined", aggregator=linear).sweep(setpoints) results = [] for i, value in enumerate(sweep_values): res = sweep_values.set(value) results.append(sweep_values._aggregate(*res)) assert results == expected_results
def testSweepBadSetpoints(parameters): with pytest.raises(ValueError): combine(*parameters, name="fail").sweep(np.array([[1, 2]]))
def testCombine(parameters): multipar = combine(*parameters, name="combined") assert multipar.dimensionality == len(parameters)
def testWrongLen(parameters): x_vals = np.linspace(1, 1, 2) y_vals = np.linspace(1, 1, 2) z_vals = np.linspace(1, 1, 3) with pytest.raises(ValueError): combine(*parameters, name="combined").sweep(x_vals, y_vals, z_vals)
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() loc_fmt = 'data/{date}/#{counter}_{name}_{date}_{time}' rcd = {'name': 'parameterInside'} loc_provider = FormatLocation(fmt=loc_fmt, record=rcd) 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(location=loc_provider, 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)))