def test_epoch2num(): mdates._reset_epoch_test_example() mdates.set_epoch('0000-12-31') assert mdates.epoch2num(86400) == 719164.0 assert mdates.num2epoch(719165.0) == 86400 * 2 # set back to the default mdates._reset_epoch_test_example() mdates.set_epoch('1970-01-01T00:00:00') assert mdates.epoch2num(86400) == 1.0 assert mdates.num2epoch(2.0) == 86400 * 2
def test_epoch2num(): with _api.suppress_matplotlib_deprecation_warning(): mdates._reset_epoch_test_example() mdates.set_epoch('0000-12-31') assert mdates.epoch2num(86400) == 719164.0 assert mdates.num2epoch(719165.0) == 86400 * 2 # set back to the default mdates._reset_epoch_test_example() mdates.set_epoch('1970-01-01T00:00:00') assert mdates.epoch2num(86400) == 1.0 assert mdates.num2epoch(2.0) == 86400 * 2
def onselect(xmin, xmax): global left global right global span_1 print('start: {} , {}'.format(xmin, xmax)) left = pd.to_datetime(mdates.num2epoch(xmin), unit='s') right = pd.to_datetime(mdates.num2epoch(xmax), unit='s') try: span_1.remove() span_1 = ax3.axvspan(xmin, xmax, facecolor='red', alpha=0.4) except: span_1 = ax3.axvspan(xmin, xmax, facecolor='red', alpha=0.4) print('start: {} , {}'.format(left, right))
def xlim_changed(self, event): x0, x1 = self.ax.get_xlim() self.tstart = DateTime(num2epoch(x0), format='unix').secs self.tstop = DateTime(num2epoch(x1), format='unix').secs stat = get_stat(self.tstart, self.tstop, self.npix) if (self.tstart < self.msid.tstart or self.tstop > self.msid.tstop or stat != self.msid.stat): dt = self.tstop - self.tstart self.tstart -= dt / 4 self.tstop += dt / 4 self.msid = self.fetch.Msid(self.msidname, self.tstart, self.tstop, stat=stat) self.draw_plot()
def __call__(self, x, pos=0): x = mpd.num2epoch(x) for sec in self.sections(): t1, t2 = self.gettime(sec) if t1 <= x and x < t2: return sec return 'Unknown'
def on_pick(event): artist = event.artist # print('Artist picked:', artist) ind = event.ind # print('{} vertices picked'.format(len(ind))) # print('Pick between vertices {} and {}'.format(min(ind), max(ind)+1)) if isinstance(artist, PathCollection): print('label:' + artist.get_label()) print('offsets:' + repr(artist.get_offsets())) elif isinstance(artist, ml.Line2D): x = artist.get_xdata() if len(ind) > 5: print('Too many points selected') else: print('{} points in range:'.format(len(ind))) for idx in ind: # print('Data point:', x[idx]) ux = md.num2epoch(x[idx]) # print(ux) # print(datetime.fromtimestamp(ux).strftime('%Y-%m-%d %H:%M:%S')) #print(artist.axes.origvalues) # find the right point for e in artist.axes.origvalues: if e['started_at'] > (ux - 1) and e['started_at'] < (ux + 1): print(' - name : ' + e['name']) print(' - pipeline : ' + e['pipeline']['web_url']) print(' - job : ' + e['web_url']) break else: print('Not found')
def __on_motion(self, event): xpos = event.xdata ypos = event.ydata text = "" if xpos is None or ypos is None or len(self.spectrum) == 0: return if self.settings.display == Display.PLOT: timeStamp = max(self.spectrum) spectrum = self.spectrum[timeStamp] elif self.settings.display == Display.SPECT: timeStamp = num2epoch(ypos) if timeStamp in self.spectrum: spectrum = self.spectrum[timeStamp] else: nearest = min(self.spectrum.keys(), key=lambda k: abs(k - timeStamp)) spectrum = self.spectrum[nearest] else: spectrum = None if spectrum is not None and len(spectrum) > 0: x = min(spectrum.keys(), key=lambda freq: abs(freq - xpos)) if xpos <= max(spectrum.keys(), key=float): y = spectrum[x] text = "f = {0:.6f}MHz, p = {1:.2f}dB/Hz".format(x, y) else: text = "f = {0:.6f}MHz".format(xpos) self.status.SetStatusText(text, 1)
def get(self, req, *args, **kwargs): json = 'application/json' in req.META.get('HTTP_ACCEPT') if not json and not Figure: raise Http404("Can't generate image") context = self.get_context_data(**kwargs) data = self.data_from_context(context) if json: # convert to list of lists data[:,0] = num2epoch(data[:,0]) data[:,0] *= 1000 # to ms ret = [None]*data.shape[0] for i in range(data.shape[0]): ret[i] = list(data[i,:]) return JsonResponse({'data':ret}) tz = get_current_timezone() fig = Figure(dpi=96, figsize=(4,3)) ax = fig.add_subplot(111) ax.plot_date(data[:,0], data[:,1]) ax.set(**self.set_axis) ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S', tz=tz)) fig.autofmt_xdate() canva = FigureCanvas(fig) resp = HttpResponse(content_type='image/png') canva.print_png(resp) return resp
def key_press(self, event): if event.key in ['z', 'p'] and event.inaxes: x0, x1 = self.ax.get_xlim() dx = x1 - x0 xc = event.xdata zoom = self.zoom if event.key == 'p' else 1.0 / self.zoom new_x1 = zoom * (x1 - xc) + xc new_x0 = new_x1 - zoom * dx tstart = max(num2epoch(new_x0), MIN_TSTART_UNIX) tstop = min(num2epoch(new_x1), MAX_TSTOP_UNIX) new_x0 = epoch2num(tstart) new_x1 = epoch2num(tstop) self.ax.set_xlim(new_x0, new_x1) self.ax.figure.canvas.draw_idle() elif event.key == 'm': for _ in range(len(self.ax.lines)): self.ax.lines.pop() self.plot_mins = not self.plot_mins print('\nPlotting mins and maxes is {}'.format( 'enabled' if self.plot_mins else 'disabled')) self.draw_plot() elif event.key == 'a': # self.fig.clf() # self.ax = self.fig.gca() self.ax.set_autoscale_on(True) self.draw_plot() self.ax.set_autoscale_on(False) self.xlim_changed(None) elif event.key == 'y': self.scaley = not self.scaley print('Autoscaling y axis is {}'.format( 'enabled' if self.scaley else 'disabled')) self.draw_plot() elif event.key == '?': print(""" Interactive MSID plot keys: a: autoscale for full data range in x and y m: toggle plotting of min/max values p: pan at cursor x y: toggle autoscaling of y-axis z: zoom at cursor x ?: print help """)
def stats(self, flist, tlist): temp = np.array(self.pixels) max_f = flist[max(temp[:, 0])] min_f = flist[min(temp[:, 0])] mean_f = flist[int(np.mean(temp[:, 0]))] self.start_f = min_f self.end_f = max_f diff_f = max_f - min_f max_t = mds.num2epoch(tlist[max(temp[:, 1])]) min_t = mds.num2epoch(tlist[min(temp[:, 1])]) mean_t = mds.num2epoch(tlist[int(np.mean(temp[:, 1]))]) self.start_t = min_t self.end_t = max_t diff_t = max_t - min_t return (max_t, min_t, mean_t, max_f, min_f, mean_f, self.type)
def meteo_read(date, path): meteo_file = find_meteo_file(date, path) if meteo_file is not None: x = np.loadtxt(meteo_file, converters={0:mdates.datestr2num}) time = mdates.num2epoch(x[:,0]) meteo = {'time':time, 'temperature':x[:,3], 'rh':x[:,4]} return meteo else: return None
def on_xlims_change(axes): xl = axes.get_xlim() #print("updated xlims: ", xl) # force only 2 ticks (leftmost, rightmost) #axes.set_xticks(xl, minor=False) uxl = md.num2epoch(xl) t1 = datetime.utcfromtimestamp(uxl[0]).strftime('%Y-%m-%d %H:%M:%S') t2 = datetime.utcfromtimestamp(uxl[1]).strftime('%Y-%m-%d %H:%M:%S') axes.get_figure().suptitle(t1 + ' - ' + t2) rv = showmetrics(uxl[0], uxl[1]) for a in axes.get_figure().axes: a.set_xlabel(rv.pop(0))
def key_press(self, event): if event.key in ['z', 'p'] and event.inaxes: x0, x1 = self.ax.get_xlim() dx = x1 - x0 xc = event.xdata zoom = self.zoom if event.key == 'p' else 1.0 / self.zoom new_x1 = zoom * (x1 - xc) + xc new_x0 = new_x1 - zoom * dx tstart = max(num2epoch(new_x0), MIN_TSTART_UNIX) tstop = min(num2epoch(new_x1), MAX_TSTOP_UNIX) new_x0 = epoch2num(tstart) new_x1 = epoch2num(tstop) self.ax.set_xlim(new_x0, new_x1) self.ax.figure.canvas.draw_idle() elif event.key == 'm': for _ in range(len(self.ax.lines)): self.ax.lines.pop() self.plot_mins = not self.plot_mins print '\nPlotting mins and maxes is {}'.format( 'enabled' if self.plot_mins else 'disabled') self.draw_plot() elif event.key == 'a': # self.fig.clf() # self.ax = self.fig.gca() self.ax.set_autoscale_on(True) self.draw_plot() self.ax.set_autoscale_on(False) self.xlim_changed(None) elif event.key == 'y': self.scaley = not self.scaley print 'Autoscaling y axis is {}'.format( 'enabled' if self.scaley else 'disabled') self.draw_plot() elif event.key == '?': print """
def radflux_read(radfile): x = np.loadtxt(radfile, converters={0:mdates.datestr2num}) # time = np.array(mdates.num2date(x[:,0]), dtype=np.float64) date = mdates.num2date(x[0,0]) time = mdates.num2epoch(x[:,0]) sangle = x[:,1] totalf = x[:,4] lw = x[:,5] data = dict() data['solar angle'] = sangle data['clear sky'] = sw_clearsky(sangle) data['total SW flux'] = np.array(totalf, dtype=np.float64) data['LW flux'] = np.array(lw, dtype=np.float64) return time, data, date
def radflux_year_read(radfile): x = np.genfromtxt(radfile, delimiter=',', missing_values='NaN') if x.ndim < 2: return yy = x[:,0] mm = x[:,1] dd = x[:,2] hh = x[:,3] dates = [datetime(int(y), int(m), int(d), int(h)) for (y, m, d, h) in zip(yy, mm, dd, hh)] date = dates[0].date() dates = mdates.date2num(dates) dates = mdates.num2epoch(dates) solar_angle = x[:,6] sw_global = x[:,9] lw = x[:,10] sw_clearsky = x[:,11] lw_clearsky = x[:,12] data = {'solar angle': solar_angle, 'lw_clearsky': lw_clearsky, 'sw_clearsky': sw_clearsky, 'total SW flux':sw_global, 'LW flux':lw, 'date':date, 'time':dates} return data
def meteo_year_read(year, path): print 'Reading meteo data for ', year meteo_file = path + '/MeteoZ1_SIRTA_Z1_1hour%04d.txt' % (year) print 'Trying ', meteo_file x = np.loadtxt(meteo_file, delimiter=',') y = np.int32(x[:,0]) m = np.int32(x[:,1]) d = np.int32(x[:,2]) hh = np.int32(x[:,3]) mm = np.int32(x[:,4]) temperature = x[:,5] matemperature = np.ma.masked_where(temperature < -100, temperature) temperature[temperature < -100] = np.nan time = [] for i in np.r_[0:len(y)]: time.append(datetime(y[i], m[i], d[i], hh[i], mm[i])) epochtime = mdates.num2epoch(mdates.date2num(time)) meteo = {'time':time, 'Temperature [C]':matemperature, 'epochtime':epochtime, 'temperature':temperature} return meteo
myrouter.lancollector(traffic) axs[0].clear() axs[1].clear() axs[0].set_ylabel("K Bytes / s", fontsize=14) axs[1].set_ylabel("K Bytes / s", fontsize=14) for dev in traffic.devices(): tArray = traffic.extract(dev, "t") n = len(tArray) dArray = traffic.extract(dev, "txbytes") if dev == "wan": #axs[0].set_xlim(left=sleepTime, right=int((tArray[n-1]+sleepTime*nhist*.15))) #axs[0].set_xlim(left=tArray[0], right=int((tArray[n-1]+sleepTime*nhist*.15))) axs[0].set_xlim(left=tArray[0], right=mdates.epoch2num( mdates.num2epoch(tArray[n - 1]) + int(sleepTime * nhist * .25))) axs[0].set_title("Transmit (TX)", fontsize=14, fontweight='bold') axs[0].plot(tArray, dArray, label=dev, linewidth=3) elif willplot(dArray, 2): axs[0].plot(tArray, dArray, label=myrouter.query_device_name(dev), linewidth=1) dArray = traffic.extract(dev, "rxbytes") if dev == "wan": axs[1].set_title("Receive (RX)", fontsize=14, fontweight='bold') axs[1].plot(tArray, dArray, label=dev, linewidth=3) elif willplot(dArray, 2): axs[1].plot(tArray,
def graphdays2datetime(x): epoch = mdates.num2epoch(x) return datetime.fromtimestamp(int(epoch), utc)
def plottm(u): """posix timestamp -> plot time""" td = (datetime.utcfromtimestamp(u) - matplotlib_epoch) return td.days + 1 + (1000000 * td.seconds + td.microseconds) / 1e6 / DAY def unixtm(p): """plot time -> posix timestamp""" td = timedelta(days=p - 1) return (matplotlib_epoch + td - posix_epoch).total_seconds() f = datetime.utcfromtimestamp u = 1270000000.1234567890 print(f(u)) print(mpl_dt.epoch2num(u)) print(plottm(u)) print(f(mpl_dt.num2epoch(mpl_dt.epoch2num(u)))) print(f(mpl_dt.num2epoch(plottm(u)))) print(f(unixtm(mpl_dt.epoch2num(u)))) print(f(unixtm(plottm(u)))) assert abs(mpl_dt.epoch2num(u) - plottm(u)) < 1e-5 p = 86401.234567890 / DAY print(f(mpl_dt.num2epoch(p))) print(f(unixtm(p))) assert abs(mpl_dt.num2epoch(p) - unixtm(p)) < 1e-5
def __on_motion(self, event): label = '' if event.xdata is not None and event.xdata >= 1: timestamp = num2epoch(event.xdata) label = datetime.datetime.fromtimestamp(timestamp).strftime('%c') self._toolbar.set_cursor_text(label)
def __on_motion(self, event): axes = self.plot.get_axes() axesBar = self.plot.get_axes_bar() xpos = event.xdata ypos = event.ydata text = "" if (xpos is None or ypos is None or self.spectrum is None or event.inaxes is None): spectrum = None elif event.inaxes == axesBar: spectrum = None level = self.plot.get_bar().norm.inverse(ypos) text = "{}".format(format_precision(self.settings, level=level)) elif self.settings.display == Display.PLOT: timeStamp = max(self.spectrum) spectrum = self.spectrum[timeStamp] elif self.settings.display == Display.SPECT: timeStamp = num2epoch(ypos) if timeStamp in self.spectrum: spectrum = self.spectrum[timeStamp] else: nearest = min(self.spectrum.keys(), key=lambda k: abs(k - timeStamp)) spectrum = self.spectrum[nearest] elif self.settings.display == Display.SURFACE: spectrum = None coords = axes.format_coord(event.xdata, event.ydata) match = re.match('x=([-|0-9|\.]+).*y=([0-9|\:]+).*z=([-|0-9|\.]+)', coords) if match is not None and match.lastindex == 3: freq = float(match.group(1)) level = float(match.group(3)) text = "{}, {}".format( *format_precision(self.settings, freq, level)) else: spectrum = None if spectrum is not None and len(spectrum) > 0: x = min(spectrum.keys(), key=lambda freq: abs(freq - xpos)) if min(spectrum.keys(), key=float) <= xpos <= max(spectrum.keys(), key=float): y = spectrum[x] text = "{}, {}".format(*format_precision(self.settings, x, y)) else: text = format_precision(self.settings, xpos) markers = find_artists(self.figure, 'peak') markers.extend(find_artists(self.figure, 'peakThres')) hit = False for marker in markers: if isinstance(marker, Line2D): location = marker.get_path().vertices[0] markX, markY = axes.transData.transform(location) dist = abs(math.hypot(event.x - markX, event.y - markY)) if dist <= 5: if self.settings.display == Display.PLOT: tip = "{}, {}".format(*format_precision( self.settings, location[0], location[1])) else: tip = "{}".format( format_precision(self.settings, location[0])) self.toolTip.SetTip(tip) hit = True break self.toolTip.Enable(hit) self.status.set_info(text, level=None)
def num2Epoch(num): timestamp = mdates.num2epoch(num) return timestamp
def startDateChanged(self): print("start date changed") print(self.startDate) print(int(round(mdates.num2epoch(mdates.date2num(self.time[0]))))) print(QDateTime.toSecsSinceEpoch(self.ui.startDateEdit.dateTime())) print(self.ui.startDateEdit.dateTime())
def test_num2epoch(self): x = self.days self.assertEqual(str(dates.num2epoch(int(x))), self.expectedDate)
def __on_motion(self, event): xpos = event.xdata ypos = event.ydata text = "" if xpos is None or ypos is None or self.spectrum is None: return if self.settings.display == Display.PLOT: timeStamp = max(self.spectrum) spectrum = self.spectrum[timeStamp] elif self.settings.display == Display.SPECT: timeStamp = num2epoch(ypos) if timeStamp in self.spectrum: spectrum = self.spectrum[timeStamp] else: nearest = min(self.spectrum.keys(), key=lambda k: abs(k - timeStamp)) spectrum = self.spectrum[nearest] elif self.settings.display == Display.SURFACE: spectrum = None coords = self.plot.get_axes().format_coord(event.xdata, event.ydata) match = re.match('x=([-|0-9|\.]+).*y=([0-9|\:]+).*z=([-|0-9|\.]+)', coords) if match is not None and match.lastindex == 3: freq = float(match.group(1)) level = float(match.group(3)) text = "{}, {}".format(*format_precision(self.settings, freq, level)) else: spectrum = None if spectrum is not None and len(spectrum) > 0: x = min(spectrum.keys(), key=lambda freq: abs(freq - xpos)) if min(spectrum.keys(), key=float) <= xpos <= max(spectrum.keys(), key=float): y = spectrum[x] text = "{}, {}".format(*format_precision(self.settings, x, y)) else: text = format_precision(self.settings, xpos) self.status.set_info(text, level=None) axes = self.figure.get_axes()[0] markers = find_artists(self.figure, 'peak') markers.extend(find_artists(self.figure, 'peakThres')) hit = False for marker in markers: if isinstance(marker, Line2D): location = marker.get_path().vertices[0] markX, markY = axes.transData.transform(location) dist = abs(math.hypot(event.x - markX, event.y - markY)) if dist <= 5: if self.settings.display == Display.PLOT: tip = "{}, {}".format(*format_precision(self.settings, location[0], location[1])) else: tip = "{}".format(format_precision(self.settings, location[0])) self.toolTip.SetTip(tip) hit = True break self.toolTip.Enable(hit)
count += 1 if count % 1000 == 0: pb(".") time = t.pop(1) tmp00 = t[:] t = time.split(".") # separating milisec from time try: d = mdates.date2num(strptime(t[0], "%Y-%m-%d %H:%M:%S")) except KeyboardInterrupt, msg: print(" \nKeyboardInterrupt: 04") sys.exit() try: d = mdates.num2epoch(d) + float(".%s" % t[1]) except IndexError: d = mdates.num2epoch(d) finally: base = tmp00.pop(0) # r_server = redis.Redis('localhost') # r_server.set('name', 'DES') return [base, round(d, 3), fix_numbers(tmp00)] def main(f): with open(f) as f: # parsing raw data f = [x.replace("\r\n", "").split(",")[-4:] for x in f.readlines()] # only official heading supported, so far!
def load_file(self, File): print("trying to read " + File) self.clear_plot_data() self.clear_plots() with open(File) as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') line_count = 0 print("started reading csv") for row in csv_reader: if (row != []): if ( float(row[TEMP_DHT_INDEX]) < 120 ): # kind of a hack filter, the temperature data reads 200 if the sensor fails, and skipping a line every now and then is no big deal. self.time.append( mdates.epoch2num(float(row[TIME_INDEX]))) self.T_dht.append(float(row[TEMP_DHT_INDEX])) self.H_dht.append(float(row[HUM_DHT_INDEX])) self.co2.append(float(row[CO2_INDEX])) self.tvoc.append(float(row[TVOC_INDEX])) self.T_hdc.append(float(row[TEMP_HDC_INDEX])) self.H_hdc.append(float(row[HUM_HDC_INDEX])) self.lux.append(float(row[LUX_INDEX])) print("read csv") self.time = mdates.num2date(self.time) self.T_dht = np.array(self.T_dht) self.co2 = np.array(self.co2) self.tvoc = np.array(self.tvoc) self.lux = np.array(self.lux) print(len(self.time)) if (len(self.time) > 1): self.ui.plot1.update_figure(self.time, self.T_dht) self.ui.plot2.update_figure(self.time, self.co2) self.ui.plot3.update_figure(self.time, self.tvoc) self.ui.plot4.update_figure(self.time, self.lux) self.ui.plot1.setTitle("temp") self.ui.plot2.setTitle("co2") self.ui.plot3.setTitle("tvoc") self.ui.plot4.setTitle("lux") print("loaded times ") startTime = int( round(mdates.num2epoch(mdates.date2num(self.time[0])))) endTime = int( round( mdates.num2epoch( mdates.date2num(self.time[len(self.time) - 1])))) print(endTime - startTime) minStartDate = QDateTime.fromTime_t(startTime) maxStartDate = QDateTime.fromTime_t(endTime) self.startDate = self.time[1] self.endDate = self.time[len(self.time) - 1] self.ui.startDateEdit.setMinimumDateTime(minStartDate) self.ui.startDateEdit.setMaximumDateTime(maxStartDate) self.ui.endDateEdit.setMinimumDateTime(minStartDate) self.ui.endDateEdit.setMaximumDateTime(maxStartDate)
def __on_motion(self, event): label = '' if event.xdata is not None and event.xdata >= 1: timestamp = num2epoch(event.xdata) label = datetime.datetime.fromtimestamp(timestamp).strftime('%c') self._textFreq.SetLabel(label)