Skip to content
Closed
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
14 changes: 10 additions & 4 deletions pipeline/src/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ def to_jsonld(self, include_empty_properties=True, embed_linked_nodes=True, with
"""

data = {"@type": self.type_}
if isinstance(self, LinkedMetadata):
data["s:schemaVersion"] = self.schema_version
if with_context:
if self.type_.startswith("https://openminds.ebrains.eu/"):
data["@context"] = {"@vocab": "https://openminds.ebrains.eu/vocab/"}
else:
data["@context"] = {"@vocab": "https://openminds.om-i.org/props/"}
data["@context"]["s"] = "https://schema.org/"
Copy link
Member

Choose a reason for hiding this comment

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

Minor point, but the prefix 'schema' is the most commonly used for schema.org.

Copy link
Member Author

Choose a reason for hiding this comment

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

ok, I'll make that change

if hasattr(self, "id") and self.id:
data["@id"] = self.id
for property in self.__class__.properties:
Expand Down Expand Up @@ -122,6 +125,10 @@ def from_jsonld(cls, data, ignore_unexpected_keys=False):
"""
data_copy = data.copy()
context = data_copy.pop("@context", None)
schema_version = data_copy.pop("s:schemaVersion", None)
# todo: also handle an expanded key, i.e., "https://schema.org/schemaVersion"
# todo: check major part of schema_version against self.schema_version
# i.e. v4.1 and v4.0 would be ok, v5.0 and v4.0 would not
type_ = data_copy.pop("@type")
if isinstance(type_, list) and len(type_) == 1:
type_ = type_[0]
Expand All @@ -138,7 +145,7 @@ def from_jsonld(cls, data, ignore_unexpected_keys=False):
else:
# todo: implement or import a function that does a full JSON-LD expansion
# not just this special case
expanded_path = f"{cls.context['@vocab']}{property.path}"
expanded_path = f"{cls.context['@vocab']}{property.path}"
if expanded_path in data_copy:
value = data_copy.pop(expanded_path)
found = True
Expand Down Expand Up @@ -268,9 +275,8 @@ def __init__(self, identifier, allowed_types=None):
self.allowed_types = allowed_types

def to_jsonld(self):
return {
"@id": self.identifier
}
return {"@id": self.identifier}


class IRI:
"""
Expand Down
14 changes: 10 additions & 4 deletions pipeline/src/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ def save(self, path, individual_files=False, include_empty_properties=False):
if node.type_.startswith("https://openminds.ebrains.eu/"):
data_context = {"@vocab": "https://openminds.ebrains.eu/vocab/"}
else:
data_context = {"@vocab": "https://openminds.om-i.org/props/"}
data_context = {
"@vocab": "https://openminds.om-i.org/props/"
}
data_context["s"] = "https://schema.org/"

for linked_node in node.links:
self._add_node(linked_node)
Expand Down Expand Up @@ -169,12 +172,15 @@ def load(self, *paths):
data = json.load(fp)
if "@graph" in data:
if data["@context"]["@vocab"].startswith("https://openminds.ebrains.eu/"):
version = "v3"
default_version = "v3"
else:
version = "latest"
default_version = "v4"
for item in data["@graph"]:
if "@type" in item:
cls = lookup_type(item["@type"], version=version)
version = default_version
if "s:schemaVersion" in item: # todo: expand this using the context
version = item["s:schemaVersion"]
cls = lookup_type(item["@type"], version=version.split(".")[0])
node = cls.from_jsonld(item)
else:
# allow links to metadata instances outside this collection
Expand Down
8 changes: 7 additions & 1 deletion pipeline/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,22 @@ def test_collection_sort_by_id():
os.remove("test_collection_sort_by_id.jsonld")

expected_saved_data = {
"@context": {"@vocab": "https://openminds.om-i.org/props/"},
"@context": {
"@vocab": "https://openminds.om-i.org/props/",
"s": "https://schema.org/",
},
"@graph": [
{
"@id": "_:001",
"@type": "https://openminds.om-i.org/types/Organization",
"fullName": "University of That Place",
"s:schemaVersion": "latest"
},
{
"@id": "_:002",
"@type": "https://openminds.om-i.org/types/Organization",
"fullName": "University of This Place",
"s:schemaVersion": "latest"
},
{
"@id": "_:004",
Expand All @@ -117,6 +122,7 @@ def test_collection_sort_by_id():
],
"familyName": "Professor",
"givenName": "A",
"s:schemaVersion": "latest"
},
],
}
Expand Down
2 changes: 2 additions & 0 deletions pipeline/tests/test_instantiation.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ def test_link():
expected = {
"@context": {
"@vocab": "https://openminds.om-i.org/props/",
"s": "https://schema.org/"
},
"@type": "https://openminds.om-i.org/types/DatasetVersion",
"s:schemaVersion": "v4.0",
"studyTarget": [
{
"@id": "https://openminds.om-i.org/instances/species/musMusculus",
Expand Down
31 changes: 26 additions & 5 deletions pipeline/tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import os
from openminds import Collection, IRI
from openminds.latest import core as omcore
import openminds.v4.core as omcore
from utils import build_fake_node


Expand Down Expand Up @@ -38,18 +38,24 @@ def test_issue_0003():
)
# on export, a single item should be wrapped in a list, where the property expects an array
expected = {
"@context": {"@vocab": "https://openminds.om-i.org/props/"},
"@context": {
"@vocab": "https://openminds.om-i.org/props/",
"s": "https://schema.org/"
},
"@type": "https://openminds.om-i.org/types/FileArchive",
"IRI": "http://example.com/archive.zip",
"format": {
"@type": "https://openminds.om-i.org/types/ContentType",
"name": "application/zip",
"s:schemaVersion": "v4.0",
},
"s:schemaVersion": "v4.0",
"sourceData": [
{
"@type": "https://openminds.om-i.org/types/File",
"IRI": "http://example.com/some_file.txt",
"name": "some_file.txt",
"s:schemaVersion": "v4.0",
}
],
}
Expand Down Expand Up @@ -90,11 +96,15 @@ def test_issue0007():

actual = person.to_jsonld(include_empty_properties=False, embed_linked_nodes=False, with_context=True)
expected = {
"@context": {"@vocab": "https://openminds.om-i.org/props/"},
"@context": {
"@vocab": "https://openminds.om-i.org/props/",
"s": "https://schema.org/"
},
"@id": "_:001",
"@type": "https://openminds.om-i.org/types/Person",
"familyName": "Professor",
"givenName": "A",
"s:schemaVersion": "v4.0",
"affiliation": [
{
"@type": "https://openminds.om-i.org/types/Affiliation",
Expand All @@ -105,6 +115,7 @@ def test_issue0007():
"memberOf": {"@id": "_:003"},
},
],

}
assert actual == expected

Expand All @@ -116,7 +127,10 @@ def test_issue0007():
saved_data = json.load(fp)
os.remove("issue0007.jsonld")
expected_saved_data = {
"@context": {"@vocab": "https://openminds.om-i.org/props/"},
"@context": {
"@vocab": "https://openminds.om-i.org/props/",
"s": "https://schema.org/"
},
"@graph": [
{
"@id": "_:001",
Expand All @@ -133,16 +147,19 @@ def test_issue0007():
],
"familyName": "Professor",
"givenName": "A",
"s:schemaVersion": "v4.0",
},
{
"@id": "_:002",
"@type": "https://openminds.om-i.org/types/Organization",
"fullName": "University of This Place",
"s:schemaVersion": "v4.0",
},
{
"@id": "_:003",
"@type": "https://openminds.om-i.org/types/Organization",
"fullName": "University of That Place",
"s:schemaVersion": "v4.0",
},
],
}
Expand All @@ -163,7 +180,10 @@ def test_issue0008():
)
actual = person.to_jsonld(include_empty_properties=False, embed_linked_nodes=False, with_context=True)
expected = {
"@context": {"@vocab": "https://openminds.om-i.org/props/"},
"@context": {
"@vocab": "https://openminds.om-i.org/props/",
"s": "https://schema.org/"
},
"@id": "_:002",
"@type": "https://openminds.om-i.org/types/Person",
"affiliation": [
Expand All @@ -175,6 +195,7 @@ def test_issue0008():
],
"familyName": "Professor",
"givenName": "A",
"s:schemaVersion": "v4.0",
}
assert actual == expected

Expand Down