-
Notifications
You must be signed in to change notification settings - Fork 4
New function to correct subject ids #27
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
tmorrell
merged 18 commits into
caltechlibrary:main
from
AbakahAlexander:subject_correction
Jun 30, 2025
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a003507
added subject correction
AbakahAlexander 86f391c
added subject correction
AbakahAlexander 47d5a02
added name to codemeta
AbakahAlexander c7d575d
added unit tests to test for the behavour of numerous records and the…
AbakahAlexander 5d06aa5
Added regression tests for subjects correction
AbakahAlexander 18a3323
addded test cases in edit_subjects
AbakahAlexander ce4ff9e
added more tests
AbakahAlexander f7cf5a2
refractoring tests
AbakahAlexander 4ca8e24
refractoring tests
AbakahAlexander 1daa180
refractoring tests
AbakahAlexander db9103b
complete
AbakahAlexander e6c0321
added
AbakahAlexander 6a6d9b3
new changes
AbakahAlexander 7f9654d
fixed issues upper and lower case
AbakahAlexander 06653aa
fixed issues upper and lower case
AbakahAlexander f421597
refactored code
AbakahAlexander 1480112
fixed tokens
AbakahAlexander 151c223
fixed header issues
AbakahAlexander 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
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,21 @@ | ||
| from caltechdata_api import get_metadata | ||
| from ames.matchers import edit_subject | ||
| import os | ||
| import pandas as pd | ||
|
|
||
| df = pd.read_csv("subjects_to_correct.csv") | ||
|
|
||
| subjects_to_correct = dict(zip(df['subject'], df['subject url'])) | ||
|
|
||
| def all_corrected(record, subjects_to_correct = subjects_to_correct): | ||
|
|
||
| metadata = edit_subject( | ||
| record, os.environ.get("CALTECH_DATA_API"), subjects_to_correct | ||
| ) | ||
|
|
||
| if metadata: | ||
| return True | ||
| else: | ||
| return False | ||
|
|
||
|
|
||
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,4 @@ | ||
| subject,subject url | ||
| Biological sciences,http://www.oecd.org/science/inno/38235147.pdf?1.6 | ||
| Chemical sciences,http://www.oecd.org/science/inno/38235147.pdf?1.4 | ||
| Computer and information sciences,http://www.oecd.org/science/inno/38235147.pdf?1.2 |
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,163 @@ | ||
| import unittest | ||
|
Member
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. Move to the tests directory |
||
| import os, copy, time, requests | ||
| import pandas as pd | ||
| from run_subject_id_correction import all_corrected | ||
tmorrell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from caltechdata_api import caltechdata_write, get_metadata | ||
|
|
||
|
|
||
| original_metadata = { | ||
| "titles": [{"title": "enter title"}], | ||
| "creators": [ | ||
| { | ||
| "familyName": "Abakah", | ||
| "givenName": "Alexander", | ||
| "nameType": "Personal", | ||
| "nameIdentifiers": [ | ||
| {"nameIdentifier": "0009-0003-5640-6691", "nameIdentifierScheme": "ORCID"} | ||
| ], | ||
| "affiliations": [{"affiliation": "Caltech"}] | ||
| } | ||
| ], | ||
| "types": {"resourceType": "Dataset", "resourceTypeGeneral": "Dataset"}, | ||
| "descriptions": [{"description": "A data set of forest fires", "descriptionType": "Summary"}], | ||
| "dates": [{"date": "2023-11-30", "dateType": "Created"}], | ||
| "publisher": "Caltech Library", | ||
| "subjects": [{"subject": "Enter Subject"}], | ||
| } | ||
|
|
||
| # A version of the metadata that is deliberately malformed | ||
| malformed_metadata = copy.deepcopy(original_metadata) | ||
| malformed_metadata["subjects"] = [ | ||
| {"subject": " Biological sciences "}, # Extra spaces | ||
| {"subject": "CHEMICAL SCIENCES"}, # All caps | ||
| {"subject": "computer and information sciences"}, # Incorrect capitalization | ||
| ] | ||
|
|
||
| df = pd.read_csv("subjects_to_correct.csv") | ||
|
|
||
| subjects_to_correct = dict(zip(df['subject'], df['subject url'])) | ||
|
|
||
| def test_change(record_id): | ||
| metadata = get_metadata(record_id, production = False) | ||
| for i in metadata["subjects"]: | ||
| for each_correct_subject in subjects_to_correct.keys(): | ||
| if "id" in i.keys(): | ||
| if ( | ||
| i["subject"] == each_correct_subject | ||
| and i["id"] != subjects_to_correct[each_correct_subject] | ||
| ): | ||
| print(i["subject"], "'s id wasn't added.") | ||
| return False | ||
| print("Changes made!") | ||
| return True | ||
|
|
||
|
|
||
| class TestSubjects(unittest.TestCase): | ||
|
|
||
| def test_subject_changes(self): | ||
| # Creates a test record with malformed subjects | ||
| test_data = copy.deepcopy(malformed_metadata) | ||
| record_id = caltechdata_write( | ||
| metadata=test_data, | ||
| production=False, | ||
| publish=True | ||
| ) | ||
| # Verify correction | ||
| self.assertEqual(all_corrected(record_id), True, f"Subjects in record {record_id} were not corrected properly") | ||
| self.assertEqual(test_change(record_id), True) | ||
| print("Passed test_subject_changes") | ||
|
|
||
| #Verify no change was made to original metadata | ||
| record_id = caltechdata_write( | ||
| metadata=copy.deepcopy(original_metadata), | ||
| production=False, | ||
| publish=True | ||
| ) | ||
| self.assertEqual(all_corrected(record_id), True, f"Subjects in original record {record_id} were not edited properly") | ||
| self.assertEqual(test_change(record_id), True) | ||
| print("Passed test_subject_changes") | ||
|
|
||
| def test_subject_id_present(self): | ||
| # Creates a record with known subjects that should map to IDs | ||
| test_data = copy.deepcopy(malformed_metadata) | ||
| test_data["subjects"] = [ | ||
| {"subject": "Biological sciences"}, | ||
| {"subject": "Chemical sciences"}, | ||
| {"subject": "Computer and information sciences"}, | ||
| ] | ||
| record_id = caltechdata_write( | ||
| metadata=test_data, | ||
| production=False, | ||
| publish=True | ||
| ) | ||
|
|
||
| all_corrected(record_id) | ||
|
|
||
| rurl = "https://data.caltechlibrary.dev/api/records/" + record_id | ||
| data = requests.get(rurl, headers=headers).json() | ||
| record_metadata = data["metadata"] | ||
| for subject_obj in record_metadata.get("subjects", []): | ||
| if subject_obj["subject"] in ["Biological sciences", "Chemical sciences", "Computer and information sciences"]: | ||
| self.assertIn("id", subject_obj, f"Subject '{subject_obj['subject']}' in record {record_id} should have an ID") | ||
| print("Passed test_subject_id_present") | ||
|
|
||
| def test_subject_scheme_consistent(self): | ||
| # Creates a record with IDs that should link to scheme FOS | ||
| test_data = copy.deepcopy(original_metadata) | ||
| test_data["subjects"] = [ | ||
| { | ||
| "id": "http://www.oecd.org/science/inno/38235147.pdf?1.2", | ||
| "subject": "Computer and information sciences", | ||
| "scheme": "fos" | ||
| } | ||
| ] | ||
| record_id = caltechdata_write( | ||
| metadata=test_data, | ||
| production=False, | ||
| publish=True | ||
| ) | ||
|
|
||
| all_corrected(record_id) | ||
|
|
||
| record_metadata = get_metadata( | ||
| record_id, production=False | ||
| ) | ||
| for subject_obj in record_metadata.get("subjects", []): | ||
| if "id" in subject_obj: | ||
| self.assertEqual( | ||
| subject_obj["scheme"], "FOS", | ||
| f"Subject scheme for '{subject_obj['subject']}' in record {record_id} should be 'FOS'" | ||
| ) | ||
| print("Passed test_subject_scheme_consistent") | ||
|
|
||
| def test_subject_has_scheme(self): | ||
| # Creates a record with IDs doesn't have a scheme | ||
| test_data = copy.deepcopy(original_metadata) | ||
| test_data["subjects"] = [ | ||
| { | ||
| "id": "http://www.oecd.org/science/inno/38235147.pdf?1.2", | ||
| "subject": "Computer and information sciences", | ||
| } | ||
| ] | ||
| record_id = caltechdata_write( | ||
| metadata=test_data, | ||
| production=False, | ||
| publish=True | ||
| ) | ||
|
|
||
| all_corrected(record_id) | ||
|
|
||
| record_metadata = get_metadata( | ||
| record_id, production=False | ||
| ) | ||
| for subject_obj in record_metadata.get("subjects", []): | ||
| if "id" in subject_obj: | ||
| self.assertIn( | ||
| "scheme", subject_obj, | ||
| f"Subject with ID '{subject_obj['id']}' should have a scheme" | ||
| ) | ||
| print("Passed test_subject_has_scheme") | ||
|
|
||
|
Member
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. Do a test with metadata to verify that "Enter Subject" isn't deleted |
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
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.