-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0bca6d
commit 8597ef1
Showing
8 changed files
with
126 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"""Threshold instances for ACS / folktables tasks. | ||
""" | ||
from folktexts.threshold import Threshold | ||
|
||
|
||
# ACSIncome task | ||
acs_income_threshold = Threshold(50_000, ">") | ||
|
||
# ACSPublicCoverage task | ||
acs_publiccoverage_threshold = Threshold(1, "==") | ||
|
||
# ACSMobility task | ||
acs_mobility_threshold = Threshold(1, "==") | ||
|
||
# ACSEmployment task | ||
acs_employment_threshold = Threshold(1, "==") | ||
|
||
# ACSTravelTime task | ||
acs_traveltime_threshold = Threshold(20, ">") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"""Helper function for defining binarization thresholds. | ||
""" | ||
from __future__ import annotations | ||
import operator | ||
import dataclasses | ||
from typing import ClassVar | ||
|
||
import pandas as pd | ||
|
||
|
||
@dataclasses.dataclass(frozen=True) | ||
class Threshold: | ||
"""A class to represent a threshold value and its comparison operator.""" | ||
value: float | int | ||
op: str | ||
|
||
valid_ops: ClassVar[dict] = { | ||
">": operator.gt, | ||
"<": operator.lt, | ||
">=": operator.ge, | ||
"<=": operator.le, | ||
"==": operator.eq, | ||
} | ||
|
||
def __post_init__(self): | ||
if self.op not in self.valid_ops.keys(): | ||
raise ValueError(f"Invalid comparison operator '{self.op}'.") | ||
|
||
def __str__(self): | ||
return f"{self.op}{self.value}" | ||
|
||
def apply_to_column_data(self, data: float | int | pd.Series) -> bool: | ||
"""Applies the threshold operation to a pandas Series or scalar value.""" | ||
if isinstance(data, pd.Series): | ||
return self.valid_ops[self.op](data, self.value).astype(int) | ||
elif isinstance(data, (float, int)): | ||
return int(self.valid_ops[self.op](data, self.value)) | ||
else: | ||
raise TypeError(f"Invalid data type '{type(data)}'.") | ||
|
||
def apply_to_column_name(self, column_name: str) -> str: | ||
"""Standardizes naming of thresholded columns.""" | ||
return column_name + str(self) |