Mercurial > hg > nsaunier > traffic-intelligence
comparison python/pavement.py @ 447:7ef40014236c
updated weather indicator calculations
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Tue, 04 Feb 2014 15:00:39 -0500 |
| parents | a65a14c90834 |
| children | 0b15fdeb903e |
comparison
equal
deleted
inserted
replaced
| 446:a65a14c90834 | 447:7ef40014236c |
|---|---|
| 117 else: | 117 else: |
| 118 somme_eau, somme_neige, somme_abrasif, somme_sel, somme_lc, somme_lrg, somme_lrd, premiere_neige, autres_abrasifs, neigeMTQ_sup_seuil = '','','','','','','','','','' | 118 somme_eau, somme_neige, somme_abrasif, somme_sel, somme_lc, somme_lrg, somme_lrd, premiere_neige, autres_abrasifs, neigeMTQ_sup_seuil = '','','','','','','','','','' |
| 119 | 119 |
| 120 return (somme_eau, somme_neige, neigeMTQ_sup_seuil, somme_abrasif, somme_sel, somme_lc, somme_lrg, somme_lrd, premiere_neige, autres_abrasifs) | 120 return (somme_eau, somme_neige, neigeMTQ_sup_seuil, somme_abrasif, somme_sel, somme_lc, somme_lrg, somme_lrd, premiere_neige, autres_abrasifs) |
| 121 | 121 |
| 122 def ecWeatherIndicators(data, startDate, endDate, snowThreshold): | 122 def ecWeatherIndicators(data, startDate, endDate, snowThreshold, minProportionMeasures = 0.): |
| 123 '''Computes the indicators from Environment Canada files | 123 '''Computes the indicators from Environment Canada files |
| 124 (loaded as a recarray using csv2rec in data), | 124 (loaded as a recarray using csv2rec in data), |
| 125 between start and end dates (datetime.datetime objects)''' | 125 between start and end dates (datetime.datetime objects) |
| 126 | |
| 127 minProportionMeasures is proportion of measures necessary to consider the indicators''' | |
| 128 from matplotlib.mlab import find | |
| 126 nbre_jours_T_negatif,nbre_jours_gel_degel,pluie_tot,neige_tot,ecart_type_T = 0,0,0,0,0 | 129 nbre_jours_T_negatif,nbre_jours_gel_degel,pluie_tot,neige_tot,ecart_type_T = 0,0,0,0,0 |
| 127 compteur,nbre_jours_gel_consecutifs=0,0 | 130 compteur,nbre_jours_gel_consecutifs=0,0 |
| 128 tmoys = [] | 131 tmoys = [] |
| 129 seuils_T = [20,15,10,5] | 132 seuils_T = [20,15,10,5] |
| 130 deltas_T = [0,0,0,0] | 133 deltas_T = [0,0,0,0] |
| 131 for i in range(int((endDate - startDate).days)+1): | 134 startIndex = find(data['date'] == startDate) |
| 135 nDays = (endDate - startDate).days+1 | |
| 136 for i in range(startIndex, startIndex+int(nDays)): | |
| 132 if data['tmax'][i] != '' and data['tmax'][i] != None: | 137 if data['tmax'][i] != '' and data['tmax'][i] != None: |
| 133 tmax = float(data['tmax'][i].replace(',','.')) | 138 tmax = float(data['tmax'][i].replace(',','.')) |
| 134 else: | 139 else: |
| 135 tmax = None | 140 tmax = None |
| 136 if data['tmin'][i] != '' and data['tmin'][i] != None: | 141 if data['tmin'][i] != '' and data['tmin'][i] != None: |
| 157 nbre_jours_gel_consecutifs = compteur | 162 nbre_jours_gel_consecutifs = compteur |
| 158 compteur = 0 | 163 compteur = 0 |
| 159 else: | 164 else: |
| 160 compteur = 0 | 165 compteur = 0 |
| 161 nbre_jours_gel_consecutifs = max(nbre_jours_gel_consecutifs,compteur) | 166 nbre_jours_gel_consecutifs = max(nbre_jours_gel_consecutifs,compteur) |
| 162 ecart_type_T = np.std(tmoys) | 167 if float(len(tmoys))/nDays >= minProportionMeasures: |
| 163 if neige_tot < snowThreshold: | 168 ecart_type_T = np.std(tmoys) |
| 164 neigeEC_sup_seuil = 0 | 169 if neige_tot < snowThreshold: |
| 165 else: | 170 neigeEC_sup_seuil = 0 |
| 166 neigeEC_sup_seuil = 1 | 171 else: |
| 167 | 172 neigeEC_sup_seuil = 1 |
| 168 return (nbre_jours_T_negatif,nbre_jours_gel_degel, deltas_T, nbre_jours_gel_consecutifs, pluie_tot, neige_tot, neigeEC_sup_seuil, ecart_type_T) | 173 return (nbre_jours_T_negatif,nbre_jours_gel_degel, deltas_T, nbre_jours_gel_consecutifs, pluie_tot, neige_tot, neigeEC_sup_seuil, ecart_type_T) |
| 169 | 174 else: |
| 175 return [None]*2+[[None]*len(seuils_T)]+[None]*5 | |
| 170 | 176 |
| 171 class RTSS: | 177 class RTSS: |
| 172 'class for data related to a RTSS, including agregating pavement marking measurements' | 178 'class for data related to a RTSS, including agregating pavement marking measurements' |
| 173 | 179 |
| 174 def __init__(self, id): | 180 def __init__(self, id): |
| 189 | 195 |
| 190 def getSite(self): | 196 def getSite(self): |
| 191 return int(self.id[:2]) | 197 return int(self.id[:2]) |
| 192 | 198 |
| 193 def getTestAttributes(self): | 199 def getTestAttributes(self): |
| 194 return [self.paintingType, self.color] | 200 return [self.paintingType, self.color, self.paintingDate.year] |
| 195 | 201 |
| 196 def plot(self, measure, options = 'o', dayRatio = 1., **kwargs): | 202 def plot(self, measure, options = 'o', dayRatio = 1., **kwargs): |
| 197 from matplotlib.pyplot import plot | 203 from matplotlib.pyplot import plot |
| 198 plot(self.data['jours']/float(dayRatio), | 204 plot(self.data['jours']/float(dayRatio), |
| 199 self.data[measure], options, **kwargs) | 205 self.data[measure], options, **kwargs) |
| 200 | 206 |
| 201 def getMarkingMeasures(self, dataLabel): | 207 def getMarkingMeasures(self, dataLabel): |
| 202 from numpy import isnan | 208 nonZeroIndices = ~np.isnan(self.data[dataLabel]) |
| 203 nonZeroIndices = ~isnan(self.data[dataLabel]) | |
| 204 return self.data[nonZeroIndices]['jours'], self.data[nonZeroIndices][dataLabel] | 209 return self.data[nonZeroIndices]['jours'], self.data[nonZeroIndices][dataLabel] |
| 205 | 210 |
| 206 def plotMarkingMeasures(self, measure, options = 'o', dayRatio = 1., **kwargs): | 211 def plotMarkingMeasures(self, measure, options = 'o', dayRatio = 1., **kwargs): |
| 207 for i in range(1,7): | 212 for i in range(1,7): |
| 208 self.plot('{}_{}'.format(measure, i), options, dayRatio, **kwargs) | 213 self.plot('{}_{}'.format(measure, i), options, dayRatio, **kwargs) |
| 209 | 214 |
| 210 def computeMarkingMeasureVariations(self, dataLabel, lanePositions, weatherData, snowThreshold): | 215 def computeMarkingMeasureVariations(self, dataLabel, lanePositions, weatherData, snowThreshold, minProportionMeasures = 0.): |
| 211 '''Computes for each successive measurement | 216 '''Computes for each successive measurement |
| 212 lanePositions = None | 217 lanePositions = None |
| 213 measure variation, initial measure, time duration, weather indicators | 218 measure variation, initial measure, time duration, weather indicators |
| 214 | 219 |
| 215 TODO if measurements per lane, add a variable for lane position (position1 to 6) | 220 TODO if measurements per lane, add a variable for lane position (position1 to 6) |
| 216 lanePositions = list of integers (range(1,7)) | 221 lanePositions = list of integers (range(1,7)) |
| 217 measure variation, initial measure, time duration, lane position1, weather indicators | 222 measure variation, initial measure, time duration, lane position1, weather indicators |
| 218 measure variation, initial measure, time duration, lane position2, weather indicators | 223 measure variation, initial measure, time duration, lane position2, weather indicators |
| 219 ...''' | 224 ...''' |
| 220 from numpy import isnan | |
| 221 variationData = [] | 225 variationData = [] |
| 222 if lanePositions == None: | 226 if lanePositions == None: |
| 223 nonZeroIndices = ~isnan(self.data[dataLabel]) | 227 nonZeroIndices = ~np.isnan(self.data[dataLabel]) |
| 224 days = self.data[nonZeroIndices]['jours'] | 228 days = self.data[nonZeroIndices]['jours'] |
| 225 dates = self.data[nonZeroIndices]['date_mesure'] | 229 dates = self.data[nonZeroIndices]['date_mesure'] |
| 226 measures = self.data[nonZeroIndices][dataLabel] | 230 measures = self.data[nonZeroIndices][dataLabel] |
| 227 for i in range(1, len(dates)): | 231 for i in range(1, len(dates)): |
| 228 nDaysTNegative, nDaysThawFreeze, deltaTemp, nConsecutiveFrozenDays, totalRain, totalSnow, snowAboveThreshold, stdevTemp = ecWeatherIndicators(weatherData, dates[i-1], dates[i], snowThreshold) | 232 nDaysTNegative, nDaysThawFreeze, deltaTemp, nConsecutiveFrozenDays, totalRain, totalSnow, snowAboveThreshold, stdevTemp = ecWeatherIndicators(weatherData, dates[i-1], dates[i], snowThreshold, minProportionMeasures) |
| 229 variationData.append([measures[i-1]-measures[i], measures[i-1], days[i]-days[i-1], nDaysTNegative, nDaysThawFreeze] + deltaTemp + [nConsecutiveFrozenDays, totalRain, totalSnow, snowAboveThreshold, stdevTemp]) | 233 variationData.append([measures[i-1]-measures[i], measures[i-1], days[i]-days[i-1], days[i-1], nDaysTNegative, nDaysThawFreeze] + deltaTemp + [nConsecutiveFrozenDays, totalRain, totalSnow, snowAboveThreshold, stdevTemp]) |
| 230 return variationData | 234 return variationData |
