Skip to content

Commit 6a40125

Browse files
committed
Added structural relations to .json and fixed error in second example
1 parent 61b7dec commit 6a40125

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

examples/tutorials/z_other_tutorials/json_io/02_horizontal_stratigraphic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
"series": [
5252
{
5353
"name": "Strat_Series",
54-
"surfaces": ["rock2", "rock1"]
54+
"surfaces": ["rock2", "rock1"],
55+
"structural_relation": "ERODE"
5556
}
5657
],
5758
"grid_settings": {

gempy/modules/json_io/json_operations.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,16 @@ def load_model_from_json(file_path: str):
5959
# Create a mapping from surface points to their names
6060
surface_point_names = {}
6161
for sp in data['surface_points']:
62-
surface_point_names[sp['id']] = next((name for series in data['series']
63-
for name in series['surfaces']
64-
if name in surface_names), "surface_0")
62+
# Find the surface name that corresponds to this ID
63+
surface_name = None
64+
for series in data['series']:
65+
for i, name in enumerate(series['surfaces']):
66+
if i == sp['id']: # Match the ID with the index in the series
67+
surface_name = name
68+
break
69+
if surface_name is not None:
70+
break
71+
surface_point_names[sp['id']] = surface_name if surface_name is not None else f"surface_{sp['id']}"
6572

6673
# Load surface points and orientations
6774
surface_points = JsonIO._load_surface_points(data['surface_points'], surface_point_names)
@@ -114,13 +121,13 @@ def load_model_from_json(file_path: str):
114121
return model
115122

116123
@staticmethod
117-
def _load_surface_points(surface_points_data: List[SurfacePoint], id_to_name: Dict[int, str]):
124+
def _load_surface_points(surface_points_data: List[SurfacePoint], id_to_name: Optional[Dict[int, str]] = None):
118125
"""
119126
Load surface points from JSON data.
120127
121128
Args:
122129
surface_points_data (List[SurfacePoint]): List of surface point dictionaries
123-
id_to_name (Dict[int, str]): Mapping from surface IDs to names
130+
id_to_name (Optional[Dict[int, str]]): Optional mapping from surface IDs to names
124131
125132
Returns:
126133
SurfacePointsTable: A new SurfacePointsTable instance
@@ -132,7 +139,7 @@ def _load_surface_points(surface_points_data: List[SurfacePoint], id_to_name: Di
132139
from gempy.core.data.surface_points import SurfacePointsTable
133140

134141
# Validate data structure
135-
required_fields = {'x', 'y', 'z', 'nugget', 'id'} # Add 'id' back to required fields
142+
required_fields = {'x', 'y', 'z', 'nugget', 'id'}
136143
for i, sp in enumerate(surface_points_data):
137144
missing_fields = required_fields - set(sp.keys())
138145
if missing_fields:
@@ -149,7 +156,13 @@ def _load_surface_points(surface_points_data: List[SurfacePoint], id_to_name: Di
149156
y = np.array([sp['y'] for sp in surface_points_data])
150157
z = np.array([sp['z'] for sp in surface_points_data])
151158
nugget = np.array([sp['nugget'] for sp in surface_points_data])
152-
names = [id_to_name.get(sp['id'], "surface_0") for sp in surface_points_data]
159+
160+
# Handle names based on whether id_to_name mapping is provided
161+
if id_to_name is not None:
162+
names = [id_to_name.get(sp['id'], f"surface_{sp['id']}") for sp in surface_points_data]
163+
else:
164+
# If no mapping provided, use surface IDs as names
165+
names = [f"surface_{sp['id']}" for sp in surface_points_data]
153166

154167
# Create SurfacePointsTable
155168
return SurfacePointsTable.from_arrays(
@@ -161,13 +174,13 @@ def _load_surface_points(surface_points_data: List[SurfacePoint], id_to_name: Di
161174
)
162175

163176
@staticmethod
164-
def _load_orientations(orientations_data: List[Orientation], id_to_name: Dict[int, str]):
177+
def _load_orientations(orientations_data: List[Orientation], id_to_name: Optional[Dict[int, str]] = None):
165178
"""
166179
Load orientations from JSON data.
167180
168181
Args:
169182
orientations_data (List[Orientation]): List of orientation dictionaries
170-
id_to_name (Dict[int, str]): Mapping from surface IDs to names
183+
id_to_name (Optional[Dict[int, str]]): Optional mapping from surface IDs to names
171184
172185
Returns:
173186
OrientationsTable: A new OrientationsTable instance
@@ -179,7 +192,7 @@ def _load_orientations(orientations_data: List[Orientation], id_to_name: Dict[in
179192
from gempy.core.data.orientations import OrientationsTable
180193

181194
# Validate data structure
182-
required_fields = {'x', 'y', 'z', 'G_x', 'G_y', 'G_z', 'nugget', 'polarity', 'id'} # Add 'id' back to required fields
195+
required_fields = {'x', 'y', 'z', 'G_x', 'G_y', 'G_z', 'nugget', 'polarity', 'id'}
183196
for i, ori in enumerate(orientations_data):
184197
missing_fields = required_fields - set(ori.keys())
185198
if missing_fields:
@@ -201,7 +214,13 @@ def _load_orientations(orientations_data: List[Orientation], id_to_name: Dict[in
201214
G_y = np.array([ori['G_y'] for ori in orientations_data])
202215
G_z = np.array([ori['G_z'] for ori in orientations_data])
203216
nugget = np.array([ori['nugget'] for ori in orientations_data])
204-
names = [id_to_name.get(ori['id'], "surface_0") for ori in orientations_data]
217+
218+
# Handle names based on whether id_to_name mapping is provided
219+
if id_to_name is not None:
220+
names = [id_to_name.get(ori['id'], f"surface_{ori['id']}") for ori in orientations_data]
221+
else:
222+
# If no mapping provided, use surface IDs as names
223+
names = [f"surface_{ori['id']}" for ori in orientations_data]
205224

206225
# Apply polarity to gradients
207226
for i, ori in enumerate(orientations_data):

0 commit comments

Comments
 (0)