def get_balance_statistics_var_names(user_map):
    means = get_balance_statistics_means(user_map)
    ordered_unique = OrderedDict.fromkeys([mean.name for mean in means])
    return [
        layer_services.legible_covariate_names(var)
        for var in list(ordered_unique)
    ]
 def get_display_name(self, obj):
     return legible_covariate_names(obj.name)
def create_report(request, bbox, width, height):
    """
    Create PDF that displays the results of the previous job that was run by the user
    :param request:
    :return:
    """
    user_map = Map.objects.get(pk=request.session['mid'])
    job = user_map.job_set.most_recent(user_map)
    if job:
        job = job[0]
        job_stats = job.jobstats_set.get()
        report_generated_time = DateFormat(now()).format('jS F Y H:i e')

        response = HttpResponse(content_type='application/pdf')
        report_filename = 'FCET_{region}_{report_generated_time}'.format(
            region=job_stats.state,
            report_generated_time=report_generated_time.replace(' ', '_'))
        response[
            'Content-Disposition'] = 'attachment; filename={filename}.pdf'.format(
                filename=report_filename)
        results_chart = table_services.get_results_chart_as_dict(user_map)
        results_data = table_services.get_results_table_as_list(user_map)

        fields = {
            'country':
            job_stats.country.encode('utf8'),
            'region_type':
            job_stats.region_type,
            'state':
            job_stats.state,
            'start_year':
            job.low_outcome_year,
            'end_year':
            job.high_outcome_year,
            'min_forest_cover':
            job_stats.min_forest_cover,
            'max_forest_cover':
            job_stats.max_forest_cover,
            'agroforest':
            job_stats.agroforest,
            'agriculture':
            job_stats.agriculture,
            'forest':
            job_stats.forest,
            'treatment_area_option':
            job_stats.treatment_area_option,
            'control_area_option':
            job_stats.control_area_option,
            'matching_method':
            job.matching_method,
            'matching_estimator':
            job.matching_estimator,
            'covariates': [
                layer_services.legible_covariate_names(cov)
                for cov in job.covariate_variables.split(',')
            ],
            'caliper':
            job.caliper_distance,
            'common_support':
            job.common_support,
            'standard_errors':
            job.standard_error_type,
            'control_mean':
            results_chart['Unmatched Control'],
            'match_mean':
            results_chart['Matched Control'],
            'treated_mean':
            results_chart['Treatment'],
            'att':
            results_chart['ATT'],
            'results_data':
            results_data,
            'summary_statistics_data':
            table_services.get_summary_statistics_table(user_map),
            'balance_statistics_data':
            table_services.get_balance_statistics_table(user_map),
            'balance_statistics_means_unmatched':
            table_services.get_balance_statistics_means_unmatched(user_map),
            'balance_statistics_means_matched':
            table_services.get_balance_statistics_means_matched(user_map),
            'balance_statistics_var_names':
            table_services.get_balance_statistics_var_names(user_map),
            'session_start_time':
            DateFormat(job_stats.session_start).format('jS F Y H:i e'),
            'report_generated_time':
            report_generated_time,
            'map_url':
            geoserver_map(bbox, width, height, user_map)
        }
        generate_results_report(response, fields)
        pdf_location = os.path.join(
            BASE_DIR, 'reports', report_filename + '_' + str(job.id) + '.pdf')
        # with open(pdf_location, 'w+') as out_file:
        #     out_file.write(response.content)
        #     out_file.close()
        return response
    else:
        return HttpResponse(
            'Please run a match before attempting to generate a report',
            status=400)
def get_balance_statistics_table(user_map):
    cbs_means = CBSmeans.objects.get_table(user_map)
    return [[
        layer_services.legible_covariate_names(row.name), row.sample,
        row.treated, row.control, row.bias, row.biasr, row.t, row.pt
    ] for row in cbs_means]