-
-
Notifications
You must be signed in to change notification settings - Fork 71
Add Smithsonian process and report script #278
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
Open
oree-xx
wants to merge
8
commits into
main
Choose a base branch
from
process_report
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+684
−3
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5fc6503
smithsonian and stacked bar plot
oree-xx 0e6f842
Made script executable
oree-xx 5776eef
Made review changes
oree-xx 4153081
Added unit full name and updates
oree-xx 646cb24
Made changes to the variable names
oree-xx 7ceb648
Made changes to the graphs
oree-xx 9e73061
Merge remote-tracking branch 'origin/main' into process_report
oree-xx 15720d1
Apply Black formatting to smithsonian scripts
oree-xx 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| #!/usr/bin/env python | ||
| """ | ||
| This file is dedicated to processing Smithsonian data | ||
| for analysis and comparison between quarters. | ||
| """ | ||
|
|
||
| # Standard library | ||
| import argparse | ||
| import os | ||
| import sys | ||
| import traceback | ||
|
|
||
| # Third-party | ||
| import pandas as pd | ||
|
|
||
| # Add parent directory so shared can be imported | ||
| sys.path.append(os.path.join(os.path.dirname(__file__), "..")) | ||
|
|
||
| # First-party/Local | ||
| import shared # noqa: E402 | ||
|
|
||
| # Setup | ||
| LOGGER, PATHS = shared.setup(__file__) | ||
|
|
||
| # Constants | ||
| QUARTER = os.path.basename(PATHS["data_quarter"]) | ||
| FILE_PATHS = [ | ||
| shared.path_join(PATHS["data_phase"], "smithsonian_totals_by_units.csv"), | ||
| shared.path_join(PATHS["data_phase"], "smithsonian_totals_by_records.csv"), | ||
| ] | ||
|
|
||
|
|
||
| def parse_arguments(): | ||
| """ | ||
| Parse command-line options, returns parsed argument namespace. | ||
| """ | ||
| global QUARTER | ||
| LOGGER.info("Parsing command-line options") | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument( | ||
| "--quarter", | ||
| default=QUARTER, | ||
| help=f"Data quarter in format YYYYQx (default: {QUARTER})", | ||
| ) | ||
| parser.add_argument( | ||
| "--enable-save", | ||
| action="store_true", | ||
| help="Enable saving results (default: False)", | ||
| ) | ||
| parser.add_argument( | ||
| "--enable-git", | ||
| action="store_true", | ||
| help="Enable git actions such as fetch, merge, add, commit, and push" | ||
| " (default: False)", | ||
| ) | ||
| parser.add_argument( | ||
| "--force", | ||
| action="store_true", | ||
| help="Regenerate data even if processed files already exist", | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
| if not args.enable_save and args.enable_git: | ||
| parser.error("--enable-git requires --enable-save") | ||
| if args.quarter != QUARTER: | ||
| global FILE_PATHS, PATHS | ||
| FILE_PATHS = shared.paths_list_update( | ||
| LOGGER, FILE_PATHS, QUARTER, args.quarter | ||
| ) | ||
| PATHS = shared.paths_update(LOGGER, PATHS, QUARTER, args.quarter) | ||
| QUARTER = args.quarter | ||
| args.logger = LOGGER | ||
| args.paths = PATHS | ||
| return args | ||
|
|
||
|
|
||
| def process_totals_by_units(args, count_data): | ||
| """ | ||
| Processing count data: totals by units | ||
| """ | ||
| LOGGER.info(process_totals_by_units.__doc__.strip()) | ||
| data = {} | ||
|
|
||
| for row in count_data.itertuples(index=False): | ||
| unit = str(row.UNIT) | ||
| total_objects = int(row.TOTAL_OBJECTS) | ||
|
|
||
| data[unit] = total_objects | ||
|
|
||
| data = pd.DataFrame(data.items(), columns=["Unit", "Total_objects"]) | ||
| data.sort_values("Unit", ascending=True, inplace=True) | ||
| data.reset_index(drop=True, inplace=True) | ||
| file_path = shared.path_join( | ||
| PATHS["data_phase"], "smithsonian_totals_by_units.csv" | ||
| ) | ||
| shared.data_to_csv(args, data, file_path) | ||
|
|
||
|
|
||
| def process_totals_by_records(args, count_data): | ||
| """ | ||
| Processing count data: totals by records | ||
| """ | ||
| LOGGER.info(process_totals_by_records.__doc__.strip()) | ||
| data = {} | ||
|
|
||
| for row in count_data.itertuples(index=False): | ||
| unit = str(row.UNIT) | ||
| CC0_records = int(row.CC0_RECORDS) | ||
| CC0_records_with_CC0_media = int(row.CC0_RECORDS_WITH_CC0_MEDIA) | ||
| total_objects = int(row.TOTAL_OBJECTS) | ||
|
|
||
| if CC0_records == 0 and CC0_records_with_CC0_media == 0: | ||
| continue | ||
|
|
||
| if unit not in data: | ||
| data[unit] = { | ||
| "CC0_records": 0, | ||
| "CC0_records_with_CC0_media": 0, | ||
| "Total_objects": 0, | ||
| } | ||
|
|
||
| data[unit]["CC0_records"] += CC0_records | ||
| data[unit]["CC0_records_with_CC0_media"] += CC0_records_with_CC0_media | ||
| data[unit]["Total_objects"] += total_objects | ||
|
|
||
| data = ( | ||
| pd.DataFrame.from_dict(data, orient="index") | ||
| .reset_index() | ||
| .rename(columns={"index": "Unit"}) | ||
| ) | ||
| data["CC0_without_media_percentage"] = ( | ||
| ( | ||
| (data["CC0_records"] - data["CC0_records_with_CC0_media"]) | ||
| / data["Total_objects"] | ||
| ) | ||
| * 100 | ||
| ).round(2) | ||
|
|
||
| data["CC0_with_media_percentage"] = ( | ||
| (data["CC0_records_with_CC0_media"] / data["Total_objects"]) * 100 | ||
| ).round(2) | ||
|
|
||
| data["Others_percentage"] = ( | ||
| ((data["Total_objects"] - data["CC0_records"]) / data["Total_objects"]) | ||
| * 100 | ||
| ).round(2) | ||
|
|
||
| data.sort_values("Unit", ascending=True, inplace=True) | ||
| data.reset_index(drop=True, inplace=True) | ||
|
|
||
| file_path = shared.path_join( | ||
| PATHS["data_phase"], "smithsonian_totals_by_records.csv" | ||
| ) | ||
| shared.data_to_csv(args, data, file_path) | ||
|
|
||
|
|
||
| def main(): | ||
| args = parse_arguments() | ||
| shared.paths_log(LOGGER, PATHS) | ||
| shared.git_fetch_and_merge(args, PATHS["repo"]) | ||
| shared.check_completion_file_exists(args, FILE_PATHS) | ||
| file_count = shared.path_join( | ||
| PATHS["data_1-fetch"], "smithsonian_2_units.csv" | ||
| ) | ||
| count_data = shared.open_data_file( | ||
| LOGGER, | ||
| file_count, | ||
| usecols=[ | ||
| "UNIT", | ||
| "CC0_RECORDS", | ||
| "CC0_RECORDS_WITH_CC0_MEDIA", | ||
| "TOTAL_OBJECTS", | ||
| ], | ||
| ) | ||
| process_totals_by_units(args, count_data) | ||
| process_totals_by_records(args, count_data) | ||
|
|
||
| # Push changes | ||
| args = shared.git_add_and_commit( | ||
| args, | ||
| PATHS["repo"], | ||
| PATHS["data_quarter"], | ||
| f"Add and commit new GitHub data for {QUARTER}", | ||
| ) | ||
| shared.git_push_changes(args, PATHS["repo"]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| try: | ||
| main() | ||
| except shared.QuantifyingException as e: | ||
| if e.exit_code == 0: | ||
| LOGGER.info(e.message) | ||
| else: | ||
| LOGGER.error(e.message) | ||
| sys.exit(e.exit_code) | ||
| except SystemExit as e: | ||
| LOGGER.error(f"System exit with code: {e.code}") | ||
| sys.exit(e.code) | ||
| except KeyboardInterrupt: | ||
| LOGGER.info("(130) Halted via KeyboardInterrupt.") | ||
| sys.exit(130) | ||
| except Exception: | ||
| LOGGER.exception(f"(1) Unhandled exception: {traceback.format_exc()}") | ||
| sys.exit(1) |
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.
Please add a comment indicating how this data was compiled. Something like "Manually compiled from information on URL".
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.
Also, does the source(s) have information on: