question1.py 6.11 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 ``````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 71 ``````#Test if it is a Nan `````` Tianyang committed Jun 13, 2019 72 73 74 75 ``````def testNan (x): test = x != x return test `````` Tianyang committed Jun 20, 2019 76 ``````#Map reduce fonction `````` Tianyang committed Jun 13, 2019 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 ``````def mapReduce_mmm(data,timeNB,targetNB): results = dict() for row in data.result(): 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] if testNan(data_time) or testNan(data_target): 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 100 ``````#Zip the values in 3 list, one by one `````` Tianyang committed Jun 13, 2019 101 102 103 104 105 106 107 ``````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 108 ``````#Fonction 1: the history courbe graph `````` Tianyang committed Jun 13, 2019 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 ``````def drawCourbe_history(session,time,target,timeNB,targetNB,espace): data = session.execute_async("select * from caitiany.database_espace where station = '%s'"%espace ) results = mapReduce_mmm(data,timeNB,targetNB) 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 139 140 `````` plt.savefig("Projet-NF26/question1.png") print ("Generate successfully") `````` Tianyang committed Jun 13, 2019 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 `````` def checkNBvariable (x): i=0 for each in table_variable: if x == each: return i i += 1 print ('Doesn\'t exist!!') def drawCourbe_season(session,season,target,targetNB,espace): data = session.execute_async("select * from caitiany.database_espace where station = '%s'"%espace ) `````` Tianyang committed Jun 20, 2019 156 `````` #We do the same map reduce as fonction 1 by fixing the time as season `````` Tianyang committed Jun 13, 2019 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 `````` results = mapReduce_mmm(data,2,targetNB) 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) 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) plt.legend(bbox_to_anchor=(1.0, 1), loc=1, borderaxespad=0.) `````` Tianyang committed Jun 20, 2019 176 177 `````` plt.savefig("Projet-NF26/question1_season.png") print ("Generate successfully") `````` Tianyang committed Jun 13, 2019 178 179 `````` `````` Tianyang committed Jun 20, 2019 180 ``````#Choose the data of the season we want `````` Tianyang committed Jun 13, 2019 181 182 183 184 185 186 187 188 189 190 191 ``````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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 `````` 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]: ") time = input("By which kind of time [year,season,month,etc]: ") 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)``````