Beispiel #1
0
def _make_biophysical_uncertainty_table(uncertainty_results):
    table = html.Table(id='biophysical_uncertainty')
    table.add_two_level_header(outer_headers=[
        'Total carbon (Mg of carbon)',
        'Sequestered carbon (compared to current scenario)'
        '<br>(Mg of carbon)'
    ],
                               inner_headers=['Mean', 'Standard deviation'],
                               row_id_header='Scenario')

    for scenario in ['cur', 'fut', 'redd']:
        if scenario not in uncertainty_results:
            continue

        row = [_make_scenario_name(scenario, 'redd' in uncertainty_results)]
        row += uncertainty_results[scenario]

        if scenario == 'cur':
            row += ['n/a', 'n/a']
        else:
            row += uncertainty_results['sequest_%s' % scenario]

        table.add_row(row)

    return table
Beispiel #2
0
def _make_valuation_uncertainty_table(uncertainty_data):
    table = html.Table(id='valuation_uncertainty')

    table.add_two_level_header(outer_headers=[
        'Sequestered carbon (Mg of carbon)', 'Net present value (USD)'
    ],
                               inner_headers=['Mean', 'Standard Deviation'],
                               row_id_header='Scenario')

    for fut_type in ['fut', 'redd']:
        if fut_type not in uncertainty_data:
            continue

        scenario_data = uncertainty_data[fut_type]
        row = [_make_scenario_name(fut_type, 'redd' in uncertainty_data)]
        row += scenario_data['sequest']
        row += scenario_data['value']
        table.add_row(row)

    return table
Beispiel #3
0
def _make_outfile_table(args, biophysical_outputs, valuation_outputs,
                        html_uri):
    table = html.Table(id='outfile_table')
    table.add_row(['Filename', 'Description'], is_header=True)

    descriptions = collections.OrderedDict()

    if biophysical_outputs:
        descriptions.update(
            _make_biophysical_outfile_descriptions(biophysical_outputs, args))

    if valuation_outputs:
        descriptions.update(
            _make_valuation_outfile_descriptions(valuation_outputs))

    html_filename = os.path.basename(html_uri)
    descriptions[html_filename] = 'This summary file.'  # dude, that's so meta

    for filename, description in sorted(descriptions.items()):
        table.add_row([filename, description])

    return table
Beispiel #4
0
def _make_biophysical_table(biophysical_outputs):
    do_uncertainty = 'uncertainty' in biophysical_outputs

    table = html.Table(id='biophysical_table')
    headers = [
        'Scenario', 'Total carbon<br>(Mg of carbon)',
        'Sequestered carbon<br>(compared to current scenario)'
        '<br>(Mg of carbon)'
    ]

    table.add_row(headers, is_header=True)

    for scenario in ['cur', 'fut', 'redd']:
        total_carbon_key = 'tot_C_%s' % scenario
        if total_carbon_key not in biophysical_outputs:
            continue

        row = []
        row.append(
            _make_scenario_name(scenario, 'tot_C_redd' in biophysical_outputs))

        # Append total carbon.
        row.append(
            carbon_utils.sum_pixel_values_from_uri(
                biophysical_outputs[total_carbon_key]))

        # Append sequestration.
        sequest_key = 'sequest_%s' % scenario
        if sequest_key in biophysical_outputs:
            row.append(
                carbon_utils.sum_pixel_values_from_uri(
                    biophysical_outputs[sequest_key]))
        else:
            row.append('n/a')

        table.add_row(row)

    return table
def create_HTML_table(output_dir, args, cycle_history, sum_hrv_weight,
                      hrv_weight, farms_npv, value_history, histogram_paths,
                      uncertainty_stats):
    '''
    Inputs:
        output_dir: The directory in which we will be creating our .html file
            output.
        cycle_history: dictionary mapping farm ID->list of tuples, each of
            which contains 3 things- (day of outplanting, day of harvest,
                harvest weight of a single fish in grams)
        sum_hrv_weight: dictionary which holds a mapping from farm ID->total
            processed weight of each farm
        hrv_weight: dictionary which holds a farm->list mapping, where the list
            holds the individual tpw for all cycles that the farm completed
        do_valuation: boolean variable that says whether or not valuation is
            desired
        farms_npv: dictionary with a farm-> float mapping, where each float is
            the net processed value of the fish processed on that farm, in
            $1000s of dollars.
        value_history: dictionary which holds a farm->list mapping, where the
            list holds tuples containing (Net Revenue, Net Present Value) for
            each cycle completed by that farm

    Output:
        HTML file: contains 3 tables that summarize inputs and outputs for the
            duration of the model.
            - Input Table: Farm Operations provided data, including Farm ID #,
                Cycle Number, weight of fish at start, weight of fish at
                harvest, number of fish in farm, start day for growing, and
                length of fallowing period
            - Output Table 1: Farm Harvesting data, including a summary table
                for each harvest cycle of each farm. Will show Farm ID, cycle
                number, days since outplanting date, harvested weight, net
                revenue, outplant day, and year.
            - Output Table 2: Model outputs for each farm, including Farm ID,
                net present value, number of completed harvest cycles, and
                total volume harvested.

        Returns nothing.
    '''
    html_uri = os.path.join(output_dir,
                            ("Harvest_Results_[%s].html" %
                             datetime.datetime.now().strftime(
                                 "%Y-%m-%d_%H_%M")))
    doc = html.HTMLDocument(html_uri, 'Marine InVEST',
                            'Aquaculture Model (Finfish Harvest)')

    doc.write_paragraph(
        'This page contains results from running the Marine InVEST Finfish '
        'Aquaculture model.')

    doc.insert_table_of_contents()

    doc.write_header('Farm Operations (input)')

    ops_table = doc.add(html.Table(id='farm_ops_table'))
    ops_table.add_row(['Farm ID Number',
                       'Weight of Fish at Start (kg)',
                       'Weight of Fish at Harvest (kg)',
                       'Number of Fish in Farm',
                       'Start Day for Growing (1-365)',
                       'Length of Fallowing Period (days)'
                       ],
                      is_header=True)

    for farm_id in cycle_history:
        farm_id = str(farm_id)
        cells = [farm_id]
        for column_key in ['weight of fish at start (kg)',
                           'target weight of fish at harvest (kg)',
                           'number of fish in farm',
                           'start day for growing',
                           'Length of Fallowing period']:
            cells.append(args['farm_op_dict'][farm_id][column_key])
        ops_table.add_row(cells, do_formatting=False)

    doc.write_header('Farm Harvesting (output)')
    harvest_table = doc.add(html.Table(id='harvest_table'))
    harvest_table.add_row([
        'Farm ID Number', 'Cycle Number',
        'Days Since Outplanting Date (Including Fallowing Period)',
        'Length of Given Cycle',
        'Harvested Weight After Processing (kg/cycle)',
        'Net Revenue (Thousands of $)',
        'Net Present Value (Thousands of $)',
        'Outplant Day (Julian Day)',
        'Outplant Year'],
        is_header=True)

    for farm_id in cycle_history:
        for cycle in range(0, len(cycle_history[farm_id])):
            cycle_num = cycle + 1

            curr_cycle = cycle_history[farm_id][cycle]
            outplant_date, harvest_date, harvest_weight = curr_cycle
            cycle_length = harvest_date - outplant_date

            # Want to get the processed weight on a farm for a given cycle. All
            # of the PW for all cycles should add to the third table's TPW.
            harvest_weight = hrv_weight[farm_id][cycle]

            out_day = outplant_date % 365
            out_year = outplant_date // 365 + 1

            if args['do_valuation']:
                # Revenue and NPV should be in thousands of dollars.
                indiv_rev, indiv_npv = value_history[farm_id][cycle]
                indiv_rev /= 1000.0
                indiv_npv /= 1000.0
            else:
                indiv_rev, indiv_npv = '(no valuation)', '(no valuation)'

            cells = [farm_id, cycle_num, harvest_date, cycle_length,
                     harvest_weight, indiv_rev, indiv_npv, out_day, out_year]
            harvest_table.add_row(cells)

    doc.write_header('Farm Result Totals (output)')

    doc.write_paragraph(
        'All values in the following table were also populated in the '
        'attribute table of the netpens feature class.')

    totals_table = doc.add(html.Table(id='totals_table'))
    totals_table.add_row([
        'Farm ID Number',
        'Net Present Value (Thousands of $) (For Duration of Model Run)',
        'Number of Completed Harvest Cycles',
        'Total Volume Harvested (kg)(After Processing Occurs)'],
        is_header=True)

    for farm_id in cycle_history:
        if args['do_valuation']:
            npv = round(farms_npv[farm_id], 4)
        else:
            npv = '(no valuation)'

        num_cy_complete = len(cycle_history[farm_id])
        total_harvested = round(sum_hrv_weight[farm_id], 4)

        cells = [farm_id, npv, num_cy_complete, total_harvested]
        totals_table.add_row(cells)

    if histogram_paths:
        doc.write_header('Uncertainty Analysis Results')

        doc.write_paragraph(
            'These results were obtained by running a Monte Carlo simulation. '
            'For each run of the simulation, each growth parameter was '
            'randomly sampled according to the provided normal distribution. '
            'The simulation involved %d runs of the model, each with '
            'different values for the growth '
            'parameters.' % args['num_monte_carlo_runs'])

        doc.write_paragraph(
            'Results labeled as <i>total results (all farms)</i> '
            'were calculated by, for each run, summing the harvested weights '
            'and the net present values of all farms for that run.')

        # Write a table with numerical results.
        doc.write_header('Numerical Results', level=3)
        doc.write_paragraph(
            'This table summarizes the mean and standard deviation for '
            'total harvested weight (after processing) and for total '
            'net present value. The mean and standard deviation were '
            'computed for results across all runs of the Monte Carlo '
            'simulation.')

        # Add a table with uncertainty stats (mean and standard deviation).
        uncertainty_table = doc.add(html.Table(id='uncertainty_table'))
        uncertainty_table.add_two_level_header(
            outer_headers=['Harvested weight after processing (kg)',
                           'Net present value (thousands of USD)',
                           'Number of cycles'],
            inner_headers=['Mean', 'Standard deviation'],
            row_id_header='Farm ID')

        for farm in uncertainty_stats:
            row = []
            if farm == 'total':
                row.append('Total (all farms)')
            else:
                row.append('Farm %s' % farm)
            for result_type in ['weight', 'value', 'cycles']:
                # Append the mean and the standard deviation to the row.
                row += uncertainty_stats[farm][result_type]
            uncertainty_table.add_row(row)

        # Add histograms.
        doc.write_header('Histograms', level=3)
        doc.write_paragraph(
            'The following histograms display the probability of different '
            'outcomes. The height of each vertical bar in the histograms '
            'represents the probability of the outcome marked by the position'
            'of the bar on the horizontal axis of the histogram.')
        doc.write_paragraph(
            'Included are histograms for total results across all farms, as '
            'well as results for each individual farm.')
        for farm, paths in histogram_paths.items():
            if farm == 'total':
                title = 'Histograms for total results (all farms)'
            else:
                title = 'Histograms for farm %s' % farm
            doc.write_header(title, level=4)

            # Put the histograms in a collapsible element that defaults to
            # open.
            collapsible_elem = doc.add(html.Element('details', open=''))
            for path in paths.values():
                collapsible_elem.add(
                    html.Element('img', src=path, end_tag=False))

    doc.flush()
Beispiel #6
0
def _make_valuation_tables(valuation_outputs):
    scenario_results = {}
    change_table = html.Table(id='change_table')
    change_table.add_row([
        "Scenario", "Sequestered carbon<br>(Mg of carbon)",
        "Net present value<br>(USD)"
    ],
                         is_header=True)

    for scenario_type in ['base', 'redd']:
        try:
            sequest_uri = valuation_outputs['sequest_%s' % scenario_type]
        except KeyError:
            # We may not be doing REDD analysis.
            continue

        scenario_name = _make_scenario_name(
            scenario_type, 'sequest_redd' in valuation_outputs)

        total_seq = carbon_utils.sum_pixel_values_from_uri(sequest_uri)
        total_val = carbon_utils.sum_pixel_values_from_uri(
            valuation_outputs['%s_val' % scenario_type])
        scenario_results[scenario_type] = (total_seq, total_val)
        change_table.add_row([scenario_name, total_seq, total_val])

        try:
            seq_mask_uri = valuation_outputs['%s_seq_mask' % scenario_type]
            val_mask_uri = valuation_outputs['%s_val_mask' % scenario_type]
        except KeyError:
            # We may not have confidence-masking data.
            continue

        # Compute output for confidence-masked data.
        masked_seq = carbon_utils.sum_pixel_values_from_uri(seq_mask_uri)
        masked_val = carbon_utils.sum_pixel_values_from_uri(val_mask_uri)
        scenario_results['%s_mask' % scenario_type] = (masked_seq, masked_val)
        change_table.add_row([
            '%s (confident cells only)' % scenario_name, masked_seq, masked_val
        ])

    yield change_table

    # If REDD scenario analysis is enabled, write the table
    # comparing the baseline and REDD scenarios.
    if 'base' in scenario_results and 'redd' in scenario_results:
        comparison_table = html.Table(id='comparison_table')
        comparison_table.add_row([
            "Scenario Comparison",
            "Difference in carbon stocks<br>(Mg of carbon)",
            "Difference in net present value<br>(USD)"
        ],
                                 is_header=True)

        # Add a row with the difference in carbon and in value.
        base_results = scenario_results['base']
        redd_results = scenario_results['redd']
        comparison_table.add_row([
            '%s vs %s' %
            (_make_scenario_name('redd'), _make_scenario_name('base')),
            redd_results[0] - base_results[0],
            redd_results[1] - base_results[1]
        ])

        if 'base_mask' in scenario_results and 'redd_mask' in scenario_results:
            # Add a row with the difference in carbon and in value for the
            # uncertainty-masked scenario.
            base_mask_results = scenario_results['base_mask']
            redd_mask_results = scenario_results['redd_mask']
            comparison_table.add_row([
                '%s vs %s (confident cells only)' %
                (_make_scenario_name('redd'), _make_scenario_name('base')),
                redd_mask_results[0] - base_mask_results[0],
                redd_mask_results[1] - base_mask_results[1]
            ])

        yield comparison_table