1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | # -*- coding: utf-8 -*- from __future__ import print_function from __future__ import unicode_literals from __future__ import with_statement import matplotlib.pyplot as plt #from matplotlib import rcParams #rcParams['figure.figsize'] = (10, 6) #rcParams['legend.fontsize'] = 16 #rcParams['axes.labelsize'] = 16 #rcParams['pdf.fonttype'] = 42 #rcParams['ps.fonttype'] = 42 # default type 3 is not supported by ACM #rcParams['text.usetex']=True #rcParams['font.family']='serif' #) # ACM font requirement from matplotlib import cm from matplotlib.lines import Line2D import matplotlib import os import sys import logging import time import pickle import numpy as np import pandas as pd class Obj(object): def __init__(self, *args, **kwargs): self.data = [] def do(self): self.vis() self.exit() def exit(self): log.info("end") def _extract_line(self, line): dt = line.split() ts = float(dt[0]) x = float(dt[1]) y = float(dt[2]) z = float(dt[3]) return [ts, x, y, z] def extract(self): datas = list() with open(c.postion_log) as fin: li = list() def add_data(): if len(li) > 0: data = np.vstack(li) datas.append(data) for line in fin: if line.startswith(c.start_flag): print("start: ", line) add_data() li = list() else: infos = self._extract_line(line) li.append(infos) add_data() return datas def load_or_extract(self): fn = c.postion_data if os.path.isfile(fn): self.data = self._load(fn) else: self.data = self.extract() self._cache(self.data, fn) def vis(self): self.load_or_extract() if 1: data = self.data[-1] X = data[:,0] Y = data[:,2] + np.pi / 2.0 formatter = matplotlib.ticker.ScalarFormatter() formatter.set_scientific(True) formatter.set_powerlimits((0,0)) ax = plt.subplot(1, 1, 1) #ax.yaxis.set_major_formatter(formatter) #plt.ylabel("recevied bytes") #plt.xlabel("timeline") #plt.ylim(ymin=0, ymax=1.05 * np.max(Y)) #plt.xlim(xmax=np.max(X)) cosxs = X * np.cos(Y) sinxs = X * np.sin(Y) c = np.arange(X.shape[0]) c = np.random.randn(X.shape[0], 1) plt.scatter(cosxs, sinxs, marker='o', cmap='Blues', c=c, alpha=1, edgecolors='none') #plt.scatter(cosxs, sinxs, marker='o', cmap='viridis', c=c, alpha=1, edgecolors='none') plt.plot([0], [0], 'o', color='r') plt.colorbar() #plt.plot(X, Y, '-', color="g") def add_line(x, y): x = 1.05 * np.array(x) y = 1.05 * np.array(y) l = Line2D(x, y, marker='.', linewidth=1, color='black') ax.add_line(l) add_line([np.min(cosxs), np.max(cosxs)], [0.0, 0.0]) add_line([0.0, 0.0], [np.min(sinxs), np.max(sinxs)]) #ax = plt.subplot(2, 1, 2) #ax.yaxis.set_major_formatter(formatter) #plt.xlim(xmax=np.max(X)) #plt.ylim(ymin=0, ymax=1.05 * np.max(cY)) #plt.xlabel("timeline") #plt.ylabel("cumulative recevied bytes") #plt.plot(X, cY, "-", color="g") #plt.scatter(x=index, y=size, c="g", label="decoding timestamp") #plt.scatter(x=ts2, y=size, c="r", label="feed to decoding") #plt.plot(index, size, color='g', marker='o', label='docoding timestamp') #plt.plot(ts2, size, '-', color='g', label='time of feeding to decoder') #plt.legend() plt.show() plt.savefig(c.postion_fig) def _cache(self, data, fout): with open(fout, "wb") as f: pickle.dump(data, f) log.info("save data: %s" %fout) def _load(self, fout): if not os.path.isfile(fout): log.fatal("file not exist: %s" % fout) return None with open(fout, "rb") as f: data = pickle.load(f) log.info("load data: %s" % fout) return data if __name__ == "__main__": obj = Obj() obj.do() |