def moving_average(y, window_length): """ Compute the moving average of y with specified window length. Args: y: an 1-d pylab array with length N, representing the y-coordinates of the N sample points window_length: an integer indicating the window length for computing moving average Returns: an 1-d pylab array with the same length as y storing moving average of y-coordinates of the N sample points """ i = 1 moving_avg = pylab.array([]) while i < window_length: moving_avg = pylab.append(moving_avg, pylab.array(sum(y[0:i])/i)) i += 1 for j in range(i, len(y)+1): moving_avg = pylab.append(moving_avg, pylab.array( sum(y[j-window_length:j] / window_length))) return moving_avg
def updateFunction(self): global x_t, axis_1_data, axis_2_data, axis_3_data, axis_4_data, x #y = lambda x: sin(x) #dy_dx = lambda x: cos(x) #y_dx_dx = lambda x: -sin(x) y = sin( x_t ) #lChannel[sampleNum]# a list of points: y, let b be iter(y); next(b) dy_dx = cos(x_t) # like for a list of points: y, let b be iter(y); next(b) y_dx_dx = -sin( x_t) # like for a list of points: y, let b be iter(y); next(b) axis_1_data = append(axis_1_data, y) axis_2_data = append(axis_2_data, dy_dx) axis_3_data = append(axis_3_data, y_dx_dx) x = append(x, x_t) sin_x_plot.set_data(x, axis_1_data) sin_x_d1_plot.set_data(x, axis_2_data) sin_x_d2_plot.set_data(x, axis_3_data) x_t += 0.05 if x_t >= max_t - 1.00: sin_x_plot.axes.set_xlim(x_t - max_t + 1.0, x_t + 1.0) sin_x_d1_plot.axes.set_xlim(x_t - max_t + 1.0, x_t + 1.0) sin_x_d2_plot.axes.set_xlim(x_t - max_t + 1.0, x_t + 1.0) return sin_x_plot, sin_x_d1_plot, sin_x_d2_plot
def gen_cities_avg(climate, multi_cities, years): """ Compute the average annual temperature over multiple cities. Args: climate: instance of Climate multi_cities: the names of cities we want to average over (list of str) years: the range of years of the yearly averaged temperature (list of int) Returns: a pylab 1-d array of floats with length = len(years). Each element in this array corresponds to the average annual temperature over the given cities for a given year. """ year_dt = list() for year in years: city_dt = pylab.array([]) for city in multi_cities: city_dt = pylab.append( city_dt, climate.get_yearly_temp(city, year)) year_dt.append(sum(city_dt) / len(city_dt)) return pylab.array(year_dt)
def updateFunction(self): global x_t, axis_1_data #song waveform vs. time y = (x_t) # a list of points: y axis_1_data = append(axis_1_data, y) x = append(x[len(x) - 1] * file.getframerate()**-1, x_t) #ut=ugly t wav_plot.set_data(x, axis_1_data) x_t += 1 / 4000 if x_t >= max_t - 1.00: wav_plot.axes.set_xlim(x_t - max_t + 1.0, x_t + 1.0) return wav_plot
def create_cone(point, radio, angle, opening, resolution=1): # Define the list for the points of the cone-shape polygon p = [] # The fisrt point will be the vertex of the cone p.append(vis.Point(point[0], point[1])) # Define the start and end of the arc start = angle - opening end = angle + opening for i in range(start, end, resolution): # Convert start angle from degrees to radians rad = math.radians(i) # Calculate the off-set of the first point of the arc x = radio * math.cos(rad) y = radio * math.sin(rad) # Add the off-set to the vertex point new_x = point[0] + x new_y = point[1] + y # Add the first point of the arc to the list p.append(vis.Point(new_x, new_y)) # Add the last point of the arc rad = math.radians(end) x = radio * math.cos(rad) y = radio * math.sin(rad) new_x = point[0] + x new_y = point[1] + y p.append(vis.Point(new_x, new_y)) return vis.Polygon(p)
def unclick(event): if not event.inaxes: return global whatx, fit_1, fit_3, startpos, endpos, fits_1, xs_1, x1, y1, x3, y3 endpos = event.xdata, event.ydata if event.button == 1: if fit_1: x1, y1 = fit_1.get_data() x1 = pl.append(x1, whatx) y1 = pl.append(y1, 0.5 * (startpos[1] + endpos[1])) fit_1.set_data(x1, y1) else: fit_1 = pl.plot(whatx, 0.5 * (startpos[1] + endpos[1]), 'r.')[0] fits_1 = [] # add this to the list of uncert lines plots fits_1 = pl.append( fits_1, pl.plot([whatx, whatx], [startpos[1], endpos[1]], 'r')[0]) xs_1 = pl.append(xs_1, whatx) # print "xs_1 = ",xs_1 # XXX TODO also set the uncert somewhere pl.draw()
def snapshot(self, txl, ctime): """ Captures the current statistics of the system and updates graph grid, including average system interval, intervention interval, average number of falls at end of system interval and proportions of population in each population category. :param txl: neo4j database write transaction :param ctime: current timestep :return: None """ look = txl.run("MATCH (n:Node) " "WHERE n.name = {node} " "RETURN n", node="Intervention") self.nrecord = look.values() if super(Monitor, self).snapshot(txl, ctime): # Update plot 1 - Int Cap # Update to track average number of each type of fall for people in care. [mild, moderate, severe, agents_n] = txl.run( "MATCH (n:Node) " "WHERE n.name={node} " "RETURN n.mild, n.moderate, n.severe, n.agents", node="Care").values()[0] if agents_n: self.y11 = pylab.append(self.y11, mild / agents_n) self.p11.set_data(self.t, self.y11) self.y12 = pylab.append(self.y12, moderate / agents_n) self.p12.set_data(self.t, self.y12) self.y13 = pylab.append(self.y13, severe / agents_n) self.p13.set_data(self.t, self.y13) if max([ mild / agents_n, moderate / agents_n, severe / agents_n ]) > self.y1: self.y1 = max([ mild / agents_n, moderate / agents_n, severe / agents_n ]) self.p11.axes.set_ylim(0.0, self.y1 + 1.0) self.p12.axes.set_ylim(0.0, self.y1 + 1.0) self.p13.axes.set_ylim(0.0, self.y1 + 1.0) else: self.y11 = pylab.append(self.y11, 0) self.p11.set_data(self.t, self.y11) self.y12 = pylab.append(self.y12, 0) self.p12.set_data(self.t, self.y12) self.y13 = pylab.append(self.y13, 0) self.p13.set_data(self.t, self.y13) # Update plot 2 - Hos to Int gaps = timesincedischarge(txl) if gaps: hiint = mean(timesincedischarge(txl)) self.y3storage = self.y3storage + [hiint] if len(self.y3storage) >= 10: self.y3storage = self.y3storage[-10:] if self.y3storage: self.y2 = pylab.append(self.y2, mean(self.y3storage)) if self.y < mean(self.y3storage): self.y = mean(self.y3storage) self.p2.set_data(self.t, self.y2) # Update plot 3 - Start to Care careint = intf.getnodevalue(txl, "Care", "interval", uid="name") if careint: scint = careint else: scint = 0 self.y3 = pylab.append(self.y3, scint) if self.y < scint: self.y = scint self.p3.set_data(self.t, self.y3) # Update plot 4 # Update plot showing distribution of well being in the general population. wb = txl.run( "MATCH (a:Agent)-[r:LOCATED]->(n:Node) " "WHERE NOT n.name={node} " "RETURN a.wellbeing", node="Care").values() wb = [val[0] for val in wb] healthy = wb.count("Healthy") / len(wb) at_risk = wb.count("At risk") / len(wb) fallen = wb.count("Fallen") / len(wb) self.y41 = pylab.append(self.y41, healthy) self.p41.set_data(self.t, self.y41) self.y42 = pylab.append(self.y42, at_risk) self.p42.set_data(self.t, self.y42) self.y43 = pylab.append(self.y43, fallen) self.p43.set_data(self.t, self.y43) if max([healthy, at_risk, fallen]) / len(wb) > self.y4: self.y4 = max([healthy, at_risk, fallen]) self.p41.axes.set_ylim(0.0, self.y4 + 1.0) self.p42.axes.set_ylim(0.0, self.y4 + 1.0) self.p43.axes.set_ylim(0.0, self.y4 + 1.0) # Update plot axes if self.x >= self.xmax - 1.00: self.p11.axes.set_xlim(0.0, self.x + 1.0) self.p12.axes.set_xlim(0.0, self.x + 1.0) self.p13.axes.set_xlim(0.0, self.x + 1.0) self.p2.axes.set_xlim(0.0, self.x + 1.0) self.p3.axes.set_xlim(0.0, self.x + 1.0) self.p41.axes.set_xlim(0.0, self.x + 1.0) self.p42.axes.set_xlim(0.0, self.x + 1.0) self.p43.axes.set_xlim(0.0, self.x + 1.0) self.xmax = self.x if self.y > self.ymax1 - 1.0: self.p2.axes.set_ylim(0.0, self.y + 1.0) self.p3.axes.set_ylim(0.0, self.y + 1.0) if self.show: plt.pause(0.0005)
def click(event): if not event.inaxes: return global whatx, fit_1, fit_3, startpos, endpos, fits_1, xs_1, x1, y1, x3, y3 startpos = event.xdata, event.ydata # find closest existing pt if fit_1 == None: # print "no fit_1?!" x1 = [] y1 = [] d1 = pl.array([1e10]) else: x1, y1 = fit_1.get_data() d1 = abs(event.xdata - x1) if fit_3 == None: # print "no fit_3?!" x3 = [] y3 = [] d3 = pl.array([1e10]) else: x3, y3 = fit_3.get_data() d3 = abs(event.xdata - x3) # todo: for deletions, make sure we have all avail wavelength pts # i suppose that the flux combination step that creates fit_wave # will do that... # print "x1=",x1 # print "x3=",x3 if len(d1) <= 0: d1 = pl.array([1e10]) if len(d3) <= 0: d3 = pl.array([1e10]) if d1.min() <= d3.min(): whatpoint = pl.where(d1 == d1.min())[0][0] whatx = x1[whatpoint] print "deleting detection %d @ " % whatpoint, whatx fit_1.set_data(pl.delete(x1, whatpoint), pl.delete(y1, whatpoint)) # delete the uncert error line too # ds_1=abs(event.xdata-xs_1) # k=pl.where(ds_1==ds_1.min())[0][0] k = whatpoint fits_1[k].remove() fits_1 = pl.delete(fits_1, k) xs_1 = pl.delete(xs_1, k) else: whatpoint = pl.where(d3 == d3.min())[0][0] whatx = x3[whatpoint] print "deleting UL %d @ " % whatpoint, whatx x3 = pl.delete(x3, whatpoint) y3 = pl.delete(y3, whatpoint) fit_3.set_data(x3, y3) if event.button == 3: #R-click x3 = pl.append(x3, whatx) y3 = pl.append(y3, startpos[1]) if fit_3 == None: fit_3 = pl.plot(x3, y3, 'rv')[0] else: fit_3.set_data(x3, y3) pl.draw()