hamilton icon indicating copy to clipboard operation
hamilton copied to clipboard

config.when decorator with numeric logic input

Open latlan1 opened this issue 3 years ago • 3 comments

Is your feature request related to a problem? Please describe. I would like to avoid if-else statements by using the config.when decorator on columns with numeric data, e.g. being able to produce new columns based on a variety of comparisons like whether inventory column is greater than, equal to or less than a certain threshold value.

Describe the solution you'd like

I would like to use config.when decorator to with numeric column e.g. inventory >300 instead of string comparisons using when_in, when_not_in. It would be great if I could do something like below:

import pandas as pd
from hamilton.function_modifiers import config

# returns inventory values exceeding threshold
@config.when(inventory>=1000)
def inventory_greater_eq_than__1000(inventory: pd.Series) -> pd.Series:
    """TODO:
        :param inventory:
        :return: inventory values exceeding threshold
    """
    pass

@config.when(inventory>=260)
def inventory_greater_eq_than__260(inventory: pd.Series) -> pd.Series:
    """TODO:
        :param inventory:
        :return: inventory values exceeding threshold
    """
    pass

latlan1 avatar Nov 16 '21 15:11 latlan1

Hey @latlan1 -- interesting use-case! To dig in more -- inventory is a column as well, right? So in the final DAG that gets created, would both inventory_greater_eq_than__260 and inventory_greater_eq_than__1000 exist? Or would only one of them exist?

The way I've thought of config is that it selects what functions end up in the final DAG. So when_in and when_not_in both make sense. when_greater_than_or_equal (like what you had) would make sense if there's some config value we're comparing to a static. If its a column it seems like it's almost a column that's split into pieces (a filter), and the @parametrized decorator might help there.

elijahbenizzy avatar Nov 17 '21 05:11 elijahbenizzy

Thanks Elijah, I saw the release of your new stitch fix library and wanted to use it because of readability and coolness. But was challenged by how to add these numeric filter-like columns in a way that would be easy to change, and extensible.

For my use case, I will likely only have 1 greater_eq_than filter for each inventory column, but I could also see a case where multiple versions are generated at first before deciding on a single filter.

Thanks for clarifying the difference between config and parametrized. It makes more sense now!

On Wed, Nov 17, 2021 at 12:06 AM Elijah ben Izzy @.***> wrote:

Hey @latlan1 https://github.com/latlan1 -- interesting use-case! To dig in more -- inventory is a column as well, right? So in the final DAG that gets created, would both inventory_greater_eq_than__260 and inventory_greater_eq_than__1000 exist? Or would only one of them exist?

The way I've thought of config is that it selects what functions end up in the final DAG. So when_in and when_not_in both make sense. when_greater_than_or_equal (like what you had) would make sense if there's some config value we're comparing to a static. If its a column it seems like it's almost a column that's split into pieces (a filter), and the @parametrized decorator might help there.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/stitchfix/hamilton/issues/24#issuecomment-971195595, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB46N5NOOLDWCQ7MQVHHCHDUMMZ57ANCNFSM5IETAX2Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

-- Regards,

Lorre Atlan, PhD

latlan1 avatar Nov 17 '21 12:11 latlan1

@latlan1 @elijahbenizzy any follow up here? E.g. @latlan1 were you able to achieve what you wanted? If you didn't, would love more details on the use case.

skrawcz avatar Aug 22 '22 23:08 skrawcz

So yeah, I think this can generally be done with lower level filtering functions, E.G. doing the filtering withing the function. Some piece could be done with this, however: https://github.com/stitchfix/hamilton/issues/208. I'm going to close this for now -- @latlan1 if you have some ideas you'd like to push, please reach out and we can discuss!

elijahbenizzy avatar Oct 29 '22 17:10 elijahbenizzy

Thank you all!

On Sat, Oct 29, 2022 at 12:40 PM Elijah ben Izzy @.***> wrote:

Closed #24 https://github.com/stitchfix/hamilton/issues/24 as completed.

— Reply to this email directly, view it on GitHub https://github.com/stitchfix/hamilton/issues/24#event-7697448794, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB46N5MN4L6ZZNSV5A2EPBDWFVOSBANCNFSM5IETAX2Q . You are receiving this because you were mentioned.Message ID: @.***>

-- Regards,

Lorre Atlan, PhD

latlan1 avatar Oct 30 '22 17:10 latlan1