class JLissajousGenerator(JavaIteratorWrapper): """ Create a LissajousGenerator and wrap the points into java Point objects """ def __init__(self, names, units, box, num_lobes, num_points): super(JLissajousGenerator, self).__init__() self.names = names self.generator = LissajousGenerator(names, units, box, num_lobes, num_points) logging.debug(self.generator.to_dict()) def _iterator(self): x_name = self.names[0] y_name = self.names[1] for point in self.generator.iterator(): index = point.indexes[0] x_position = point.positions[x_name] y_position = point.positions[y_name] java_point = Point(x_name, index, x_position, y_name, index, y_position, False) # Set is2D=False yield java_point
def test_line_lissajous(self): expected = [{'y': 0.0, 'x': 0.5, 'z': 0.0}, {'y': 0.2938926261462366, 'x': 0.15450849718747375, 'z': 0.0}, {'y': -0.4755282581475768, 'x': -0.40450849718747367, 'z': 0.0}, {'y': 0.47552825814757677, 'x': -0.4045084971874738, 'z': 0.0}, {'y': -0.2938926261462364, 'x': 0.1545084971874736, 'z': 0.0}, {'y': 0.0, 'x': 0.5, 'z': 2.0}, {'y': 0.2938926261462366, 'x': 0.15450849718747375, 'z': 2.0}, {'y': -0.4755282581475768, 'x': -0.40450849718747367, 'z': 2.0}, {'y': 0.47552825814757677, 'x': -0.4045084971874738, 'z': 2.0}, {'y': -0.2938926261462364, 'x': 0.1545084971874736, 'z': 2.0}, {'y': 0.0, 'x': 0.5, 'z': 4.0}, {'y': 0.2938926261462366, 'x': 0.15450849718747375, 'z': 4.0}, {'y': -0.4755282581475768, 'x': -0.40450849718747367, 'z': 4.0}, {'y': 0.47552825814757677, 'x': -0.4045084971874738, 'z': 4.0}, {'y': -0.2938926261462364, 'x': 0.1545084971874736, 'z': 4.0}] z = LineGenerator("z", "mm", 0.0, 4.0, 3) liss = LissajousGenerator(['x', 'y'], "mm", [0., 0.], [1., 1.], 1, size=5) g = CompoundGenerator([z, liss], [], []) g.prepare() self.assertEqual(g.axes, ["z", "x", "y"]) points = list(g.iterator()) self.assertEqual(len(expected), len(points)) for i, p in enumerate(points): self.assertEqual(expected[i], p.positions)
def lissajous_check(): bounding_box = dict(centre=[0.0, 0.0], span=[1.0, 1.0], lobes=2) lissajous = LissajousGenerator(['x', 'y'], ["mm", "mm"], **bounding_box) gen = CompoundGenerator([lissajous], [], []) plot_generator(gen)
def test_to_dict(self): g = LissajousGenerator(["x", "y"], ["mm", "cm"], [0., 0.], [1., 1.], 1) expected_dict = dict() expected_dict[ 'typeid'] = "scanpointgenerator:generator/LissajousGenerator:1.0" expected_dict['axes'] = ["x", "y"] expected_dict['units'] = ["mm", "cm"] expected_dict['centre'] = [0., 0.] expected_dict['span'] = [1., 1.] expected_dict['lobes'] = 1 expected_dict['size'] = 250 expected_dict['alternate'] = False d = g.to_dict() self.assertEqual(expected_dict, d)
def lissajous_rectangle_check(): bounding_box = dict(centre=[0.0, 0.0], span=[1.0, 1.0], lobes=2) lissajous = LissajousGenerator(['x', 'y'], ["mm", "mm"], **bounding_box) rectangle = ROIExcluder([RectangularROI([0.0, 0.0], 0.8, 0.8)], ['x', 'y']) gen = CompoundGenerator([lissajous], [rectangle], []) plot_generator(gen, rectangle)
def test_horrible_scan(self): lissajous = LissajousGenerator(["j1", "j2"], "mm", [-0.5, 0.7], [2, 3.5], 7, 100) line2 = LineGenerator(["l2"], "mm", -3, 3, 7, True) line1 = LineGenerator(["l1"], "mm", -1, 2, 5, True) spiral = SpiralGenerator(["s1", "s2"], "mm", [1, 2], 5, 2.5, True) r1 = CircularROI([1, 1], 2) r2 = CircularROI([-1, -1], 4) r3 = CircularROI([1, 1], 2) e1 = ROIExcluder([r1], ["j1", "l2"]) e2 = ROIExcluder([r2], ["s2", "l1"]) e3 = ROIExcluder([r3], ["s1", "s2"]) g = CompoundGenerator([lissajous, line2, line1, spiral], [e1, e2, e3], []) g.prepare() l2_f = True l1_f = True s_f = True points = [] for (j1, j2) in zip(lissajous.positions["j1"], lissajous.positions["j2"]): l2p = line2.positions["l2"] if l2_f else line2.positions["l2"][::-1] l2_f = not l2_f for l2 in l2p: l1p = line1.positions["l1"] if l1_f else line1.positions[ "l1"][::-1] l1_f = not l1_f for l1 in l1p: sp = zip(spiral.positions["s1"], spiral.positions["s2"]) if s_f \ else zip(spiral.positions["s1"][::-1], spiral.positions["s2"][::-1]) s_f = not s_f for (s1, s2) in sp: points.append((s1, s2, l1, l2, j1, j2)) self.assertEqual( lissajous.size * line2.size * line1.size * spiral.size, len(points)) points = [(s1, s2, l1, l2, j1, j2) for (s1, s2, l1, l2, j1, j2) in points if (j1 - 1)**2 + (l2 - 1)**2 <= 4 and (s2 + 1)**2 + (l1 + 1)**2 <= 16 and (s1 - 1)**2 + (s2 - 1)**2 <= 4] self.assertEqual(len(points), g.size) generated_points = list(g.iterator()) self.assertEqual(len(points), len(generated_points)) actual = [p.positions for p in generated_points] expected = [{ "j1": j1, "j2": j2, "l2": l2, "l1": l1, "s1": s1, "s2": s2 } for (s1, s2, l1, l2, j1, j2) in points] for e, a in zip(expected, actual): self.assertEqual(e, a) self.assertEqual((181, 10), g.shape)
def __init__(self, names, units, box, num_lobes, num_points): super(JLissajousGenerator, self).__init__() self.names = names liss_gen = LissajousGenerator(names, units, box["centre"], [box["width"], box["height"]], num_lobes, num_points) self.generator = CompoundGenerator([liss_gen], [], []) self.generator.prepare() logging.debug(self.generator.to_dict())
def test_to_dict(self): bounding_box = dict(centre=[0.0, 0.0], width=1.0, height=1.0) g = LissajousGenerator(["x", "y"], ["mm", "cm"], [0., 0.], [1., 1.], 1) expected_dict = dict() box = dict() expected_dict[ 'typeid'] = "scanpointgenerator:generator/LissajousGenerator:1.0" expected_dict['axes'] = ["x", "y"] expected_dict['units'] = ["mm", "cm"] expected_dict['centre'] = [0., 0.] expected_dict['span'] = [1., 1.] expected_dict['lobes'] = 1 expected_dict['size'] = 250 d = g.to_dict() self.assertEqual(expected_dict, d)
def test_array_positions_with_offset(self): g = LissajousGenerator(['x', 'y'], ['mm', 'mm'], [ 1., 0., ], [2., 1.], 1, size=5) g.prepare_positions() g.prepare_bounds() expected_x = [ 2.0, 1.3090169943749475, 0.19098300532505266, 0.19098300532505266, 1.3090169943749475 ] expected_y = [ 0.0, 0.2938926261462366, -0.4755282581475768, 0.47552825814757677, -0.2938926261462364 ] expected_bx = [ 1.8090169943749475, 1.8090169943749475, 0.6909830056250528, 0.0, 0.6909830056250523, 1.8090169943749472 ] expected_by = [ -0.47552825814757677, 0.47552825814757677, -0.2938926261462365, -1.2246467991473532e-16, 0.2938926261462367, -0.4755282581475769 ] self.assertListAlmostEqual(expected_x, g.positions['x'].tolist()) self.assertListAlmostEqual(expected_y, g.positions['y'].tolist()) self.assertListAlmostEqual(expected_bx, g.bounds['x'].tolist()) self.assertListAlmostEqual(expected_by, g.bounds['y'].tolist())
def test_array_positions(self): g = LissajousGenerator(['x', 'y'], ["mm", "mm"], [0., 0.], [1., 1.], 1, size=10) expected_x = [ 0.5, 0.4045084971874737, 0.15450849718747375, -0.15450849718747364, -0.40450849718747367, -0.5, -0.4045084971874738, -0.1545084971874738, 0.1545084971874736, 0.4045084971874736 ] expected_y = [ 0.0, 0.47552825814757677, 0.2938926261462366, -0.2938926261462365, -0.4755282581475768, -1.2246467991473532e-16, 0.47552825814757677, 0.2938926261462367, -0.2938926261462364, -0.4755282581475769 ] expected_bx = [ 0.47552825814757677, 0.4755282581475768, 0.2938926261462366, 6.123233995736766e-17, -0.2938926261462365, -0.47552825814757677, -0.47552825814757682, -0.2938926261462367, -1.2246467991473532e-16, 0.29389262614623646, 0.47552825814757677 ] expected_by = [ -0.29389262614623657, 0.29389262614623657, 0.4755282581475768, 6.123233995736766e-17, -0.47552825814757677, -0.2938926261462367, 0.29389262614623607, 0.4755282581475768, 1.8369701987210297e-16, -0.4755282581475767, -0.29389262614623674 ] g.prepare_positions() g.prepare_bounds() self.assertListAlmostEqual(expected_x, g.positions['x'].tolist()) self.assertListAlmostEqual(expected_y, g.positions['y'].tolist()) self.assertListAlmostEqual(expected_bx, g.bounds['x'].tolist()) self.assertListAlmostEqual(expected_by, g.bounds['y'].tolist())
def test_from_dict(self): _dict = dict() _dict['axes'] = ["x", "y"] _dict['units'] = ["cm", "mm"] _dict['centre'] = [0.0, 0.0] _dict['span'] = [1., 2.] _dict['lobes'] = 5 _dict['size'] = 250 units_dict = dict() units_dict['x'] = "cm" units_dict['y'] = "mm" gen = LissajousGenerator.from_dict(_dict) self.assertEqual(["x", "y"], gen.axes) self.assertEqual(units_dict, gen.axis_units()) self.assertEqual(5, gen.x_freq) self.assertEqual(0.5, gen.x_max) self.assertEqual(1.0, gen.y_max) self.assertEqual([0.0, 0.0], gen.centre) self.assertEqual(250, gen.size)
def test_time_constraint_complex(self): a = LineGenerator("a", "eV", 0, 1, 10) b = LineGenerator("b", "rad", 0, 1, 10) c = LissajousGenerator(["c", "d"], ["mm", "cm"], [0, 0], [5, 5], 3, 10) d = SpiralGenerator(["e", "f"], ["mm", "s"], [10, 5], 7, 0.6) e = LineGenerator("g", "mm", 0, 1, 10) f = LineGenerator("h", "mm", 0, 5, 20) r1 = CircularROI([0.2, 0.2], 0.1) r2 = CircularROI([0.4, 0.2], 0.1) r3 = CircularROI([0.6, 0.2], 0.1) e1 = ROIExcluder([r1, r2, r3], ["a", "b"]) m1 = RandomOffsetMutator(12, ["a"], [0.1]) m2 = RandomOffsetMutator(200, ["c", "f"], [0.01, 0.5]) g = CompoundGenerator([c, d, e, f, b, a], [e1], [m1, m2]) g.prepare() # 1e6 points for i in [1, 2, 3, 4, 5, 6]: p = 10**i start_time = time.time() g.get_points(0, p) end_time = time.time() self.assertLess(end_time - start_time, TIMELIMIT * p / 1e4)
def test_duplicate_name_raises(self): with self.assertRaises(AssertionError): LissajousGenerator(["x", "x"], ["mm", "mm"], [ 0., 0, ], [1., 1.], 1)
def test_init(self): g = LissajousGenerator(["x", "y"], ["mm", "cm"], [0., 0.], [1., 1.], 1) self.assertEqual(g.axis_units(), dict(x="mm", y="cm")) self.assertEqual(g.axes, ["x", "y"])
def __init__(self, names, units, box, num_lobes, num_points): super(JLissajousGenerator, self).__init__() self.names = names self.generator = LissajousGenerator(names, units, box, num_lobes, num_points) logging.debug(self.generator.to_dict())