Skip to content

Commit

Permalink
Revert "v2.3.0 (#72)"
Browse files Browse the repository at this point in the history
This reverts commit 4d4079a.
  • Loading branch information
MBueschelberger authored Dec 1, 2024
1 parent 4d4079a commit 013795c
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 522 deletions.
61 changes: 8 additions & 53 deletions data2rdf/models/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@
import warnings
from typing import Any, Dict, List, Optional, Union

from data2rdf.models.utils import apply_datatype, detect_datatype
from data2rdf.qudt.utils import _get_query_match
from data2rdf.utils import make_prefix

from data2rdf.models.utils import ( # isort:skip
apply_datatype,
detect_datatype,
is_float,
is_integer,
)

from data2rdf.models.base import ( # isort:skip
BasicGraphModel,
BasicSuffixModel,
Expand All @@ -21,7 +15,6 @@

from pydantic import ( # isort:skip
AnyUrl,
AliasChoices,
BaseModel,
Field,
ValidationInfo,
Expand Down Expand Up @@ -114,9 +107,9 @@ class QuantityGraph(BasicGraphModel, BasicSuffixModel):
a quantity describing a column of a time series or table with a unit."""

unit: Optional[Union[str, AnyUrl]] = Field(
None, description="QUDT Symbol or any other IRI for the unit mapping"
..., description="QUDT Symbol or any other IRI for the unit mapping"
)
value: Optional[Union[int, float, str]] = Field(
value: Optional[Union[int, float]] = Field(
None, description="Value of the quantity"
)

Expand All @@ -132,17 +125,6 @@ class QuantityGraph(BasicGraphModel, BasicSuffixModel):
for mapping the data value to the individual.""",
)

@field_validator("value", mode="after")
@classmethod
def validate_value(
cls, value: Union[int, float, str]
) -> Union[int, float]:
if isinstance(value, str) and is_integer(value):
value = int(value)
elif isinstance(value, str) and is_float(value):
value = float(value)
return value

@field_validator("unit", mode="after")
@classmethod
def validate_unit(
Expand Down Expand Up @@ -218,34 +200,23 @@ class PropertyGraph(BasicGraphModel, BasicSuffixModel):
discrete value but can also be a reference to a column in a table or
time series."""

value: Optional[
Union[str, int, float, bool, AnyUrl, "PropertyGraph", "QuantityGraph"]
] = Field(None, description="Value of the property")
value: Optional[Union[str, int, float, bool, AnyUrl]] = Field(
None, description="Value of the property"
)
annotation: Optional[Union[str, AnyUrl]] = Field(
None, description="Base IRI with which the value shall be concatenated"
)
value_relation: Optional[Union[str, AnyUrl]] = Field(
"rdfs:label",
description="""Data or annotation property
for mapping the data value to the individual.""",
alias=AliasChoices("relation", "value_relation", "valuerelation"),
)
value_relation_type: Optional[RelationType] = Field(
None,
description="Type of the semantic relation used in the mappings",
alias=AliasChoices(
"value_relation_type",
"value_relationtype",
"relation_type",
"relationtype",
),
None, description="Type of the semantic relation used in the mappings"
)
value_datatype: Optional[str] = Field(
None,
description="In case of an annotation or data property, this field indicates the XSD Datatype of the value",
alias=AliasChoices(
"value_datatype", "value_data_type", "datatype", "data_type"
),
)

@field_validator("annotation")
Expand All @@ -256,19 +227,6 @@ def validate_annotation(cls, value: AnyUrl) -> AnyUrl:
value = AnyUrl(str(value).strip())
return value

@model_validator(mode="after")
@classmethod
def validate_value(cls, self: "PropertyGraph") -> "PropertyGraph":
"""
Validate value of a property graph.
In case the value is a property graph or a quantity graph, make sure that
the config is set correctly.
"""
if isinstance(self.value, (PropertyGraph, QuantityGraph)):
self.value.config = self.config
return self

@model_validator(mode="after")
@classmethod
def validate_property_graph(cls, self: "PropertyGraph") -> "PropertyGraph":
Expand Down Expand Up @@ -306,10 +264,7 @@ def value_json(self) -> "Optional[Dict[str, str]]":
spec = apply_datatype(self.value, self.value_datatype)
response = {self.value_relation: spec}
else:
if isinstance(self.value, (PropertyGraph, QuantityGraph)):
response = {self.value_relation: self.value.json_ld}
else:
response = {self.value_relation: {"@id": str(self.value)}}
response = {self.value_relation: {"@id": str(self.value)}}
else:
response = {}
return response
Expand Down
49 changes: 4 additions & 45 deletions data2rdf/models/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@

from typing import List, Optional, Union

from pydantic import (
AliasChoices,
AnyUrl,
BaseModel,
Field,
field_validator,
model_validator,
)
from pydantic import AnyUrl, BaseModel, Field, field_validator, model_validator

from .base import BasicConceptMapping, BasicSuffixModel, RelationType

Expand All @@ -35,33 +28,7 @@ class TBoxBaseMapping(BasicConceptMapping):
)

datatype: Optional[str] = Field(
None,
description="XSD Datatype of the targed value",
alias=AliasChoices("datatype", "data_type"),
)


class CustomRelationPropertySubgraph(BasicSuffixModel):
value_relation: Optional[str] = Field(
"rdfs:label",
description="""Object/Data/Annotation property for the value
resolving from `key` of this model""",
)


class CustomRelationQuantitySubgraph(BasicSuffixModel):
unit_relation: Optional[Union[str, AnyUrl]] = Field(
"qudt:hasUnit",
description="""Object property for mapping the IRI
of the unit to the individual.""",
)
value_relation: Optional[Union[str, AnyUrl]] = Field(
"qudt:value",
description="""Data property
for mapping the data value to the individual.""",
)
unit: Optional[Union[str, AnyUrl]] = Field(
None, description="Symbol or QUDT IRI for the mapping"
None, description="XSD Datatype of the targed value"
)


Expand All @@ -77,16 +44,8 @@ class CustomRelation(BaseModel):
...,
description="Cell number or Jsonpath to the value of the quantity or property",
)
object_data_type: Optional[
Union[
str, CustomRelationPropertySubgraph, CustomRelationQuantitySubgraph
]
] = Field(
None,
description="XSD Data type of the object or PropertyGraph-mapping or QuantityGraph-mapping",
alias=AliasChoices(
"object_datatype", "object_data_type", "object_type"
),
object_data_type: Optional[str] = Field(
None, description="XSD Data type of the object"
)
relation_type: Optional[RelationType] = Field(
None, description="Type of the semantic relation used in the mappings"
Expand Down
66 changes: 13 additions & 53 deletions data2rdf/parsers/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,11 @@
from data2rdf.warnings import MappingMissmatchWarning

from .base import ABoxBaseParser, BaseFileParser, TBoxBaseParser

from .utils import ( # isort:skip
_make_tbox_classes,
_make_tbox_json_ld,
_strip_unit,
_value_exists,
)
from .utils import _make_tbox_classes, _make_tbox_json_ld, _strip_unit

from data2rdf.models.mapping import ( # isort:skip
ABoxExcelMapping,
TBoxBaseMapping,
CustomRelationPropertySubgraph,
CustomRelationQuantitySubgraph,
)


Expand Down Expand Up @@ -393,23 +385,18 @@ def _run_parser(

if datum.value_location and not datum.time_series_start:
value = worksheet[datum.value_location].value

if model_data.get("unit") and _value_exists(value):
if model_data.get("unit") and value:
model_data["value"] = value
elif not model_data.get("unit") and _value_exists(value):
elif not model_data.get("unit") and value:
model_data["value"] = str(value)
else:
message = f"""Concept with key `{datum.key}`
does not have a value at location `{datum.value_location}`.
Concept will be omitted in graph.
"""
warnings.warn(message, MappingMissmatchWarning)
else:
value = None

value_exists = _value_exists(value)

if value_exists or suffix in self.time_series:
if model_data.get("value") or suffix in self.time_series:
if datum.value_relation:
model_data["value_relation"] = datum.value_relation
if model_data.get("unit"):
Expand All @@ -423,7 +410,7 @@ def _run_parser(
value_relation_type=datum.value_relation_type,
)

if value_exists:
if model_data.get("value"):
self._general_metadata.append(model)
else:
self._time_series_metadata.append(model)
Expand All @@ -432,35 +419,14 @@ def _run_parser(
for relation in datum.custom_relations:
value = worksheet[relation.object_location].value

if isinstance(
relation.object_data_type,
(
CustomRelationPropertySubgraph,
CustomRelationQuantitySubgraph,
),
):
if isinstance(
relation.object_data_type,
CustomRelationPropertySubgraph,
):
Model = PropertyGraph
else:
Model = QuantityGraph
model = Model(
value=value,
**relation.object_data_type.model_dump(),
)
model.suffix += "_" + suffix
model = PropertyGraph(
value_relation=relation.relation,
value_relation_type="object_property",
value=model,
iri=datum.iri,
suffix=suffix,
config=self.config,
)
self._general_metadata.append(model)
elif _value_exists(value):
if not value:
message = f"""Concept with for iri `{datum.iri}`
does not have a value at location `{relation.object_location}`.
Concept will be omitted in graph.
"""
warnings.warn(message, MappingMissmatchWarning)

if value:
model = PropertyGraph(
value_relation=relation.relation,
value_relation_type=relation.relation_type,
Expand All @@ -471,12 +437,6 @@ def _run_parser(
config=self.config,
)
self._general_metadata.append(model)
else:
message = f"""Concept with for iri `{datum.iri}`
does not have a value at location `{relation.object_location}`.
Concept will be omitted in graph.
"""
warnings.warn(message, MappingMissmatchWarning)

# set time series as pd dataframe
self._time_series = pd.DataFrame.from_dict(
Expand Down
Loading

0 comments on commit 013795c

Please sign in to comment.