-
-
Notifications
You must be signed in to change notification settings - Fork 72
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
19 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 eee780d
Made review changes
oree-xx bf1ee8b
Made review changes
oree-xx abff7c7
Made review changes
oree-xx f26fdbb
Made review changes
oree-xx f6b06e1
Changed the variable name UNIT_NAME to DATA_SOURCE and added the ylabels
oree-xx 198a8f4
Merge branch 'main' into process_report
oree-xx b937dfb
Merge remote-tracking branch 'origin/main' into process_report
oree-xx 5868ca8
Merge branch 'process_report' of https://github.com/creativecommons/q…
oree-xx ffbf5a7
rewrap long strings
TimidRobot 5e7c8e6
renamed data_to_csv as dataframe_to_csv
oree-xx 071c3b8
Merge branch 'process_report' of https://github.com/creativecommons/q…
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
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,203 @@ | ||
| #!/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_NAME) | ||
| total_objects = int(row.TOTAL_OBJECTS) | ||
|
|
||
| data[unit] = total_objects | ||
|
|
||
| data = pd.DataFrame(data.items(), columns=["Unit_name", "Total_objects"]) | ||
| data.sort_values("Unit_name", 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_NAME) | ||
| 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 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_name"}) | ||
| ) | ||
| 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_name", 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) | ||
|
TimidRobot marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| 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_CODE", | ||
| "UNIT_NAME", | ||
| "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.
Uh oh!
There was an error while loading. Please reload this page.