Skip to content

Commit

Permalink
Change of attributes for added individuals (#698)
Browse files Browse the repository at this point in the history
Authored-by: kysrpex <kysrpex@users.noreply.github.com>
Authored-by: José Manuel Domínguez <jose.manuel.dominguez@iwm.fraunhofer.de>
(merged from 2d71d21)
  • Loading branch information
kysrpex committed Sep 1, 2021
1 parent 4367fda commit 398428e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
45 changes: 42 additions & 3 deletions osp/core/cuds.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
"""

import logging
from copy import deepcopy
from uuid import uuid4, UUID
from typing import Union, List, Iterator, Dict, Any, Optional, Tuple, Iterable
from typing import Any, Dict, Hashable, Iterable, Iterator, List, Optional, \
Tuple, Union
import rdflib
from rdflib import URIRef, BNode, RDF, Graph, Literal
import osp.core.warnings as warning_settings
from osp.core.namespaces import cuba, from_iri
from osp.core.ontology.relationship import OntologyRelationship
from osp.core.ontology.attribute import OntologyAttribute
Expand Down Expand Up @@ -179,7 +183,9 @@ def get_attributes(self):
for s, p, o in self._graph.triples((self.iri, None, None)):
obj = from_iri(p, raise_error=False)
if isinstance(obj, OntologyAttribute):
result[obj] = o.toPython()
value = self._rdflib_5_inplace_modification_prevention_filter(
o.toPython(), obj)
result[obj] = value
return result

def is_a(self, oclass):
Expand Down Expand Up @@ -870,7 +876,9 @@ def __getattr__(self, name):
attr = self._get_attribute_by_argname(name)
if self.session:
self.session._notify_read(self)
return self._graph.value(self.iri, attr.iri).toPython()
value = self._rdflib_5_inplace_modification_prevention_filter(
self._graph.value(self.iri, attr.iri).toPython(), attr)
return value
except AttributeError as e:
if ( # check if user calls session's methods on wrapper
self.is_a(cuba.Wrapper)
Expand All @@ -893,6 +901,37 @@ def _get_attribute_by_argname(self, name):
return attr
raise AttributeError(name)

@staticmethod
def _rdflib_5_inplace_modification_prevention_filter(
value: Any, attribute: OntologyAttribute) -> Any:
if rdflib.__version__ < "6.0.0" and not isinstance(value,
Hashable):
value = deepcopy(value)
if warning_settings.attributes_cannot_modify_in_place:
warning_settings.attributes_cannot_modify_in_place = False
logger.warning(f"Attribute {attribute} references the mutable "
f"object {value} of type {type(value)}. Please "
f"note that if you modify this object "
f"in-place, the changes will not be reflected "
f"on the cuds object's attribute. \n"
f"For example, executing "
f"`fr = city.City(name='Freiburg', "
f"coordinates=[1, 2]); fr.coordinates[0]=98; "
f"fr.coordinates` would yield `array([1, 2])` "
f"instead of `array([98, 2])`, as you could "
f"expect. Use `fr.coordinates = [98, 2]` "
f"instead, or save the attribute to a "
f"different variable, i.e. `value = "
f"fr.coordinates; value[0] = 98, "
f"fr.coordinates = value`."
f"\n"
f"You will not see this kind of warning again "
f"during this session. You can turn off the "
f"warning by running `import osp.core.warnings "
f"as warning_settings; warning_settings."
f"attributes_cannot_modify_in_place = False`.")
return value

def __setattr__(self, name, new_value):
"""Set an attribute.
Expand Down
7 changes: 4 additions & 3 deletions osp/core/ontology/oclass.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""A class defined in the ontology."""

import logging
import uuid

import rdflib
from rdflib import OWL, RDF, RDFS, BNode

from osp.core.ontology.entity import OntologyEntity
from osp.core.ontology.cuba import rdflib_cuba
import logging
from rdflib import OWL, RDFS, RDF, BNode
from osp.core.ontology.entity import OntologyEntity

logger = logging.getLogger(__name__)

Expand Down
8 changes: 8 additions & 0 deletions osp/core/warnings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Configuration of OSP-core warnings."""

attributes_cannot_modify_in_place = True
"""Warns when a user fetches a mutable attribute of a CUDS object.
For example `fr = city.City(name='Freiburg', coordinates=[1, 2]);
fr.coordinates`.
"""

2 comments on commit 398428e

@kysrpex
Copy link
Contributor Author

@kysrpex kysrpex commented on 398428e Sep 1, 2021

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 398428e Previous: 3fe52c3 Ratio
benchmark_cuds_api.py::benchmark_cuds_create 136.64682699948648 iter/sec (stddev: 0.007783676826874716) 287.4609791124471 iter/sec (stddev: 0.005569435857023758) 2.10
benchmark_cuds_api.py::benchmark_add_default 96.90321145314729 iter/sec (stddev: 0.006182959898938153) 165.22841418035875 iter/sec (stddev: 0.0027972483864435976) 1.71
benchmark_cuds_api.py::benchmark_cuds_add_rel 101.47061051406197 iter/sec (stddev: 0.005467040351700866) 155.25828744634427 iter/sec (stddev: 0.0033941906301663714) 1.53
benchmark_cuds_api.py::benchmark_get_byrel 346.12300741928277 iter/sec (stddev: 0.000771145247620635) 577.787539973339 iter/sec (stddev: 0.00022141764611459234) 1.67
benchmark_cuds_api.py::benchmark_get_byoclass 10.461025333696977 iter/sec (stddev: 0.01674042734820773) 24.069474711786057 iter/sec (stddev: 0.0036280097481363564) 2.30
benchmark_cuds_api.py::benchmark_cuds_iter_byrel 390.93045320409135 iter/sec (stddev: 0.00036936553636259266) 613.1694030002608 iter/sec (stddev: 0.00008818101190929666) 1.57
benchmark_cuds_api.py::benchmark_iter_byoclass 10.516310268350493 iter/sec (stddev: 0.012071637063644301) 24.526870337132355 iter/sec (stddev: 0.0033582347184151345) 2.33
benchmark_cuds_api.py::benchmark_cuds_is_a 4411.646616482133 iter/sec (stddev: 0.00013633353149428195) 7165.053865556911 iter/sec (stddev: 0.00007299664480780023) 1.62
benchmark_cuds_api.py::benchmark_cuds_attributes 264.74736223770515 iter/sec (stddev: 0.0007789531613296508) 597.1914323424747 iter/sec (stddev: 0.00013294263673717234) 2.26

This comment was automatically generated by workflow using github-action-benchmark.

CC: @yoavnash @pablo-de-andres @kysrpex

@kysrpex
Copy link
Contributor Author

@kysrpex kysrpex commented on 398428e Sep 6, 2021

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 398428e Previous: 9ef9cf2 Ratio
benchmark_cuds_api.py::benchmark_cuds_create 141.45969030274154 iter/sec (stddev: 0.0054382482461264895) 280.0907132033582 iter/sec (stddev: 0.005826507104895408) 1.98
benchmark_cuds_api.py::benchmark_get_byuiduriref 256.96169621031726 iter/sec (stddev: 0.0013355205493754269) 389.4433188312303 iter/sec (stddev: 0.0006538066212404773) 1.52
benchmark_cuds_api.py::benchmark_get_byrel 344.7819962779913 iter/sec (stddev: 0.0007825979345396903) 548.9185327829107 iter/sec (stddev: 0.00029547835197549986) 1.59
benchmark_cuds_api.py::benchmark_get_byoclass 10.452764519786463 iter/sec (stddev: 0.01463386318027106) 23.176188967677486 iter/sec (stddev: 0.006304180921543873) 2.22
benchmark_cuds_api.py::benchmark_cuds_iter_byrel 355.0939415119642 iter/sec (stddev: 0.0007938264423998444) 590.6787039836076 iter/sec (stddev: 0.00017057798098012075) 1.66
benchmark_cuds_api.py::benchmark_iter_byoclass 9.68762193250267 iter/sec (stddev: 0.020749984112774966) 22.28441069426951 iter/sec (stddev: 0.009978926394715208) 2.30
benchmark_cuds_api.py::benchmark_cuds_is_a 2890.8770298215427 iter/sec (stddev: 0.00011866707475782272) 6520.748997239624 iter/sec (stddev: 0.00007786717701194264) 2.26
benchmark_cuds_api.py::benchmark_cuds_iri 4879.875234464278 iter/sec (stddev: 0.00003700524745998585) 14017.412598168989 iter/sec (stddev: 0.000021059110199333104) 2.87
benchmark_cuds_api.py::benchmark_cuds_attributes 240.49830517722722 iter/sec (stddev: 0.0010180440367446655) 535.474038593375 iter/sec (stddev: 0.0003410400583564741) 2.23

This comment was automatically generated by workflow using github-action-benchmark.

CC: @yoavnash @pablo-de-andres @kysrpex

Please sign in to comment.