def test_build_agg_conc_exio():
    """ Some agg_conc test with a subset of exio countries
    """

    original_countries = ['TW', 'XX', 'AT', 'US', 'WA']
    aggregates = ['EU', 'OECD', 'continent']

    agg_dict_replace = coco.agg_conc(original_countries,
                                     aggregates,
                                     merge_multiple_string=False,
                                     missing_countries='RoW',
                                     log_missing_countries=None,
                                     log_merge_multiple_strings=None,
                                     as_dataframe=False)

    assert agg_dict_replace == OrderedDict([('TW', 'Asia'), ('XX', 'RoW'),
                                            ('AT', 'EU'), ('US', 'OECD'),
                                            ('WA', 'RoW')])

    agg_dict_skip = coco.agg_conc(original_countries,
                                  aggregates,
                                  merge_multiple_string=False,
                                  missing_countries=False,
                                  log_missing_countries=None,
                                  log_merge_multiple_strings=None,
                                  as_dataframe=False)

    assert agg_dict_skip == OrderedDict([('TW', 'Asia'), ('AT', 'EU'),
                                         ('US', 'OECD')])

    agg_matrix_skip = coco.agg_conc(original_countries,
                                    aggregates,
                                    merge_multiple_string=False,
                                    missing_countries=False,
                                    log_missing_countries=None,
                                    log_merge_multiple_strings=None,
                                    as_dataframe='full')

    assert agg_matrix_skip.index.tolist() == ['TW', 'AT', 'US']

    aggregates_oecd_first = ['OECD', 'EU', {'WA': 'RoW', 'WF': 'RoW'}]

    agg_dict_oecd_eu = coco.agg_conc(original_countries,
                                     aggregates_oecd_first,
                                     merge_multiple_string=False,
                                     missing_countries=True,
                                     log_missing_countries=None,
                                     log_merge_multiple_strings=None,
                                     as_dataframe=False)

    assert agg_dict_oecd_eu == OrderedDict([('TW', 'TW'), ('XX', 'XX'),
                                            ('AT', 'OECD'), ('US', 'OECD'),
                                            ('WA', 'RoW')])

    aggregates = 'EU'
    agg_dict_full_exio = coco.agg_conc('EXIO2',
                                       aggregates,
                                       merge_multiple_string=False,
                                       missing_countries='RoW',
                                       log_missing_countries=None,
                                       log_merge_multiple_strings=None,
                                       as_dataframe=False)

    assert len(agg_dict_full_exio) == 48
    assert agg_dict_full_exio['US'] == 'RoW'
    assert agg_dict_full_exio['AT'] == 'EU'
def test_build_agg_conc_exio():
    """Some agg_conc test with a subset of exio countries"""

    original_countries = ["TW", "XX", "AT", "US", "WA"]
    aggregates = ["EU", "OECD", "continent"]

    agg_dict_replace = coco.agg_conc(
        original_countries,
        aggregates,
        merge_multiple_string=False,
        missing_countries="RoW",
        log_missing_countries=None,
        log_merge_multiple_strings=None,
        as_dataframe=False,
    )

    assert agg_dict_replace == OrderedDict([("TW", "Asia"), ("XX", "RoW"),
                                            ("AT", "EU"), ("US", "OECD"),
                                            ("WA", "RoW")])

    agg_dict_skip = coco.agg_conc(
        original_countries,
        aggregates,
        merge_multiple_string=False,
        missing_countries=False,
        log_missing_countries=None,
        log_merge_multiple_strings=None,
        as_dataframe=False,
    )

    assert agg_dict_skip == OrderedDict([("TW", "Asia"), ("AT", "EU"),
                                         ("US", "OECD")])

    agg_matrix_skip = coco.agg_conc(
        original_countries,
        aggregates,
        merge_multiple_string=False,
        missing_countries=False,
        log_missing_countries=None,
        log_merge_multiple_strings=None,
        as_dataframe="full",
    )

    assert agg_matrix_skip.index.tolist() == ["TW", "AT", "US"]

    aggregates_oecd_first = ["OECD", "EU", {"WA": "RoW", "WF": "RoW"}]

    agg_dict_oecd_eu = coco.agg_conc(
        original_countries,
        aggregates_oecd_first,
        merge_multiple_string=False,
        missing_countries=True,
        log_missing_countries=None,
        log_merge_multiple_strings=None,
        as_dataframe=False,
    )

    assert agg_dict_oecd_eu == OrderedDict([("TW", "TW"), ("XX", "XX"),
                                            ("AT", "OECD"), ("US", "OECD"),
                                            ("WA", "RoW")])

    aggregates = "EU"
    agg_dict_full_exio = coco.agg_conc(
        "EXIO2",
        aggregates,
        merge_multiple_string=False,
        missing_countries="RoW",
        log_missing_countries=None,
        log_merge_multiple_strings=None,
        as_dataframe=False,
    )

    assert len(agg_dict_full_exio) == 48
    assert agg_dict_full_exio["US"] == "RoW"
    assert agg_dict_full_exio["AT"] == "EU"
def test_build_agg_conc_custom():
    """ Minimal test of the aggregation concordance building functionality
    """

    original_countries = ['c1', 'c2', 'c3', 'c4']
    aggregates = [{'c1': 'r1', 'c2': 'r1', 'c3': 'r2'}]

    agg_dict_wmiss = coco.agg_conc(
        original_countries,
        aggregates,
        merge_multiple_string=None,
        missing_countries=True,
        log_missing_countries=(
            lambda x: logging.error('Country {} missing'.format(x))),
        log_merge_multiple_strings=None,
        as_dataframe=False)

    assert agg_dict_wmiss == OrderedDict([('c1', 'r1'), ('c2', 'r1'),
                                          ('c3', 'r2'), ('c4', 'c4')])

    agg_dict_replace = coco.agg_conc(original_countries,
                                     aggregates,
                                     merge_multiple_string=None,
                                     missing_countries='RoW',
                                     log_missing_countries=None,
                                     log_merge_multiple_strings=None,
                                     as_dataframe=False)

    assert agg_dict_replace == OrderedDict([('c1', 'r1'), ('c2', 'r1'),
                                            ('c3', 'r2'), ('c4', 'RoW')])

    agg_vec_womiss = coco.agg_conc(original_countries,
                                   aggregates,
                                   merge_multiple_string=None,
                                   missing_countries=False,
                                   log_missing_countries=None,
                                   log_merge_multiple_strings=None,
                                   as_dataframe='sparse')

    expected_vec = pd.DataFrame(data=[
        ['c1', 'r1'],
        ['c2', 'r1'],
        ['c3', 'r2'],
    ],
                                columns=['original', 'aggregated'])

    assert_frame_equal(agg_vec_womiss, expected_vec)

    agg_matrix_womiss = coco.agg_conc(original_countries,
                                      aggregates,
                                      merge_multiple_string=None,
                                      missing_countries=False,
                                      log_missing_countries=None,
                                      log_merge_multiple_strings=None,
                                      as_dataframe='full')

    expected_matrix = pd.DataFrame(
        data=[
            [1.0, 0.0],
            [1.0, 0.0],
            [0.0, 1.0],
        ],
        columns=['r1', 'r2'],
        index=['c1', 'c2', 'c3'],
    )
    expected_matrix.index.names = ['original']
    expected_matrix.columns.names = ['aggregated']

    assert_frame_equal(agg_matrix_womiss, expected_matrix)

    original_countries = ['c1', 'c2', 'c3', 'c4']
    aggregates = [{'c1': ['r1', 'r2'], 'c2': 'r1', 'c3': 'r2'}]
    agg_matrix_double_region = coco.agg_conc(
        original_countries,
        aggregates,
        merge_multiple_string='_&_',
        missing_countries=False,
        log_missing_countries=None,
        log_merge_multiple_strings=(
            lambda x: logging.warning('Country {} belongs to '
                                      'multiple '
                                      'regions'.format(x))),
        as_dataframe='full')
    expected_matrix = pd.DataFrame(
        data=[
            [0.0, 1.0, 0.0],
            [1.0, 0.0, 0.0],
            [0.0, 0.0, 1.0],
        ],
        columns=['r1', 'r1_&_r2', 'r2'],
        index=['c1', 'c2', 'c3'],
    )
    expected_matrix.index.names = ['original']
    expected_matrix.columns.names = ['aggregated']

    assert_frame_equal(agg_matrix_double_region, expected_matrix)
def test_build_agg_conc_custom():
    """Minimal test of the aggregation concordance building functionality"""

    original_countries = ["c1", "c2", "c3", "c4"]
    aggregates = [{"c1": "r1", "c2": "r1", "c3": "r2"}]

    agg_dict_wmiss = coco.agg_conc(
        original_countries,
        aggregates,
        merge_multiple_string=None,
        missing_countries=True,
        log_missing_countries=(
            lambda x: logging.error("Country {} missing".format(x))),
        log_merge_multiple_strings=None,
        as_dataframe=False,
    )

    assert agg_dict_wmiss == OrderedDict([("c1", "r1"), ("c2", "r1"),
                                          ("c3", "r2"), ("c4", "c4")])

    agg_dict_replace = coco.agg_conc(
        original_countries,
        aggregates,
        merge_multiple_string=None,
        missing_countries="RoW",
        log_missing_countries=None,
        log_merge_multiple_strings=None,
        as_dataframe=False,
    )

    assert agg_dict_replace == OrderedDict([("c1", "r1"), ("c2", "r1"),
                                            ("c3", "r2"), ("c4", "RoW")])

    agg_vec_womiss = coco.agg_conc(
        original_countries,
        aggregates,
        merge_multiple_string=None,
        missing_countries=False,
        log_missing_countries=None,
        log_merge_multiple_strings=None,
        as_dataframe="sparse",
    )

    expected_vec = pd.DataFrame(
        data=[["c1", "r1"], ["c2", "r1"], ["c3", "r2"]],
        columns=["original", "aggregated"],
    )

    assert_frame_equal(agg_vec_womiss, expected_vec)

    agg_matrix_womiss = coco.agg_conc(
        original_countries,
        aggregates,
        merge_multiple_string=None,
        missing_countries=False,
        log_missing_countries=None,
        log_merge_multiple_strings=None,
        as_dataframe="full",
    )

    expected_matrix = pd.DataFrame(
        data=[[1.0, 0.0], [1.0, 0.0], [0.0, 1.0]],
        columns=["r1", "r2"],
        index=["c1", "c2", "c3"],
    )
    expected_matrix.index.names = ["original"]
    expected_matrix.columns.names = ["aggregated"]

    assert_frame_equal(agg_matrix_womiss, expected_matrix)

    original_countries = ["c1", "c2", "c3", "c4"]
    aggregates = [{"c1": ["r1", "r2"], "c2": "r1", "c3": "r2"}]
    agg_matrix_double_region = coco.agg_conc(
        original_countries,
        aggregates,
        merge_multiple_string="_&_",
        missing_countries=False,
        log_missing_countries=None,
        log_merge_multiple_strings=(
            lambda x: logging.warning("Country {} belongs to "
                                      "multiple "
                                      "regions".format(x))),
        as_dataframe="full",
    )
    expected_matrix = pd.DataFrame(
        data=[[0.0, 1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]],
        columns=["r1", "r1_&_r2", "r2"],
        index=["c1", "c2", "c3"],
    )
    expected_matrix.index.names = ["original"]
    expected_matrix.columns.names = ["aggregated"]

    assert_frame_equal(agg_matrix_double_region, expected_matrix)