コード例 #1
0
ファイル: rate_view.py プロジェクト: flaxter/gbd
def rate_plot(request, path, format='png'):
    """
    use matplotlib plotting functions to render transparent
    rectangles on the current figure representing each
    piece of RateData that will be used in fitting this
    age-specific rate function

    path is the format param_value-param2_value2-..., where
    each param is from the list
      [disease, rate, region, sex, limit]
    values for region and disease are the id of the object,
    limit is an int upper-bound on number of rates to plot,
    and all the other options will do partial name matching
    """
    filter_params = {'limit': '100'}
    for p in path.split('-'):
        param, value = p.split('_')
        if param == 'disease':
            filter_params['disease__id'] = int(value)
        elif param == 'rate':
            filter_params['rate_type__contains'] = value
        elif param == 'region':
            filter_params['region__id'] = int(value)
        elif param == 'sex':
            filter_params['sex'] = value
        elif param == 'limit':
            filter_params['limit'] = value
        else:
            raise KeyError
            
    limit = int(filter_params.pop('limit'))
    rates = Rate.objects.filter(**filter_params)

    view_utils.clear_plot()
    for r in rates[:limit]:
        rate_val = float(r.numerator)/float(r.denominator)
        x_jitter = np.random.rand()
        y_jitter = 0.
        if r.sex == 'male':
            color = (0.,0.,.8)
        elif r.sex == 'female':
            color = (.8,0.,0.)
        else:
            color = (0.,.8,0.)
            
        text_color = 'black'
        alpha=.65
        pl.plot(np.array([r.age_start, r.age_end+1.])+x_jitter, 
                np.array([rate_val,rate_val])+y_jitter,
                color=color, alpha=alpha, linewidth=5,)
        pl.text(r.age_end+x_jitter, rate_val+y_jitter,
                "n=%d" % r.denominator,
                color=text_color, alpha=alpha, fontsize=6)
    view_utils.label_plot(path.replace('-', ', ').replace('_', ': '))

    
    return HttpResponse(view_utils.figure_data(format),
                        view_utils.MIMETYPE[format])
コード例 #2
0
ファイル: disease_model_view.py プロジェクト: flaxter/gbd
def disease_model_sparkplot(request, id, format):
    dm = get_object_or_404(DiseaseModel, id=id)

    width = 1
    height = .5
    
    fig = view_utils.clear_plot(width,height)

    ax = None
    for ii, rf in enumerate(dm.rates.all()):
        ax = pl.axes([ii/4., 0., 1., (ii+1)/4.], frameon=False)
        pl.subplot(2, 2, ii + 1)
        #pl.subplot(4, 1, ii+1, frameon=False)
        #plot_map_fit(rf)
        plot_mcmc_fit(rf)
        #plot_truth(rf)
        #plot_prior(rf)
        pl.xticks([])
        pl.yticks([])
        #pl.delaxes()
    pl.subplots_adjust(left=0, bottom=0, right=1, top=1,
                    wspace=0, hspace=0)

    return HttpResponse(view_utils.figure_data(format),
                        view_utils.MIMETYPE[format])
コード例 #3
0
def age_specific_rate_function_sparkplot(request, id_str, format='png'):
    asrfs = view_utils.id_str_to_objects(id_str, AgeSpecificRateFunction)

    width = 1
    height = .5
    
    fig = view_utils.clear_plot(width,height)
    pl.subplot(1, 1, 1, frameon=False)
    ax = None
    for ii, rf in enumerate(asrfs):
        plot_truth(rf)
        plot_mcmc_fit(rf)
        #plot_fit(rf, 'mcmc_mean', color='#000000')

        #plot_map_fit(rf)
        #plot_mcmc_fit(rf)
        #plot_prior(rf)
        pl.xticks([])
        pl.yticks([])
        #pl.delaxes()
    pl.subplots_adjust(left=-.1, bottom=0, right=1, top=1.1,
                    wspace=0, hspace=0)

    return HttpResponse(view_utils.figure_data(format),
                        view_utils.MIMETYPE[format])
コード例 #4
0
ファイル: population_view.py プロジェクト: flaxter/gbd
def population_plot(request, id, format):
    pop = get_object_or_404(Population, pk=id)

    M,C = pop.gaussian_process()
    x = np.arange(0.,100.,1.)

    # handle json & csv formats, which are not technically a plot
    if format in ['json', 'csv']:
        if format == 'json':
            data_str = pop.data_json
        elif format == 'csv':
            headings = ['Age (years)', 'Population (thousands)']
            rows = [[a, p] for a,p in zip(pop.data['mesh'], pop.data['vals'])]
            data_str = view_utils.csv_str(headings, rows)
        return HttpResponse(data_str, view_utils.MIMETYPE[format])
        

    view_utils.clear_plot()
    pl.plot(x,np.maximum(0.0,M(x)))
    view_utils.label_plot("%s, %d, %s" % (pop.country, pop.year, pop.sex))
    pl.ylabel('Population (thousands)')
    
    return HttpResponse(view_utils.figure_data(format),
                        view_utils.MIMETYPE[format])
コード例 #5
0
def asrf_posterior_predictive_check(request, id, format, style):
    """
    generate a posterior predictive check of the model's
    'goodness-of-fit', which is to say a scatterplot of the observed
    rates versus the rates predicted by the model parameters
    """
    rf = get_object_or_404(AgeSpecificRateFunction, id=id)


    params = {'fontsize': 7}
    #params.update(url_params_to_dict(param_str))

    fig= view_utils.clear_plot(width=4,height=3)
    ax = fig.add_subplot(111)
    
    
    if rf.rates.count() == 0 or not rf.fit.has_key('mcmc_mean'):
        pl.figtext(.2, .5, 'Rates or MCMC Fit not found')
    
        return HttpResponse(view_utils.figure_data(format),
                            view_utils.MIMETYPE[format])
    
    observed_rates = np.array([ float(rate.numerator)/float(rate.denominator) for rate in rf.rates.all() ])
    observed_cls = np.transpose([ rate.ci() for rate in rf.rates.all() ])

    predicted_rates = np.array([ probabilistic_utils.predict_rate_from_asrf(rf, rate) for rate in rf.rates.all() ])
    predicted_cls = np.array([ [ probabilistic_utils.predict_rate_from_asrf(rf, rate, 'mcmc_lower_cl') for rate in rf.rates.all() ],
                                [ probabilistic_utils.predict_rate_from_asrf(rf, rate, 'mcmc_upper_cl') for rate in rf.rates.all() ] ])

    max_x = np.max(observed_cls[1,:])
    max_y = np.max(predicted_cls[1,:])
    max_t = max(max_x, max_y, probabilistic_utils.NEARLY_ZERO)

    observed_errs = np.abs(observed_cls - observed_rates)
    predicted_errs = np.abs(predicted_cls - predicted_rates)

    if style == 'scatter':
        pl.plot([probabilistic_utils.NEARLY_ZERO,1.], [probabilistic_utils.NEARLY_ZERO,1.], linestyle='dashed', linewidth=2, color='black', alpha=.75)
        #pl.errorbar(x=observed_rates + np.random.rand(len(observed_rates)) * max_t / 250., xerr=observed_errs,
        #            y=predicted_rates + np.random.rand(len(predicted_rates)) * max_t / 250., yerr=predicted_errs, fmt='bo')
        pl.plot(observed_rates, predicted_rates, '.', alpha=1.)

        from matplotlib.patches import Ellipse
        
        for ii in range(len(observed_rates)):
            e = Ellipse(xy=[np.mean(observed_cls[:,ii]),np.mean(predicted_cls[:,ii])],
                        width=(observed_cls[1,ii] - observed_cls[0,ii]),
                        height=(predicted_cls[1,ii] - predicted_cls[0,ii]),
                        alpha=.05)
            if same_side_of_xy(observed_cls[:,ii], predicted_cls[:,ii]):
                e.set_facecolor((1.,0.,0.))
            ax.add_artist(e)
            
            
        
        pl.axis([-.001, max_t + .001, -.001, max_t + .001])

        tick_list, tick_objs = pl.xticks()
        pl.xticks([0,tick_list[-1]], **params)
        pl.xlabel('Observed Rates', **params)

        tick_list, tick_objs = pl.yticks()
        pl.yticks([0,tick_list[-1]], **params)
        pl.ylabel('Predicted Rates', **params)
    elif style == 'intervals':
        rate_list = rf.rates.all()

        pl.subplot(2,1,1)
        plot_intervals(rf, rate_list, color='green', **params)
        pl.axis([0., 100., 0., max_t])
        pl.xticks([])
        pl.yticks([])
        pl.xlabel('')
        pl.ylabel('')

        for x,r in zip(predicted_rates, rate_list):
            r.numerator = x * r.denominator

        pl.subplot(2,1,2)
        plot_intervals(rf, rate_list, color='red', **params)
        pl.axis([0., 100., 0., max_t])
        pl.xticks([])
        pl.yticks([])
        pl.ylabel('')
        pl.title('')
        
        pl.subplot(2,1,1)
    else:
        raise Exception('ERROR: plot style "%s" unrecognized' % style)
    pl.title('Predictive Check for %s (id=%d)' % (rf, rf.id), **params)
    
    return HttpResponse(view_utils.figure_data(format),
                        view_utils.MIMETYPE[format])
コード例 #6
0
def age_specific_rate_function_compare(request, id_str, format='html'):
    """
    display information for comparing multiple age specific rate functions
    
    id_str is a comma separate list of asrf ids, and format can be html
    or a graphics format that is recognized by matplotlib
    """
    asrfs = view_utils.id_str_to_objects(id_str, AgeSpecificRateFunction)
    if format == 'html':
        return render_to_response('age_specific_rate_function/compare.html', {'id_str': id_str, 'asrfs': asrfs})

    size = request.GET.get('size', default='normal')
    style = request.GET.get('style', default='overlay')

    if size == 'small':
        width = 3
        height = 2
    elif size == 'full_page':
        width = 11
        height = 8.5
    else:
        width = 6
        height = 4

    max_rate = .0001

    view_utils.clear_plot(width=width,height=height)
    try:
        if style == 'overlay':
            for ii, rf in enumerate(asrfs):
                plot_fit(rf, 'map', alpha=.75, linewidth=5, label='asrf %d'%rf.id)
                max_rate = np.max([max_rate] + rf.fit['map'])
            pl.axis([0, 100, 0, 1.25*max_rate])

        elif style == 'scatter':
            x, y = [ [ asrfs[ii].fit[fit_type] for fit_type in ['mcmc_mean', 'mcmc_lower_cl', 'mcmc_upper_cl'] ] for ii in [0,1] ]

            max_x = np.max(x[2])
            max_y = np.max(y[2])
            max_t = max(max_x, max_y, .00001)

            xerr = np.abs(np.array(x[1:]) - np.array(x[0]))
            yerr = np.abs(np.array(y[1:]) - np.array(y[0]))

            pl.plot([probabilistic_utils.NEARLY_ZERO,1.], [probabilistic_utils.NEARLY_ZERO,1.], linestyle='dashed', linewidth=2, color='black', alpha=.75)
            pl.errorbar(x=x[0], xerr=xerr, y=y[0], yerr=yerr, fmt='bo')
            pl.axis([0,max_t,0,max_t])

        elif style == 'stack':
            n = asrfs.count()
            max_t = probabilistic_utils.NEARLY_ZERO
            for ii in range(n):
                x = asrfs[ii].fit['mcmc_mean']
                max_t = max(np.max(x), max_t)

                pl.subplot(n, 1, ii+1, frameon=False)
                pl.plot(x, linewidth=3)
                if size != 'small':
                    pl.title(asrfs[ii])
                pl.axis([0, 100, 0, max_t*1.1])
                pl.xticks([])
                pl.yticks([])
            pl.subplots_adjust(left=0, right=1)

        elif style == 'parallel':
            for xx in zip(*[ rf.fit['mcmc_mean'] for rf in asrfs ]):
                pl.plot(xx, linewidth=2, color='blue', alpha=.5)
            xmin, xmax, ymin, ymax = pl.axis()
            pl.vlines(range(len(asrfs)), ymin, ymax, color='black', linestyles='dashed',
                      alpha=.5, linewidth=2)
            pl.xticks(range(len(asrfs)), [ 'asrf %d' % rf.id for rf in asrfs ])
    except KeyError:
        pass
        #pl.figtext(0.4,0.2, 'No MCMC Fit Found')

    if size == 'small':
        pl.xticks([])
        pl.yticks([])
    else:
        if style != 'stack' and style != 'parallel':
            pl.legend()
            view_utils.label_plot('Comparison of Age-Specific Rate Functions')

    return HttpResponse(view_utils.figure_data(format),
                        view_utils.MIMETYPE[format])
コード例 #7
0
def age_specific_rate_function_show(request, id_str, format='html'):
    asrfs = view_utils.id_str_to_objects(id_str, AgeSpecificRateFunction)

    if format == 'html':
        return render_to_response('age_specific_rate_function/show.html',
                                  view_utils.template_params(asrfs[0], asrfs=asrfs, id_str=id_str, query_str=request.META['QUERY_STRING']))

    # handle json & csv formats
    if format in ['json', 'csv']:
        if format == 'json':
            data_str = json.dumps([[rf.id, rf.fit] for rf in asrfs])
        elif format == 'csv':
            headings = {}
            rows = {}
            data_str = ''

            for rf in asrfs:
                headings[rf] = ['Age (years)', 'MAP Rate (per 1.0)']
                rows[rf] = [[a, p] for a,p in zip(rf.fit['out_age_mesh'], rf.fit['map'])]
                data_str += view_utils.csv_str(headings[rf], rows[rf])
        return HttpResponse(data_str, view_utils.MIMETYPE[format])

    # handle graphics formats
    cnt = asrfs.count()
    cols = 2
    rows = int(np.ceil(float(cnt) / float(cols)))

    subplot_width = 6
    subplot_height = 4
    
    view_utils.clear_plot(width=subplot_width*cols,height=subplot_height*rows)
    for ii, rf in enumerate(asrfs):
        pl.subplot(rows,cols,ii+1)
        if request.GET.get('bars'):
            bars_mcmc_fit(rf)
            #plot_map_fit(rf, alpha=.3)
            plot_truth(rf)
        else:
            plot_intervals(rf, rf.rates.all(), fontsize=12, alpha=.5)
            plot_intervals(rf, rf.rates.filter(params_json__contains='Rural'), fontsize=12, alpha=.5, color='brown')
            #plot_normal_approx(rf)
            plot_map_fit(rf)
            plot_mcmc_fit(rf)
            plot_truth(rf)
            plot_prior(rf)
            pl.text(0,0,rf.fit.get('priors',''), color='black', family='monospace', alpha=.75)
        view_utils.label_plot('%s (id=%d)' % (rf, rf.id), fontsize=10)
        
        max_rate = np.max([.0001] + [r.rate for r in rf.rates.all()] + rf.fit.get('mcmc_upper_cl', []))
        xmin = float(request.GET.get('xmin', default=0.))
        xmax = float(request.GET.get('xmax', default=100.))
        ymin = float(request.GET.get('ymin', default=0.))
        ymax = float(request.GET.get('ymax', default=1.25*max_rate))
        pl.axis([xmin, xmax, ymin, ymax])

        if ii % cols != 0:
            pl.ylabel('')
        #pl.yticks([])
        if (ii + cols) < cnt:
            pl.xlabel('')
        #pl.xticks([])
    
    
    return HttpResponse(view_utils.figure_data(format),
                        view_utils.MIMETYPE[format])