Пример #1
0
    def test_generate_report_csv_style(self):
        """Reporting: testing full report w/ csv table data and css file."""
        from natcap.invest import reporting

        workspace_dir = self.workspace_dir
        args = ReportingRegressionTests.generate_base_args()

        csv_path = os.path.join(REGRESSION_DATA, 'sample_input',
                                'sample_csv.csv')
        style_path = os.path.join(REGRESSION_DATA, 'sample_input',
                                  'sample_style.css')

        args['out_uri'] = os.path.join(workspace_dir, 'report_csv_style.html')
        args['elements'][0]['attributes'] = {'class': 'my_class'}
        args['elements'][0]['data_type'] = 'csv'
        args['elements'][0]['data'] = csv_path
        args['elements'][0]['key'] = 'ws_id'
        args['elements'][1]['format'] = 'style'
        args['elements'][1]['data_src'] = style_path
        args['elements'][1]['input_type'] = 'File'

        reporting.generate_report(args)

        pygeoprocessing.testing.assert_text_equal(
            args['out_uri'],
            os.path.join(REGRESSION_DATA, 'html_reports',
                         'report_csv_style.html'))
Пример #2
0
    def test_generate_report_shape_json(self):
        """Reporting: testing full report w/ shape table data and json file."""
        from natcap.invest import reporting

        workspace_dir = self.workspace_dir
        workspace_dir = 'C:/Users/dmf/projects/invest_dev/py36_compatibility/reporting'
        args = ReportingRegressionTests.generate_base_args()

        shape_path = os.path.join(REGRESSION_DATA, 'sample_input',
                                  'sample_shape.shp')
        json_data = "{'key': 0, 'data': {'door' : 1, 'room': 'kitchen'}}"

        args['out_uri'] = os.path.join(workspace_dir, 'report_shape_json.html')
        args['elements'][0]['data_type'] = 'shapefile'
        args['elements'][0]['data'] = shape_path
        args['elements'][0]['key'] = 'ws_id'
        args['elements'][1]['format'] = 'json'
        args['elements'][1]['data_src'] = json_data
        args['elements'][1]['input_type'] = 'Text'

        reporting.generate_report(args)
        print(args['out_uri'])
        pygeoprocessing.testing.assert_text_equal(
            args['out_uri'],
            os.path.join(REGRESSION_DATA, 'html_reports',
                         'report_shape_json.html'))
Пример #3
0
    def test_generate_report_dict_script(self):
        """Reporting: test full report w/ dict table data and script file."""
        from natcap.invest import reporting

        workspace_dir = self.workspace_dir
        args = ReportingRegressionTests.generate_base_args()

        script_path = os.path.join(
            REGRESSION_DATA, 'sample_input', 'sample_script.js')

        dict_list = [
            {'ws_id': 0, 'num_pixels': 47017.0, 'wyield_vol': 50390640.85},
            {'ws_id': 1, 'num_pixels': 93339.0, 'wyield_vol': 103843576.83},
            {'ws_id': 2, 'num_pixels': 20977.0, 'wyield_vol': 21336791.14}]

        args['out_uri'] = os.path.join(
            workspace_dir, 'report_dict_script.html')
        args['elements'][0]['attributes'] = {'id': 'my_id'}
        args['elements'][0]['data_type'] = 'dictionary'
        args['elements'][0]['data'] = dict_list
        args['elements'][1]['format'] = 'script'
        args['elements'][1]['data_src'] = script_path
        args['elements'][1]['input_type'] = 'File'

        reporting.generate_report(args)

        pygeoprocessing.testing.assert_text_equal(
            args['out_uri'],
            os.path.join(
                REGRESSION_DATA, 'html_reports', 'report_dict_script.html'))
Пример #4
0
    def test_generate_report_remove_output(self):
        """Reporting: test full report removes html output if exists."""
        from natcap.invest import reporting

        workspace_dir = self.workspace_dir
        args = ReportingRegressionTests.generate_base_args()

        script_path = os.path.join(REGRESSION_DATA, 'sample_input',
                                   'sample_script.js')

        dict_list = [{
            'ws_id': 0,
            'num_pixels': 47017.0,
            'wyield_vol': 50390640.85
        }, {
            'ws_id': 1,
            'num_pixels': 93339.0,
            'wyield_vol': 103843576.83
        }, {
            'ws_id': 2,
            'num_pixels': 20977.0,
            'wyield_vol': 21336791.14
        }]

        args['out_uri'] = os.path.join(workspace_dir,
                                       'report_test_remove.html')
        args['elements'][0]['attributes'] = {'id': 'my_id'}
        args['elements'][0]['data_type'] = 'dictionary'
        args['elements'][0]['data'] = dict_list
        args['elements'][1]['format'] = 'script'
        args['elements'][1]['data_src'] = script_path
        args['elements'][1]['input_type'] = 'File'

        reporting.generate_report(args)
        # Run again to make sure output file that was created is removed
        reporting.generate_report(args)

        regression_path = os.path.join(REGRESSION_DATA, 'html_reports',
                                       'report_dict_script.html')
        for source_line, regression_line in itertools.zip_longest(
                open(args['out_uri']).readlines(),
                open(regression_path).readlines(),
                fillvalue=None):
            if source_line is None or regression_line is None:
                raise AssertionError('Number of lines are unequal.')

            # Strip trailing newlines.
            self.assertEqual(source_line.rstrip(), regression_line.rstrip())
Пример #5
0
def _create_results_html(vars_dict):
    '''
    Creates an HTML file that contains a summary of all harvest totals
    for each subregion.
    '''
    do_batch = vars_dict['do_batch']
    if do_batch is True:
        basename = os.path.splitext(
            os.path.basename(vars_dict['population_csv_uri']))[0]
        filename = 'results_page_' + basename + '.html'
    elif vars_dict['results_suffix'] is not '':
        filename = 'results_page_' + vars_dict['results_suffix'] + '.html'
    else:
        filename = 'results_page.html'
    uri = os.path.join(vars_dict['output_dir'], filename)

    recruitment_type = vars_dict['recruitment_type']
    Spawners_t = vars_dict['Spawners_t']
    H_tx = vars_dict['H_tx']
    V_tx = vars_dict['V_tx']
    equilibrate_timestep = int(vars_dict['equilibrate_timestep'])
    Regions = vars_dict['Regions']

    # Set Reporting Arguments
    rep_args = {}
    rep_args['title'] = "Fisheries Results Page"
    rep_args['out_uri'] = uri
    rep_args['sortable'] = True  # JS Functionality
    rep_args['totals'] = True  # JS Functionality

    total_timesteps = len(H_tx)

    # Create Model Run Overview Table
    overview_columns = [{
        'name': 'Attribute',
        'total': False
    }, {
        'name': 'Value',
        'total': False
    }]

    overview_body = [
        {
            'Attribute': 'Model Type',
            'Value': vars_dict['population_type']
        },
        {
            'Attribute': 'Recruitment Type',
            'Value': vars_dict['recruitment_type']
        },
        {
            'Attribute': 'Sex-Specific?',
            'Value': ('Yes' if vars_dict['sexsp'] == 2 else 'No')
        },
        {
            'Attribute': 'Classes',
            'Value': str(len(vars_dict['Classes']))
        },
        {
            'Attribute': 'Regions',
            'Value': str(len(Regions))
        },
    ]

    # Create Final Cycle Harvest Summary Table
    final_cycle_columns = [{
        'name': 'Subregion',
        'total': False
    }, {
        'name': 'Harvest',
        'total': True
    }]

    if vars_dict['val_cont']:
        final_cycle_columns.append({'name': 'Valuation', 'total': True})

    final_timestep_body = []
    for i in range(0, len(H_tx[-1])):  # i is a cycle
        sub_dict = {}
        sub_dict['Subregion'] = Regions[i]
        sub_dict['Harvest'] = "%.2f" % H_tx[-1, i]
        if vars_dict['val_cont']:
            sub_dict['Valuation'] = "%.2f" % V_tx[-1, i]
        final_timestep_body.append(sub_dict)

    # Create Harvest Time Step Table
    timestep_breakdown_columns = [{
        'name': 'Time Step',
        'total': False
    }, {
        'name': 'Equilibrated?',
        'total': False
    }, {
        'name': 'Spawners',
        'total': True
    }, {
        'name': 'Harvest',
        'total': True
    }]

    timestep_breakdown_body = []
    for i in range(0, total_timesteps):
        sub_dict = {}
        sub_dict['Time Step'] = str(i)
        if i == 0:
            sub_dict['Spawners'] = "(none)"
        elif recruitment_type == 'Fixed':
            sub_dict['Spawners'] = "(fixed recruitment)"
        else:
            sub_dict['Spawners'] = "%.2f" % Spawners_t[i]
        sub_dict['Harvest'] = "%.2f" % H_tx[i].sum()
        # This can be more rigorously checked
        if equilibrate_timestep and i >= equilibrate_timestep:
            sub_dict['Equilibrated?'] = 'Y'
        else:
            sub_dict['Equilibrated?'] = 'N'
        timestep_breakdown_body.append(sub_dict)

    # Generate Report
    css = """body { background-color: #EFECCA; color: #002F2F; }
         h1 { text-align: center }
         h1, h2, h3, h4, strong, th { color: #046380 }
         h2 { border-bottom: 1px solid #A7A37E }
         table { border: 5px solid #A7A37E; margin-bottom: 50px; \
         background-color: #E6E2AF; }
         table.sortable thead:hover { border: 5px solid #A7A37E; \
         margin-bottom: 50px; background-color: #E6E2AF; }
         td, th { margin-left: 0px; margin-right: 0px; padding-left: \
         8px; padding-right: 8px; padding-bottom: 2px; padding-top: \
         2px; text-align: left; }
         td { border-top: 5px solid #EFECCA }
         img { margin: 20px }"""

    elements = [{
        'type': 'text',
        'section': 'body',
        'text': '<h2>Model Run Overview</h2>'
    }, {
        'type': 'table',
        'section': 'body',
        'sortable': True,
        'checkbox': False,
        'total': False,
        'data_type': 'dictionary',
        'columns': overview_columns,
        'data': overview_body
    }, {
        'type':
        'text',
        'section':
        'body',
        'text':
        '<h2>Final Harvest by Subregion After ' + str(total_timesteps - 1) +
        ' Time Steps</h2>'
    }, {
        'type': 'table',
        'section': 'body',
        'sortable': True,
        'checkbox': False,
        'total': True,
        'data_type': 'dictionary',
        'columns': final_cycle_columns,
        'data': final_timestep_body
    }, {
        'type': 'text',
        'section': 'body',
        'text': '<h2>Time Step Breakdown</h2>'
    }, {
        'type': 'table',
        'section': 'body',
        'sortable': True,
        'checkbox': False,
        'total': False,
        'data_type': 'dictionary',
        'columns': timestep_breakdown_columns,
        'data': timestep_breakdown_body
    }, {
        'type': 'head',
        'section': 'head',
        'format': 'style',
        'data_src': css,
        'input_type': 'Text'
    }]

    equilibrium_warning = [{
        'type':
        'text',
        'section':
        'body',
        'text':
        '<h2 style="color:red">Warning: Population Did Not Reach Equilibrium State</h2>'
    }]

    if not bool(vars_dict['equilibrate_timestep']):
        elements = equilibrium_warning + elements

    rep_args['elements'] = elements

    reporting.generate_report(rep_args)