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

support reading parquet files #40

Merged
merged 3 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"pandas",
"pyyaml>=5.1",
"vivarium",
"pyarrow",
]

interactive_requirements = [
Expand Down
7 changes: 6 additions & 1 deletion src/pseudopeople/entity_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ def __call__(
randomness_stream: RandomnessStream,
additional_key: Any,
) -> pd.Series:
column = column.copy()
# TODO: this is a temporary hack to account for all string columns having been made categorical
# We should record expected output dtype in the columns data structure
if column.dtype.name == "category":
column = column.astype(str)
else:
column = column.copy()
noise_level = configuration.row_noise_level
to_noise_idx = get_index_to_noise(
column, noise_level, randomness_stream, f"{self.name}_{additional_key}"
Expand Down
21 changes: 18 additions & 3 deletions src/pseudopeople/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,27 @@ def _generate_form(
Noised form data
"""
configuration_tree = get_configuration(configuration)
if isinstance(source, str):
source = Path(source)
if isinstance(source, pd.DataFrame):
data = source
elif isinstance(source, Path):
if source.suffix == ".hdf":
data = pd.read_hdf(source)
if not isinstance(data, pd.DataFrame):
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be moved to after the parquet elif?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure I can move it.

raise TypeError(f"File located at {source} must contain a pandas DataFrame.")
elif source.suffix == ".parquet":
data = pd.read_parquet(source)
else:
raise ValueError(
"Source path must either be a .hdf or a .parquet file. Provided "
f"{source.suffix}"
)
else:
data = pd.read_hdf(source)
if not isinstance(data, pd.DataFrame):
raise TypeError(f"File located at {source} must contain a pandas DataFrame.")
raise TypeError(
f"Source {source} must be either a pandas DataFrame or a path to a "
"file containing a pandas DataFrame."
)
return noise_form(form, data, configuration_tree, seed)


Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_noise_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def test_correct_forms_are_used(func, form, mocker):
pytest.skip(reason=f"TODO: implement function for {form.value} form")
mock = mocker.patch("pseudopeople.interface.noise_form")
mocker.patch("pseudopeople.interface.pd.read_hdf", return_value=pd.DataFrame())
_ = func("dummy/path")
_ = func("dummy/path.hdf")

assert mock.call_args[0][0] == form

Expand Down