Skip to content
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

Fix/OpenAlex "abstract_inverted_index" field is sometimes a string, not a dictionary #173

Merged
merged 2 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Git LFS file not shown
26 changes: 22 additions & 4 deletions academic_observatory_workflows/workflows/openalex_telescope.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,12 +1016,30 @@ def transform_object(obj: dict):

field = "abstract_inverted_index"
if field in obj:
if not isinstance(obj.get(field), dict):
if not isinstance(obj.get(field), (dict, str)):
return
keys = list(obj[field].keys())
values = [str(value)[1:-1] for value in obj[field].values()]
else:
# If data is held in a string dump, load json string again.
if isinstance(obj.get(field), str):
obj_part = json.loads(obj[field])
field2 = "InvertedIndex"
if isinstance(obj_part.get(field2), dict):
keys = list(obj_part[field2].keys())
values = [str(value)[1:-1] for value in obj_part[field2].values()]

index_sum = sum(len(value.split(", ")) for value in values)
assert (
index_sum == obj_part["IndexLength"]
), f"Calculated IndexLength {index_sum} does not match value from file {obj_part['IndexLength']}."

obj[field] = {"keys": keys, "values": values}
else:
raise TypeError(f"obj_part['InvertedIndex'] is not a dictionary: {obj_part}")
else:
keys = list(obj[field].keys())
values = [str(value)[1:-1] for value in obj[field].values()]

obj[field] = {"keys": keys, "values": values}
obj[field] = {"keys": keys, "values": values}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to check if the index length matches, as it is a problem for OpenAlex to fix if that is the case. It could be a data quality check that we do in the future (if we re-ran the workflow and updated the field).

You could simplify the code a bit by putting the parsing into a function, like this:

    field = "abstract_inverted_index"
    if field in obj:
        def parse_abstract(dict_: dict):
            keys_ = list(dict_.keys())
            values_ = [str(value_)[1:-1] for value_ in dict_.values()]
            return {"keys": keys_, "values": values_}
        if isinstance(obj.get(field), str):
            data = json.loads(obj[field])
            obj[field] = parse_abstract(data["InvertedIndex"])
        elif isinstance(obj.get(field), dict):
            obj[field] = parse_abstract(obj[field])
        else:
            return


field = "international"
if field in obj:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,25 @@ def test_transform_object(self):
obj3,
)

# Test object with nested "abstract_inverted_index" none
obj4 = {"abstract_inverted_index": None}
# Test object when "abstract_inverted_index" is a json string dump
obj4 = {
"abstract_inverted_index": '{"IndexLength": 7, "InvertedIndex": { "Malignant": [0], "hyperthermia": [1], "susceptibility": [2],"(MHS)": [3], "is": [4, 6], "primarily": [5]}}'
}
transform_object(obj4)
self.assertDictEqual({"abstract_inverted_index": None}, obj4)
self.assertDictEqual(
{
"abstract_inverted_index": {
"keys": ["Malignant", "hyperthermia", "susceptibility", "(MHS)", "is", "primarily"],
"values": ["0", "1", "2", "3", "4, 6", "5"],
}
},
obj4,
)

# Test object with nested "abstract_inverted_index" none
obj5 = {"abstract_inverted_index": None}
transform_object(obj5)
self.assertDictEqual({"abstract_inverted_index": None}, obj5)


def upload_folder_to_s3(bucket_name: str, folder_path: str, s3_prefix=None):
Expand Down