question1.py 6.77 KB
 Tianyang committed Jun 13, 2019 1 2 3 ``````import numpy as np import matplotlib.pyplot as plt from functools import reduce `````` Tianyang committed Jun 15, 2019 4 ``````from database_pre1 import connection `````` Tianyang committed Jun 13, 2019 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 ``````import matplotlib.pyplot as plt table_variable = ['station varchar', 'year', 'season', 'month', 'day', 'hour', 'minute', 'lon', 'lat', 'tmpf', 'dwpf', 'relh', 'drct', 'sknt', 'p01i', 'alti', 'mslp', 'vsby', 'gust', 'skyc1', 'skyc2', 'skyc3', 'skyc4', 'skyl1', 'skyl2', 'skyl3', 'skyl4', 'wxcodes', 'ice_accretion_1hr', 'ice_accretion_3hr', 'ice_accretion_6hr', 'peak_wind_gust', 'peak_wind_drct', 'peak_wind_time', 'feel', 'metar'] def add (x,y): return x+y def fmax(x,y): return max(x,y) def fmin(x,y): return min(x,y) #caculate mean, max, min reduce #input [count,mean,max,min] def reduceFonction (x,y): result = [] fonctions = {0:'add',1:'add',2:'fmax',3:'fmin'} for i in range(4): result.append(reduce(eval(fonctions.get(i)),[x[i],y[i]])) return result #input [valeur] -> [count,mean,max,min] def mapFonction1 (x): return [1,x,x,x] #input [count,mean,max,min] -> [mean,max,min] def mapFonction2 (x): return [x[1]/x[0],x[2],x[3]] `````` Tianyang committed Jun 20, 2019 72 ``````#Map reduce fonction `````` Tianyang committed Jun 22, 2019 73 ``````def mapReduce_mmm(timeNB,targetNB,espace): `````` Tianyang committed Jun 13, 2019 74 `````` results = dict() `````` Tianyang committed Jun 22, 2019 75 `````` for row in session.execute("select * from caitiany.database_espace where station = '%s'"%espace ): `````` Tianyang committed Jun 13, 2019 76 77 78 79 80 81 82 83 84 `````` if timeNB == 1: data_time = row[timeNB] elif timeNB == 2: data_time = (row[timeNB-1],row[timeNB]) elif timeNB == 3: data_time = (row[timeNB-2],row[timeNB-1],row[timeNB]) else: assert 1==2, "Doesn\'t exits!" data_target = row[targetNB] `````` Tianyang committed Jun 22, 2019 85 `````` if str(data_time) == 'null' or data_target == None: `````` Tianyang committed Jun 13, 2019 86 87 88 89 90 91 92 93 94 95 `````` continue if results.get(data_time) is None: results[data_time] = mapFonction1(data_target) else: mapresult = mapFonction1(data_target) results[data_time] = reduceFonction(mapresult,results[data_time]) for eachTime in results: results[eachTime] = mapFonction2(results[eachTime]) return results `````` Tianyang committed Jun 20, 2019 96 ``````#Zip the values in 3 list, one by one `````` Tianyang committed Jun 13, 2019 97 98 99 100 101 102 103 ``````def zipValues (values): result = [[],[],[]] for i in range(3): for each in values: result[i].append(each[i]) return result `````` Tianyang committed Jun 20, 2019 104 ``````#Fonction 1: the history courbe graph `````` Tianyang committed Jun 13, 2019 105 ``````def drawCourbe_history(session,time,target,timeNB,targetNB,espace): `````` Tianyang committed Jun 22, 2019 106 107 `````` #data = session.execute_async("select * from caitiany.database_espace where station = '%s'"%espace ) results = mapReduce_mmm(timeNB,targetNB,espace) `````` Tianyang committed Jun 13, 2019 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 `````` keys = list(results.keys()) values = list(results.values()) zipped_result = zipValues(values) if isinstance(keys[0],tuple): for each in keys: index_each = keys.index(each) each = list(each) if timeNB == 2: each[0] = str(each[0]) keys[index_each] = ".".join(each) elif timeNB == 3: each[0] = str(each[0]) each[2] = str(each[2]) keys[index_each] = each[0] +'.'+ each[2] fig, ax = plt.subplots(1, 1) plt.plot(keys,zipped_result[0],'x-',label="mean") plt.plot(keys,zipped_result[1],'+-',label="max") plt.plot(keys,zipped_result[2],'b--',label="min") plt.xticks(keys, keys, rotation=45, fontsize=5) plt.ylabel(target) plt.grid(True) if timeNB == 3: for label in ax.get_xticklabels(): label.set_visible(False) for label in ax.get_xticklabels()[::6]: label.set_visible(True) plt.legend(bbox_to_anchor=(1.0, 1), loc=1, borderaxespad=0.) `````` Tianyang committed Jun 20, 2019 135 136 `````` plt.savefig("Projet-NF26/question1.png") print ("Generate successfully") `````` Tianyang committed Jun 13, 2019 137 138 `````` `````` Tianyang committed Jun 21, 2019 139 ``````#Check which number of the indicateur `````` Tianyang committed Jun 13, 2019 140 141 142 143 144 145 146 147 148 149 150 ``````def checkNBvariable (x): i=0 for each in table_variable: if x == each: return i i += 1 print ('Doesn\'t exist!!') `````` Tianyang committed Jun 20, 2019 151 152 153 154 155 156 157 158 159 ``````#Caculate the mean of the values of each season def caculateMean_Season(result): i = 0 total = 0 for each in result: total += each i += 1 return total/i , i `````` Tianyang committed Jun 21, 2019 160 ``````#Draw the courbe of the fonction2 `````` Tianyang committed Jun 13, 2019 161 ``````def drawCourbe_season(session,season,target,targetNB,espace): `````` Tianyang committed Jun 22, 2019 162 `````` #data = session.execute_async("select * from caitiany.database_espace where station = '%s'"%espace ) `````` Tianyang committed Jun 20, 2019 163 `````` #We do the same map reduce as fonction 1 by fixing the time as season `````` Tianyang committed Jun 22, 2019 164 `````` results = mapReduce_mmm(2,targetNB,espace) `````` Tianyang committed Jun 13, 2019 165 166 167 168 169 170 171 172 173 174 175 `````` results = seprateSeason(results,season) keys = list(results.keys()) values = list(results.values()) zipped_result = zipValues(values) for each in keys: index_each = keys.index(each) each = list(each) each[0] = str(each[0]) keys[index_each] = ".".join(each) fig, ax = plt.subplots(1, 1) `````` Tianyang committed Jun 20, 2019 176 `````` mean_season_mean, longth = caculateMean_Season(zipped_result[0]) `````` Tianyang committed Jun 13, 2019 177 `````` plt.plot(keys,zipped_result[0],'x-',label="mean") `````` Tianyang committed Jun 20, 2019 178 179 180 `````` plt.plot(keys,[mean_season_mean for i in range(longth)],'--') mean_season_mean, longth = caculateMean_Season(zipped_result[1]) `````` Tianyang committed Jun 13, 2019 181 `````` plt.plot(keys,zipped_result[1],'+-',label="max") `````` Tianyang committed Jun 20, 2019 182 183 184 `````` plt.plot(keys,[mean_season_mean for i in range(longth)],'--') mean_season_mean, longth = caculateMean_Season(zipped_result[2]) `````` Tianyang committed Jun 13, 2019 185 `````` plt.plot(keys,zipped_result[2],'b--',label="min") `````` Tianyang committed Jun 20, 2019 186 187 `````` plt.plot(keys,[mean_season_mean for i in range(longth)],'--') `````` Tianyang committed Jun 13, 2019 188 189 190 191 `````` plt.xticks(keys, keys, rotation=45, fontsize=5) plt.ylabel(target) plt.grid(True) plt.legend(bbox_to_anchor=(1.0, 1), loc=1, borderaxespad=0.) `````` Tianyang committed Jun 20, 2019 192 193 `````` plt.savefig("Projet-NF26/question1_season.png") print ("Generate successfully") `````` Tianyang committed Jun 13, 2019 194 195 `````` `````` Tianyang committed Jun 20, 2019 196 ``````#Choose the data of the season we want `````` Tianyang committed Jun 13, 2019 197 198 199 200 201 202 203 204 205 206 207 ``````def seprateSeason (results,season): output = dict() for each in results: if season in each: output[each] = results[each] return output if __name__ == "__main__": session = connection() `````` Tianyang committed Jun 20, 2019 208 209 210 211 212 `````` choice = int(input("Which kind of service do you want?\n1.Station history\n2.Check history by seasons\nYour choice: ")) #if choice == 1, we will use the fonction 1 if choice == 1: espace = input("Please enter which station you want to search [LEBZ,LETO,etc]: ") `````` Tianyang committed Jun 20, 2019 213 `````` time = input("By which kind of time [year,season,month]: ") `````` Tianyang committed Jun 20, 2019 214 215 216 217 218 219 220 221 222 223 224 `````` target = input("Which indicator do you want to check [tmpf,dwpf,etc]: ") timeNB = checkNBvariable(time) targetNB = checkNBvariable(target) drawCourbe_history(session,time,target,timeNB,targetNB,espace) else: #Fonction 2, the check according to the seasons espace = input("Please enter what station you want to search [LEBZ,LETO,etc]: ") target = input("Which indicator do you want to check [tmpf,dwpf,etc]: ") targetNB = checkNBvariable(target) season = input("Please enter which season you want to search [Spring,Summer,Autumn,Winter]: ") drawCourbe_season(session,season,target,targetNB,espace)``````