Example #1
0
    def test_successive_merges(self):
        g1, g2, h1, h2 = Mock(), Mock(), Mock(), Mock()
        g1.axes, g2.axes = ["g1x"], ["g2x", "g2y"]
        g1.positions = {"g1x": np.array([0, 1, 2, 3, 4])}
        g2.positions = {
            "g2x": np.array([10, 11, 12, 13, 14, 15, 16]),
            "g2y": np.array([-10, -11, -12, -13, -14, -15, -16])
        }
        h1.axes, h2.axes = ["h1x", "h1y"], ["h2x"]
        h1.positions = {
            "h1x": np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]),
            "h1y": np.array([1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21])
        }
        h2.positions = {
            "h2x": np.array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 9])
        }
        g1.size, g2.size = 5, 7
        h1.size, h2.size = 11, 13
        g2mask = Mock()
        h1mask = Mock()
        dg1, dg2 = Dimension(g1), Dimension(g2)
        dh1, dh2 = Dimension(h1), Dimension(h2)
        dg2._masks = [{"repeat": 1, "tile": 1, "mask": g2mask}]
        dh1._masks = [{"repeat": 1, "tile": 1, "mask": h1mask}]

        outer = Dimension.merge_dimensions([dg1, dg2])
        inner = Dimension.merge_dimensions([dh1, dh2])
        self.assertEqual(5 * 7, outer._max_length)
        self.assertEqual(11 * 13, inner._max_length)
        self.assertEqual([{
            "repeat": 1,
            "tile": 5,
            "mask": g2mask
        }], outer._masks)
        self.assertEqual([{
            "repeat": 13,
            "tile": 1,
            "mask": h1mask
        }], inner._masks)
        self.assertEqual([4, 16, -10], outer.upper)
        self.assertEqual([0, 10, -16], outer.lower)
        self.assertEqual([11, 21, 9], inner.upper)
        self.assertEqual([1, 1, 0], inner.lower)
        combined = Dimension.merge_dimensions([outer, inner])

        expected_masks = [{
            "repeat": 11 * 13,
            "tile": 5,
            "mask": g2mask
        }, {
            "repeat": 13,
            "tile": 5 * 7,
            "mask": h1mask
        }]
        self.assertEqual(expected_masks, combined._masks)
        self.assertEqual(5 * 7 * 11 * 13, combined._max_length)
        self.assertEqual(["g1x", "g2x", "g2y", "h1x", "h1y", "h2x"],
                         combined.axes)
        self.assertEqual([4, 16, -10, 11, 21, 9], combined.upper)
        self.assertEqual([0, 10, -16, 1, 1, 0], combined.lower)
Example #2
0
    def test_merge_three(self):
        g1, g2, g3 = Mock(), Mock(), Mock()
        g1.axes, g2.axes, g3.axes = ["g1"], ["g2"], ["g3"]
        g1.size, g2.size, g3.size = 3, 4, 5
        g1.positions = {"g1":np.array([0, 1, 2])}
        g2.positions = {"g2":np.array([-1, 0, 1, 2])}
        g3.positions = {"g3":np.array([-2, 0, 2, 4, 6])}
        d1, d2, d3 = Dimension(g1), Dimension(g2), Dimension(g3)
        d1_m, d2_m, d3_m = Mock(), Mock(), Mock()
        d1._masks = [{"repeat":1, "tile":1, "mask":d1_m}]
        d2._masks = [{"repeat":2, "tile":3, "mask":d2_m}]
        d3._masks = [{"repeat":5, "tile":7, "mask":d3_m}]

        combined = Dimension.merge_dimensions([d1, d2, d3])

        self.assertEqual(60, combined._max_length)
        self.assertEqual(g1.alternate or g2.alternate or g3.alternate, combined.alternate)
        self.assertEqual(["g1", "g2", "g3"], combined.axes)
        self.assertEqual([0, -1, -2], combined.lower)
        self.assertEqual([2, 2, 6], combined.upper)

        expected_masks = [
                {"repeat":20, "tile":1, "mask":d1_m},
                {"repeat":10, "tile":9, "mask":d2_m},
                {"repeat":5, "tile":84, "mask":d3_m}]
        self.assertEqual(expected_masks, combined._masks)
Example #3
0
    def test_high_dimensional_excluder(self):
        w_pos = np.array([0, 1, 2, 3, 4, 5])
        x_pos = np.array([0, 1, 2, 3, 4, 5])
        y_pos = np.array([0, 1, 2, 3, 4, 5])
        z_pos = np.array([0, 1, 2, 3, 4, 5])
        mask_function = lambda pw, px, py, pz: (pw-2)**2 + (px-2)**2 + (py-1)**2 + (pz-3)**2 <= 1.1
        excluder = Mock(axes=["w", "x", "y", "z"], create_mask=Mock(side_effect=mask_function))
        gw = Mock(axes=["w"], positions={"w":w_pos}, size=len(w_pos), alternate=False)
        gx = Mock(axes=["x"], positions={"x":x_pos}, size=len(x_pos), alternate=False)
        gy = Mock(axes=["y"], positions={"y":y_pos}, size=len(y_pos), alternate=False)
        gz = Mock(axes=["z"], positions={"z":z_pos}, size=len(z_pos), alternate=False)
        d = Dimension.merge_dimensions([Dimension(gz), Dimension(gy), Dimension(gx), Dimension(gw)])

        d.apply_excluder(excluder)
        d.prepare()

        w_positions = np.tile(w_pos, len(x_pos) * len(y_pos) * len(z_pos))
        x_positions = np.repeat(np.tile(x_pos, len(y_pos) * len(z_pos)), len(w_pos))
        y_positions = np.repeat(np.tile(y_pos, len(z_pos)), len(w_pos) * len(x_pos))
        z_positions = np.repeat(z_pos, len(w_pos) * len(x_pos) * len(y_pos))
        mask = mask_function(w_positions, x_positions, y_positions, z_positions)
        w_expected = w_positions[mask].tolist()
        x_expected = x_positions[mask].tolist()
        y_expected = y_positions[mask].tolist()
        z_expected = z_positions[mask].tolist()

        self.assertEqual(w_expected, d.get_positions("w").tolist())
        self.assertEqual(x_expected, d.get_positions("x").tolist())
        self.assertEqual(y_expected, d.get_positions("y").tolist())
        self.assertEqual(z_expected, d.get_positions("z").tolist())
Example #4
0
    def test_spread_excluder_multi_axes_per_gen(self):
        gx1_pos = np.array([1, 2, 3, 4, 5])
        gx2_pos = np.array([11, 10, 9, 8, 7])
        gy_pos = np.array([-1, 0, 1])
        gz_pos = np.array([1, 0, -1, -2, -3])
        mask_x1z_func = lambda px, pz: (px-4)**2 + (pz+1)**2 <= 1
        exz = Mock(axes=["gx1", "gz"], create_mask=Mock(side_effect=mask_x1z_func))
        gx = Mock(axes=["gx1", "gx2"], positions={"gx1":gx1_pos, "gx2":gx2_pos}, size=5, alternate=False)
        gy = Mock(axes=["gy"], positions={"gy":gy_pos}, size=3, alternate=False)
        gz = Mock(axes=["gz"], positions={"gz":gz_pos}, size=5, alternate=False)
        d = Dimension.merge_dimensions([Dimension(gz), Dimension(gy), Dimension(gx)])

        d.apply_excluder(exz)
        d.prepare()

        x1_positions = np.tile(gx1_pos, 15)
        x2_positions = np.tile(gx2_pos, 15)
        y_positions = np.repeat(np.tile(gy_pos, 5), 5)
        z_positions = np.repeat(gz_pos, 15)

        mask = mask_x1z_func(x1_positions, z_positions)
        expected_x1 = x1_positions[mask].tolist()
        expected_x2 = x2_positions[mask].tolist()
        expected_y = y_positions[mask].tolist()
        expected_z = z_positions[mask].tolist()

        self.assertEqual(expected_x1, d.get_positions("gx1").tolist())
        self.assertEqual(expected_x2, d.get_positions("gx2").tolist())
        self.assertEqual(expected_y, d.get_positions("gy").tolist())
        self.assertEqual(expected_z, d.get_positions("gz").tolist())
Example #5
0
    def test_merge_dimensions(self):
        g, h = Mock(), Mock()
        g.axes, h.axes = ["gx", "gy"], ["hx", "hy"]
        g.size, h.size = 16, 64
        g.positions = {"gx":np.array([0, 1, 2]), "gy":np.array([10, 11, 12])}
        h.positions = {"hx":np.array([0, -1, -2]), "hy":np.array([-10, -11, -12])}
        outer, inner = Dimension(g), Dimension(h)
        om1, om2 = Mock(), Mock()
        im1, im2 = Mock(), Mock()
        outer._masks = [{"repeat":2, "tile":3, "mask":om1},
            {"repeat":5, "tile":7, "mask":om2}]
        inner._masks = [{"repeat":11, "tile":13, "mask":im1},
            {"repeat":17, "tile":19, "mask":im2}]
        combined = Dimension.merge_dimensions([outer, inner])

        self.assertEqual(g.size * h.size, combined._max_length)
        self.assertEqual(outer.alternate or inner.alternate, combined.alternate)
        self.assertEqual(["gx", "gy", "hx", "hy"], combined.axes)
        self.assertEqual([2, 12, 0, -10], combined.upper)
        self.assertEqual([0, 10, -2, -12], combined.lower)
        expected_masks = [
            {"repeat":128, "tile":3, "mask":om1},
            {"repeat":320, "tile":7, "mask":om2},
            {"repeat":11, "tile":13*16, "mask":im1},
            {"repeat":17, "tile":19*16, "mask":im2}]
        self.assertEqual(expected_masks, combined._masks)
Example #6
0
 def test_get_positions_with_alternating(self):
     gx_pos = np.array([0, 1, 2, 3])
     gy_pos = np.array([0, 1, 2, 3])
     gz_pos = np.array([0, 1, 2, 3])
     mask_xy_func = lambda px, py: (px-1)**2 + (py-2)**2 <= 2
     mask_yz_func = lambda py, pz: (py-2)**2 + (pz-1)**2 <= 1
     exy = Mock(axes=["gx", "gy"], create_mask=Mock(side_effect=mask_xy_func))
     eyz = Mock(axes=["gy", "gz"], create_mask=Mock(side_effect=mask_yz_func))
     gx = Mock(axes=["gx"], positions={"gx":gx_pos}, size=4, alternate=True)
     gy = Mock(axes=["gy"], positions={"gy":gy_pos}, size=4, alternate=True)
     gz = Mock(axes=["gz"], positions={"gz":gz_pos}, size=4, alternate=True)
     dx = Dimension(gx)
     dy = Dimension(gy)
     dz = Dimension(gz)
     d = Dimension.merge_dimensions([dz, dy, dx])
     d.apply_excluder(exy)
     d.apply_excluder(eyz)
     d.prepare()
     self.assertEqual(15, d.size)
     self.assertEqual(
         [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2],
         d.get_positions("gz").tolist())
     self.assertEqual(
         [2, 2, 2, 3, 3, 3, 2, 2, 2, 1, 1, 1, 2, 2, 2],
         d.get_positions("gy").tolist())
     self.assertEqual(
         [0, 1, 2, 0, 1, 2, 2, 1, 0, 0, 1, 2, 0, 1, 2],
         d.get_positions("gx").tolist())
Example #7
0
    def test_excluder_over_spread_axes(self):
        gw_pos = np.array([0.1, 0.2])
        gx_pos = np.array([0, 1, 2, 3])
        gy_pos = np.array([10, 11, 12, 13])
        gz_pos = np.array([100, 101, 102, 103])
        go_pos = np.array([1000, 1001, 1002])
        mask_xz_func = lambda px, pz: (px - 1)**2 + (pz - 102)**2 <= 1
        exz = Mock(axes=["gx", "gz"],
                   create_mask=Mock(side_effect=mask_xz_func))
        gw = Mock(axes=["gw"],
                  positions={"gw": gw_pos},
                  size=2,
                  alternate=False)
        gx = Mock(axes=["gx"],
                  positions={"gx": gx_pos},
                  size=4,
                  alternate=False)
        gy = Mock(axes=["gy"],
                  positions={"gy": gy_pos},
                  size=4,
                  alternate=False)
        gz = Mock(axes=["gz"],
                  positions={"gz": gz_pos},
                  size=4,
                  alternate=False)
        go = Mock(axes=["go"],
                  positions={"go": go_pos},
                  size=3,
                  alternate=False)
        dw = Dimension(gw)
        dx = Dimension(gx)
        dy = Dimension(gy)
        dz = Dimension(gz)
        do = Dimension(go)
        d = Dimension.merge_dimensions([do, dz, dy, dx, dw])

        d.apply_excluder(exz)
        d.prepare()

        x_positions = np.tile(np.array([0, 1, 2, 3]), 16)
        y_positions = np.repeat(np.tile(np.array([10, 11, 12, 13]), 4), 4)
        z_positions = np.repeat(np.array([100, 101, 102, 103]), 16)
        x_positions = np.tile(np.repeat(x_positions, gw.size), go.size)
        y_positions = np.tile(np.repeat(y_positions, gw.size), go.size)
        z_positions = np.tile(np.repeat(z_positions, gw.size), go.size)

        mask = mask_xz_func(x_positions, z_positions)
        expected_x = x_positions[mask].tolist()
        expected_y = y_positions[mask].tolist()
        expected_z = z_positions[mask].tolist()

        self.assertEqual(expected_x, d.get_positions("gx").tolist())
        self.assertEqual(expected_y, d.get_positions("gy").tolist())
        self.assertEqual(expected_z, d.get_positions("gz").tolist())
Example #8
0
 def test_get_positions_after_merge(self):
     gx_pos = np.array([0, 1, 2, 3])
     gy_pos = np.array([0, 1, 2, 3])
     mask_func = lambda px, py: (px-1)**2 + (py-2)**2 <= 1
     gx = Mock(axes=["gx"], positions={"gx":gx_pos}, size=4, alternate=False)
     gy = Mock(axes=["gy"], positions={"gy":gy_pos}, size=4, alternate=False)
     e = Mock(axes=["gx", "gy"], create_mask=Mock(side_effect=mask_func))
     dx = Dimension(gx)
     dy = Dimension(gy)
     d = Dimension.merge_dimensions([dy, dx])
     d.apply_excluder(e)
     d.prepare()
     self.assertEqual([1, 0, 1, 2, 1], d.get_positions("gx").tolist())
     self.assertEqual([1, 2, 2, 2, 3], d.get_positions("gy").tolist())
Example #9
0
    def test_get_mesh_map(self):
        # Set up a generator, with 3x4 grid with alternating x and a circular
        # excluder such that the four 'corners' of the grid are excluded
        gx_pos = np.array([0.1, 0.2, 0.3])
        gy_pos = np.array([1.1, 1.2, 1.3, 1.4])
        mask_func = lambda px, py: (px - 0.2) ** 2 + (py - 1.25) ** 2 <= 0.0225
        gx = Mock(axes=["gx"], positions={"gx": gx_pos}, size=3,
                  alternate=True)
        gy = Mock(axes=["gy"], positions={"gy": gy_pos}, size=4,
                  alternate=False)
        e = Mock(axes=["gx", "gy"], create_mask=Mock(side_effect=mask_func))

        dx = Dimension(gx)
        dy = Dimension(gy)
        d = Dimension.merge_dimensions([dy, dx])
        d.apply_excluder(e)
        d.prepare()

        self.assertEqual([1, 2, 1, 0, 0, 1, 2, 1],
                         d.get_mesh_map("gx").tolist())
        self.assertEqual([0, 1, 1, 1, 2, 2, 2, 3],
                         d.get_mesh_map("gy").tolist())
Example #10
0
    def prepare(self):
        """
        Prepare data structures required for point generation and
        initialize size, shape, and dimensions attributes.
        Must be called before get_point or iterator are called.
        """
        if self._prepared:
            return
        self.dimensions = []
        self._dim_meta = {}
        self._generator_dim_scaling = {}

        # we're going to mutate these structures
        excluders = list(self.excluders)
        generators = list(self.generators)

        # special case if we have rectangular regions on line generators
        # we should restrict the resulting grid rather than merge dimensions
        # this changes the alternating case a little (without doing this, we
        # may have started in reverse direction)
        for excluder_ in [e for e in excluders if isinstance(e, ROIExcluder)]:
            if len(excluder_.rois) == 1 \
                    and isinstance(excluder_.rois[0], RectangularROI) \
                    and excluder_.rois[0].angle == 0:
                rect = excluder_.rois[0]
                axis_1, axis_2 = excluder_.axes[0], excluder_.axes[1]
                gen_1 = [g for g in generators if axis_1 in g.axes][0]
                gen_2 = [g for g in generators if axis_2 in g.axes][0]
                if gen_1 is gen_2:
                    continue
                if isinstance(gen_1, LineGenerator) \
                        and isinstance(gen_2, LineGenerator):
                    gen_1.prepare_positions()
                    gen_2.prepare_positions()
                    # Filter by axis 1
                    valid = np.full(gen_1.size, True, dtype=np.int8)
                    valid &= \
                        gen_1.positions[axis_1] <= rect.width + rect.start[0]
                    valid &= \
                        gen_1.positions[axis_1] >= rect.start[0]
                    points_1 = gen_1.positions[axis_1][valid.astype(np.bool)]
                    # Filter by axis 2
                    valid = np.full(gen_2.size, True, dtype=np.int8)
                    valid &= \
                        gen_2.positions[axis_2] <= rect.height + rect.start[1]
                    valid &= gen_2.positions[axis_2] >= rect.start[1]
                    points_2 = gen_2.positions[axis_2][valid.astype(np.bool)]
                    # Recreate generators to replace larger generators + ROI
                    new_gen1 = LineGenerator(gen_1.axes, gen_1.units,
                                             points_1[0], points_1[-1],
                                             len(points_1), gen_1.alternate)
                    new_gen2 = LineGenerator(gen_2.axes, gen_2.units,
                                             points_2[0], points_2[-1],
                                             len(points_2), gen_2.alternate)
                    generators[generators.index(gen_1)] = new_gen1
                    generators[generators.index(gen_2)] = new_gen2
                    # Remove Excluder as it is now empty
                    excluders.remove(excluder_)

        for generator in generators:
            generator.prepare_positions()
            self.dimensions.append(Dimension(generator))
        # only the inner-most generator needs to have bounds calculated
        if self.continuous:
            generators[-1].prepare_bounds()

        for excluder in excluders:
            matched_dims = [
                d for d in self.dimensions
                if len(set(d.axes) & set(excluder.axes)) != 0
            ]
            if len(matched_dims) == 0:
                raise ValueError(
                    "Excluder references axes that have not been provided by generators: %s"
                    % str(excluder.axes))
            d_start = self.dimensions.index(matched_dims[0])
            d_end = self.dimensions.index(matched_dims[-1])
            if d_start != d_end:
                # merge all excluders between d_start and d_end (inclusive)
                alternate = self.dimensions[d_end].alternate
                # verify consistent alternate settings (ignoring outermost dimesion where it doesn't matter)
                for d in self.dimensions[max(1, d_start):d_end]:
                    # filter out dimensions consisting of a single NullPointGenerator, since alternation means nothing
                    if len(d.generators) == 1 and isinstance(
                            d.generators[0], NullPointGenerator):
                        continue
                    if alternate != d.alternate:
                        raise ValueError(
                            "Nested generators connected by regions must have the same alternate setting"
                        )
                merged_dim = Dimension.merge_dimensions(
                    self.dimensions[d_start:d_end + 1])
                self.dimensions = self.dimensions[:d_start] + [
                    merged_dim
                ] + self.dimensions[d_end + 1:]
                dim = merged_dim
            else:
                dim = self.dimensions[d_start]
            dim.apply_excluder(excluder)

        self.size = 1
        for dim in self.dimensions:
            self._dim_meta[dim] = {}
            dim.prepare()
            if dim.size == 0:
                raise ValueError("Regions would exclude entire scan")
            self.size *= dim.size

        self.shape = tuple(dim.size for dim in self.dimensions)
        repeat = self.size
        tile = 1
        for dim in self.dimensions:
            repeat /= dim.size
            self._dim_meta[dim]["tile"] = tile
            self._dim_meta[dim]["repeat"] = repeat
            tile *= dim.size

        for dim in self.dimensions:
            tile = 1
            repeat = dim._max_length
            for g in dim.generators:
                repeat /= g.size
                d = {"tile": tile, "repeat": repeat}
                tile *= g.size
                self._generator_dim_scaling[g] = d

        self._prepared = True
Example #11
0
    def prepare(self):
        """
        Prepare data structures required for point generation and
        initialize size, shape, and dimensions attributes.
        Must be called before get_point or iterator are called.
        """
        if self._prepared:
            return
        self.dimensions = []
        self._dim_meta = {}
        self._generator_dim_scaling = {}

        # we're going to mutate these structures
        excluders = list(self.excluders)
        generators = list(self.generators)

        # special case if we have rectangular regions on line generators
        # we should restrict the resulting grid rather than merge dimensions
        # this changes the alternating case a little (without doing this, we
        # may have started in reverse direction)
        for excluder_ in [e for e in excluders if isinstance(e, ROIExcluder)]:
            if len(excluder_.rois) == 1 \
                    and isinstance(excluder_.rois[0], RectangularROI) \
                    and excluder_.rois[0].angle == 0:
                rect = excluder_.rois[0]
                axis_1, axis_2 = excluder_.axes[0], excluder_.axes[1]
                gen_1 = [g for g in generators if axis_1 in g.axes][0]
                gen_2 = [g for g in generators if axis_2 in g.axes][0]
                if gen_1 is gen_2:
                    continue
                if isinstance(gen_1, LineGenerator) \
                        and isinstance(gen_2, LineGenerator):
                    gen_1.prepare_positions()
                    gen_2.prepare_positions()
                    # Filter by axis 1
                    valid = np.full(gen_1.size, True, dtype=np.int8)
                    valid &= \
                        gen_1.positions[axis_1] <= rect.width + rect.start[0]
                    valid &= \
                        gen_1.positions[axis_1] >= rect.start[0]
                    points_1 = gen_1.positions[axis_1][valid.astype(np.bool)]
                    # Filter by axis 2
                    valid = np.full(gen_2.size, True, dtype=np.int8)
                    valid &= \
                        gen_2.positions[axis_2] <= rect.height + rect.start[1]
                    valid &= gen_2.positions[axis_2] >= rect.start[1]
                    points_2 = gen_2.positions[axis_2][valid.astype(np.bool)]
                    # Recreate generators to replace larger generators + ROI
                    new_gen1 = LineGenerator(gen_1.axes, gen_1.units,
                                             points_1[0], points_1[-1],
                                             len(points_1), gen_1.alternate)
                    new_gen2 = LineGenerator(gen_2.axes, gen_2.units,
                                             points_2[0], points_2[-1],
                                             len(points_2), gen_2.alternate)
                    generators[generators.index(gen_1)] = new_gen1
                    generators[generators.index(gen_2)] = new_gen2
                    # Remove Excluder as it is now empty
                    excluders.remove(excluder_)

        for generator in generators:
            generator.prepare_positions()
            self.dimensions.append(Dimension(generator))
        # only the inner-most generator needs to have bounds calculated
        generators[-1].prepare_bounds()

        for excluder in excluders:
            axis_1, axis_2 = excluder.axes
            gen_1 = [g for g in generators if axis_1 in g.axes][0]
            gen_2 = [g for g in generators if axis_2 in g.axes][0]
            gen_diff = generators.index(gen_1) \
                - generators.index(gen_2)
            if gen_diff < -1 or gen_diff > 1:
                raise ValueError(
                    "Excluders must be defined on axes that are adjacent in " \
                        "generator order")

            # merge dimensions if region spans two
            dim_1 = [i for i in self.dimensions if axis_1 in i.axes][0]
            dim_2 = [i for i in self.dimensions if axis_2 in i.axes][0]
            dim_diff = self.dimensions.index(dim_1) \
                - self.dimensions.index(dim_2)
            if dim_diff == 1:
                dim_1, dim_2 = dim_2, dim_1
                dim_diff = -1
            if dim_1.alternate != dim_2.alternate \
                    and dim_1 is not self.dimensions[0]:
                raise ValueError(
                    "Generators tied by regions must have the same " \
                            "alternate setting")
            # merge "inner" into "outer"
            if dim_diff == -1:
                # dim_1 is "outer" - preserves axis ordering
                new_dim = Dimension.merge_dimensions(dim_1, dim_2)
                self.dimensions[self.dimensions.index(dim_1)] = new_dim
                self.dimensions.remove(dim_2)
                dim = new_dim
            else:
                dim = dim_1

            dim.apply_excluder(excluder)

        self.size = 1
        for dim in self.dimensions:
            self._dim_meta[dim] = {}
            dim.prepare()
            if dim.size == 0:
                raise ValueError("Regions would exclude entire scan")
            self.size *= dim.size

        self.shape = tuple(dim.size for dim in self.dimensions)
        repeat = self.size
        tile = 1
        for dim in self.dimensions:
            repeat /= dim.size
            self._dim_meta[dim]["tile"] = tile
            self._dim_meta[dim]["repeat"] = repeat
            tile *= dim.size

        for dim in self.dimensions:
            tile = 1
            repeat = dim._max_length
            for g in dim.generators:
                repeat /= g.size
                d = {"tile": tile, "repeat": repeat}
                tile *= g.size
                self._generator_dim_scaling[g] = d

        self._prepared = True