cognite-neat 0.78.5__py3-none-any.whl → 0.79.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of cognite-neat might be problematic. Click here for more details.

cognite/neat/_version.py CHANGED
@@ -1 +1 @@
1
- __version__ = "0.78.5"
1
+ __version__ = "0.79.0"
@@ -3,10 +3,12 @@ import sys
3
3
  import time
4
4
  from abc import ABC, abstractmethod
5
5
  from collections.abc import Iterable, Iterator
6
+ from datetime import datetime
6
7
  from pathlib import Path
7
8
  from typing import Literal, TypeAlias, cast
8
9
 
9
10
  import pandas as pd
11
+ import pytz
10
12
  from prometheus_client import Gauge, Summary
11
13
  from rdflib import RDF, Graph, Namespace, URIRef
12
14
  from rdflib.query import Result, ResultRow
@@ -16,6 +18,8 @@ from cognite.neat.graph.models import Triple
16
18
  from cognite.neat.graph.stores._rdf_to_graph import rdf_file_to_graph
17
19
  from cognite.neat.utils import remove_namespace
18
20
 
21
+ from ._provenance import Change, Provenance
22
+
19
23
  if sys.version_info >= (3, 11):
20
24
  pass
21
25
  else:
@@ -49,6 +53,7 @@ class NeatGraphStoreBase(ABC):
49
53
  namespace: Namespace = DEFAULT_NAMESPACE,
50
54
  prefixes: dict = PREFIXES,
51
55
  ):
56
+ _start = datetime.now(pytz.utc)
52
57
  self.graph = graph or Graph()
53
58
  self.base_prefix: str = base_prefix
54
59
  self.namespace: Namespace = namespace
@@ -63,6 +68,16 @@ class NeatGraphStoreBase(ABC):
63
68
  self.internal_storage_dir_orig: Path | None = None
64
69
  self.storage_dirs_to_delete: list[Path] = []
65
70
  self.queries = _Queries(self)
71
+ self.provenance = Provenance(
72
+ [
73
+ Change.record(
74
+ activity=f"{type(self).__name__}.__init__",
75
+ start=_start,
76
+ end=datetime.now(pytz.utc),
77
+ description="Initialize graph store",
78
+ )
79
+ ]
80
+ )
66
81
 
67
82
  @abstractmethod
68
83
  def _set_graph(self) -> None:
@@ -0,0 +1,99 @@
1
+ # we will use prov-o to represent the provenance of the neat graph store
2
+ # basically tracking changes that occur in the graph store
3
+ # prov-o use concepts of Agent, Activity and Entity to represent provenance
4
+ # where in case of neat we have:
5
+ # Agent: triples extractors, graph enhancers, contextualizers, etc.
6
+ # Activity: write triple, add connection, etc.
7
+ # Entity: neat graph store
8
+
9
+
10
+ import uuid
11
+ from collections import UserList
12
+ from collections.abc import Sequence
13
+ from dataclasses import dataclass
14
+ from datetime import datetime
15
+ from typing import TypeVar
16
+
17
+ from rdflib import PROV, RDF, Literal, URIRef
18
+
19
+ from cognite.neat.constants import DEFAULT_NAMESPACE
20
+
21
+
22
+ @dataclass(frozen=True)
23
+ class Agent:
24
+ id_: URIRef = DEFAULT_NAMESPACE.agent
25
+ acted_on_behalf_of: str = "NEAT"
26
+
27
+ def as_triples(self):
28
+ return [
29
+ (self.id_, RDF.type, PROV[type(self).__name__]),
30
+ (self.id_, PROV.actedOnBehalfOf, self.acted_on_behalf_of),
31
+ ]
32
+
33
+
34
+ @dataclass(frozen=True)
35
+ class Activity:
36
+ was_associated_with: Agent
37
+ ended_at_time: datetime
38
+ started_at_time: datetime
39
+ used: str # this would be set to for example Extractor, Enhancer, Contextualizer, etc.
40
+ id_: URIRef = DEFAULT_NAMESPACE[f"activity-{uuid.uuid4()}"]
41
+
42
+ def as_triples(self):
43
+ return [
44
+ (self.id_, RDF.type, PROV[type(self).__name__]),
45
+ (self.id_, PROV.wasAssociatedWith, self.was_associated_with.id_),
46
+ (self.id_, PROV.startedAtTime, Literal(self.started_at_time)),
47
+ (self.id_, PROV.endedAtTime, Literal(self.ended_at_time)),
48
+ (self.id_, PROV.used, self.used),
49
+ ]
50
+
51
+
52
+ @dataclass(frozen=True)
53
+ class Entity:
54
+ was_generated_by: Activity
55
+ was_attributed_to: Agent
56
+ id_: URIRef = DEFAULT_NAMESPACE["graph-store"]
57
+
58
+ def as_triples(self):
59
+ return [
60
+ (self.id_, RDF.type, PROV[type(self).__name__]),
61
+ (self.id_, PROV.wasGeneratedBy, self.was_generated_by.id_),
62
+ (self.id_, PROV.wasAttributedTo, self.was_attributed_to.id_),
63
+ ]
64
+
65
+
66
+ @dataclass(frozen=True)
67
+ class Change:
68
+ agent: Agent
69
+ activity: Activity
70
+ entity: Entity
71
+ description: str
72
+
73
+ def as_triples(self):
74
+ return self.agent.as_triples() + self.activity.as_triples() + self.entity.as_triples()
75
+
76
+ @classmethod
77
+ def record(cls, activity: str, start: datetime, end: datetime, description: str):
78
+ """User friendly method to record a change that occurred in the graph store."""
79
+ agent = Agent()
80
+ activity = Activity(used=activity, was_associated_with=agent, started_at_time=start, ended_at_time=end)
81
+ entity = Entity(was_generated_by=activity, was_attributed_to=agent)
82
+ return cls(agent, activity, entity, description)
83
+
84
+
85
+ T_Change = TypeVar("T_Change", bound=Change)
86
+
87
+
88
+ class Provenance(UserList[T_Change]):
89
+ def __init__(self, changes: Sequence[T_Change] | None = None):
90
+ super().__init__(changes or [])
91
+
92
+ def did_this_happen(self, this: str) -> bool:
93
+ return any(change.description == this for change in self)
94
+
95
+ def __delitem__(self, *args, **kwargs):
96
+ raise TypeError("Cannot delete change from provenance")
97
+
98
+ def __setitem__(self, *args, **kwargs):
99
+ raise TypeError("Cannot modify change from provenance")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: cognite-neat
3
- Version: 0.78.5
3
+ Version: 0.79.0
4
4
  Summary: Knowledge graph transformation
5
5
  Home-page: https://cognite-neat.readthedocs-hosted.com/
6
6
  License: Apache-2.0
@@ -1,5 +1,5 @@
1
1
  cognite/neat/__init__.py,sha256=v-rRiDOgZ3sQSMQKq0vgUQZvpeOkoHFXissAx6Ktg84,61
2
- cognite/neat/_version.py,sha256=2uubg0ZmASu9ykJZHeQzJ7RUMWo2b-kKPcvmMJ5XS4A,23
2
+ cognite/neat/_version.py,sha256=1nnRuZbUvt4h80F41M2EZqt3TsZYyhJ2C2rRdk9uzhA,23
3
3
  cognite/neat/app/api/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  cognite/neat/app/api/asgi/metrics.py,sha256=nxFy7L5cChTI0a-zkCiJ59Aq8yLuIJp5c9Dg0wRXtV0,152
5
5
  cognite/neat/app/api/configuration.py,sha256=2U5M6M252swvQPQyooA1EBzFUZNtcTmuSaywfJDgckM,4232
@@ -70,11 +70,12 @@ cognite/neat/graph/loaders/_base.py,sha256=VOCRIee9ms6FuBlT3mwBV_mQnI6bO53mrardq
70
70
  cognite/neat/graph/loaders/_rdf2dms.py,sha256=bVFLjukCwEUGVoyQ6YnmdRXV945fhX3SiHR6yHLXO2k,12873
71
71
  cognite/neat/graph/models.py,sha256=AtLgZh2qyRP6NRetjQCy9qLMuTQB0CH52Zsev-qa2sk,149
72
72
  cognite/neat/graph/stores/__init__.py,sha256=ivvk7STSo-4wuP_CpizKUCPKmt_ufpNWRJUN9Bv5gdY,543
73
- cognite/neat/graph/stores/_base.py,sha256=ZrPDfWjmO3nJwpLS6r0ru7LZAhMtOcA76-v5akHq-kQ,14226
73
+ cognite/neat/graph/stores/_base.py,sha256=ZeeHxPHV0TU5B3Ep2db6FOkB1VP1gbJzKRjcY46zqGg,14682
74
74
  cognite/neat/graph/stores/_graphdb_store.py,sha256=8QM8I4srDKNsR0PddN6htCYUhfkoqlyy-c232Os7C0A,1776
75
75
  cognite/neat/graph/stores/_memory_store.py,sha256=GQq19xiyAWU0WQU5txmWnLXBuyP6ywd8plR21UtD3Uw,1420
76
76
  cognite/neat/graph/stores/_oxigraph_store.py,sha256=Xj69oE4M-9aqd8bq5CpLCMAhwNjJQAP1AC7lxzDsCn0,5448
77
77
  cognite/neat/graph/stores/_oxrdflib.py,sha256=A5zeRm5_e8ui_ihGpgstRDg_N7qcLZ3QZBRGrOXSGI0,9569
78
+ cognite/neat/graph/stores/_provenance.py,sha256=Y20-I8dP3DwTQ1sdI_eC4va2Az2FpK0oZwdfJ5T-2wc,3279
78
79
  cognite/neat/graph/stores/_rdf_to_graph.py,sha256=1ezWHTPn9UkIsAlxZcYRlqWvj3ixlmB5GGG9NN0ls2Q,1244
79
80
  cognite/neat/issues.py,sha256=pxQfqfBseMDE8JM0iqZnkLXngeyeFfT0TFtu1UuAd4c,4629
80
81
  cognite/neat/legacy/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -293,8 +294,8 @@ cognite/neat/workflows/steps_registry.py,sha256=fkTX14ZA7_gkUYfWIlx7A1XbCidvqR23
293
294
  cognite/neat/workflows/tasks.py,sha256=dqlJwKAb0jlkl7abbY8RRz3m7MT4SK8-7cntMWkOYjw,788
294
295
  cognite/neat/workflows/triggers.py,sha256=_BLNplzoz0iic367u1mhHMHiUrCwP-SLK6_CZzfODX0,7071
295
296
  cognite/neat/workflows/utils.py,sha256=gKdy3RLG7ctRhbCRwaDIWpL9Mi98zm56-d4jfHDqP1E,453
296
- cognite_neat-0.78.5.dist-info/LICENSE,sha256=W8VmvFia4WHa3Gqxq1Ygrq85McUNqIGDVgtdvzT-XqA,11351
297
- cognite_neat-0.78.5.dist-info/METADATA,sha256=cndbsOaHg6ndd8FtHUJVa5ojU6kQvr_ikXsByBRloW8,9298
298
- cognite_neat-0.78.5.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
299
- cognite_neat-0.78.5.dist-info/entry_points.txt,sha256=61FPqiWb25vbqB0KI7znG8nsg_ibLHBvTjYnkPvNFso,50
300
- cognite_neat-0.78.5.dist-info/RECORD,,
297
+ cognite_neat-0.79.0.dist-info/LICENSE,sha256=W8VmvFia4WHa3Gqxq1Ygrq85McUNqIGDVgtdvzT-XqA,11351
298
+ cognite_neat-0.79.0.dist-info/METADATA,sha256=Ww02h9AuPpAD5i76P15Q0T3gYFtxd2crDdFGX0yxcBg,9298
299
+ cognite_neat-0.79.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
300
+ cognite_neat-0.79.0.dist-info/entry_points.txt,sha256=61FPqiWb25vbqB0KI7znG8nsg_ibLHBvTjYnkPvNFso,50
301
+ cognite_neat-0.79.0.dist-info/RECORD,,