Skip to content

Commit d34e6e2

Browse files
authored
[BUG] Fix typo in test_topography_II and update Spremberg file paths (#1033)
# Description This PR fixes a typo in the test function name `test_topography_II` (previously misspelled as `test_topogrphy_II`) and adds model serialization verification to this test. It also updates the environment variable paths in the `TestStratigraphicPile` class to use a base path with `PATH_TO_SPREMBERG` instead of separate environment variables for each file. # Checklist - [x] My code uses type hinting for function and method arguments and return values. - [x] I have created tests which cover my code. - [x] The test code either 1. demonstrates at least one valuable use case (e.g. integration tests) or 2. verifies that outputs are as expected for given inputs (e.g. unit tests). - [x] New tests pass locally with my changes.
2 parents 95a580a + 333fc88 commit d34e6e2

File tree

8 files changed

+71
-61
lines changed

8 files changed

+71
-61
lines changed

gempy/modules/serialization/save_load.py

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import re
22

3-
from typing import Literal
4-
53
import warnings
64

75
from ...core.data import GeoModel
@@ -159,47 +157,3 @@ def _validate_serialization(original_model, model_deserialized):
159157
assert deserialized___str__ == original_model___str__
160158

161159

162-
def verify_model_serialization(model: GeoModel, verify_moment: Literal["before", "after"], file_name: str):
163-
"""
164-
Verifies the serialization and deserialization process of a GeoModel instance
165-
by ensuring the serialized JSON and binary data match during either the
166-
initial or post-process phase, based on the specified verification moment.
167-
168-
Args:
169-
model: The GeoModel instance to be verified.
170-
verify_moment: A literal value specifying whether to verify the model
171-
before or after the deserialization process. Accepts "before"
172-
or "after" as valid inputs.
173-
file_name: The filename to associate with the verification process for
174-
logging or output purposes.
175-
176-
Raises:
177-
ValueError: If `verify_moment` is not set to "before" or "after".
178-
"""
179-
model_json = model.model_dump_json(by_alias=True, indent=4)
180-
181-
# Compress the binary data
182-
zlib = require_zlib()
183-
compressed_binary = zlib.compress(model.structural_frame.input_tables_binary)
184-
185-
binary_file = _to_binary(model_json, compressed_binary)
186-
187-
188-
original_model = model
189-
original_model.meta.creation_date = "<DATE_IGNORED>"
190-
191-
from verify_helper import verify_json
192-
if verify_moment == "before":
193-
verify_json(
194-
item=original_model.model_dump_json(by_alias=True, indent=4),
195-
name=file_name
196-
)
197-
elif verify_moment == "after":
198-
model_deserialized = _deserialize_binary_file(binary_file)
199-
model_deserialized.meta.creation_date = "<DATE_IGNORED>"
200-
verify_json(
201-
item=model_deserialized.model_dump_json(by_alias=True, indent=4),
202-
name=file_name
203-
)
204-
else:
205-
raise ValueError("Invalid model parameter")

test/test_modules/_geophysics_TO_UPDATE/test_gravity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Importing auxiliary libraries
55
import numpy as np
66

7-
from gempy.modules.serialization.save_load import verify_model_serialization
7+
from test.verify_helper import verify_model_serialization
88

99

1010
def test_gravity():
@@ -59,7 +59,7 @@ def test_gravity():
5959
structural_frame=frame,
6060
)
6161

62-
# gp.compute_model(geo_model)
62+
gp.compute_model(geo_model, validate_serialization=False)
6363

6464
import gempy_viewer as gpv
6565
gpv.plot_2d(geo_model, cell_number=0)

test/test_modules/_geophysics_TO_UPDATE/test_gravity.test_gravity.verify/2-layers.approved.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"_model_transform": null
2828
},
29-
"scalar_field_at_interface": null,
29+
"scalar_field_at_interface": 0.09000000000000002,
3030
"_id": -1
3131
},
3232
{
@@ -43,7 +43,7 @@
4343
"name_id_map": null,
4444
"_model_transform": null
4545
},
46-
"scalar_field_at_interface": null,
46+
"scalar_field_at_interface": -0.2483333333333333,
4747
"_id": -1
4848
}
4949
],
@@ -154,7 +154,7 @@
154154
"mesh_extraction_masking_options": 3,
155155
"mesh_extraction_fancy": true,
156156
"evaluation_chunk_size": 500000,
157-
"compute_scalar_gradient": false,
157+
"compute_scalar_gradient": true,
158158
"verbose": false
159159
},
160160
"debug": true,

test/test_modules/test_faults/test_finite_faults.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import gempy as gp
55
import gempy_viewer as gpv
66
from gempy.core.data.enumerators import ExampleModel
7-
from gempy.modules.serialization.save_load import verify_model_serialization
7+
from test.verify_helper import verify_model_serialization
88
from gempy_viewer.optional_dependencies import require_pyvista
99
from test.conftest import TEST_SPEED, TestSpeed
1010

test/test_modules/test_grids/test_custom_grid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy as np
22
import pytest
33

4-
from gempy.modules.serialization.save_load import verify_model_serialization
4+
from test.verify_helper import verify_model_serialization
55
from test.conftest import TEST_SPEED, TestSpeed
66
import gempy as gp
77
from gempy.core.data.enumerators import ExampleModel

test/test_modules/test_grids/test_grids_sections.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import gempy as gp
55
import gempy_viewer as gpv
66
from gempy.core.data.enumerators import ExampleModel
7-
from gempy.modules.serialization.save_load import verify_model_serialization
7+
from test.verify_helper import verify_model_serialization
88

99
from test.conftest import TEST_SPEED, TestSpeed
1010

@@ -64,7 +64,7 @@ def test_section_grids():
6464

6565

6666
@pytest.mark.skipif(TEST_SPEED.value < TestSpeed.MINUTES.value, reason="Global test speed below this test value.")
67-
def test_topogrphy_II():
67+
def test_topography_II():
6868
geo_model: gp.data.GeoModel = gp.generate_example_model(
6969
example_model=ExampleModel.TWO_AND_A_HALF_D,
7070
compute_model=False
@@ -77,7 +77,12 @@ def test_topogrphy_II():
7777
show_lith=False,
7878
image=True
7979
)
80-
80+
81+
verify_model_serialization(
82+
model=geo_model,
83+
verify_moment="after",
84+
file_name=f"verify/{geo_model.meta.name}"
85+
)
8186
gp.compute_model(geo_model)
8287

8388
gpv.plot_2d(geo_model, show_boundaries=False, section_names=['topography'])

test/test_modules/test_pile/test_stratigraphic_pile.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717

1818
# Check if PATH_TO_SPREMBERG_STRATIGRAPHY is set if not skip the test
1919
@pytest.mark.skipif(
20-
os.getenv("PATH_TO_SPREMBERG_STRATIGRAPHY") is None,
20+
os.getenv("PATH_TO_SPREMBERG") is None,
2121
reason="PATH_TO_SPREMBERG_STRATIGRAPHY is not set"
2222
)
2323

2424
class TestStratigraphicPile:
2525
@pytest.fixture(autouse=True)
2626
def borehole_set(self):
2727
reader: GenericReaderFilesHelper = GenericReaderFilesHelper(
28-
file_or_buffer=os.getenv("PATH_TO_SPREMBERG_STRATIGRAPHY"),
28+
file_or_buffer=os.getenv("PATH_TO_SPREMBERG") + "Spremberg_stratigraphy.csv",
2929
columns_map={
3030
'hole_id' : 'id',
3131
'depth_from': 'top',
@@ -36,7 +36,7 @@ def borehole_set(self):
3636

3737
lith: pd.DataFrame = read_lith(reader)
3838
reader: GenericReaderFilesHelper = GenericReaderFilesHelper(
39-
file_or_buffer=os.getenv("PATH_TO_SPREMBERG_SURVEY"),
39+
file_or_buffer=os.getenv("PATH_TO_SPREMBERG") + "Spremberg_survey.csv",
4040
columns_map={
4141
'depth' : 'md',
4242
'dip' : 'dip',
@@ -49,7 +49,7 @@ def borehole_set(self):
4949
survey.update_survey_with_lith(lith)
5050

5151
reader_collar: GenericReaderFilesHelper = GenericReaderFilesHelper(
52-
file_or_buffer=os.getenv("PATH_TO_SPREMBERG_COLLAR"),
52+
file_or_buffer=os.getenv("PATH_TO_SPREMBERG") + "Spremberg_collar_updated.csv",
5353
header=0,
5454
usecols=[0, 1, 2, 4],
5555
columns_map={

test/verify_helper.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Literal
2+
13
import subprocess
24

35
import numpy as np
@@ -8,6 +10,10 @@
810
from approvaltests.namer import NamerFactory
911
from approvaltests.reporters import GenericDiffReporter, GenericDiffReporterConfig
1012

13+
from gempy.core.data import GeoModel
14+
from gempy.modules.serialization.save_load import _to_binary, _deserialize_binary_file
15+
from gempy.optional_dependencies import require_zlib
16+
1117

1218
class WSLWindowsDiffReporter(GenericDiffReporter):
1319
def get_command(self, received, approved):
@@ -94,4 +100,49 @@ def get_default_extension(self) -> str:
94100

95101
def write(self, received, received_path: str) -> None:
96102
with open(received_path, "w", encoding="utf-8") as f:
97-
json.dump(received, f, indent=2, ensure_ascii=False)
103+
json.dump(received, f, indent=2, ensure_ascii=False)
104+
105+
106+
def verify_model_serialization(model: GeoModel, verify_moment: Literal["before", "after"], file_name: str):
107+
"""
108+
Verifies the serialization and deserialization process of a GeoModel instance
109+
by ensuring the serialized JSON and binary data match during either the
110+
initial or post-process phase, based on the specified verification moment.
111+
112+
Args:
113+
model: The GeoModel instance to be verified.
114+
verify_moment: A literal value specifying whether to verify the model
115+
before or after the deserialization process. Accepts "before"
116+
or "after" as valid inputs.
117+
file_name: The filename to associate with the verification process for
118+
logging or output purposes.
119+
120+
Raises:
121+
ValueError: If `verify_moment` is not set to "before" or "after".
122+
"""
123+
model_json = model.model_dump_json(by_alias=True, indent=4)
124+
125+
# Compress the binary data
126+
zlib = require_zlib()
127+
compressed_binary = zlib.compress(model.structural_frame.input_tables_binary)
128+
129+
binary_file = _to_binary(model_json, compressed_binary)
130+
131+
132+
original_model = model
133+
original_model.meta.creation_date = "<DATE_IGNORED>"
134+
135+
if verify_moment == "before":
136+
verify_json(
137+
item=original_model.model_dump_json(by_alias=True, indent=4),
138+
name=file_name
139+
)
140+
elif verify_moment == "after":
141+
model_deserialized = _deserialize_binary_file(binary_file)
142+
model_deserialized.meta.creation_date = "<DATE_IGNORED>"
143+
verify_json(
144+
item=model_deserialized.model_dump_json(by_alias=True, indent=4),
145+
name=file_name
146+
)
147+
else:
148+
raise ValueError("Invalid model parameter")

0 commit comments

Comments
 (0)