cofog-panel 1.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Trong Tin Nguyen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,246 @@
1
+ Metadata-Version: 2.4
2
+ Name: cofog-panel
3
+ Version: 1.1.0
4
+ Summary: Harmonise IMF GFS COFOG data into reproducible country-year panel datasets.
5
+ Author: Thanh-Tung Pham
6
+ Author-email: Trong Tin Nguyen <nttin@hcmiu.edu.vn>
7
+ License-Expression: MIT
8
+ Project-URL: Repository, https://github.com/trongtin142/cofog-panel
9
+ Project-URL: Issues, https://github.com/trongtin142/cofog-panel/issues
10
+ Project-URL: Archive, https://doi.org/10.5281/zenodo.23511907
11
+ Keywords: COFOG,IMF GFS,government expenditure,panel data,harmonisation
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Intended Audience :: Science/Research
15
+ Classifier: Topic :: Scientific/Engineering :: Information Analysis
16
+ Requires-Python: >=3.10
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE.txt
19
+ Requires-Dist: pandas>=2.0.0
20
+ Requires-Dist: openpyxl>=3.1.5
21
+ Requires-Dist: typer>=0.18.0
22
+ Provides-Extra: dev
23
+ Requires-Dist: pytest>=7.0; extra == "dev"
24
+ Provides-Extra: gui
25
+ Requires-Dist: streamlit>=1.49.0; extra == "gui"
26
+ Provides-Extra: replication
27
+ Requires-Dist: statsmodels>=0.14; extra == "replication"
28
+ Requires-Dist: matplotlib>=3.9; extra == "replication"
29
+ Dynamic: license-file
30
+
31
+ # COFOG-Panel
32
+
33
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/trongtin142/cofog-panel/blob/main/LICENSE.txt)
34
+ [![tests](https://github.com/trongtin142/cofog-panel/actions/workflows/tests.yml/badge.svg)](https://github.com/trongtin142/cofog-panel/actions/workflows/tests.yml)
35
+ [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.23511907.svg)](https://doi.org/10.5281/zenodo.23511907)
36
+
37
+ COFOG-Panel builds a country-year panel for any COFOG division or group from a raw download (CSV or Excel) of the IMF Government Finance Statistics (GFS) dataset on expenditure by function of government. Countries report these data for different sets of government subsectors. The tool selects one value per country-year with a documented three-tier rule and records, next to every value, the tier, the sectors that entered it and whether it covers every level of government.
38
+
39
+ The DOI badge points to all versions of the software. Each release also has its own DOI, listed on the Zenodo page and in `CITATION.cff`.
40
+
41
+ ## Quickstart (GUI)
42
+
43
+ For users who prefer a graphical interface (a browser page, no command line options to remember):
44
+
45
+ 1. Install Python 3.10 or later from [python.org](https://www.python.org/) (on Windows, tick "Add Python to PATH").
46
+ 2. Start the application: on macOS or Linux run `bash run_gui.sh`, on Windows double-click `run_gui.bat`. The script creates a `.venv` folder, installs the package with the `gui` extra (`pip install -e ".[gui]"`, which adds Streamlit 1.49 or later) and opens the page.
47
+ 3. In the page, upload the IMF file, choose the COFOG function, method and transformation in the sidebar, and click **Run Pipeline**. Download the master when it finishes. The page shows the reports and warnings of the stages (the same text the command line prints) in a run log, and the reason when a stage fails.
48
+
49
+ The GUI runs the same five stages as `cofog-panel run` for one COFOG function from the built-in list, with the default column names. It does not offer `--zero-as-missing`, `--check-ranges`, several functions, `--split-dir` or a country lookup in another format than `.xlsx`; use the command line for those. Each run builds a new master with one indicator: uploaded files and the result live in a temporary folder that is deleted when the run ends, so download the master from the page. To collect several indicators in one master, use `cofog-panel run` (Section 3).
50
+
51
+ ## Quickstart in 3 steps
52
+
53
+ **1. Install** (Python 3.10 or later):
54
+
55
+ ```bash
56
+ git clone https://github.com/trongtin142/cofog-panel.git
57
+ cd cofog-panel
58
+ pip install -e .
59
+ ```
60
+
61
+ **2. Download the IMF data** (once, about 220 MB):
62
+
63
+ 1. Open the dataset page <https://data.imf.org/en/datasets/IMF.STA:GFS_COFOG> ("GFS Government Expenditures by Function").
64
+ 2. Click **DOWNLOAD** (top right). A "Download Dataset" window opens.
65
+ 3. Keep the defaults: **Download full dataset** switched on, **File Format: CSV**, **Data format: Timeseries per row**. Click **DOWNLOAD** in the window.
66
+ 4. The browser saves a file named like `dataset_2026-..._IMF.STA_GFS_COFOG_11.0.0.csv`. Move it to the `data` folder of the repository and rename it `gfs_raw_data.csv`.
67
+
68
+ An Excel download (`data/gfs_raw_data.xlsx`) works as well and is used when both files exist; the CSV file is read much faster.
69
+
70
+ **3. Run** one command, here for environmental protection (COFOG 05):
71
+
72
+ ```bash
73
+ cofog-panel run --cofog 05
74
+ ```
75
+
76
+ The panel is written to `output/COFOG_MASTER_SCHEMA.xlsx` (sheet `MasterData`). Five rows of the result:
77
+
78
+ | SortKey | Country | Year | DATA_05 | SOURCE_TIER_05 | SOURCE_SECTORS_05 | SOURCE_COMPLETE_05 |
79
+ | :--- | :--- | ---: | ---: | :--- | :--- | :--- |
80
+ | ARG2002 | Argentina | 2002 | 0.018 | T2 | CGSS+SG | Y |
81
+ | ARM2012 | Armenia | 2012 | 0.404 | T3 | BCG+LG | N |
82
+ | ARM2013 | Armenia | 2013 | 0.349 | T1 | GG | Y |
83
+ | ARM2014 | Armenia | 2014 | 0.097 | T3 | BCG | N |
84
+ | DEU2015 | Germany | 2015 | 0.541 | T1 | GG | Y |
85
+
86
+ Values are in per cent of GDP (rounded here; the file keeps all digits). They come from the release published on 3 February 2026; a later download gives revised values.
87
+
88
+ * **T1**: general government (GG) as reported. **T2**: central government including social security (CGSS) plus the state and local levels that are reported. **T3**: the central government components (budgetary BCG, extrabudgetary EBCG, social security funds SSF) plus state (SG) and local (LG) government, when neither GG nor CGSS is reported.
89
+ * **Y / N**: `Y` when the value covers every level of government that the country reports in its series; `N` when a level is missing (Armenia 2014 lacks the local level it reports in other years), so the value understates spending.
90
+
91
+ Other useful commands: `cofog-panel list-cofog` lists the COFOG codes and labels in the file, `cofog-panel list-sectors` the accepted `--sector` and `--filter-type` values, and `cofog-panel run --help` all options.
92
+
93
+ ## 1. How it works
94
+
95
+ The pipeline runs on local files (an Excel master and one CSV file per country between stages 4 and 5) in five stages. Each stage is a sub-command, and `cofog-panel run` runs all five.
96
+
97
+ | Stage | Command | Module | Purpose |
98
+ | :--- | :--- | :--- | :--- |
99
+ | 1 | `check-format` | `checks.py` | Checks the IMF metadata columns and the year columns (no gaps, from 1972) of the extract; unknown extra columns give a warning, not an error |
100
+ | 2 | `check-country-format` | `checks.py` | Checks the country lookup (`name`, `alpha-2`, `alpha-3`, `country-code`; every alpha-3 is three letters, none repeated; `seed-master` applies the same check) |
101
+ | 3 | `seed-master` | `master_seed.py` | Writes the master panel: one row per country and year, `SortKey` = ISO3 + year, 1972–2025 by default; `--start-year` must not be after `--end-year` |
102
+ | 4 | `split` | `etl.py` | Lists the countries of the extract that have no row in the lookup (`--lookup-file`, default the bundled one; a warning, the files are still written). Keeps one transformation and one COFOG function and writes one CSV file per country (`AAA.csv`; splits with `.xlsx` country files from versions up to 1.0.5 are still read by `aggregate`) |
103
+ | 5 | `aggregate` | `aggregate.py` | Applies the selection rule and adds the values and their provenance to the master |
104
+
105
+ ### Selection rule (`--sector "General government"`, the default)
106
+
107
+ | Tier | Value used |
108
+ | :--- | :--- |
109
+ | T1 | General government (GG), if reported |
110
+ | T2 | otherwise, if central government including social security (CGSS) is reported: CGSS + state + local |
111
+ | T3 | otherwise: central government excluding social security (or, if that is not reported, budgetary + extrabudgetary central government) + social security funds + state + local, over the sectors reported |
112
+
113
+ T2 and T3 are unconsolidated sums: transfers between levels of government (and, in T3, between central government units) that are recorded under the same function are counted twice. Two other methods are available: `--sector "Central government including social security"` (C1 = CGSS, C2 = central core + social security funds) and any single sector name (D = that sector alone).
114
+
115
+ ### Output columns
116
+
117
+ | Column | Content |
118
+ | :--- | :--- |
119
+ | `SortKey`, `Country`, `Year` | Panel keys |
120
+ | `--data-col` (default `DATA_<COFOG code>`, e.g. `DATA_05`, `DATA_07_3`) | Selected value, as a number |
121
+ | `--tier-col` (default `SOURCE_TIER_<suffix>`) | T1, T2, T3, C1, C2 or D |
122
+ | `--sectors-col` (default `SOURCE_SECTORS_<suffix>`) | Sectors that entered the value, e.g. `CGSS+LG` or `BCG+SG` |
123
+ | `--complete-col` (default `SOURCE_COMPLETE_<suffix>`) | `Y` or `N` (see below) |
124
+
125
+ The data column is named after the COFOG code unless it is given: `--cofog 05` writes `DATA_05`, `--cofog 07.3` writes `DATA_07_3`, and a label is mapped to its code. The tier, sectors and completeness columns take the suffix of the data column (`DATA_05` gives `SOURCE_TIER_05`, `SOURCE_SECTORS_05` and `SOURCE_COMPLETE_05`), so that several indicators in one master keep separate provenance. A data column given as `DATA_NEW` writes `SOURCE_TIER`, `SOURCE_SECTORS` and `SOURCE_COMPLETE`, as in earlier versions. The tool warns when a run would replace a column that is already in the master.
126
+
127
+ Completeness: a T2, T3 or C2 sum is `N` when it leaves out a level (state, local, social security funds, extrabudgetary central government) that the country reports in another year of the same series, or the central core. A T1 value is `N` when GG equals CGSS exactly while state or local government report positive spending in that year.
128
+
129
+ Further rules: reported zeros are kept unless `--zero-as-missing` is given; `--check-ranges` (`aggregate`, `run`) lists selected values that are negative, or above 100 for percentage transformations, and keeps them; shares of total outlays are never summed, so they only give T1 values; the IMF codes KOS and WBG are mapped to the ISO codes XKX and PSE. Values in domestic currency are added only when the series of a country agree in `SCALE`, `UNIT` and `CURRENCY` (letter case ignored); otherwise the country gets reported values only (T1, C1, D) and `aggregate` prints a warning.
130
+
131
+ ## 2. Installation
132
+
133
+ ```bash
134
+ git clone https://github.com/trongtin142/cofog-panel.git
135
+ cd cofog-panel
136
+ python -m venv venv
137
+ source venv/bin/activate # macOS and Linux
138
+ pip install -e . # the command-line tool
139
+ pip install -e ".[dev]" # adds pytest, to run the tests
140
+ pip install -e ".[gui]" # adds Streamlit, for the browser interface (streamlit run gui.py)
141
+ pip install -e ".[replication]" # adds statsmodels and matplotlib, for the scripts in replication/
142
+ ```
143
+
144
+ `cofog-panel --version` prints the installed version.
145
+
146
+ ### Windows (PowerShell)
147
+
148
+ ```powershell
149
+ python -m venv venv
150
+ .\venv\Scripts\Activate.ps1
151
+ pip install -e .
152
+ ```
153
+
154
+ If PowerShell refuses to run `Activate.ps1`, allow local scripts once with `Set-ExecutionPolicy -Scope CurrentUser RemoteSigned`, or use `venv\Scripts\activate.bat` in the Command Prompt. Put paths that contain spaces in double quotes, and use a backtick (`` ` ``) instead of a backslash to continue a command on the next line:
155
+
156
+ ```powershell
157
+ cofog-panel run --cofog 05 `
158
+ --source-file "D:\WORKING PAPER\data\gfs_raw_data.xlsx" `
159
+ --output-dir "D:\WORKING PAPER\output"
160
+ ```
161
+
162
+ ## 3. Usage
163
+
164
+ Defaults: `--source-file` is `data/gfs_raw_data.xlsx`, or `data/gfs_raw_data.csv` if there is no Excel file (relative to the current directory), `--lookup-file` is the country lookup bundled with the package, `--filter-type` is `"Percent of GDP"`, `--sector` is `"General government"` and the data column is named after the COFOG code. Only `--cofog` is required.
165
+
166
+ A second indicator is added to the same master by running the command again: `cofog-panel run --cofog 07` adds `DATA_07`, `SOURCE_TIER_07`, `SOURCE_SECTORS_07` and `SOURCE_COMPLETE_07`. An existing master is kept and the new columns are added to it. `--overwrite-master` rebuilds the master from the lookup file instead, which deletes the indicator columns it holds; the tool prints a warning when it does so.
167
+
168
+ Several functions can be added in one command by repeating the option: `cofog-panel run --cofog 05 --cofog 07`. Stages 1-3 run once; stages 4 and 5 run for each function in turn, so the source file is read once per function. Each function gets its own split directory (`intermediate_splits/05`, `intermediate_splits/07`) and its own `DATA_<code>` and provenance columns, exactly as if `run` had been called once per function. All `--cofog` values are checked before any file is written. `--data-col`, `--tier-col`, `--sectors-col` and `--complete-col` cannot be combined with several `--cofog` values, and two values that name the same function are refused.
169
+
170
+ Options:
171
+
172
+ * `--cofog` takes a division or group code (`05`, `07.3`) or the exact label used in the file (`"Environmental protection"`). A value that is neither is rejected at once, with the closest codes or labels. `cofog-panel list-cofog` lists the codes and labels in the file with the number of countries (it reads the whole file: seconds for the CSV download, about a minute for the Excel one). A well-formed code that is not in the built-in list (for example `07.99`) is also looked up in the file before the scan and rejected, with the nearest codes, if the file has no series for it; for an Excel download this takes under a second, for a CSV download a few seconds.
173
+ * `--filter-type` is one of `"Percent of GDP"`, `"Domestic currency"` and `"Percent of total outlays"`, in any letter case. Any other value is rejected at once.
174
+ * `--sector` selects the method described in Section 1; `cofog-panel list-sectors` lists the accepted values.
175
+
176
+ These checks run before the extract is read, so a mistake is reported within seconds rather than after the scan of the whole file.
177
+
178
+ Single stages:
179
+
180
+ ```bash
181
+ cofog-panel check-format # data/gfs_raw_data.xlsx or .csv by default
182
+ cofog-panel check-country-format # the bundled lookup by default
183
+ cofog-panel seed-master --output-dir output
184
+ cofog-panel split --cofog "07" --output-dir intermediate_splits
185
+ cofog-panel aggregate --master-file output/COFOG_MASTER_SCHEMA.xlsx --folder-path intermediate_splits
186
+ ```
187
+
188
+ `seed-master` always writes a new master, replacing a file of the same name. `split` writes only into a new or empty directory, or into one it created earlier (marked by a `.cofog_panel_split` file that also records the COFOG function, the `PUBLICATION_DATE` of the selected series and the extract's year span), where it replaces the country files of its previous run; it never deletes files elsewhere. `split` warns about rows of the transformation whose series code gives no COFOG function; the series of other indicators that the extract also carries (such as total outlays, `TOA_T` and `TOTOUT_T`), which have an empty `COFOG` label, are skipped without a warning. `aggregate` names the data column after the function recorded by `split` (`DATA_07` here) unless `--data-col` is given; a `--data-col` of the form `DATA_<code>` for another function than the recorded one gives a warning.
189
+
190
+ ### Errors and exit codes
191
+
192
+ Every error prints one line on what went wrong and one line starting with `Hint:` on how to fix it, for example:
193
+
194
+ ```text
195
+ ❌ Error: Invalid --filter-type 'per cent of GDP'.
196
+ Hint: Choose one of: 'Percent of GDP', 'Domestic currency', 'Percent of total outlays' (letter case does not matter).
197
+ ```
198
+
199
+ Unexpected errors are reported in the same form; `cofog-panel --debug <command> ...` shows the full traceback instead. Exit codes: `0` success, `1` data or option error (missing file, invalid `--cofog`, `--filter-type` or `--sector`, unusable split directory, unreadable country files), `2` command-line usage error (unknown option, missing `--cofog`).
200
+
201
+ ### Python
202
+
203
+ The selection rule can be called from Python:
204
+
205
+ ```python
206
+ from decimal import Decimal
207
+ from cofog_panel import resolve_value
208
+
209
+ value, tier, sectors = resolve_value({
210
+ "general government": None,
211
+ "central government including social security": Decimal("0.5"),
212
+ "local government": Decimal("0.3"),
213
+ })
214
+ # Decimal('0.8'), 'T2', ['central government including social security', 'local government']
215
+ ```
216
+
217
+ ## 4. Tests
218
+
219
+ ```bash
220
+ pip install -e ".[dev]"
221
+ pytest
222
+ ```
223
+
224
+ The tests of the browser interface (`tests/test_gui.py`) are skipped unless Streamlit is installed: `pip install -e ".[dev,gui]"` runs them as well.
225
+
226
+ ## 5. Data
227
+
228
+ The source file can be the CSV or the Excel download of IMF.STA:GFS_COFOG; both have the same columns and give the same panel. The IMF revises the dataset in place: a download made on 21 October 2026 still carries the version number 11.0.0 but has a new `PUBLICATION_DATE` and revised values. Keep the downloaded file with your results and report its `PUBLICATION_DATE` (a column of the file) when you cite the data. `split` warns if the selected series carry several dates, and `aggregate` prints the date it recorded.
229
+
230
+ The country lookup (`data/country_codes.xlsx`, also bundled in the package as `cofog_panel/data/country_codes.xlsx`) is an ISO 3166 lookup with Kosovo (XKX) added. The IMF GFS data are not covered by the MIT License: `DATA_LICENSE.md` gives the IMF terms, the required attribution and the notice on transformed data.
231
+
232
+ ## 6. Replication
233
+
234
+ `replication/` holds the scripts that reproduce the published analyses that use COFOG-Panel, each with its own README. They need the `[replication]` extra.
235
+
236
+ ## 7. Feedback and testing
237
+
238
+ Reports from users help improve the tool. After a run, please open a short [test report](https://github.com/trongtin142/cofog-panel/issues/new?template=test_report.yml) (version, operating system, command, outcome and what was unclear). Problems go in a [bug report](https://github.com/trongtin142/cofog-panel/issues/new?template=bug_report.yml), and questions or ideas in [Discussions](https://github.com/trongtin142/cofog-panel/discussions).
239
+
240
+ ## 8. Citation
241
+
242
+ See `CITATION.cff`. Please cite the DOI of the release you used.
243
+
244
+ ## 9. Authors and acknowledgements
245
+
246
+ COFOG-Panel is developed by Trong Tin Nguyen and Thanh-Tung Pham. Thanh-Tung Pham wrote the early prototype of the pipeline (March 2026), from which this repository grew, and, in version 1.1.0, the browser interface and the input validation. Cam Quyen Tran (GitHub `qynntrn`) contributed to an early version of the code (the `--sector` option and the notebook) and gave helpful comments.
@@ -0,0 +1,216 @@
1
+ # COFOG-Panel
2
+
3
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/trongtin142/cofog-panel/blob/main/LICENSE.txt)
4
+ [![tests](https://github.com/trongtin142/cofog-panel/actions/workflows/tests.yml/badge.svg)](https://github.com/trongtin142/cofog-panel/actions/workflows/tests.yml)
5
+ [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.23511907.svg)](https://doi.org/10.5281/zenodo.23511907)
6
+
7
+ COFOG-Panel builds a country-year panel for any COFOG division or group from a raw download (CSV or Excel) of the IMF Government Finance Statistics (GFS) dataset on expenditure by function of government. Countries report these data for different sets of government subsectors. The tool selects one value per country-year with a documented three-tier rule and records, next to every value, the tier, the sectors that entered it and whether it covers every level of government.
8
+
9
+ The DOI badge points to all versions of the software. Each release also has its own DOI, listed on the Zenodo page and in `CITATION.cff`.
10
+
11
+ ## Quickstart (GUI)
12
+
13
+ For users who prefer a graphical interface (a browser page, no command line options to remember):
14
+
15
+ 1. Install Python 3.10 or later from [python.org](https://www.python.org/) (on Windows, tick "Add Python to PATH").
16
+ 2. Start the application: on macOS or Linux run `bash run_gui.sh`, on Windows double-click `run_gui.bat`. The script creates a `.venv` folder, installs the package with the `gui` extra (`pip install -e ".[gui]"`, which adds Streamlit 1.49 or later) and opens the page.
17
+ 3. In the page, upload the IMF file, choose the COFOG function, method and transformation in the sidebar, and click **Run Pipeline**. Download the master when it finishes. The page shows the reports and warnings of the stages (the same text the command line prints) in a run log, and the reason when a stage fails.
18
+
19
+ The GUI runs the same five stages as `cofog-panel run` for one COFOG function from the built-in list, with the default column names. It does not offer `--zero-as-missing`, `--check-ranges`, several functions, `--split-dir` or a country lookup in another format than `.xlsx`; use the command line for those. Each run builds a new master with one indicator: uploaded files and the result live in a temporary folder that is deleted when the run ends, so download the master from the page. To collect several indicators in one master, use `cofog-panel run` (Section 3).
20
+
21
+ ## Quickstart in 3 steps
22
+
23
+ **1. Install** (Python 3.10 or later):
24
+
25
+ ```bash
26
+ git clone https://github.com/trongtin142/cofog-panel.git
27
+ cd cofog-panel
28
+ pip install -e .
29
+ ```
30
+
31
+ **2. Download the IMF data** (once, about 220 MB):
32
+
33
+ 1. Open the dataset page <https://data.imf.org/en/datasets/IMF.STA:GFS_COFOG> ("GFS Government Expenditures by Function").
34
+ 2. Click **DOWNLOAD** (top right). A "Download Dataset" window opens.
35
+ 3. Keep the defaults: **Download full dataset** switched on, **File Format: CSV**, **Data format: Timeseries per row**. Click **DOWNLOAD** in the window.
36
+ 4. The browser saves a file named like `dataset_2026-..._IMF.STA_GFS_COFOG_11.0.0.csv`. Move it to the `data` folder of the repository and rename it `gfs_raw_data.csv`.
37
+
38
+ An Excel download (`data/gfs_raw_data.xlsx`) works as well and is used when both files exist; the CSV file is read much faster.
39
+
40
+ **3. Run** one command, here for environmental protection (COFOG 05):
41
+
42
+ ```bash
43
+ cofog-panel run --cofog 05
44
+ ```
45
+
46
+ The panel is written to `output/COFOG_MASTER_SCHEMA.xlsx` (sheet `MasterData`). Five rows of the result:
47
+
48
+ | SortKey | Country | Year | DATA_05 | SOURCE_TIER_05 | SOURCE_SECTORS_05 | SOURCE_COMPLETE_05 |
49
+ | :--- | :--- | ---: | ---: | :--- | :--- | :--- |
50
+ | ARG2002 | Argentina | 2002 | 0.018 | T2 | CGSS+SG | Y |
51
+ | ARM2012 | Armenia | 2012 | 0.404 | T3 | BCG+LG | N |
52
+ | ARM2013 | Armenia | 2013 | 0.349 | T1 | GG | Y |
53
+ | ARM2014 | Armenia | 2014 | 0.097 | T3 | BCG | N |
54
+ | DEU2015 | Germany | 2015 | 0.541 | T1 | GG | Y |
55
+
56
+ Values are in per cent of GDP (rounded here; the file keeps all digits). They come from the release published on 3 February 2026; a later download gives revised values.
57
+
58
+ * **T1**: general government (GG) as reported. **T2**: central government including social security (CGSS) plus the state and local levels that are reported. **T3**: the central government components (budgetary BCG, extrabudgetary EBCG, social security funds SSF) plus state (SG) and local (LG) government, when neither GG nor CGSS is reported.
59
+ * **Y / N**: `Y` when the value covers every level of government that the country reports in its series; `N` when a level is missing (Armenia 2014 lacks the local level it reports in other years), so the value understates spending.
60
+
61
+ Other useful commands: `cofog-panel list-cofog` lists the COFOG codes and labels in the file, `cofog-panel list-sectors` the accepted `--sector` and `--filter-type` values, and `cofog-panel run --help` all options.
62
+
63
+ ## 1. How it works
64
+
65
+ The pipeline runs on local files (an Excel master and one CSV file per country between stages 4 and 5) in five stages. Each stage is a sub-command, and `cofog-panel run` runs all five.
66
+
67
+ | Stage | Command | Module | Purpose |
68
+ | :--- | :--- | :--- | :--- |
69
+ | 1 | `check-format` | `checks.py` | Checks the IMF metadata columns and the year columns (no gaps, from 1972) of the extract; unknown extra columns give a warning, not an error |
70
+ | 2 | `check-country-format` | `checks.py` | Checks the country lookup (`name`, `alpha-2`, `alpha-3`, `country-code`; every alpha-3 is three letters, none repeated; `seed-master` applies the same check) |
71
+ | 3 | `seed-master` | `master_seed.py` | Writes the master panel: one row per country and year, `SortKey` = ISO3 + year, 1972–2025 by default; `--start-year` must not be after `--end-year` |
72
+ | 4 | `split` | `etl.py` | Lists the countries of the extract that have no row in the lookup (`--lookup-file`, default the bundled one; a warning, the files are still written). Keeps one transformation and one COFOG function and writes one CSV file per country (`AAA.csv`; splits with `.xlsx` country files from versions up to 1.0.5 are still read by `aggregate`) |
73
+ | 5 | `aggregate` | `aggregate.py` | Applies the selection rule and adds the values and their provenance to the master |
74
+
75
+ ### Selection rule (`--sector "General government"`, the default)
76
+
77
+ | Tier | Value used |
78
+ | :--- | :--- |
79
+ | T1 | General government (GG), if reported |
80
+ | T2 | otherwise, if central government including social security (CGSS) is reported: CGSS + state + local |
81
+ | T3 | otherwise: central government excluding social security (or, if that is not reported, budgetary + extrabudgetary central government) + social security funds + state + local, over the sectors reported |
82
+
83
+ T2 and T3 are unconsolidated sums: transfers between levels of government (and, in T3, between central government units) that are recorded under the same function are counted twice. Two other methods are available: `--sector "Central government including social security"` (C1 = CGSS, C2 = central core + social security funds) and any single sector name (D = that sector alone).
84
+
85
+ ### Output columns
86
+
87
+ | Column | Content |
88
+ | :--- | :--- |
89
+ | `SortKey`, `Country`, `Year` | Panel keys |
90
+ | `--data-col` (default `DATA_<COFOG code>`, e.g. `DATA_05`, `DATA_07_3`) | Selected value, as a number |
91
+ | `--tier-col` (default `SOURCE_TIER_<suffix>`) | T1, T2, T3, C1, C2 or D |
92
+ | `--sectors-col` (default `SOURCE_SECTORS_<suffix>`) | Sectors that entered the value, e.g. `CGSS+LG` or `BCG+SG` |
93
+ | `--complete-col` (default `SOURCE_COMPLETE_<suffix>`) | `Y` or `N` (see below) |
94
+
95
+ The data column is named after the COFOG code unless it is given: `--cofog 05` writes `DATA_05`, `--cofog 07.3` writes `DATA_07_3`, and a label is mapped to its code. The tier, sectors and completeness columns take the suffix of the data column (`DATA_05` gives `SOURCE_TIER_05`, `SOURCE_SECTORS_05` and `SOURCE_COMPLETE_05`), so that several indicators in one master keep separate provenance. A data column given as `DATA_NEW` writes `SOURCE_TIER`, `SOURCE_SECTORS` and `SOURCE_COMPLETE`, as in earlier versions. The tool warns when a run would replace a column that is already in the master.
96
+
97
+ Completeness: a T2, T3 or C2 sum is `N` when it leaves out a level (state, local, social security funds, extrabudgetary central government) that the country reports in another year of the same series, or the central core. A T1 value is `N` when GG equals CGSS exactly while state or local government report positive spending in that year.
98
+
99
+ Further rules: reported zeros are kept unless `--zero-as-missing` is given; `--check-ranges` (`aggregate`, `run`) lists selected values that are negative, or above 100 for percentage transformations, and keeps them; shares of total outlays are never summed, so they only give T1 values; the IMF codes KOS and WBG are mapped to the ISO codes XKX and PSE. Values in domestic currency are added only when the series of a country agree in `SCALE`, `UNIT` and `CURRENCY` (letter case ignored); otherwise the country gets reported values only (T1, C1, D) and `aggregate` prints a warning.
100
+
101
+ ## 2. Installation
102
+
103
+ ```bash
104
+ git clone https://github.com/trongtin142/cofog-panel.git
105
+ cd cofog-panel
106
+ python -m venv venv
107
+ source venv/bin/activate # macOS and Linux
108
+ pip install -e . # the command-line tool
109
+ pip install -e ".[dev]" # adds pytest, to run the tests
110
+ pip install -e ".[gui]" # adds Streamlit, for the browser interface (streamlit run gui.py)
111
+ pip install -e ".[replication]" # adds statsmodels and matplotlib, for the scripts in replication/
112
+ ```
113
+
114
+ `cofog-panel --version` prints the installed version.
115
+
116
+ ### Windows (PowerShell)
117
+
118
+ ```powershell
119
+ python -m venv venv
120
+ .\venv\Scripts\Activate.ps1
121
+ pip install -e .
122
+ ```
123
+
124
+ If PowerShell refuses to run `Activate.ps1`, allow local scripts once with `Set-ExecutionPolicy -Scope CurrentUser RemoteSigned`, or use `venv\Scripts\activate.bat` in the Command Prompt. Put paths that contain spaces in double quotes, and use a backtick (`` ` ``) instead of a backslash to continue a command on the next line:
125
+
126
+ ```powershell
127
+ cofog-panel run --cofog 05 `
128
+ --source-file "D:\WORKING PAPER\data\gfs_raw_data.xlsx" `
129
+ --output-dir "D:\WORKING PAPER\output"
130
+ ```
131
+
132
+ ## 3. Usage
133
+
134
+ Defaults: `--source-file` is `data/gfs_raw_data.xlsx`, or `data/gfs_raw_data.csv` if there is no Excel file (relative to the current directory), `--lookup-file` is the country lookup bundled with the package, `--filter-type` is `"Percent of GDP"`, `--sector` is `"General government"` and the data column is named after the COFOG code. Only `--cofog` is required.
135
+
136
+ A second indicator is added to the same master by running the command again: `cofog-panel run --cofog 07` adds `DATA_07`, `SOURCE_TIER_07`, `SOURCE_SECTORS_07` and `SOURCE_COMPLETE_07`. An existing master is kept and the new columns are added to it. `--overwrite-master` rebuilds the master from the lookup file instead, which deletes the indicator columns it holds; the tool prints a warning when it does so.
137
+
138
+ Several functions can be added in one command by repeating the option: `cofog-panel run --cofog 05 --cofog 07`. Stages 1-3 run once; stages 4 and 5 run for each function in turn, so the source file is read once per function. Each function gets its own split directory (`intermediate_splits/05`, `intermediate_splits/07`) and its own `DATA_<code>` and provenance columns, exactly as if `run` had been called once per function. All `--cofog` values are checked before any file is written. `--data-col`, `--tier-col`, `--sectors-col` and `--complete-col` cannot be combined with several `--cofog` values, and two values that name the same function are refused.
139
+
140
+ Options:
141
+
142
+ * `--cofog` takes a division or group code (`05`, `07.3`) or the exact label used in the file (`"Environmental protection"`). A value that is neither is rejected at once, with the closest codes or labels. `cofog-panel list-cofog` lists the codes and labels in the file with the number of countries (it reads the whole file: seconds for the CSV download, about a minute for the Excel one). A well-formed code that is not in the built-in list (for example `07.99`) is also looked up in the file before the scan and rejected, with the nearest codes, if the file has no series for it; for an Excel download this takes under a second, for a CSV download a few seconds.
143
+ * `--filter-type` is one of `"Percent of GDP"`, `"Domestic currency"` and `"Percent of total outlays"`, in any letter case. Any other value is rejected at once.
144
+ * `--sector` selects the method described in Section 1; `cofog-panel list-sectors` lists the accepted values.
145
+
146
+ These checks run before the extract is read, so a mistake is reported within seconds rather than after the scan of the whole file.
147
+
148
+ Single stages:
149
+
150
+ ```bash
151
+ cofog-panel check-format # data/gfs_raw_data.xlsx or .csv by default
152
+ cofog-panel check-country-format # the bundled lookup by default
153
+ cofog-panel seed-master --output-dir output
154
+ cofog-panel split --cofog "07" --output-dir intermediate_splits
155
+ cofog-panel aggregate --master-file output/COFOG_MASTER_SCHEMA.xlsx --folder-path intermediate_splits
156
+ ```
157
+
158
+ `seed-master` always writes a new master, replacing a file of the same name. `split` writes only into a new or empty directory, or into one it created earlier (marked by a `.cofog_panel_split` file that also records the COFOG function, the `PUBLICATION_DATE` of the selected series and the extract's year span), where it replaces the country files of its previous run; it never deletes files elsewhere. `split` warns about rows of the transformation whose series code gives no COFOG function; the series of other indicators that the extract also carries (such as total outlays, `TOA_T` and `TOTOUT_T`), which have an empty `COFOG` label, are skipped without a warning. `aggregate` names the data column after the function recorded by `split` (`DATA_07` here) unless `--data-col` is given; a `--data-col` of the form `DATA_<code>` for another function than the recorded one gives a warning.
159
+
160
+ ### Errors and exit codes
161
+
162
+ Every error prints one line on what went wrong and one line starting with `Hint:` on how to fix it, for example:
163
+
164
+ ```text
165
+ ❌ Error: Invalid --filter-type 'per cent of GDP'.
166
+ Hint: Choose one of: 'Percent of GDP', 'Domestic currency', 'Percent of total outlays' (letter case does not matter).
167
+ ```
168
+
169
+ Unexpected errors are reported in the same form; `cofog-panel --debug <command> ...` shows the full traceback instead. Exit codes: `0` success, `1` data or option error (missing file, invalid `--cofog`, `--filter-type` or `--sector`, unusable split directory, unreadable country files), `2` command-line usage error (unknown option, missing `--cofog`).
170
+
171
+ ### Python
172
+
173
+ The selection rule can be called from Python:
174
+
175
+ ```python
176
+ from decimal import Decimal
177
+ from cofog_panel import resolve_value
178
+
179
+ value, tier, sectors = resolve_value({
180
+ "general government": None,
181
+ "central government including social security": Decimal("0.5"),
182
+ "local government": Decimal("0.3"),
183
+ })
184
+ # Decimal('0.8'), 'T2', ['central government including social security', 'local government']
185
+ ```
186
+
187
+ ## 4. Tests
188
+
189
+ ```bash
190
+ pip install -e ".[dev]"
191
+ pytest
192
+ ```
193
+
194
+ The tests of the browser interface (`tests/test_gui.py`) are skipped unless Streamlit is installed: `pip install -e ".[dev,gui]"` runs them as well.
195
+
196
+ ## 5. Data
197
+
198
+ The source file can be the CSV or the Excel download of IMF.STA:GFS_COFOG; both have the same columns and give the same panel. The IMF revises the dataset in place: a download made on 21 October 2026 still carries the version number 11.0.0 but has a new `PUBLICATION_DATE` and revised values. Keep the downloaded file with your results and report its `PUBLICATION_DATE` (a column of the file) when you cite the data. `split` warns if the selected series carry several dates, and `aggregate` prints the date it recorded.
199
+
200
+ The country lookup (`data/country_codes.xlsx`, also bundled in the package as `cofog_panel/data/country_codes.xlsx`) is an ISO 3166 lookup with Kosovo (XKX) added. The IMF GFS data are not covered by the MIT License: `DATA_LICENSE.md` gives the IMF terms, the required attribution and the notice on transformed data.
201
+
202
+ ## 6. Replication
203
+
204
+ `replication/` holds the scripts that reproduce the published analyses that use COFOG-Panel, each with its own README. They need the `[replication]` extra.
205
+
206
+ ## 7. Feedback and testing
207
+
208
+ Reports from users help improve the tool. After a run, please open a short [test report](https://github.com/trongtin142/cofog-panel/issues/new?template=test_report.yml) (version, operating system, command, outcome and what was unclear). Problems go in a [bug report](https://github.com/trongtin142/cofog-panel/issues/new?template=bug_report.yml), and questions or ideas in [Discussions](https://github.com/trongtin142/cofog-panel/discussions).
209
+
210
+ ## 8. Citation
211
+
212
+ See `CITATION.cff`. Please cite the DOI of the release you used.
213
+
214
+ ## 9. Authors and acknowledgements
215
+
216
+ COFOG-Panel is developed by Trong Tin Nguyen and Thanh-Tung Pham. Thanh-Tung Pham wrote the early prototype of the pipeline (March 2026), from which this repository grew, and, in version 1.1.0, the browser interface and the input validation. Cam Quyen Tran (GitHub `qynntrn`) contributed to an early version of the code (the `--sector` option and the notebook) and gave helpful comments.
@@ -0,0 +1,16 @@
1
+ """COFOG-Panel: harmonise IMF GFS COFOG data into reproducible country-year panels."""
2
+
3
+ __version__ = "1.1.0"
4
+
5
+ from .aggregate import is_complete, resolve_value, run_aggregation # noqa: E402
6
+ from .checks import verify_cofog_format, verify_country_format # noqa: E402
7
+ from .etl import split_data # noqa: E402
8
+ from .master_seed import DEFAULT_END_YEAR, DEFAULT_START_YEAR, prepare_master, seed_master # noqa: E402
9
+
10
+ __all__ = [
11
+ "__version__",
12
+ "is_complete", "resolve_value", "run_aggregation",
13
+ "verify_cofog_format", "verify_country_format",
14
+ "split_data",
15
+ "DEFAULT_START_YEAR", "DEFAULT_END_YEAR", "prepare_master", "seed_master",
16
+ ]