Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions docs/proto/KeyValueStore.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,42 @@ message KeyValueStoreInputChunk {
}
}

// Element type of the values stored for a key
enum KeyValueElementType {
KEY_VALUE_ELEMENT_TYPE_FLOAT32 = 0;
KEY_VALUE_ELEMENT_TYPE_FLOAT64 = 1;
KEY_VALUE_ELEMENT_TYPE_INT32 = 2;
KEY_VALUE_ELEMENT_TYPE_INT64 = 3;
KEY_VALUE_ELEMENT_TYPE_UINT32 = 4;
KEY_VALUE_ELEMENT_TYPE_UINT64 = 5;
}

message KeyValueInputParameters {
// Name needs to be sent in the first message
string name = 1;
int64 num_elements = 2;
KeyValueElementType element_type = 3;
}

message KeyValueStoreChunk { repeated float values = 1; }
// FLOAT32 values travel in values, FLOAT64 in double_values, INT32 and INT64
// in int_values, UINT32 and UINT64 in uint_values
message KeyValueStoreChunk {
repeated float values = 1;
repeated double double_values = 2;
repeated sint64 int_values = 3;
repeated uint64 uint_values = 4;
}

message KeyValueStoreOutputRequest { string name = 1; }
message KeyValueStoreOutputRequest {
string name = 1;
KeyValueElementType element_type = 2;
}

message KeyValueStoreOutputChunk { repeated float values = 1; }
message KeyValueStoreOutputChunk {
repeated float values = 1;
repeated double double_values = 2;
repeated sint64 int_values = 3;
repeated uint64 uint_values = 4;
}

message KeyValueStoreRemoveRequest { string name = 1; }
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
######################################################################
# This example imports a polygon from a .pol file
######################################################################
import os
import tempfile

import rips

resinsight = rips.Instance.find()

polygon_collection = resinsight.project.descendants(rips.PolygonCollection)[0]

with tempfile.TemporaryDirectory(prefix="rips") as tmpdirname:
# A .pol file has one "x y depth" point per line. "999.0 999.0 999.0" ends a polygon.
file_name = os.path.join(tmpdirname, "example_polygon.pol")
with open(file_name, "w") as f:
f.write("0.0 0.0 1000.0\n")
f.write("1000.0 0.0 1000.0\n")
f.write("1000.0 1000.0 1000.0\n")
f.write("0.0 1000.0 1000.0\n")
f.write("999.0 999.0 999.0\n")

# The polygons are imported into a new folder named after the file.
# The first polygon in the file is returned.
polygon = polygon_collection.import_polygon_file(
file_name=file_name,
on_name_conflict=rips.NameConflictPolicy.AUTO_RENAME,
)

print(f"Imported polygon: {polygon.name}")
for coordinate in polygon.coordinates:
print(coordinate)
26 changes: 14 additions & 12 deletions docs/rips/generated/KeyValueStore_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions docs/rips/generated/generated_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2772,6 +2772,20 @@ def create_polygon(self, name: str="", coordinates: List[List[float]]=[], on_nam
return self._call_pdm_method_return_value("CreatePolygon", Polygon, name=name, coordinates=coordinates, on_name_conflict=on_name_conflict)


def import_polygon_file(self, file_name: str="", name: str="", on_name_conflict: NameConflictPolicy=NameConflictPolicy.FAIL) -> Polygon:
"""
Import polygons from file into a new folder. The folder is named after the file unless a name is given. Returns the first polygon in the file

Arguments:
file_name (str): Polygon file (*.pol, *.dat, *.txt, *.csv)
name (str): Name of the new folder. A file with a single polygon also gives the polygon this name. Defaults to the file name
on_name_conflict (NameConflictPolicy): One of [FAIL, AUTO_RENAME, OVERWRITE]
Returns:
Polygon
"""
return self._call_pdm_method_return_value("ImportPolygonFile", Polygon, file_name=file_name, name=name, on_name_conflict=on_name_conflict)


def polygons(self) -> List[Polygon]:
"""Polygons

Expand Down Expand Up @@ -4622,8 +4636,8 @@ def set_geometry_from_keys(self, vertices_key: str="", triangles_key: str="") ->
Set geometry from keys.

Arguments:
vertices_key (str): Key for x, y and depth of each vertex
triangles_key (str): Key for three vertex indices per triangle
vertices_key (str): Key for x, y and depth of each vertex (float64)
triangles_key (str): Key for three vertex indices per triangle (uint32)
Returns:

"""
Expand Down
78 changes: 63 additions & 15 deletions docs/rips/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
WellPath,
)

from typing import Optional, List
from typing import Optional, List, Tuple


@add_method(Project)
Expand Down Expand Up @@ -540,36 +540,73 @@ def create_corner_point_grid(
)


# Element type and chunk field for each numpy dtype name the key-value store transports
_KEY_VALUE_ELEMENT_TYPES = {
"float32": (KeyValueStore_pb2.KEY_VALUE_ELEMENT_TYPE_FLOAT32, "values"),
"float64": (KeyValueStore_pb2.KEY_VALUE_ELEMENT_TYPE_FLOAT64, "double_values"),
"int32": (KeyValueStore_pb2.KEY_VALUE_ELEMENT_TYPE_INT32, "int_values"),
"int64": (KeyValueStore_pb2.KEY_VALUE_ELEMENT_TYPE_INT64, "int_values"),
"uint32": (KeyValueStore_pb2.KEY_VALUE_ELEMENT_TYPE_UINT32, "uint_values"),
"uint64": (KeyValueStore_pb2.KEY_VALUE_ELEMENT_TYPE_UINT64, "uint_values"),
}


def _key_value_element_type(dtype: object) -> Tuple[int, str]:
# Accepts a numpy dtype, a numpy scalar type or a dtype name, without importing numpy
if isinstance(dtype, type):
name = dtype.__name__
else:
name = str(getattr(dtype, "name", dtype))
if name not in _KEY_VALUE_ELEMENT_TYPES:
raise ValueError(
"Unsupported dtype {}. Supported: {}".format(
name, ", ".join(_KEY_VALUE_ELEMENT_TYPES)
)
)
return _KEY_VALUE_ELEMENT_TYPES[name]


@add_method(Project)
def set_key_values(self, key, values):
def set_key_values(self, key, values, dtype="float32"):
"""Sets values for a given key in the key-value store.

Arguments:
key(str): The key (should be unique).
values(list): a list of double precision floating point numbers
values(list): a list of numbers
dtype: numpy dtype of the values: float32 (default), float64, int32, int64,
uint32 or uint64. Reading the key needs the same dtype.
"""
request_iterator = self.__generate_key_value_store_input_chunks(values, key)
element_type, field = _key_value_element_type(dtype)
request_iterator = self.__generate_key_value_store_input_chunks(
values, key, element_type, field
)
reply = self.__key_value_store_stub.SetValue(request_iterator)
if reply.accepted_value_count < len(values):
raise IndexError


@add_method(Project)
def __generate_key_value_store_input_chunks(self, array, name):
def __generate_key_value_store_input_chunks(
self,
array,
name,
element_type=KeyValueStore_pb2.KEY_VALUE_ELEMENT_TYPE_FLOAT32,
field="values",
):
index = -1
while index < len(array):
chunk = KeyValueStore_pb2.KeyValueStoreInputChunk()
if index == -1:
parameters = KeyValueStore_pb2.KeyValueInputParameters(
name=name, num_elements=len(array)
name=name, num_elements=len(array), element_type=element_type
)
chunk.parameters.CopyFrom(parameters)
index += 1
else:
actual_chunk_size = min(len(array) - index + 1, self.chunk_size)
chunk.values.CopyFrom(
KeyValueStore_pb2.KeyValueStoreChunk(
values=array[index : index + actual_chunk_size]
**{field: array[index : index + actual_chunk_size]}
)
)
index += actual_chunk_size
Expand All @@ -584,35 +621,46 @@ def __generate_key_value_store_input_chunks(self, array, name):
def key_values_async(
self,
key,
dtype="float32",
):
"""Get float values for a given key. Async: returns an iterator.
"""Get values for a given key. Async: returns an iterator.

Arguments:
key(str): The key to get.
dtype: numpy dtype the values were set with: float32 (default), float64,
int32, int64, uint32 or uint64.

Returns:
An iterator to a chunk object containing an array of float values
An iterator to a chunk object containing an array of values: values for
float32, double_values for float64, int_values for int32 and int64 and
uint_values for uint32 and uint64.
Loop through the chunks and then the values within the chunk to get all values.
"""
request = KeyValueStore_pb2.KeyValueStoreOutputRequest(name=key)
element_type, _ = _key_value_element_type(dtype)
request = KeyValueStore_pb2.KeyValueStoreOutputRequest(
name=key, element_type=element_type
)
for chunk in self.__key_value_store_stub.GetValue(request):
yield chunk


@add_method(Project)
def key_values(self, key):
"""Get float values for a given key. Synchronous: returns a list. `
def key_values(self, key, dtype="float32"):
"""Get values for a given key. Synchronous: returns a list. `

Arguments:
key(str): The key to get.
dtype: numpy dtype the values were set with: float32 (default), float64,
int32, int64, uint32 or uint64.

Returns:
A list of float values
A list of values
"""
_, field = _key_value_element_type(dtype)
all_values = []
generator = self.key_values_async(key)
generator = self.key_values_async(key, dtype)
for chunk in generator:
for value in chunk.values:
for value in getattr(chunk, field):
all_values.append(value)
return all_values

Expand Down
45 changes: 35 additions & 10 deletions docs/rips/surface.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
import operator
import uuid
from collections.abc import Iterable

Expand All @@ -12,9 +13,10 @@
SurfaceInterface,
TriangulatedSurface,
)
from typing import List, Optional, Sequence, Type, TypeVar, Union
from typing import Any, Callable, List, Optional, Sequence, Type, TypeVar, Union

SurfaceT = TypeVar("SurfaceT", bound=SurfaceInterface)
ValueT = TypeVar("ValueT")


@add_method(RegularSurface)
Expand Down Expand Up @@ -73,16 +75,32 @@ def get_property(self: RegularSurface, name: str) -> List[float]:
pass


def _flatten(values: Sequence[Union[float, Iterable[float]]]) -> List[float]:
flat: List[float] = []
def _flatten(
values: Sequence[Union[Any, Iterable[Any]]], convert: Callable[[Any], ValueT]
) -> List[ValueT]:
flat: List[ValueT] = []
for value in values:
if isinstance(value, Iterable):
flat.extend(float(v) for v in value)
flat.extend(convert(v) for v in value)
else:
flat.append(float(value))
flat.append(convert(value))
return flat


def _triangle_index(value: Any) -> int:
# Whole-number floats such as 2.0, also numpy floats, are accepted as indices
try:
index = operator.index(value)
except TypeError:
number = float(value)
if not number.is_integer():
raise RipsError("Invalid triangle index {}".format(value))
index = int(number)
if index < 0:
raise RipsError("Invalid triangle index {}".format(value))
return index


@add_method(TriangulatedSurface)
def set_geometry(
self: TriangulatedSurface,
Expand All @@ -95,9 +113,9 @@ def set_geometry(

Arguments:
vertices (List[List[float]]): x, y and depth of each vertex, depth positive
downwards (float32). A flat list of x, y, depth values is also accepted.
triangles (List[List[int]]): Three vertex indices per triangle, zero based.
A flat list of indices is also accepted.
downwards (float64). A flat list of x, y, depth values is also accepted.
triangles (List[List[int]]): Three vertex indices per triangle, zero based
(uint32). A flat list of indices is also accepted.
"""
project = self.ancestor(Project)
if not project:
Expand All @@ -106,8 +124,15 @@ def set_geometry(
vertices_key = "{}_{}".format(uuid.uuid4(), "triangulated_surface_vertices")
triangles_key = "{}_{}".format(uuid.uuid4(), "triangulated_surface_triangles")

project.set_key_values(vertices_key, _flatten(vertices))
project.set_key_values(triangles_key, _flatten(triangles))
vertex_values = _flatten(vertices, float)
indices = _flatten(triangles, _triangle_index)

project.set_key_values(vertices_key, vertex_values, dtype="float64")
try:
project.set_key_values(triangles_key, indices, dtype="uint32")
except Exception:
project.remove_key_values(vertices_key)
raise
self.set_geometry_from_keys(vertices_key=vertices_key, triangles_key=triangles_key)


Expand Down
Loading