Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transforms for downloaded data #89

Open
sgreenbury opened this issue Oct 8, 2024 · 0 comments
Open

Add transforms for downloaded data #89

sgreenbury opened this issue Oct 8, 2024 · 0 comments

Comments

@sgreenbury
Copy link
Contributor

As included in #88, this issue is for implementing the ability to specify custom transforms to downloaded data. This captures initial ideas for design, transforms to implement and a proposed implementation from discussion with @stuartlynn.

Design goals

  • Can handle different data types (e.g. count, continuous, categorical, geometries, etc) and be extendable to new ones
  • Can be composed together with many transforms applied to many metrics in the downloaded data
  • Should be applicable in any order?
  • Transform can be validated using metadata before downloading?

Transforms to implement

The types of transform might include the following for examples:

  • Applying formulae to modify derived metrics
  • Specify additional metadata to be returned (e.g. include variable descriptions for categorical datatypes)
  • Enable column renaming
  • Be able to include spatial interpolation transforms (e.g. transform to h3 grid)

An initial implementation could include the above but should ideally be designed to enable the transforms available to be expanded and specified from other crates.

Proposed implementation

One approach within the current API (#88) would be to include a new enum type within DownloadParams:

struct DownloadParams {
     ...,
     transformations: Vec<PopgetterTransform>
}

and then create an enum of various transforms with the data of variants implementing a Transform trait with enum dispatch then being used:

#[enum_dispatch(Transform)]
enum PopgetterTransform {
    Census(CensusTransform)
    ...
}

struct CensusTransform {
    rename_column: Option<(&str, &str)>
}

#[enum_dispatch]
trait Transform {
    fn transform(&self, mut output: DataFrame) -> PolarsResult<DataFrame>;
}

impl Transform for CensusTransform {
    fn transform(&self, mut output: DataFrame) -> PolarsResult<DataFrame> {
        if let Some((old_name, new_name)) = self.rename_column.as_ref() {
            output.rename(old_name, new_name)?;
        }
        Ok(output)
    }
}

...

The transforms can then be applied at the end of the .download() method on a downloaded popgetter dataframe result:

download_params.transform.iter().try_fold(result, |result, transform| transform.transform(result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress:
Development

When branches are created from issues, their pull requests are automatically linked.

1 participant