-
Notifications
You must be signed in to change notification settings - Fork 109
Option to write output tables as parquet files #763
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2f61e36
Adding parquet file support for output tables.
stefancoe 4b33bca
Adding missing import- pydantic validator
stefancoe c636cfb
Updated write_tables doc string.
stefancoe 45f74ea
Code formatted with black.
stefancoe 66b2063
Merge branch 'develop' into outputs_parquet
jpn-- d18ceef
simplify code
jpn-- File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
import pandas as pd | ||
import pyarrow as pa | ||
import pyarrow.csv as csv | ||
import pyarrow.parquet as parquet | ||
|
||
from activitysim.core import configuration, workflow | ||
from activitysim.core.workflow.checkpoint import CHECKPOINT_NAME | ||
|
@@ -226,8 +227,13 @@ def write_data_dictionary(state: workflow.State) -> None: | |
@workflow.step | ||
def write_tables(state: workflow.State) -> None: | ||
""" | ||
Write pipeline tables as csv files (in output directory) as specified by output_tables list | ||
in settings file. | ||
Write pipeline tables as csv or parquet files (in output directory) as specified | ||
by output_tables list in settings file. Output to parquet or a single h5 file is | ||
also supported. | ||
|
||
'h5_store' defaults to False, which means the output will be written out to csv. | ||
'file_type' defaults to 'csv' but can also be used to specify 'parquet' or 'h5'. | ||
When 'h5_store' is set to True, 'file_type' is ingored and the outputs are written to h5. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The old setting should now be deprecated. I opened an issue #791 to address that separate from this PR. |
||
|
||
'output_tables' can specify either a list of output tables to include or to skip | ||
if no output_tables list is specified, then all checkpointed tables will be written | ||
|
@@ -261,6 +267,16 @@ def write_tables(state: workflow.State) -> None: | |
tables: | ||
- households | ||
|
||
To write tables to parquet files, use the file_type setting: | ||
|
||
:: | ||
|
||
output_tables: | ||
file_type: parquet | ||
action: include | ||
tables: | ||
- households | ||
|
||
Parameters | ||
---------- | ||
output_dir: str | ||
|
@@ -277,6 +293,7 @@ def write_tables(state: workflow.State) -> None: | |
tables = output_tables_settings.tables | ||
prefix = output_tables_settings.prefix | ||
h5_store = output_tables_settings.h5_store | ||
file_type = output_tables_settings.file_type | ||
sort = output_tables_settings.sort | ||
|
||
registered_tables = state.registered_tables() | ||
|
@@ -383,14 +400,18 @@ def map_func(x): | |
): | ||
dt = dt.drop([f"_original_{lookup_col}"]) | ||
|
||
if h5_store: | ||
if h5_store or file_type == "h5": | ||
file_path = state.get_output_file_path("%soutput_tables.h5" % prefix) | ||
dt.to_pandas().to_hdf( | ||
str(file_path), key=table_name, mode="a", format="fixed" | ||
) | ||
|
||
else: | ||
file_name = f"{prefix}{table_name}.csv" | ||
file_name = f"{prefix}{table_name}.{file_type}" | ||
file_path = state.get_output_file_path(file_name) | ||
|
||
# include the index if it has a name or is a MultiIndex | ||
csv.write_csv(dt, file_path) | ||
if file_type == "csv": | ||
csv.write_csv(dt, file_path) | ||
else: | ||
jpn-- marked this conversation as resolved.
Show resolved
Hide resolved
|
||
parquet.write_table(dt, file_path) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.