Skip to content

Commit 12043f4

Browse files
authored
Adding server files GEN-11702 (#9)
Adding server files [WIP/TEST] Trying to add server tests
2 parents ad5e50d + a8241df commit 12043f4

File tree

13 files changed

+228
-196
lines changed

13 files changed

+228
-196
lines changed

gempy_engine/API/server/_old_example.json

Lines changed: 0 additions & 75 deletions
This file was deleted.
Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import logging
3+
from typing import List
34

45
import numpy as np
56
import pandas as pd
@@ -12,92 +13,118 @@
1213
PLOT_SUBSURFACE_OBJECT = False
1314

1415

15-
def process_output(meshes: list[DualContouringMesh], n_stack: int, solutions: Solutions, logger: logging.Logger) \
16-
-> bytes:
17-
# * serialize meshes
16+
def process_output(
17+
meshes: List[DualContouringMesh],
18+
n_stack: int,
19+
solutions: Solutions,
20+
logger: logging.Logger
21+
) -> bytes:
22+
"""
23+
Process model outputs into binary format for transmission.
24+
25+
Args:
26+
meshes: List of dual contouring meshes
27+
n_stack: Number of stacks in the model
28+
solutions: Model solutions containing octree data
29+
logger: Logger instance
30+
31+
Returns:
32+
Binary data containing serialized meshes and octrees
33+
"""
34+
# Serialize meshes
1835
unstructured_data_meshes: UnstructuredData = _meshes_to_unstruct(meshes, logger)
1936
if PLOT_SUBSURFACE_OBJECT:
2037
_plot_subsurface_object(unstructured_data_meshes)
21-
body_meshes, header_meshes = unstructured_data_meshes.to_binary()
22-
header_json = json.dumps(header_meshes)
23-
header_json_bytes = header_json.encode('utf-8')
24-
header_json_length = len(header_json_bytes)
25-
header_json_length_bytes = header_json_length.to_bytes(4, byteorder='little')
26-
body_meshes = header_json_length_bytes + header_json_bytes + body_meshes
27-
28-
# * serialize octrees
38+
body_meshes = unstructured_data_meshes.to_binary()
39+
40+
# Serialize octrees
2941
unstructured_data_volume = subsurface.UnstructuredData.from_array(
3042
vertex=solutions.octrees_output[0].grid_centers.values,
3143
cells="points",
3244
cells_attr=pd.DataFrame(
3345
data=solutions.octrees_output[0].last_output_center.ids_block,
3446
columns=['id']
35-
) # TODO: We have to create an array with the shape of simplex array with the id of each simplex
47+
)
3648
)
49+
body_volume = unstructured_data_volume.to_binary()
3750

38-
body_volume, header_volume = unstructured_data_volume.to_binary()
39-
header_json = json.dumps(header_volume)
40-
header_json_bytes = header_json.encode('utf-8')
41-
header_json_length = len(header_json_bytes)
42-
header_json_length_bytes = header_json_length.to_bytes(4, byteorder='little')
43-
body_volume = header_json_length_bytes + header_json_bytes + body_volume
44-
45-
# * serialize global header
51+
# Serialize global header and combine data
4652
body = body_meshes + body_volume
4753
global_header = json.dumps({"mesh_size": len(body_meshes), "octree_size": len(body_volume)})
4854
global_header_bytes = global_header.encode('utf-8')
4955
global_header_length = len(global_header_bytes)
5056
global_header_length_bytes = global_header_length.to_bytes(4, byteorder='little')
51-
body = global_header_length_bytes + global_header_bytes + body
52-
return body
57+
58+
return global_header_length_bytes + global_header_bytes + body
5359

5460

55-
def _meshes_to_unstruct(meshes: list[DualContouringMesh], logger: logging.Logger) -> UnstructuredData:
56-
# ? I added this function to the Solutions class
61+
def _meshes_to_unstruct(
62+
meshes: List[DualContouringMesh],
63+
logger: logging.Logger
64+
) -> UnstructuredData:
65+
"""
66+
Convert a list of dual contouring meshes to an unstructured data format.
67+
68+
Args:
69+
meshes: List of dual contouring meshes
70+
logger: Logger instance
71+
72+
Returns:
73+
Unstructured data representation of the meshes
74+
"""
5775
n_meshes = len(meshes)
5876
logger.debug(f"Number of meshes: {n_meshes}")
59-
logger.debug("Mesh1TriShape" + str(meshes[0].edges.shape))
77+
logger.debug(f"Mesh1TriShape: {meshes[0].edges.shape}")
6078

79+
# Prepare the vertex array
6180
vertex_array = np.concatenate([meshes[i].vertices for i in range(n_meshes)])
81+
82+
# Check for edge uniqueness (debugging)
6283
simplex_array = np.concatenate([meshes[i].edges for i in range(n_meshes)])
6384
unc, count = np.unique(simplex_array, axis=0, return_counts=True)
6485
logger.debug(f"edges shape {simplex_array.shape}")
6586

66-
# * Prepare the simplex array
87+
# Prepare the simplex array with proper indexing
6788
simplex_array = meshes[0].edges
68-
for i in range(1,n_meshes):
89+
for i in range(1, n_meshes):
6990
adder = np.max(meshes[i - 1].edges) + 1
70-
logger.debug("triangle counts adder:" + str(adder))
91+
logger.debug(f"triangle counts adder: {adder}")
7192
add_mesh = meshes[i].edges + adder
7293
simplex_array = np.append(simplex_array, add_mesh, axis=0)
7394

7495
logger.debug(f"edges shape {simplex_array.shape}")
7596

76-
# * Prepare the cells_attr array
97+
# Prepare the cells_attr array
7798
ids_array = np.ones(simplex_array.shape[0])
7899
l0 = 0
79-
id = 1
100+
id_value = 1
80101
for mesh in meshes:
81102
l1 = l0 + mesh.edges.shape[0]
82-
logger.debug(f"l0 {l0} l1 {l1} id {id}")
83-
ids_array[l0:l1] = id
103+
logger.debug(f"l0 {l0} l1 {l1} id {id_value}")
104+
ids_array[l0:l1] = id_value
84105
l0 = l1
85-
id += 1
86-
logger.debug("ids_array count" + str(np.unique(ids_array)))
106+
id_value += 1
107+
logger.debug(f"ids_array count: {np.unique(ids_array)}")
87108

88-
# * Create the unstructured data
109+
# Create the unstructured data
89110
unstructured_data = subsurface.UnstructuredData.from_array(
90111
vertex=vertex_array,
91112
cells=simplex_array,
92-
cells_attr=pd.DataFrame(ids_array, columns=['id']) # TODO: We have to create an array with the shape of simplex array with the id of each simplex
113+
cells_attr=pd.DataFrame(ids_array, columns=['id'])
93114
)
94115

95116
return unstructured_data
96117

97118

98-
def _plot_subsurface_object(unstructured_data):
119+
def _plot_subsurface_object(unstructured_data: UnstructuredData) -> None:
120+
"""
121+
Visualize the unstructured data using subsurface.
122+
123+
Args:
124+
unstructured_data: The unstructured data to visualize
125+
"""
99126
print(unstructured_data)
100127
obj = subsurface.TriSurf(unstructured_data)
101128
pv_unstruct = subsurface.visualization.to_pyvista_mesh(obj)
102129
print(pv_unstruct)
103-
subsurface.visualization.pv_plot([pv_unstruct])
130+
subsurface.visualization.pv_plot([pv_unstruct])
Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
import sys
2-
import logging
1+
import logging
2+
import sys
3+
from typing import Tuple
34

4-
from gempy_engine.core.data.engine_grid import EngineGrid
5-
from core.data.regular_grid import RegularGrid
65
from gempy_engine.core.data.input_data_descriptor import InputDataDescriptor
76
from gempy_engine.core.data.interpolation_input import InterpolationInput
87
from gempy_engine.core.data.kernel_classes.server.input_parser import GemPyInput
98

109

11-
def setup_logger():
10+
def setup_logger() -> logging.Logger:
11+
"""
12+
Configure and set up a logger for the application.
13+
14+
Returns:
15+
logging.Logger: Configured logger instance with console and file handlers
16+
"""
1217
logger = logging.getLogger("my-fastapi-app")
1318
logger.setLevel(logging.DEBUG)
1419

@@ -23,25 +28,38 @@ def setup_logger():
2328
fh = logging.FileHandler("app.log")
2429
fh.setLevel(logging.DEBUG)
2530
fh.setFormatter(formatter)
26-
27-
# Add the console handler to the logger
31+
32+
# Add the handlers to the logger
2833
logger.addHandler(ch)
2934
logger.addHandler(fh)
3035

3136
return logger
3237

3338

34-
def process_input(gempy_input: GemPyInput, logger: logging.Logger) -> (InputDataDescriptor, InterpolationInput, int):
39+
def process_input(
40+
gempy_input: GemPyInput,
41+
logger: logging.Logger
42+
) -> Tuple[InputDataDescriptor, InterpolationInput, int]:
43+
"""
44+
Process the GemPy input data to prepare it for model computation.
3545
36-
logger.debug("Input grid:", gempy_input.interpolation_input.grid)
37-
38-
gempy_input.interpolation_input.grid = EngineGrid.from_regular_grid(
39-
regular_grid=RegularGrid.from_schema(gempy_input.interpolation_input.grid)
40-
)
46+
Args:
47+
gempy_input: Input data for the GemPy model
48+
logger: Logger instance for recording processing information
49+
50+
Returns:
51+
Tuple containing:
52+
- InputDataDescriptor: Structure descriptor for the model
53+
- InterpolationInput: Prepared interpolation input data
54+
- int: Number of stacks in the model
55+
"""
56+
logger.debug(f"Input grid: {gempy_input.interpolation_input.grid}")
57+
4158
interpolation_input: InterpolationInput = InterpolationInput.from_schema(gempy_input.interpolation_input)
4259
input_data_descriptor: InputDataDescriptor = InputDataDescriptor.from_schema(gempy_input.input_data_descriptor)
4360
n_stack = len(input_data_descriptor.stack_structure.masking_descriptor)
44-
logger.debug("masking descriptor: ", input_data_descriptor.stack_structure.masking_descriptor)
45-
logger.debug("stack structure: ", input_data_descriptor.stack_structure)
46-
return input_data_descriptor, interpolation_input, n_stack
4761

62+
logger.debug(f"masking descriptor: {input_data_descriptor.stack_structure.masking_descriptor}")
63+
logger.debug(f"stack structure: {input_data_descriptor.stack_structure}")
64+
65+
return input_data_descriptor, interpolation_input, n_stack

0 commit comments

Comments
 (0)