Beispiel #1
0
class JSpiralGenerator(JavaIteratorWrapper):
    """
    Create a SpiralGenerator and wrap the points into java Point objects
    """

    def __init__(self, names, units, centre, radius, scale=1.0, alternate_direction=False):
        super(JSpiralGenerator, self).__init__()
        
        self.names = names
        self.generator = SpiralGenerator(names, units, centre, radius, scale, alternate_direction)
        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
class JSpiralGenerator(JavaIteratorWrapper):
    """
    Create a SpiralGenerator and wrap the points into java Point objects
    """

    def __init__(self, names, units, centre, radius, scale=1.0, alternate_direction=False):
        super(JSpiralGenerator, self).__init__()
        
        self.names = names
        self.generator = SpiralGenerator(names, units, centre, radius, scale, alternate_direction)
        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_double_spiral_scan(self):
        line1 = LineGenerator(["l1"], "mm", -1, 2, 5)
        spiral_s = SpiralGenerator(["s1", "s2"], "mm", [1, 2], 5, 2.5, True)
        spiral_t = SpiralGenerator(["t1", "t2"], "mm", [0, 0], 5, 2.5, True)
        line2 = LineGenerator(["l2"], "mm", -1, 2, 5, True)
        r = CircularROI([0, 0], 1)
        e1 = ROIExcluder([r], ["s1", "l1"])
        e2 = ROIExcluder([r], ["l2", "t1"])
        g = CompoundGenerator([line1, spiral_s, spiral_t, line2], [e1, e2], [])
        g.prepare()

        points = []
        l1s = []
        tl2 = []

        s_f = True
        for l1 in line1.positions["l1"]:
            sp = zip(spiral_s.positions['s1'], spiral_s.positions['s2'])
            sp = sp if s_f else list(sp)[::-1]
            s_f = not s_f
            l1s += [(s1, s2, l1) for (s1, s2) in sp]
        l2_f = True
        for (t1, t2) in zip(spiral_t.positions['t1'], spiral_t.positions['t2']):
            l2p = line2.positions['l2'] if l2_f else line2.positions['l2'][::-1]
            l2pu = line2.bounds['l2'][1:len(line2.positions['l2'])+1]
            l2pl = line2.bounds['l2'][0:len(line2.positions['l2'])]
            if not l2_f:
                l2pu, l2pl = l2pl[::-1], l2pu[::-1]
            l2_f = not l2_f
            tl2 += [(l2, l2u, l2l, t1, t2) for (l2, l2u, l2l) in
                zip(l2p, l2pu, l2pl) if l2*l2 + t1*t1 <= 1]
        t_f = True
        for (s1, s2, l1) in l1s:
            inner = tl2 if t_f else tl2[::-1]
            t_f = not t_f
            points += [(l2, l2u, l2l, t1, t2, s1, s2, l1)
                for (l2, l2u, l2l, t1, t2) in inner if s1*s1 + l1*l1 <= 1]
        l1s_original = l1s
        l1s = [(s1, s2, l1) for (s1, s2, l1) in l1s if s1*s1 + l1*l1 <= 1]

        expected = [{"l2":l2, "t1":t1, "t2":t2, "s1":s1, "s2":s2, "l1":l1}
            for (l2, l2u, l2l, t1, t2, s1, s2, l1) in points]

        expected_idx = []
        t_f = (l1s_original.index(l1s[0])) % 2 == 0 # t_f is False
        for d1 in range_(len(l1s)):
            expected_idx += [[d1, d2] for d2 in (range_(len(tl2)) if t_f else
                range_(len(tl2) - 1, -1, -1))]
            t_f = not t_f

        expected_l2_lower = [l2l for (l2, l2u, l2l, t1, t2, s1, s2, l1) in points]
        expected_l2_upper = [l2u for (l2, l2u, l2l, t1, t2, s1, s2, l1) in points]

        gpoints = list(g.iterator())
        self.assertEqual(expected, [p.positions for p in gpoints])
        self.assertEqual(expected_idx, [p.indexes for p in gpoints])
        self.assertEqual(expected_l2_lower, [p.lower["l2"] for p in gpoints])
        self.assertEqual(expected_l2_upper, [p.upper["l2"] for p in gpoints])
    def test_three_dim_middle_alternates(self):
        tg = LineGenerator("t", "mm", 1, 5, 5)
        zg = LineGenerator("z", "mm", -1, 3, 5, True)
        spiral = SpiralGenerator(["s1", "s2"], "mm", [1, 1], 2, 1, True)
        yg = LineGenerator("y", "mm", 0, 4, 5)
        xg = LineGenerator("x", "mm", 0, 4, 5)
        r1 = CircularROI([0, 0], 1)
        e1 = ROIExcluder([r1], ["s1", "z"])
        e2 = ROIExcluder([r1], ["y", "x"])
        g = CompoundGenerator([tg, zg, spiral, yg, xg], [e2, e1], [])
        g.prepare()

        it = 0
        iz = 0
        iy = 0
        ix = 0
        tzs = []
        points = []
        for t in range_(1, 6):
            for z in (range_(-1, 4) if it % 2 == 0 else range_(3, -2, -1)):
                s1p = spiral.positions["s1"] if iz % 2 == 0 else spiral.positions["s1"][::-1]
                s2p = spiral.positions["s2"] if iz % 2 == 0 else spiral.positions["s2"][::-1]
                points += [(x, y, s1, s2, z, t) for (s1, s2) in zip(s1p, s2p)
                        for y in range(0, 5) for x in range(0, 5)
                        if s1*s1 + z*z <= 1 and y*y + x*x <= 1]
                iz += 1
            it += 1
        expected = [{"x":float(x), "y":float(y), "s1":s1, "s2":s2, "z":float(z), "t":float(t)}
            for (x, y, s1, s2, z, t) in points]
        actual = [p.positions for p in list(g.iterator())]
        for e, a in zip(expected, actual):
            self.assertEqual(e, a)
    def test_excluder_spread_axes(self):
        sp = SpiralGenerator(["s1", "s2"], ["mm", "mm"], centre=[0, 0], radius=1, scale=0.5, alternate=True)
        y = LineGenerator("y", "mm", 0, 1, 3, True)
        z = LineGenerator("z", "mm", -2, 3, 6, True)
        e = ROIExcluder([CircularROI([0., 0.], 1.0)], ["s1", "z"])
        g = CompoundGenerator([z, y, sp], [e], [])

        g.prepare()

        s1_pos, s2_pos = sp.positions["s1"], sp.positions["s2"]
        s1_pos = np.tile(np.append(s1_pos, s1_pos[::-1]), 9)
        s2_pos = np.tile(np.append(s2_pos, s2_pos[::-1]), 9)
        y_pos = np.tile(np.repeat(np.array([0, 0.5, 1.0, 1.0, 0.5, 0]), sp.size), 3)
        z_pos = np.repeat(np.array([-2, -1, 0, 1, 2, 3]), sp.size * 3)

        mask_func = lambda ps1, pz: ps1**2 + pz**2 <= 1
        mask = mask_func(s1_pos, z_pos)

        expected_s1 = s1_pos[mask]
        expected_s2 = s2_pos[mask]
        expected_y = y_pos[mask]
        expected_z = z_pos[mask]
        expected_positions = [{'s1':ps1, 's2':ps2, 'y':py, 'z':pz}
                for (ps1, ps2, py, pz) in zip(expected_s1, expected_s2, expected_y, expected_z)]
        positions = [point.positions for point in list(g.iterator())]

        self.assertEqual(positions, expected_positions)
 def test_line_spiral(self):
     expected = [
         {
             'y': -0.3211855677650875,
             'x': 0.23663214944574582,
             'z': 0.0
         },
         {
             'y': -0.25037538922751695,
             'x': -0.6440318266552169,
             'z': 0.0
         },
         {
             'y': 0.6946549630820702,
             'x': -0.5596688286164636,
             'z': 0.0
         },
         {
             'y': 0.6946549630820702,
             'x': -0.5596688286164636,
             'z': 2.0
         },
         {
             'y': -0.25037538922751695,
             'x': -0.6440318266552169,
             'z': 2.0
         },
         {
             'y': -0.3211855677650875,
             'x': 0.23663214944574582,
             'z': 2.0
         },
         {
             'y': -0.3211855677650875,
             'x': 0.23663214944574582,
             'z': 4.0
         },
         {
             'y': -0.25037538922751695,
             'x': -0.6440318266552169,
             'z': 4.0
         },
         {
             'y': 0.6946549630820702,
             'x': -0.5596688286164636,
             'z': 4.0
         },
     ]
     z = LineGenerator("z", "mm", 0.0, 4.0, 3)
     spiral = SpiralGenerator(['x', 'y'],
                              "mm", [0.0, 0.0],
                              0.8,
                              alternate=True)
     g = CompoundGenerator([z, spiral], [], [])
     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 test_get_point_large_scan(self):
        s = SpiralGenerator(["x", "y"], "mm", [0, 0], 6, 1) #114 points
        z = LineGenerator("z", "mm", 0, 1, 100)
        w = LineGenerator("w", "mm", 0, 1, 5)
        t = LineGenerator("t", "mm", 0, 1, 5)
        rad1 = 2.8
        r1 = CircularROI([1., 1.], rad1)
        e1 = ROIExcluder([r1], ["x", "y"])
        rad2 = 2
        r2 = CircularROI([0.5, 0.5], rad2)
        e2 = ROIExcluder([r2], ["y", "z"])
        rad3 = 0.5
        r3 = CircularROI([0.5, 0.5], rad3)
        e3 = ROIExcluder([r3], ["w", "t"])
        g = CompoundGenerator([t, w, z, s], [e1, e2, e3], [])
        g.prepare()

        spiral = [(x, y) for (x, y) in zip(s.positions["x"], s.positions["y"])]
        zwt = [(z/99., w/4., t/4.) for t in range_(0, 5)
            for w in range_(0, 5)
            for z in range_(0, 100)]
        expected = [(x, y, z, w, t) for (z, w, t) in zwt for (x, y) in spiral]
        expected = [{"x":x, "y":y, "z":z, "w":w, "t":t}
                for (x,y,z,w,t) in expected if
                (x-1)*(x-1) + (y-1)*(y-1) <= rad1*rad1 and
                (y-0.5)*(y-0.5) + (z-0.5)*(z-0.5) <= rad2*rad2 and
                (w-0.5)*(w-0.5) + (t-0.5)*(t-0.5) <= rad3*rad3]
        points = [g.get_point(n) for n in range_(0, g.size)]
        pos = [p.positions for p in points]
        # assertEqual on a sequence of dicts is *really* slow
        for (e, p) in zip(expected, pos):
            self.assertEquals(e.keys(), p.keys())
            for k in e.keys():
                self.assertAlmostEqual(e[k], p[k])
Beispiel #8
0
    def __init__(self, names, units, centre, radius, scale=1.0, alternate_direction=False):
        super(JSpiralGenerator, self).__init__()

        self.names = names
        spiral_gen = SpiralGenerator(names, units, centre, radius, scale, alternate_direction)
        self.generator = CompoundGenerator([spiral_gen], [], [])
        self.generator.prepare()
        logging.debug(self.generator.to_dict())
Beispiel #9
0
def spiral_rectangle_check():

    spiral = SpiralGenerator(['x', 'y'], ["mm", "mm"], [0.0, 0.0], 10.0)
    rectangle = ROIExcluder([RectangularROI([0.0, 0.0], 10.0, 10.0)],
                            ['x', 'y'])
    gen = CompoundGenerator([spiral], [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 test_simple_mask_alternating_spiral(self):
     z = LineGenerator("z", "mm", 0.0, 4.0, 5)
     spiral = SpiralGenerator(['x', 'y'], "mm", [0.0, 0.0], 3, alternate=True) #29 points
     r = RectangularROI([-2, -2], 3, 4)
     e = ROIExcluder([r], ["x", "y"])
     g = CompoundGenerator([z, spiral], [e], [])
     g.prepare()
     p = list(zip(spiral.positions['x'], spiral.positions['y']))
     expected = [x >= -2 and x < 1 and y >= -2 and y < 2 for (x, y) in p]
     expected_r = [x >= -2 and x < 1 and y >= -2 and y < 2 for (x, y) in p[::-1]]
     actual = g.dimensions[1].mask.tolist()
     self.assertEqual(expected, actual)
 def test_double_mask_spiral(self):
     zgen = LineGenerator("z", "mm", 0.0, 4.0, 5)
     spiral = SpiralGenerator(['x', 'y'], "mm", [0.0, 0.0], 3) #29 points
     r1 = RectangularROI([-2, -2], 4, 3)
     r2 = RectangularROI([-2, 0], 4, 3)
     e1 = ROIExcluder([r1], ["y", "x"])
     e2 = ROIExcluder([r2], ["y", "z"])
     g = CompoundGenerator([zgen, spiral], [e1, e2], [])
     g.prepare()
     p = list(zip(spiral.positions['x'], spiral.positions['y']))
     p = [(x, y, z) for z in range_(0, 5) for (x, y) in p]
     expected = [x >= -2 and x <= 1 and y >= -2 and y <= 2
             and z >= 0 and z <= 3 for (x, y, z) in p]
     actual = g.dimensions[0].mask.tolist()
     self.assertEqual(expected, actual)
    def test_from_dict(self):
        _dict = dict()
        _dict['axes'] = ["x", "y"]
        _dict['units'] = ["mm", "cm"]
        _dict['centre'] = [0.0, 0.0]
        _dict['radius'] = 1.4
        _dict['scale'] = 1
        _dict['alternate'] = True

        units_dict = dict()
        units_dict['x'] = "mm"
        units_dict['y'] = "cm"

        gen = SpiralGenerator.from_dict(_dict)

        self.assertEqual(["x", "y"], gen.axes)
        self.assertEqual(units_dict, gen.axis_units())
        self.assertEqual([0.0, 0.0], gen.centre)
        self.assertEqual(1.4, gen.radius)
        self.assertEqual(1, gen.scale)
    def test_200_million_time_constraint(self):
        start_time = time.time()

        s = SpiralGenerator(
            ["x", "y"], "mm", [0, 0], 6, 0.02, True) # ~2e5 points
        z = LineGenerator("z", "mm", 0, 1, ZSIZE, True) #1e2 points or 1e1 for Jython
        w = LineGenerator("w", "mm", 0, 1, 10, True) #1e1 points
        r1 = CircularROI([-0.7, 4], 0.5)
        r2 = CircularROI([0.5, 0.5], 0.3)
        r3 = CircularROI([0.2, 4], 0.5)
        e1 = ROIExcluder([r1], ["x", "y"])
        e2 = ROIExcluder([r2], ["w", "z"])
        e3 = ROIExcluder([r3], ["z", "y"])
        om = RandomOffsetMutator(0, ["x", "y"], {"x":0.2, "y":0.2})
        g = CompoundGenerator([w, z, s], [e1, e3, e2], [om])
        g.prepare() # g.size ~3e5

        end_time = time.time()
        # if this test becomes problematic then we'll just have to remove it
        self.assertLess(end_time - start_time, TIMELIMIT)
    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)
Beispiel #16
0
    def test_configure(self):
        task = MagicMock()
        params = MagicMock()
        energy = LineGenerator("energy", "kEv", 13.0, 15.2, 2)
        spiral = SpiralGenerator(["x", "y"], ["mm", "mm"], [0., 0.],
                                 5.,
                                 scale=2.0)
        params.generator = CompoundGenerator([energy, spiral], [], [], 0.1)
        params.filePath = "/tmp/file.h5"
        params.generator.prepare()
        completed_steps = 0
        steps_to_do = 38
        part_info = {
            "DET": [NDArrayDatasetInfo("xspress3", 2)],
            "STAT": [CalculatedNDAttributeDatasetInfo("sum", "StatsTotal")],
        }
        infos = self.o.configure(task, completed_steps, steps_to_do, part_info,
                                 params)
        self.assertEqual(len(infos), 5)
        self.assertEquals(infos[0].name, "xspress3.data")
        self.assertEquals(infos[0].filename, "file.h5")
        self.assertEquals(infos[0].type, "primary")
        self.assertEquals(infos[0].rank, 4)
        self.assertEquals(infos[0].path, "/entry/detector/detector")
        self.assertEquals(infos[0].uniqueid,
                          "/entry/NDAttributes/NDArrayUniqueId")
        self.assertEquals(infos[1].name, "xspress3.sum")
        self.assertEquals(infos[1].filename, "file.h5")
        self.assertEquals(infos[1].type, "secondary")
        self.assertEquals(infos[1].rank, 4)
        self.assertEquals(infos[1].path, "/entry/sum/sum")
        self.assertEquals(infos[1].uniqueid,
                          "/entry/NDAttributes/NDArrayUniqueId")
        self.assertEquals(infos[2].name, "energy.value_set")
        self.assertEquals(infos[2].filename, "file.h5")
        self.assertEquals(infos[2].type, "position_set")
        self.assertEquals(infos[2].rank, 1)
        self.assertEquals(infos[2].path, "/entry/detector/energy_set")
        self.assertEquals(infos[2].uniqueid, "")
        self.assertEquals(infos[3].name, "x.value_set")
        self.assertEquals(infos[3].filename, "file.h5")
        self.assertEquals(infos[3].type, "position_set")
        self.assertEquals(infos[3].rank, 1)
        self.assertEquals(infos[3].path, "/entry/detector/x_set")
        self.assertEquals(infos[3].uniqueid, "")
        self.assertEquals(infos[4].name, "y.value_set")
        self.assertEquals(infos[4].filename, "file.h5")
        self.assertEquals(infos[4].type, "position_set")
        self.assertEquals(infos[4].rank, 1)
        self.assertEquals(infos[4].path, "/entry/detector/y_set")
        self.assertEquals(infos[4].uniqueid, "")
        self.assertEqual(task.put.call_args_list, [
            call(self.child["positionMode"], True),
            call(self.child["numCapture"], 0),
            call('flushDataPerNFrames', 10.0),
            call('flushAttrPerNFrames', 10.0)
        ])
        self.assertEqual(task.put_many_async.call_count, 2)
        self.assertEqual(
            task.put_many_async.call_args_list[0],
            call(
                self.child,
                dict(enableCallbacks=True,
                     fileWriteMode="Stream",
                     swmrMode=True,
                     positionMode=True,
                     dimAttDatasets=True,
                     lazyOpen=True,
                     arrayCounter=0,
                     filePath="/tmp/",
                     fileName="file.h5",
                     fileTemplate="%s%s")))
        self.assertEqual(
            task.put_many_async.call_args_list[1],
            call(
                self.child,
                dict(numExtraDims=1,
                     posNameDimN="d1",
                     extraDimSizeN=20,
                     posNameDimX="d0",
                     extraDimSizeX=2,
                     posNameDimY="",
                     extraDimSizeY=1,
                     posNameDim3="",
                     extraDimSize3=1,
                     posNameDim4="",
                     extraDimSize4=1,
                     posNameDim5="",
                     extraDimSize5=1,
                     posNameDim6="",
                     extraDimSize6=1,
                     posNameDim7="",
                     extraDimSize7=1,
                     posNameDim8="",
                     extraDimSize8=1,
                     posNameDim9="",
                     extraDimSize9=1)))
        expected_xml = """<?xml version="1.0" ?>
<hdf5_layout>
<group name="entry">
<attribute name="NX_class" source="constant" type="string" value="NXentry" />
<group name="detector">
<attribute name="signal" source="constant" type="string" value="detector" />
<attribute name="axes" source="constant" type="string" value="energy_set,.,.,." />
<attribute name="NX_class" source="constant" type="string" value="NXdata" />
<attribute name="energy_set_indices" source="constant" type="string" value="0" />
<dataset name="energy_set" source="constant" type="float" value="13,15.2">
<attribute name="units" source="constant" type="string" value="kEv" />
</dataset>
<attribute name="x_set_indices" source="constant" type="string" value="1" />
<dataset name="x_set" source="constant" type="float" value="0.473264298891,-1.28806365331,-1.11933765723,0.721339144968,2.26130106714,2.3717213098,1.08574712174,-0.863941392256,-2.59791589857,-3.46951769442,-3.22399679412,-1.98374931946,-0.132541097885,1.83482458567,3.45008680308,4.36998121172,4.42670524204,3.63379270355,2.15784413199,0.269311496406">
<attribute name="units" source="constant" type="string" value="mm" />
</dataset>
<attribute name="y_set_indices" source="constant" type="string" value="1" />
<dataset name="y_set" source="constant" type="float" value="-0.64237113553,-0.500750778455,1.38930992616,1.98393756064,0.784917470231,-1.17377831157,-2.66405897615,-2.9669684623,-2.01825893141,-0.24129368636,1.72477821509,3.27215424484,3.98722048131,3.71781556747,2.5610299588,0.799047653518,-1.18858453138,-3.01284626565,-4.34725663835,-4.9755042398">
<attribute name="units" source="constant" type="string" value="mm" />
</dataset>
<dataset det_default="true" name="detector" source="detector">
<attribute name="NX_class" source="constant" type="string" value="SDS" />
</dataset>
</group>
<group name="sum">
<attribute name="signal" source="constant" type="string" value="sum" />
<attribute name="axes" source="constant" type="string" value="energy_set,.,.,." />
<attribute name="NX_class" source="constant" type="string" value="NXdata" />
<attribute name="energy_set_indices" source="constant" type="string" value="0" />
<hardlink name="energy_set" target="/entry/detector/energy_set" />
<attribute name="x_set_indices" source="constant" type="string" value="1" />
<hardlink name="x_set" target="/entry/detector/x_set" />
<attribute name="y_set_indices" source="constant" type="string" value="1" />
<hardlink name="y_set" target="/entry/detector/y_set" />
<dataset name="sum" ndattribute="StatsTotal" source="ndattribute" />
</group>
<group name="NDAttributes" ndattr_default="true">
<attribute name="NX_class" source="constant" type="string" value="NXcollection" />
</group>
</group>
</hdf5_layout>"""
        expected_filename = "/tmp/BLOCK-HDF5-layout.xml"
        self.assertEqual(task.put_async.call_args_list[0][0][1],
                         expected_filename)
        actual_xml = open(expected_filename).read().replace(">", ">\n")
        self.assertEqual(actual_xml.splitlines(), expected_xml.splitlines())
Beispiel #17
0
    def test_configure(self):
        energy = LineGenerator("energy", "kEv", 13.0, 15.2, 2)
        spiral = SpiralGenerator(["x", "y"], ["mm", "mm"], [0., 0.],
                                 5.,
                                 scale=2.0)
        generator = CompoundGenerator([energy, spiral], [], [], 0.1)
        generator.prepare()
        fileDir = "/tmp"
        formatName = "xspress3"
        fileTemplate = "thing-%s.h5"
        completed_steps = 0
        steps_to_do = 38
        part_info = {
            "DET": [NDArrayDatasetInfo(2)],
            "PANDA": [
                NDAttributeDatasetInfo("I0", AttributeDatasetType.DETECTOR,
                                       "COUNTER1.COUNTER", 2),
                NDAttributeDatasetInfo("It", AttributeDatasetType.MONITOR,
                                       "COUNTER2.COUNTER", 2),
                NDAttributeDatasetInfo("t1x", AttributeDatasetType.POSITION,
                                       "INENC1.VAL", 2)
            ],
            "STAT": [CalculatedNDAttributeDatasetInfo("sum", "StatsTotal")],
        }
        infos = self.o.configure(self.context, completed_steps, steps_to_do,
                                 part_info, generator, fileDir, formatName,
                                 fileTemplate)
        assert len(infos) == 8
        assert infos[0].name == "xspress3.data"
        assert infos[0].filename == "thing-xspress3.h5"
        assert infos[0].type == DatasetType.PRIMARY
        assert infos[0].rank == 4
        assert infos[0].path == "/entry/detector/detector"
        assert infos[0].uniqueid == "/entry/NDAttributes/NDArrayUniqueId"

        assert infos[1].name == "xspress3.sum"
        assert infos[1].filename == "thing-xspress3.h5"
        assert infos[1].type == DatasetType.SECONDARY
        assert infos[1].rank == 4
        assert infos[1].path == "/entry/sum/sum"
        assert infos[1].uniqueid == "/entry/NDAttributes/NDArrayUniqueId"

        assert infos[2].name == "I0.data"
        assert infos[2].filename == "thing-xspress3.h5"
        assert infos[2].type == DatasetType.PRIMARY
        assert infos[2].rank == 4
        assert infos[2].path == "/entry/I0/I0"
        assert infos[2].uniqueid == "/entry/NDAttributes/NDArrayUniqueId"

        assert infos[3].name == "It.data"
        assert infos[3].filename == "thing-xspress3.h5"
        assert infos[3].type == DatasetType.MONITOR
        assert infos[3].rank == 4
        assert infos[3].path == "/entry/It/It"
        assert infos[3].uniqueid == "/entry/NDAttributes/NDArrayUniqueId"

        assert infos[4].name == "t1x.value"
        assert infos[4].filename == "thing-xspress3.h5"
        assert infos[4].type == DatasetType.POSITION_VALUE
        assert infos[4].rank == 4
        assert infos[4].path == "/entry/t1x/t1x"
        assert infos[4].uniqueid == "/entry/NDAttributes/NDArrayUniqueId"

        assert infos[5].name == "energy.value_set"
        assert infos[5].filename == "thing-xspress3.h5"
        assert infos[5].type == DatasetType.POSITION_SET
        assert infos[5].rank == 1
        assert infos[5].path == "/entry/detector/energy_set"
        assert infos[5].uniqueid == ""

        assert infos[6].name == "x.value_set"
        assert infos[6].filename == "thing-xspress3.h5"
        assert infos[6].type == DatasetType.POSITION_SET
        assert infos[6].rank == 1
        assert infos[6].path == "/entry/detector/x_set"
        assert infos[6].uniqueid == ""

        assert infos[7].name == "y.value_set"
        assert infos[7].filename == "thing-xspress3.h5"
        assert infos[7].type == DatasetType.POSITION_SET
        assert infos[7].rank == 1
        assert infos[7].path == "/entry/detector/y_set"
        assert infos[7].uniqueid == ""

        expected_xml_filename = "/tmp/BLOCK-HDF5-layout.xml"
        # Wait for the start_future so the post gets through to our child
        # even on non-cothread systems
        self.o.start_future.result(timeout=1)
        assert self.child.handled_requests.mock_calls == [
            call.put('positionMode', True),
            call.put('arrayCounter', 0),
            call.put('dimAttDatasets', True),
            call.put('enableCallbacks', True),
            call.put('fileName', 'xspress3'),
            call.put('filePath', '/tmp/'),
            call.put('fileTemplate', '%sthing-%s.h5'),
            call.put('fileWriteMode', 'Stream'),
            call.put('lazyOpen', True),
            call.put('positionMode', True),
            call.put('swmrMode', True),
            call.put('extraDimSize3', 1),
            call.put('extraDimSize4', 1),
            call.put('extraDimSize5', 1),
            call.put('extraDimSize6', 1),
            call.put('extraDimSize7', 1),
            call.put('extraDimSize8', 1),
            call.put('extraDimSize9', 1),
            call.put('extraDimSizeN', 20),
            call.put('extraDimSizeX', 2),
            call.put('extraDimSizeY', 1),
            call.put('numExtraDims', 1),
            call.put('posNameDim3', ''),
            call.put('posNameDim4', ''),
            call.put('posNameDim5', ''),
            call.put('posNameDim6', ''),
            call.put('posNameDim7', ''),
            call.put('posNameDim8', ''),
            call.put('posNameDim9', ''),
            call.put('posNameDimN', 'd1'),
            call.put('posNameDimX', 'd0'),
            call.put('posNameDimY', ''),
            call.put('flushAttrPerNFrames', 10.0),
            call.put('flushDataPerNFrames', 10.0),
            call.put('xml', expected_xml_filename),
            call.put('numCapture', 0),
            call.post('start')
        ]
        expected_xml = """<?xml version="1.0" ?>
<hdf5_layout>
<group name="entry">
<attribute name="NX_class" source="constant" type="string" value="NXentry" />
<group name="detector">
<attribute name="signal" source="constant" type="string" value="detector" />
<attribute name="axes" source="constant" type="string" value="energy_set,.,.,." />
<attribute name="NX_class" source="constant" type="string" value="NXdata" />
<attribute name="energy_set_indices" source="constant" type="string" value="0" />
<dataset name="energy_set" source="constant" type="float" value="13,15.2">
<attribute name="units" source="constant" type="string" value="kEv" />
</dataset>
<attribute name="x_set_indices" source="constant" type="string" value="1" />
<dataset name="x_set" source="constant" type="float" value="0.473264298891,-1.28806365331,-1.11933765723,0.721339144968,2.26130106714,2.3717213098,1.08574712174,-0.863941392256,-2.59791589857,-3.46951769442,-3.22399679412,-1.98374931946,-0.132541097885,1.83482458567,3.45008680308,4.36998121172,4.42670524204,3.63379270355,2.15784413199,0.269311496406">
<attribute name="units" source="constant" type="string" value="mm" />
</dataset>
<attribute name="y_set_indices" source="constant" type="string" value="1" />
<dataset name="y_set" source="constant" type="float" value="-0.64237113553,-0.500750778455,1.38930992616,1.98393756064,0.784917470231,-1.17377831157,-2.66405897615,-2.9669684623,-2.01825893141,-0.24129368636,1.72477821509,3.27215424484,3.98722048131,3.71781556747,2.5610299588,0.799047653518,-1.18858453138,-3.01284626565,-4.34725663835,-4.9755042398">
<attribute name="units" source="constant" type="string" value="mm" />
</dataset>
<dataset det_default="true" name="detector" source="detector">
<attribute name="NX_class" source="constant" type="string" value="SDS" />
</dataset>
</group>
<group name="sum">
<attribute name="signal" source="constant" type="string" value="sum" />
<attribute name="axes" source="constant" type="string" value="energy_set,.,.,." />
<attribute name="NX_class" source="constant" type="string" value="NXdata" />
<attribute name="energy_set_indices" source="constant" type="string" value="0" />
<hardlink name="energy_set" target="/entry/detector/energy_set" />
<attribute name="x_set_indices" source="constant" type="string" value="1" />
<hardlink name="x_set" target="/entry/detector/x_set" />
<attribute name="y_set_indices" source="constant" type="string" value="1" />
<hardlink name="y_set" target="/entry/detector/y_set" />
<dataset name="sum" ndattribute="StatsTotal" source="ndattribute" />
</group>
<group name="I0">
<attribute name="signal" source="constant" type="string" value="I0" />
<attribute name="axes" source="constant" type="string" value="energy_set,.,.,." />
<attribute name="NX_class" source="constant" type="string" value="NXdata" />
<attribute name="energy_set_indices" source="constant" type="string" value="0" />
<hardlink name="energy_set" target="/entry/detector/energy_set" />
<attribute name="x_set_indices" source="constant" type="string" value="1" />
<hardlink name="x_set" target="/entry/detector/x_set" />
<attribute name="y_set_indices" source="constant" type="string" value="1" />
<hardlink name="y_set" target="/entry/detector/y_set" />
<dataset name="I0" ndattribute="COUNTER1.COUNTER" source="ndattribute" />
</group>
<group name="It">
<attribute name="signal" source="constant" type="string" value="It" />
<attribute name="axes" source="constant" type="string" value="energy_set,.,.,." />
<attribute name="NX_class" source="constant" type="string" value="NXdata" />
<attribute name="energy_set_indices" source="constant" type="string" value="0" />
<hardlink name="energy_set" target="/entry/detector/energy_set" />
<attribute name="x_set_indices" source="constant" type="string" value="1" />
<hardlink name="x_set" target="/entry/detector/x_set" />
<attribute name="y_set_indices" source="constant" type="string" value="1" />
<hardlink name="y_set" target="/entry/detector/y_set" />
<dataset name="It" ndattribute="COUNTER2.COUNTER" source="ndattribute" />
</group>
<group name="t1x">
<attribute name="signal" source="constant" type="string" value="t1x" />
<attribute name="axes" source="constant" type="string" value="energy_set,.,.,." />
<attribute name="NX_class" source="constant" type="string" value="NXdata" />
<attribute name="energy_set_indices" source="constant" type="string" value="0" />
<hardlink name="energy_set" target="/entry/detector/energy_set" />
<attribute name="x_set_indices" source="constant" type="string" value="1" />
<hardlink name="x_set" target="/entry/detector/x_set" />
<attribute name="y_set_indices" source="constant" type="string" value="1" />
<hardlink name="y_set" target="/entry/detector/y_set" />
<dataset name="t1x" ndattribute="INENC1.VAL" source="ndattribute" />
</group>
<group name="NDAttributes" ndattr_default="true">
<attribute name="NX_class" source="constant" type="string" value="NXcollection" />
</group>
</group>
</hdf5_layout>"""
        with open(expected_xml_filename) as f:
            actual_xml = f.read().replace(">", ">\n")
        assert actual_xml.splitlines() == expected_xml.splitlines()
Beispiel #18
0
 def __init__(self, names, units, centre, radius, scale=1.0, alternate_direction=False):
     super(JSpiralGenerator, self).__init__()
     
     self.names = names
     self.generator = SpiralGenerator(names, units, centre, radius, scale, alternate_direction)
     logging.debug(self.generator.to_dict())
 def __init__(self, names, units, centre, radius, scale=1.0, alternate_direction=False):
     super(JSpiralGenerator, self).__init__()
     
     self.names = names
     self.generator = SpiralGenerator(names, units, centre, radius, scale, alternate_direction)
     logging.debug(self.generator.to_dict())
    def configure_and_check_output(self, on_windows=False):
        energy = LineGenerator("energy", "kEv", 13.0, 15.2, 2)
        spiral = SpiralGenerator(["x", "y"], ["mm", "mm"], [0.0, 0.0],
                                 5.0,
                                 scale=2.0)
        generator = CompoundGenerator([energy, spiral], [], [], 0.1)
        generator.prepare()
        fileDir = "/tmp"
        formatName = "xspress3"
        fileTemplate = "thing-%s.h5"
        completed_steps = 0
        steps_to_do = 38
        part_info = {
            "DET": [NDArrayDatasetInfo(2)],
            "PANDA": [
                NDAttributeDatasetInfo.from_attribute_type(
                    "I0", AttributeDatasetType.DETECTOR, "COUNTER1.COUNTER"),
                NDAttributeDatasetInfo.from_attribute_type(
                    "It", AttributeDatasetType.MONITOR, "COUNTER2.COUNTER"),
                NDAttributeDatasetInfo.from_attribute_type(
                    "t1x", AttributeDatasetType.POSITION, "INENC1.VAL"),
            ],
            "STAT": [CalculatedNDAttributeDatasetInfo("sum", "StatsTotal")],
        }
        if on_windows:
            part_info["WINPATH"] = [FilePathTranslatorInfo("Y", "/tmp", "")]
        infos = self.o.on_configure(
            self.context,
            completed_steps,
            steps_to_do,
            part_info,
            generator,
            fileDir,
            formatName,
            fileTemplate,
        )
        assert len(infos) == 8
        assert infos[0].name == "xspress3.data"
        assert infos[0].filename == "thing-xspress3.h5"
        assert infos[0].type == DatasetType.PRIMARY
        assert infos[0].rank == 4
        assert infos[0].path == "/entry/detector/detector"
        assert infos[0].uniqueid == "/entry/NDAttributes/NDArrayUniqueId"

        assert infos[1].name == "xspress3.sum"
        assert infos[1].filename == "thing-xspress3.h5"
        assert infos[1].type == DatasetType.SECONDARY
        assert infos[1].rank == 2
        assert infos[1].path == "/entry/sum/sum"
        assert infos[1].uniqueid == "/entry/NDAttributes/NDArrayUniqueId"

        assert infos[2].name == "I0.data"
        assert infos[2].filename == "thing-xspress3.h5"
        assert infos[2].type == DatasetType.PRIMARY
        assert infos[2].rank == 2
        assert infos[2].path == "/entry/I0.data/I0.data"
        assert infos[2].uniqueid == "/entry/NDAttributes/NDArrayUniqueId"

        assert infos[3].name == "It.data"
        assert infos[3].filename == "thing-xspress3.h5"
        assert infos[3].type == DatasetType.MONITOR
        assert infos[3].rank == 2
        assert infos[3].path == "/entry/It.data/It.data"
        assert infos[3].uniqueid == "/entry/NDAttributes/NDArrayUniqueId"

        assert infos[4].name == "t1x.value"
        assert infos[4].filename == "thing-xspress3.h5"
        assert infos[4].type == DatasetType.POSITION_VALUE
        assert infos[4].rank == 2
        assert infos[4].path == "/entry/t1x.value/t1x.value"
        assert infos[4].uniqueid == "/entry/NDAttributes/NDArrayUniqueId"

        assert infos[5].name == "energy.value_set"
        assert infos[5].filename == "thing-xspress3.h5"
        assert infos[5].type == DatasetType.POSITION_SET
        assert infos[5].rank == 1
        assert infos[5].path == "/entry/detector/energy_set"
        assert infos[5].uniqueid == ""

        assert infos[6].name == "x.value_set"
        assert infos[6].filename == "thing-xspress3.h5"
        assert infos[6].type == DatasetType.POSITION_SET
        assert infos[6].rank == 1
        assert infos[6].path == "/entry/detector/x_set"
        assert infos[6].uniqueid == ""

        assert infos[7].name == "y.value_set"
        assert infos[7].filename == "thing-xspress3.h5"
        assert infos[7].type == DatasetType.POSITION_SET
        assert infos[7].rank == 1
        assert infos[7].path == "/entry/detector/y_set"
        assert infos[7].uniqueid == ""

        expected_xml_filename_local = "/tmp/BLOCK_HDF5-layout.xml"
        if on_windows:
            expected_xml_filename_remote = "Y:\\BLOCK_HDF5-layout.xml"
            expected_filepath = "Y:" + os.sep
        else:
            expected_xml_filename_remote = expected_xml_filename_local
            expected_filepath = "/tmp" + os.sep
        # Wait for the start_future so the post gets through to our child
        # even on non-cothread systems
        self.o.start_future.result(timeout=1)
        assert self.child.handled_requests.mock_calls == [
            call.put("positionMode", True),
            call.put("arrayCounter", 0),
            call.put("dimAttDatasets", True),
            call.put("enableCallbacks", True),
            call.put("fileName", "xspress3"),
            call.put("filePath", expected_filepath),
            call.put("fileTemplate", "%sthing-%s.h5"),
            call.put("fileWriteMode", "Stream"),
            call.put("lazyOpen", True),
            call.put("storeAttr", True),
            call.put("swmrMode", True),
            call.put("extraDimSize3", 1),
            call.put("extraDimSize4", 1),
            call.put("extraDimSize5", 1),
            call.put("extraDimSize6", 1),
            call.put("extraDimSize7", 1),
            call.put("extraDimSize8", 1),
            call.put("extraDimSize9", 1),
            call.put("extraDimSizeN", 20),
            call.put("extraDimSizeX", 2),
            call.put("extraDimSizeY", 1),
            call.put("numExtraDims", 1),
            call.put("posNameDim3", ""),
            call.put("posNameDim4", ""),
            call.put("posNameDim5", ""),
            call.put("posNameDim6", ""),
            call.put("posNameDim7", ""),
            call.put("posNameDim8", ""),
            call.put("posNameDim9", ""),
            call.put("posNameDimN", "d1"),
            call.put("posNameDimX", "d0"),
            call.put("posNameDimY", ""),
            call.put("flushAttrPerNFrames", 0),
            call.put("flushDataPerNFrames", 38),
            call.put("xmlLayout", expected_xml_filename_remote),
            call.put("numCapture", 0),
            call.post("start"),
            call.when_value_matches("arrayCounterReadback", greater_than_zero,
                                    None),
        ]
        with open(expected_xml_filename_local) as f:
            actual_xml = f.read().replace(">", ">\n")
        # Check the layout filename Malcolm uses for file creation
        assert self.o.layout_filename == expected_xml_filename_local
        return actual_xml
 def setUp(self):
     self.g = SpiralGenerator(['x', 'y'], ["cm", "mm"], [0.0, 0.0],
                              1.4,
                              alternate=True)
 def test_duplicate_name_raises(self):
     with self.assertRaises(ValueError):
         SpiralGenerator(["x", "x"], ["mm", "mm"], [0.0, 0.0], 1.0)
class SpiralGeneratorTest(ScanPointGeneratorTest):
    def setUp(self):
        self.g = SpiralGenerator(['x', 'y'], ["cm", "mm"], [0.0, 0.0],
                                 1.4,
                                 alternate=True)

    def test_init(self):
        self.assertEqual(self.g.units, dict(x="cm", y="mm"))
        self.assertEqual(self.g.axes, ["x", "y"])

    def test_duplicate_name_raises(self):
        with self.assertRaises(ValueError):
            SpiralGenerator(["x", "x"], ["mm", "mm"], [0.0, 0.0], 1.0)

    def test_array_positions(self):
        expected_x = [
            0.23663214944574582, -0.6440318266552169, -0.5596688286164636,
            0.36066957248394327, 1.130650533568409, 1.18586065489788,
            0.5428735608675326
        ]
        expected_y = [
            -0.3211855677650875,
            -0.25037538922751695,
            0.6946549630820702,
            0.9919687803189761,
            0.3924587351155914,
            -0.5868891557832875,
            -1.332029488076613,
        ]
        expected_bx = [
            0.0, -0.2214272368007088, -0.7620433832656455,
            -0.13948222773063082, 0.8146440851904461, 1.2582363345925418,
            0.9334439933089926, 0.06839234794968006
        ]
        expected_by = [
            0.0, -0.5189218293602549, 0.23645222432582483, 0.9671992383675001,
            0.7807653675717078, -0.09160107657707395, -1.0190886264001306,
            -1.4911377166541206
        ]
        self.g.prepare_positions()
        self.g.prepare_bounds()
        self.assertListAlmostEqual(expected_x, self.g.positions['x'].tolist())
        self.assertListAlmostEqual(expected_y, self.g.positions['y'].tolist())
        self.assertListAlmostEqual(expected_bx, self.g.bounds['x'].tolist())
        self.assertListAlmostEqual(expected_by, self.g.bounds['y'].tolist())

    def test_to_dict(self):
        expected_dict = dict()
        expected_dict[
            'typeid'] = "scanpointgenerator:generator/SpiralGenerator:1.0"
        expected_dict['axes'] = ['x', 'y']
        expected_dict['units'] = ['cm', 'mm']
        expected_dict['centre'] = [0.0, 0.0]
        expected_dict['radius'] = 1.4
        expected_dict['scale'] = 1
        expected_dict['alternate'] = True

        d = self.g.to_dict()

        self.assertEqual(expected_dict, d)

    def test_from_dict(self):
        _dict = dict()
        _dict['type'] = "SpiralGenerator"
        _dict['axes'] = ["x", "y"]
        _dict['units'] = ["mm", "cm"]
        _dict['centre'] = [0.0, 0.0]
        _dict['radius'] = 1.4
        _dict['scale'] = 1
        _dict['alternate'] = True

        units_dict = dict()
        units_dict['x'] = "mm"
        units_dict['y'] = "cm"

        gen = SpiralGenerator.from_dict(_dict)

        self.assertEqual(["x", "y"], gen.axes)
        self.assertEqual(units_dict, gen.units)
        self.assertEqual([0.0, 0.0], gen.centre)
        self.assertEqual(1.4, gen.radius)
        self.assertEqual(1, gen.scale)
Beispiel #24
0
def spiral_check():

    spiral = SpiralGenerator(['x', 'y'], ["mm", "mm"], [0.0, 0.0], 10.0)
    gen = CompoundGenerator([spiral], [], [])

    plot_generator(gen)