def _calculate_optimal_operating_voltage(self, xs, ys): ''' find the flattest region of the grad 1. smooth the signal 2. calculate the gradient 3. get x of min y @todo: this algorithm may not work properly. need to collect a real scan to test on ''' # 1. smooth smoothed = smooth(ys, window_len=100) # truncate the smooth to elimate edge artifacts trunc = 20 xs = xs[trunc:-trunc] smoothed = smoothed[trunc:-trunc] self.graph.new_series(xs, smoothed) # 2. gradient grads = np.gradient(smoothed) # 3. get min cp = xs[np.argmin(grads)] return cp
def _calculate_optimal_operating_voltage(self, xs, ys): ''' find the flattest region of the grad 1. smooth the signal 2. calculate the gradient 3. get x of min y @todo: this algorithm may not work properly. need to collect a real scan to test on ''' # 1. smooth smoothed = smooth(ys, window_len=100) # truncate the smooth to elimate edge artifacts trunc = 20 xs = xs[trunc:-trunc] smoothed = smoothed[trunc:-trunc] self.graph.new_series(xs, smoothed) # 2. gradient grads = np.gradient(smoothed) # 3. get min cp = xs[np.argmin(grads)] return cp
def _gc(self, p, det, kind): g = Graph(container_dict=dict(padding=5), window_width=1000, window_height=800, window_x=40, window_y=20) with open(p, "r") as rfile: # gather data reader = csv.reader(rfile) header = reader.next() groups = self._parse_data(reader) """ groups= [data,] data shape = nrow,ncols """ data = groups[0] x = data[0] y = data[header.index(det)] sy = smooth(y, window_len=120) # , window='flat') x = x[::50] y = y[::50] sy = sy[::50] # smooth # plot g.new_plot(zoom=True, xtitle="Time (s)", ytitle="{} Baseline Intensity (fA)".format(det)) g.new_series(x, y, type=kind, marker="dot", marker_size=2) g.new_series(x, sy, line_width=2) # g.set_x_limits(500, 500 + 60 * 30) # g.edit_traits() return g
def run(self): ages = [(1, 0.1), (1.5, 0.4), (1.7, 0.1), (1.8, 0.2), (2.1, 0.5)] pool = Pool(processes=10) n = 1e5 # st = time.time() # aa = ((ai, ages) for ai in arange(n)) # print 'a"', time.time() - st age_gen = age_generator(ages, n) st = time.time() results = pool.map(_monte_carlo_step, age_gen) print 'a', time.time() - st st = time.time() results = vstack((ri for ri in results if ri is not None)) print 'b', time.time() - st for xx, (ai, ei) in zip(results.T, ages): # print 'dev ', abs(xx.mean() - ai) / ai * 100, abs(xx.std() - ei) / ei * 100 # print xx f, v = histogram(xx, 40) lp = plot(v[:-1], f)[0] c = lp.get_color() nf = smooth(f, window='flat') plot(v[:-1], nf, c=c, ls='--', lw=2) axvline(ai, c=c) # print f, v idx = argmax(nf) # axvline(xx.mean(), c=c, ls='--') axvline(v[idx], c=c, ls='--') show()
def new_series(self, x=None, y=None, plotid=0, normalize=False, time_series=True, timescale=False, downsample=None, use_smooth=False, scale=None, ** kw): ''' ''' if not time_series: return super(TimeSeriesGraph, self).new_series(x=x, y=y, plotid=plotid, **kw) xd = x if x is not None: if isinstance(x[0], str): # convert the time stamp into seconds since the Epoch # Epoch = 12:00 am 1/1/1970 fmt = "%Y-%m-%d %H:%M:%S" args = x[0].split(' +') timefunc = lambda xi, fmt: time.mktime(time.strptime(xi, fmt)) if len(args) > 1: xd = [timefunc(xi.split(' +')[0], fmt) + float(xi.split(' +')[1]) / 1000.0 for xi in x] else: fmt = '%a %b %d %H:%M:%S %Y' xd = [timefunc(xi, fmt) for xi in x] if downsample: xd = downsample_1d(x, downsample) y = downsample_1d(y, downsample) if use_smooth: y = smooth(y) if xd is not None: xd = array(xd) if normalize: xd = xd - xd[0] if scale: xd = xd * scale plot, names, rd = self._series_factory(xd, y, plotid=plotid, **kw) if 'type' in rd: if rd['type'] == 'line_scatter': plot.plot(names, type='scatter', marker_size=2, marker='circle') rd['type'] = 'line' plota = plot.plot(names, **rd)[0] # plota.unified_draw = True # plota.use_downsampling = True # if the plot is not visible dont remove the underlays if plota.visible: self._set_bottom_axis(plota, plot, plotid, timescale=timescale) return plota, plot
def new_series(self, x=None, y=None, plotid=0, normalize=False, time_series=True, timescale=False, downsample=None, use_smooth=False, scale=None, ** kw): ''' ''' if not time_series: return super(TimeSeriesGraph, self).new_series(x=x, y=y, plotid=plotid, **kw) xd = x if x is not None: if isinstance(x[0], str): # convert the time stamp into seconds since the Epoch # Epoch = 12:00 am 1/1/1970 fmt = "%Y-%m-%d %H:%M:%S" args = x[0].split(' +') timefunc = lambda xi, fmt: time.mktime(time.strptime(xi, fmt)) if len(args) > 1: xd = [timefunc(xi.split(' +')[0], fmt) + float(xi.split(' +')[1]) / 1000.0 for xi in x] else: fmt = '%a %b %d %H:%M:%S %Y' xd = [timefunc(xi, fmt) for xi in x] if downsample: xd = downsample_1d(x, downsample) y = downsample_1d(y, downsample) if use_smooth: y = smooth(y) if xd is not None: xd = array(xd) if normalize: xd = xd - xd[0] if scale: xd = xd * scale plot, names, rd = self._series_factory(xd, y, plotid=plotid, **kw) if 'type' in rd: if rd['type'] == 'line_scatter': plot.plot(names, type='scatter', marker_size=2, marker='circle') rd['type'] = 'line' plota = plot.plot(names, **rd)[0] # plota.unified_draw = True # plota.use_downsampling = True # if the plot is not visible dont remove the underlays if plota.visible: self._set_bottom_axis(plota, plot, plotid, timescale=timescale) return plota, plot
def _calculate_nominal_focal_point(self, fms, focussteps): if fms: sfms = smooth(fms) if sfms is not None: self.graph.new_series(focussteps, sfms) self.graph.redraw() fmi = focussteps[argmin(sfms)] fma = focussteps[argmax(sfms)] mi = min(sfms) ma = max(sfms) return mi, fmi, ma, fma
def _calculate_nominal_focal_point(self, fms, focussteps): if fms: sfms = smooth(fms) if sfms is not None: self.graph.new_series(focussteps, sfms) self.graph.redraw() fmi = focussteps[argmin(sfms)] fma = focussteps[argmax(sfms)] mi = min(sfms) ma = max(sfms) return mi, fmi, ma, fma
def _gc(self, p, det, kind): g = Graph(container_dict=dict(padding=5), window_width=1000, window_height=800, window_x=40, window_y=20) with open(p, 'r') as rfile: # gather data reader = csv.reader(rfile) header = reader.next() groups = self._parse_data(reader) ''' groups= [data,] data shape = nrow,ncols ''' data = groups[0] x = data[0] y = data[header.index(det)] sy = smooth(y, window_len=120) # , window='flat') x = x[::50] y = y[::50] sy = sy[::50] # smooth # plot g.new_plot(zoom=True, xtitle='Time (s)', ytitle='{} Baseline Intensity (fA)'.format(det)) g.new_series(x, y, type=kind, marker='dot', marker_size=2) g.new_series(x, sy, line_width=2) # g.set_x_limits(500, 500 + 60 * 30) # g.edit_traits() return g
def run(self): ages = [(1, 0.1), (1.5, 0.4), (1.7, 0.1), (1.8, 0.2), (2.1, 0.5)] pool = Pool(processes=10) n = 1e5 # st = time.time() # aa = ((ai, ages) for ai in arange(n)) # print 'a"', time.time() - st age_gen = age_generator(ages, n) st = time.time() results = pool.map(_monte_carlo_step, age_gen) print 'a', time.time() - st st = time.time() results = vstack((ri for ri in results if ri is not None)) print 'b', time.time() - st for xx, (ai, ei)in zip(results.T, ages): # print 'dev ', abs(xx.mean() - ai) / ai * 100, abs(xx.std() - ei) / ei * 100 # print xx f, v = histogram(xx, 40) lp = plot(v[:-1], f)[0] c = lp.get_color() nf = smooth(f, window='flat') plot(v[:-1], nf, c=c, ls='--', lw=2) axvline(ai, c=c) # print f, v idx = argmax(nf) # axvline(xx.mean(), c=c, ls='--') axvline(v[idx], c=c, ls='--') show()
def smooth(self, x, **kw): return smooth(x, **kw)
def smooth(self, x, **kw): return smooth(x, **kw)