open-sdg / sdg-build

Python package to convert SDG-related data and metadata between formats
MIT License
5 stars 23 forks source link

Alter indicators knowing data and metadata #324

Closed brockfanning closed 2 years ago

brockfanning commented 2 years ago

A commonly requested feature in Open SDG has been the ability to alter data with a custom Python function that has access to the metadata. Or vice versa, to alter metadata with a custom Python function that has access to the data. This has been difficult because our alteration functions apply to each input, and Open SDG uses many inputs, most of which are only for data or only for metadata.

This adds an "alter_indicator" option that is applied to the outputs (not the inputs) after the data and metadata have been completed merged together from all the inputs. The thing being altered is the Indicator object, which has data and metadata in it.

The existing "alter_meta" and "alter_data" options will continue to work the same as they have.

Example of usage:

from sdg.open_sdg import open_sdg_build

def my_indicator_alteration(indicator, context):
    # If the number of years is 2 or less, use a bar chart.
    if indicator.data.Year.nunique() <= 2:
        indicator.meta['graph_type'] = 'bar'
    return indicator

open_sdg_build(config='config_data.yml', alter_indicator=my_indicator_alteration)
codecov[bot] commented 2 years ago

Codecov Report

Merging #324 (4a92219) into 2.1.0-dev (1d13903) will increase coverage by 3.46%. The diff coverage is 87.50%.

@@              Coverage Diff              @@
##           2.1.0-dev     #324      +/-   ##
=============================================
+ Coverage      70.32%   73.79%   +3.46%     
=============================================
  Files             74       74              
  Lines           4536     4571      +35     
=============================================
+ Hits            3190     3373     +183     
+ Misses          1346     1198     -148     
Impacted Files Coverage Δ
sdg/open_sdg.py 78.62% <83.33%> (+12.37%) :arrow_up:
sdg/outputs/OutputBase.py 92.38% <93.75%> (+0.24%) :arrow_up:
sdg/OutputDocumentationService.py 68.93% <0.00%> (+0.42%) :arrow_up:
sdg/helpers/sdmx.py 32.99% <0.00%> (+0.50%) :arrow_up:
sdg/DisaggregationStatusService.py 73.64% <0.00%> (+0.67%) :arrow_up:
sdg/outputs/OutputCsvw.py 69.66% <0.00%> (+2.24%) :arrow_up:
sdg/inputs/InputBase.py 81.61% <0.00%> (+5.14%) :arrow_up:
sdg/data_schemas/DataSchemaInputBase.py 53.33% <0.00%> (+6.66%) :arrow_up:
sdg/Indicator.py 83.62% <0.00%> (+11.63%) :arrow_up:
... and 3 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

LucyGwilliamAdmin commented 2 years ago

@brockfanning I've tested this and looks to be working: http://uk-sdg-feature-branches.s3-website.eu-west-2.amazonaws.com/feature-sdg-build-324