def __init__(self, box_bounds=None, box_pos=None, box_size=None, box_var=None, keep_aspect_ratio=True): super(Boxed, self).__init__(box_var=box_var) self._key_pressed = None self.keep_aspect_ratio = keep_aspect_ratio # Find the box bounds if only the box positions are passed. if box_bounds is None: assert box_pos is not None # This will find a good box size automatically if it is not # specified. box_bounds = _get_boxes(box_pos, size=box_size, keep_aspect_ratio=self.keep_aspect_ratio, margin=self.margin) self._box_bounds = np.atleast_2d(box_bounds) assert self._box_bounds.shape[1] == 4 self.transforms = TransformChain() self.transforms.add_on_gpu( [Scale('u_box_scaling'), Range(NDC, 'box_bounds')], origin=self)
def update_boxes(self, box_pos, box_size): """Set the box bounds from specified box positions and sizes.""" assert box_pos.shape == (self.n_boxes, 2) assert len(box_size) == 2 self.box_bounds = _get_boxes(box_pos, size=box_size, margin=self.margin, keep_aspect_ratio=self.keep_aspect_ratio)
def _get_box_bounds(bunchs, channel_ids): cp = {} for d in bunchs: cp.update( {cid: pos for cid, pos in zip(d.channel_ids, d.channel_positions)}) box_pos = np.stack([cp[cid] for cid in channel_ids]) bounds = _get_boxes(box_pos, margin=Boxed.margin) return bounds
def _iter_channel(positions): size = 100 margin = 5 boxes = _get_boxes(positions, keep_aspect_ratio=False) xmin, ymin = boxes[:, :2].min(axis=0) xmax, ymax = boxes[:, 2:].max(axis=0) x = boxes[:, [0, 2]].mean(axis=1) y = -boxes[:, [1, 3]].mean(axis=1) positions = np.c_[x, y] tr = [margin, margin, size - margin, size - margin] positions_tr = range_transform([-1, -1, 1, 1], tr, positions) for x, y in positions_tr: yield x, y
def test_get_boxes(): positions = [[-1, 0], [1, 0]] boxes = _get_boxes(positions) ac(boxes, [[-1, -.25, 0, .25], [+0, -.25, 1, .25]], atol=1e-4) positions = [[-1, 0], [1, 0]] boxes = _get_boxes(positions, keep_aspect_ratio=False) ac(boxes, [[-1, -1, 0, 1], [0, -1, 1, 1]], atol=1e-4) positions = linear_positions(4) boxes = _get_boxes(positions) ac(boxes, [[-0.5, -1.0, +0.5, -0.5], [-0.5, -0.5, +0.5, +0.0], [-0.5, +0.0, +0.5, +0.5], [-0.5, +0.5, +0.5, +1.0], ], atol=1e-4) positions = staggered_positions(8) boxes = _get_boxes(positions) ac(boxes[:, 1], np.arange(.75, -1.1, -.25), atol=1e-6) ac(boxes[:, 3], np.arange(1, -.76, -.25), atol=1e-7)
def _get_pos_data_bounds(positions): boxes = _get_boxes(positions, keep_aspect_ratio=False) xmin, ymin = boxes[:, :2].min(axis=0) xmax, ymax = boxes[:, 2:].max(axis=0) x = boxes[:, [0, 2]].mean(axis=1) y = boxes[:, [1, 3]].mean(axis=1) positions = np.c_[x, y] # x, y = positions.T # xmin, ymin, xmax, ymax = x.min(), y.min(), x.max(), y.max() w = xmax - xmin h = ymax - ymin k = .05 data_bounds = (xmin - w * k, ymin - h * k, xmax + w * k, ymax + h * k) return positions, data_bounds
def box_size(self, val): assert len(val) == 2 self.box_bounds = _get_boxes(self.box_pos, size=val, margin=self.margin, keep_aspect_ratio=self.keep_aspect_ratio)
def box_pos(self, val): self.box_bounds = _get_boxes(val, size=self.box_size, margin=self.margin, keep_aspect_ratio=self.keep_aspect_ratio)