Beispiel #1
0
def test_normalize_col_name():
    vma_r = VMAReader(wb)
    assert vma_r.normalize_col_name(
        'Conedition calculation') == 'Conversion calculation'
    assert vma_r.normalize_col_name(
        'Manually Exclude Data?') == 'Exclude Data?'
    assert vma_r.normalize_col_name('Weight by: Production') == 'Weight'
Beispiel #2
0
def test_read_xls_num_dfs():
    """ Check we produce the right amount of tables + discard empty ones """
    vma_r = VMAReader(wb)
    df_dict = vma_r.read_xls()
    assert len(df_dict) == 24
    num_empty_tables = len([x for (x, _, _) in df_dict.values() if x is None])
    assert num_empty_tables == 13
Beispiel #3
0
def test_single_table():
    vma_r = VMAReader(wb)
    result, _, _ = vma_r.read_single_table('C48',
                                           sheetname='Variable Meta-analysis',
                                           fixed_summary=False)
    expected = pd.read_csv(thisdir.joinpath('silvopasture_vma1.csv'))
    expected['Year / Date'] = expected['Year / Date'].astype('object')
    pd.testing.assert_frame_equal(expected, result)
Beispiel #4
0
def test_rrs():
    wb = xlrd.open_workbook(thisdir.joinpath('solarpvutil_vma.xlsm'))
    vma_r = VMAReader(wb)
    df_dict = vma_r.read_xls(alt_vma=True)
    table = df_dict['Current Adoption'][0]
    assert table.at[
        0,
        'SOURCE ID: Author/Org, Date, Info'] == 'IRENA (2016)_Historical data for solar PV'
    assert table.at[
        0,
        'Assumptions'] == 'Assuming that PV utility represents around 60% of total Solar PV'
    assert table.at[0, 'Exclude Data?'] == True
Beispiel #5
0
def test_read_xls():
    """ Check some specifc values from Silvopasture """
    vma_r = VMAReader(wb)
    df_dict = vma_r.read_xls()
    table = df_dict[
        'SOLUTION Net Profit Margin per Functional Unit per Annum'][0]
    assert table.at[5, 'Raw Data Input'] == 416
    table = df_dict['Sequestration Rates'][0]
    assert len(table) == 28
    (table, exclude, summary) = df_dict['SOLUTION Energy Efficiency Factor']
    assert table is None
    assert exclude == False
    assert len(summary) == 3
    assert pd.isna(summary[0])
    assert pd.isna(summary[1])
    assert pd.isna(summary[2])
Beispiel #6
0
    def _read_xls(self, filename, title):
        """
        Read a properly formatted xlsx/xlsm file (with a Variable
        Meta-analysis sheet) to instantiate this VMA.

        Arguments:
            filename: pathlib.Path to an Excel file
            title: string matching VMA name in the Variable Meta-analysis sheet

        Populates self.source_data, self.df, and self.fixed_summary if the
        required values are present.
        """
        workbook = xlrd.open_workbook(filename=filename)
        vma_reader = VMAReader(workbook)
        if 'Variable Meta-analysis-DD' in workbook.sheet_names():
            alt_vma = True
        else:
            alt_vma = False

        # Pull the desired table from this workbook
        try:
            (xl_df, use_weight, summary) = \
                vma_reader.xls_df_dict(alt_vma=alt_vma, title=title)[title]
        except KeyError:
            # The title wasn't available in the given workbook. Read all titles
            # to give the user a hint.
            full_dict = vma_reader.xls_df_dict(alt_vma=alt_vma)
            raise ValueError(
                f"Title {title!r} not available in {filename}\nOptions\n\t" + \
                "\n\t".join(full_dict.keys())
            )

        self._convert_from_human_readable(xl_df, filename)

        # Populate the self.fixed_summary field if the values are valid
        fixed_summary = check_fixed_summary(*summary)
        if fixed_summary is not None:
            self.fixed_summary = fixed_summary
Beispiel #7
0
def test_read_xls_additional_var():
    """ Check the additional var from Silvopasture """
    vma_r = VMAReader(wb)
    df_dict = vma_r.read_xls()
    assert 'Percent silvopasture area to the total grassland area (including potential)' in df_dict
Beispiel #8
0
def test_single_table_use_weight():
    vma_r = VMAReader(wb)
    _, uw, _ = vma_r.read_single_table('C48',
                                       sheetname='Variable Meta-analysis',
                                       fixed_summary=False)
    assert not uw