def run_model(data):
    cidade = data["ibgeID"].iloc[0]
    data_fil = data[columns]
    data_fil = data_fil[data_fil['totalCases'] > 0]
    data_fil = data_fil.sort_values(by='DayNum')
    data_fil = filter_inconsistent_data(data_fil)
    if (data_fil['totalCases'].max() > min_casos) and (len(data_fil) > min_dias) and (cidade > 1e6):
        Ne = pops[pops['ibgeID'] == cidade][' POPULAÇÃO ESTIMADA '].to_list()[0]
        if len(data_fil) >= 30:
            nbetas = 3
        else:
            nbetas = 2
        try:
            model = SIR_BETAS(Ne, nproc)
            model.fit_lsquares(data_fil['totalCases'].to_numpy(), data_fil['DayNum'].to_numpy(),
                               nbetas=nbetas, stand_error=True, nrand=nrand)
            temp = create_summary(model, nbetas, cidade)
            parameters = outp_par.append(temp, ignore_index = True)
            results_df = pd.concat([outp_data,create_output(model, data_fil, pred_days,\
                                cidade, Ne)], sort=False)
            return(results_df)
        except:
            new_temp = pd.DataFrame({"city": data_fill["city"].loc[0], "ibgeID": cidade})
            global not_computed
            not_computed.append(new_temp)
def run_model(data):
    estado = data["state"].iloc[0]
    Ne = pops[estado]
    model = SIR_BETAS(Ne, nproc)
    model.fit_lsquares(data['totalCases'].to_numpy(),
                       data['DayNum'].to_numpy(),
                       nbetas=nbetas,
                       stand_error=True,
                       nrand=nrand)

    temp = create_summary(model, nbetas, estado)
    parameters = outp_par.append(temp, ignore_index=True)
    results_df = pd.concat([outp_data,create_output(model, data, pred_days,\
                                                   estado, Ne)], sort=False)

    tts, Y = model.predict(coefs='LS')
    return (results_df)
import matplotlib.pyplot as plt

#import PA data from Painel Covida (26/06/20)
data = pd.read_csv('chart.csv', sep=';', decimal=',')
dday = 'DateTime'
data[dday] = pd.to_datetime(data[dday], yearfirst=True)
data['DayNum'] = data[dday].dt.dayofyear
#Remove the predict only days
data = data.dropna()
nbeta_max = 4
#PARA population
N = 8.074e6
models = list()
for nbetas in range(1, nbeta_max + 1):
    print(nbetas)
    model = SIR_BETAS(N, 16)
    model.fit_lsquares(data["PA"].to_numpy(),
                       t=data['DayNum'].to_numpy(),
                       nbetas=nbetas,
                       stand_error=True,
                       nrand=16)
    model.fit_ML(data["PA"].to_numpy(),
                 t=data['DayNum'].to_numpy(),
                 nbetas=nbetas,
                 init=model.pos_ls)

    #model.fit(data["PA"].to_numpy(), t=data['DayNum'].to_numpy(), nbetas=2)
    models.append(model)
#tt, Yo = model.predict()

#%%
    'totalCasesPred': [],
    'residuo_quadratico': [],
    'res_quad_padronizado': [],
    'suscetivel': [],
    'infectado': [],
    'recuperado': []
})

#%%
#the main loop
for i, estado in enumerate(estados):
    print(estado)
    Ne = pops[estado]
    data_fil = data[data['state'] == estado][columns]
    data_fil = data_fil.sort_values(by='DayNum')
    model = SIR_BETAS(Ne, nproc)
    model.fit_lsquares(data_fil['totalCases'].to_numpy(),
                       data_fil['DayNum'].to_numpy(),
                       nbetas=nbetas,
                       stand_error=True,
                       nrand=nrand)
    temp = create_summary(model, nbetas, estado)
    outp_par = outp_par.append(temp, ignore_index=True)
    outp_data = pd.concat([outp_data,create_output(model, data_fil, pred_days,\
                                                   estado)], sort=False)
    tts, Y = model.predict(coefs='LS')
    if gen_plots:
        if i % 12 == 0:
            plt.figure()
        plt.subplot(3, 4, (i % 12) + 1)
        plt.plot(model.t, model.Y, label='Data')