def test_nest(): n_sample_points = 100 x = ManualParameter("x") sweep_values_x = np.linspace(-1, 1, n_sample_points) y = ManualParameter("y") sweep_values_y = np.linspace(-1, 1, n_sample_points) m = ManualParameter("m") m.get = lambda: np.sin(x()) n = ManualParameter("n") n.get = lambda: np.cos(x()) + 2 * np.sin(y()) sweep_object = sweep(x, sweep_values_x)(m, sweep(y, sweep_values_y)(n)) experiment = new_experiment("sweep_measure", sample_name="sine") station = Station() meas = SweepMeasurement(exp=experiment, station=station) meas.register_sweep(sweep_object) with meas.run() as datasaver: for data in sweep_object: datasaver.add_result(*data.items()) data_set = datasaver._dataset assert data_set.paramspecs["x"].depends_on == "" assert data_set.paramspecs["y"].depends_on == "" assert data_set.paramspecs["m"].depends_on == "x" assert data_set.paramspecs["n"].depends_on == "x, y" data_x = data_set.get_data('x') data_y = data_set.get_data('y') assert data_x[::n_sample_points + 1] == [[xi] for xi in sweep_values_x] assert data_y[::n_sample_points + 1] == [[None] for _ in sweep_values_x] coordinate_layout = itertools.product(sweep_values_x, sweep_values_y) expected_x, expected_y = zip(*coordinate_layout) assert [ix for c, ix in enumerate(data_x) if c % (n_sample_points + 1)] == [[xi] for xi in expected_x] assert [iy for c, iy in enumerate(data_y) if c % (n_sample_points + 1)] == [[yi] for yi in expected_y]
def test_bare_function(self): # not a use case we want to promote, but it's there... p = ManualParameter('test') def doubler(x): p.set(x * 2) f = Function('f', call_cmd=doubler, args=[Numbers(-10, 10)]) f(4) self.assertEqual(p.get(), 8) with self.assertRaises(ValueError): f(20)
def main(): x = ManualParameter("x", unit="V") y = ManualParameter("y", unit="V") m = ManualParameter("m", unit="A") m.get = lambda: x()**2 n = ManualParameter("n", unit="A") n.get = lambda: x() - y()**2 + 16 setup = [(lambda: None, tuple())] cleanup = [(lambda: None, tuple())] result = do_experiment("cool_experiment/my_sample", setup, sweep(x, [0, 1, 2])(m), cleanup, return_format=["data_set_path"]) data_set_path = result[0] data = get_results_from_db_path(data_set_path, return_as_dict=True) print(data)
def test_szip_measure_prior_to_set(): """ We can use szip to perform a measurement before setting sweep set points. Test this scenario """ x = ManualParameter("x") v = range(1, 10) m = ManualParameter("m") m.get = lambda: 2 * x() x(0) count = 0 previous_x = x() for count, i in enumerate(szip(m, sweep(x, v))): assert i[ "m"] == 2 * previous_x # Note that at this point, x should already have been incremented assert count < len(v) previous_x = x() assert count == len(v) - 1
def test_inferred(): x = ManualParameter("x", unit="V") @setter([("xmv", "mV")], inferred_parameters=[("x", "V")]) def xsetter(milivolt_value): volt_value = milivolt_value / 1000.0 # From mV to V x.set(volt_value) return volt_value m = ManualParameter("m") m.get = lambda: np.sin(x()) sweep_values = np.linspace(-1000, 1000, 100) # We sweep in mV sweep_object = nest(sweep(xsetter, sweep_values), m) experiment = new_experiment("sweep_measure", sample_name="sine") station = Station() meas = SweepMeasurement(exp=experiment, station=station) meas.register_sweep(sweep_object) with meas.run() as datasaver: for data in sweep_object: datasaver.add_result(*data.items()) data_set = datasaver._dataset assert data_set.paramspecs["x"].depends_on == "" assert data_set.paramspecs["x"].inferred_from == "xmv" assert data_set.paramspecs["xmv"].depends_on == "" assert data_set.paramspecs["m"].depends_on == "x" expected_xmv = [[xi] for xi in sweep_values] expected_x = [[xi / 1000] for xi in sweep_values] assert data_set.get_data('xmv') == expected_xmv assert data_set.get_data('x') == expected_x
def test_simple(): x = ManualParameter("x") sweep_values = np.linspace(-1, 1, 100) m = ManualParameter("m") m.get = lambda: np.sin(x()) sweep_object = nest(sweep(x, sweep_values), m) experiment = new_experiment("sweep_measure", sample_name="sine") station = Station() meas = SweepMeasurement(exp=experiment, station=station) meas.register_sweep(sweep_object) with meas.run() as datasaver: for data in sweep_object: datasaver.add_result(*data.items()) data_set = datasaver._dataset assert data_set.paramspecs["x"].depends_on == "" assert data_set.paramspecs["m"].depends_on == "x" expected_x = [[xi] for xi in sweep_values] assert data_set.get_data('x') == expected_x