Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. from enum import unique
  2.  
  3. import pandas as pd
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. import operator
  7. import seaborn as sns
  8. from scipy import stats
  9. from mpl_toolkits.mplot3d import Axes3D
  10. from os import path
  11. from matplotlib.colors import LogNorm
  12.  
  13.  
  14. from collections import Counter
  15. import sys
  16. import math
  17. from sklearn.preprocessing import minmax_scale
  18. # from matplotlib.colors import LogNorm
  19. from sklearn import preprocessing
  20. import argparse
  21. import joblib
  22. import os
  23.  
  24.  
  25.  
  26. headers = {"C-100003", "2018-07-27", "FOD Turbine",
  27. "C-100005", "2018-06-25", "FOD Compressor",
  28. "C-100004", "2018-06-07", "FOD Compressor",
  29. "C-100002", "2018-06-08", "FOD Compressor",
  30. "C-100001", "2018-07-18", "FOD Compressor"}
  31.  
  32. data = pd.read_csv("Multi_P1FWM_turbo_failure_deviation_ano.csv", header=0)
  33.  
  34. ##############################################################
  35.  
  36. parser = argparse.ArgumentParser()
  37. parser.add_argument('-a', "--anonymous", action='store_true', default=False, help='Hide axes')
  38. parser.add_argument('-t', "--truck", type=str, default=None, help='PLot one truck ID')
  39. parser.add_argument('-f', "--filename", type=str, default="Multi_P1FWM_turbo_failure_deviation_ano.csv", help='CSV filename')
  40. parser.add_argument('-n', "--normalise", action='store_true', default=False, help='Normalise')
  41. parser.add_argument("--max_pics", type=int, default=16, help='Max histograms on a plot')
  42. parser.add_argument("--cols", type=int, default=4, help='Number of histogram columns in plot')
  43. parser.add_argument("--rows", type=int, default=None, help='Number of rows to read from CSV file')
  44. parser.add_argument("--top", type=int, default=None, help='Top n')
  45. parser.add_argument("--start", type=int, default=0, help='Start histogram')
  46. parser.add_argument("--trucks", type=str, default=None, help='Multiple chassis IDs, comma seperated')
  47.  
  48.  
  49. args = parser.parse_args()
  50. print("Reading data...", end="", flush=True)
  51. if not os.path.exists("df_train.pickle"):
  52. lines = [] # list(range(1, 100000))
  53. df_train = pd.read_csv(args.filename,
  54. sep=";", dtype={'VEHICL_ID': str},
  55. # skiprows=lines,
  56. nrows=args.rows
  57. )
  58. print( "pickling...", end="", flush=True )
  59. joblib.dump( df_train,"df_train.pickle" )
  60. df_train.to_pickle("df_train.pickle")
  61. else:
  62. print("from pickle...", end="", flush=True)
  63. df_train = pd.read_pickle("df_train.pickle")
  64. print("Ready")
  65. print(df_train.shape)
  66. print(df_train.head(2))
  67. print(data.shape)
  68.  
  69.  
  70.  
  71. c = data[data['VEHICLE_ID'] == 1]
  72. d = data[data['VEHICLE_ID'] == 2]
  73. e = data[data['VEHICLE_ID'] == 3]
  74. f = data[data['VEHICLE_ID'] == 4]
  75. g = data[data['VEHICLE_ID'] == 5]
  76.  
  77. m1 = c.SEND_DATETIME
  78. m2 = d.SEND_DATETIME
  79. m3 = e.SEND_DATETIME
  80. m4 = f.SEND_DATETIME
  81. m5 = g.SEND_DATETIME
  82.  
  83. m1 = pd.to_datetime(m1)
  84. m2 = pd.to_datetime(m2)
  85. m3 = pd.to_datetime(m3)
  86. m4 = pd.to_datetime(m4)
  87. m5 = pd.to_datetime(m5)
  88.  
  89.  
  90. n=m1.dt.year
  91. print(m1)
  92. A=g.sample(n=5)
  93. B=g.drop(['VEHICLE_ID','SEND_DATETIME','PARAMETER_CODE','T_CHASSIS','Y_INDEX_2_X_INDEX_13_VALUE'] ,axis=1)
  94. print(B.shape)
  95. fig, axes = plt.subplots(nrows=1, ncols=g.shape[0])
  96. fig.set_figheight(20)
  97. fig.set_figwidth(20)
  98. num_pics = data.shape[0]
  99. if num_pics > args.max_pics:
  100. st = num_pics - args.max_pics
  101. num_pics = args.max_pics
  102. print(num_pics)
  103.  
  104. lable="SEND_DATETIME"
  105. cols = args.cols
  106. rows = int(num_pics/cols)+ 1*((num_pics % cols)!=0)
  107. sp=1
  108. pc=0
  109. fig = plt.figure(figsize=(cols*3, rows*3)) #plt.figure(figsize=(sz,sz))
  110. plt.subplots_adjust( hspace=0.7, wspace=0.5 )
  111.  
  112. for index, row in B.iterrows():
  113. ax = fig.add_subplot(rows, cols, sp)
  114.  
  115. print(type(row))
  116. xx = row.iloc[:19]
  117. yy = row.iloc[19:]
  118.  
  119. drop_reshape1 = row.values.reshape(20, 20)
  120. im = drop_reshape1.astype(int)
  121. im = np.flipud(im) # rot90(im) #flipud(im) #rot90()
  122.  
  123. if args.normalise:
  124. _min = 0
  125. _max = 1
  126. im += -(np.min(im))
  127. im /= np.max(im) / (_max - _min)
  128. #ax.append( fig.add_subplot(20, 20, index+1) )
  129.  
  130. im_masked = np.ma.masked_where(im == 0, im)
  131. plt.imshow(im_masked, interpolation='none')
  132.  
  133. ax.set_aspect('equal')
  134. ax.get_xaxis().set_ticks([0, 19])
  135. ax.get_yaxis().set_ticks([])
  136. #
  137. ax.set_xticks(np.arange(-.5, 19, 1), minor=True);
  138. ax.set_yticks(np.arange(-.5, 19, 1), minor=True);
  139. ax.grid(which='minor', color='w', linestyle='-', linewidth=1)
  140. #
  141. if not args.anonymous:
  142. ax.set_xlabel("engine speed")
  143. ax.set_ylabel("engine torque")
  144. plt.colorbar(orientation='vertical', ax=ax, format='%.1f', fraction=0.0408, pad=0.04)
  145. # plt.clim(0, 10);
  146. else:
  147. plt.colorbar(orientation='vertical', ax=ax, ticks=[])
  148. sp += 1
  149. if not args.anonymous:
  150. fig.suptitle('VEHICLE_ID'+ ": " + str('SEND_DATETIME') + ' P1FWM') # Training data '+str(st)+" +"+str(nn) )
  151. fn = "training_" + str('SEND_DATETIME') + "_" + str(index) + "+" + str(pc)
  152. if args.normalise:
  153. fn += "_N"
  154. fn += ".png"
  155. print("Saving", fn)
  156. if os.path.exists(fn):
  157. os.remove(fn)
  158. fig.savefig(fn, dpi=288)
  159.  
  160. plt.show()
  161. #ax.set_aspect('equal')
  162. #ax.get_xaxis().set_ticks([0, 19])
  163. #ax.get_yaxis().set_ticks([])
  164. #
  165. #ax.set_xticks(np.arange(-.5, 19, 1), minor=True);
  166. #ax.set_yticks(np.arange(-.5, 19, 1), minor=True);
  167. #ax.grid(which='minor', color='w', linestyle='-', linewidth=1)
  168. #
  169. #if not args.anonymous:
  170. #ax.set_xlabel("engine speed")
  171. #ax.set_ylabel("engine torque")
  172. #plt.colorbar(orientation='vertical', ax=ax, format='%.1f', fraction=0.0408, pad=0.04)
  173. # plt.clim(0, 10);
  174. #else:
  175. #plt.colorbar(orientation='vertical', ax=ax, ticks=[])
  176.  
  177. #print(drop_reshape1.shape)
  178. #x = drop_reshape1[:,19]
  179. #y = drop_reshape1[19,:]
  180. #fig2 = plt.figure()
  181. #plt.hist2d(x, y, bins=100)
  182. #plt.xlabel('x')
  183. #plt.ylabel('y')
  184. #cbar = plt.colorbar()
  185. #cbar.ax.set_ylabel('Counts')
  186. #heat plots
  187.  
  188. #df = pd.DataFrame(drop_reshape1)
  189. #df.columns = df.columns + 1
  190. #df.index = df.index + 1
  191. #f, ax = plt.subplots(figsize=(10, 6))
  192. #corr = df.corr()
  193. #hm = sns.heatmap(round(corr, 2), annot=True, ax=ax, cmap="coolwarm", fmt='.2f',linewidths=.05)
  194. #f.subplots_adjust(top=0.93)
  195. #t = f.suptitle('Wine Attributes Correlation Heatmap', fontsize=14)
  196.  
  197. #img plots
  198.  
  199. #histogram plots
  200. #plt.hist(drop_reshape1)
  201. #plt.title(m5[index])
  202. #plt.show()
  203. #fig.savefig("dataname_"+str(index)+".png")
  204.  
  205.  
  206. #drop = m.drop(['VEHICLE_ID','SEND_DATETIME','PARAMETER_CODE','T_CHASSIS','Y_INDEX_2_X_INDEX_13_VALUE'] ,axis=1)
  207. #drop_array=np.array(B)
  208. #drop_reshape = drop_array.reshape(20,20)
  209. #plt.title('2015-06-04 19:42:43')
  210. #plt.hist(drop_reshape)
  211. #plt.show()
  212.  
  213. #a = np.array(m)
  214. #b = np.array(l)
  215. #d = np.array(data)
  216. #sort = sorted(data,key=operator.itemgetter(1))
  217.  
  218. #s.plot()
  219. #plt.savefig('vehicle id ')
  220. #print(l)
  221. #print(m)
  222. #v=data.ix[0:,['SEND_DATETIME','Y_INDEX_1_X_INDEX_1_VALUE']]
  223. #print (grouped)
  224. #data = data[data['VEHICLE_ID'] == 1]
  225. #data = data.set_index(['SEND_DATETIME'])
  226. #data.plot()
  227. #plt.show()
  228. # Assign colors for each airline and the names
  229. # Make the histogram using a list of lists
  230. # Normalize the flights and assign colors and names
  231. #plt.hist([x1, x2, x3, x4, x5], bins=int(180 / 15), normed=True,
  232. #color=colors, label=names)
  233. # Plot formatting
  234. #g= np.reshape(a, (np.product(a.shape),))
  235. #k= np.reshape(m, (np.product(m.shape),))
  236.  
  237. #print(g.shape)
  238. #plt.ploy(a,)
  239. #plt.show()
  240.  
  241. #BY FAR BEST PLOTS
  242. # use the function regplot to make a scatterplot
  243. #sns.regplot(x=data["VEHICLE_ID"], y=data["Y_INDEX_1_X_INDEX_1_VALUE"])
  244. #plt.show()
  245.  
  246. # Without regression fit:
  247. #sns.regplot(x=data["VEHICLE_ID"], y=data["Y_INDEX_1_X_INDEX_1_VALUE"], fit_reg=False)
  248. #plt.show()
  249. #BY FAR BEST PLOTS
  250.  
  251. #fig = plt.figure()
  252. #ax = fig.add_subplot(111, projection='3d')
  253. #ax.scatter(data['VEHICLE_ID'], data['Y_INDEX_1_X_INDEX_1_VALUE'], data['C-100002'], c='skyblue', s=60)
  254. #ax.view_init(30, 185)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement