def convert_pm(pm, us_pm, r): crawling = Crawling() pm_ = [] # extract stations class for d in pm: pr = [] for s in us_pm: v = d[s] * r #pr.append(get_aqi_class(crawling.AQIPM25(v))) pr.append(crawling.aqi_pm25_china_class(v)) pm_.append(pr) # extract class for all pixels # for d in pm: # pr = [] # for idx in d: # pr.append(get_aqi_class(crawling.AQIPM25(idx))) # pm_.append(pr) return pm_
def evaluate_china(url, url2, stations, encoder_length=12, decoder_length=24, eval_stat=False): cr = Crawling() # missing = [136,156,824,1028,1053,1084,1085,1457,1460,1461,1462,1464,1465,1476,1477, \ # 1478,1479,1481,1482,1484,1485,1486,1487,1510,1841,3814,4246,4268,4301,4311, \ # 4313,4317,4329,4331,4333,4349,4360,4504,4524,4529,4532,4535,4580,4860,5270, \ # 5457,5489,5509,5962,6007,6011,6039,6054,6125,6172,6189,6192,6201,6230,6234, \ # 6246,6254,6255,6256,6257,6258,6295,6300,6319,6336,6356,6362,6371,6372,6394, \ # 6491,6492,6493,6494,6497,6517,6519,6523,6539,6559,6564,6568,6569,6570,6612, \ # 6613,6614,6615,6616,6617,6618,6619,6620,6621,6622,6623,6624,6625,6626,6627, \ # 6628,6629,6630,6631,6632,6633,6634,6635,6636,6637,6638,6687,6704,6754,6832, \ # 6849,6858,6873,6979,7255,8099,8100,8101,8225,8226,8227,8228,8229,8230,8231, \ # 8232,8233,8234,8235,8236,8237,8238,8239,8240,8241,8273,8274,8275,8276,8277, \ # 8278,8279,8280,8281,8282,8283,8284,8285,8286,8287,8288,8289,8290,8291,8344, \ # 8421,8422,8423,8424,8425,8426,8427,8428,8429,8430,8431,8432,8495,8496,8497, \ # 8498,8499,8500,8501,8502,8503,8504,8631,8759] data = utils.load_file(url) labels = utils.load_file(url2) if type(data) is list: data = np.asarray(data) if len(data.shape) == 4: lt = data.shape[0] * data.shape[1] data = np.reshape(data, (lt, data.shape[-2], data.shape[-1])) labels = np.reshape(labels, (lt, data.shape[-2], data.shape[-1])) else: lt = len(data) data = np.array(data) labels = np.array(labels) if not eval_stat: loss_mae = [0.0] * decoder_length else: loss_mae = np.array([[0.0] * decoder_length] * len(stations)) classes = [0.0] * decoder_length # loss_lt = 0 loss_total = [0.0] * decoder_length # ckt = 0 valid = [] for i, (d_, l_) in enumerate(zip(data, labels)): # st = 2 * i # ed = st + decoder_length # flag = False # for ck in range(st, ed): # if ck in missing: # flag = True # break # if flag: # continue d = d_[:decoder_length, :] lbt = l_[:decoder_length, :] for t_i, (t, l_t) in enumerate(zip(d, lbt)): pred_t = [] label_t = [] for r in stations: #pred_t.append(t[r]) #label_t.append(l_t[r]) # if not math.isnan(t[r]): pr = t[r] if pr < 0: pr = 0 print(pr) pred_t.append(cr.aqi_pm25_china(pr)) label_t.append(cr.aqi_pm25_china(l_t[r] * 300)) # label_t.append(l_t[r]) # print(t[r], l_t[r]) # if pred_t: if not eval_stat: mae = mean_absolute_error(pred_t, label_t) classes[t_i] += accuracy_score( [cr.aqi_pm25_china_class(x) for x in pred_t], [cr.aqi_pm25_china_class(x) for x in label_t]) if mae > 80: valid.append("%i,%i" % (i, t_i)) if mae < 80: loss_total[t_i] += 1 loss_mae[t_i] += mae else: mae = [abs(p_s - l_s) for p_s, l_s in zip(pred_t, label_t)] for m_i, m in enumerate(mae): loss_mae[m_i, t_i] += m # loss_lt += 1 # utils.update_progress((i + 1.0) / lt) va = "\n".join(valid) save_file("outline_china", va, False) loss_mae = [x / y if y > 0 else 0 for x, y in zip(loss_mae, loss_total)] for i, x in enumerate(loss_mae): # if not eval_stat: print(x, loss_total[i]) # else: # print([y/loss_lt for y in x]) # print("accumulated:", np.mean(loss_mae[1:6])) print(np.array(classes) / lt)