Skip to content

Refactor labeling structure - #83

Open
diraol wants to merge 10 commits into
ipea:mainfrom
diraol:refactor/variable-metadata
Open

Refactor labeling structure#83
diraol wants to merge 10 commits into
ipea:mainfrom
diraol:refactor/variable-metadata

Conversation

@diraol

@diraol diraol commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Over here we are splitting code and configuration for label definition.

Mainly we are focusing on a shared code structure to evaluate and "apply" labels to the data objects, while keeping label definitions on configuration files (yaml files).

The shared code is a bit more efficient (one single mutate operation per "dataset" instead of one mutate per variable for each dataset).

The yaml based configuration for labels, besides being easier for humans to read (and contribute), it also allows the re-use of configurations (the same variable across many different cohorts can be defined only once and shared).

The proposed structure also allow us to give "alias" and "description" characteristics for the variables (besides its IDs).
Example: tests/testthat/fixtures/label_imports/population/2010-pt.yml

New features and metadata handling:

  • Added the label_variable_metadata() function, which returns optional aliases and descriptions for variables as declared in the label configuration. This helps users interpret variable names without altering the data columns. (NEWS.md, NAMESPACE) [1] [2]

Refactoring and code maintainability:

  • Refactored add_labels_emigration() and add_labels_families() to use a configuration-based approach (load_label_config() and apply_label_config()), replacing large blocks of hardcoded label mappings. This reduces code duplication and improves maintainability. (R/add_labels_emigration.R, R/add_labels_families.R) [1] [2]

Dependencies and configuration:

  • Added the yaml package to the list of imports in DESCRIPTION, supporting the new configuration-driven labeling logic.
  • Updated .gitattributes to clarify whitespace rules for YAML files, ensuring they are not affected by global indentation rules.

Add a versioned YAML schema for categorical labels and an internal API
to load, validate, cache, compile, and apply configurations.
Configurations preserve codes as strings, reject duplicate variables and
codes, distinguish null unmatched values from explicit fallback labels,
and compile all available mappings into one dplyr::mutate() while
retaining character output.

Add a population fixture and tests covering cache use, validation,
unknown codes, missing values, binary fallback behavior, and absent
columns. Add yaml to Imports and exempt YAML files from the repository
tab-indentation whitespace rule because YAML syntax requires spaces.

No existing add_labels_*() helper consumes the new API yet; this commit
establishes the tested contract before migrating the published label
maps.
Move the 2010 population mappings for urban/rural status (V1006), relationship to the household reference person (V0502), and sex (V0601) out of add_labels_population() into the installed YAML configuration.

Load and compile these mappings through apply_label_config() before the remaining legacy mutations so available configured columns are updated together in one lazy dplyr mutate().

Add coverage for published label fidelity, unmatched values, character output, and Arrow query laziness, including an integration test for add_labels_population().
Extract all six categorical mappings from add_labels_families() into the installed families/2000-pt label configuration.

Apply the configuration through one lazy mutation and cover mapped, unknown, and missing source values.
Extract the four 2010 mortality categorical mappings into the installed mortality label configuration and apply them through apply_label_config().

Add coverage for mapped, unknown, and missing values.
Extract all 2010 emigration categorical mappings, including the V3061 country map, into the installed emigration label configuration.

Apply configured columns in one lazy mutation and retain the legacy first-match result for the duplicated V3061 code 8000826.

Add coverage for mapped, unknown, and missing values.
Add the installed households/2000-pt configuration for all 2000 categorical labels and route 2000 household labeling through apply_label_config().

Keep the 2010 implementation unchanged in this commit so the annual migration remains independently runnable.
Extract all 2010 household categorical mappings into households/2010-pt and replace the remaining manual mutations with apply_label_config().

Represent the shared yes/no groups declaratively with unmatched: Não, preserving the legacy treatment of unknown and missing values for both supported years.

Add regression coverage for the YAML yes/no mappings.
@diraol

diraol commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

This is a BIG PR.

I've worked on it to have meaningfull commits, so I'd suggest to evaluate the PR commit by commit.

@diraol
diraol force-pushed the refactor/variable-metadata branch from f4af4b8 to 7e401be Compare September 7, 2026 21:07
Extend label configuration loading with relative imports, named definitions, and use references that normalize to the existing mapping representation before validation and mutation compilation.

Reject missing or unsafe imports, resolved paths outside the label root, duplicate definition names, unknown references, unsupported library fields, and circular imports.

Centralize the shared urban/rural V1006 map in _shared/geography.yml and reuse it across population, households, mortality, and emigration configurations.

Add fixtures and coverage for import expansion, collisions, path traversal, unknown definitions, and circular imports.
Add optional alias and description metadata to configured variables without changing physical dataset column names.

Expose label_variable_metadata() as a catalog of configured aliases and descriptions and document the metadata-only YAML schema.
@diraol
diraol force-pushed the refactor/variable-metadata branch from 7e401be to b458055 Compare September 7, 2026 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant