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_random_offset(self): ''' Also tests consistency between Jython and Cython: all hardcoded values generated with Cython 2.7.13, but should be consistent with all Cython, Jython ''' l1 = LineGenerator("x", "mm", 0.5, 5.5, 6) l2 = LineGenerator("y", "mm", 0.5, 5.5, 6) m1 = RandomOffsetMutator(12, ["x", "y"], [0.1, 0.1]) self.comp = CompoundGenerator([l1, l2], [], [m1], 5, True, 7) self.comp.prepare() pos = self.comp.get_points(0, 8) shortpos = self.comp.get_points(0, 4) rev = self.comp.get_points(7, -1) shortrev = self.comp.get_points(3, -1) positions = [ 0.438320, 0.430532, 0.462758, 0.540163, 0.585728, 0.565177, 1.402957, 1.461795 ] uppy = [ 1.001194, 2.063024, 3.048318, 4.036334, 5.043193, 6.051262, 1.013331, 1.984931 ] lowx = [ 0.515187, 0.434426, 0.446645, 0.501460, 0.562946, 0.575453, 1.484067, 1.432376 ] for i in range(8): point = self.comp.get_point(i) a = (pos.positions["x"][i], pos.lower["x"][i], pos.upper["y"][i]) b = (rev.positions["x"][7 - i], rev.lower["x"][7 - i], rev.upper["y"][7 - i]) c = (point.positions["x"], point.lower["x"], point.upper["y"]) for k in [a, b, c]: self.assertAlmostEqual(positions[i], k[0], delta=0.0001) self.assertAlmostEqual(lowx[i], k[1], delta=0.0001) self.assertAlmostEqual(uppy[i], k[2], delta=0.0001) # Test for when one dimension is stationary and so points = bounds for i in range(3): point = self.comp.get_point(i) a = (shortpos.positions["x"][i], shortpos.lower["x"][i], shortpos.upper["y"][i]) b = (shortrev.positions["x"][3 - i], shortrev.lower["x"][3 - i], shortrev.upper["y"][3 - i]) c = (point.positions["x"], point.lower["x"], point.upper["y"]) for k in [a, b, c]: self.assertAlmostEqual(positions[i], k[0], delta=0.0001) self.assertAlmostEqual(lowx[i], k[1], delta=0.0001) self.assertAlmostEqual(uppy[i], k[2], delta=0.0001)
def test_consistency(self): ''' Tests the consistency of the random offsetmutator function: a mutated Points should contain the same as the sum of the Point that make it up mutated seperately; as should a Points constructed from multiple Points. ''' l1 = LineGenerator("x", "mm", 0.5, 5.5, 6) l2 = LineGenerator("y", "mm", 0.5, 5.5, 6) m1 = RandomOffsetMutator(12, ["x", "y"], [0.1, 0.1]) self.comp = CompoundGenerator([l1, l2], [], [m1], 5, True, 7) self.comp.prepare() points = self.comp.get_points(5, 12) # get_points apoints = self.comp.get_points(5, 8) # get_points + get_points apoints += self.comp.get_points(8, 12) addi_points = Points() # get_point + get_point for i in range(6): point = self.comp.get_point(i + 5) # get_point addi_points += point # a = b, a = c, a = d => a = b = c = d a = (points.positions["x"][i], points.lower["x"][i], "a") b = (point.positions["x"], point.lower["x"], "b") c = (addi_points.positions["x"][i], addi_points.lower["x"][i], "c") d = (apoints.positions["x"][i], apoints.lower["x"][i], "d") for k in [b, c, d]: self.assertAlmostEqual(a[0], k[0]) self.assertAlmostEqual(a[1], k[1]) # One dimension stationary therefore bounds = points apoints = self.comp.get_points(8, 10) apoints += self.comp.get_points(10, 12) mpoints = self.comp.get_points(8, 12) addi_points = Points() for i in range(4): point = self.comp.get_point(8 + i) addi_points += point # a = b, a = c, a = d => a = b = c = d a = (points.positions["x"][3 + i], points.lower["x"][3 + i]) b = (point.positions["x"], point.lower["x"]) c = (addi_points.positions["x"][i], addi_points.lower["x"][i]) d = (apoints.positions["x"][i], apoints.lower["x"][i]) e = (mpoints.positions["x"][i], mpoints.lower["x"][i]) for k in [b, c, d, e]: self.assertAlmostEqual(a[0], k[0]) self.assertAlmostEqual(a[1], k[1])
def random_offset_check(): x = LineGenerator("x", "mm", 0.0, 4.0, 5, alternate=True) y = LineGenerator("y", "mm", 0.0, 3.0, 4) mutator = RandomOffsetMutator(2, ['x', 'y'], dict(x=0.25, y=0.25)) gen = CompoundGenerator([y, x], [], [mutator]) plot_generator(gen) gen = CompoundGenerator([y, x], [], [mutator, mutator]) plot_generator(gen) gen = CompoundGenerator([y, x], [], [mutator, mutator, mutator]) plot_generator(gen) gen = CompoundGenerator([y, x], [], [mutator, mutator, mutator, mutator, mutator]) plot_generator(gen)
def test_slicing(self): l1 = LineGenerator("x", "mm", 0.5, 5.5, 6) l2 = LineGenerator("y", "mm", 0.5, 5.5, 6) m1 = RandomOffsetMutator(12, ["x", "y"], [0.1, 0.1]) self.comp = CompoundGenerator([l1, l2], [], [m1], 5, True, 7) self.comp.prepare() points = self.comp.get_points(0, 8) self.assertEqual(8, len(points)) self.assertAlmostEqual(0.45867, points[0].positions["y"], delta=0.0001) for i in [0, 1]: self.assertAlmostEqual([0.45867, 1.54371][i], points[0:2].positions["y"][i], delta=0.0001) self.assertAlmostEqual([0.58572, 0.54016][i], points[4:2:-1].positions["x"][i], delta=0.0001) self.assertAlmostEqual(1.46179, points[-1].positions["x"], delta=0.0001) for i in range(4): self.assertAlmostEqual([1.46179, 0.56517, 0.54016, 0.43053][i], points[-1:0:-2].positions["x"][i], delta=0.0001)
def __init__(self, seed, axes, max_offset): self.py_mutator = RandomOffsetMutator(seed, axes, max_offset) logging.debug(self.py_mutator.to_dict())