Пример #1
0
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')
    """
Пример #2
0
def train_korea():
    province_code_dict = {900003: 'South Korea'}
    for i in range(1):
        all_param = {}
        for ind, item in enumerate([900003]):
            print(i, ind, item, province_code_dict[item])
            x = run_opt_foreign(item,
                                40000,
                                start_date=date(2020, 1, 24),
                                important_dates=[get_important_date(900003)],
                                infectratio_range=[0.0, 0.05],
                                dummy_range=[0, 100],
                                unob_flow_num=None,
                                repeat_time=3,
                                training_end_date=training_end_date,
                                seed=i,
                                json_name='data_run_lstm_korea{}.json'.format(
                                    int(i)),
                                touch_range=[0, 0.333])
            run_simulation_foreign(
                x,
                item,
                60,
                60,
                start_date,
                get_important_date(item),
                json_name='data_run_lstm_korea{}.json'.format(int(i)))
            all_param[item] = x
        all_param_df = pd.DataFrame(all_param)
        all_param_df.to_csv('params_lstm_korea{}.csv'.format(int(i)),
                            index=False)
    load_and_save('data_run_lstm_korea{}.json', 'data_run_lstm_korea_{}.json',
                  1)
Пример #3
0
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)
Пример #4
0
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 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')
    """
Пример #6
0
def main():
    training_end_date = None
    for i in range(30):
        iso_ratio_it = [0.1, 0.5]
        if i >= 10:
            iso_ratio_it = [0.2, 0.5]
        if i >= 20:
            iso_ratio_it = [0.3, 0.5]
        x = run_opt(420000,
                    200000,
                    start_date=date(2020, 1, 11),
                    important_dates=[get_important_date(420000)],
                    repeat_time=1,
                    training_date_end=training_end_date,
                    isoratio_it=iso_ratio_it,
                    seed=1,
                    json_name='data_run_no_healed{}.json'.format(int(i)))

        clear_child_process()
Пример #7
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_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
Пример #8
0
def run_simulation2(x,
                    city,
                    simulate_day1,
                    simulate_day2,
                    start_date,
                    simulate_date,
                    history_real,
                    incubation=3,
                    unob_flow_num=None,
                    json_name='data_run.json'):
    days_predict = 0

    infectratio = InfectRatio(1, [[0, 1]], [True])
    touchratio = TouchRatio(1, [[0, 1.]], [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])
    dummy = DummyModel(1, [[0, 2000000]], [True, True])
    cure_ratio = InfectRatio(1, [[0., 100]], [True])

    flow_out_data = None
    important_dates = [get_important_date(city)]
    json_path = json_name
    end_date = datetime.strptime(history_real['date'].max(), '%Y-%m-%d').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)]
    duration = len(history_train["date"].unique()) - 1

    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,
                          incubation=incubation)
    if not os.path.exists('./img/{}'.format(city)):
        os.makedirs('./img/{}'.format(city))
    if not os.path.exists('./result'.format(city)):
        os.makedirs('./result'.format(city))

    with open('./result/result{}.txt'.format(city), 'w') as f:
        print('infection_ratio: [0, 1], touch_ratio_in_province: [0, 0.5]',
              file=f)
        print('touch_ratio_between_province: [0, 1]', file=f)
        print('observe_ratio_each_day: [0.05613, 1]', file=f)
        print('dead_ratio_observe: [0, 0.1], dead_ratio_unob: [0, 0.1]',
              file=f)
        print('initial_infection_hubei: [8, 8.01].', file=f)
        print('isolation_ratio: [0, 0.1]', file=f)
        print('BEST RESUTL:', file=f)
        print('x = ', x, file=f)
        simulator.set_param(x)
        print('##################################', file=f)
        print('HISTORY REAL', file=f)
        print('Data form ' + str(start_date) + ' to ' + str(end_date) +
              'are used for training',
              file=f)
        print(history_train[history_train['adcode'] == city], file=f)
        print('##################################', file=f)
        print('SIMULATED RESULTS', file=f)
        print('start_date: ', start_date, 'duration: ', duration, file=f)
        sim, records = simulator.simulate(str(start_date), duration)
        append_to_json(json_path, 'x', str(city), list(x))
        append_to_json(json_path, 'R01', str(city), get_R0(15, x[0], x[5], 14))
        append_to_json(json_path, 'R02', str(city),
                       get_R0(15 * x[1], x[0], x[5], 14))
        append_to_json(json_path, 'DT1', str(city), get_DT(15, x[0], x[5]))
        append_to_json(json_path, 'DT2', str(city),
                       get_DT(15 * x[1], x[0], x[5]))
        append_to_json(json_path, 'death_rate', str(city),
                       get_death_rate(x[3], x[7], 14, 10))

        append_to_json(json_path, 'real_confirmed', str(city),
                       list(history_train['observed']))
        append_to_json(json_path, 'real_cum_confirmed', str(city),
                       list(history_train['cum_confirmed']))
        append_to_json(json_path, 'sim_confirmed', str(city),
                       list(sim['observed']))
        append_to_json(json_path, 'sim_cum_confirmed', str(city),
                       list(sim['cum_confirmed']))
        append_to_json(json_path, 'loss', str(city),
                       get_loss(sim, history_train))
        append_to_json(json_path, 'newly_confirmed_loss', str(city),
                       get_newly_loss(sim, history_train))
        append_to_json(json_path, 'real_cum_dead', str(city),
                       list(history_train['cum_dead']))
        append_to_json(json_path, 'sim_new_infection', str(city),
                       list(sim['new_infection']))

        append_to_json(json_path, 'sim_total_infection', str(city),
                       list(sim['total_infection']))
        append_to_json(json_path, 'sim_cum_self_cured', str(city),
                       list(sim['cum_cured']))
        append_to_json(json_path, 'sim_cum_nosymbol', str(city),
                       list(sim['cum_no_symbol']))

        print(f'loss: {get_loss(sim, history_train)}', file=f)
        cum_real = history_train['cum_confirmed'].values
        simulated_data, _ = simulator.simulate(str(start_date), duration)
        cum_simulated = simulated_data['cum_confirmed'].values
        print(simulated_data[simulated_data['adcode'] == city], file=f)

        s1, _ = simulator.simulate(str(start_date),
                                   duration + simulate_day1 + 30)
        touch_ratio_high = 0.35 * x[1]
        touch_ratio_very_high = 0.6 * x[1]
        touch_ratio_low = 0.1 * x[1]
        s2, _ = simulator.simulate(str(start_date),
                                   duration + simulate_day2 + 30,
                                   simulate_date=simulate_date,
                                   simulate_touchratio=touch_ratio_high)
        s3, _ = simulator.simulate(str(start_date),
                                   duration + simulate_day2 + 30,
                                   simulate_date=simulate_date,
                                   simulate_touchratio=touch_ratio_low)
        s8, _ = simulator.simulate(str(start_date),
                                   duration + simulate_day2 + 30,
                                   simulate_date=simulate_date,
                                   simulate_touchratio=touch_ratio_very_high)

        print(len(s1['cum_confirmed']), len(s3['cum_confirmed']),
              str(start_date + timedelta(len(s3['cum_confirmed']))))
        s4, _ = simulator.simulate(str(start_date),
                                   duration + simulate_day2 + 30,
                                   simulate_date=get_important_date(city) +
                                   timedelta(15),
                                   simulate_touchratio=touch_ratio_low)
        s5, _ = simulator.simulate(str(start_date),
                                   duration + simulate_day2 + 30,
                                   simulate_date=get_important_date(city) +
                                   timedelta(15),
                                   simulate_touchratio=touch_ratio_high)
        s6, _ = simulator.simulate(str(start_date),
                                   duration + simulate_day2 + 30,
                                   simulate_date=get_important_date(city) +
                                   timedelta(5),
                                   simulate_touchratio=touch_ratio_low)
        s7, _ = simulator.simulate(str(start_date),
                                   duration + simulate_day2 + 30,
                                   simulate_date=get_important_date(city) +
                                   timedelta(5),
                                   simulate_touchratio=touch_ratio_high)
        c_con = list(
            s1['cum_confirmed'])[len(list(history_train['observed'])) - 1]
        c_inf = list(
            s1['cum_infection'])[len(list(history_train['observed'])) - 1]
        append_to_json(json_path, 'current_asym', str(city), c_con / c_inf)
        c_con = list(s1['cum_confirmed'])[-1]
        c_inf = list(s1['cum_infection'])[-1]
        append_to_json(json_path, 'final_asym', str(city), c_con / c_inf)
        append_to_json(json_path, 'sim_cum_confirmed_deduction_s1', str(city),
                       list(s1['cum_confirmed']))
        append_to_json(json_path, 'sim_cum_confirmed_deduction_s2', str(city),
                       list(s2['cum_confirmed']))
        append_to_json(json_path, 'sim_cum_confirmed_deduction_s3', str(city),
                       list(s3['cum_confirmed']))
        append_to_json(json_path, 'sim_cum_confirmed_deduction_s4', str(city),
                       list(s4['cum_confirmed']))
        append_to_json(json_path, 'sim_cum_confirmed_deduction_s5', str(city),
                       list(s5['cum_confirmed']))
        append_to_json(json_path, 'sim_cum_confirmed_deduction_s6', str(city),
                       list(s6['cum_confirmed']))
        append_to_json(json_path, 'sim_cum_confirmed_deduction_s7', str(city),
                       list(s7['cum_confirmed']))
        append_to_json(json_path, 'sim_cum_confirmed_deduction_s8', str(city),
                       list(s7['cum_confirmed']))
        append_to_json(json_path, 'sim_confirmed_deduction_s1', str(city),
                       list(s1['observed']))
        append_to_json(json_path, 'sim_confirmed_deduction_s2', str(city),
                       list(s2['observed']))
        append_to_json(json_path, 'sim_confirmed_deduction_s3', str(city),
                       list(s3['observed']))
        append_to_json(json_path, 'sim_confirmed_deduction_s4', str(city),
                       list(s4['observed']))
        append_to_json(json_path, 'sim_confirmed_deduction_s5', str(city),
                       list(s5['observed']))
        append_to_json(json_path, 'sim_confirmed_deduction_s6', str(city),
                       list(s6['observed']))
        append_to_json(json_path, 'sim_confirmed_deduction_s7', str(city),
                       list(s7['observed']))
        append_to_json(json_path, 'sim_confirmed_deduction_s8', str(city),
                       list(s7['observed']))
        append_to_json(json_path, 'sim_infection_deduction_s1', str(city),
                       list(s1['total_infection']))
        append_to_json(json_path, 'sim_infection_deduction_s2', str(city),
                       list(s2['total_infection']))
        append_to_json(json_path, 'sim_infection_deduction_s3', str(city),
                       list(s3['total_infection']))
        append_to_json(json_path, 'sim_infection_deduction_s4', str(city),
                       list(s4['total_infection']))
        append_to_json(json_path, 'sim_infection_deduction_s5', str(city),
                       list(s5['total_infection']))
        append_to_json(json_path, 'sim_infection_deduction_s6', str(city),
                       list(s6['total_infection']))
        append_to_json(json_path, 'sim_infection_deduction_s7', str(city),
                       list(s7['total_infection']))
        append_to_json(json_path, 'sim_infection_deduction_s8', str(city),
                       list(s7['total_infection']))
        append_to_json(json_path, 'sim_cum_infection_deduction_s1', str(city),
                       list(s1['cum_infection']))
        append_to_json(json_path, 'sim_cum_infection_deduction_s2', str(city),
                       list(s2['cum_infection']))
        append_to_json(json_path, 'sim_cum_infection_deduction_s3', str(city),
                       list(s3['cum_infection']))
        append_to_json(json_path, 'sim_cum_infection_deduction_s4', str(city),
                       list(s4['cum_infection']))
        append_to_json(json_path, 'sim_cum_infection_deduction_s5', str(city),
                       list(s5['cum_infection']))
        append_to_json(json_path, 'sim_cum_infection_deduction_s6', str(city),
                       list(s6['cum_infection']))
        append_to_json(json_path, 'sim_cum_infection_deduction_s7', str(city),
                       list(s7['cum_infection']))
        append_to_json(json_path, 'sim_cum_infection_deduction_s8', str(city),
                       list(s7['cum_infection']))
        append_to_json(json_path, 'touch_ratio_low', str(city),
                       touch_ratio_low)
        append_to_json(json_path, 'touch_ratio_hight', str(city),
                       touch_ratio_high)
        append_to_json(json_path, 'sim_cum_dead_s1', str(city),
                       list(s1['cum_dead']))
        append_to_json(json_path, 'sim_cum_dead_s2', str(city),
                       list(s2['cum_dead']))
        append_to_json(json_path, 'sim_cum_dead_s3', str(city),
                       list(s3['cum_dead']))
        append_to_json(json_path, 'sim_cum_dead_s4', str(city),
                       list(s4['cum_dead']))
        append_to_json(json_path, 'sim_cum_dead_s5', str(city),
                       list(s5['cum_dead']))
        append_to_json(json_path, 'sim_cum_dead_s6', str(city),
                       list(s6['cum_dead']))
        append_to_json(json_path, 'sim_cum_dead_s7', str(city),
                       list(s7['cum_dead']))
        append_to_json(json_path, 'sim_cum_dead_s8', str(city),
                       list(s7['cum_dead']))

        append_to_json(json_path, 'sim_total_infection_deduction_s1',
                       str(city), list(s1['total_unobserved']))
        append_to_json(json_path, 'sim_total_infection_deduction_s2',
                       str(city), list(s2['total_unobserved']))
        append_to_json(json_path, 'sim_total_infection_deduction_s3',
                       str(city), list(s3['total_unobserved']))
        append_to_json(json_path, 'sim_total_infection_deduction_s4',
                       str(city), list(s4['total_unobserved']))
        append_to_json(json_path, 'sim_total_infection_deduction_s5',
                       str(city), list(s5['total_unobserved']))
        append_to_json(json_path, 'sim_total_infection_deduction_s6',
                       str(city), list(s6['total_unobserved']))
        append_to_json(json_path, 'sim_total_infection_deduction_s7',
                       str(city), list(s7['total_unobserved']))
        append_to_json(json_path, 'sim_total_infection_deduction_s8',
                       str(city), list(s8['total_unobserved']))
        append_to_json(json_path, 'sim_cum_self_cured_deduction_s1', str(city),
                       list(s1['cum_self_cured']))
        append_to_json(json_path, 'sim_cum_self_cured_deduction_s2', str(city),
                       list(s2['cum_self_cured']))
        append_to_json(json_path, 'sim_cum_self_cured_deduction_s3', str(city),
                       list(s3['cum_self_cured']))
        append_to_json(json_path, 'sim_cum_self_cured_deduction_s4', str(city),
                       list(s4['cum_self_cured']))
        append_to_json(json_path, 'sim_cum_self_cured_deduction_s5', str(city),
                       list(s5['cum_self_cured']))
        append_to_json(json_path, 'sim_cum_self_cured_deduction_s6', str(city),
                       list(s6['cum_self_cured']))
        append_to_json(json_path, 'sim_cum_self_cured_deduction_s7', str(city),
                       list(s7['cum_self_cured']))
        append_to_json(json_path, 'sim_cum_self_cured_deduction_s8', str(city),
                       list(s8['cum_self_cured']))

        append_to_json(json_path, 'sim_cum_nosymbol_deduction_s1', str(city),
                       list(s1['cum_no_symbol']))
        append_to_json(json_path, 'sim_cum_nosymbol_deduction_s2', str(city),
                       list(s2['cum_no_symbol']))
        append_to_json(json_path, 'sim_cum_nosymbol_deduction_s3', str(city),
                       list(s3['cum_no_symbol']))
        append_to_json(json_path, 'sim_cum_nosymbol_deduction_s4', str(city),
                       list(s4['cum_no_symbol']))
        append_to_json(json_path, 'sim_cum_nosymbol_deduction_s5', str(city),
                       list(s5['cum_no_symbol']))
        append_to_json(json_path, 'sim_cum_nosymbol_deduction_s6', str(city),
                       list(s6['cum_no_symbol']))
        append_to_json(json_path, 'sim_cum_nosymbol_deduction_s7', str(city),
                       list(s7['cum_no_symbol']))
        append_to_json(json_path, 'sim_cum_nosymbol_deduction_s8', str(city),
                       list(s8['cum_no_symbol']))
        append_to_json(json_path, 'sim_total_isolation_deduction_s1',
                       str(city), list(s1['total_isolation']))
        append_to_json(json_path, 'sim_total_isolation_deduction_s2',
                       str(city), list(s2['total_isolation']))
        append_to_json(json_path, 'sim_total_isolation_deduction_s3',
                       str(city), list(s3['total_isolation']))
        append_to_json(json_path, 'sim_total_isolation_deduction_s4',
                       str(city), list(s4['total_isolation']))
        append_to_json(json_path, 'sim_total_isolation_deduction_s5',
                       str(city), list(s5['total_isolation']))
        append_to_json(json_path, 'sim_total_isolation_deduction_s6',
                       str(city), list(s6['total_isolation']))
        append_to_json(json_path, 'sim_total_isolation_deduction_s7',
                       str(city), list(s7['total_isolation']))
        append_to_json(json_path, 'sim_total_isolation_deduction_s8',
                       str(city), list(s8['total_isolation']))
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)
Пример #10
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
Пример #11
0
    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,
                        200000,
                        start_date=date(2020, 1, 11),
                        important_dates=[get_important_date(420000)],
                        repeat_time=1,
                        training_date_end=training_end_date,
                        seed=i,
                        json_name='data_run{}.json'.format(int(i)))
            clear_child_process()
            all_param[420000] = x
            x_buffer.append(x)
        else:
            x = list(old_param_buffer[i][str(420000)])
            x_buffer.append(x)
            all_param[420000] = x

        unob_flow_num = initHubei(x_buffer[i],
                                  start_date=date(2020, 1, 11),
                                  important_date=[get_important_date(420000)],
Пример #12
0
 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]
             iso_range = None
             x = run_opt(item,
                         80000,
                         start_date=date(2020, 1, 24),
                         important_dates=[get_important_date(item)],
                         infectratio_range=[0.0, 0.05],
                         dummy_range=[0, 400],
                         unob_flow_num=None,
                         repeat_time=1,
                         seed=i,
                         json_name='data_run_foreign{}.json'.format(int(i)),
                         loss_ord=1.5,
                         touch_range=touch_range,
                         iso_range=iso_range,
                         training_end_date=training_end_date)
             all_param[item] = x
             clear_child_process()
         else:
             all_param[item] = list(old_param_buffer[i][str(item)])
     all_param_df = pd.DataFrame(all_param)
Пример #13
0
    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]
                iso_range = None
                x = run_opt(item, 80000, start_date=date(2020, 2, 4), important_dates=[get_important_date(item)],
                            infectratio_range=[0.0, 0.05], dummy_range=[0, 400], unob_flow_num=None, repeat_time=1,
                            seed=i, json_name='data_run_foreign{}.json'.format(int(i)), loss_ord=1.5, touch_range=touch_range,
                            iso_range=iso_range, training_end_date=training_end_date)
                all_param[item] = x
                clear_child_process()
            else:
                all_param[item] = list(old_param_buffer[i][str(item)])
        all_param_df = pd.DataFrame(all_param)
        all_param_df.to_csv('params_foreign{}.csv'.format(int(i)), index=False)