示例#1
0
    def observed_vs_model(self, out='simple_fit/', save=False):

        countries = self.countries + ['All Countries']
        data = {}
        for i, c in enumerate(countries):
            data[c] = {}

            # first reported cases assumed to have been reported in generation 5
            start_generation = 5

            fig = plt.figure()
            ax = fig.add_subplot(111)
            width = 0.35

            # up-to-date data
            x, y = self.get_SI_series(c, fill_y=True)
            x = x + 5
            R0, d = f.RMSD_fit(x, y)
            y_rmsd = f.cumI(x, R0, d)
            ax.bar(x, y, width, color='b', alpha=0.3)
            ax.plot(x + width, y_rmsd, color='b', label='Latest Data Fit')

            data[c]['R0'] = R0
            data[c]['d'] = d
            data[c]['RMSD'] = f.RMSD(y, y_rmsd)

            # fisman data
            x, y = self.get_SI_series(c, fill_y=True, fisman_data=True)
            x = x + 5
            R0, d = f.RMSD_fit(x, y)
            y_rmsd = f.cumI(x, R0, d)
            ax.bar(x + width, y, width, color='r', alpha=0.3)
            ax.plot(x + width, y_rmsd, color='r', label='Fisman Data Fit')

            data[c]['Fisman R0'] = R0
            data[c]['Fisman d'] = d
            data[c]['Fisman RMSD'] = f.RMSD(y, y_rmsd)

            plt.legend(loc=2)
            # plt.title(c)
            if save:
                d = self.root + out
                Analysis.check_dir(d)
                fn = d + 'simple_fit_%s.png' % (c.lower().replace(' ', '_'))
                plt.savefig(fn)

                # save latex table
                keys = [
                    'R0', 'Fisman R0', 'd', 'Fisman d', 'RMSD', 'Fisman RMSD'
                ]
                with open(d + 'simple_fit.tex', 'w') as fw:
                    fw.write(Analysis.to_latex_table(data, keys))
            else:
                plt.show()
示例#2
0
 def contour_plot(self, out='contour/', save=False):
     countries = self.countries + ['All Countries']
     intervals = [12, 15, 18]
     R0_range = np.linspace(1.4, 3.6, 100)
     d_range = np.linspace(0, 0.07, 100)
     data = {}
     for i, c in enumerate(countries):
         print 'Contour Plot for %s' % (c)
         data[c] = {}
         x, y = self.get_SI_series(c, fill_y=True)
         x = x + 5
         R0, d = f.RMSD_fit(x, y)
         data[c]['R0'] = R0
         data[c]['d'] = d
         if save:
             d = self.root + out
             Analysis.check_dir(d)
             fn = d + "param_contour_%s.png" % (c.lower().replace(' ', '_'))
             p.parameter_heatmap(x,
                                 y,
                                 R0_range,
                                 d_range,
                                 f.cumI,
                                 f.RMSD,
                                 fn=fn)
         else:
             p.parameter_heatmap(x, y, R0_range, d_range, f.cumI, f.RMSD)
     if save:
         d = self.root + out
         Analysis.check_dir(d)
         keys = ['R0', 'd']
         with open(d + 'contour.tex', 'w') as fw:
             fw.write(
                 Analysis.to_latex_table(
                     data, keys, caption='Best-fit R0 and d by Country'))
示例#3
0
    def control_assessment(self, out='control/', save=False):
        countries = self.countries + ['All Countries']
        data = {}
        for i, c in enumerate(countries):
            data[c] = {}

            # first reported cases assumed to have been reported in generation 5
            start_generation = 5

            # up-to-date data
            x0, y0 = self.get_SI_series(c, fill_y=True)
            x0 = x0 + start_generation

            fig, (ax1, ax2) = plt.subplots(1, 2)  #, sharey=True)
            fig.set_size_inches(15, 5)
            #.plot(x, y) # projection
            # axarr[0].set_title('Sharing X axis')
            # axarr[1].scatter(x, y) # error

            for i in range(2, len(x0)):
                x = x0[0:i]
                y = y0[0:i]

                actual = y0[i]

                R0, d = f.RMSD_fit(x, y)
                SI = x[-1]
                projected = f.cumI(SI + 1, R0, d)

                # projection vs actual
                if projected < 2 * actual:
                    ax1.scatter(actual, projected, alpha=0.3)
                    if actual > 500:
                        ax1.annotate('%i' % (SI),
                                     (actual - 100, projected + 150))

                # error
                error = np.abs(projected - actual) / float(actual)
                if error > 1.5:
                    print 'Error for SI=%i is %f !' % (x[-1], error)
                else:
                    plt.scatter(x0[i], error)

            ax1.plot(y0, y0, 'b-')
            ax1.set_ylabel('Projected Cases')
            ax1.set_xlabel('Actual Cases')
            ax2.set_ylim([0, 1])
            ax2.set_ylabel('Percent Error')
            ax2.set_xlabel('Serial Inteval')

            if save:
                d = self.root + out
                Analysis.check_dir(d)
                fn = d + 'control_%s.png' % (c.lower().replace(' ', '_'))
                plt.savefig(fn)
            else:
                plt.show()
示例#4
0
    def progressive_projections(self, out='sensitivity/', save=False):
        countries = self.countries + ['All Countries']
        data = {}
        for i, c in enumerate(countries):
            data[c] = {}

            # first reported cases assumed to have been reported in generation 5
            start_generation = 5

            # up-to-date data
            x0, y0 = self.get_SI_series(c, fill_y=True)
            x0 = x0 + start_generation

            intervals = range(2, len(x0))

            plt.figure()
            fig = plt.figure()
            ax = plt.subplot(111)

            colors = self.get_colors(n=len(intervals))
            for ind, i in enumerate(intervals):
                x = x0[:i + 1]
                y = y0[:i + 1]
                R0, d = f.RMSD_fit(x, y)

                y_fit = f.cumI(x0, R0, d)
                ax.plot(x,
                        y_fit[:i + 1],
                        color=colors[ind],
                        label='SI=%i' % (i))
                ax.plot(x0, y_fit, '--', color=colors[ind])  #, ls='--')
                ax.scatter(x[-1], y_fit[i], color=colors[ind])

            width = 0.8
            ax.bar(x0 - 0.5 * width, y0, width, color='blue', alpha=0.1)
            ax.set_ylim(0, y0[-1] * 1.5)
            ax.legend(loc='upper center',
                      bbox_to_anchor=(0.5, 1.05),
                      ncol=5,
                      fancybox=True,
                      shadow=True)
            ax.set_ylabel('Cumulative Incidence Count')
            ax.set_xlabel('Generations Available for Fitting')

            if save:
                d = self.root + out
                Analysis.check_dir(d)
                fn = d + 'progressive_projections_%s.png' % (c.lower().replace(
                    ' ', '_'))
                plt.savefig(fn)
            else:
                plt.show()
示例#5
0
    def sensitivity_analysis(self, out='sensitivity/', save=False):
        countries = self.countries + ['All Countries']
        keys = [
            'Base Case', '12 day generation time', '18 day generation time',
            'Outbreak recognized generation 3',
            'Outbreak recognized generation 7', 'Outbreak 50% underreported',
            'Outbreak 99% underreported', 'Deaths only'
        ]

        for c in countries:
            series = {'up-to-date': {}, 'Fisman': {}}

            for k, xy in series.iteritems():

                if k == 'Fisman':
                    self.fisman_data = True
                else:
                    self.fisman_data = False

                x0, y0 = self.get_SI_series(c, fill_y=True)
                xy['Base Case'] = (x0 + 5, y0)

                self.serial_interval = 12
                x0, y0 = self.get_SI_series(c, fill_y=True)
                xy['12 day generation time'] = (x0 + 5, y0)

                self.serial_interval = 18
                x0, y0 = self.get_SI_series(c, fill_y=True)
                xy['18 day generation time'] = (x0 + 5, y0)

                #reset
                self.serial_interval = 15

                x0, y0 = self.get_SI_series(c, fill_y=True)
                xy['Outbreak recognized generation 3'] = (x0 + 3, y0)
                xy['Outbreak recognized generation 7'] = (x0 + 7, y0)

                xy['Outbreak 50% underreported'] = ((x0 + 5) * 0.5, y0)
                xy['Outbreak 99% underreported'] = ((x0 + 5) * 0.01, y0)

                self.incidence_type = 'deaths'
                x0, y0 = self.get_SI_series(c, fill_y=True)
                xy['Deaths only'] = (x0 + 5, y0)

                # reset
                self.incidence_type = 'cases'

            data = {}
            categories = ['R0', 'R0 (Fisman)', 'd', 'd (Fisman)']
            for category in categories:
                if 'Fisman' in category:
                    sub_series = series['Fisman']
                else:
                    sub_series = series['up-to-date']

                column = {}
                for k in keys:
                    x, y = sub_series[k]
                    R0, d = f.RMSD_fit(x, y)

                    if 'R0' in category:
                        column[k] = R0
                    else:
                        column[k] = d
                data[category] = column
            table = Analysis.to_latex_table(data,
                                            keys,
                                            countries=categories,
                                            caption=c)
            if save:
                d = self.root + out
                Analysis.check_dir(d)
                fn = d + 'sensitivity_%s.tex' % (c.lower().replace(' ', '_'))
                with open(fn, 'w') as fw:
                    fw.write(table)
            else:
                print table
示例#6
0
    def deltaD(self, out='deltaD/', save=False):
        countries = self.countries + ['All Countries']
        data = {}
        for i, c in enumerate(countries):
            data[c] = {}

            # first reported cases assumed to have been reported in generation 5
            start_generation = 5

            # up-to-date data
            x0, y0 = self.get_SI_series(c, fill_y=True)
            x0 = x0 + start_generation

            fig, ax1 = plt.subplots()

            intervals = range(5, len(x0))
            R0_list = [None] * len(x0)
            d_list = [None] * len(x0)
            deltaD_list = [None] * len(x0)

            colors = self.get_color_map(n=len(intervals))
            prev_d = None
            for i in intervals:
                x = x0[:i]
                y = y0[:i]
                R0, d = f.RMSD_fit(x, y)
                R0_list[i] = R0
                d_list[i] = d

                if prev_d is None:
                    prev_d = d
                else:
                    deltaD = d - prev_d
                    deltaD_list[i] = deltaD
                    prev_d = d

            ax1.set_xlabel("Generations Available For fitting")

            # plot d
            ax1.plot(x0, d_list, 'b--', label='d')
            d_tuples = [i for i in zip(x0, d_list) if i[1] is not None]
            ax1.scatter(*zip(*d_tuples), alpha=0.5)
            ax1.plot(x0, deltaD_list, 'b-', label='delta d')
            ax1.set_ylabel('', color='b')
            for tl in ax1.get_yticklabels():
                tl.set_color('b')

            # plot deltaD
            deltaD_tuples = [
                i for i in zip(x0, deltaD_list) if i[1] is not None
            ]
            _x, _y = zip(*deltaD_tuples)
            ax1.fill_between(_x, 1e-6, _y, facecolor='blue', alpha=0.5)
            plt.legend(loc=2)

            # plot R0
            ax2 = ax1.twinx()
            # ax2.set_yscale('log')
            ax2.plot(x0, R0_list, 'r-', label='R0')
            R0_tuples = [i for i in zip(x0, R0_list) if i[1] is not None]
            ax2.scatter(*zip(*R0_tuples), color='red', alpha=0.5)
            ax2.set_ylabel('', color='r')
            for t2 in ax2.get_yticklabels():
                t2.set_color('r')
            plt.legend()

            if save:
                d = self.root + out
                Analysis.check_dir(d)
                fn = d + 'deltaD_%s.png' % (c.lower().replace(' ', '_'))
                plt.savefig(fn)
            else:
                plt.show()
示例#7
0
    def duration_and_size_projection(self, out='projected/', save=False):
        countries = self.countries + ['All Countries']
        data = {}
        for i, c in enumerate(countries):
            data[c] = {}

            # first reported cases assumed to have been reported in generation 5
            start_generation = 5

            # up-to-date data
            x, y = self.get_SI_series(c, fill_y=True)
            x = x + start_generation

            start_generation_index = 2
            generations = range(start_generation_index, len(x))
            I_total = [None] * len(x)
            t_max = [None] * len(x)
            for i in generations:
                _x = x[:i]
                _y = y[:i]
                R0, d = f.RMSD_fit(_x, _y)
                try:
                    I_total[i] = f.get_I_total(R0, d)
                except:
                    I_total[i] = None
                if I_total[i] > 1e6:
                    I_total[i] = None
                t_max[i] = f.get_t_max(R0, d)

            colors = ['red', 'blue', 'green']
            series = [t_max, I_total, y]
            labels = [
                'Projected Outbreak Duration', 'Projected Outbreak Size',
                'Cumulative Incidence'
            ]

            fig, ax1 = plt.subplots()
            ax1.set_xlabel('Generations Available for Fitting')
            ax2 = ax1.twinx()
            # ax2.set_ylim(0, 200000)

            ax3 = ax1.twinx()
            ax3.spines['right'].set_position(('outward', 55))
            plt.subplots_adjust(right=0.8)
            axes = [ax1, ax2, ax3]

            width = 1
            for i, s in enumerate(series):
                ax = axes[i]
                if i == 2:
                    ax.bar(x - width * 0.5,
                           s,
                           width,
                           color=colors[i],
                           alpha=0.3)
                else:
                    ax.plot(x, s, color=colors[i])

                    scatter_tuples = [(n, m) for n, m in zip(x, s)
                                      if m is not None]
                    xS, sS = zip(*scatter_tuples)
                    print xS, sS
                    ax.scatter(xS, sS, color=colors[i], alpha=0.5)
                ax.set_ylabel(labels[i], color=colors[i])
                for tl in ax.get_yticklabels():
                    tl.set_color(colors[i])

            # p.align_yaxis(ax1, 0, ax2, 0)
            if save:
                d = self.root + out
                Analysis.check_dir(d)
                fn = d + 'projected_size_and_duration_%s.png' % (
                    c.lower().replace(' ', '_'))
                plt.savefig(fn)
            else:
                plt.show()

            print c
            print 'I total:', I_total[-1]
            print 't max:', t_max[-1]