Datasets:
Formats:
parquet
Languages:
English
Size:
100K - 1M
Tags:
isora
international-survey-on-revenue-administration
tax-administration
revenue-administration
tax-authority
taxation
License:
v1.1.0: consolidated panel + panel_dictionary, isora.py loader, World Bank income groups (current + per fiscal year), thousands flag on derived expenditure aggregates
9f0fcd7 verified Download pipeline/tests/test_observations.py from FrenchCastle/isora-tax-administration: direct link, hf CLI and curl.
- Browser
- Download file 4.58 kB
-
https://huggingface.co/datasets/FrenchCastle/isora-tax-administration/resolve/main/pipeline/tests/test_observations.py
- Command line
-
hf download hf://datasets/FrenchCastle/isora-tax-administration/pipeline/tests/test_observations.py
-
curl -L -o test_observations.py https://huggingface.co/datasets/FrenchCastle/isora-tax-administration/resolve/main/pipeline/tests/test_observations.py
4.58 kB
| from isora_hf.observations import ( | |
| STATUS_EMPTY, | |
| STATUS_NOT_APPLICABLE, | |
| STATUS_NOT_AVAILABLE, | |
| STATUS_UNRECOGNIZED, | |
| STATUS_VALUE, | |
| clean_text, | |
| parse_value, | |
| ) | |
| def test_numeric_values_are_parsed_to_float(): | |
| parsed = parse_value(" 15.35949622 ") | |
| assert parsed.status == STATUS_VALUE | |
| assert parsed.numeric == 15.35949622 | |
| assert parsed.text is None | |
| def test_negative_and_exponent_numbers_are_numeric(): | |
| assert parse_value("-3").numeric == -3.0 | |
| assert parse_value("1e3").numeric == 1000.0 | |
| def test_nan_and_inf_strings_are_not_numeric(): | |
| for raw in ("nan", "inf", "NaN", "Infinity"): | |
| parsed = parse_value(raw) | |
| assert parsed.numeric is None | |
| assert parsed.text == raw | |
| def test_d_is_not_available_sentinel(): | |
| parsed = parse_value("D") | |
| assert parsed.status == STATUS_NOT_AVAILABLE | |
| assert parsed.numeric is None and parsed.text is None | |
| def test_p_is_flagged_as_unrecognized_but_kept(): | |
| parsed = parse_value("P") | |
| assert parsed.status == STATUS_UNRECOGNIZED | |
| assert parsed.text == "P" | |
| def test_not_applicable_variants(): | |
| for raw in ("Not Applicable", "N/A", "not applicable "): | |
| assert parse_value(raw).status == STATUS_NOT_APPLICABLE | |
| def test_empty_string_is_empty_status(): | |
| assert parse_value("").status == STATUS_EMPTY | |
| assert parse_value(None).status == STATUS_EMPTY | |
| def test_categorical_answers_keep_text_and_strip_html(): | |
| parsed = parse_value("option a)<br/>") | |
| assert parsed.status == STATUS_VALUE | |
| assert parsed.text == "option a)" | |
| assert parsed.numeric is None | |
| def test_clean_text_collapses_whitespace_and_entities(): | |
| assert clean_text(" High & Medium\n priority ") == "High & Medium priority" | |
| def test_repair_mojibake_handles_double_encoding_and_leaves_clean_text(): | |
| from isora_hf.observations import repair_mojibake | |
| assert repair_mojibake("from ‘tax type’") == "from ‘tax type’" | |
| assert repair_mojibake("Türkiye") == "Türkiye" | |
| assert repair_mojibake("Türkiye ‘quoted’") == "Türkiye ‘quoted’" | |
| def test_space_grouped_numbers_are_numeric(): | |
| parsed = parse_value("163 310 020") | |
| assert parsed.status == STATUS_VALUE and parsed.numeric == 163310020.0 | |
| def test_observed_value_kind_inference(): | |
| import pandas as pd | |
| from isora_hf.observations import observed_value_kind | |
| def kind(vals): | |
| parsed = [parse_value(v) for v in vals] | |
| return observed_value_kind( | |
| pd.Series([p.numeric for p in parsed], dtype="float64"), | |
| pd.Series([p.text for p in parsed], dtype="string"), | |
| pd.Series([p.status for p in parsed], dtype="string"), | |
| ) | |
| assert kind(["1", "2.5", "D", ""]) == "numeric" | |
| assert kind(["Yes", "No", "yes", "D"]) == "binary" | |
| assert kind(["High", "Low", "Medium"]) == "categorical" | |
| assert kind(["1", "Yes"]) == "mixed" | |
| assert kind(["D", "", "Not Applicable"]) == "no_values" | |
| assert kind([f"free text {i}" for i in range(40)]) == "free_text" | |
| def test_monetary_harmonization_by_generation(): | |
| import pandas as pd | |
| from isora_hf.config import DATAFLOWS | |
| from isora_hf.observations import UNIT_LCU, UNIT_LCU_THOUSANDS, monetary_columns | |
| codes = pd.Series(["80040_3", "83690_206"]) | |
| numeric = pd.Series([407383000.0, 120.0]) | |
| unit, harmonized = monetary_columns( | |
| DATAFLOWS[1], codes, pd.Series([0, 0]), numeric, {"80040_3"} | |
| ) | |
| assert unit.tolist()[0] == UNIT_LCU_THOUSANDS and pd.isna(unit.tolist()[1]) | |
| assert harmonized.tolist()[0] == 407383000.0 * 1000 and pd.isna(harmonized.tolist()[1]) | |
| unit, harmonized = monetary_columns( | |
| DATAFLOWS[2], codes, pd.Series([3, 0]), pd.Series([619373009000.0, 120.0]), set() | |
| ) | |
| assert unit.tolist()[0] == UNIT_LCU and harmonized.tolist()[0] == 619373009000.0 | |
| assert pd.isna(harmonized.tolist()[1]) | |
| def test_latest_derived_expenditure_aggregates_are_thousands(): | |
| import pandas as pd | |
| from isora_hf.config import DATAFLOWS | |
| from isora_hf.observations import UNIT_LCU, UNIT_LCU_THOUSANDS, monetary_columns | |
| codes = pd.Series(["337_176", "80040_3", "337_001"]) | |
| unit, harmonized = monetary_columns( | |
| DATAFLOWS[2], codes, pd.Series([0, 3, 0]), pd.Series([4398349.0, 6.27e11, 0.7]), set() | |
| ) | |
| assert unit.tolist()[0] == UNIT_LCU_THOUSANDS and harmonized.tolist()[0] == 4398349000.0 | |
| assert unit.tolist()[1] == UNIT_LCU and harmonized.tolist()[1] == 6.27e11 | |
| assert pd.isna(unit.tolist()[2]) and pd.isna(harmonized.tolist()[2]) | |