Skip to content

Commit

Permalink
Merge branch 'sorting_collection_by_id' into pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
Peyman-N authored Feb 6, 2025
2 parents 88853ec + 08ca7ca commit d25af55
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pipeline/src/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ def _get_blank_node_identifier(self):
identifier = len(self.nodes)
return fmt.format(identifier=identifier)

def _sort_nodes_by_id(self):
sorted_nodes=dict(sorted(self.nodes.items()))
self.nodes=sorted_nodes


def save(self, path, individual_files=False, include_empty_properties=False):
"""
Save the node collection to disk in JSON-LD format.
Expand Down Expand Up @@ -102,6 +107,7 @@ def save(self, path, individual_files=False, include_empty_properties=False):
parent_dir = os.path.dirname(path)
if parent_dir:
os.makedirs(parent_dir, exist_ok=True)
self._sort_nodes_by_id()
data = {
"@context": data_context,
"@graph": [
Expand All @@ -121,6 +127,7 @@ def save(self, path, individual_files=False, include_empty_properties=False):
raise OSError(
f"If saving to multiple files, `path` must be a directory. path={path}, pwd={os.getcwd()}"
)
self._sort_nodes_by_id()
output_paths = []
for node in self:
if node.id.startswith("http"):
Expand Down
57 changes: 57 additions & 0 deletions pipeline/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,60 @@ def test_round_trip_multi_file():
p = person.to_jsonld(include_empty_properties=False, embed_linked_nodes=True)
np = new_person.to_jsonld(include_empty_properties=False, embed_linked_nodes=True)
assert p == np


def test_collection_sort_by_id():
person = omcore.Person(given_name="A", family_name="Professor", id="_:004")
uni1 = omcore.Organization(full_name="University of This Place", id="_:002")
uni2 = omcore.Organization(full_name="University of That Place", id="_:001")
person.affiliations = [
omcore.Affiliation(member_of = uni1),
omcore.Affiliation(member_of = uni2),
]

c = Collection(person,uni1,uni2)
output_paths = c.save("test_collection_sort_by_id.jsonld", individual_files=False, include_empty_properties=False)

assert output_paths == ["test_collection_sort_by_id.jsonld"]

with open(output_paths[0]) as fp:
saved_data = json.load(fp)
os.remove("test_collection_sort_by_id.jsonld")

expected_saved_data={
"@context": {"@vocab": "https://openminds.ebrains.eu/vocab/"},
"@graph": [
{
"@id": "_:001",
"@type": "https://openminds.ebrains.eu/core/Organization",
"fullName": "University of That Place"
},
{
"@id": "_:002",
"@type": "https://openminds.ebrains.eu/core/Organization",
"fullName": "University of This Place"
},
{
"@id": "_:004",
"@type": "https://openminds.ebrains.eu/core/Person",
"affiliation": [
{
"@type": "https://openminds.ebrains.eu/core/Affiliation",
"memberOf": {
"@id": "_:002"
}
},
{
"@type": "https://openminds.ebrains.eu/core/Affiliation",
"memberOf": {
"@id": "_:001"
}
}
],
"familyName": "Professor",
"givenName": "A"
}
]
}

assert saved_data == expected_saved_data

0 comments on commit d25af55

Please sign in to comment.