def _plot(self, rs, tag, n, plotid): plot = self.graph.new_plot(padding_left=100) plot.y_axis.title = tag xs = arange(n) ys = array([nominal_value(ri) for ri in rs]) yes = array([std_dev(ri) for ri in rs]) p, s, l = self.graph.new_series(xs, ys, yerror=yes, fit='weighted mean', type='scatter') ebo = ErrorBarOverlay(component=s, orientation='y', nsigma=2, visible=True, use_end_caps=True) s.underlays.append(ebo) s.yerror = ArrayDataSource(yes) self.graph.set_x_limits(pad='0.1', plotid=plotid) ymin, ymax = min(ys - 2 * yes), max(ys + 2 * yes) self.graph.set_y_limits(min_=ymin, max_=ymax, pad='0.1', plotid=plotid)
def __init__(self, *args, **kw): BaseInset.__init__(self, *args, **kw) ScatterPlot.__init__(self) self.border_visible = kw.get('border_visible', True) self.marker = 'circle' # self.color = 'red' self.marker_size = 1.5 if not self.visible_axes: self.x_axis.visible = False self.y_axis.visible = False # self.set_limits() nsigma = 1 orientation = 'x' line_width = 1 visible = True ebo = ErrorBarOverlay(component=self, orientation=orientation, nsigma=nsigma, line_width=line_width, use_end_caps=False, visible=visible) self.overlays.append(ebo)
def replot(self): graph = self.graph if graph is None: graph = AnalysisStackedRegressionGraph() else: graph.clear() vs = self._get_values(scalar=self.standard_ratio) xs = self._get_xs() graph.new_plot() plot, scatter, line = graph.new_series(x=xs, y=vs[0], fit='weighted mean', yerror=vs[1], type='scatter') ebo = ErrorBarOverlay(component=scatter, orientation='y') scatter.overlays.append(ebo) graph.set_x_title('Time (hrs)') graph.set_y_title(self.options.ratio_str) graph.set_y_limits(min_=min([v - e for v, e in zip(*vs)]), max_=max([v + e for v, e in zip(*vs)]), pad='0.1') graph.set_x_limits(min_=min(xs), max_=max(xs), pad='0.1') graph.refresh() self.graph = graph
def _add_error_bars(self, scatter, errors, axis, nsigma, visible=True): ebo = ErrorBarOverlay(component=scatter, orientation=axis, nsigma=nsigma, visible=visible) scatter.underlays.append(ebo) setattr(scatter, '{}error'.format(axis), ArrayDataSource(errors)) return ebo
def _add_error_bars(self, scatter, errors, orientation='y', visible=True, nsigma=1, line_width=1): from pychron.graph.error_bar_overlay import ErrorBarOverlay ebo = ErrorBarOverlay(component=scatter, orientation=orientation, nsigma=nsigma, line_width=line_width, visible=visible) scatter.underlays.append(ebo) # print len(errors),scatter.index.get_size() setattr(scatter, '{}error'.format(orientation), ArrayDataSource(errors)) return ebo
def _graph_individual_analyses(self, *args, **kw): g = self.graph ans, ixs, iys, ies = self._analyses s, _p = g.new_series(ixs, iys, yerror=ies, type='scatter', marker='circle', marker_size=1.5, **kw) ebo = ErrorBarOverlay(component=s, orientation='y') s.underlays.append(ebo) s.error_bars = ebo add_analysis_inspector(s, ans) s.index.on_trait_change(self._update_graph_metadata, 'metadata_changed') return s, iys
def _rebuild_hole_vs_j(self, x, y, r, reg): g = Graph() self.graph = g p = g.new_plot(xtitle='Hole (Theta)', ytitle='J', padding=[90, 5, 5, 40]) p.y_axis.tick_label_formatter = lambda x: floatfmt(x, n=2, s=3) xs = arctan2(x, y) ys = reg.ys yserr = reg.yserr scatter, _ = g.new_series(xs, ys, yerror=yserr, type='scatter', marker='circle') ebo = ErrorBarOverlay(component=scatter, orientation='y') scatter.overlays.append(ebo) self._add_inspector(scatter) a = max((abs(min(xs)), abs(max(xs)))) fxs = linspace(-a, a) a = r * sin(fxs) b = r * cos(fxs) pts = vstack((a, b)).T fys = reg.predict(pts) g.new_series(fxs, fys) g.set_x_limits(-3.2, 3.2)
def _update_ring_graph(self): self.ring_graph = g = StackedGraph() g.plotcontainer.spacing = 10 g.plotcontainer.stack_order = 'top_to_bottom' self.deviation_graph = dg = StackedGraph() dg.plotcontainer.spacing = 10 dg.plotcontainer.stack_order = 'top_to_bottom' self.spoke_graph = sg = StackedGraph() sg.plotcontainer.spacing = 10 sg.plotcontainer.stack_order = 'top_to_bottom' l, h = 0, 0 dl, dh = 0, 0 for poss in self._group_rings(): poss = list(poss) x, y, z, ze, j, je, sj, sje = self._extract_position_arrays(poss) fx, fy, mx, my = self.model_plane(x, y, z, ze) x = arctan2(x, y) plot = g.new_plot(padding_left=100, padding_top=20, padding_right=10, padding_bottom=30) s = g.new_series(x, z, yerror=ze, type='scatter')[0] ebo = ErrorBarOverlay(component=s, orientation='y') s.underlays.append(ebo) g.new_series(x, j, color='green') # g.new_series(x, sj) g.new_series(fx, fy, color='blue') l = min(min(x), l) h = max(max(x), h) plot = dg.new_plot(padding_left=100, padding_top=20, padding_right=10, padding_bottom=30) ds = (j - z) / z * 100 ds2 = (my - z) / z * 100 # dg.new_series(x, (my-j)/j*100) dg.new_series(x, ds, color='green') dg.new_series(mx, ds2, color='blue') dl = min(min(ds), dl) dh = max(max(ds), dh) for i, p in enumerate(g.plots): g.set_x_limits(l, h, plotid=i, pad='0.05') dg.set_x_limits(l, h, plotid=i, pad='0.05') dg.set_y_limits(dl, dh, plotid=i, pad='0.05') for spoke, poss in self._group_spokes(): print(spoke) x, y, z, ze, j, je, sj, sje = self._extract_position_arrays(poss) x = (x**2 + y**2)**0.5 plot = sg.new_plot(padding_left=100, padding_top=20, padding_right=10, padding_bottom=30) s = sg.new_series(x, z, yerror=ze, type='scatter')[0] ebo = ErrorBarOverlay(component=s, orientation='y') s.underlays.append(ebo) sg.new_series(x, j, color='green')
def _graph_grid(self, x, y, z, ze, r, reg, refresh): self.min_j = min(z) self.max_j = max(z) g = self.graph layout = FigureLayout(fixed='filled_grid') nrows, ncols = layout.calculate(len(x)) if not isinstance(g, Graph): g = RegressionGraph(container_dict={ 'bgcolor': 'gray', 'kind': 'g', 'shape': (nrows, ncols) }) self.graph = g def get_ip(xi, yi): return next((ip for ip in self.monitor_positions if ((ip.x - xi)**2 + (ip.y - yi)**2)**0.5 < 0.01), None) opt = self.plotter_options monage = opt.monitor_age * 1e6 lk = opt.lambda_k ans = self._analyses[0] scale = opt.flux_scalar for r in range(nrows): for c in range(ncols): idx = c + ncols * r if refresh: try: yy = z[idx] * scale ye = ze[idx] * scale except IndexError: continue # if hasattr(g, 'rules'): # if idx in g.rules: # l1, l2, l3 = g.rules[idx] # l1.value = yy # l2.value = yy + ye # l3.value = yy - ye else: plot = g.new_plot(padding_left=65, padding_right=5, padding_top=30, padding_bottom=5) try: ip = get_ip(x[idx], y[idx]) except IndexError: continue add_axes_tools(g, plot) yy = z[idx] * scale ye = ze[idx] * scale plot.title = 'Identifier={} Position={}'.format( ip.identifier, ip.hole_id) plot.x_axis.visible = False if c == 0 and r == nrows // 2: plot.y_axis.title = 'J x{}'.format(scale) if not ip.use: continue # get ip via x,y ais = [ a for a in ans if a.irradiation_position == ip.hole_id ] n = len(ais) # plot mean value # l1 = g.add_horizontal_rule(yy, color='black', line_style='solid', plotid=idx) # l2 = g.add_horizontal_rule(yy + ye, plotid=idx) # l3 = g.add_horizontal_rule(yy - ye, plotid=idx) # rs = (l1, l2, l3) # d = {idx: rs} # if hasattr(g, 'rules'): # g.rules.update(d) # else: # g.rules = d # plot individual analyses fs = [a.model_j(monage, lk) * scale for a in ais] fs = sorted(fs) iys = array([nominal_value(fi) for fi in fs]) ies = array([std_dev(fi) for fi in fs]) if self.plotter_options.use_weighted_fit: fit = 'weighted mean' else: fit = 'average' ek = self.plotter_options.error_kind if ek == MSEM: ek = 'msem' fit = '{}_{}'.format(fit, ek) p_, s, l_ = g.new_series(linspace(0, n - 1, n), iys, yerror=ies, type='scatter', fit=fit, add_point_inspector=False, add_inspector=False, marker='circle', marker_size=3) g.set_x_limits(0, n - 1, pad='0.1', plotid=idx) g.set_y_limits(min(iys - ies), max(iys + ies), pad='0.1', plotid=idx) g.add_statistics(plotid=idx) ebo = ErrorBarOverlay(component=s, orientation='y') s.underlays.append(ebo) s.error_bars = ebo add_analysis_inspector(s, ais) s.index.on_trait_change( self._grid_update_graph_metadata(ais), 'metadata_changed') self.suppress_metadata_change = True sel = [i for i, a in enumerate(ais) if a.is_omitted()] s.index.metadata['selections'] = sel self.suppress_metadata_change = False g.refresh()
def _graph_hole_vs_j(self, x, y, r, reg, refresh): if self._individual_analyses_enabled: sel = [ i for i, (a, x, y, e) in enumerate(zip(*self._analyses)) if a.is_omitted() ] g = self.graph if not isinstance(g, Graph): g = Graph(container_dict={'bgcolor': self.plotter_options.bgcolor}) self.graph = g po = self.plotter_options ys = reg.ys xs = arctan2(x, y) yserr = reg.yserr lyy = ys - yserr uyy = ys + yserr a = max((abs(min(xs)), abs(max(xs)))) fxs = linspace(-a, a, 200) a = r * sin(fxs) b = r * cos(fxs) pts = vstack((a, b)).T fys = reg.predict(pts) use_ee = False if po.model_kind not in (MATCHING, BRACKETING, NN): use_ee = True try: l, u = reg.calculate_error_envelope(fxs, rmodel=fys) except BaseException: l, u = reg.calculate_error_envelope(pts, rmodel=fys) if not refresh: g.clear() p = g.new_plot( xtitle='Hole (Theta)', ytitle='J', # padding=[90, 5, 5, 40], padding=po.get_paddings()) p.bgcolor = po.plot_bgcolor add_axes_tools(g, p) def label_fmt(xx): return floatfmt(xx, n=2, s=4, use_scientific=True) p.y_axis.tick_label_formatter = label_fmt # plot fit line # plot0 == line if po.model_kind in (MATCHING, BRACKETING, NN): g.new_series(fxs, fys, render_style='connectedhold') else: line, _p = g.new_series(fxs, fys) if use_ee: ee = ErrorEnvelopeOverlay(component=line, xs=fxs, lower=l, upper=u) line.error_envelope = ee line.underlays.append(ee) miy = 100 may = -1 if self._individual_analyses_enabled: # plot the individual analyses # plot1 == scatter iscatter, iys = self._graph_individual_analyses() miy = min(iys) may = max(iys) # plot means # plot2 == scatter scatter, _ = g.new_series(xs, ys, yerror=yserr, type='scatter', marker_size=4, marker='diamond') ebo = ErrorBarOverlay(component=scatter, orientation='y') scatter.underlays.append(ebo) scatter.error_bars = ebo add_inspector(scatter, self._additional_info) ymi = min(lyy.min(), miy) yma = max(uyy.max(), may) g.set_x_limits(-3.5, 3.5) g.set_y_limits(ymi, yma, pad='0.1') if self._individual_analyses_enabled: # set metadata last because it will trigger a refresh self.suppress_metadata_change = True iscatter.index.metadata['selections'] = sel self.suppress_metadata_change = False if self._individual_analyses_enabled: # add a legend labels = [ ('plot1', 'Individual'), ('plot2', 'Mean'), ('plot0', 'Fit'), ] else: labels = [('plot0', 'Mean')] legend = ExplicitLegend(plots=self.graph.plots[0].plots, labels=labels) p.overlays.append(legend) else: plot = g.plots[0] s1 = plot.plots['plot2'][0] s1.yerror.set_data(yserr) s1.error_bars.invalidate() g.set_data(ys, plotid=0, series=2, axis=1) l1 = plot.plots['plot0'][0] l1.index.metadata['selections'] = sel g.set_data(fys, plotid=0, series=0, axis=1) if use_ee: l1.error_envelope.trait_set(xs=fxs, lower=l, upper=u) l1.error_envelope.invalidate() self.max_j = fys.max() self.min_j = fys.min()
def _graph_linear_j(self, x, y, r, reg, refresh): g = self.graph if not isinstance(g, RegressionGraph): g = RegressionGraph( container_dict={'bgcolor': self.plotter_options.bgcolor}) self.graph = g po = self.plotter_options g.clear() plot = g.new_plot(padding=po.get_paddings()) if po.model_kind == WEIGHTED_MEAN_1D: fit = 'weighted mean' else: fit = po.least_squares_fit _, scatter, line = g.new_series(x=reg.xs, y=reg.ys, yerror=reg.yserr, fit=fit) ebo = ErrorBarOverlay(component=scatter, orientation='y') scatter.underlays.append(ebo) scatter.error_bars = ebo add_inspector(scatter, self._additional_info) add_axes_tools(g, plot) g.set_x_title(po.one_d_axis) g.set_y_title('J') g.add_statistics() miy = 100 may = -1 if self._individual_analyses_enabled: sel = [ i for i, (a, x, y, e) in enumerate(zip(*self._analyses)) if a.is_omitted() ] # plot the individual analyses iscatter, iys = self._graph_individual_analyses(fit=None, add_tools=False) miy = min(iys) may = max(iys) # set metadata last because it will trigger a refresh self.suppress_metadata_change = True iscatter.index.metadata['selections'] = sel self.suppress_metadata_change = False g.set_y_limits(min_=miy, max_=may, pad='0.1') g.set_x_limits(pad='0.1') g.refresh() fys = line.value.get_data() self.max_j = fys.max() self.min_j = fys.min()
def _graph_contour(self, x, y, z, r, reg, refresh): cg = self.graph if not isinstance(cg, FluxVisualizationGraph): cg = FluxVisualizationGraph(container_dict={ 'kind': 'h', 'bgcolor': self.plotter_options.bgcolor }) self.graph = cg else: cg.clear() center_plot = cg.new_plot(xtitle='X', ytitle='Y', add=False, padding=0, width=550, height=550, resizable='', aspect_ratio=1) ito = IrradiationTrayOverlay(component=center_plot, geometry=self.geometry, show_labels=self.show_labels) self.irradiation_tray_overlay = ito center_plot.overlays.append(ito) gx, gy, m, me = self._model_flux(reg, r) # self._visualization_update(gx, gy, m, me, reg.xs, reg.ys) r = r * 1.1 s, p = cg.new_series(z=m, xbounds=(-r, r), ybounds=(-r, r), levels=self.levels, cmap=self.color_map_name, colorbar=True, style='contour') # add data tool def predict_func(ptx, pty): return floatfmt(reg.predict([(ptx, pty)])[0], n=2, use_scientific=True, s=6) dt = DataTool(plot=s, filter_components=False, predict_value_func=predict_func, use_date_str=False, component=p) dto = DataToolOverlay(component=p, tool=dt) p.tools.append(dt) p.overlays.append(dto) # add slice inspectors cg.add_inspectors(s) # add 1D slices bottom_plot = cg.new_plot(add=False, height=175, resizable='h', padding=0, xtitle='mm', ytitle='J') right_plot = cg.new_plot( add=False, width=175, resizable='v', padding=0, # xtitle='J', ytitle='mm') center = center_plot.plots['plot0'][0] options = dict(style='cmap_scatter', type='cmap_scatter', marker='circle', color_mapper=center.color_mapper) cg.new_series(plotid=1, render_style='connectedpoints') cg.new_series(plotid=1, **options) s = cg.new_series(plotid=1, type='scatter', marker_size=2, color='red', style='xy', marker='circle')[0] ebo = ErrorBarOverlay(component=s, orientation='y', use_component=False) s.underlays.append(ebo) x, y = reg.xs.T ebo.index, ebo.value, ebo.error = x, reg.ys, reg.yserr cg.bottom_error_bars = ebo center_plot.x_axis.orientation = 'top' right_plot.orientation = 'v' right_plot.x_axis.orientation = 'top' right_plot.x_axis.tick_label_rotate_angle = 45 right_plot.y_axis.orientation = 'right' right_plot.x_axis.axis_line_visible = False right_plot.y_axis.axis_line_visible = False s = cg.new_series(plotid=2, render_style='connectedpoints')[0] s.orientation = 'v' s = cg.new_series(plotid=2, **options)[0] s.orientation = 'v' ss = cg.new_series(plotid=2, type='scatter', marker_size=2, color='red', style='xy', marker='circle')[0] ss.orientation = 'v' ebo = ErrorBarOverlay(component=ss, orientation='x', use_component=False) ss.underlays.append(ebo) x, y = reg.xs.T ebo.index, ebo.value, ebo.error = y, reg.ys, reg.yserr cg.right_error_bars = ebo center.index.on_trait_change(cg.metadata_changed, 'metadata_changed') gridcontainer = container_factory( kind='g', # fill_padding=True, # bgcolor='red', padding_left=100, padding_right=20, padding_top=100, padding_bottom=40, shape=(2, 2), spacing=(5, 5)) gridcontainer.add(center_plot) gridcontainer.add(right_plot) gridcontainer.add(bottom_plot) # cb = cg.make_colorbar(center) # cb.width = 50 # cb.padding_left = 50 # cg.plotcontainer.add(cb) # plot means s = cg.new_series(x, y, z=z, style='cmap_scatter', color_mapper=s.color_mapper, marker='circle', marker_size=self.marker_size) cg.errors = reg.yserr cg.x = reg.xs cg.y = reg.ys cg.plotcontainer.add(gridcontainer)
def _graph_hole_vs_j(self, x, y, r, reg, refresh): sel = [i for i, a in enumerate(self.analyses) if a.is_omitted()] g = self.graph if not isinstance(g, Graph): g = Graph(container_dict={'bgcolor': self.plotter_options.bgcolor}) self.graph = g po = self.plotter_options xs = arctan2(x, y) ys = reg.ys a = max((abs(min(xs)), abs(max(xs)))) fxs = linspace(-a, a) a = r * sin(fxs) b = r * cos(fxs) pts = vstack((a, b)).T fys = reg.predict(pts) yserr = reg.yserr l, u = reg.calculate_error_envelope([[p] for p in pts], rmodel=fys) lyy = ys - yserr uyy = ys + yserr if not refresh: g.clear() p = g.new_plot(xtitle='Hole (Theta)', ytitle='J', # padding=[90, 5, 5, 40], padding=po.paddings()) p.bgcolor = po.plot_bgcolor p.y_axis.tick_label_formatter = lambda x: floatfmt(x, n=2, s=4, use_scientific=True) scatter, _ = g.new_series(xs, ys, yerror=yserr, type='scatter', marker='circle') ebo = ErrorBarOverlay(component=scatter, orientation='y') scatter.overlays.append(ebo) scatter.error_bars = ebo add_inspector(scatter, self._additional_info) line, _p = g.new_series(fxs, fys) ee = ErrorEnvelopeOverlay(component=line, xs=fxs, lower=l, upper=u) line.error_envelope = ee line.overlays.append(ee) # plot the individual analyses s, iys = self._graph_individual_analyses() s.index.metadata['selections'] = sel # s.index.metadata_changed = True ymi = min(lyy.min(), min(iys)) yma = max(uyy.max(), max(iys)) g.set_x_limits(-3.2, 3.2) else: plot = g.plots[0] s1 = plot.plots['plot0'][0] s1.yerror.set_data(yserr) s1.error_bars.invalidate() l1 = plot.plots['plot1'][0] l1.error_envelope.trait_set(xs=fxs, lower=l, upper=u) l1.error_envelope.invalidate() g.set_data(ys, plotid=0, series=0, axis=1) g.set_data(fys, plotid=0, series=1, axis=1) s2 = plot.plots['plot2'][0] iys = s2.value.get_data() ymi = min(fys.min(), lyy.min(), iys.min()) yma = max(fys.max(), uyy.max(), iys.max()) s2.index.metadata['selections'] = sel g.set_y_limits(ymi, yma, pad='0.1') self._model_sin_flux(fxs, fys)
def _graph_contour(self, x, y, z, r, reg, refresh): cg = self.graph if not isinstance(cg, FluxVisualizationGraph): cg = FluxVisualizationGraph(container_dict={'kind': 'h', 'bgcolor': self.plotter_options.bgcolor}) self.graph = cg else: cg.clear() center_plot = cg.new_plot(xtitle='X', ytitle='Y', add=False, padding=0, width=550, height=550, resizable='', aspect_ratio=1) ito = IrradiationTrayOverlay(component=center_plot, geometry=self.geometry, show_labels=self.show_labels) self.irradiation_tray_overlay = ito center_plot.overlays.append(ito) gx, gy, m, me = self._model_flux(reg, r) # self._visualization_update(gx, gy, m, me, reg.xs, reg.ys) r = r * 1.1 s, p = cg.new_series(z=m, xbounds=(-r, r), ybounds=(-r, r), levels=self.levels, cmap=self.color_map_name, colorbar=True, style='contour') # add data tool def predict_func(ptx, pty): return floatfmt(reg.predict([(ptx, pty)])[0], n=2, use_scientific=True, s=6) dt = DataTool(plot=s, filter_components=False, predict_value_func=predict_func, use_date_str=False, component=p) dto = DataToolOverlay(component=p, tool=dt) p.tools.append(dt) p.overlays.append(dto) # add slice inspectors cg.add_inspectors(s) # add 1D slices bottom_plot = cg.new_plot(add=False, height=175, resizable='h', padding=0, xtitle='mm', ytitle='J') right_plot = cg.new_plot(add=False, width=175, resizable='v', padding=0, # xtitle='J', ytitle='mm') center = center_plot.plots['plot0'][0] options = dict(style='cmap_scatter', type='cmap_scatter', marker='circle', color_mapper=center.color_mapper) cg.new_series(plotid=1, render_style='connectedpoints') cg.new_series(plotid=1, **options) s = cg.new_series(plotid=1, type='scatter', marker_size=2, color='red', style='xy', marker='circle')[0] ebo = ErrorBarOverlay(component=s, orientation='y', use_component=False) s.underlays.append(ebo) x, y = reg.xs.T ebo.index, ebo.value, ebo.error = x, reg.ys, reg.yserr cg.bottom_error_bars = ebo center_plot.x_axis.orientation = 'top' right_plot.orientation = 'v' right_plot.x_axis.orientation = 'top' right_plot.x_axis.tick_label_rotate_angle = 45 right_plot.y_axis.orientation = 'right' right_plot.x_axis.axis_line_visible = False right_plot.y_axis.axis_line_visible = False s = cg.new_series(plotid=2, render_style='connectedpoints')[0] s.orientation = 'v' s = cg.new_series(plotid=2, **options)[0] s.orientation = 'v' ss = cg.new_series(plotid=2, type='scatter', marker_size=2, color='red', style='xy', marker='circle')[0] ss.orientation = 'v' ebo = ErrorBarOverlay(component=ss, orientation='x', use_component=False) ss.underlays.append(ebo) x, y = reg.xs.T ebo.index, ebo.value, ebo.error = y, reg.ys, reg.yserr cg.right_error_bars = ebo center.index.on_trait_change(cg.metadata_changed, 'metadata_changed') gridcontainer = container_factory(kind='g', # fill_padding=True, # bgcolor='red', padding_left=100, padding_right=20, padding_top=100, padding_bottom=40, shape=(2, 2), spacing=(5, 5)) gridcontainer.add(center_plot) gridcontainer.add(right_plot) gridcontainer.add(bottom_plot) # cb = cg.make_colorbar(center) # cb.width = 50 # cb.padding_left = 50 # cg.plotcontainer.add(cb) # plot means s = cg.new_series(x, y, z=z, style='cmap_scatter', color_mapper=s.color_mapper, marker='circle', marker_size=self.marker_size) cg.errors = reg.yserr cg.x = reg.xs cg.y = reg.ys cg.plotcontainer.add(gridcontainer)