Example #1
0
def disease_model_show(request, id, format='html'):
    dm = get_object_or_404(DiseaseModel, id=id)

    if format == 'html':
        return render_to_response('disease_model/show.html',
                                  view_utils.template_params(dm, id_str=dm.get_asrf_id_str(), asrfs=dm.rates.all()))

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

            for rf in dm.rates.all():
                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])

    else:
        raise Http404
Example #2
0
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])
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])