mean_daily_variance = NO2.groupby(lambda x: (x.date)).std() for row in mean_daily_variance.index: if mean_daily_variance.mean(1)[row] < 1: valid_dates = NO2.index[NO2.index.date != row] NO2 = NO2.transpose()[valid_dates].transpose() #ffill and bfill NA's NO2 = NO2.ffill().bfill() distance = pandas.DataFrame() for rack in bikes_metadata: distance[rack] = pandas.Series(dict((k, opensensor.getDistance(v,bikes_metadata[rack])) for k, v in LAQN_locations.items())) import scipy.stats as stats distance = distance.transpose()[list(NO2.columns)].transpose() #work out the weight for each LAQN sensor #2 is arbitrary decay rate weight = np.exp(-2*distance)/(np.exp(-2*distance)).sum() rack_AQ = pandas.DataFrame() for rack in weight.columns: rack_AQ[rack] = (NO2 * weight[rack]).sum(axis = 1) #set the decay parameters for the exp moving avgs nslow = 6
#the last date defaults to today end_date =datetime.datetime.now().strftime("%Y%m%d") #Get the meta data not on opensensors at the moment with open(LAQN_file) as f: reader = csv.reader(f) LAQN_locations = dict((rows[0],[float(rows[1]),float(rows[2])]) for rows in reader) #create instance of Air Quality Object opensensor = London(API_KEY) #set my postcode myPostCode = 'E1W 2PA' #get the geo data for my postcode geo_meta_data=opensensor.getPostcode(myPostCode) #get the distance from all the LAQN sensors from my postcode distance = pandas.Series(dict((k, opensensor.getDistance(v,[geo_meta_data['geo']['lng'], geo_meta_data['geo']['lat']])) for k, v in LAQN_locations.items())) #work out the weight for each LAQN sensor weight = np.exp(-2*distance)/sum(np.exp(-2*distance)) distance = pandas.concat([distance, weight], axis=1) distance.columns = ['distance','weight'] #get the list of LAQN measures LAQN_mtx = opensensor.getAirQuality(start=start_date,end=end_date) #grab the NO2 data where we bhave the intersect of weights and NO2 NO2 = LAQN_mtx['NO2'][list(weight[list(LAQN_mtx['NO2'].columns)].index)] NO2_weight = weight[list(NO2.columns)]/weight[list(NO2.columns)].sum() #sort the matrices NO2 = NO2.reindex_axis(sorted(NO2.columns), axis=1) NO2_weight = NO2_weight.reindex(sorted(NO2_weight.index))