Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
cec6e0f
feat: Add JSON I/O functionality for surface points
flohorovicic Mar 19, 2025
1e86bcd
feat: Add orientation data loading functionality
flohorovicic Mar 19, 2025
8d031e6
feat: Add horizontal stratigraphic model tutorial
flohorovicic Mar 19, 2025
7790956
fix: Update JSON loading to use surface names from series data - Add …
flohorovicic Mar 19, 2025
74ef1d3
fix: Update horizontal stratigraphic tutorial with correct data and m…
flohorovicic Mar 19, 2025
8e580d8
fix: correct IDs and positions for fault and rock1 in multiple series…
flohorovicic Mar 19, 2025
3eb7dc0
Added .json input file
flohorovicic Mar 19, 2025
5c2b339
Updated .json input file
flohorovicic Mar 19, 2025
430a5ac
Adjustments in stack-mapping for more flexible handling of faults
flohorovicic Mar 19, 2025
2f5a6b5
Added modules __init__ and minor changes in json module
flohorovicic Mar 19, 2025
04e87a1
fix: Fix metadata handling in JSON I/O for proper preservation when l…
flohorovicic Mar 22, 2025
1df8dac
Updated .gitignore (only to ignore files generated by new tutorial)
flohorovicic Mar 22, 2025
1c60ddd
Extended functionality to save .json and adjusted tests. Simple model…
flohorovicic Mar 23, 2025
427092e
Added structural relations to .json and fixed error in second example
flohorovicic Mar 23, 2025
3fd2106
Fixed problem with loading of surface layer stack
flohorovicic Mar 23, 2025
9caa992
Fixed stratigraphic pile handling in JSON I/O by reverting to working…
flohorovicic Mar 23, 2025
c5ddb5a
Included name-id mapping in .json
flohorovicic Mar 24, 2025
e6d8c23
Fix JSON serialization for NumPy types and update example data
flohorovicic Mar 24, 2025
f80c4b0
Adjusted date format
flohorovicic Mar 24, 2025
c06aa90
Simplified required json input further and added "minimal working exa…
flohorovicic Mar 25, 2025
2a2c46b
Simplified minimal input even further: now only points and orientatio…
flohorovicic Mar 25, 2025
c4f60aa
Updated minimal json examples and comparison to minimal GemPy model
flohorovicic Mar 25, 2025
e6f9794
Additional fixes to get defaults right
flohorovicic Mar 25, 2025
558a185
Added default nugget value to minimize input even further
flohorovicic Mar 25, 2025
f9862ed
Updated tests and fixed code to pass tests.
flohorovicic Mar 28, 2025
bf069e9
fix: Update fault model example with correct series mapping and visua…
flohorovicic Apr 5, 2025
77a9625
Improve scalar field visualization in fault model example - Add prope…
flohorovicic Apr 6, 2025
5948e42
Example model for a combination of series and faults from json
flohorovicic Apr 6, 2025
a615e8d
Add combination model JSON files to gitignore
flohorovicic Apr 6, 2025
56ac045
fix: preserve colors when loading models from JSON - Added color pres…
flohorovicic Apr 6, 2025
e35ec32
test: update JSON I/O tests to verify color preservation - Added colo…
flohorovicic Apr 6, 2025
986d6c8
Added TODOs for PR.
javoha Apr 11, 2025
27b2292
Added TODOs for PR.
javoha Apr 11, 2025
bb3dc7a
fix: ensure NotRequired import works for both Python 3.11+ and earlie…
flohorovicic Apr 27, 2025
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
13 changes: 11 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


.vscode
.idea
.DS_Store
Expand Down Expand Up @@ -172,3 +170,14 @@ examples/integrations/*.out
/.obsidian/core-plugins-migration.json
/.obsidian/hotkeys.json
/.obsidian/workspace.json

# Ignore generated tutorial files
examples/tutorials/z_other_tutorials/json_io/04_simple_layer_stack.py
examples/tutorials/z_other_tutorials/json_io/fault_scalar_field.png
examples/tutorials/z_other_tutorials/json_io/initial_model_y.png
examples/tutorials/z_other_tutorials/json_io/model_with_data.png
examples/tutorials/z_other_tutorials/json_io/multiple_series_faults_computed.json

# Generated JSON files from examples
examples/tutorials/z_other_tutorials/json_io/combination_model.json
examples/tutorials/z_other_tutorials/json_io/combination_model_computed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""
Tutorial for JSON I/O operations in GemPy - Surface Points
=======================================================

This tutorial demonstrates how to save and load surface points data using JSON files in GemPy.
"""

import json
import numpy as np
import gempy as gp
from gempy.modules.json_io import JsonIO

# Create a sample surface points dataset
# ------------------------------------

# Create some sample surface points
x = np.array([0, 1, 2, 3, 4])
y = np.array([0, 1, 2, 3, 4])
z = np.array([0, 1, 2, 3, 4])
ids = np.array([0, 0, 1, 1, 2]) # Three different surfaces
nugget = np.array([0.00002, 0.00002, 0.00002, 0.00002, 0.00002])

# Create name to id mapping
name_id_map = {f"surface_{id}": id for id in np.unique(ids)}

# Create a SurfacePointsTable
surface_points = gp.data.SurfacePointsTable.from_arrays(
x=x,
y=y,
z=z,
names=[f"surface_{id}" for id in ids],
nugget=nugget,
name_id_map=name_id_map
)

# Create a JSON file with the surface points data
# ---------------------------------------------

# Create the JSON structure
json_data = {
"metadata": {
"name": "sample_model",
"creation_date": "2024-03-19",
"last_modification_date": "2024-03-19",
"owner": "tutorial"
},
"surface_points": [
{
"x": float(x[i]),
"y": float(y[i]),
"z": float(z[i]),
"id": int(ids[i]),
"nugget": float(nugget[i])
}
for i in range(len(x))
],
"orientations": [],
"faults": [],
"series": [],
"grid_settings": {
"regular_grid_resolution": [10, 10, 10],
"regular_grid_extent": [0, 4, 0, 4, 0, 4],
"octree_levels": None
},
"interpolation_options": {}
}

# Save the JSON file
with open("sample_surface_points.json", "w") as f:
json.dump(json_data, f, indent=4)

# Load the surface points from JSON
# -------------------------------

# Load the model from JSON
loaded_surface_points = JsonIO._load_surface_points(json_data["surface_points"])

# Verify the loaded data
print("\nOriginal surface points:")
print(surface_points)
print("\nLoaded surface points:")
print(loaded_surface_points)

# Verify the data matches
print("\nVerifying data matches:")
print(f"X coordinates match: {np.allclose(surface_points.xyz[:, 0], loaded_surface_points.xyz[:, 0])}")
print(f"Y coordinates match: {np.allclose(surface_points.xyz[:, 1], loaded_surface_points.xyz[:, 1])}")
print(f"Z coordinates match: {np.allclose(surface_points.xyz[:, 2], loaded_surface_points.xyz[:, 2])}")
print(f"IDs match: {np.array_equal(surface_points.ids, loaded_surface_points.ids)}")
print(f"Nugget values match: {np.allclose(surface_points.nugget, loaded_surface_points.nugget)}")

# Print the name_id_maps to compare
print("\nName to ID mappings:")
print(f"Original: {surface_points.name_id_map}")
print(f"Loaded: {loaded_surface_points.name_id_map}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""
Tutorial: Loading a Horizontal Stratigraphic Model using JSON I/O
===============================================================

This tutorial demonstrates how to load a horizontal stratigraphic model using GemPy's JSON I/O functionality.
The model consists of two horizontal layers (rock1 and rock2) with surface points and orientations.
"""

# %%
# Import necessary libraries
import matplotlib
matplotlib.use('Agg') # Use non-interactive backend
import gempy as gp
import gempy_viewer as gpv
import numpy as np
import json
from pathlib import Path
import matplotlib.pyplot as plt
from datetime import datetime

# %%
# Define the model data
model_data = {
"metadata": {
"name": "Horizontal Stratigraphic Model",
"creation_date": "2024-03-24",
"last_modification_date": datetime.now().strftime("%Y-%m-%d"),
"owner": "GemPy Team"
},
"surface_points": [
{"x": 0.0, "y": 0.0, "z": 0.0, "id": 1, "nugget": 0.0},
{"x": 1.0, "y": 0.0, "z": 0.0, "id": 1, "nugget": 0.0},
{"x": 0.0, "y": 1.0, "z": 0.0, "id": 1, "nugget": 0.0},
{"x": 1.0, "y": 1.0, "z": 0.0, "id": 1, "nugget": 0.0},
{"x": 0.0, "y": 0.0, "z": 1.0, "id": 2, "nugget": 0.0},
{"x": 1.0, "y": 0.0, "z": 1.0, "id": 2, "nugget": 0.0},
{"x": 0.0, "y": 1.0, "z": 1.0, "id": 2, "nugget": 0.0},
{"x": 1.0, "y": 1.0, "z": 1.0, "id": 2, "nugget": 0.0}
],
"orientations": [
{"x": 0.5, "y": 0.5, "z": 0.0, "G_x": 0.0, "G_y": 0.0, "G_z": 1.0, "id": 1, "nugget": 0.0, "polarity": 1},
{"x": 0.5, "y": 0.5, "z": 1.0, "G_x": 0.0, "G_y": 0.0, "G_z": 1.0, "id": 2, "nugget": 0.0, "polarity": 1}
],
"series": [
{
"name": "series1",
"surfaces": ["layer1", "layer2"],
"structural_relation": "ERODE",
"colors": ["#ff0000", "#00ff00"]
}
],
"grid_settings": {
"regular_grid_resolution": [10, 10, 10],
"regular_grid_extent": [0.0, 1.0, 0.0, 1.0, 0.0, 1.0],
"octree_levels": None
},
"interpolation_options": {
"kernel_options": {
"range": 1.7,
"c_o": 10
},
"mesh_extraction": True,
"number_octree_levels": 1
},
"fault_relations": None,
"id_name_mapping": {
"name_to_id": {
"layer1": 1,
"layer2": 2
}
}
}

# %%
# Save the model data to a JSON file
tutorial_dir = Path(__file__).parent
json_file = tutorial_dir / "horizontal_stratigraphic.json"
with open(json_file, "w") as f:
json.dump(model_data, f, indent=4)

# %%
# Load the model from JSON
model = gp.modules.json_io.JsonIO.load_model_from_json(str(json_file))

# %%
# Compute the geological model
gp.compute_model(model)

# %%
# Plot the model
# Plot the initial geological model in the y direction without results
fig, ax = plt.subplots(figsize=(10, 6))
gpv.plot_2d(model, direction=['y'], show_results=False, ax=ax)
plt.title("Initial Geological Model (y direction)")
plt.savefig('initial_model_y.png')
plt.close()

# Plot the result of the model in the x and y direction with data and without boundaries
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 6))
gpv.plot_2d(model, direction=['x'], show_data=True, show_boundaries=False, ax=ax1)
ax1.set_title("Model with Data (x direction)")
gpv.plot_2d(model, direction=['y'], show_data=True, show_boundaries=False, ax=ax2)
ax2.set_title("Model with Data (y direction)")
plt.tight_layout()
plt.savefig('model_with_data.png')
plt.close()
Loading