Skip to content

Commit

Permalink
minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreFCruz committed Jun 24, 2024
1 parent 5614d6a commit 990fc94
Show file tree
Hide file tree
Showing 4 changed files with 1,076 additions and 57 deletions.
10 changes: 4 additions & 6 deletions folktexts/acs/acs_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,8 @@
acs_pubcov_target_col = ColumnToText(
name=acs_public_coverage_threshold.apply_to_column_name("PUBCOV"),
short_description="public health coverage status",
value_map={
1: "Covered by public health insurance",
0: "Not covered by public health insurance",
},
question=acs_pubcov_qa,
use_value_map_only=True,
)

# DIS: Disability Status
Expand Down Expand Up @@ -341,6 +338,7 @@
2: "Multiple ancestry",
3: "Unclassified",
4: "Not reported",
8: "N/A (information suppressed for certain area codes)",
},
)

Expand Down Expand Up @@ -510,7 +508,7 @@
"PUMA",
short_description="Public Use Microdata Area (PUMA) code",
use_value_map_only=True,
value_map=lambda x: f"PUMA code: {int(x)}.",
value_map=lambda x: f"Public Use Microdata Area (PUMA) code: {int(x)}.",
# missing_value_fill="N/A (less than 16 years old)",
)

Expand All @@ -519,7 +517,7 @@
"POWPUMA",
short_description="place of work PUMA",
use_value_map_only=True,
value_map=lambda x: f"Place of work PUMA code: {int(x)}.",
value_map=lambda x: f"Public Use Microdata Area (PUMA) code for the place of work: {int(x)}.",
# missing_value_fill="N/A (not a worker, or worker who worked at home)",
)

Expand Down
41 changes: 23 additions & 18 deletions folktexts/col_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,38 @@ def __init__(

# If a `question` was provided and `value_map` was not
# > infer `value_map` from question (`value_map` is required for `__getitem__`)
if self._question is not None and self._value_map is None:
if isinstance(self._question, MultipleChoiceQA):
self._value_map = self._question.get_value_to_text_map()
else:
raise ValueError(
f"Cannot infer `ColumnToText` value map from the provided question; "
f"Must explicitly provide a `value_map` for column {self.name}.")
if (
self._question is not None
and isinstance(self._question, MultipleChoiceQA)
and self._value_map is None
):
self._value_map = self._question.get_value_to_text_map()

# If `value_map` was provided and `question` was not
# > infer `question` from value map (if possible)
elif self._value_map is not None and self._question is None:
if isinstance(self._value_map, dict):
self._question = MultipleChoiceQA.create_question_from_value_map(
column=self.name,
value_map=self._value_map,
attribute=self.short_description,
)
elif (
self._value_map is not None
and isinstance(self._value_map, dict)
and self._question is None
):
self._question = MultipleChoiceQA.create_question_from_value_map(
column=self.name,
value_map=self._value_map,
attribute=self.short_description,
)

# Else, warn if both were provided (as they may use inconsistent value maps)
elif self._value_map is not None and self._question is not None:
logging.info(
logging.debug(
f"Got both `value_map` and `question` for column '{self.name}'. "
f"Please make sure value mappings are consistent.")
f"Please make sure value mappings are consistent:"
f"\n- `value_map`: {self._value_map}"
f"\n- `question`: {self._question.choices}"
)

# Else, raise an error if neither were provided
# Else, log critical error -- ColumnToText object is incomplete
else:
raise ValueError(
logging.critical(
f"Must provide either a `value_map` or a `question` for column "
f"'{self.name}' but neither was provided.")

Expand Down
4 changes: 4 additions & 0 deletions folktexts/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def __init__(
"""
self._data = data
self._task = task
if not isinstance(self._task, TaskMetadata):
raise ValueError(
f"Invalid `task` type: {type(self._task)}. "
f"Expected `TaskMetadata`.")

self._test_size = test_size
self._val_size = val_size or 0
Expand Down
Loading

0 comments on commit 990fc94

Please sign in to comment.