def main(load_inter_flag=False): if not load_inter_flag: real_data = list(pd.read_csv(get_data_path(True))['adcode'].unique()) start_date = date(2020, 1, 24) real_data1 = pd.read_csv(get_data_path(True)) history_real = prepareData(real_data1) rerun_cities = None for i in range(get_seed_num()): total_params = pd.read_csv( os.path.join(os.path.dirname(__file__), './params_foreign{}.csv'.format(int(i)))) for ind, city in enumerate(real_data): print(i, ind, city, str(get_important_date(city))) x = get_params(total_params, city) if rerun_cities is None or city in rerun_cities: run_simulation2(x, city, 60, 60, start_date, get_important_date(city), history_real, unob_flow_num=None, json_name='data_run_foreign{}.json'.format( int(i))) # choose the final parameters max_data, min_data, mean_data, best_data, \ std_data, middle_data, xmin_data, xmax_data, \ xmean_data, xstd_data = load_and_save('./data_run_foreign{}.json', './data_run_foreign_{}.json') """
def disp(): flow_out_data = flowOutData() for i in range(4): real_data = pd.read_csv(get_data_path()) data = json.load(open('data_run_no_healed{}.json'.format(int(i)))) infectratio = InfectRatio(1, [[0., 1.]], [True]) touchratio = TouchRatio(1, [[0.0, 0.6]], [True]) touchratiointra = TouchRatio(1, [[0, 1]], [True]) obs = ObservationRatio(1, [[0.0, 0.3]], [True]) dead = DeadRatio(1, [[0., 0.01]], [True]) isoratio = IsolationRatio(1, [[0.0, 0.5]], [True]) dummy = DummyModel(1, [[0, 400]], [True, True]) cure_ratio = InfectRatio(1, [[0., 0.1]], [True]) city = 420000 start_date = date(2020, 1, 11) # set the time of applying touchratio simulator = Simulator(city, infectratio, touchratio, obs, dead, dummy, isoratio, touchratiointra, cure_ratio, [get_important_date(city)], unob_flow_num=None, flow_out_data=flow_out_data, training_date_end=None) simulator.set_param(data['x'][str(city)]) duration = len(real_data["date"].unique()) - 1 sim_res, _ = simulator.simulate(str(start_date), duration) plot1(data['real_confirmed'][str(city)], sim_res['observed'], '', 'no_heal{}.pdf'.format(int(i)), 7)
def prepare_data_china(load_inter_flag=False): real_data = pd.read_csv(get_data_path())['adcode'].unique() rerun_cities = None if rerun_cities is not None: real_data = rerun_cities.copy() start_date = date(2020, 1, 11) real_data1 = pd.read_csv(get_data_path()) history_real = prepareData(real_data1) if not load_inter_flag: print('re-simulate the progress') province_travel_dict = flowHubei() for i in range(get_seed_num()): total_params = pd.read_csv( os.path.join(os.path.dirname(__file__), './params{}.csv'.format(int(i)))) unob_flow_num = initHubei( get_params(total_params, 420000), start_date, important_date=[get_important_date(420000)], travel_from_hubei=province_travel_dict) for ind, city in enumerate(real_data): print(i, ind, city) x = get_params(total_params, city) run_simulation2(x, city, 90, 90, start_date, get_important_date(city), history_real, unob_flow_num=unob_flow_num, json_name='data_run{}.json'.format(int(i))) else: print('directly load the pre-simulated data') for i in range(get_seed_num()): get_total_province('data_run{}.json'.format(int(i))) # choose the final parameters max_data, min_data, mean_data, best_data, std_data, middle_data, \ xmin_data, xmax_data, xmean_data, xstd_data = load_and_save( './data_run{}.json', './data_run_{}.json') """
def run_simulation(x, city, simulate_day1, simulate_day2, start_date, simulate_date, incubation=3, unob_flow_num=None, json_name='data_run.json'): real_data = pd.read_csv(get_data_path(True)) history_real = prepareData(real_data) run_simulation2(x, city, simulate_day1, simulate_day2, start_date, simulate_date, history_real, incubation, unob_flow_num, json_name)
def train_china(): province_travel_dict = flowHubei() real_data = pd.read_csv(get_data_path())['adcode'].unique() province_code_dict = codeDict() for i in range(1): all_param = {} x = run_opt_china(420000, 200000, start_date=date(2020, 1, 11), important_dates=[get_important_date(420000)], repeat_time=3, training_date_end=training_end_date, seed=i, json_name='data_run_lstm_china{}.json'.format( int(i)), loss_ord=4., touch_range=[0, 0.33]) unob_flow_num = initHubei(x, start_date=date(2020, 1, 11), important_date=[get_important_date(420000)], travel_from_hubei=province_travel_dict) all_param[420000] = x real_data = [110000, 440000, 330000, 310000, 320000, 120000] for ind, item in enumerate(real_data): print(i, ind, item, province_code_dict[item]) if item == 420000: continue x = run_opt_china(item, 40000, start_date=date(2020, 1, 11), important_dates=[get_important_date(420000)], infectratio_range=[0.0, 0.05], dummy_range=[0, 0.000001], unob_flow_num=unob_flow_num, repeat_time=2, training_date_end=training_end_date, json_name='data_run_lstm_china{}.json'.format( int(i)), loss_ord=4., touch_range=[0.0, 0.33], iso_range=[0.03, 0.12]) all_param[item] = x all_param_df = pd.DataFrame(all_param) all_param_df.to_csv('params_lstm_china{}.csv'.format(int(i)), index=False) load_and_save('data_run_lstm_china{}.json', 'data_run_lstm_china_{}.json', 1)
def run_opt(city, budget, start_date, important_dates, infectratio_range=None, dummy_range=None, unob_flow_num=None, repeat_time=1, init_samples=None, training_end_date=None, json_name='data_run.json', seed=3, loss_ord=0.0, touch_range=None,iso_range=None): assert infectratio_range is not None and dummy_range is not None days_predict = 0 # load data real_data = pd.read_csv(get_data_path(True)) history_real = prepareData(real_data) flow_out_data = None # initialize models infectratio = InfectRatio(1, [infectratio_range], [True]) if touch_range is None: touchratio = TouchRatio(1, [[0.999, 1.0000]], [True]) else: touchratio = TouchRatio(1, [touch_range], [True]) touchratiointra = TouchRatio(1, [[0, 1]], [True]) obs = ObservationRatio(1, [[0.0, 0.3]], [True]) dead = DeadRatio(1, [[0., 0.01]], [True]) if iso_range is None: isoratio = IsolationRatio(1, [[0.03, 0.12]], [True]) else: isoratio = IsolationRatio(1, [iso_range], [True]) dummy = DummyModel(1, [dummy_range], [True, True]) cure_ratio = InfectRatio(1, [[0., 0.15]], [True]) simulator = Simulator(city, infectratio, touchratio, obs, dead, dummy, isoratio, touchratiointra, cure_ratio, important_dates, unob_flow_num=unob_flow_num, flow_out_data=flow_out_data, training_date_end=training_end_date) test_date = datetime.strptime(history_real['date'].max(), '%Y-%m-%d').date() - timedelta(days_predict) history_real = history_real[history_real['adcode'] == city] history_real = history_real[history_real['date'] >= str(start_date)] history_train = history_real[history_real['date'] <= str(test_date)] x, y = simulator.fit(history_train, budget=budget, server_num=get_core_num(), repeat=repeat_time, seed=seed, intermediate_freq=10000, init_samples=init_samples, loss_ord=loss_ord) print('best_solution: x = ', x, 'y = ', y) simulator.set_param(x) run_simulation(x, city, 60, 60, start_date, get_important_date(city), unob_flow_num=unob_flow_num, json_name=json_name) return x
def main(load_inter_flag=False): if not load_inter_flag: if korea_flag: train_korea() else: train_china() fmt = 'pdf' #all_city = pd.read_csv(get_data_path())['adcode'].unique() all_city = [420000, 110000, 440000, 330000, 310000, 320000, 120000] if korea_flag: all_city = [900003] mlsim_loss_array = [] seir_loss_array = [] lstm_loss_array = [] for city in all_city: print(city) torch.manual_seed(6) if not os.path.exists('./img/{}{}/'.format('./lstm', str(city))): os.mkdir('./img/{}{}/'.format('./lstm', str(city))) if not korea_flag: div_idx = period_it dataX_list, dataY_list = [], [] mask_list = [] _DataX, _DataY = construct_data_set(prepare_data(), foresee_size) _DataX, _DataY = np.expand_dims(_DataX, 0), np.expand_dims(_DataY, 0) dataX_list.append(_DataX) dataY_list.append(_DataY) mask_list.append(np.zeros((1, np.shape(_DataY)[1], 1))) TestDataX, TestDataY = construct_data_set( get_COVID_new_confirm(cities=city), foresee_size) mask_list.append(np.zeros((1, np.shape(_DataY)[1], 1))) dataX_list.append(np.zeros((1, np.shape(_DataY)[1], foresee_size))) dataY_list.append(np.zeros((1, np.shape(_DataY)[1], 1))) for i in range(div_idx): mask_list[-1][0][i][0] = 1 dataY_list[-1][0][i][0] = TestDataY[i][0] for ii in range(foresee_size): dataX_list[-1][0][i][ii] = TestDataX[i][ii] DataX = np.concatenate(dataX_list, 0) DataY = np.concatenate(dataY_list, 0) mask = np.concatenate(mask_list, 0) TestDataX, TestDataY = construct_data_set( get_COVID_new_confirm(cities=city), foresee_size) _, real_cum = construct_data_set( get_COVID_new_confirm('real_cum_confirmed', cities=city), foresee_size) _, sim_cum = construct_data_set( get_COVID_new_confirm('sim_cum_confirmed_deduction_s1', cities=city), foresee_size) _, sim_inc = construct_data_set( get_COVID_new_confirm('sim_confirmed_deduction_s1', cities=city), foresee_size) load_model = False _, sim_inc_seir = construct_data_set( ChinaSEIRData(city=city)[:-3], foresee_size) else: div_idx = period_it dataX_list, dataY_list = [], [] mask_list = [] for item in pd.read_csv(get_data_path())['adcode'].unique(): _DataX, _DataY = construct_data_set( get_COVID_new_confirm(cities=int(item)), foresee_size) _DataX, _DataY = np.expand_dims(_DataX, 0), np.expand_dims(_DataY, 0) dataX_list.append(_DataX) dataY_list.append(_DataY) mask_list.append(np.ones((1, np.shape(_DataY)[1], 1))) TestDataX, TestDataY = construct_data_set( get_COVID_new_confirm(file='data_run_lstm_korea0.json', cities=900003), foresee_size) mask_list.append(np.zeros((1, np.shape(_DataY)[1], 1))) dataX_list.append(np.zeros((1, np.shape(_DataY)[1], foresee_size))) dataY_list.append(np.zeros((1, np.shape(_DataY)[1], 1))) for i in range(div_idx): mask_list[-1][0][i][0] = 1 dataY_list[-1][0][i][0] = TestDataY[i][0] for ii in range(foresee_size): dataX_list[-1][0][i][ii] = TestDataX[i][ii] DataX = np.concatenate(dataX_list, 0) DataY = np.concatenate(dataY_list, 0) mask = np.concatenate(mask_list, 0) TestDataX, TestDataY = construct_data_set( get_COVID_new_confirm(file='data_run_lstm_korea0.json', cities=900003), foresee_size) _, real_cum = construct_data_set( get_COVID_new_confirm(type='real_cum_confirmed', file='data_run_lstm_korea0.json', cities=900003), foresee_size) _, sim_cum = construct_data_set( get_COVID_new_confirm('sim_cum_confirmed_deduction_s1', file='data_run_lstm_korea0.json', cities=900003), foresee_size) _, sim_inc = construct_data_set( get_COVID_new_confirm('sim_confirmed_deduction_s1', file='data_run_lstm_korea0.json', cities=900003), foresee_size) _, sim_inc_seir = construct_data_set(HanSEIRData()[:], foresee_size) load_model = False den = 4500 if city < 900000 and not city == 420000: den = 200 # print(TestDataX) if korea_flag: pred_train, pred_test = train_and_pred(DataX, DataY, TestDataX[:div_idx][:], len(sim_cum) - div_idx, den, load_model=load_model, mask=mask) else: pred_train, pred_test = train_and_pred(DataX, DataY, TestDataX[:div_idx][:], len(sim_cum) - div_idx, den, load_model=load_model, mask=mask) pred_test = pred_test * den pred_test[pred_test < 0] = 0 if not os.path.exists('./img/lstm'): os.mkdir('./img/lstm') max_len = len(TestDataY) plot1( sim_inc[:max_len], TestDataY, pred_test[:max_len], sim_inc_seir[:max_len], 'The number of newly confirmed cases ', './img/{}{}/newly_real_sim.{}'.format('./lstm', str(city), fmt), date_it=date_it, loc='upper right', ) plot1(sim_cum, real_cum, np.cumsum(pred_test), np.cumsum(sim_inc_seir), 'The cumulative number of newly confirmed cases ', 'img/{}{}/cum_real_sim.{}'.format('./lstm', str(city), fmt), date_it=date_it, loc='lower right') if city == 420000: plot1( sim_inc, TestDataY, pred_test, sim_inc_seir, 'The number of newly confirmed cases ', './img/{}{}/newly_real_sim.{}'.format('./lstm', str(city), fmt), date_it=date_it, loc='upper right', #y_max2=7000 ) if city == 900003: plot1(sim_inc[:max_len], TestDataY, pred_test[:max_len], sim_inc_seir[:max_len], 'The number of newly confirmed cases ', './img/{}{}/newly_real_sim.{}'.format( './lstm', str(city), fmt), date_it=date_it, loc='upper left', interval=12) train_size = (date_it - start_date).days print(train_size, len(TestDataY)) test_label = np.array([TestDataY[item] for item in range(train_size)]) loss_ours = np.sqrt( np.mean( np.square( test_label - np.array([sim_inc[item] for item in range(train_size)])))) loss_seir = np.sqrt( np.mean( np.square(test_label - np.array( [sim_inc_seir[item] for item in range(train_size)])))) loss_lstm = np.sqrt( np.mean( np.square( test_label - np.array([pred_test[item] for item in range(train_size)])))) print('training, ', city, ' ours: ', loss_ours, 'seir: ', loss_seir, 'lstm: ', loss_lstm) mlsim_loss_array.append(loss_ours) seir_loss_array.append(loss_seir) lstm_loss_array.append(loss_lstm) test_label = np.array( [TestDataY[item] for item in range(train_size, len(TestDataY))]) loss_ours = np.sqrt( np.mean( np.square(test_label - np.array([ sim_inc[item] for item in range(train_size, len(TestDataY)) ])))) loss_seir = np.sqrt( np.mean( np.square(test_label - np.array([ sim_inc_seir[item] for item in range(train_size, len(TestDataY)) ])))) loss_lstm = np.sqrt( np.mean( np.square(test_label - np.array([ pred_test[item] for item in range(train_size, len(TestDataY)) ])))) print('testing, ours: ', loss_ours, 'seir: ', loss_seir, 'lstm: ', loss_lstm) mlsim_loss_array.append(loss_ours) seir_loss_array.append(loss_seir) lstm_loss_array.append(loss_lstm) print('MLSim ', sep='', end='') for lsim in mlsim_loss_array: print('& ${:.2f}$ '.format(lsim), sep='', end='') print('\\\\') print('SEIR ', sep='', end='') for lsim in seir_loss_array: print('& ${:.2f}$ '.format(lsim), sep='', end='') print('\\\\') print('LSTM ', sep='', end='') for lsim in lstm_loss_array: print('& ${:.2f}$ '.format(lsim), sep='', end='') print('\\\\') pass
def main(): data = {} code_dict = it_code_dict() fmt = 'pdf' #total_cities = pd.read_csv("./data/province_data.csv")['adcode'].unique() total_cities = list(pd.read_csv(get_data_path(True))['adcode'].unique()) important_cities = total_cities important_cities = [900003, 900004, 900005, 900006, 900007, 900008] with open('./data_run_foreign_middle.json', 'r') as f: data = json.load(f) with open('./data_run_foreign_xmin.json', 'r') as f: min_data = json.load(f) with open('./data_run_foreign_xmax.json', 'r') as f: max_data = json.load(f) if not os.path.exists('./img'): os.mkdir('./img') if not os.path.exists('./img/0000'): os.makedirs('./img/0000') for item in important_cities: it_path = './img/{}'.format(str(item)) if not os.path.exists(it_path): os.mkdir(it_path) print( "final day: ", date(2020, 1, 24) + timedelta(days=len( get_total(data, 'sim_cum_confirmed_deduction_s1', total_cities)))) final_date = date(2020, 1, 24) + timedelta(days=len( get_total(data, 'sim_cum_confirmed_deduction_s1', total_cities))) cur_date = date(2020, 1, 24) + timedelta( days=len(get_total(data, 'real_confirmed', total_cities))) print('early') for item in important_cities: print(code_dict[int(item)], ": ", data['sim_cum_confirmed_deduction_s3'][str(item)][-1]) print(len(data['real_confirmed'][str(item)])) print('late') for item in important_cities: print(code_dict[int(item)], ": ", data['sim_cum_confirmed_deduction_s4'][str(item)][-1]) print('3-20') for item in important_cities: print(code_dict[int(item)], ": ", data['sim_cum_confirmed_deduction_s6'][str(item)][-1]) city_d = [900004, 900005, 900006, 900007, 900008, 900003] print('\n\n===========') print('country ', sep='', end='') for item in city_d: print('& ', code_dict[item], sep='', end='') print('\\\\') print('total confirmed cases', sep='', end='') for item in city_d: print(' & ', int(data['sim_cum_confirmed_deduction_s1'][str(item)][-1]), sep='', end='') print('\\\\') print('total infections', sep='', end='') for item in city_d: print(' & ', int(data['sim_cum_infection_deduction_s1'][str(item)][-1]), sep='', end='') print('\\\\') print('===========\n\n') print('Date when $k$ was changed & ', sep='', end='') for item in city_d: print(code_dict[item], ' & ', sep='', end='') print('\\\\') print('middle &', sep='', end='') for item in city_d: print(int(data['sim_cum_confirmed_deduction_s1'][str(item)][-1]), ' & ', sep='', end='') print(int(data['sim_cum_infection_deduction_s1'][str(item)][-1]), ' & ', sep='', end='') print('\\\\') print('min&', sep='', end='') for item in city_d: print(int(min_data['sim_cum_confirmed_deduction_s1'][str(item)][-1]), ' & ', sep='', end='') print(int(min_data['sim_cum_infection_deduction_s1'][str(item)][-1]), ' & ', sep='', end='') print('\\\\') print('max&', sep='', end='') for item in city_d: print(int(max_data['sim_cum_confirmed_deduction_s1'][str(item)][-1]), ' & ', sep='', end='') print(int(max_data['sim_cum_infection_deduction_s1'][str(item)][-1]), ' & ', sep='', end='') print('\\\\') print( 'country&real confirmed&MLSim confirmed&MLSim infected& proportion of asymptomatic\\\\\\hline' ) lreal = len(get_total(data, 'real_cum_confirmed', total_cities)) for item in city_d: real_cum = data['real_cum_confirmed'][str(item)][-1] sim_cum_confirm = data['sim_cum_confirmed'][str(item)][-1] sim_cum_infect = data['sim_cum_infection_deduction_s1'][str(item)][ lreal - 1] print(code_dict[int(item)], ' & ', real_cum, ' & ', int(sim_cum_confirm), ' & ', int(sim_cum_infect), ' & ', np.round((1 - sim_cum_confirm / sim_cum_infect) * 100, 3), '$\\%$\\\\ ', sep='') #exit(0) lreal = len(get_total(data, 'real_cum_confirmed', total_cities)) # draw compare to real data for every city and full print('drawing real/sim compare') for item in important_cities: print(item) plot1_shade(data['real_cum_confirmed'][str(item)], data['sim_cum_confirmed'][str(item)], min_data['sim_cum_confirmed'][str(item)], max_data['sim_cum_confirmed'][str(item)], 'The cumulative number of confirmed cases ', 'img/{}/cum_real_sim.{}'.format(str(item), fmt)) plot1_shade(data['real_confirmed'][str(item)], data['sim_confirmed'][str(item)], min_data['sim_confirmed'][str(item)], max_data['sim_confirmed'][str(item)], 'The newly number of confirmed cases ', 'img/{}/increase_real_sim.{}'.format(str(item), fmt)) print('drawing real/sim compare forecast') for item in important_cities: print(item) plot1_shade(data['real_cum_confirmed'][str(item)], data['sim_cum_confirmed_deduction_s1'][str(item)], min_data['sim_cum_confirmed_deduction_s1'][str(item)], max_data['sim_cum_confirmed_deduction_s1'][str(item)], 'The cumulative number of confirmed cases ', 'img/{}/only_cum_real_sim.{}'.format(str(item), fmt), 18, date_it=get_important_date(item)) plot1_shade(data['real_confirmed'][str(item)], data['sim_confirmed_deduction_s1'][str(item)], min_data['sim_confirmed_deduction_s1'][str(item)], max_data['sim_confirmed_deduction_s1'][str(item)], 'The newly number of confirmed cases ', 'img/{}/only_increase_real_sim.{}'.format(str(item), fmt), 18, date_it=get_important_date(item)) plot12_shade( data['real_confirmed'][str(item)], data['sim_confirmed'][str(item)], min_data['sim_confirmed'][str(item)], max_data['sim_confirmed'][str(item)], data['sim_new_infection'][str(item)], min_data['sim_new_infection'][str(item)], max_data['sim_new_infection'][str(item)], 'The newly number of confirmed cases ', 'img/{}/increase_real_sim_infect.{}'.format(str(item), fmt)) plot12_shade( data['real_cum_confirmed'][str(item)], data['sim_cum_confirmed'][str(item)], min_data['sim_confirmed'][str(item)], max_data['sim_confirmed'][str(item)], data['sim_cum_infection_deduction_s1'][str(item)][:lreal], min_data['sim_cum_infection_deduction_s1'][str(item)][:lreal], max_data['sim_cum_infection_deduction_s1'][str(item)][:lreal], 'The newly number of confirmed cases ', 'img/{}/cum_real_sim_infect.{}'.format(str(item), fmt)) print('==================================', code_dict[item], str(cur_date)) print('current total infections and confirmed cases') print('confirmed') print('middle: ') print( '100\%', format(int(data['sim_cum_confirmed'][str(item)][lreal - 1]), ','), '({}-{})'.format( format( int(min_data['sim_cum_confirmed'][str(item)][lreal - 1]), ','), format( int(max_data['sim_cum_confirmed'][str(item)][lreal - 1]), ','))) print('min: ') print( '100\%', format(int(min_data['sim_cum_confirmed'][str(item)][lreal - 1]), ',')) print('max: ') print( '100\%', format(int(max_data['sim_cum_confirmed'][str(item)][lreal - 1]), ',')) print('real: ', format(int(data['real_cum_confirmed'][str(item)][-1]), ',')) print('infected') print('middle: ') print( '100\%', format( int(data['sim_cum_infection_deduction_s1'][str(item)][lreal - 1]), ','), '({}-{})'.format( format( int(min_data['sim_cum_infection_deduction_s1'][str( item)][lreal - 1]), ','), format( int(max_data['sim_cum_infection_deduction_s1'][str( item)][lreal - 1]), ','))) print('min: ') print( '100\%', format( int(min_data['sim_cum_infection_deduction_s1'][str(item)][lreal - 1]), ',')) print('max: ') print( '100\%', format( int(max_data['sim_cum_infection_deduction_s1'][str(item)][lreal - 1]), ',')) print('asym') print('middle: ') print('100\%', format(int(data['current_asym'][str(item)]), ',')) print('min: ') print('100\%', format(int(min_data['current_asym'][str(item)]), ',')) print('max: ') print('100\%', format(int(max_data['current_asym'][str(item)]), ',')) print('final asym') print('middle: ') print('100\%', format(int(data['final_asym'][str(item)]), ',')) print('min: ') print('100\%', format(int(min_data['final_asym'][str(item)]), ',')) print('max: ') print('100\%', format(int(max_data['final_asym'][str(item)]), ',')) print('cum self_cure') print( '100\%', format( int(data['sim_cum_self_cured_deduction_s1'][str(item)][lreal - 1]), ','), '({}-{})'.format( format( int(min_data['sim_cum_self_cured_deduction_s1'][str( item)][lreal - 1]), ','), format( int(max_data['sim_cum_self_cured_deduction_s1'][str( item)][lreal - 1]), ','))) print('total infection') print( '100\%', format( int(data['sim_total_infection_deduction_s1'][str(item)][lreal - 1]), ','), '({}-{})'.format( format( int(min_data['sim_total_infection_deduction_s1'][str( item)][lreal - 1]), ','), format( int(max_data['sim_total_infection_deduction_s1'][str( item)][lreal - 1]), ','))) print('nosymbol') print( '100\%', format( int(data['sim_cum_nosymbol_deduction_s1'][str(item)][lreal - 1]), ','), '({}-{})'.format( format( int(min_data['sim_cum_nosymbol_deduction_s1'][str(item)][ lreal - 1]), ','), format( int(max_data['sim_cum_nosymbol_deduction_s1'][str(item)][ lreal - 1]), ','))) # sim_total_isolation_deduction_s1 print('total iso') print( '100\%', format( int(data['sim_total_isolation_deduction_s1'][str(item)][lreal - 1]), ','), '({}-{})'.format( format( int(min_data['sim_total_isolation_deduction_s1'][str( item)][lreal - 1]), ','), format( int(max_data['sim_total_isolation_deduction_s1'][str( item)][lreal - 1]), ','))) print('ratio') print('{:.3f} ({:.3f}-{:.3f})'.format( 1 - data['current_asym'][str(item)], 1 - min_data['current_asym'][str(item)], 1 - max_data['current_asym'][str(item)])) print('==================================', code_dict[item], str(cur_date)) """ plot1(get_total(data, 'real_cum_confirmed',total_cities), get_total(data,'sim_cum_confirmed', total_cities), 'The cumulative number of confirmed cases ', 'img/{}/cum_real_sim.{}'.format('0000', fmt)) plot1(get_total(data, 'real_confirmed',total_cities), get_total(data,'sim_confirmed', total_cities), 'The newly number of confirmed cases ', 'img/{}/increase_real_sim.{}'.format('0000', fmt)) """ # draw different deduction in Feb 24th print('drawing different deduction') for item in important_cities: print(item) it_max = None if item == 900004: it_max = 85000 if item == 900005: it_max = 10000 if item == 900006: it_max = 15000 if item == 900007: it_max = 20000 if item == 900008: it_max = 25000 it_min = 0 plot3(data['sim_cum_confirmed_deduction_s1'][str(item)], data['sim_cum_confirmed_deduction_s2'][str(item)], data['sim_cum_confirmed_deduction_s3'][str(item)], data['real_cum_confirmed'][str(item)], 'Prediction of the cumulative number of confirmed cases', 'img/{}/cum_confirmed_prediction.{}'.format(str(item), fmt), touchratio=data['x'][str(item)][1], ratio_low=data['touch_ratio_low'][str(item)], ratio_high=data['touch_ratio_hight'][str(item)], date_it=get_important_date(item), start_date_it=date(2020, 2, 15)) plot3(data['sim_confirmed_deduction_s1'][str(item)], data['sim_confirmed_deduction_s2'][str(item)], data['sim_confirmed_deduction_s3'][str(item)], data['real_confirmed'][str(item)], 'Prediction of the cumulative number of confirmed cases', 'img/{}/confirmed_prediction.{}'.format(str(item), fmt), touchratio=data['x'][str(item)][1], ratio_low=data['touch_ratio_low'][str(item)], ratio_high=data['touch_ratio_hight'][str(item)], date_it=get_important_date(item), it_max=it_max, it_min=it_min, start_date_it=date(2020, 2, 15)) plot3_shade( data['sim_cum_confirmed_deduction_s1'][str(item)], min_data['sim_cum_confirmed_deduction_s1'][str(item)], max_data['sim_cum_confirmed_deduction_s1'][str(item)], data['sim_cum_confirmed_deduction_s2'][str(item)], min_data['sim_cum_confirmed_deduction_s2'][str(item)], max_data['sim_cum_confirmed_deduction_s2'][str(item)], data['sim_cum_confirmed_deduction_s3'][str(item)], min_data['sim_cum_confirmed_deduction_s3'][str(item)], max_data['sim_cum_confirmed_deduction_s3'][str(item)], data['sim_cum_confirmed_deduction_s8'][str(item)], min_data['sim_cum_confirmed_deduction_s8'][str(item)], max_data['sim_cum_confirmed_deduction_s8'][str(item)], data['real_cum_confirmed'][str(item)], 'Prediction of the cumulative number of confirmed cases', 'img/{}/cum_confirmed_prediction_shade.{}'.format(str(item), fmt), touchratio=data['x'][str(item)][1], ratio_low=data['touch_ratio_low'][str(item)], ratio_high=data['touch_ratio_hight'][str(item)], date_it=get_important_date(item), start_date_it=date(2020, 2, 15), ) plot3_shade(data['sim_confirmed_deduction_s1'][str(item)], min_data['sim_confirmed_deduction_s1'][str(item)], max_data['sim_confirmed_deduction_s1'][str(item)], data['sim_confirmed_deduction_s2'][str(item)], min_data['sim_confirmed_deduction_s2'][str(item)], max_data['sim_confirmed_deduction_s2'][str(item)], data['sim_confirmed_deduction_s3'][str(item)], min_data['sim_confirmed_deduction_s3'][str(item)], max_data['sim_confirmed_deduction_s3'][str(item)], data['sim_confirmed_deduction_s8'][str(item)], min_data['sim_confirmed_deduction_s8'][str(item)], max_data['sim_confirmed_deduction_s8'][str(item)], data['real_confirmed'][str(item)], 'Prediction of the cumulative number of confirmed cases', 'img/{}/confirmed_prediction_shade.{}'.format( str(item), fmt), touchratio=data['x'][str(item)][1], ratio_low=data['touch_ratio_low'][str(item)], ratio_high=data['touch_ratio_hight'][str(item)], date_it=get_important_date(item), it_max=it_max, it_min=it_min, start_date_it=date(2020, 2, 15), loc='upper right') if item == 900003: plot3_shade( data['sim_confirmed_deduction_s1'][str(item)], min_data['sim_confirmed_deduction_s1'][str(item)], max_data['sim_confirmed_deduction_s1'][str(item)], data['sim_confirmed_deduction_s2'][str(item)], min_data['sim_confirmed_deduction_s2'][str(item)], max_data['sim_confirmed_deduction_s2'][str(item)], data['sim_confirmed_deduction_s3'][str(item)], min_data['sim_confirmed_deduction_s3'][str(item)], max_data['sim_confirmed_deduction_s3'][str(item)], data['sim_confirmed_deduction_s8'][str(item)], min_data['sim_confirmed_deduction_s8'][str(item)], max_data['sim_confirmed_deduction_s8'][str(item)], data['real_confirmed'][str(item)], 'Prediction of the cumulative number of confirmed cases', 'img/{}/confirmed_prediction_shade.{}'.format(str(item), fmt), touchratio=data['x'][str(item)][1], ratio_low=data['touch_ratio_low'][str(item)], ratio_high=data['touch_ratio_hight'][str(item)], date_it=get_important_date(item), it_max=it_max, it_min=it_min, start_date_it=date(2020, 2, 1), loc='upper right') print('==================================', code_dict[item], str(final_date)) print('final total infections and confirmed cases') print('confirmed') print('middle: ') print( '100\%', format( int((data['sim_cum_confirmed_deduction_s1'][str(item)][-1])), ','), '({}-{})\n'.format( format( int((min_data['sim_cum_confirmed_deduction_s1'][str(item)] [-1])), ','), format( int((max_data['sim_cum_confirmed_deduction_s1'][str(item)] [-1])), ',')), '60\%', format( int((data['sim_cum_confirmed_deduction_s8'][str(item)])[-1]), ','), '({}-{})\n'.format( format( int((min_data['sim_cum_confirmed_deduction_s8'][str(item)] [-1])), ','), format( int((max_data['sim_cum_confirmed_deduction_s8'][str(item)] [-1])), ',')), '35\%', format( int((data['sim_cum_confirmed_deduction_s2'][str(item)])[-1]), ','), '({}-{})\n'.format( format( int((min_data['sim_cum_confirmed_deduction_s2'][str(item)] [-1])), ','), format( int((max_data['sim_cum_confirmed_deduction_s2'][str(item)] [-1])), ',')), '10\%', format( int((data['sim_cum_confirmed_deduction_s3'][str(item)])[-1]), ','), '({}-{})\n'.format( format( int((min_data['sim_cum_confirmed_deduction_s3'][str(item)] [-1])), ','), format( int((max_data['sim_cum_confirmed_deduction_s3'][str(item)] [-1])), ',')), ) print('min: ') print( '100\%', format( int((min_data['sim_cum_confirmed_deduction_s1'][str(item)][-1] )), ','), '60\%', format( int((min_data['sim_cum_confirmed_deduction_s8'][str(item)] )[-1]), ','), '35\%', format( int((min_data['sim_cum_confirmed_deduction_s2'][str(item)] )[-1]), ','), '10\%', format( int((min_data['sim_cum_confirmed_deduction_s3'][str(item)] )[-1]), ','), ) print('max:') print( '100\%', format( int((max_data['sim_cum_confirmed_deduction_s1'][str(item)][-1] )), ','), '60\%', format( int((max_data['sim_cum_confirmed_deduction_s8'][str(item)] )[-1]), ','), '35\%', format( int((max_data['sim_cum_confirmed_deduction_s2'][str(item)] )[-1]), ','), '10\%', format( int((max_data['sim_cum_confirmed_deduction_s3'][str(item)] )[-1]), ','), ) print('infected') print('middle: ') print( '100\%', format( int((data['sim_cum_infection_deduction_s1'][str(item)][-1])), ','), '({}-{})\n'.format( format( int((min_data['sim_cum_infection_deduction_s1'][str(item)] [-1])), ','), format( int((max_data['sim_cum_infection_deduction_s1'][str(item)] [-1])), ',')), '60\%', format( int((data['sim_cum_infection_deduction_s8'][str(item)])[-1]), ','), '({}-{})\n'.format( format( int((min_data['sim_cum_infection_deduction_s8'][str(item)] [-1])), ','), format( int((max_data['sim_cum_infection_deduction_s8'][str(item)] [-1])), ',')), '35\%', format( int((data['sim_cum_infection_deduction_s2'][str(item)])[-1]), ','), '({}-{})\n'.format( format( int((min_data['sim_cum_infection_deduction_s2'][str(item)] [-1])), ','), format( int((max_data['sim_cum_infection_deduction_s2'][str(item)] [-1])), ',')), '10\%', format( int((data['sim_cum_infection_deduction_s3'][str(item)])[-1]), ','), '({}-{})\n'.format( format( int((min_data['sim_cum_infection_deduction_s3'][str(item)] [-1])), ','), format( int((max_data['sim_cum_infection_deduction_s3'][str(item)] [-1])), ',')), ) print('min: ') print( '100\%', format( int((min_data['sim_cum_infection_deduction_s1'][str(item)][-1] )), ','), '60\%', format( int((min_data['sim_cum_infection_deduction_s8'][str(item)] )[-1]), ','), '35\%', format( int((min_data['sim_cum_infection_deduction_s2'][str(item)] )[-1]), ','), '10\%', format( int((min_data['sim_cum_infection_deduction_s3'][str(item)] )[-1]), ','), ) print('max:') print( '100\%', format( int((max_data['sim_cum_infection_deduction_s1'][str(item)][-1] )), ','), '60\%', format( int((max_data['sim_cum_infection_deduction_s8'][str(item)] )[-1]), ','), '35\%', format( int((max_data['sim_cum_infection_deduction_s2'][str(item)] )[-1]), ','), '10\%', format( int((max_data['sim_cum_infection_deduction_s3'][str(item)] )[-1]), ','), ) print('==================================', code_dict[item], str(final_date)) """ plot3(get_total(data, 'sim_cum_confirmed_deduction_s1',total_cities), get_total(data, 'sim_cum_confirmed_deduction_s2',total_cities), get_total(data, 'sim_cum_confirmed_deduction_s3',total_cities), get_total(data, 'real_cum_confirmed',total_cities), 'Prediction of the cumulative number of confirmed cases', 'img/{}/cum_confirmed_prediction.{}'.format('0000', fmt), touchratio=1, ratio_low=0.5, ratio_high=1.5) plot3(get_total(data, 'sim_confirmed_deduction_s1', total_cities), get_total(data, 'sim_confirmed_deduction_s2', total_cities), get_total(data, 'sim_confirmed_deduction_s3', total_cities), get_total(data, 'real_confirmed', total_cities), 'Prediction of the cumulative number of confirmed cases', 'img/{}/confirmed_prediction.{}'.format('0000', fmt), touchratio=1, ratio_low=0.5, ratio_high=1.5) """ print('drawing different deduction lately') for item in important_cities: print(item) it_max = None #if item == 900004: # it_max = 40000 #if item == 900005: # it_max = 200000 #if item == 900007: # it_max = 80000 #if item == 900008: # it_max = 60000 plot3(data['sim_cum_confirmed_deduction_s1'][str(item)], data['sim_cum_confirmed_deduction_s5'][str(item)], data['sim_cum_confirmed_deduction_s4'][str(item)], data['real_cum_confirmed'][str(item)], 'Prediction of the cumulative number of confirmed cases', 'img/{}/cum_confirmed_prediction_simdate.{}'.format( str(item), fmt), touchratio=data['x'][str(item)][1], ratio_low=data['touch_ratio_low'][str(item)], ratio_high=data['touch_ratio_hight'][str(item)], date_it=get_important_date(item) + timedelta(15), start_date_it=date(2020, 2, 15)) plot3(data['sim_confirmed_deduction_s1'][str(item)], data['sim_confirmed_deduction_s5'][str(item)], data['sim_confirmed_deduction_s4'][str(item)], data['real_confirmed'][str(item)], 'Prediction of the cumulative number of confirmed cases', 'img/{}/confirmed_prediction_simdate.{}'.format(str(item), fmt), touchratio=data['x'][str(item)][1], ratio_low=data['touch_ratio_low'][str(item)], ratio_high=data['touch_ratio_hight'][str(item)], date_it=get_important_date(item) + timedelta(15), it_max=it_max, it_min=0, start_date_it=date(2020, 2, 15)) """ plot3(get_total(data, 'sim_cum_confirmed_deduction_s1', total_cities), get_total(data, 'sim_cum_confirmed_deduction_s5', total_cities), get_total(data, 'sim_cum_confirmed_deduction_s4', total_cities), get_total(data, 'real_cum_confirmed', total_cities), 'Prediction of the cumulative number of confirmed cases', 'img/{}/cum_confirmed_prediction_simdate.{}'.format('0000', fmt), touchratio=1, ratio_low=0.5, ratio_high=1.5) plot3(get_total(data, 'sim_confirmed_deduction_s1', total_cities), get_total(data, 'sim_confirmed_deduction_s5', total_cities), get_total(data, 'sim_confirmed_deduction_s4', total_cities), get_total(data, 'real_confirmed', total_cities), 'Prediction of the cumulative number of confirmed cases', 'img/{}/confirmed_prediction_simdate.{}'.format('0000', fmt), touchratio=1, ratio_low=0.5, ratio_high=1.5) """ print('output loss') loss_dict = {} for item in total_cities: loss_dict[item] = data['loss'][str(item)] loss_df = pd.DataFrame(loss_dict, index=[0]) loss_df.to_csv('./loss.csv', index=False) print('======== cur') for item in total_cities: print('{} &'.format(code_dict[item]), sep='', end='') print('{} &'.format( format(int(data['real_cum_confirmed'][str(item)][-1]), ',')), sep='', end='') print('{} ({}-{}) &'.format( format( int(data['sim_cum_confirmed_deduction_s1'][str(item)][lreal - 1]), ','), format( int(min_data['sim_cum_confirmed_deduction_s1'][str(item)][lreal - 1]), ','), format( int(max_data['sim_cum_confirmed_deduction_s1'][str(item)][lreal - 1]), ','), ), sep='', end='') print('{} ({}-{}) &'.format( format( int(data['sim_cum_infection_deduction_s1'][str(item)][lreal - 1]), ','), format( int(min_data['sim_cum_infection_deduction_s1'][str(item)][lreal - 1]), ','), format( int(max_data['sim_cum_infection_deduction_s1'][str(item)][lreal - 1]), ','), ), sep='', end='') print('{} ({}-{}) &'.format( format( int(data['sim_total_infection_deduction_s1'][str(item)][lreal - 1]), ','), format( int(min_data['sim_total_infection_deduction_s1'][str(item)][ lreal - 1]), ','), format( int(max_data['sim_total_infection_deduction_s1'][str(item)][ lreal - 1]), ','), ), sep='', end='') print('{} ({}-{}) \\\\'.format( format( int(data['sim_cum_self_cured_deduction_s1'][str(item)][lreal - 1]), ','), format( int(min_data['sim_cum_self_cured_deduction_s1'][str(item)][ lreal - 1]), ','), format( int(max_data['sim_cum_self_cured_deduction_s1'][str(item)][ lreal - 1]), ','), ), sep='', end='') print('') print('=======') # print variables x_list = construct_x(data, total_cities) min_x_list = construct_x(min_data, total_cities) max_x_list = construct_x(max_data, total_cities) format_out(x_list, min_x_list, max_x_list)
def main(): data = {} code_dict = it_code_dict() fmt = 'pdf' total_cities = pd.read_csv(get_data_path())['adcode'].unique() #important_cities = [420000,110000,120000,320000,330000,440000,510000] important_cities = total_cities with open('./data_run_middle.json', 'r') as f: data = json.load(f) with open('./data_run_xmin.json', 'r') as f: min_data = json.load(f) with open('./data_run_xmax.json', 'r') as f: max_data = json.load(f) if not os.path.exists('./img'): os.mkdir('./img') if not os.path.exists('./img/0000'): os.makedirs('./img/0000') for item in important_cities: it_path = './img/{}'.format(str(item)) if not os.path.exists(it_path): os.mkdir(it_path) # test # end test cum_dead_res = [get_total(data, 'real_cum_dead', total_cities)[-1], get_total(data, 'sim_cum_dead_s1', total_cities)[-1], get_total(data, 'sim_cum_dead_s2', total_cities)[-1], get_total(data, 'sim_cum_dead_s3', total_cities)[-1], get_total(data, 'sim_cum_dead_s4', total_cities)[-1], get_total(data, 'sim_cum_dead_s5', total_cities)[-1], get_total(data, 'sim_cum_dead_s6', total_cities)[-1], get_total(data, 'sim_cum_dead_s7', total_cities)[-1], get_total(data, 'sim_cum_dead_s8', total_cities)[-1], ] d_cum =get_total(data, 'sim_cum_dead_s1', total_cities)[-60:] print(d_cum[0], d_cum[-1], d_cum[-1]-d_cum[0]) for i in range(2, len(cum_dead_res)): cum_dead_res[i] = cum_dead_res[i] + cum_dead_res[0] - cum_dead_res[1] + (d_cum[-1]-d_cum[0]) cum_dead_res[1] = cum_dead_res[1] + cum_dead_res[0] - cum_dead_res[1] + (d_cum[-1]-d_cum[0]) print('cum dead res: ',cum_dead_res) cum_confirm_res = [get_total(data, 'real_cum_confirmed', total_cities)[-1], get_total(data, 'sim_cum_confirmed_deduction_s1', total_cities)[-1], get_total(data, 'sim_cum_confirmed_deduction_s2', total_cities)[-1], get_total(data, 'sim_cum_confirmed_deduction_s3', total_cities)[-1], get_total(data, 'sim_cum_confirmed_deduction_s4', total_cities)[-1], get_total(data, 'sim_cum_confirmed_deduction_s5', total_cities)[-1], get_total(data, 'sim_cum_confirmed_deduction_s6', total_cities)[-1], get_total(data, 'sim_cum_confirmed_deduction_s7', total_cities)[-1], get_total(data, 'sim_cum_confirmed_deduction_s8', total_cities)[-1], ] print('cum confirm case: ', cum_confirm_res) cum_res = [cum_confirm_res[1], cum_confirm_res[8], cum_confirm_res[3], cum_confirm_res[2], cum_confirm_res[1], cum_confirm_res[4], cum_confirm_res[5], cum_confirm_res[6], cum_dead_res[1], cum_dead_res[8], cum_dead_res[3], cum_dead_res[2], cum_dead_res[1], cum_dead_res[4], cum_dead_res[5], cum_dead_res[6]] for item in cum_res: print(int(item), ' & ', end='') print('\\\\') cum_infection_res = [np.sum(get_total(data, 'sim_cum_infection_deduction_s1', total_cities)[-1]), np.sum(get_total(data, 'sim_cum_infection_deduction_s2', total_cities)[-1]), np.sum(get_total(data, 'sim_cum_infection_deduction_s3', total_cities)[-1]), np.sum(get_total(data, 'sim_cum_infection_deduction_s4', total_cities)[-1]), np.sum(get_total(data, 'sim_cum_infection_deduction_s5', total_cities)[-1]), np.sum(get_total(data, 'sim_cum_infection_deduction_s6', total_cities)[-1]), np.sum(get_total(data, 'sim_cum_infection_deduction_s7', total_cities)[-1]), ] print(cum_infection_res) lreal = len(get_total(data, 'real_cum_confirmed', total_cities)) cum_infection_res = [np.sum(get_total(data, 'sim_cum_infection_deduction_s1', total_cities)[lreal-1]), np.sum(get_total(data, 'sim_cum_infection_deduction_s2', total_cities)[lreal-1]), np.sum(get_total(data, 'sim_cum_infection_deduction_s3', total_cities)[lreal-1]), np.sum(get_total(data, 'sim_cum_infection_deduction_s4', total_cities)[lreal-1]), np.sum(get_total(data, 'sim_cum_infection_deduction_s5', total_cities)[lreal-1]), np.sum(get_total(data, 'sim_cum_infection_deduction_s6', total_cities)[lreal-1]), np.sum(get_total(data, 'sim_cum_infection_deduction_s7', total_cities)[lreal-1]), ] print(cum_infection_res) print(get_total(data, 'sim_cum_infection_deduction_s1', total_cities)[lreal-1], get_total(data, 'sim_cum_confirmed', total_cities)[lreal - 1], get_total(data, 'real_cum_confirmed', total_cities)[lreal - 1], ) print(get_total(data, 'sim_cum_infection_deduction_s1', total_cities)[- 1], get_total(data, 'sim_cum_confirmed_deduction_s1', total_cities)[- 1], get_total(data, 'real_cum_confirmed', total_cities)[- 1], ) print("final day: ", date(2020,1,11) + timedelta(days=len(get_total(data, 'sim_cum_confirmed_deduction_s1', total_cities)))) final_date = date(2020,1,11) + timedelta(days=len(get_total(data, 'sim_cum_confirmed_deduction_s1', total_cities))) cur_date = date(2020,1, 11) + timedelta(days=len(get_total(data, 'real_confirmed', total_cities))) # draw compare to real data for every city and full print('drawing real/sim compare') print('city', 'real_confirmed', 'sim_confirmed', 'sim_infect', 'sim_confirmed / sim_infect') print('Province&real confirmed&MLSim confirmed&MLSim infected& proportion of asymptomatic\\\\\\hline') for item in total_cities: real_cum = data['real_cum_confirmed'][str(item)][-1] sim_cum_confirm =data['sim_cum_confirmed'][str(item)][-1] sim_cum_infect = data['sim_cum_infection_deduction_s1'][str(item)][lreal-1] #print(code_dict[int(item)], # real_cum, # sim_cum_confirm, # sim_cum_infect, # #np.round(real_cum / sim_cum_infect, 3), # np.round(sim_cum_confirm / sim_cum_infect, 3),sep='\t\t') print(code_dict[int(item)], ' & ', real_cum, ' & ', int(sim_cum_confirm), ' & ', int(sim_cum_infect), ' & ', np.round((1-sim_cum_confirm / sim_cum_infect)*100, 3), '$\\%$\\\\ ',sep='') print('China\'s mainland', ' & ', int(get_total(data, 'real_cum_confirmed', total_cities)[-1]), ' & ', int(get_total(data, 'sim_cum_confirmed', total_cities)[-1]), ' & ', int(get_total(data, 'sim_cum_infection_deduction_s1', total_cities)[lreal-1]), ' & ', np.round((1 - (get_total(data, 'sim_cum_confirmed', total_cities)[-1]) / (get_total(data, 'sim_cum_infection_deduction_s1', total_cities)[lreal-1])) * 100, 3), '$\\%$\\\\ ', sep='') diff_time_confirm = [ int(get_total(data,'sim_cum_confirmed_deduction_s1',total_cities)[-1]), int(get_total(data, 'sim_cum_confirmed_deduction_s9', total_cities)[-1]), int(get_total(data, 'sim_cum_confirmed_deduction_s10', total_cities)[-1]), int(get_total(data, 'sim_cum_confirmed_deduction_s11', total_cities)[-1]), int(get_total(data, 'sim_cum_confirmed_deduction_s12', total_cities)[-1]), int(get_total(data, 'sim_cum_infection_deduction_s1', total_cities)[-1]), int(get_total(data, 'sim_cum_infection_deduction_s9', total_cities)[-1]), int(get_total(data, 'sim_cum_infection_deduction_s10', total_cities)[-1]), int(get_total(data, 'sim_cum_infection_deduction_s11', total_cities)[-1]), int(get_total(data, 'sim_cum_infection_deduction_s12', total_cities)[-1]), ] print('diff confirm: ', diff_time_confirm) for item in diff_time_confirm: print(int(item), ' & ', end='') print('\\\\') for item in total_cities: plot1_shade(data['real_cum_confirmed'][str(item)], data['sim_cum_confirmed'][str(item)], min_data['sim_cum_confirmed'][str(item)], max_data['sim_cum_confirmed'][str(item)], code_dict[int(item)], 'img/{}/cum_real_sim.{}'.format(str(item), fmt)) plot1_shade(data['real_confirmed'][str(item)], data['sim_confirmed'][str(item)], min_data['sim_confirmed'][str(item)], max_data['sim_confirmed'][str(item)], code_dict[int(item)], 'img/{}/increase_real_sim.{}'.format(str(item), fmt)) plot1_shade(data['real_cum_confirmed'][str(item)], data['sim_cum_confirmed_deduction_s1'][str(item)], min_data['sim_cum_confirmed_deduction_s1'][str(item)], max_data['sim_cum_confirmed_deduction_s1'][str(item)], code_dict[int(item)], 'img/{}/cum_real_sim_deduction.{}'.format(str(item), fmt),interval=20) plot1_shade(data['real_confirmed'][str(item)], data['sim_confirmed_deduction_s1'][str(item)], min_data['sim_confirmed_deduction_s1'][str(item)], max_data['sim_confirmed_deduction_s1'][str(item)], code_dict[int(item)], 'img/{}/increase_real_sim_dedection.{}'.format(str(item), fmt), interval=20) plot33_shade(data['sim_cum_confirmed_deduction_s1'][str(item)], min_data['sim_cum_confirmed_deduction_s1'][str(item)], max_data['sim_cum_confirmed_deduction_s1'][str(item)], data['sim_cum_confirmed_deduction_s2'][str(item)], min_data['sim_cum_confirmed_deduction_s2'][str(item)], max_data['sim_cum_confirmed_deduction_s2'][str(item)], data['sim_cum_confirmed_deduction_s3'][str(item)], min_data['sim_cum_confirmed_deduction_s3'][str(item)], max_data['sim_cum_confirmed_deduction_s3'][str(item)], data['sim_cum_confirmed_deduction_s8'][str(item)], min_data['sim_cum_confirmed_deduction_s8'][str(item)], max_data['sim_cum_confirmed_deduction_s8'][str(item)], data['real_cum_confirmed'][str(item)], code_dict[int(item)], 'img/{}/cum_confirmed_prediction.{}'.format(str(item), fmt), touchratio=data['x'][str(item)][1], ratio_low=data['touch_ratio_low'][str(item)], ratio_high=data['touch_ratio_hight'][str(item)], loc='lower right', date_it=date(2020, 1, 23), ext_flag=True ) plot33_shade(data['sim_confirmed_deduction_s1'][str(item)], min_data['sim_confirmed_deduction_s1'][str(item)], max_data['sim_confirmed_deduction_s1'][str(item)], data['sim_confirmed_deduction_s2'][str(item)], min_data['sim_confirmed_deduction_s2'][str(item)], max_data['sim_confirmed_deduction_s2'][str(item)], data['sim_confirmed_deduction_s3'][str(item)],min_data['sim_confirmed_deduction_s3'][str(item)], max_data['sim_confirmed_deduction_s3'][str(item)], data['sim_confirmed_deduction_s8'][str(item)], min_data['sim_confirmed_deduction_s8'][str(item)], max_data['sim_confirmed_deduction_s8'][str(item)], data['real_confirmed'][str(item)], code_dict[int(item)], 'img/{}/confirmed_prediction.{}'.format(str(item), fmt), touchratio=data['x'][str(item)][1], ratio_low=data['touch_ratio_low'][str(item)], ratio_high=data['touch_ratio_hight'][str(item)], loc='upper right', date_it=date(2020, 1, 23), ext_flag=True ) plot33_shade( get_total(data, 'sim_cum_confirmed_deduction_s1', total_cities), get_total(min_data,'sim_cum_confirmed_deduction_s1',total_cities), get_total(max_data,'sim_cum_confirmed_deduction_s1',total_cities), get_total(data, 'sim_cum_confirmed_deduction_s2', total_cities), get_total(min_data,'sim_cum_confirmed_deduction_s2',total_cities), get_total(max_data,'sim_cum_confirmed_deduction_s2',total_cities), get_total(data, 'sim_cum_confirmed_deduction_s3', total_cities), get_total(min_data,'sim_cum_confirmed_deduction_s3',total_cities), get_total(max_data,'sim_cum_confirmed_deduction_s3',total_cities), get_total(data, 'sim_cum_confirmed_deduction_s8', total_cities), get_total(min_data,'sim_cum_confirmed_deduction_s8',total_cities), get_total(max_data,'sim_cum_confirmed_deduction_s8',total_cities), get_total(data,'real_cum_confirmed', total_cities), 'China\'s mainland', 'img/{}/cum_confirmed_prediction.{}'.format('0000', fmt), touchratio=0.1, ratio_low=0.2, ratio_high=0.4, loc='lower right', date_it=date(2020, 1, 23), ext_flag=True ) plot33_shade( get_total(data, 'sim_confirmed_deduction_s1', total_cities), get_total(min_data,'sim_confirmed_deduction_s1',total_cities), get_total(max_data,'sim_confirmed_deduction_s1',total_cities), get_total(data, 'sim_confirmed_deduction_s2', total_cities), get_total(min_data,'sim_confirmed_deduction_s2',total_cities), get_total(max_data,'sim_confirmed_deduction_s2',total_cities), get_total(data, 'sim_confirmed_deduction_s3', total_cities), get_total(min_data,'sim_confirmed_deduction_s3',total_cities), get_total(max_data,'sim_confirmed_deduction_s3',total_cities), get_total(data, 'sim_confirmed_deduction_s8', total_cities), get_total(min_data,'sim_confirmed_deduction_s8',total_cities), get_total(max_data,'sim_confirmed_deduction_s8',total_cities), get_total(data,'real_confirmed', total_cities), 'China\'s mainland', 'img/{}/confirmed_prediction.{}'.format('0000', fmt), touchratio=0.1, ratio_low=0.2, ratio_high=0.3, loc='upper right', date_it=date(2020, 1, 23), ext_flag=True ) print('==================================', str(final_date)) print('Jan 23 different control measure: ') print('confirmed') print('middle: ') print('100\%', format(int(get_total(data, 'sim_cum_confirmed_deduction_s1', total_cities)[-1]), ','), '190\%', format(int(get_total(data, 'sim_cum_confirmed_deduction_s2', total_cities)[-1]), ','), '160\%', format(int(get_total(data, 'sim_cum_confirmed_deduction_s3', total_cities)[-1]), ','), '130\%', format(int(get_total(data, 'sim_cum_confirmed_deduction_s8', total_cities)[-1]), ','), ) print('min: ') print('100\%', format(int(get_total(min_data, 'sim_cum_confirmed_deduction_s1', total_cities)[-1]), ','), '190\%', format(int(get_total(min_data, 'sim_cum_confirmed_deduction_s2', total_cities)[-1]), ','), '160\%', format(int(get_total(min_data, 'sim_cum_confirmed_deduction_s3', total_cities)[-1]), ','), '130\%', format(int(get_total(min_data, 'sim_cum_confirmed_deduction_s8', total_cities)[-1]), ','), ) print('max:') print('100\%', format(int(get_total(max_data, 'sim_cum_confirmed_deduction_s1', total_cities)[-1]), ','), '190\%', format(int(get_total(max_data, 'sim_cum_confirmed_deduction_s2', total_cities)[-1]), ','), '160\%', format(int(get_total(max_data, 'sim_cum_confirmed_deduction_s3', total_cities)[-1]), ','), '130\%', format(int(get_total(max_data, 'sim_cum_confirmed_deduction_s8', total_cities)[-1]), ','), ) print('infected') print('middle: ') print('100\%', format(int(get_total(data, 'sim_cum_infection_deduction_s1', total_cities)[-1]), ','), '190\%', format(int(get_total(data, 'sim_cum_infection_deduction_s2', total_cities)[-1]), ','), '160\%', format(int(get_total(data, 'sim_cum_infection_deduction_s3', total_cities)[-1]), ','), '130\%', format(int(get_total(data, 'sim_cum_infection_deduction_s8', total_cities)[-1]), ','), ) print('min: ') print('100\%', format(int(get_total(min_data, 'sim_cum_infection_deduction_s1', total_cities)[-1]), ','), '190\%', format(int(get_total(min_data, 'sim_cum_infection_deduction_s2', total_cities)[-1]), ','), '160\%', format(int(get_total(min_data, 'sim_cum_infection_deduction_s3', total_cities)[-1]), ','), '130\%', format(int(get_total(min_data, 'sim_cum_infection_deduction_s8', total_cities)[-1]), ','), ) print('max:') print('100\%', format(int(get_total(max_data, 'sim_cum_infection_deduction_s1', total_cities)[-1]), ','), '190\%', format(int(get_total(max_data, 'sim_cum_infection_deduction_s2', total_cities)[-1]), ','), '160\%', format(int(get_total(max_data, 'sim_cum_infection_deduction_s3', total_cities)[-1]), ','), '130\%', format(int(get_total(max_data, 'sim_cum_infection_deduction_s8', total_cities)[-1]), ','), ) print('==================================', str(final_date)) plot33_shade( get_total(data, 'sim_cum_confirmed_deduction_s1', total_cities), get_total(min_data, 'sim_cum_confirmed_deduction_s1', total_cities), get_total(max_data, 'sim_cum_confirmed_deduction_s1', total_cities), get_total(data, 'sim_cum_confirmed_deduction_s5', total_cities), get_total(min_data, 'sim_cum_confirmed_deduction_s5', total_cities), get_total(max_data, 'sim_cum_confirmed_deduction_s5', total_cities), get_total(data, 'sim_cum_confirmed_deduction_s4', total_cities), get_total(min_data, 'sim_cum_confirmed_deduction_s4', total_cities), get_total(max_data, 'sim_cum_confirmed_deduction_s4', total_cities), get_total(data, 'sim_cum_confirmed_deduction_s6', total_cities), get_total(min_data, 'sim_cum_confirmed_deduction_s6', total_cities), get_total(max_data, 'sim_cum_confirmed_deduction_s6', total_cities), get_total(data, 'real_cum_confirmed', total_cities), 'China\'s mainland', 'img/{}/cum_confirmed_prediction_simdate.{}'.format('0000', fmt), touchratio=0.1, ratio_low=0.2, ratio_high=0.4, loc='lower right', date_it=date(2020, 3, 1), ) plot33_shade( get_total(data, 'sim_confirmed_deduction_s1', total_cities), get_total(min_data, 'sim_confirmed_deduction_s1', total_cities), get_total(max_data, 'sim_confirmed_deduction_s1', total_cities), get_total(data, 'sim_confirmed_deduction_s5', total_cities), get_total(min_data, 'sim_confirmed_deduction_s5', total_cities), get_total(max_data, 'sim_confirmed_deduction_s5', total_cities), get_total(data, 'sim_confirmed_deduction_s4', total_cities), get_total(min_data, 'sim_confirmed_deduction_s4', total_cities), get_total(max_data, 'sim_confirmed_deduction_s4', total_cities), get_total(data, 'sim_confirmed_deduction_s6', total_cities), get_total(min_data, 'sim_confirmed_deduction_s6', total_cities), get_total(max_data, 'sim_confirmed_deduction_s6', total_cities), get_total(data, 'real_confirmed', total_cities), 'China\'s mainland', 'img/{}/confirmed_prediction_simdate.{}'.format('0000', fmt), touchratio=0.1, ratio_low=0.2, ratio_high=0.3, loc='upper right', date_it=date(2020, 3, 1), ) for item in important_cities: print(item) plot33_shade(data['sim_cum_confirmed_deduction_s1'][str(item)],min_data['sim_cum_confirmed_deduction_s1'][str(item)],max_data['sim_cum_confirmed_deduction_s1'][str(item)], data['sim_cum_confirmed_deduction_s2'][str(item)],min_data['sim_cum_confirmed_deduction_s2'][str(item)],max_data['sim_cum_confirmed_deduction_s2'][str(item)], data['sim_cum_confirmed_deduction_s3'][str(item)],min_data['sim_cum_confirmed_deduction_s3'][str(item)],max_data['sim_cum_confirmed_deduction_s3'][str(item)], data['sim_cum_confirmed_deduction_s8'][str(item)],min_data['sim_cum_confirmed_deduction_s8'][str(item)],max_data['sim_cum_confirmed_deduction_s8'][str(item)], data['real_cum_confirmed'][str(item)], code_dict[int(item)], 'img/{}/cum_confirmed_prediction.{}'.format(str(item), fmt), touchratio=data['x'][str(item)][1], ratio_low=data['touch_ratio_low'][str(item)], ratio_high=data['touch_ratio_hight'][str(item)], loc='lower right', date_it=date(2020,1,23), ext_flag=True ) plot33_shade(data['sim_confirmed_deduction_s1'][str(item)],min_data['sim_confirmed_deduction_s1'][str(item)],max_data['sim_confirmed_deduction_s1'][str(item)], data['sim_confirmed_deduction_s2'][str(item)],min_data['sim_confirmed_deduction_s2'][str(item)],max_data['sim_confirmed_deduction_s2'][str(item)], data['sim_confirmed_deduction_s3'][str(item)],min_data['sim_confirmed_deduction_s3'][str(item)],max_data['sim_confirmed_deduction_s3'][str(item)], data['sim_confirmed_deduction_s8'][str(item)],min_data['sim_confirmed_deduction_s8'][str(item)],max_data['sim_confirmed_deduction_s8'][str(item)], data['real_confirmed'][str(item)], code_dict[int(item)], 'img/{}/confirmed_prediction.{}'.format(str(item), fmt), touchratio=data['x'][str(item)][1], ratio_low=data['touch_ratio_low'][str(item)], ratio_high=data['touch_ratio_hight'][str(item)], loc='upper right', date_it=date(2020,1,23), ext_flag=True ) plot33_shade(data['sim_cum_confirmed_deduction_s1'][str(item)], min_data['sim_cum_confirmed_deduction_s1'][str(item)], max_data['sim_cum_confirmed_deduction_s1'][str(item)], data['sim_cum_confirmed_deduction_s5'][str(item)], min_data['sim_cum_confirmed_deduction_s5'][str(item)], max_data['sim_cum_confirmed_deduction_s5'][str(item)], data['sim_cum_confirmed_deduction_s4'][str(item)], min_data['sim_cum_confirmed_deduction_s4'][str(item)], max_data['sim_cum_confirmed_deduction_s4'][str(item)], data['sim_cum_confirmed_deduction_s6'][str(item)], min_data['sim_cum_confirmed_deduction_s6'][str(item)], max_data['sim_cum_confirmed_deduction_s6'][str(item)], data['real_cum_confirmed'][str(item)], code_dict[int(item)], 'img/{}/cum_confirmed_prediction_simdate.{}'.format(str(item), fmt), touchratio=data['x'][str(item)][1], ratio_low=data['touch_ratio_low'][str(item)], ratio_high=data['touch_ratio_hight'][str(item)], loc='lower right', date_it=date(2020, 1, 23), #ext_flag=True ) plot33_shade(data['sim_confirmed_deduction_s1'][str(item)], min_data['sim_confirmed_deduction_s1'][str(item)], max_data['sim_confirmed_deduction_s1'][str(item)], data['sim_confirmed_deduction_s5'][str(item)], min_data['sim_confirmed_deduction_s5'][str(item)], max_data['sim_confirmed_deduction_s5'][str(item)], data['sim_confirmed_deduction_s4'][str(item)], min_data['sim_confirmed_deduction_s4'][str(item)], max_data['sim_confirmed_deduction_s4'][str(item)], data['sim_confirmed_deduction_s6'][str(item)], min_data['sim_confirmed_deduction_s4'][str(item)], max_data['sim_confirmed_deduction_s6'][str(item)], data['real_confirmed'][str(item)], code_dict[int(item)], 'img/{}/confirmed_prediction_simdate.{}'.format(str(item), fmt), touchratio=data['x'][str(item)][1], ratio_low=data['touch_ratio_low'][str(item)], ratio_high=data['touch_ratio_hight'][str(item)], loc='upper right', date_it=date(2020, 1, 23), #ext_flag=True ) plot33_time_shade( data['sim_confirmed_deduction_s1'][str(item)],min_data['sim_confirmed_deduction_s1'][str(item)],max_data['sim_confirmed_deduction_s1'][str(item)], data['sim_confirmed_deduction_s9'][str(item)],min_data['sim_confirmed_deduction_s9'][str(item)],max_data['sim_confirmed_deduction_s9'][str(item)], data['sim_confirmed_deduction_s10'][str(item)],min_data['sim_confirmed_deduction_s10'][str(item)],max_data['sim_confirmed_deduction_s10'][str(item)], data['sim_confirmed_deduction_s11'][str(item)],min_data['sim_confirmed_deduction_s11'][str(item)],max_data['sim_confirmed_deduction_s11'][str(item)], data['sim_confirmed_deduction_s12'][str(item)],min_data['sim_confirmed_deduction_s12'][str(item)],max_data['sim_confirmed_deduction_s12'][str(item)], data['real_confirmed'][str(item)], code_dict[item], 'img/{}/confirmed_prediction_simdate_diff.{}'.format(str(item), fmt), touchratio=1, ratio_high=1.5, ratio_low=0.5, loc='upper right') plot33_time_shade( data['sim_cum_confirmed_deduction_s1'][str(item)], min_data['sim_cum_confirmed_deduction_s1'][str(item)], max_data['sim_cum_confirmed_deduction_s1'][str(item)], data['sim_cum_confirmed_deduction_s9'][str(item)], min_data['sim_cum_confirmed_deduction_s9'][str(item)], max_data['sim_cum_confirmed_deduction_s9'][str(item)], data['sim_cum_confirmed_deduction_s10'][str(item)], min_data['sim_cum_confirmed_deduction_s10'][str(item)], max_data['sim_cum_confirmed_deduction_s10'][str(item)], data['sim_cum_confirmed_deduction_s11'][str(item)], min_data['sim_cum_confirmed_deduction_s11'][str(item)], max_data['sim_cum_confirmed_deduction_s11'][str(item)], data['sim_cum_confirmed_deduction_s12'][str(item)], min_data['sim_cum_confirmed_deduction_s12'][str(item)], max_data['sim_cum_confirmed_deduction_s12'][str(item)], data['real_cum_confirmed'][str(item)], code_dict[item], 'img/{}/cum_confirmed_prediction_simdate_diff.{}'.format(str(item), fmt), touchratio=1, ratio_high=1.5, ratio_low=0.5, loc='upper right') print('==================================', str(final_date)) print('March 1 different control measure: ') print('confirmed') print('middle: ') print('100\%', format(int(get_total(data, 'sim_cum_confirmed_deduction_s1', total_cities)[-1]), ','), '190\%', format(int(get_total(data, 'sim_cum_confirmed_deduction_s5', total_cities)[-1]), ','), '160\%', format(int(get_total(data, 'sim_cum_confirmed_deduction_s4', total_cities)[-1]), ','), '210\%', format(int(get_total(data, 'sim_cum_confirmed_deduction_s6', total_cities)[-1]), ','), ) print('min: ') print('100\%', format(int(get_total(min_data, 'sim_cum_confirmed_deduction_s1', total_cities)[-1]), ','), '190\%', format(int(get_total(min_data, 'sim_cum_confirmed_deduction_s5', total_cities)[-1]), ','), '160\%', format(int(get_total(min_data, 'sim_cum_confirmed_deduction_s4', total_cities)[-1]), ','), '210\%', format(int(get_total(min_data, 'sim_cum_confirmed_deduction_s6', total_cities)[-1]), ','), ) print('max:') print('100\%', format(int(get_total(max_data, 'sim_cum_confirmed_deduction_s1', total_cities)[-1]), ','), '190\%', format(int(get_total(max_data, 'sim_cum_confirmed_deduction_s5', total_cities)[-1]), ','), '160\%', format(int(get_total(max_data, 'sim_cum_confirmed_deduction_s4', total_cities)[-1]), ','), '210\%', format(int(get_total(max_data, 'sim_cum_confirmed_deduction_s6', total_cities)[-1]), ','), ) print('infected') print('middle: ') print('100\%', format(int(get_total(data, 'sim_cum_infection_deduction_s1', total_cities)[-1]), ','), '190\%', format(int(get_total(data, 'sim_cum_infection_deduction_s5', total_cities)[-1]), ','), '160\%', format(int(get_total(data, 'sim_cum_infection_deduction_s4', total_cities)[-1]), ','), '210\%', format(int(get_total(data, 'sim_cum_infection_deduction_s6', total_cities)[-1]), ','), ) print('min: ') print('100\%', format(int(get_total(min_data, 'sim_cum_infection_deduction_s1', total_cities)[-1]), ','), '190\%', format(int(get_total(min_data, 'sim_cum_infection_deduction_s5', total_cities)[-1]), ','), '160\%', format(int(get_total(min_data, 'sim_cum_infection_deduction_s4', total_cities)[-1]), ','), '210\%', format(int(get_total(min_data, 'sim_cum_infection_deduction_s6', total_cities)[-1]), ','), ) print('max:') print('100\%', format(int(get_total(max_data, 'sim_cum_infection_deduction_s1', total_cities)[-1]), ','), '190\%', format(int(get_total(max_data, 'sim_cum_infection_deduction_s5', total_cities)[-1]), ','), '160\%', format(int(get_total(max_data, 'sim_cum_infection_deduction_s4', total_cities)[-1]), ','), '210\%', format(int(get_total(max_data, 'sim_cum_infection_deduction_s6', total_cities)[-1]), ','), ) print('==================================', str(final_date)) plot1_shade(get_total(data, 'real_cum_confirmed', total_cities), get_total(data, 'sim_cum_confirmed', total_cities), get_total(min_data, 'sim_cum_confirmed', total_cities), get_total(max_data, 'sim_cum_confirmed', total_cities), 'China\'s mainland', 'img/{}/cum_real_sim.{}'.format('0000', fmt)) plot1_shade(get_total(data, 'real_confirmed', total_cities), get_total(data, 'sim_confirmed', total_cities), get_total(min_data, 'sim_confirmed', total_cities), get_total(max_data, 'sim_confirmed', total_cities), 'China\'s mainland', 'img/{}/increase_real_sim.{}'.format('0000', fmt)) plot1_shade(get_total(data, 'real_cum_dead', total_cities), get_total(data, 'sim_cum_dead_s1', total_cities), get_total(min_data, 'sim_cum_dead_s1', total_cities), get_total(max_data, 'sim_cum_dead_s1', total_cities), 'China\'s mainland', 'img/{}/cum_real_sim_dead.{}'.format('0000', fmt), interval=20) plot33_time_shade( get_total(data, 'sim_cum_confirmed_deduction_s1', total_cities), get_total(min_data, 'sim_cum_confirmed_deduction_s1', total_cities), get_total(max_data, 'sim_cum_confirmed_deduction_s1', total_cities), get_total(data, 'sim_cum_confirmed_deduction_s9', total_cities), get_total(min_data, 'sim_cum_confirmed_deduction_s9', total_cities), get_total(max_data, 'sim_cum_confirmed_deduction_s9', total_cities), get_total(data, 'sim_cum_confirmed_deduction_s10', total_cities), get_total(min_data, 'sim_cum_confirmed_deduction_s10', total_cities), get_total(max_data, 'sim_cum_confirmed_deduction_s10', total_cities), get_total(data, 'sim_cum_confirmed_deduction_s11', total_cities), get_total(min_data, 'sim_cum_confirmed_deduction_s11', total_cities), get_total(max_data, 'sim_cum_confirmed_deduction_s11', total_cities), get_total(data, 'sim_cum_confirmed_deduction_s12', total_cities), get_total(min_data, 'sim_cum_confirmed_deduction_s12', total_cities), get_total(max_data, 'sim_cum_confirmed_deduction_s12', total_cities), get_total(data, 'real_cum_confirmed', total_cities), 'China\'s mainland', 'img/{}/cum_confirmed_prediction_time.{}'.format('0000', fmt), touchratio=1, ratio_high=1.5, ratio_low=0.5, loc='upper right' ) plot33_time_shade( get_total(data, 'sim_confirmed_deduction_s1', total_cities), get_total(min_data, 'sim_confirmed_deduction_s1', total_cities), get_total(max_data, 'sim_confirmed_deduction_s1', total_cities), get_total(data, 'sim_confirmed_deduction_s9', total_cities), get_total(min_data, 'sim_confirmed_deduction_s9', total_cities), get_total(max_data, 'sim_confirmed_deduction_s9', total_cities), get_total(data, 'sim_confirmed_deduction_s10', total_cities), get_total(min_data, 'sim_confirmed_deduction_s10', total_cities), get_total(max_data, 'sim_confirmed_deduction_s10', total_cities), get_total(data, 'sim_confirmed_deduction_s11', total_cities), get_total(min_data, 'sim_confirmed_deduction_s11', total_cities), get_total(max_data, 'sim_confirmed_deduction_s11', total_cities), get_total(data, 'sim_confirmed_deduction_s12', total_cities), get_total(min_data, 'sim_confirmed_deduction_s12', total_cities), get_total(max_data, 'sim_confirmed_deduction_s12', total_cities), get_total(data, 'real_confirmed', total_cities), 'China\'s mainland', 'img/{}/confirmed_prediction_time.{}'.format('0000', fmt), touchratio=1, ratio_high=1.5, ratio_low=0.5, loc='upper right' ) print('============== parameter') for item in total_cities: print(code_dict[item]) print('RMSE: {} ({}-{})'.format(data['newly_confirmed_loss'][str(item)], min_data['newly_confirmed_loss'][str(item)], max_data['newly_confirmed_loss'][str(item)])) print('RO1: {} ({}-{})'.format(data['R01'][str(item)], min_data['R01'][str(item)], max_data['R01'][str(item)])) print('RO2: {} ({}-{})'.format(data['R02'][str(item)], min_data['R02'][str(item)], max_data['R02'][str(item)])) print('DT1: {} ({}-{})'.format(data['DT1'][str(item)], min_data['DT1'][str(item)], max_data['DT1'][str(item)])) print('DT2: {} ({}-{})'.format(data['DT2'][str(item)], min_data['DT2'][str(item)], max_data['DT2'][str(item)])) print('Death ratio: {} ({}-{})'.format(data['death_rate'][str(item)], min_data['death_rate'][str(item)], max_data['death_rate'][str(item)])) print('============== parameter') print('==================================', str(final_date)) print('Jan 23 different control date: ') print('confirmed') print('middle: ') print('Jan 23', format(int(get_total(data, 'sim_cum_confirmed_deduction_s1', total_cities)[-1]), ','), 'Jan 24', format(int(get_total(data, 'sim_cum_confirmed_deduction_s9', total_cities)[-1]), ','), 'Jan 26', format(int(get_total(data, 'sim_cum_confirmed_deduction_s10', total_cities)[-1]), ','), 'Jan 28', format(int(get_total(data, 'sim_cum_confirmed_deduction_s11', total_cities)[-1]), ','), 'Jan 30', format(int(get_total(data, 'sim_cum_confirmed_deduction_s12', total_cities)[-1]), ','), ) print('min: ') print('Jan 23', format(int(get_total(min_data, 'sim_cum_confirmed_deduction_s1', total_cities)[-1]), ','), 'Jan 24', format(int(get_total(min_data, 'sim_cum_confirmed_deduction_s9', total_cities)[-1]), ','), 'Jan 26', format(int(get_total(min_data, 'sim_cum_confirmed_deduction_s10', total_cities)[-1]), ','), 'Jan 28', format(int(get_total(min_data, 'sim_cum_confirmed_deduction_s11', total_cities)[-1]), ','), 'Jan 30', format(int(get_total(min_data, 'sim_cum_confirmed_deduction_s12', total_cities)[-1]), ','), ) print('max:') print('Jan 23', format(int(get_total(max_data, 'sim_cum_confirmed_deduction_s1', total_cities)[-1]), ','), 'Jan 24', format(int(get_total(max_data, 'sim_cum_confirmed_deduction_s9', total_cities)[-1]), ','), 'Jan 26', format(int(get_total(max_data, 'sim_cum_confirmed_deduction_s10', total_cities)[-1]), ','), 'Jan 28', format(int(get_total(max_data, 'sim_cum_confirmed_deduction_s11', total_cities)[-1]), ','), 'Jan 30', format(int(get_total(max_data, 'sim_cum_confirmed_deduction_s12', total_cities)[-1]), ','), ) print('infected') print('middle: ') print('Jan 23', format(int(get_total(data, 'sim_cum_infection_deduction_s1', total_cities)[-1]), ','), 'Jan 24', format(int(get_total(data, 'sim_cum_infection_deduction_s9', total_cities)[-1]), ','), 'Jan 26', format(int(get_total(data, 'sim_cum_infection_deduction_s10', total_cities)[-1]), ','), 'Jan 28', format(int(get_total(data, 'sim_cum_infection_deduction_s11', total_cities)[-1]), ','), 'Jan 30', format(int(get_total(data, 'sim_cum_infection_deduction_s12', total_cities)[-1]), ','), ) print('min: ') print('Jan 23', format(int(get_total(min_data, 'sim_cum_infection_deduction_s1', total_cities)[-1]), ','), 'Jan 24', format(int(get_total(min_data, 'sim_cum_infection_deduction_s9', total_cities)[-1]), ','), 'Jan 26', format(int(get_total(min_data, 'sim_cum_infection_deduction_s10', total_cities)[-1]), ','), 'Jan 28', format(int(get_total(min_data, 'sim_cum_infection_deduction_s11', total_cities)[-1]), ','), 'Jan 30', format(int(get_total(min_data, 'sim_cum_infection_deduction_s12', total_cities)[-1]), ','), ) print('max:') print('Jan 23', format(int(get_total(max_data, 'sim_cum_infection_deduction_s1', total_cities)[-1]), ','), 'Jan 24', format(int(get_total(max_data, 'sim_cum_infection_deduction_s9', total_cities)[-1]), ','), 'Jan 26', format(int(get_total(max_data, 'sim_cum_infection_deduction_s10', total_cities)[-1]), ','), 'Jan 28', format(int(get_total(max_data, 'sim_cum_infection_deduction_s11', total_cities)[-1]), ','), 'Jan 30', format(int(get_total(max_data, 'sim_cum_infection_deduction_s12', total_cities)[-1]), ','), ) print('==================================', str(final_date)) plot12_shade(get_total(data, 'real_confirmed', total_cities), get_total(data, 'sim_confirmed', total_cities), get_total(min_data, 'sim_confirmed', total_cities), get_total(max_data, 'sim_confirmed', total_cities), get_total(data, 'sim_new_infection', total_cities), get_total(min_data, 'sim_new_infection', total_cities), get_total(max_data, 'sim_new_infection', total_cities), 'The newly number of confirmed cases ', 'img/{}/increase_real_sim_infect.{}'.format('0000', fmt)) plot12_shade(get_total(data, 'real_cum_confirmed', total_cities), get_total(data, 'sim_cum_confirmed', total_cities), get_total(min_data, 'sim_cum_confirmed', total_cities), get_total(max_data, 'sim_cum_confirmed', total_cities), get_total(data, 'sim_cum_infection_deduction_s1', total_cities)[:lreal], get_total(min_data, 'sim_cum_infection_deduction_s1', total_cities)[:lreal],get_total(max_data, 'sim_cum_infection_deduction_s1', total_cities)[:lreal], 'The newly number of confirmed cases ', 'img/{}/cum_real_sim_infect.{}'.format('0000', fmt)) bias = (today - date(2020,3,13)).days - 1 print('==================================', str(cur_date + timedelta(bias))) print('current total infections and confirmed cases') print('confirmed') print('middle: ') print('100\%', format(int(get_total(data, 'sim_cum_confirmed_deduction_s1', total_cities)[lreal-1 + bias]), ',')) print('min: ') print('100\%', format(int(get_total(min_data, 'sim_cum_confirmed_deduction_s1', total_cities)[lreal-1 + bias]), ',')) print('max: ') print('100\%', format(int(get_total(max_data, 'sim_cum_confirmed_deduction_s1', total_cities)[lreal-1+ bias]), ',')) print('real', format(int(get_total(data,'real_cum_confirmed', total_cities)[-1]), ',')) print('infected') print('middle: ') print('100\%', format(int(get_total(data, 'sim_cum_infection_deduction_s1', total_cities)[lreal-1+ bias]), ',')) print('min: ') print('100\%', format(int(get_total(min_data, 'sim_cum_infection_deduction_s1', total_cities)[lreal-1+ bias]), ',')) print('max: ') print('100\%', format(int(get_total(max_data, 'sim_cum_infection_deduction_s1', total_cities)[lreal-1+ bias]), ',')) print('infe-confi: ') print('{} ({}-{})'.format(format(int(data['sim_cum_infe_minus_conf_s1']['0000'][lreal-1+bias]),','), format(int(min_data['sim_cum_infe_minus_conf_s1']['0000'][lreal - 1 + bias]), ','), format(int(max_data['sim_cum_infe_minus_conf_s1']['0000'][lreal - 1 + bias]), ','))) print('ratio') print('middle: ') print('100\%', format(int(get_total(data, 'sim_cum_infection_deduction_s1', total_cities)[lreal - 1+ bias]), ',')) print('min: ') print('100\%', format(int(get_total(min_data, 'sim_cum_infection_deduction_s1', total_cities)[lreal - 1+ bias]), ',')) print('max: ') print('100\%', format(int(get_total(max_data, 'sim_cum_infection_deduction_s1', total_cities)[lreal - 1+ bias]), ',')) print('cum self_cure') print('100\%', format(int(get_total(data, 'sim_cum_self_cured_deduction_s1', total_cities)[lreal - 1+ bias]), ','), '({}-{})'.format(format(int(get_total(min_data,'sim_cum_self_cured_deduction_s1', total_cities)[lreal - 1+ bias]), ','), format(int(get_total(max_data,'sim_cum_self_cured_deduction_s1', total_cities)[lreal - 1+ bias]), ','))) print('total infection') print('100\%', format(int(get_total(data, 'sim_total_infection_deduction_s1', total_cities)[lreal - 1+ bias]), ','), '({}-{})'.format(format(int(get_total(min_data,'sim_total_infection_deduction_s1', total_cities)[lreal - 1+ bias]), ','), format(int(get_total(max_data,'sim_total_infection_deduction_s1', total_cities)[lreal - 1+ bias]), ','))) print('nosymbol') print('100\%', format(int(get_total(data, 'sim_cum_nosymbol_deduction_s1', total_cities)[lreal - 1+ bias]), ','), '({}-{})'.format(format(int(get_total(min_data,'sim_cum_nosymbol_deduction_s1', total_cities)[lreal - 1+ bias]), ','), format(int(get_total(max_data,'sim_cum_nosymbol_deduction_s1', total_cities)[lreal - 1+ bias]), ','))) print('total_iso') # sim_total_isolation_deduction_s1 print('100\%', format(int(get_total(data, 'sim_total_isolation_deduction_s1', total_cities)[lreal - 1+ bias]), ','), '({}-{})'.format(format(int(get_total(min_data,'sim_total_isolation_deduction_s1', total_cities)[lreal - 1+ bias]), ','), format(int(get_total(max_data,'sim_total_isolation_deduction_s1', total_cities)[lreal - 1+ bias]), ','))) print('ratio cur') print('{} ({}-{})'.format(data['current_asym']['0000'], min_data['current_asym']['0000'], max_data['current_asym']['0000'])) print('final') print('{} ({}-{})'.format(data['final_asym']['0000'], min_data['final_asym']['0000'], max_data['final_asym']['0000'])) print('==================================', str(cur_date)) print('\n\n') cur_data_4_03 = json.load(open('./data/cur_confirmed-{}.json'.format(str(today)), 'r')) for item in total_cities: print('{} &'.format(code_dict[item]), sep='', end='') print('{} &'.format(format(int(cur_data_4_03[str(item)]), ',')), sep='',end='') print('{} &'.format(format(int(data['sim_cum_confirmed_deduction_s1'][str(item)][lreal-1+bias]), ',')), sep='',end='') print('{} &'.format(format(int(data['sim_cum_infection_deduction_s1'][str(item)][lreal-1+bias]), ',')), sep='',end='') print('{} &'.format(format(int(data['sim_total_infection_deduction_s1'][str(item)][lreal-1+bias]), ',')), sep='',end='') print('{} \\\\'.format(format(int(data['sim_cum_self_cured_deduction_s1'][str(item)][lreal-1+bias]), ',')), sep='',end='') print('') pass print('\n\n') print('{} &'.format('China\'s Mainland'), sep='', end='') total_it = sum([cur_data_4_03[key] for key in cur_data_4_03.keys()]) print('{} &'.format(format(int(total_it), ',')), sep='', end='') print('{} ({}-{}) &'.format( format(int(get_total(data, 'sim_cum_confirmed_deduction_s1', total_cities)[lreal - 1 + bias]), ','), format(int(get_total(min_data, 'sim_cum_confirmed_deduction_s1', total_cities)[lreal - 1 + bias]), ','), format(int(get_total(max_data, 'sim_cum_confirmed_deduction_s1', total_cities)[lreal - 1 + bias]), ','), ), sep='', end='') print('{} ({}-{}) &'.format( format(int(get_total(data, 'sim_cum_infection_deduction_s1', total_cities)[lreal - 1 + bias]), ','), format(int(get_total(min_data, 'sim_cum_infection_deduction_s1', total_cities)[lreal - 1 + bias]), ','), format(int(get_total(max_data, 'sim_cum_infection_deduction_s1', total_cities)[lreal - 1 + bias]), ','), ), sep='', end='') print('{} ({}-{}) &'.format( format(int(get_total(data, 'sim_total_infection_deduction_s1', total_cities)[lreal - 1 + bias]), ','), format(int(get_total(min_data, 'sim_total_infection_deduction_s1', total_cities)[lreal - 1 + bias]), ','), format(int(get_total(max_data, 'sim_total_infection_deduction_s1', total_cities)[lreal - 1 + bias]), ','), ), sep='', end='') print('{} ({}-{}) \\\\'.format( format(int(get_total(data, 'sim_cum_self_cured_deduction_s1', total_cities)[lreal - 1 + bias]), ','), format(int(get_total(min_data, 'sim_cum_self_cured_deduction_s1', total_cities)[lreal - 1 + bias]), ','), format(int(get_total(max_data, 'sim_cum_self_cured_deduction_s1', total_cities)[lreal - 1 + bias]), ','), ), sep='', end='') print('') for item in total_cities: print('{} &'.format(code_dict[item]), sep='', end='') print('{} &'.format(format(int(cur_data_4_03[str(item)]), ',')), sep='',end='') print('{} ({}-{}) &'.format( format(int(data['sim_cum_confirmed_deduction_s1'][str(item)][lreal-1+bias]), ','), format(int(min_data['sim_cum_confirmed_deduction_s1'][str(item)][lreal - 1 + bias]), ','), format(int(max_data['sim_cum_confirmed_deduction_s1'][str(item)][lreal - 1 + bias]), ','), ), sep='',end='') print('{} ({}-{}) &'.format( format(int(data['sim_cum_infection_deduction_s1'][str(item)][lreal-1+bias]), ','), format(int(min_data['sim_cum_infection_deduction_s1'][str(item)][lreal - 1 + bias]), ','), format(int(max_data['sim_cum_infection_deduction_s1'][str(item)][lreal - 1 + bias]), ','), ), sep='',end='') print('{} ({}-{}) &'.format( format(int(data['sim_total_infection_deduction_s1'][str(item)][lreal-1+bias]), ','), format(int(min_data['sim_total_infection_deduction_s1'][str(item)][lreal - 1 + bias]), ','), format(int(max_data['sim_total_infection_deduction_s1'][str(item)][lreal - 1 + bias]), ','), ), sep='',end='') print('{} ({}-{}) \\\\'.format( format(int(data['sim_cum_self_cured_deduction_s1'][str(item)][lreal-1+bias]), ','), format(int(min_data['sim_cum_self_cured_deduction_s1'][str(item)][lreal - 1 + bias]), ','), format(int(max_data['sim_cum_self_cured_deduction_s1'][str(item)][lreal - 1 + bias]), ','), ), sep='',end='') print('') # print variables x_list = construct_x(data, total_cities) min_x_list = construct_x(min_data, total_cities) max_x_list = construct_x(max_data, total_cities) format_out(x_list, min_x_list, max_x_list) exit(0)
def run_opt(city, budget, start_date, important_dates, infectratio_range=None, dummy_range=None, unob_flow_num=None, repeat_time=1, init_samples=None, training_date_end=None, json_name='data_run.json', seed=1, loss_ord=0.0, unob_period=None, obs_period=None, iso_period=None, cure_period=None, isoratio_it=None): if city == 420000: infectratio_range = [0., 0.05] dummy_range = [0.0000, 400.00001] else: assert infectratio_range is not None and dummy_range is not None days_predict = 0 # load data real_data = pd.read_csv(get_data_path()) history_real = prepareData(real_data) flow_out_data = flowOutData() # initialize models infectratio = InfectRatio(1, [infectratio_range], [True]) touchratio = TouchRatio(1, [[0.0, 0.6]], [True]) touchratiointra = TouchRatio(1, [[0, 1]], [True]) obs = ObservationRatio(1, [[0.0, 0.3]], [True]) dead = DeadRatio(1, [[0., 0.01]], [True]) if isoratio_it is None: isoratio = IsolationRatio(1, [[0.2, 0.5]], [True]) else: isoratio = IsolationRatio(1, [isoratio_it], [True]) dummy = DummyModel(1, [dummy_range], [True, True]) cure_ratio = InfectRatio(1, [[0., 0.1]], [True]) # set the time of applying touchratio simulator = Simulator(city, infectratio, touchratio, obs, dead, dummy, isoratio, touchratiointra, cure_ratio, important_dates, unob_flow_num=unob_flow_num, flow_out_data=flow_out_data, training_date_end=training_date_end) # set period here simulator.set_period() test_date = datetime.strptime(history_real['date'].max(), '%Y-%m-%d').date() - timedelta(days_predict) history_real = history_real[history_real['adcode'] == city] history_real = history_real[history_real['date'] >= str(start_date)] history_train = history_real[history_real['date'] <= str(test_date)] x, y = simulator.fit(history_train, budget=budget, server_num=get_core_num(), repeat=repeat_time, seed=seed, intermediate_freq=10000, init_samples=init_samples, loss_ord=loss_ord) print('best_solution: x = ', x, 'y = ', y) simulator.set_param(x) run_simulation(x, city, 60, 60, start_date, get_important_date(city), unob_flow_num=unob_flow_num, json_name=json_name) duration = len(real_data["date"].unique()) - 1 sim_res, _ = simulator.simulate(str(start_date), duration) print('RMSE: ', get_newly_loss(sim_res, history_real)) return x, sim_res
def initHubei(x, start_date, important_date, travel_from_hubei): if travel_from_hubei is None: return None days_predict = 0 # load data real_data = pd.read_csv(get_data_path()) history_real = prepareData(real_data) infectratio = InfectRatio(1, [[0, 1]], [True]) touchratio = TouchRatio(1, [[0., 0.3]], [True]) touchratiointra = TouchRatio(1, [[0, 10]], [True]) obs = ObservationRatio(1, [[0.0, 1.]], [True]) dead = DeadRatio(1, [[0., 0.1]], [True]) isoratio = IsolationRatio(1, [[0., 1]], [True]) cure_ratio = InfectRatio(1, [[0., 100]], [True]) dummy = DummyModel(1, [[0, 200000]], [True, True]) flow_out_data = flowOutData() test_date = datetime.strptime(history_real['date'].max(), '%Y-%m-%d').date() - timedelta(days_predict) history_real = history_real[history_real['adcode'] == 420000] history_real = history_real[history_real['date'] >= str(start_date)] history_train = history_real[history_real['date'] <= str(test_date)] duration = len(history_train["date"].unique()) city = 420000 simulator = Simulator(city, infectratio, touchratio, obs, dead, dummy, isoratio, touchratiointra, cure_ratio, important_date, flow_out_data=flow_out_data) simulator.set_param(x) total_population = get_populations()[420000] simulated_result, detailed_result = simulator.simulate( str(start_date), duration + 60) init_unob = [item for item in reversed(detailed_result.unob_total_init)] unob_ratio = {} for i, item in enumerate(init_unob): date_now = start_date - timedelta(i + 1) unob_ratio[date_now] = item / total_population for it_date, unob_num in zip(simulated_result['date'], simulated_result['total_unobserved']): unob_ratio[datetime.strptime( it_date, '%Y-%m-%d').date()] = unob_num / total_population unob_flow_num = {} lst_travel = {} lst_date = None for it_date in travel_from_hubei: if it_date not in unob_ratio: continue if it_date not in unob_flow_num: unob_flow_num[it_date] = {} for it_code in travel_from_hubei[it_date]: unob_flow_num[it_date][it_code] = travel_from_hubei[it_date][ it_code] * unob_ratio[it_date] lst_travel = travel_from_hubei[it_date] lst_date = it_date lst_date = lst_date + timedelta(1) while True: if lst_date in unob_ratio: if lst_date not in unob_flow_num: unob_flow_num[lst_date] = {} for it_code in lst_travel: unob_flow_num[lst_date][ it_code] = lst_travel[it_code] * unob_ratio[lst_date] else: break lst_date = lst_date + timedelta(1) return unob_flow_num
while True: if lst_date in unob_ratio: if lst_date not in unob_flow_num: unob_flow_num[lst_date] = {} for it_code in lst_travel: unob_flow_num[lst_date][ it_code] = lst_travel[it_code] * unob_ratio[lst_date] else: break lst_date = lst_date + timedelta(1) return unob_flow_num if __name__ == '__main__': province_travel_dict = flowHubei() real_data = pd.read_csv(get_data_path())['adcode'].unique() province_code_dict = codeDict() training_end_date = None x_buffer = [] rerun_list = None if rerun_list is not None: old_param_buffer = [ pd.read_csv('params{}.csv'.format(int(i))) for i in range(get_seed_num()) ] for i in range(get_seed_num()): global_seed(i) all_param = {} if rerun_list is None or 420000 in rerun_list: x = run_opt(420000,
loss_ord=loss_ord) print('best_solution: x = ', x, 'y = ', y) simulator.set_param(x) run_simulation(x, city, 60, 60, start_date, get_important_date(city), unob_flow_num=unob_flow_num, json_name=json_name) return x if __name__ == '__main__': real_data = list(pd.read_csv(get_data_path(True))['adcode'].unique()) province_code_dict = it_code_dict() rerun_list = None old_param_buffer = None training_end_date = None if rerun_list is not None: old_param_buffer = [ pd.read_csv('params_foreign{}.csv'.format(int(i))) for i in range(get_seed_num()) ] for i in range(get_seed_num()): all_param = {} for ind, item in enumerate(real_data): print(i, ind, item, province_code_dict[item]) if rerun_list is None or item in rerun_list: touch_range = [0, 0.33333]