-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor to library-first architecture with thin CLI wrapper #32
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
emmleroy
merged 60 commits into
emmleroy-v1.0.0
from
emmleroy-sc-20240-library-sc-20270-logging-v2
Jul 8, 2026
Merged
Changes from all commits
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
6c74da7
Use namedtuple for FLAG and FLAG_CRITERIA
emmleroy 02606fb
Replace single-filter flag command with multi-filter flagging
emmleroy 28197e1
Add util for determining the timestamp col
emmleroy 1f8f9ba
Refactor - rename bit to flag_value, fix click commands
emmleroy 79e26bf
Update tests
emmleroy 003d5ad
Fix indentation
emmleroy d4b2c97
Remove relative imports
emmleroy 22b7560
Remove arisense flagging functionality
emmleroy 61f6d34
Modify safe_loader() to add device serial number to rawSD columns
emmleroy f0b40aa
Add func to infer data model from the sn column
emmleroy 5ad1d1f
Add func to infer data source from the dataframe sampling frequency (…
emmleroy 045e34b
Infer data model and source in flag_dataframe() instead of taking args
emmleroy d04686a
Define identical FLAG_DEFINITIONS for QuantAQ products and modifiable…
emmleroy e19c028
Convert model-specific FLAG_DEFINITIONS into one generic list
emmleroy fe018eb
Remove tests for rawSD flagging - currently not implemented
emmleroy 30646f7
Upgrade deps to Click8.x
emmleroy 89a2ce6
Port expunge_dataframe() and echo_flag_table() from Isoprene
emmleroy 49c186a
Move flag_summary and echo_flag_table to flag.py and fix all_nan_colu…
emmleroy cae63d5
Remove arisense from test_expunge() and add some expunge tests from I…
emmleroy 2e67857
Remove feather tests and fix --wind unpack error in resample CLI
emmleroy eb2c043
Remove data model and data source as required args to flag_command
emmleroy 04d34ed
Remove data model and data source as required args in cli logic and t…
emmleroy 9e729a7
Fix cli args again
emmleroy cf1fe9a
Add jupyter for playbook notebooks
emmleroy 61ccdea
Rename quantaq_cli to quantaq_py
emmleroy 7d09cc4
Add logging configuration file
emmleroy b11c7c5
Create importable concat_files() function and move concat_command() t…
emmleroy 554d0fe
Create importable merge_files() function and move merge_command() to …
emmleroy 6afab69
Create importable resample_dataframe() function and move resample_com…
emmleroy 736358d
Add importable flag_dataframe() and echo_flag_table(), move flag_comm…
emmleroy 6db884c
Create importable expunge_dataframe(), move expunge_command to cli.py
emmleroy ace9101
Create importable clean_file(), move clean_command to cli.py
emmleroy 134f7b0
Remove feather functionality for parquet
emmleroy 63fee0d
Make add_sn_column an optional arg
emmleroy ee78d7f
Remove console/commands sub-dirs
emmleroy 4f332ba
Add tests for parquet file support
emmleroy bfe66fb
Move library function into quantaq_py/toolkit
emmleroy c50de58
Add pandera
emmleroy a625733
Add Pandera schema checks, always add sn column when safe_loading raw…
emmleroy 9966b89
Define dtypes and schema checks in schema.py
emmleroy 8162df4
Remove tests for arisense
emmleroy 349b388
Update docstrings with types, add helper for fixing timestamps
emmleroy 2ca1564
Add MOD-UFP to sn_to_model docstrings and tests
emmleroy d838aaa
Infer data source based on the frequency of tdiffs, add tests for mod…
emmleroy 583a903
Fix: only local timezone if tscol is timestamp or timestamp_iso, and …
emmleroy 84d4fc0
Fix: raise error if no files found before fixing timestamps and log e…
emmleroy 44a89e5
Remove echo_flag_table() from flag_command, rely on logging in _add_f…
emmleroy 7cf009c
Create a save_file utility function
emmleroy e739f6d
Passed desired suffixes for overlapping columns, and add option to ke…
emmleroy a49159a
Fix: merge_df bug and add error for attempting to merge >2 files
emmleroy 1e1d347
Ensure all errors are logged and that errors are referenced when raised
emmleroy c3a2060
More informative docstring for clean_dataframe()
emmleroy 405bc1d
Move drop_unnamed() to a helper func used in safe_load and clean_data…
emmleroy 69b39e2
Update docstring
emmleroy 1311341
Specify that u/v columns don't need be present in dataframe, but the …
emmleroy 537bb1e
Add intercept handler to route standard logging calls through loguru
emmleroy e981a49
Rename quantaq_py to quantaq_tools
emmleroy 8839d93
Set log level for noisy loggers to WARNING
emmleroy a294b6a
Add init file
emmleroy 06e2356
Undo package rename
emmleroy 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| from quantaq_cli.toolkit.concat import concat_files | ||
| from quantaq_cli.toolkit.merge import merge_files | ||
| from quantaq_cli.toolkit.resample import resample_dataframe | ||
| from quantaq_cli.toolkit.flag import flag_dataframe, echo_flag_table, flag_summary | ||
| from quantaq_cli.toolkit.expunge import expunge_dataframe | ||
| from quantaq_cli.toolkit.munge import clean_dataframe | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "concat_files", | ||
| "merge_files", | ||
| "resample_dataframe", | ||
| "flag_dataframe", | ||
| "echo_flag_table", | ||
| "expunge_dataframe", | ||
| "flag_summary", | ||
| "clean_dataframe" | ||
| ] |
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,242 @@ | ||
| from pathlib import Path | ||
|
|
||
| import pkg_resources | ||
| import rich_click as click | ||
| from loguru import logger | ||
|
|
||
| from quantaq_cli import concat_files, merge_files, resample_dataframe, clean_dataframe | ||
| from quantaq_cli import flag_dataframe, echo_flag_table, expunge_dataframe | ||
| from quantaq_cli.exceptions import InvalidFileExtension | ||
| from quantaq_cli.log import configure_logging, LOG_LEVELS | ||
| from quantaq_cli.toolkit.resample import WIND_COLUMNS | ||
| from quantaq_cli.utilities import safe_load | ||
| from quantaq_cli.variables import SUPPORTED_MODELS, SUPPORTED_SOURCES | ||
|
|
||
|
|
||
| CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help']) | ||
|
|
||
| __version__ = pkg_resources.get_distribution('quantaq_cli').version | ||
|
|
||
|
|
||
| @click.group(context_settings=CONTEXT_SETTINGS) | ||
| @click.version_option(__version__) | ||
| @click.pass_context | ||
| def main(ctx): | ||
| pass | ||
|
|
||
|
|
||
| def save_file(df, output): | ||
| """Helper func to save a DataFrame to disk as CSV or parquet. | ||
|
|
||
| Args: | ||
| df (pd.DataFrame): DataFrame to save | ||
| output (str): Destination filepath. | ||
| """ | ||
| logger.info("Saving file to {}", output) | ||
| output = Path(output) | ||
| if output.suffix == ".csv": | ||
| df.to_csv(output, index=False) | ||
| else: | ||
| df.to_parquet(output, index=False) | ||
|
|
||
| def check_extension(output): | ||
| """Helper func to check if the output extension is either a csv or parquet | ||
| format. | ||
|
|
||
| Args: | ||
| output (str): Destination filepath. | ||
| """ | ||
| output = Path(output) | ||
| if output.suffix not in (".csv", ".parquet"): | ||
| error = InvalidFileExtension(f"Invalid file extension; got {output.suffix!r}") | ||
| logger.error(error) | ||
| raise error | ||
| return output | ||
|
|
||
|
|
||
| @click.command("concat") | ||
| @click.argument("files", nargs=-1, type=click.Path()) | ||
| @click.option("-o", "--output", default="output.csv", help="The filepath where you would like to save the file", type=str) | ||
| @click.option("--log-level", default="INFO", | ||
| type=click.Choice(LOG_LEVELS, case_sensitive=False), | ||
| help="loguru log level (default: INFO)") | ||
| def concat_command(files, output, log_level): | ||
| """Concat FILES and save to OUTPUT.""" | ||
| configure_logging(log_level) | ||
|
|
||
| # make sure the extension is either a csv or parquet format | ||
| check_extension(output) | ||
|
|
||
| # concat everything in filepath | ||
| logger.info("Files to read: {}", files) | ||
|
|
||
| df = concat_files(files) | ||
|
|
||
| # save the file | ||
| save_file(df, output) | ||
|
|
||
|
|
||
| @click.command("merge", short_help="merge two files together on their timestamp") | ||
| @click.argument("files", nargs=-1, type=click.Path()) | ||
| @click.option("-ts", "--tscol", default="timestamp", help="The column by which to join the files", type=str) | ||
| @click.option("-o", "--output", default="output.csv", help="The filepath where you would like to save the file", type=str) | ||
| @click.option("--log-level", default="INFO", | ||
| type=click.Choice(LOG_LEVELS, case_sensitive=False), | ||
| help="loguru log level (default: INFO)") | ||
| def merge_command(files, output, tscol, log_level): | ||
| """Merge FILES together and save to OUTPUT.""" | ||
| configure_logging(log_level) | ||
|
|
||
| # make sure the extension is either a csv or parquet format | ||
| check_extension(output) | ||
|
|
||
| # merge everything in filepath | ||
| logger.info("Files to read: {}", files) | ||
|
|
||
| df = merge_files(files, tscol) | ||
|
|
||
| # save the file | ||
| save_file(df, output) | ||
|
|
||
|
|
||
| @click.command("resample", short_help="up/down sample data") | ||
| @click.argument("file", nargs=1, type=click.Path()) | ||
| @click.argument("rule", nargs=1, type=str) | ||
| @click.option("-o", "--output", default="output.csv", help="The filepath where you would like to save the file", type=str) | ||
| @click.option("--on", default="timestamp", help="Name of the datetime column to resample over.", type=str) | ||
| @click.option("--by", default=None, help="Optional column(s) to group by first") | ||
| @click.option( | ||
| "--wind", | ||
| nargs=4, | ||
| type=str, | ||
| default=("wx_u", "wx_v", "wx_ws", "wx_wd"), | ||
| help="(u, v, speed, direction) column names. The u/v columns don't need to be present in the dataframe, but the column names need to be specified.", | ||
| ) | ||
| @click.option("--numeric_how", default="mean", help="Aggregation for numeric columns.") | ||
| @click.option("--nonnumeric_how", default="first", help="Aggregation for non-numeric columns.") | ||
| @click.option("--log-level", default="INFO", | ||
| type=click.Choice(LOG_LEVELS, case_sensitive=False), | ||
| help="loguru log level (default: INFO)") | ||
| def resample_command(file, rule, output, log_level, **kwargs): | ||
| """Resample FILE at INTERVAL and save to OUTPUT.""" | ||
| configure_logging(log_level) | ||
|
|
||
| on = kwargs.pop("on", "timestamp") | ||
| by = kwargs.pop("by", None) | ||
| wind = kwargs.pop("wind", WIND_COLUMNS) | ||
| numeric_how = kwargs.pop("numeric_how", "mean") | ||
| nonnumeric_how = kwargs.pop("nonnumeric_how", "first") | ||
|
|
||
| # make sure the extension is either a csv or parquet format | ||
| check_extension(output) | ||
|
|
||
| logger.info("File to read: {}", file) | ||
|
|
||
| # load the file | ||
| df = safe_load(file) | ||
|
|
||
| # if column to resample over needs to be made a datetime obj, do so | ||
| if on not in df.columns: | ||
| error = ValueError(f"Invalid column name for the timestamp; got {on!r}") | ||
| logger.error(error) | ||
| raise error | ||
|
|
||
| # resample | ||
| df = resample_dataframe( | ||
| df, | ||
| rule, | ||
| on=on, | ||
| by=by, | ||
| wind=wind, | ||
| numeric_how=numeric_how, | ||
| nonnumeric_how=nonnumeric_how, | ||
| ) | ||
|
|
||
| # save the file | ||
| save_file(df, output) | ||
|
|
||
|
|
||
| @click.command("flag") | ||
| @click.argument("file", nargs=1, type=click.Path()) | ||
| @click.option("-o", "--output", default="output.csv", help="The filepath where you would like to save the file", type=str) | ||
| @click.option("--log-level", default="INFO", | ||
| type=click.Choice(LOG_LEVELS, case_sensitive=False), | ||
| help="loguru log level (default: INFO)") | ||
| def flag_command(file, output, log_level): | ||
| """Flag FILE and save to OUTPUT.""" | ||
| configure_logging(log_level) | ||
|
|
||
| # make sure the extension is either a csv or parquet format | ||
| check_extension(output) | ||
|
|
||
| logger.info("File to read: {}", file) | ||
|
|
||
| # load the file | ||
| df = safe_load(file) | ||
|
|
||
| # flag the dataframe | ||
| df = flag_dataframe(df) | ||
|
|
||
| # save the file | ||
| save_file(df, output) | ||
|
|
||
|
|
||
| @click.command("expunge") | ||
| @click.argument("file", nargs=1, type=click.Path()) | ||
| @click.option("-d", "--dry-run", is_flag=True, help="Print table to screen and bypass file save") | ||
| @click.option("-o", "--output", default="output.csv", help="The filepath where you would like to save the file", type=str) | ||
| @click.option("--log-level", default="INFO", | ||
| type=click.Choice(LOG_LEVELS, case_sensitive=False), | ||
| help="loguru log level (default: INFO)") | ||
| def expunge_command(file, output, log_level, dry_run): | ||
| """Expunge FILE and save to OUTPUT.""" | ||
| configure_logging(log_level) | ||
|
|
||
| # make sure the extension is either a csv or parquet format | ||
| check_extension(output) | ||
|
|
||
| logger.info("Expunging data for {}", file) | ||
|
|
||
| df = safe_load(file) | ||
| show_summary = dry_run or log_level == "INFO" | ||
|
|
||
| if show_summary: | ||
| logger.info("Original flag summary:") | ||
| echo_flag_table(df) | ||
|
|
||
| df_expunged = expunge_dataframe(df) | ||
|
|
||
| if show_summary: | ||
| logger.info("New flag summary:") | ||
| echo_flag_table(df_expunged) | ||
|
|
||
| if not dry_run: | ||
| save_file(df, output) | ||
|
|
||
|
|
||
| @click.command("clean") | ||
| @click.argument("file", nargs=1, type=click.Path()) | ||
| @click.option("-o", "--output", default="output.csv", help="The filepath where you would like to save the file", type=str) | ||
| @click.option("--log-level", default="INFO", | ||
| type=click.Choice(LOG_LEVELS, case_sensitive=False), | ||
| help="loguru log level (default: INFO)") | ||
| def clean_command(file, output, log_level): | ||
| """Clean FILE and save to OUTPUT.""" | ||
| configure_logging(log_level) | ||
|
|
||
| # make sure the extension is either a csv or parquet format | ||
| check_extension(output) | ||
|
|
||
| logger.info("Cleaning data for {}", file) | ||
| df = safe_load(file) | ||
| df = clean_dataframe(df) | ||
| # save the file | ||
| save_file(df, output) | ||
|
|
||
| # add the commands one-by-one | ||
| main.add_command(concat_command) | ||
| main.add_command(merge_command) | ||
| main.add_command(resample_command) | ||
| main.add_command(expunge_command) | ||
| main.add_command(flag_command) | ||
| main.add_command(clean_command) | ||
Oops, something went wrong.
Oops, something went wrong.
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.