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

update apply_classification task and tests #224

Draft
wants to merge 2 commits into
base: legacy
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ci/envs/py3.10-ecoscope1.8.3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ dependencies:
- numpy<2
- pip
- pip:
- ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/ecoscope@e0f77e7
- ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/ecoscope@510771e
2 changes: 1 addition & 1 deletion ci/envs/py3.11-ecoscope1.8.3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ dependencies:
- numpy<2
- pip
- pip:
- ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/ecoscope@e0f77e7
- ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/ecoscope@510771e
2 changes: 1 addition & 1 deletion ci/envs/py3.12-ecoscope1.8.3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ dependencies:
- numpy<2
- pip
- pip:
- ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/ecoscope@e0f77e7
- ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/ecoscope@510771e
26 changes: 20 additions & 6 deletions ecoscope_workflows/tasks/transformation/_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ class NaturalBreaksArgs(SharedArgs):
initial: int = 10


class LabelOptions(BaseModel):
label_prefix: str = ""
label_suffix: str = ""


class CustomLabels(LabelOptions):
labels: list[str]


class DefaultLabels(LabelOptions):
label_ranges: bool = False
label_decimals: int = 1


@task
def apply_classification(
df: Annotated[
Expand All @@ -42,12 +56,12 @@ def apply_classification(
description="The dataframe column that will contain the classification values."
),
] = None,
labels: Annotated[
list[str] | SkipJsonSchema[None],
label_options: Annotated[
DefaultLabels | CustomLabels | SkipJsonSchema[None],
Field(
description="Labels of classification bins, uses bin edges if not provied."
description="Optional specification or formatting of classification values."
),
] = None,
] = DefaultLabels(),
scheme: Annotated[
Literal[
"equal_interval",
Expand Down Expand Up @@ -76,7 +90,7 @@ def apply_classification(
input_column_name (str): The dataframe column to classify.
output_column_name (str): The dataframe column that will contain the classification.
Defaults to "<input_column_name>_classified"
labels (list[str]): labels of bins, use bin edges if labels==None.
label_options (DefaultLabels | CustomLabels): Optional specification or formatting of classification values.
scheme (str): Classification scheme to use [equal_interval, natural_breaks, quantile, std_mean, max_breaks,
fisher_jenks]

Expand Down Expand Up @@ -110,8 +124,8 @@ def apply_classification(
df,
input_column_name=input_column_name,
output_column_name=output_column_name,
labels=labels,
scheme=scheme,
**label_options.model_dump(exclude_none=True),
**classification_options.model_dump(exclude_none=True),
)

Expand Down
29 changes: 22 additions & 7 deletions tests/tasks/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
MaxBreaksArgs,
StdMeanArgs,
NaturalBreaksArgs,
CustomLabels,
DefaultLabels,
)


Expand Down Expand Up @@ -36,21 +38,34 @@ def test_color_map():


@pytest.mark.parametrize(
"scheme, args",
"scheme, classification_args, label_args",
[
("equal_interval", SharedArgs(k=3)),
("max_breaks", MaxBreaksArgs(k=4, min_diff=20)),
("natural_breaks", NaturalBreaksArgs(k=4, initial=3)),
("std_mean", StdMeanArgs(multiples=[-1, 1], anchor=False)),
(
"equal_interval",
SharedArgs(k=3),
DefaultLabels(label_ranges=True, label_decimals=0),
),
(
"max_breaks",
MaxBreaksArgs(k=4, min_diff=20),
DefaultLabels(label_prefix="_", label_suffix="_"),
),
(
"natural_breaks",
NaturalBreaksArgs(k=4, initial=3),
CustomLabels(labels=["One", "Two", "Three", "Four"]),
),
("std_mean", StdMeanArgs(multiples=[-1, 1], anchor=False), DefaultLabels()),
],
)
def test_apply_classification_equal_interval(test_df, scheme, args):
def test_apply_classification(test_df, scheme, classification_args, label_args):
result = apply_classification(
df=test_df,
input_column_name="column_name",
scheme=scheme,
output_column_name="classified",
classification_options=args,
label_options=label_args,
classification_options=classification_args,
)

assert "classified" in result.columns