Skip to content

Commit

Permalink
Merge pull request #252 from EMMC-ASBL/optional-parse-label
Browse files Browse the repository at this point in the history
Made `label` optional in the parse strategy
  • Loading branch information
jesper-friis authored Jun 25, 2024
2 parents 2d1330d + d92949c commit 9ce67ba
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 6 deletions.
26 changes: 20 additions & 6 deletions oteapi_dlite/strategies/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions tests/entities/Molecule.yaml
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 16 additions & 0 deletions tests/static/molecule.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
1 change: 1 addition & 0 deletions tests/strategies/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
entitydir = testdir / "entities"
inputdir = testdir / "input"
outputdir = testdir / "output"
staticdir = testdir / "static"


# Add utils to sys path
Expand Down
99 changes: 99 additions & 0 deletions tests/strategies/test_parse.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 9ce67ba

Please sign in to comment.