fmu-sumo-explorer 3.0.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.
fmu/__init__.py ADDED
@@ -0,0 +1,2 @@
1
+ # This line ONLY! :
2
+ __path__ = __import__("pkgutil").extend_path(__path__, __name__)
@@ -0,0 +1,13 @@
1
+ """Top-level package for fmu.sumo.explorer"""
2
+
3
+ try:
4
+ from ._version import version
5
+
6
+ __version__ = version
7
+ except ImportError:
8
+ __version__ = "0.0.0"
9
+
10
+
11
+ from fmu.sumo.explorer.explorer import Explorer
12
+ from fmu.sumo.explorer.filters import Filters
13
+ from fmu.sumo.explorer.timefilter import TimeFilter, TimeType
@@ -0,0 +1,24 @@
1
+ # file generated by vcs-versioning
2
+ # don't change, don't track in version control
3
+ from __future__ import annotations
4
+
5
+ __all__ = [
6
+ "__version__",
7
+ "__version_tuple__",
8
+ "version",
9
+ "version_tuple",
10
+ "__commit_id__",
11
+ "commit_id",
12
+ ]
13
+
14
+ version: str
15
+ __version__: str
16
+ __version_tuple__: tuple[int | str, ...]
17
+ version_tuple: tuple[int | str, ...]
18
+ commit_id: str | None
19
+ __commit_id__: str | None
20
+
21
+ __version__ = version = '3.0.0'
22
+ __version_tuple__ = version_tuple = (3, 0, 0)
23
+
24
+ __commit_id__ = commit_id = None
@@ -0,0 +1,39 @@
1
+ from collections import deque
2
+ from threading import Lock
3
+
4
+ # Based on https://llego.dev/posts/implement-lru-cache-python/
5
+
6
+
7
+ class LRUCache:
8
+ def __init__(self, capacity):
9
+ self.cache = {}
10
+ self.capacity = capacity
11
+ self.access = deque()
12
+ self.lock = Lock()
13
+
14
+ def get(self, key):
15
+ with self.lock:
16
+ if key not in self.cache:
17
+ return None
18
+ else:
19
+ self.access.remove(key)
20
+ self.access.append(key)
21
+ return self.cache[key]
22
+
23
+ def put(self, key, value):
24
+ with self.lock:
25
+ if key in self.cache:
26
+ self.access.remove(key)
27
+ elif len(self.cache) == self.capacity:
28
+ oldest = self.access.popleft()
29
+ del self.cache[oldest]
30
+ self.cache[key] = value
31
+ self.access.append(key)
32
+
33
+ def has(self, key):
34
+ return key in self.cache
35
+
36
+ def clear(self):
37
+ with self.lock:
38
+ self.cache.clear()
39
+ self.access.clear()
@@ -0,0 +1,94 @@
1
+ """Module containing class for exploring results from sumo"""
2
+
3
+ import warnings
4
+
5
+ import httpx
6
+ from sumo.wrapper import SumoClient
7
+
8
+ from .objects._search_context import SearchContext
9
+ from .objects.cases import Cases
10
+
11
+
12
+ class Explorer(SearchContext):
13
+ """Class for consuming FMU results from Sumo.
14
+ The Sumo Explorer is a Python package for consuming FMU results stored
15
+ in Sumo. It is FMU aware, and creates an abstraction on top of the
16
+ Sumo API. The purpose of the package is to create an FMU-oriented
17
+ Python interface towards FMU data in Sumo, and make it easy for FMU
18
+ users in various contexts to use data stored in Sumo.
19
+
20
+ Examples of use cases:
21
+ - Applications (example: Webviz)
22
+ - Scripts (example: Local post-processing functions)
23
+ - Manual data browsing and visualization (example: A Jupyter Notebook)
24
+ """
25
+
26
+ def __init__(
27
+ self,
28
+ env: str = "prod",
29
+ token: str | None = None,
30
+ interactive: bool = True,
31
+ keep_alive: str | None = None,
32
+ http_client=None,
33
+ async_http_client=None,
34
+ ):
35
+ """Initialize the Explorer class
36
+
37
+ Args:
38
+ env (str): Sumo environment
39
+ token (str): authenticate with existing token
40
+ interactive (bool): authenticate using interactive flow (browser)
41
+ keep_alive (str): point in time lifespan (deprecated and ignored)
42
+ """
43
+ sumo = SumoClient(
44
+ env,
45
+ token=token,
46
+ interactive=interactive,
47
+ timeout=httpx.Timeout(180.0),
48
+ http_client=http_client,
49
+ async_http_client=async_http_client,
50
+ )
51
+ SearchContext.__init__(self, sumo)
52
+ if keep_alive:
53
+ warnings.warn(
54
+ "The constructor argument 'keep_alive' to class 'Explorer' has been deprecated.",
55
+ DeprecationWarning,
56
+ )
57
+
58
+ @property
59
+ def cases(self):
60
+ uuids = self._context_for_class("case").uuids
61
+ return Cases(self, uuids)
62
+
63
+ def get_permissions(self, asset: str | None = None):
64
+ """Get permissions
65
+
66
+ Args:
67
+ asset (str): asset in Sumo
68
+
69
+ Returns:
70
+ dict: Dictionary of user permissions
71
+ """
72
+ res = self._sumo.get("/userpermissions").json()
73
+
74
+ if asset is not None and asset not in res:
75
+ raise PermissionError(f"No permissions for asset: {asset}")
76
+
77
+ return res
78
+
79
+ async def get_permissions_async(self, asset: str | None = None):
80
+ """Get permissions
81
+
82
+ Args:
83
+ asset (str): asset in Sumo
84
+
85
+ Returns:
86
+ dict: Dictionary of user permissions
87
+ """
88
+ res = await self._sumo.get_async("/userpermissions")
89
+ res = res.json()
90
+
91
+ if asset is not None and asset not in res:
92
+ raise PermissionError(f"No permissions for asset: {asset}")
93
+
94
+ return res
@@ -0,0 +1,34 @@
1
+ """
2
+ Complex(ish) filters for use with fmu-sumo Explorer.
3
+ """
4
+
5
+ from typing import ClassVar
6
+
7
+
8
+ class Filters:
9
+ # Filter that matches 4d-seismic objects.
10
+ seismic4d: ClassVar = {
11
+ "bool": {
12
+ "must": [
13
+ {"term": {"data.content.keyword": "seismic"}},
14
+ {"exists": {"field": "data.time.t0.label"}},
15
+ {"exists": {"field": "data.time.t1.label"}},
16
+ ]
17
+ }
18
+ }
19
+
20
+ # Filter that matches aggregations
21
+ aggregations: ClassVar = {"exists": {"field": "fmu.aggregation.operation"}}
22
+
23
+ # Filter that matches observations
24
+ observations: ClassVar = {
25
+ "bool": {
26
+ "must_not": [
27
+ {"exists": {"field": "fmu.ensemble.name.keyword"}},
28
+ {"exists": {"field": "fmu.realization.id"}},
29
+ ]
30
+ }
31
+ }
32
+
33
+ # Filter that matches realizations
34
+ realizations: ClassVar = {"exists": {"field": "fmu.realization.id"}}
@@ -0,0 +1,19 @@
1
+ """Sumo cases and child objects"""
2
+
3
+ from ._child import Child
4
+ from ._document import Document
5
+ from ._metrics import Metrics
6
+ from ._search_context import SearchContext
7
+ from .case import Case
8
+ from .cases import Cases
9
+ from .cpgrid import CPGrid
10
+ from .cpgrid_property import CPGridProperty
11
+ from .cube import Cube
12
+ from .dictionary import Dictionary
13
+ from .ensemble import Ensemble
14
+ from .ensembles import Ensembles
15
+ from .polygons import Polygons
16
+ from .realization import Realization
17
+ from .realizations import Realizations
18
+ from .surface import Surface
19
+ from .table import Table
@@ -0,0 +1,212 @@
1
+ """module containing class for child object"""
2
+
3
+ from io import BytesIO
4
+
5
+ from sumo.wrapper import SumoClient
6
+
7
+ from ._document import Document
8
+
9
+
10
+ class Child(Document):
11
+ """Class representing a child object in Sumo"""
12
+
13
+ def __init__(self, sumo: SumoClient, metadata: dict, blob=None) -> None:
14
+ """
15
+ Args:
16
+ sumo (SumoClient): connection to Sumo
17
+ metadata: (dict): child object metadata
18
+ """
19
+ super().__init__(metadata)
20
+ self._sumo = sumo
21
+ self._blob = blob
22
+
23
+ def __str__(self):
24
+ if self.stage == "case" and self.__class__.__name__ != "Case":
25
+ return (
26
+ f"<{self.__class__.__name__}: {self.name} {self.uuid}(uuid) "
27
+ f"in case {self.casename} "
28
+ f"in asset {self.asset}>"
29
+ )
30
+ else:
31
+ if self.realization:
32
+ return (
33
+ f"<{self.__class__.__name__}: {self.name} {self.uuid}(uuid) "
34
+ f"in realization {self.realization} "
35
+ f"in ensemble {self.ensemble} "
36
+ f"in case {self.casename} "
37
+ f"in asset {self.asset}>"
38
+ )
39
+ if self.operationname:
40
+ return (
41
+ f"<{self.__class__.__name__}: {self.name} {self.uuid}(uuid) "
42
+ f"in operation {self.operationname} "
43
+ f"in ensemble {self.ensemble} "
44
+ f"in case {self.casename} "
45
+ f"in asset {self.asset}>"
46
+ )
47
+ else:
48
+ return super().__str__()
49
+
50
+ def __repr__(self):
51
+ return self.__str__()
52
+
53
+ @property
54
+ def blob(self) -> BytesIO:
55
+ """Object blob"""
56
+ if self._blob is None:
57
+ res = self._sumo.get(f"/objects('{self.uuid}')/blob")
58
+ self._blob = BytesIO(res.content)
59
+
60
+ return self._blob
61
+
62
+ @property
63
+ async def blob_async(self) -> BytesIO:
64
+ """Object blob"""
65
+ if self._blob is None:
66
+ res = await self._sumo.get_async(f"/objects('{self.uuid}')/blob")
67
+ self._blob = BytesIO(res.content)
68
+
69
+ return self._blob
70
+
71
+ @property
72
+ def timestamp(self) -> str | None:
73
+ """Object timestmap data"""
74
+ t0 = self._get_property(["data", "time", "t0", "value"])
75
+ t1 = self._get_property(["data", "time", "t1", "value"])
76
+
77
+ if t0 is not None and t1 is None:
78
+ return t0
79
+
80
+ return None
81
+
82
+ @property
83
+ def interval(self) -> str | tuple[str, str] | None:
84
+ """Object interval data"""
85
+ t0 = self._get_property(["data", "time", "t0", "value"])
86
+ t1 = self._get_property(["data", "time", "t1", "value"])
87
+
88
+ if t0 is not None and t1 is not None:
89
+ return (t0, t1)
90
+
91
+ return None
92
+
93
+ @property
94
+ def template_path(self) -> str:
95
+ return "/".join(
96
+ ["{realization}", "{ensemble}"] + self.relative_path.split("/")[2:]
97
+ )
98
+
99
+ @property
100
+ def spec(self) -> dict:
101
+ """Object spec data"""
102
+ return self.get_property("data.spec")
103
+
104
+ @property
105
+ def bbox(self) -> dict:
106
+ """Object boundary-box data"""
107
+ return self.get_property("data.bbox")
108
+
109
+ @property
110
+ def relative_path(self) -> str:
111
+ """Object relative file path"""
112
+ return self.get_property("file.relative_path")
113
+
114
+ @property
115
+ def dataformat(self) -> str:
116
+ """Object file format"""
117
+ return self.get_property("data.format")
118
+
119
+ @property
120
+ def format(self) -> str:
121
+ """Object file format"""
122
+ return self.get_property("data.format")
123
+
124
+ @property
125
+ def stage(self) -> str:
126
+ """Object stage"""
127
+ return self.get_property("fmu.context.stage")
128
+
129
+ @property
130
+ def aggregation(self) -> str:
131
+ """Object aggregation operation"""
132
+ return self.get_property("fmu.aggregation.operation")
133
+
134
+ @property
135
+ def realization(self) -> str:
136
+ """Object realization"""
137
+ return self.get_property("fmu.realization.id")
138
+
139
+ @property
140
+ def ensemble(self) -> str:
141
+ """Object ensemble"""
142
+ return self.get_property("fmu.ensemble.name")
143
+
144
+ @property
145
+ def context(self) -> str:
146
+ """Object context"""
147
+ return self.get_property("fmu.context.stage")
148
+
149
+ @property
150
+ def vertical_domain(self) -> str:
151
+ """Object vertical domain"""
152
+ return self.get_property("data.vertical_domain")
153
+
154
+ @property
155
+ def stratigraphic(self) -> str:
156
+ """Object stratigraphic"""
157
+ return self.get_property("data.stratigraphic")
158
+
159
+ @property
160
+ def columns(self) -> list[str]:
161
+ """Object table columns"""
162
+ return self.get_property("data.spec.columns")
163
+
164
+ @property
165
+ def tagname(self) -> str:
166
+ """Object tagname"""
167
+ return self.get_property("data.tagname")
168
+
169
+ @property
170
+ def content(self) -> str:
171
+ """Content"""
172
+ return self.get_property("data.content")
173
+
174
+ @property
175
+ def caseuuid(self) -> str:
176
+ """Object case uuid"""
177
+ return self.get_property("fmu.case.uuid")
178
+
179
+ @property
180
+ def casename(self) -> str:
181
+ """Object case name"""
182
+ return self.get_property("fmu.case.name")
183
+
184
+ @property
185
+ def classname(self) -> str:
186
+ """Object class name"""
187
+ return self.get_property("class.name")
188
+
189
+ @property
190
+ def dataname(self) -> str:
191
+ """Object data name"""
192
+ return self.get_property("data.name")
193
+
194
+ @property
195
+ def name(self) -> str:
196
+ """Object data name"""
197
+ return self.get_property("data.name")
198
+
199
+ @property
200
+ def asset(self) -> str:
201
+ """Object asset name"""
202
+ return self.get_property("access.asset.name")
203
+
204
+ @property
205
+ def operationname(self) -> str:
206
+ """Object aggregation operation name"""
207
+ return self.get_property("fmu.aggregation.operation")
208
+
209
+ @property
210
+ def entity(self) -> str:
211
+ """Entity uuid for object."""
212
+ return self.get_property("fmu.entity.uuid")
@@ -0,0 +1,79 @@
1
+ """Contains class for one document"""
2
+
3
+ import re
4
+ from typing import Any
5
+
6
+ _path_split_rx = re.compile(r"\]\.|\.|\[")
7
+
8
+
9
+ def _splitpath(path):
10
+ parts = _path_split_rx.split(path)
11
+ return [int(x) if re.match(r"\d+", x) else x for x in parts]
12
+
13
+
14
+ class Document:
15
+ """Class for representing a document in Sumo"""
16
+
17
+ def __init__(self, metadata: dict) -> None:
18
+ self._uuid = metadata["_id"]
19
+ self._metadata = metadata["_source"]
20
+
21
+ def __str__(self):
22
+ return (
23
+ f"<{self.__class__.__name__}: {self.name} {self.uuid}(uuid) "
24
+ f"in asset {self.asset}>"
25
+ )
26
+
27
+ def __repr__(self):
28
+ return self.__str__()
29
+
30
+ @property
31
+ def uuid(self):
32
+ """Return uuid
33
+
34
+ Returns:
35
+ str: the uuid of the case
36
+ """
37
+ return self._uuid
38
+
39
+ @property
40
+ def metadata(self):
41
+ """Return metadata for document
42
+
43
+ Returns:
44
+ dict: the metadata
45
+ """
46
+ return self._metadata
47
+
48
+ def _get_property(self, path: list[str | int]):
49
+ curr = self._metadata
50
+
51
+ for key in path:
52
+ if (
53
+ isinstance(curr, list)
54
+ and isinstance(key, int)
55
+ and key < len(curr)
56
+ ) or key in curr:
57
+ curr = curr[key]
58
+ else:
59
+ return None
60
+
61
+ return curr
62
+
63
+ def get_property(self, path: str) -> Any:
64
+ return self._get_property(_splitpath(path))
65
+
66
+ def __getitem__(self, key: str):
67
+ return self._metadata[key]
68
+
69
+ @property
70
+ def template_path(self) -> str:
71
+ return ""
72
+
73
+ @property
74
+ def name(self) -> str:
75
+ return "Should not happen"
76
+
77
+ @property
78
+ def asset(self) -> str:
79
+ return self.get_property("access.asset.name")