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

Allow passing of iterable arguments downstream. #12

Open
jakobwes opened this issue Jul 29, 2022 · 1 comment
Open

Allow passing of iterable arguments downstream. #12

jakobwes opened this issue Jul 29, 2022 · 1 comment
Labels
enhancement New feature or request question Further information is requested

Comments

@jakobwes
Copy link
Collaborator

jakobwes commented Jul 29, 2022

Option:

  • add iterable_args option in apply-function. Unpacking function from ISIMIP used if is not None
  • as proof of concept implement glm-based debiasing
  • *iterable_args or **iterable_args?
@jakobwes
Copy link
Collaborator Author

jakobwes commented Aug 1, 2022

Need to discuss whether this is really necessary: do we need locationwise arguments also for glm based debiasing?

    # Helpers
    @staticmethod
    def _unpack_locationwise_args_and_get_locationwise_info(locationwise_args, i, j):
        return {
            key: value[
                :,
                i,
                j,
            ]
            for key, value in locationwise_args.items()
        }

    @staticmethod
    def map_over_locations(func, output_size, obs, cm_hist, cm_future, locationwise_args=None, **kwargs):
        output = np.empty(output_size, dtype=cm_future.dtype)
        if locationwise_args is None:
            for i, j in tqdm(np.ndindex(obs.shape[1:]), total=np.prod(obs.shape[1:])):
                output[:, i, j] = func(obs[:, i, j], cm_hist[:, i, j], cm_future[:, i, j], **kwargs)
        else:
            for i, j in tqdm(np.ndindex(obs.shape[1:]), total=np.prod(obs.shape[1:])):
                output[:, i, j] = func(
                    obs[:, i, j],
                    cm_hist[:, i, j],
                    cm_future[:, i, j],
                    Debiaser._unpack_locationwise_args_and_get_locationwise_info(locationwise_args, i, j),
                    **kwargs,
                )

        return output

    # Apply functions:
    def apply_location(self, obs, cm_hist, cm_future, locationwise_args, **kwargs):
        raise NotImplementedError(
            "apply_location is an abstract method which needs to be overriden in derived classes."
        )

    def apply(self, obs, cm_hist, cm_future, locationwise_args=None, **kwargs):
        print("----- Running debiasing -----")
        Debiaser.check_inputs(obs, cm_hist, cm_future)

        output = Debiaser.map_over_locations(
            self.apply_location,
            output_size=cm_future.shape,
            obs=obs,
            cm_hist=cm_hist,
            cm_future=cm_future,
            locationwise_args=locationwise_args,
            **kwargs,
        )
        return output

@jakobwes jakobwes added enhancement New feature or request question Further information is requested labels Aug 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant