-
Notifications
You must be signed in to change notification settings - Fork 1
docs: 📝 pseudo code and docstring for write_resource_parquet()
#816
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9355eb9
docs: :memo: pseudo code and docstring for `write_resource_parquet()`
lwjohnst86 07ef602
Merge branch 'main' of https://github.com/seedcase-project/seedcase-s…
lwjohnst86 b316ae5
chore: :truck: move file into pseudocode folder
lwjohnst86 a9040a2
Merge branch 'main' of https://github.com/seedcase-project/seedcase-s…
lwjohnst86 63a8ed4
docs: :memo: add Mermaid diagram and rename to `build_`
lwjohnst86 fa28b98
docs: :building_construction: updated and finished pseudocode for `bu…
lwjohnst86 fa590b6
chore: :truck: rename and move to `interface/`
lwjohnst86 4f1c992
docs: :pencil2: clarifications from review
lwjohnst86 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
71 changes: 71 additions & 0 deletions
71
docs/design/interface/pseudocode/build_resource_parquet.py
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# ruff: noqa | ||
def build_resource_parquet( | ||
raw_files_path: list[Path], resource_properties: ResourceProperties | ||
) -> Path: | ||
"""Merge all raw resource file(s) and write into a Parquet file. | ||
|
||
This function takes the file(s) provided by `raw_files_path` and merges them into | ||
a `data.parquet` file. The Parquet file will be stored at the path found in `ResourceProperties.path`. | ||
While Sprout generally assumes | ||
that the files stored in the `resources/raw/` folder are already correctly | ||
structured and tidy, it still runs checks to ensure the data are correct | ||
by comparing to the properties. All data in the | ||
`resources/raw/` folder will be merged into one single data object and then | ||
written back to the Parquet file. The Parquet file will be overwritten. | ||
|
||
If there are any duplicate observation units in the data, only the most recent | ||
observation unit will be kept. This way, if there are any errors or mistakes | ||
in older raw files that have been corrected in later files, the mistake can still | ||
be kept, but won't impact the data that will actually be used. | ||
|
||
Examples: | ||
|
||
``` python | ||
import seedcase_sprout.core as sp | ||
|
||
sp.build_resource_parquet( | ||
raw_files_path=sp.path_resources_raw_files(1), | ||
resource_properties=sp.example_resource_properties, | ||
) | ||
``` | ||
|
||
Args: | ||
raw_files_path: A list of paths for all the raw files, mostly commonly stored in the | ||
`.csv.gz` format. Use `path_resource_raw_files()` to help provide the | ||
correct paths to the raw files. | ||
resource_properties: The `ResourceProperties` object that contains the properties | ||
of the resource you want to create the Parquet file for. | ||
|
||
Returns: | ||
Outputs the path object of the created Parquet file. | ||
""" | ||
# Not sure if this is the correct way to verify multiple files. | ||
[check_is_file(path) for path in raw_files_path] | ||
check_resource_properties(resource_properties) | ||
|
||
data = read_raw_files(raw_files_path) | ||
data = drop_duplicate_obs_units(data) | ||
|
||
# This function could be several functions or the one full function. | ||
check_data(data, resource_properties) | ||
|
||
return write_parquet(data, resource_properties["path"]) | ||
|
||
|
||
def write_parquet(data: DataFrame, path: Path) -> Path: | ||
return path | ||
|
||
|
||
def read_raw_files(paths: list[Path]) -> DataFrame: | ||
# Can read gzip files. | ||
data_list = [polars.read_csv(path) for path in paths] | ||
# Merge them all together. | ||
data = polars.concat(data_list) | ||
return data | ||
|
||
|
||
def drop_duplicate_obs_units(data: DataFrame) -> DataFrame: | ||
# Drop duplicates based on the observation unit, keeping only the most | ||
# recent one. This allows older raw files to contain potentially wrong | ||
# data that was corrected in the most recent file. | ||
return data.drop_duplicates() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsure of the naming here. And I'm unsure if it should output a DataFrame and have another function
write_resource_parquet()
that does the writing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, if that DataFrame output is used somewhere else, have 2 functions, otherwise have one function that does the writing as well?
build
orcreate
sounds okay to me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe for now, only the writing, and as we develop more and make the guides, it might need to be changed.