예제 #1
0
 def test_check_settings_plot_intervals_basic(self):
     data = DataStructure()
     data.add_data_set(x=np.random.random_sample((100, 2)), y=None)
     results = gf.setup_pseudo_results()
     chain = results['chain']
     s2chain = results['s2chain']
     intervals = uqp.calculate_intervals(
             chain, results, data, model3D, s2chain=s2chain)
     fig, ax, isets = uqp.plot_3d_intervals(
             intervals, data.xdata[0], limits=[95],
             return_settings=True)
     self.assertEqual(ax.get_legend_handles_labels()[1],
                      ['Model', '95% PI', '95% CI'],
                      msg=str('Strings should match: {}'.format(
                              ax.get_legend_handles_labels()[1])))
     self.assertTrue(isinstance(isets, dict),
                     msg='Expect dictionary')
     self.assertEqual(isets['ciset']['limits'], [95])
     self.assertEqual(isets['piset']['limits'], [95])
     plt.close()
     plt.close()
     fig, ax, isets = uqp.plot_3d_intervals(
             intervals, data.xdata[0], limits=[95],
             adddata=True, ydata=data.xdata[0][:, 0],
             return_settings=True)
     self.assertEqual(ax.get_legend_handles_labels()[1],
                      ['Model', 'Data', '95% PI', '95% CI'],
                      msg=str('Strings should match: {}'.format(
                              ax.get_legend_handles_labels()[1])))
     self.assertTrue(isinstance(isets, dict),
                     msg='Expect dictionary')
     self.assertEqual(isets['ciset']['limits'], [95])
     self.assertEqual(isets['piset']['limits'], [95])
     plt.close()
     
예제 #2
0
 def test_predintcreation(self):
     data = DataStructure()
     data.add_data_set(x=np.linspace(0, 1), y=None)
     results = gf.setup_pseudo_results()
     chain = results['chain']
     s2chain = results['s2chain']
     intervals = uqp.calculate_intervals(
             chain, results, data, model, s2chain=s2chain)
     self.assertTrue('credible' in intervals.keys(),
                     msg='Expect credible intervals')
     self.assertTrue('prediction' in intervals.keys(),
                     msg='Expect prediction intervals')
     self.assertTrue(isinstance(intervals['credible'], np.ndarray),
                     msg='Expect numpy array')
     self.assertTrue(isinstance(intervals['prediction'], np.ndarray),
                     msg='Expect numpy array')
     intervals = uqp.calculate_intervals(
             chain, results, data, model, s2chain=0.1)
     self.assertTrue('credible' in intervals.keys(),
                     msg='Expect credible intervals')
     self.assertTrue('prediction' in intervals.keys(),
                     msg='Expect prediction intervals')
     self.assertTrue(isinstance(intervals['credible'], np.ndarray),
                     msg='Expect numpy array')
     self.assertTrue(isinstance(intervals['prediction'], np.ndarray),
                     msg='Expect numpy array')
예제 #3
0
 def test_plot_intervals_basic(self):
     data = DataStructure()
     data.add_data_set(x=np.linspace(0, 1), y=None)
     results = gf.setup_pseudo_results()
     chain = results['chain']
     s2chain = results['s2chain']
     intervals = uqp.calculate_intervals(
             chain, results, data, model, s2chain=s2chain)
     fig, ax = uqp.plot_intervals(intervals, data.xdata[0], limits=[95])
     self.assertEqual(ax.get_legend_handles_labels()[1],
                      ['Model', '95% PI', '95% CI'],
                      msg=str('Strings should match: {}'.format(
                              ax.get_legend_handles_labels()[1])))
     plt.close()
     fig, ax = uqp.plot_intervals(intervals, data.xdata[0], limits=[95],
                                  adddata=True, ydata=data.xdata[0])
     self.assertEqual(ax.get_legend_handles_labels()[1],
                      ['Model', 'Data', '95% PI', '95% CI'],
                      msg=str('Strings should match: {}'.format(
                              ax.get_legend_handles_labels()[1])))
     plt.close()
def main():
    #############################################################
    print('Getting data from github...')
    try:
        subprocess.run([
            'gitdir',
            'https://github.com/CSSEGISandData/COVID-19/tree/master/csse_covid_19_data/csse_covid_19_daily_reports/'
        ])
        directory = './csse_covid_19_data/csse_covid_19_daily_reports'
        df = dfProcess(directory, 'US')
    except:
        print('Directory error.')

    print('Countries:\n {} \n'.format(df.countries))
    print('Dates in database:\n {} \n'.format(df.dates))
    print('Data for US available from the following dates:\n {} \n'.format(
        df.cDates))
    print('Data for US States Isolated: \n {}'.format(df.states))

    ############################################################
    '''Map Creation'''
    #Will take a while
    #Create maps for all dates
    print('\n Creating maps for each available date...')
    mpl.use('Agg')
    for i, date in enumerate(df.country.keys()):
        print("Creating map for {}.........{}/{}".format(
            date, i + 1, len(df.country.keys())))
        df.mapPlot(df.country[date], 'Deaths', 1)
    print('Maps are saved in Progress folder...')
    ############################################################
    '''Stitching images together'''
    print('Creating video of maps...')
    path = sorted(glob.glob(os.path.join('./Progress/*.png')))
    files = [cv2.imread(file) for file in path]
    height, width, layers = files[0].shape
    size = (width, height)
    files = [cv2.resize(file, size) for file in files]

    out = cv2.VideoWriter('ConfirmedvsDeaths.avi',
                          cv2.VideoWriter_fourcc(*'XVID'), 60, size)
    for file in files:
        for _ in range(15):
            out.write(file)
    out.release()
    print('Done.')
    #########################################################
    '''Parameter estimation'''

    #Define cost function for MCMC
    def ssfunc(q, data):
        y = data.ydata[0]
        beta, gamma, N, S, I, R = q
        ymodel = model.runmodel(beta, gamma, N, S, I, R)
        res = ymodel[1] - y
        return (res**2).sum()

    print('Setting up auxilary files and MCMC parameters')
    #Aux data
    census = pd.read_excel('nst-est2019-01.xlsx', encoding='UTF-8')
    census['State'] = list(
        map(lambda x: re.sub(r'\.', '', x), census['State']
            ))  #Some states has a period in the beginning, cleaning that
    census.set_index('State', inplace=True)

    #Scatter
    confirmed = [
        j for k in df.states.columns.levels[0][:-2]
        for j in df.states[k]['Confirmed']
    ]
    timespan = np.linspace(0, len(df.cDates), len(confirmed))
    model = SIR(timespan, 14)

    #invoke module
    mcmcstat = MCMC()
    mcdata = DataStructure(
    )  #Just doing this for the sake of following proper format
    mcmcstat.data.add_data_set(x=timespan, y=confirmed)

    #apply cost function
    mcmcstat.model_settings.define_model_settings(sos_function=ssfunc)

    #simulation options. Using DRAM method
    mcmcstat.simulation_options.define_simulation_options(nsimu=10.0e3,
                                                          updatesigma=True,
                                                          method='dram',
                                                          adaptint=100,
                                                          verbosity=1,
                                                          waitbar=1)

    #Parameters of interest
    mcmcstat.parameters.add_model_parameter(name='beta',
                                            theta0=0.01,
                                            minimum=0.00001,
                                            maximum=0.5)

    mcmcstat.parameters.add_model_parameter(name='gamma',
                                            theta0=0.01,
                                            minimum=0.001,
                                            maximum=0.5 / 2.1)
    '''Population and initial conditions, these are fixed.'''
    N, S, I = census['Population'].sum() / len(
        df.states), census['Population'].sum() / len(df.states) - 1, 1
    R = 0
    mcmcstat.parameters.add_model_parameter(name='N', theta0=N, sample=False)

    mcmcstat.parameters.add_model_parameter(name='S', theta0=S, sample=False)

    mcmcstat.parameters.add_model_parameter(name='I', theta0=I, sample=False)

    mcmcstat.parameters.add_model_parameter(name='R', theta0=R, sample=False)

    print('Running MCMC. Will take a while...')
    mcmcstat.run_simulation()
    print('Done.')

    ####MCMC outputs
    mcmcresults = mcmcstat.simulation_results.results
    burnin = int(mcmcresults['nsimu'] / 2)

    #Statistics
    chain = mcmcresults['chain']
    s2chain = mcmcresults['s2chain']
    sschain = mcmcresults['sschain']
    names = mcmcresults['names']
    mcmcstat.chainstats(chain[burnin:, :], mcmcresults)
    print('Acceptance rate: {:6.4}%'.format(
        100 * (1 - mcmcresults['total_rejected'])))
    print('Model Evaluations: {}'.format(mcmcresults['nsimu'] -
                                         mcmcresults['iacce'][0] +
                                         mcmcresults['nsimu']))

    if not os.path.exists('MCMC'):
        os.makedirs('MCMC')
    fig = mcp.plot_density_panel(chain[burnin:, :], names)
    fig.savefig("MCMC/Density.png", bbox_inches='tight', dpi=100)
    fig = mcp.plot_pairwise_correlation_panel(chain[burnin:, :], names)
    fig.savefig("MCMC/Pairwise.png", bbox_inches='tight', dpi=100)
    print('MCMC parameter plots are saved in MCMC')
    ##########################################################
    print('Creating final projection graph...')

    #difference from first date to last day
    ddiff = abs((dt.strptime(df.cDates[0], '%m-%d-%Y') -
                 dt.strptime(df.cDates[-1], '%m-%d-%Y')).days)
    #projected parameters
    pbeta, pgamma = mcmcresults['mean'][0], mcmcresults['mean'][1]
    #Projecting 360 days, fingers crossed
    ptimespan = np.linspace(ddiff, ddiff + 365, 365 * len(df.states))
    model = SIR(ptimespan, 14)
    #define current susceptible, infected, removed.
    cconfirmed = df.statsPerCountry('US').loc[
        df.cDates[-1]]['Confirmed'] / len(df.states)
    cremoved = (df.statsPerCountry('US').loc[df.cDates[-1]]['Recovered'] +
                df.statsPerCountry('US').loc[df.cDates[-1]]['Deaths']) / len(
                    df.states)
    csus = S - cconfirmed - cremoved

    projection = model.runmodel(pbeta, pgamma, N, csus, cconfirmed, cremoved)
    pfig, pax = plt.subplots(1, 1, figsize=(35, 8))
    pax.plot(ptimespan, projection[1])
    pax.set(title='Projected Infectives', xlabel='Days since 03-01-2020')
    plt.savefig('./FinalProjection.png', bbox_inches='tight', dpi=100)

    print('FinalProjection graph saved in current folder...Done')