Skip to content

Commit f91fa03

Browse files
committed
fix: Update JSON loading to use surface names from series data - Add id_to_name mapping from surface IDs to names - Update surface points and orientations loading to use actual surface names - Fix issue with surface names not matching between structural frame and series
1 parent cb34e62 commit f91fa03

File tree

1 file changed

+64
-18
lines changed

1 file changed

+64
-18
lines changed

gempy/modules/json_io/json_operations.py

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,14 @@
77
from typing import Dict, Any, Optional, List
88
import numpy as np
99

10-
from gempy.core.data.surface_points import SurfacePointsTable
11-
from gempy.core.data.orientations import OrientationsTable
12-
from gempy.core.data.structural_frame import StructuralFrame
13-
from gempy.core.data.grid import Grid
14-
from gempy.core.data.geo_model import GeoModel
1510
from .schema import SurfacePoint, Orientation, GemPyModelJson
1611

1712

1813
class JsonIO:
1914
"""Class for handling JSON I/O operations for GemPy models."""
2015

2116
@staticmethod
22-
def load_model_from_json(file_path: str) -> GeoModel:
17+
def load_model_from_json(file_path: str):
2318
"""
2419
Load a GemPy model from a JSON file.
2520
@@ -29,34 +24,81 @@ def load_model_from_json(file_path: str) -> GeoModel:
2924
Returns:
3025
GeoModel: A new GemPy model instance
3126
"""
27+
# Import here to avoid circular imports
28+
from gempy.core.data.geo_model import GeoModel
29+
from gempy.core.data.grid import Grid
30+
from gempy.core.data.structural_frame import StructuralFrame
31+
from gempy_engine.core.data import InterpolationOptions
32+
from gempy.API.map_stack_to_surfaces_API import map_stack_to_surfaces
33+
3234
with open(file_path, 'r') as f:
3335
data = json.load(f)
3436

3537
# Validate the JSON data against our schema
3638
if not JsonIO._validate_json_schema(data):
3739
raise ValueError("Invalid JSON schema")
3840

41+
# Get surface names from series data
42+
surface_names = []
43+
for series in data['series']:
44+
surface_names.extend(series['surfaces'])
45+
46+
# Create id to name mapping
47+
id_to_name = {i: name for i, name in enumerate(surface_names)}
48+
3949
# Load surface points and orientations
40-
surface_points = JsonIO._load_surface_points(data['surface_points'])
41-
orientations = JsonIO._load_orientations(data['orientations'])
50+
surface_points = JsonIO._load_surface_points(data['surface_points'], id_to_name)
51+
orientations = JsonIO._load_orientations(data['orientations'], id_to_name)
52+
53+
# Create structural frame
54+
structural_frame = StructuralFrame.from_data_tables(surface_points, orientations)
4255

43-
# TODO: Load other components
44-
raise NotImplementedError("Only surface points and orientations loading is implemented")
56+
# Create grid
57+
grid = Grid(
58+
extent=data['grid_settings']['regular_grid_extent'],
59+
resolution=data['grid_settings']['regular_grid_resolution']
60+
)
61+
62+
# Create interpolation options
63+
interpolation_options = InterpolationOptions(
64+
range=1.7, # Default value
65+
c_o=10, # Default value
66+
mesh_extraction=True, # Default value
67+
number_octree_levels=1 # Default value
68+
)
69+
70+
# Create GeoModel
71+
model = GeoModel(
72+
name=data['metadata']['name'],
73+
structural_frame=structural_frame,
74+
grid=grid,
75+
interpolation_options=interpolation_options
76+
)
77+
78+
# Map series to surfaces
79+
for series in data['series']:
80+
map_stack_to_surfaces(model, {series['name']: series['surfaces']})
81+
82+
return model
4583

4684
@staticmethod
47-
def _load_surface_points(surface_points_data: List[SurfacePoint]) -> SurfacePointsTable:
85+
def _load_surface_points(surface_points_data: List[SurfacePoint], id_to_name: Dict[int, str]):
4886
"""
4987
Load surface points from JSON data.
5088
5189
Args:
5290
surface_points_data (List[SurfacePoint]): List of surface point dictionaries
91+
id_to_name (Dict[int, str]): Mapping from surface IDs to names
5392
5493
Returns:
5594
SurfacePointsTable: A new SurfacePointsTable instance
5695
5796
Raises:
5897
ValueError: If the data is invalid or missing required fields
5998
"""
99+
# Import here to avoid circular imports
100+
from gempy.core.data.surface_points import SurfacePointsTable
101+
60102
# Validate data structure
61103
required_fields = {'x', 'y', 'z', 'id', 'nugget'}
62104
for i, sp in enumerate(surface_points_data):
@@ -79,32 +121,36 @@ def _load_surface_points(surface_points_data: List[SurfacePoint]) -> SurfacePoin
79121

80122
# Create name_id_map from unique IDs
81123
unique_ids = np.unique(ids)
82-
name_id_map = {f"surface_{id}": id for id in unique_ids}
124+
name_id_map = {id_to_name[id]: id for id in unique_ids}
83125

84126
# Create SurfacePointsTable
85127
return SurfacePointsTable.from_arrays(
86128
x=x,
87129
y=y,
88130
z=z,
89-
names=[f"surface_{id}" for id in ids],
131+
names=[id_to_name[id] for id in ids],
90132
nugget=nugget,
91133
name_id_map=name_id_map
92134
)
93135

94136
@staticmethod
95-
def _load_orientations(orientations_data: List[Orientation]) -> OrientationsTable:
137+
def _load_orientations(orientations_data: List[Orientation], id_to_name: Dict[int, str]):
96138
"""
97139
Load orientations from JSON data.
98140
99141
Args:
100142
orientations_data (List[Orientation]): List of orientation dictionaries
143+
id_to_name (Dict[int, str]): Mapping from surface IDs to names
101144
102145
Returns:
103146
OrientationsTable: A new OrientationsTable instance
104147
105148
Raises:
106149
ValueError: If the data is invalid or missing required fields
107150
"""
151+
# Import here to avoid circular imports
152+
from gempy.core.data.orientations import OrientationsTable
153+
108154
# Validate data structure
109155
required_fields = {'x', 'y', 'z', 'G_x', 'G_y', 'G_z', 'id', 'nugget', 'polarity'}
110156
for i, ori in enumerate(orientations_data):
@@ -139,7 +185,7 @@ def _load_orientations(orientations_data: List[Orientation]) -> OrientationsTabl
139185

140186
# Create name_id_map from unique IDs
141187
unique_ids = np.unique(ids)
142-
name_id_map = {f"surface_{id}": id for id in unique_ids}
188+
name_id_map = {id_to_name[id]: id for id in unique_ids}
143189

144190
# Create OrientationsTable
145191
return OrientationsTable.from_arrays(
@@ -149,18 +195,18 @@ def _load_orientations(orientations_data: List[Orientation]) -> OrientationsTabl
149195
G_x=G_x,
150196
G_y=G_y,
151197
G_z=G_z,
152-
names=[f"surface_{id}" for id in ids],
198+
names=[id_to_name[id] for id in ids],
153199
nugget=nugget,
154200
name_id_map=name_id_map
155201
)
156202

157203
@staticmethod
158-
def save_model_to_json(model: GeoModel, file_path: str) -> None:
204+
def save_model_to_json(model, file_path: str) -> None:
159205
"""
160206
Save a GemPy model to a JSON file.
161207
162208
Args:
163-
model (GeoModel): The GemPy model to save
209+
model: The GemPy model to save
164210
file_path (str): Path where to save the JSON file
165211
"""
166212
# TODO: Implement saving logic

0 commit comments

Comments
 (0)