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

Little refactor and minor bug #41

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
31 changes: 22 additions & 9 deletions folktables/acs.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ def get_definitions(self, download=False):
return load_definitions(root_dir=self._root_dir, year=self._survey_year, horizon=self._horizon,
download=download)

def fillna_safe(x, value=-1):
Copy link
Member

@mrtzh mrtzh May 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you describe what the intended behavior of this function should be?

Note that np.nan_to_num will convert nans to 0.0 unless you set the keyword nan=value. See here.

So, currently, I believe this function will first convert all nans to zero and then pass an array without any nans to pandas.

Am I getting this wrong?

x = np.nan_to_num(x, value)
return pd.DataFrame(x).fillna(value=value).values

def adult_filter(data):
"""Mimic the filters in place for Adult data.
Expand Down Expand Up @@ -98,7 +101,7 @@ def adult_filter(data):
target_transform=lambda x: x > 50000,
group='RAC1P',
preprocess=adult_filter,
postprocess=lambda x: np.nan_to_num(x, -1),
postprocess=fillna_safe,
)

ACSEmployment = folktables.BasicProblem(
Expand All @@ -124,7 +127,7 @@ def adult_filter(data):
target_transform=lambda x: x == 1,
group='RAC1P',
preprocess=lambda x: x,
postprocess=lambda x: np.nan_to_num(x, -1),
postprocess=fillna_safe,
)

ACSHealthInsurance = folktables.BasicProblem(
Expand Down Expand Up @@ -159,7 +162,7 @@ def adult_filter(data):
target_transform=lambda x: x == 1,
group='RAC1P',
preprocess=lambda x: x,
postprocess=lambda x: np.nan_to_num(x, -1),
postprocess=fillna_safe,
)

def public_coverage_filter(data):
Expand Down Expand Up @@ -197,7 +200,7 @@ def public_coverage_filter(data):
target_transform=lambda x: x == 1,
group='RAC1P',
preprocess=public_coverage_filter,
postprocess=lambda x: np.nan_to_num(x, -1),
postprocess=fillna_safe,
)

def travel_time_filter(data):
Expand Down Expand Up @@ -233,9 +236,19 @@ def travel_time_filter(data):
target_transform=lambda x: x > 20,
group='RAC1P',
preprocess=travel_time_filter,
postprocess=lambda x: np.nan_to_num(x, -1),
postprocess=fillna_safe,
)


def mobility_filter(data):
"""
Filters for the employment prediction task
"""
df = data
df = df[df['AGEP'] > 18]
df = df[df['AGEP'] < 35]
return df

ACSMobility = folktables.BasicProblem(
features=[
'AGEP',
Expand Down Expand Up @@ -263,8 +276,8 @@ def travel_time_filter(data):
target="MIG",
target_transform=lambda x: x == 1,
group='RAC1P',
preprocess=lambda x: x.drop(x.loc[(x['AGEP'] <= 18) | (x['AGEP'] >= 35)].index),
postprocess=lambda x: np.nan_to_num(x, -1),
preprocess=mobility_filter,
postprocess=fillna_safe,
)

def employment_filter(data):
Expand Down Expand Up @@ -301,7 +314,7 @@ def employment_filter(data):
target_transform=lambda x: x == 1,
group='RAC1P',
preprocess=employment_filter,
postprocess=lambda x: np.nan_to_num(x, -1),
postprocess=fillna_safe,
)

ACSIncomePovertyRatio = folktables.BasicProblem(
Expand Down Expand Up @@ -331,5 +344,5 @@ def employment_filter(data):
target_transform=lambda x: x < 250,
group='RAC1P',
preprocess=lambda x: x,
postprocess=lambda x: np.nan_to_num(x, -1),
postprocess=fillna_safe,
)
3 changes: 2 additions & 1 deletion folktables/load_acs.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def load_definitions(root_dir, year=2018, horizon='1-Year', download=False):
year_string = year if horizon == '1-Year' else f'{year - 4}-{year}'
url = f'https://www2.census.gov/programs-surveys/acs/tech_docs/pums/data_dict/PUMS_Data_Dictionary_{year_string}.csv'

os.makedirs(base_datadir, exist_ok=True)
response = requests.get(url)
with open(file_path, 'wb') as handle:
handle.write(response.content)
Expand Down Expand Up @@ -191,4 +192,4 @@ def generate_categories(features, definition_df):
del mapping_dict[-99999999999999.0]

categories[feature] = mapping_dict
return categories
return categories
Loading