def normal_mouse_move(self, event): # over a hole ca = self.calibration_item if ca: for obj in self.mp.sample_holes: hole = obj.id pos = obj.x, obj.y rot = ca.rotation cpos = ca.center aff = AffineTransform() aff.translate(*cpos) aff.rotate(rot) aff.translate(-cpos[0], -cpos[1]) aff.translate(*cpos) dpos = aff.transformPt(pos) pos = self.map_screen([dpos])[0] if abs(pos[0] - event.x) <= 10 and abs(pos[1] - event.y) <= 10: event.window.set_pointer(self.select_pointer) event.handled = True self.current_hole = hole break if not event.handled: self.current_hole = None super(MapCanvas, self).normal_mouse_move(event)
def normal_left_down(self, event): super(MapCanvas, self).normal_left_down(event) if self.current_hole is not None: # and not event.handled ca = self.calibration_item if ca is not None: if hasattr(event, 'item'): if hasattr(ca, 'right'): if event.item.right is ca.right: return rot = ca.rotation cpos = ca.center aff = AffineTransform() aff.translate(*cpos) aff.rotate(rot) aff.translate(-cpos[0], -cpos[1]) aff.translate(*cpos) mpos = self.mp.get_hole_pos(self.current_hole) # dpos = aff.transformPt(mpos) dpos = aff.transform(*mpos) spos = self.map_data((event.x, event.y)) # not much point in adding an indicator because the hole # renders its own # self.markupdict['tweak'] = Indicator(*spos, canvas = self) tweak = spos[0] - dpos[0], spos[1] - dpos[1] ca.tweak_dict[self.current_hole] = tweak self.request_redraw()
def _calculate_affine_transform(self, pts): rps, ps = zip(*pts) s, r, t = calculate_rigid_transform(rps, ps) self.A = AffineTransform() self.A.scale(s, s) self.A.rotate(r) self.A.translate(-t[0], -t[1]) print self.A
def map_to_uncalibration(self, pos, cpos=None, rot=None, scale=None): cpos, rot, scale = self._get_calibration_params(cpos, rot, scale) a = AffineTransform() a.scale(1 / scale, 1 / scale) a.rotate(-rot) a.translate(cpos[0], cpos[1]) # a.translate(-cpos[0], -cpos[1]) # a.translate(*cpos) # a.rotate(-rot) # a.translate(-cpos[0], -cpos[1]) pos = a.transform(*pos) return pos
def raster_rubberband_pattern(cx, cy, offset, l, dx, rotation, single_pass): a = AffineTransform() a.translate(cx, cy) a.rotate(rotation) a.translate(-cx, -cy) # print offset, l n = int((l + 2 * offset) / dx) if n*dx<=l+2*offset: n = n+1 if n%2 else n dx = (l+2*offset)/float(n+1) n = int((l + 2 * offset) / dx) for i in xrange(0, n+1): y = cy - offset if i % 2 else cy + offset yield a.transform(cx - offset + dx * i, y) if not single_pass: for i in xrange(0, n+1): y = cy - offset if i % 2 else cy + offset yield a.transform(cx +l+offset - dx * i, y) yield a.transform(cx-offset, cy+offset)
def rubberband_pattern(cx, cy, offset, l, rotation): p1 = cx - offset, cy + offset p2 = cx + l + offset, cy + offset p3 = cx + l + offset, cy - offset p4 = cx - offset, cy - offset a = AffineTransform() a.translate(cx, cy) a.rotate(rotation) a.translate(-cx, -cy) ps = (p1, p2, p3, p4, p1) for p in ps: yield a.transform(*p)
def line_pattern(cx, cy, length, rotation, n): p1 = (cx, cy) p2 = (cx + length, cy) for i in xrange(n): a = AffineTransform() a.translate(cx, cy) a.rotate(rotation) a.translate(-cx, -cy) if i % 2 == 0: ps = (p1, p2) else: ps = (p2, p1) for x, y in ps: yield a.transform(x, y)
def trough_pattern(cx, cy, length, width, rotation, use_x): """ 1 -------------- 2 | | 4 -------------- 3 """ p1 = (cx, cy) p2 = (cx + length, cy) p3 = (cx + length, cy - width) p4 = (cx, cy - width) a = AffineTransform() a.translate(cx, cy) a.rotate(rotation) a.translate(-cx, -cy) if use_x: ps = (p1, p2, p4, p3, p1) else: ps = (p1, p2, p3, p4, p1) for p in ps: yield a.transform(*p)
class UVAnalysisImage(ImageViewer): open_button = Button define_points = Button define_points_label = String _defining_points = False #=============================================================================== # handlers #=============================================================================== def _set_reference_point(self, pt): ''' assumes no rotation of the reference frames only scale and translate ''' rp = ReferencePoint(pt) info = rp.edit_traits() if info.result: plot = self.plot self.points.append(((rp.x, rp.y), pt)) # if not self.reference_pt1: # self.reference_pt1 = (rp.x, rp.y), pt # else: # # calculate bounds # dp1, sp1 = self.reference_pt1 # dp2, sp2 = (rp.x, rp.y), pt # # # w = plot.width # h = plot.height # if sp1[0] < sp2[0]: # sx1, sx2 = sp1[0], sp2[0] # x1, x2 = dp1[0], dp2[0] # else: # sx2, sx1 = sp1[0], sp2[0] # x2, x1 = dp1[0], dp2[0] # # if sp1[1] < sp2[1]: # sy1, sy2 = sp1[1], sp2[1] # y1, y2 = dp1[1], dp2[1] # else: # sy2, sy1 = sp1[1], sp2[1] # y2, y1 = dp1[1], dp2[1] # # pxperunit = abs((sx2 - sx1) / (x2 - x1)) # # li = x1 - sx1 / pxperunit # hi = x2 + (w - sx2) / pxperunit # lv = y1 - sy1 / pxperunit # hv = y2 + (h - sy2) / pxperunit # # plot.index_range.low_setting = li # plot.index_range.high_setting = hi # plot.value_range.low_setting = lv # plot.value_range.high_setting = hv # plot.request_redraw() def _define_points_fired(self): if self.plot: plot = self.plot if not self._defining_points: self.points = [] st = ReferencePointsTool(plot) st.on_trait_change(self._set_reference_point, 'current_position') self.points_tool = st plot.tools.insert(0, st) self.define_points_label = 'Finish' plot.overlays.append(ReferencePointsOverlay(tool=st, component=plot)) else: self.define_points_label = 'Define Points' self.points_tool.on_trait_change(self._set_reference_point, 'current_position', remove=True) plot.tools.pop(0) plot.overlays.pop(-1) self._calculate_affine_transform(self.points) self._defining_points = not self._defining_points def load_points(self, path): points = [(100, 100), (300, 400), (200, 300) ] if os.path.isfile(path): points = loadtxt(path, delimiter=',') po = PointOverlay(component=self.plot, points=points ) self.plot.overlays.append(po) def _calculate_affine_transform(self, pts): rps, ps = zip(*pts) s, r, t = calculate_rigid_transform(rps, ps) self.A = AffineTransform() self.A.scale(s, s) self.A.rotate(r) self.A.translate(-t[0], -t[1]) print self.A def traits_view(self): image = Item('container', style='custom', show_label=False, editor=ComponentEditor() ) v = View( Item('open_button', show_label=False), Item('define_points', enabled_when='plot', editor=ButtonEditor(label_value='define_points_label'), show_label=False), image, resizable=True ) return v
def map_to_calibration(self, pos, cpos=None, rot=None, use_modified=False, scale=None, translate=None): cpos, rot, scale = self._get_calibration_params(cpos, rot, scale) a = AffineTransform() # if translate: # a.translate(*translate) # if scale: a.scale(scale, scale) if use_modified: a.translate(*cpos) # print cpos, rot, scale a.rotate(rot) a.translate(-cpos[0], -cpos[1]) if use_modified: a.translate(*cpos) pos = a.transform(*pos) return pos
class UVAnalysisImage(ImageViewer): open_button = Button define_points = Button define_points_label = String _defining_points = False # =============================================================================== # handlers # =============================================================================== def _set_reference_point(self, pt): ''' assumes no rotation of the reference frames only scale and translate ''' rp = ReferencePoint(pt) info = rp.edit_traits() if info.result: plot = self.plot self.points.append(((rp.x, rp.y), pt)) # if not self.reference_pt1: # self.reference_pt1 = (rp.x, rp.y), pt # else: # # calculate bounds # dp1, sp1 = self.reference_pt1 # dp2, sp2 = (rp.x, rp.y), pt # # # w = plot.width # h = plot.height # if sp1[0] < sp2[0]: # sx1, sx2 = sp1[0], sp2[0] # x1, x2 = dp1[0], dp2[0] # else: # sx2, sx1 = sp1[0], sp2[0] # x2, x1 = dp1[0], dp2[0] # # if sp1[1] < sp2[1]: # sy1, sy2 = sp1[1], sp2[1] # y1, y2 = dp1[1], dp2[1] # else: # sy2, sy1 = sp1[1], sp2[1] # y2, y1 = dp1[1], dp2[1] # # pxperunit = abs((sx2 - sx1) / (x2 - x1)) # # li = x1 - sx1 / pxperunit # hi = x2 + (w - sx2) / pxperunit # lv = y1 - sy1 / pxperunit # hv = y2 + (h - sy2) / pxperunit # # plot.index_range.low_setting = li # plot.index_range.high_setting = hi # plot.value_range.low_setting = lv # plot.value_range.high_setting = hv # plot.request_redraw() def _define_points_fired(self): if self.plot: plot = self.plot if not self._defining_points: self.points = [] st = ReferencePointsTool(plot) st.on_trait_change(self._set_reference_point, 'current_position') self.points_tool = st plot.tools.insert(0, st) self.define_points_label = 'Finish' plot.overlays.append( ReferencePointsOverlay(tool=st, component=plot)) else: self.define_points_label = 'Define Points' self.points_tool.on_trait_change(self._set_reference_point, 'current_position', remove=True) plot.tools.pop(0) plot.overlays.pop(-1) self._calculate_affine_transform(self.points) self._defining_points = not self._defining_points def load_points(self, path): points = [(100, 100), (300, 400), (200, 300)] if os.path.isfile(path): points = loadtxt(path, delimiter=',') po = PointOverlay(component=self.plot, points=points) self.plot.overlays.append(po) def _calculate_affine_transform(self, pts): rps, ps = zip(*pts) s, r, t = calculate_rigid_transform(rps, ps) self.A = AffineTransform() self.A.scale(s, s) self.A.rotate(r) self.A.translate(-t[0], -t[1]) print self.A def traits_view(self): image = Item('container', style='custom', show_label=False, editor=ComponentEditor()) v = View(Item('open_button', show_label=False), Item('define_points', enabled_when='plot', editor=ButtonEditor(label_value='define_points_label'), show_label=False), image, resizable=True) return v
def predict_values(self, refresh=False): self.debug('predict values {}'.format(refresh)) try: x, y, z, ze, j, je, sj, sje = self._extract_position_arrays() t = AffineTransform() t.rotate(self.rotation) x, y = t.transforms(x, y) # print(x) except ValueError as e: self.debug('no monitor positions to fit, {}'.format(e)) return # print(x) # print(y) # print(z) # print(ze) n = x.shape[0] if n >= 3: # n = z.shape[0] * 10 r = max((max(abs(x)), max(abs(y)))) # r *= 1.25 reg = self._regressor_factory(x, y, z, ze) self._regressor = reg else: msg = 'Not enough monitor positions. At least 3 required. Currently only {} active'.format(n) self.debug(msg) self.information_dialog(msg) return options = self.plotter_options ipositions = self.unknown_positions + self.monitor_positions pts = array([[p.x, p.y] for p in ipositions]) if options.use_monte_carlo and options.model_kind not in ['Matching', 'Bracketing']: fe = FluxEstimator(options.monte_carlo_ntrials, reg) split = len(self.unknown_positions) nominals, errors = fe.estimate(pts) if options.position_error: _, pos_errors = fe.estimate_position_err(pts, options.position_error) else: pos_errors = zeros(pts.shape[0]) for positions, s, e in ((self.unknown_positions, 0, split), (self.monitor_positions, split, None)): noms, es, ps = nominals[s:e], errors[s:e], pos_errors[s:e] for p, j, je, pe in zip(positions, noms, es, ps): oj = p.saved_j p.j = j p.jerr = je p.position_jerr = pe p.dev = (oj - j) / j * 100 else: js = reg.predict(pts) jes = reg.predict_error(pts) for j, je, p in zip(js, jes, ipositions): p.j = float(j) p.jerr = float(je) p.dev = (p.saved_j - j) / j * 100 if options.plot_kind == '2D': self._graph_contour(x, y, z, r, reg, refresh) elif options.plot_kind == 'Grid': self._graph_grid(x, y, z, ze, r, reg, refresh) else: self._graph_hole_vs_j(x, y, r, reg, refresh)