Exemple #1
0
def get_data(country = "USA", level = 3, start = date(2020,1,1), pr_capita = 100000):

    #Get data function
    x, src = covid19(country, level = level, start = start, verbose = False)
    ##clean up
    #Select columns:
    x = x[[
        'date',
        'id',
        'confirmed',
        'deaths',
        'population',
        'administrative_area_level_1',
        'administrative_area_level_2',
        'administrative_area_level_3']]

    #Add new infected columns and remove (very few NAs - one in the start of each state)
    x["new_infected"] = x.groupby(["administrative_area_level_2"])["confirmed"].diff()
    #Remove NAs
    x = x[x["new_infected"].notna()]
    #Remove peeked values with 3day rolling window 
    x['smoothed_new_infected'] = x.groupby('administrative_area_level_2')['new_infected'].rolling(3).mean().reset_index(0,drop=True)
    #Normalize new infected per capita within state
    x["new_infected_pr_capita"] = (x["smoothed_new_infected"]/x["population"])*pr_capita

    return x
 def _covid19(self, *args, **kw):
     x, src = covid19dh.covid19(*args, **kw, vintage=True,
                                verbose=False)  # fetch
     # test
     self.assertIsInstance(x, pd.DataFrame)
     for col in [
             "id", "date", "tests", "confirmed", "recovered", "deaths",
             "hosp", "vent", "icu"
     ]:
         self.assertIn(col, x.columns)
     for col in ["population", "latitude", "longitude"]:
         self.assertIn(col, x.columns)
     for col in [
             "school_closing", "workplace_closing", "cancel_events",
             "gatherings_restrictions", "transport_closing",
             "testing_policy", "stay_home_restrictions",
             "internal_movement_restrictions",
             "international_movement_restrictions", "information_campaigns",
             "contact_tracing", "stringency_index", "key",
             "key_apple_mobility", "key_google_mobility"
     ]:
         self.assertIn(col, x.columns)
     for col in [
             "iso_alpha_3", "iso_alpha_2", "iso_numeric", "currency",
             "administrative_area_level", "administrative_area_level_1",
             "administrative_area_level_2", "administrative_area_level_3"
     ]:
         self.assertIn(col, x.columns)
     return x, src
 def _covid19(self, *args, **kw):
     x,src = covid19dh.covid19(*args, **kw, verbose = False) # fetch
     # test
     self.assertIsInstance(x, pd.DataFrame)
     cols = set(self._numeric_attributes) | set(self._constant_attributes)
     cols |= set(self._sourceless_attributes) | set(self._indicator_attributes)
     for col in cols:
         self.assertIn(col, x.columns)
     return x,src
Exemple #4
0
def data_item(item_id: str, q: str = None):
    jsondataset = covid19(item_id, verbose=False)
    data = {}
    datapoints = json.loads(jsondataset.to_json())
    data['authors'] = str(covid19datahub_authors)
    data['goal'] = str(covid19datahub_goal)
    data['data'] = datapoints
    data['citations'] = cite(jsondataset)
    #return json.dumps(data, sort_keys=True, indent=4)
    return data
Exemple #5
0
    def _covid19dh_retrieve(self):
        """
        Retrieve dataset and citation list from COVID-19 Data Hub.
        Citation list will be saved to self.

        Returns:
            (tuple):
                (pandas.DataFrame): dataset at country level
                (pandas.DataFrame): dataset at province level
        """
        c_df = covid19dh.covid19(country=None, level=1, verbose=False)
        c_citations = covid19dh.cite(c_df)
        # For some countries, province-level data is included
        p_df = covid19dh.covid19(country=None, level=2, verbose=False)
        p_citations = covid19dh.cite(p_df)
        # Citation
        citations = list(dict.fromkeys(c_citations + p_citations))
        self._covid19dh_citation = "\n".join(citations)
        return (c_df, p_df)
    def _covid19dh_retrieve(self):
        """
        Retrieve dataset and citation list from COVID-19 Data Hub.
        Citation list will be saved to self.

        Returns:
            (tuple):
                (pandas.DataFrame): dataset at country level
                (pandas.DataFrame): dataset at province level

        Notes:
            For some countries, province-level data is included.
        """
        warnings.simplefilter("ignore", ResourceWarning)
        c_res = covid19dh.covid19(country=None,
                                  level=1,
                                  verbose=False,
                                  raw=False)
        p_res = covid19dh.covid19(country=None,
                                  level=2,
                                  verbose=False,
                                  raw=False)
        try:
            c_df, c_cite = c_res
            p_df, p_cite = p_res
        except ValueError:
            # covid19dh <= 1.14
            c_df, c_cite = c_res.copy(), covid19dh.cite(c_res)
            p_df, p_cite = p_res.copy(), covid19dh.cite(p_res)
            citations = list(dict.fromkeys(c_cite + p_cite))
            self._covid19dh_citation = "\n".join(citations)
            return (c_df, p_df)
        # Citation
        cite = pd.concat([c_cite, p_cite], axis=0, ignore_index=True)
        cite = cite.loc[:, ["title", "year", "url"]]
        cite = cite.sort_values(["year", "url"], ascending=[False, True])
        cite.drop_duplicates(subset="title", inplace=True)
        series = cite.apply(lambda x: f"{x[0]} ({x[1]}), {x[2]}", axis=1)
        self._covid19dh_citation = "\n".join(series.tolist())
        return (c_df, p_df)
    def _download(self):
        """
        Retrieve dataset and citation list from COVID-19 Data Hub.

        Returns:
            tuple:
                pandas.DataFrame: dataset at country level
                pandas.DataFrame: dataset at province level
                str: the list of primary sources

        Note:
            For some countries, province-level data is included.
        """
        warnings.simplefilter("ignore", ResourceWarning)
        levels = [f"administrative_area_level_{i}" for i in range(1, 4)]
        # Level 1 (country/region)
        c_raw, c_cite = covid19dh.covid19(country=None,
                                          level=1,
                                          verbose=False,
                                          raw=True)
        c_df = c_raw.groupby(levels[0]).ffill().fillna(0)
        for num in range(3):
            c_df.loc[:, levels[num]] = c_raw[levels[num]]
        # Level 2 (province/state)
        p_raw, p_cite = covid19dh.covid19(country=None,
                                          level=2,
                                          verbose=False,
                                          raw=True)
        p_df = p_raw.groupby(levels[:2]).ffill().fillna(0)
        for num in range(3):
            p_df.loc[:, levels[num]] = p_raw[levels[num]]
        # Citation
        cite = pd.concat([c_cite, p_cite], axis=0, ignore_index=True)
        cite = cite.loc[:, ["title", "year", "url"]]
        cite = cite.sort_values(["year", "url"], ascending=[False, True])
        cite.drop_duplicates(subset="title", inplace=True)
        series = cite.apply(lambda x: f"{x[0]} ({x[1]}), {x[2]}", axis=1)
        return (c_df, p_df, "\n".join(series.tolist()))
Exemple #8
0
def get_covid_data(filename='Covid'):
    """
    Gets the daily records about the ongoing pandemic due by the proliferation of the coronavirus SARS-CoV-2.

    :param filename: name of the pickle file that will host the data.
    :return: address of the pickle file.
    """
    # Get the data
    dataframe, src = covid19(raw=True)
    # Create the path where to save the data
    dataframe_path = os.path.join(base_dir, f'{filename}.pkl')
    # Save the dataset in a pickle file
    to_pickle(dataframe, dataframe_path)
    return dataframe_path
 def test_register_total(self):
     # Directly download province level data from COVID-19 Data Hub
     raw_df, *_ = covid19dh.covid19("Italy", level=2, verbose=False)
     filename = "input/italy_raw.csv"
     raw_df.to_csv(filename)
     # Create CountryData instance
     country_data = CountryData(filename=filename, country="Italy")
     country_data.set_variables(
         date="date", confirmed="confirmed", recovered="recovered", fatal="deaths",
         province="administrative_area_level_2"
     )
     # Register total value of all provinces as country level data
     country_data.register_total()
     provinces = country_data.cleaned()[Term.PROVINCE].unique()
     assert Term.UNKNOWN in provinces
Exemple #10
0
def mismatching_days():
    # get data
    cases = covid_death_cases()
    x = cases.groupby(["date"]).size().reset_index(name='case_agg')
    # reference
    ref, _ = covid19dh.covid19("Poland", verbose=False)
    ref = ref
    ref['deaths'] = ref.deaths.diff()

    # merge
    x = x.merge(ref, on="date")

    # parse not matching
    x = x[x.case_agg != x.deaths]
    return x
 def test_twitter_deaths(self):
     # get data
     cases = PL.covid_death_cases()
     x = cases.groupby(["date"]).size().reset_index(name='case_agg')
     
     # reference
     ref,_ = covid19dh.covid19("Poland", verbose = False)
     ref = ref[["date","deaths"]]
     ref['deaths'] = ref.deaths.diff()
     
     # merge
     x = x.merge(ref, on = "date")
     
     # parse not matching
     x = x[x.case_agg != x.deaths]
Exemple #12
0
def restrictions(country, level = 3):
    data,_ = covid19dh.covid19(country, level = level, verbose = False) # load data
    if level >= 1: level_cols = ['iso_alpha_2']
    if level >= 2: level_cols = [*level_cols,'administrative_area_level_2']
    if level == 3: level_cols = [*level_cols,'administrative_area_level_3']
    x = data[['date','iso_alpha_3',*level_cols,*restriction_cols]]\
        .reset_index(drop = True)
    
    # restrictions imposed
    x['week'] = x.date.apply( lambda dt: dt.isocalendar()[1] )
    x = x[["week",*level_cols,*restriction_cols]]\
        .groupby(["week",*level_cols])\
        .aggregate({c:np.nanmean for c in restriction_cols})\
        .reset_index()
    if level > 1:
        cities = x\
            .drop_duplicates(subset = ["iso_alpha_2","administrative_area_level_3"])
        mapcities = tools.nuts(city = cities.administrative_area_level_3,
                               country = cities.iso_alpha_2)
    return x
Exemple #13
0
def _IT_data(level=1):
    # read cache
    it = cache.read(country='IT', level=level)
    if it is not None: return it
    # fetch
    x, _ = covid19dh.covid19('ITA', level=level, verbose=False)
    # country level
    if level == 1:
        x = x[['date','confirmed','tests','deaths','recovered']]\
            .sort_values('date')\
            .reset_index(drop = True)
        # to diff
        x['confirmed'] = x.confirmed.diff().fillna(x.loc[0, 'confirmed'])
        x['tests'] = x.tests.diff().fillna(x.loc[0, 'tests'])
        x['deaths'] = x.deaths.diff().fillna(x.loc[0, 'deaths'])
        x['recovered'] = x.recovered.diff().fillna(x.loc[0, 'recovered'])
        x = x[~x.confirmed.isna() & ~x.tests.isna() & ~x.deaths.isna()
              & ~x.recovered.isna()]
        if level == 1:
            x['region'] = 'IT'
    # region level
    else:
        name_mapper = _name_to_nuts()
        x['region'] = x.administrative_area_level_2.apply(name_mapper.get)
        x = x[['date','region','confirmed','tests','deaths','recovered']]\
            .sort_values('date')\
            .reset_index(drop = True)
        # to diff
        for reg, df in x.groupby('region'):
            x.loc[x.region == reg,'confirmed'] = df.confirmed\
                .diff().fillna(df.reset_index(drop=True).loc[0,'confirmed'])
            x.loc[x.region == reg,'tests'] = df.tests\
                .diff().fillna(df.reset_index(drop=True).loc[0,"tests"])
            x.loc[x.region == reg,'deaths'] = df.deaths\
                .diff().fillna(df.reset_index(drop=True).loc[0,"deaths"])
            x.loc[x.region == reg,'recovered'] = df.recovered\
                .diff().fillna(df.reset_index(drop=True).loc[0,"recovered"])
    cache.write(x, country='IT', level=level)
    return x
Exemple #14
0
 def test_cite_verbose(self):
     x,src = covid19dh.covid19("CZE", verbose = False)
     # cite
     src2 = covid19dh._cite.cite(x, verbose = False)
df = df.rename(columns={'date': 'date_key'})

copy_to_sql(df = df, table_name = "STG_Google_Mobility", schema_name=params.SchemaName, primary_index = ['date_key'], if_exists = 'replace')

from datetime import datetime
datetime.utcnow()
dateTimeObj = pytz.utc.localize(datetime.utcnow()).astimezone(pytz.timezone('US/Pacific'))
timestampStr = dateTimeObj.strftime("%d-%b-%Y (%H:%M:%S.%f)")
print("Google Mobility Finished!  " + timestampStr)

#############################################################
# 6)  COVID Datahub Level 3
#############################################################
import datetime

df = covid19("USA", level = 3, start = date(2020,1,1), verbose = False)
df['current_dttm'] = datetime.datetime.today()
df = df.rename(columns={'date': 'date_key'})

copy_to_sql(df = df, table_name = "STG_COVID19_Datahub_LVL3", schema_name=params.SchemaName, primary_index = ['date_key'], if_exists = 'replace')

from datetime import datetime
datetime.utcnow()
dateTimeObj = pytz.utc.localize(datetime.utcnow()).astimezone(pytz.timezone('US/Pacific'))
timestampStr = dateTimeObj.strftime("%d-%b-%Y (%H:%M:%S.%f)")
print("COVID Datahub Level 3 Finished!  " + timestampStr)

#############################################################
# 7)  COVID Datahub Level 2
#############################################################
import datetime
// AUTHOR: Dev Gupta
// Python3 Concept: using covid19dh library to get data
// GITHUB: https://github.com/guptadev21

//Add your python3 concept below

from covid19dh import covid19
import datetime

a, data = covid19(raw = True, verbose = False)

country = input("For which country you want the data: \n")

a, data = covid19(country, cache = False, verbose = False, start = datetime.date(2021,10,1))


#print(data.columns) #shows no. of total columns that can be accessed
#print(data)    #to show all the data
print(data.iso_alpha_3) # to show only country and no. of cases
print("Getting latest data from covid19dh")

from covid19dh import covid19
import datetime

countries = [
    "Italy", "Austria", "Germany", "Belgium", "France", "United Kingdom",
    "Switzerland", "Portugal"
]

yesterday = datetime.date.today() - datetime.timedelta(days=1)

x, src = covid19(countries,
                 raw=True,
                 verbose=False,
                 end=yesterday,
                 cache=False)
print("Preprocessing european data..")
x_small = x.loc[:, [
    'administrative_area_level_1', 'date', 'vaccines', 'confirmed', 'tests',
    'recovered', 'deaths', 'population'
]]
x_small.rename(columns={'administrative_area_level_1': 'id'}, inplace=True)

x_small[
    'confirmed_per'] = 100000 * x_small['confirmed'] / x_small['population']
x_small['deaths_per'] = 100000 * x_small['deaths'] / x_small['population']
x_small['ratio'] = 100 * (x_small['deaths']) / (x_small['confirmed'])
x_small['tests_per'] = 100000 * (x_small['tests']) / (x_small['population'])
x_small['vaccines_per'] = x_small['vaccines'] / x_small['population']
def doFit(args):
    args.output_dir = os.path.join(args.output_dir,
                                   time.strftime("%Y%m%d%H%M"))
    if not os.path.exists(args.output_dir):
        os.makedirs(args.output_dir)
    global mainVars

    #if args.country=="ESP":
    #df=pd.read_csv('/home/enrico/iper-social-simulations/examples/SAR-COV2/0dim/data/dfESP.csv')
    #else:
    df, _ = covid19(args.country)
    #df, _ = covid19(args.country)
    #import ipdb; ipdb.set_trace()
    df = df[df["date"] > args.start]
    df = df[df["date"] < args.stop]
    df = df[df["confirmed"] > 0]  #Starts from first infections
    #dati_regione1=dati_regione_[dati_regione_['totale_positivi']>25] #per stabilire il t0

    N = df["population"].mean()
    icu_beds = pd.read_csv(args.data_icu, header=0)
    icu_beds = dict(zip(icu_beds["Country"], icu_beds["ICU_Beds"]))
    icu_beds = icu_beds[
        "Italy"] * N / 100000  # Emergency life support beds per 100k citizens
    print("Maximum icy beds", icu_beds)
    #dati_regione1=pd.read_csv('dpc-covid19-ita-andamento-nazionale.csv')
    #regione='ITA'

    age_effect = 1
    demographic = {"0-29": 0.2, "30-59": 0.4, "60-89": 0.35, "89+": 0.05}

    #I=np.array(dati_regione1['totale_positivi'])
    #D=np.array(dati_regione1['deceduti'])
    #R=np.array(dati_regione1['dimessi_guariti'])
    #H=np.array(dati_regione1['totale_ospedalizzati'])
    #S=N-I-D-H-R

    df.plot(x="date", y=["tests"])
    plt.savefig(os.path.join(args.output_dir, "tests-%s.png" % args.country))

    df.plot(x="date", y=["confirmed", "recovered", "deaths"])
    plt.savefig(
        os.path.join(args.output_dir,
                     "confirmed-recovered-%s.png" % args.country))

    df.plot(x="date", y=["deaths", "hosp", "vent", "icu"])
    plt.savefig(os.path.join(args.output_dir,
                             "severity-%s.png" % args.country))

    print("Maximum people in vent", df["vent"].max())
    print("Maximum people in icu", df["icu"].max())

    #x1=df.copy(deep=True)
    #Iday=x1.iloc[:,3]
    #Iday.fillna(0,inplace=True,axis=0)
    #Iday=Iday.values

    #for i in range(len(Iday)-1,1,-1):
    #Iday[i]=Iday[i]-Iday[i-1]

    #if args.country=="ESP":
    # df1=pd.read_csv('https://raw.githubusercontent.com/datadista/datasets/master/COVID%2019/nacional_covid19.csv')
    # df1 = df1[df1["fecha"]>args.start]
    # df1 = df1[df1["fecha"]<args.stop]
    # x1=df1.copy(deep=True)
    # Hday=x1.iloc[:,3]
    # Hday.fillna(0,inplace=True,axis=0)
    # Hday=Hday.values

    if args.country == 'ESP':
        I = df[
            "confirmed"]  # in questo caso rappresenta il totale degli individui che si sono infettati
        #R = df["recovered"]# totale recovered
        D = df["deaths"]  # Totale morti
        #H = df["hosp"]
        S = N - I

        t = np.array([i + 1 for i in range(len(I))])
        i0 = df["confirmed"].iloc[0]  #  35708    #infetti a t0
        a0 = 2 * i0
        r0 = 0
        ra0 = 0
        h0 = 0
        d0 = df["deaths"].iloc[0]  # 35587      #deceduti  t0
        s0 = N - i0  #-a0                   #suscettibili a t0
        y0 = s0, a0, i0, r0, ra0, h0, d0

    else:
        I = df["confirmed"] - df["recovered"] - df["hosp"] - df["deaths"]
        R = df["recovered"]
        D = df["deaths"]
        H = df["hosp"]
        S = N - df["confirmed"]

        t = np.array([i + 1 for i in range(len(I))])
        i0 = df["confirmed"].iloc[0] - df["recovered"].iloc[0] - df[
            "deaths"].iloc[0]  #  35708    #infetti a t0
        a0 = 2 * i0  #asintomatici a t0
        r0 = df["recovered"].iloc[0]  # 211885      #recovered a t0
        ra0 = 2 * r0
        h0 = df["hosp"].iloc[0]  # 2000    #ospedaliz. a t0
        d0 = df["deaths"].iloc[0]  # 35587      #deceduti  t0
        s0 = N - i0 - r0 - h0 - d0 - a0 - ra0  #suscettibili a t0
        y0 = s0, a0, i0, r0, ra0, h0, d0

    def resid_ESP(params, y0, t, N, age_effect, S, I, D, icu_beds):
        print(params)
        rates = {"rsa": 0, "rai": 0, "rih": 0, "rir": 0, "rhr": 0, "rhd": 0}

        r0_max = params['r0_max']
        r0_min = params['r0_min']
        k = params['k']
        startLockdown = params['startLockdown']
        seihrd_det = solveSIRdet(y0, t, N, age_effect, r0_max, r0_min, k,
                                 startLockdown, params, demographic, icu_beds)
        #return sum((seihrd_det['S']-S)**2+(seihrd_det['I']-I)**2+(seihrd_det['H']-H)**2+(seihrd_det['R']-R)**2+(seihrd_det['D']-D)**2)
        #er=np.array((((seihrd_det['S']-S)**2),((seihrd_det['I']-I)**2),((seihrd_det['H']-H)**2),((seihrd_det['R']-R)**2),((seihrd_det['D']-D)**2)))

        a = np.array((S - (seihrd_det['S'] + seihrd_det['A']))**2)

        b = np.array((I - (seihrd_det['I'] + seihrd_det['R'] +
                           seihrd_det['H'] + seihrd_det['D']))**2)
        #b=np.array((Iday-seihrd_det['I'])**2)
        #c=np.array((H-seihrd_det['H'])**2)
        #d=np.array((R-seihrd_det['R'])**2)
        e = np.array((1.2 * D - 1.2 * seihrd_det['D'])**2)

        tot = np.concatenate((b, e))

        #return er
        #er=er.flatten()
        #er2= np.array((seihrd_det['S']-S)**2,(seihrd_det['I']-I)**2,(seihrd_det['H']-H)**2,(seihrd_det['R']-R)**2,(seihrd_det['D']-D)**2)
        return tot

    def resid_ALL(params, y0, t, N, age_effect, S, I, H, R, D, icu_beds):
        print(params)
        rates = {"rsa": 0, "rai": 0, "rih": 0, "rir": 0, "rhr": 0, "rhd": 0}

        r0_max = params['r0_max']
        r0_min = params['r0_min']
        k = params['k']
        startLockdown = params['startLockdown']
        seihrd_det = solveSIRdet(y0, t, N, age_effect, r0_max, r0_min, k,
                                 startLockdown, params, demographic, icu_beds)
        #return sum((seihrd_det['S']-S)**2+(seihrd_det['I']-I)**2+(seihrd_det['H']-H)**2+(seihrd_det['R']-R)**2+(seihrd_det['D']-D)**2)
        #er=np.array((((seihrd_det['S']-S)**2),((seihrd_det['I']-I)**2),((seihrd_det['H']-H)**2),((seihrd_det['R']-R)**2),((seihrd_det['D']-D)**2)))

        a = np.array((S - (seihrd_det['S'] + seihrd_det['A']))**2)
        b = np.array((I - seihrd_det['I'])**2)
        c = np.array((H - seihrd_det['H'])**2)
        d = np.array((R - seihrd_det['R'])**2)
        e = np.array(1.44 * (D - seihrd_det['D'])**2)  #mettere fuori 1.2

        tot = np.concatenate((a, b, e, c, d))

        #return er
        #er=er.flatten()
        #er2= np.array((seihrd_det['S']-S)**2,(seihrd_det['I']-I)**2,(seihrd_det['H']-H)**2,(seihrd_det['R']-R)**2,(seihrd_det['D']-D)**2)
        return tot

    if args.country == 'ESP':

        params2 = Parameters()
        params2.add('r0_max', value=5.0, min=2.0, max=5.0, vary=True)
        params2.add('r0_min', value=0.9, min=0.3, max=3.5, vary=True)
        params2.add('k', value=2.5, min=0.01, max=5.0, vary=True)
        params2.add('startLockdown', value=31.0, min=0, max=100, vary=True)
        params2.add('rsa', value=1.0, min=0.01, max=2.0, vary=True)
        params2.add('rai', value=0.5, min=0.01, max=2, vary=True)
        params2.add('rar',
                    value=0.010080807956200328,
                    min=0.01,
                    max=2,
                    vary=False)
        params2.add('rih',
                    value=0.15376988655220583,
                    min=0.01,
                    max=2.0,
                    vary=False)
        params2.add('rir',
                    value=0.012084665739583795,
                    min=0.01,
                    max=2,
                    vary=False)
        params2.add('rhr', value=0.012, min=0.01, max=2,
                    vary=False)  #0.0743636663768294
        params2.add('rhd', value=0.125, min=0.01, max=2, vary=True)  #

        out = minimize(resid_ESP,
                       params2,
                       args=(y0, t, N, age_effect, S, I, D, icu_beds),
                       method='differential_evolution'
                       )  #method='differential_evolution',method='leastsq'

    else:

        params2 = Parameters()
        params2.add('r0_max', value=5.0, min=2.0, max=5.0, vary=True)
        params2.add('r0_min', value=0.9, min=0.3, max=3.5, vary=True)
        params2.add('k', value=2.5, min=0.01, max=5.0, vary=True)
        params2.add('startLockdown', value=31.0, min=0, max=100, vary=True)
        params2.add('rsa', value=1.0, min=0.01, max=2.0, vary=True)
        params2.add('rai', value=0.5, min=0.01, max=2, vary=True)
        params2.add('rar', value=0.1, min=0.01, max=2, vary=True)
        params2.add('rih', value=0.1, min=0.01, max=2.0, vary=True)
        params2.add('rir', value=0.1, min=0.01, max=2, vary=True)
        params2.add('rhr', value=0.142, min=0.01, max=2, vary=True)
        params2.add('rhd', value=0.125, min=0.01, max=2, vary=True)

        #res3=resid_(params2,y0, t, N,age_effect,S,I,H,R,D,icu_beds)
        out = minimize(resid_ALL,
                       params2,
                       args=(y0, t, N, age_effect, S, I, H, R, D, icu_beds),
                       method='differential_evolution'
                       )  #method='differential_evolution',method='leastsq'

    print(fit_report(out))

    print(out.params.pretty_print())
    #out.plot_fit(datafmt="-")
    #plt.savefig(os.path.join(args.output_dir, "best_fit.png"))
    #import ipdb; ipdb.set_trace()

    print("**** Estimated parameters:")
    print([out.params.items()])

    rates_fit = {
        "rsa": out.params['rsa'].value,
        "rai": out.params['rai'].value,
        "rar": out.params['rar'].value,
        "rih": out.params['rih'].value,
        "rir": out.params['rir'].value,
        "rhr": out.params['rhr'].value,
        "rhd": out.params['rhd'].value
    }

    seihrd_det_fit = solveSIRdet(y0, t, N, age_effect,
                                 out.params['r0_max'].value,
                                 out.params['r0_min'].value,
                                 out.params['k'].value,
                                 out.params['startLockdown'].value, rates_fit,
                                 demographic, icu_beds)

    plt.figure(figsize=(15, 12))
    plt.suptitle(args.country)

    if args.country == 'ESP':
        I_tot = seihrd_det_fit['I'] + seihrd_det_fit['R'] + seihrd_det_fit[
            'D'] + seihrd_det_fit['H']
        ax3 = plt.subplot(331)
        ax3.plot(t, I_tot, color="r")
        ax3.plot(t, I, label='Infected-cumulative', color="r", ls="--")
        plt.xlabel('day')
        plt.ylabel('Infected')

        ax4 = plt.subplot(332)
        ax4.plot(t, seihrd_det_fit['D'], color="black")
        ax4.plot(t, D, label='Dead', color="black", ls="--")
        plt.xlabel('day')
        plt.ylabel('Dead')

        ax5 = plt.subplot(333)
        ax5.plot(t, seihrd_det_fit['R'], color="g")
        plt.xlabel('day')
        plt.ylabel('Recovered-seihrd')

        #ax6=plt.subplot(334)
        #ax6.plot(t,seihrd_det_fit['S'],color="y")
        #ax6.plot(t,S,label='Susceptible',color="y",ls="--")
        #plt.xlabel('day')
        #plt.ylabel('Susceptible')

        ax7 = plt.subplot(335)
        ax7.plot(t, seihrd_det_fit['H'], color="orange")
        plt.xlabel('day')
        plt.ylabel('Hosp-seihrd')

        ax8 = plt.subplot(336)
        ax8.plot(t, I_tot, color="r")
        ax8.plot(t, I, label='Infected-cumulative', color="r", ls="--")
        ax8.plot(t, seihrd_det_fit['R'], color="g")
        ax8.plot(t, seihrd_det_fit['D'], color="black")
        ax8.plot(t, D, label='Dead', color="black", ls="--")
        ax8.plot(t, seihrd_det_fit['H'], color="orange")
        plt.xlabel('day')
        plt.ylabel('people')

    else:
        ax3 = plt.subplot(331)
        ax3.plot(t, seihrd_det_fit['I'], color="r")
        ax3.plot(t, I, label='Infected', color="r", ls="--")
        plt.xlabel('day')
        plt.ylabel('Infected')

        ax4 = plt.subplot(332)
        ax4.plot(t, seihrd_det_fit['D'], color="black")
        ax4.plot(t, D, label='Dead', color="black", ls="--")
        plt.xlabel('day')
        plt.ylabel('Dead')

        ax5 = plt.subplot(333)
        ax5.plot(t, seihrd_det_fit['R'], color="g")
        ax5.plot(t, R, label='Recovered', color="g", ls="--")
        plt.xlabel('day')
        plt.ylabel('Recovered')

        ax6 = plt.subplot(334)
        ax6.plot(t, ((seihrd_det_fit['S'] + seihrd_det_fit['A'])), color="y")
        ax6.plot(t, S, label='Susceptible', color="y", ls="--")
        plt.xlabel('day')
        plt.ylabel('Susceptible')

        ax7 = plt.subplot(335)
        ax7.plot(t, seihrd_det_fit['H'], color="orange")
        ax7.plot(t, H, label='Hosp', color="orange", ls="--")
        plt.xlabel('day')
        plt.ylabel('Hosp.')

        ax8 = plt.subplot(336)
        ax8.plot(t, seihrd_det_fit['I'], color="r")
        ax8.plot(t, I, label='Infected', color="r", ls="--")
        ax8.plot(t, seihrd_det_fit['R'], color="g")
        ax8.plot(t, R, label='Recovered', color="g", ls="--")
        ax8.plot(t, seihrd_det_fit['D'], color="black")
        ax8.plot(t, D, label='Dead', color="black", ls="--")
        ax8.plot(t, seihrd_det_fit['H'], color="orange")
        ax8.plot(t, H, label='Hosp', color="orange", ls="--")
        plt.xlabel('day')
        plt.ylabel('people')

    #df_fitSAIHRD=pd.read_csv(os.path.join('fit','df_fitSAIHRD.csv'))

    risult = np.array([[
        args.country, out.params['rsa'].value, out.params['rai'].value,
        out.params['rih'].value, out.params['rir'].value,
        out.params['rhr'].value, out.params['rhd'].value,
        out.params['r0_max'].value, out.params['r0_min'].value,
        out.params['k'].value, out.params['startLockdown'].value
    ]])

    df_fitSAIHRD = pd.DataFrame(risult,
                                columns=[
                                    "regione", "rsa", "rai", "rih", "rir",
                                    "rhr", "rhd", "r0_max", "r0_min", "k",
                                    "startLockdown"
                                ])
    fname = "df_fitSAIHRD.csv"
    df_fitSAIHRD.to_csv(os.path.join(args.output_dir, fname), index=False)
    saveFIG = args.country + '.png'
    plt.savefig(os.path.join(args.output_dir, saveFIG))

    mainVars = inspect.currentframe().f_locals