-
Notifications
You must be signed in to change notification settings - Fork 0
Add re-flagging based on multiple filters #30
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
23 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
|---|---|---|
| @@ -1,30 +1,56 @@ | ||
| from pathlib import Path | ||
| import pandas as pd | ||
| import numpy as np | ||
|
|
||
| import click | ||
| from terminaltables import SingleTable | ||
| import numpy as np | ||
| import pandas as pd | ||
|
|
||
| from quantaq_cli.exceptions import InvalidFileExtension, InvalidDeviceModel | ||
| from quantaq_cli.utilities import safe_load, determine_timestamp_column | ||
| from quantaq_cli.variables import FLAG_DEFINITIONS, SUPPORTED_MODELS | ||
| from quantaq_cli.console.commands.flag import ( | ||
| echo_flag_table | ||
| ) | ||
|
|
||
| from ...exceptions import InvalidFileExtension, InvalidDeviceModel | ||
| from ...utilities import safe_load | ||
| from ...variables import FLAGS, SUPPORTED_MODELS | ||
|
|
||
| def expunge_dataframe(df): | ||
|
|
||
| # get the flags (in the future, this will come from the file itself) | ||
| list_of_flags = FLAG_DEFINITIONS | ||
|
|
||
| # force the flag column to be an int | ||
| df["flag"] = df["flag"].astype(int, errors='ignore') | ||
|
|
||
| # Drop NaNs | ||
| df = df.dropna(how='any', subset=["flag"]) | ||
|
|
||
| for label, value, cols in list_of_flags: | ||
| mask = df["flag"] & value == value | ||
| if not mask.any(): | ||
| continue | ||
|
|
||
| # NaN the necessary columns | ||
| if cols == "all_columns": | ||
| tscol = determine_timestamp_column(df) | ||
| cols_to_keep = {tscol, "sn", "flag"} | ||
| cols = [c for c in df.columns if c not in cols_to_keep] | ||
| elif len(cols) > 0: | ||
| cols = [c for c in cols if c in df.columns] | ||
|
|
||
| # set the mask | ||
| df.loc[mask, cols] = np.nan | ||
|
|
||
| return df | ||
|
|
||
| def expunge_command(file, output, **kwargs): | ||
| verbose = kwargs.pop("verbose", False) | ||
| dry_run = kwargs.pop("dry_run", False) | ||
| flagcol = kwargs.pop("flagcol", "flag") | ||
| model = kwargs.pop("model", "modulair_pm") | ||
| table = kwargs.pop("table", False) | ||
|
|
||
| # make sure the extension is either a csv or feather format | ||
| output = Path(output) | ||
| if output.suffix not in (".csv", ".feather"): | ||
| raise InvalidFileExtension("Invalid file extension") | ||
|
|
||
| # ensure the model is valid | ||
| if model not in SUPPORTED_MODELS: | ||
| raise InvalidDeviceModel("Invalid device model. Must be one of {}".format(SUPPORTED_MODELS)) | ||
|
|
||
| save_as_csv = True if output.suffix == ".csv" else False | ||
|
|
||
| # concat everything in filepath | ||
|
|
@@ -34,49 +60,15 @@ def expunge_command(file, output, **kwargs): | |
| # load the file | ||
| df = safe_load(file) | ||
|
|
||
| # expunge | ||
| if verbose: | ||
| click.echo("Expunging data for {}".format(model)) | ||
|
|
||
| # init an array to hold the table data | ||
| data = [] | ||
| data.append(["FLAG", "FLAG VALUE", "# OCCURENCES", "% DATA"]) | ||
| click.echo("Expunging data for {}".format(file)) | ||
|
|
||
| # get the flags (in the future, this will come from the file itself) | ||
| list_of_flags = FLAGS.get(model) | ||
|
|
||
| # force the flag column to be an int | ||
| df[flagcol] = df[flagcol].astype(int, errors='ignore') | ||
|
|
||
| # Drop NaNs | ||
| df = df.dropna(how='any', subset=[flagcol]) | ||
|
|
||
| for label, value, cols in list_of_flags: | ||
| mask = df[flagcol] & value == value | ||
| n_affected = mask.sum() | ||
| pct_affected = round((n_affected / df.shape[0]) * 100.0, 2) | ||
|
|
||
| # NaN the necessary columns | ||
| if cols is None: | ||
| cols = df.columns | ||
| elif len(cols) > 0: | ||
| cols = [c for c in cols if c in df.columns] | ||
|
|
||
| # set the mask | ||
| df.loc[mask, cols] = np.nan | ||
|
|
||
| # add a row to the output table | ||
| data.append([label, value, n_affected, pct_affected]) | ||
| df = expunge_dataframe(df) | ||
|
|
||
| if dry_run or verbose: | ||
| tbl = SingleTable(data) | ||
| tbl.title = "Flag Breakdown".upper() | ||
|
|
||
| if table: | ||
| click.echo(tbl.table) | ||
| else: | ||
| for item in data: | ||
| click.echo(item) | ||
|
|
||
| echo_flag_table(df) | ||
|
Contributor
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. This will be removed once we have logging implemented |
||
|
|
||
| # save the file (if not a dry run) | ||
| if not dry_run: | ||
| if verbose: | ||
|
|
||
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.
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.
Definitely want to log this, as it should never occur (for future PR)
Uh oh!
There was an error while loading. Please reload this page.
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.
Got it! Added a logger.warning in 349b388 (in PR #32)