diff --git a/oteapi_dlite/strategies/parse.py b/oteapi_dlite/strategies/parse.py index 96149cb8..01a65d77 100644 --- a/oteapi_dlite/strategies/parse.py +++ b/oteapi_dlite/strategies/parse.py @@ -21,11 +21,11 @@ class DLiteParseConfig(AttrDict): """Configuration for generic DLite parser.""" driver: Annotated[ - Optional[str], + str, Field( description='Name of DLite driver (ex: "json").', ), - ] = None + ] location: Annotated[ Optional[str], Field( @@ -52,11 +52,24 @@ class DLiteParseConfig(AttrDict): ), ] = None label: Annotated[ - str, + Optional[str], + Field( + description=( + "Optional label of the new DLite instance in the collection." + ), + ), + ] = None + datamodel: Annotated[ + Optional[str], Field( - description="Label of the new DLite instance in the collection.", + description=( + "DLite datamodel documenting the structure of the data set. " + "Often unused, since the datamodel is implicitly defined in " + "the DLite driver (DLite plugin), but for a documentation " + "point of view this is a very important field." + ), ), - ] + ] = None datacache_config: Annotated[ Optional[DataCacheConfig], Field( @@ -154,7 +167,8 @@ def get( # Insert inst into collection coll = get_collection(session) - coll.add(config.label, inst) + label = config.label if config.label else inst.uuid + coll.add(label, inst) # __TODO__ # See diff --git a/tests/entities/Molecule.yaml b/tests/entities/Molecule.yaml new file mode 100644 index 00000000..19477675 --- /dev/null +++ b/tests/entities/Molecule.yaml @@ -0,0 +1,34 @@ +da31ff66-98af-590c-855d-386c63617755: + uuid: da31ff66-98af-590c-855d-386c63617755 + uri: http://onto-ns.com/meta/0.1/Molecule + meta: http://onto-ns.com/meta/0.3/EntitySchema + description: A minimal description of a molecules + dimensions: + natoms: Number of atoms + ncoords: Number coordinates. Always 3 + properties: + name: + type: string + description: Name of the molecule. + positions: + type: float64 + shape: + - natoms + - ncoords + unit: "\xC5ngstr\xF6m" + description: Atomic positions in Cartesian coordinates. + symbols: + type: string + shape: + - natoms + description: Chemical symbols. + masses: + type: float64 + shape: + - natoms + unit: u + description: Atomic masses. + groundstate_energy: + type: float64 + unit: eV + description: Molecule ground state energy. diff --git a/tests/static/molecule.json b/tests/static/molecule.json new file mode 100644 index 00000000..a515e078 --- /dev/null +++ b/tests/static/molecule.json @@ -0,0 +1,16 @@ +{ + "b8906302-5cd9-5e34-9fec-9d6101e52554": { + "meta": "http://onto-ns.com/meta/0.1/Molecule", + "dimensions": { + "natoms": 2, + "ncoords": 3 + }, + "properties": { + "name": "H2", + "positions": [[-1.64097, 1.37077, 0], [-1.03814, 1.74207, 0]], + "symbols": ["H", "H"], + "masses": [1.008, 1.008], + "groundstate_energy": 1.34613 + } + } +} diff --git a/tests/strategies/paths.py b/tests/strategies/paths.py index 2934e341..3086048d 100644 --- a/tests/strategies/paths.py +++ b/tests/strategies/paths.py @@ -11,6 +11,7 @@ entitydir = testdir / "entities" inputdir = testdir / "input" outputdir = testdir / "output" +staticdir = testdir / "static" # Add utils to sys path diff --git a/tests/strategies/test_parse.py b/tests/strategies/test_parse.py new file mode 100644 index 00000000..42dbd4a8 --- /dev/null +++ b/tests/strategies/test_parse.py @@ -0,0 +1,99 @@ +"""Test the image formats in the image parse strategy.""" + +# pylint: disable=too-many-locals +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from pathlib import Path + from typing import Optional + + from oteapi.interfaces import IParseStrategy + + from oteapi_dlite.models import DLiteSessionUpdate + + +# if True: +def test_parse_no_options() -> None: + """Test the dlite-parse strategy.""" + + import dlite + from oteapi.datacache import DataCache + from paths import staticdir + + from oteapi_dlite.strategies.parse import DLiteParseStrategy + + sample_file = staticdir / "molecule.json" + + cache = DataCache() + + orig_key = cache.add(sample_file.read_bytes()) + config = { + "downloadUrl": sample_file.as_uri(), + "mediaType": "image/vnd.dlite-parse", + "configuration": { + "driver": "json", + }, + } + coll = dlite.Collection() + session = { + "collection_id": coll.uuid, + "key": orig_key, + } + cache.add(coll.asjson(), key=coll.uuid) + parser: "IParseStrategy" = DLiteParseStrategy(config) + output: "DLiteSessionUpdate" = parser.get(session) + assert "collection_id" in output + assert output.collection_id == coll.uuid + + # Get all instances of given metaid (there should only be one instance) + metaid = "http://onto-ns.com/meta/0.1/Molecule" + instances = list(coll.get_instances(metaid=metaid)) + assert len(instances) == 1 + (inst,) = instances + assert inst.meta.uri == metaid + + +# if True: +def test_parse_label() -> None: + """Test the dlite-parse strategy.""" + + import dlite + from oteapi.datacache import DataCache + from paths import staticdir + + from oteapi_dlite.strategies.parse import DLiteParseStrategy + + sample_file = staticdir / "molecule.json" + + cache = DataCache() + + orig_key = cache.add(sample_file.read_bytes()) + config = { + "downloadUrl": sample_file.as_uri(), + "mediaType": "image/vnd.dlite-parse", + "configuration": { + "driver": "json", + "label": "instance", + }, + } + coll = dlite.Collection() + session = { + "collection_id": coll.uuid, + "key": orig_key, + } + cache.add(coll.asjson(), key=coll.uuid) + parser: "IParseStrategy" = DLiteParseStrategy(config) + output: "DLiteSessionUpdate" = parser.get(session) + assert "collection_id" in output + assert output.collection_id == coll.uuid + + coll2: dlite.Collection = dlite.get_instance(session["collection_id"]) + inst: dlite.Instance = coll2.get("instance") + assert inst.properties.keys() == { + "name", + "positions", + "symbols", + "masses", + "groundstate_energy", + } + assert inst.properties["name"] == "H2"