diff --git a/gempy/modules/serialization/save_load.py b/gempy/modules/serialization/save_load.py index c379d3d89..f157ef509 100644 --- a/gempy/modules/serialization/save_load.py +++ b/gempy/modules/serialization/save_load.py @@ -1,7 +1,5 @@ import re -from typing import Literal - import warnings from ...core.data import GeoModel @@ -159,47 +157,3 @@ def _validate_serialization(original_model, model_deserialized): assert deserialized___str__ == original_model___str__ -def verify_model_serialization(model: GeoModel, verify_moment: Literal["before", "after"], file_name: str): - """ - Verifies the serialization and deserialization process of a GeoModel instance - by ensuring the serialized JSON and binary data match during either the - initial or post-process phase, based on the specified verification moment. - - Args: - model: The GeoModel instance to be verified. - verify_moment: A literal value specifying whether to verify the model - before or after the deserialization process. Accepts "before" - or "after" as valid inputs. - file_name: The filename to associate with the verification process for - logging or output purposes. - - Raises: - ValueError: If `verify_moment` is not set to "before" or "after". - """ - model_json = model.model_dump_json(by_alias=True, indent=4) - - # Compress the binary data - zlib = require_zlib() - compressed_binary = zlib.compress(model.structural_frame.input_tables_binary) - - binary_file = _to_binary(model_json, compressed_binary) - - - original_model = model - original_model.meta.creation_date = "" - - from verify_helper import verify_json - if verify_moment == "before": - verify_json( - item=original_model.model_dump_json(by_alias=True, indent=4), - name=file_name - ) - elif verify_moment == "after": - model_deserialized = _deserialize_binary_file(binary_file) - model_deserialized.meta.creation_date = "" - verify_json( - item=model_deserialized.model_dump_json(by_alias=True, indent=4), - name=file_name - ) - else: - raise ValueError("Invalid model parameter") diff --git a/test/test_modules/_geophysics_TO_UPDATE/test_gravity.py b/test/test_modules/_geophysics_TO_UPDATE/test_gravity.py index 335ca7d72..caae2b84d 100644 --- a/test/test_modules/_geophysics_TO_UPDATE/test_gravity.py +++ b/test/test_modules/_geophysics_TO_UPDATE/test_gravity.py @@ -4,7 +4,7 @@ # Importing auxiliary libraries import numpy as np -from gempy.modules.serialization.save_load import verify_model_serialization +from test.verify_helper import verify_model_serialization def test_gravity(): @@ -59,7 +59,7 @@ def test_gravity(): structural_frame=frame, ) - # gp.compute_model(geo_model) + gp.compute_model(geo_model, validate_serialization=False) import gempy_viewer as gpv gpv.plot_2d(geo_model, cell_number=0) diff --git a/test/test_modules/_geophysics_TO_UPDATE/test_gravity.test_gravity.verify/2-layers.approved.txt b/test/test_modules/_geophysics_TO_UPDATE/test_gravity.test_gravity.verify/2-layers.approved.txt index 5fe87bb49..eacd96348 100644 --- a/test/test_modules/_geophysics_TO_UPDATE/test_gravity.test_gravity.verify/2-layers.approved.txt +++ b/test/test_modules/_geophysics_TO_UPDATE/test_gravity.test_gravity.verify/2-layers.approved.txt @@ -26,7 +26,7 @@ }, "_model_transform": null }, - "scalar_field_at_interface": null, + "scalar_field_at_interface": 0.09000000000000002, "_id": -1 }, { @@ -43,7 +43,7 @@ "name_id_map": null, "_model_transform": null }, - "scalar_field_at_interface": null, + "scalar_field_at_interface": -0.2483333333333333, "_id": -1 } ], @@ -154,7 +154,7 @@ "mesh_extraction_masking_options": 3, "mesh_extraction_fancy": true, "evaluation_chunk_size": 500000, - "compute_scalar_gradient": false, + "compute_scalar_gradient": true, "verbose": false }, "debug": true, diff --git a/test/test_modules/test_faults/test_finite_faults.py b/test/test_modules/test_faults/test_finite_faults.py index 33cee774b..ad3a1b4a9 100644 --- a/test/test_modules/test_faults/test_finite_faults.py +++ b/test/test_modules/test_faults/test_finite_faults.py @@ -4,7 +4,7 @@ import gempy as gp import gempy_viewer as gpv from gempy.core.data.enumerators import ExampleModel -from gempy.modules.serialization.save_load import verify_model_serialization +from test.verify_helper import verify_model_serialization from gempy_viewer.optional_dependencies import require_pyvista from test.conftest import TEST_SPEED, TestSpeed diff --git a/test/test_modules/test_grids/test_custom_grid.py b/test/test_modules/test_grids/test_custom_grid.py index ed765c2e6..6606948dd 100644 --- a/test/test_modules/test_grids/test_custom_grid.py +++ b/test/test_modules/test_grids/test_custom_grid.py @@ -1,7 +1,7 @@ import numpy as np import pytest -from gempy.modules.serialization.save_load import verify_model_serialization +from test.verify_helper import verify_model_serialization from test.conftest import TEST_SPEED, TestSpeed import gempy as gp from gempy.core.data.enumerators import ExampleModel diff --git a/test/test_modules/test_grids/test_grids_sections.py b/test/test_modules/test_grids/test_grids_sections.py index e04899842..832cd98bc 100644 --- a/test/test_modules/test_grids/test_grids_sections.py +++ b/test/test_modules/test_grids/test_grids_sections.py @@ -4,7 +4,7 @@ import gempy as gp import gempy_viewer as gpv from gempy.core.data.enumerators import ExampleModel -from gempy.modules.serialization.save_load import verify_model_serialization +from test.verify_helper import verify_model_serialization from test.conftest import TEST_SPEED, TestSpeed @@ -64,7 +64,7 @@ def test_section_grids(): @pytest.mark.skipif(TEST_SPEED.value < TestSpeed.MINUTES.value, reason="Global test speed below this test value.") -def test_topogrphy_II(): +def test_topography_II(): geo_model: gp.data.GeoModel = gp.generate_example_model( example_model=ExampleModel.TWO_AND_A_HALF_D, compute_model=False @@ -77,7 +77,12 @@ def test_topogrphy_II(): show_lith=False, image=True ) - + + verify_model_serialization( + model=geo_model, + verify_moment="after", + file_name=f"verify/{geo_model.meta.name}" + ) gp.compute_model(geo_model) gpv.plot_2d(geo_model, show_boundaries=False, section_names=['topography']) diff --git a/test/test_modules/test_pile/test_stratigraphic_pile.py b/test/test_modules/test_pile/test_stratigraphic_pile.py index 60cc77400..74f56d374 100644 --- a/test/test_modules/test_pile/test_stratigraphic_pile.py +++ b/test/test_modules/test_pile/test_stratigraphic_pile.py @@ -17,7 +17,7 @@ # Check if PATH_TO_SPREMBERG_STRATIGRAPHY is set if not skip the test @pytest.mark.skipif( - os.getenv("PATH_TO_SPREMBERG_STRATIGRAPHY") is None, + os.getenv("PATH_TO_SPREMBERG") is None, reason="PATH_TO_SPREMBERG_STRATIGRAPHY is not set" ) @@ -25,7 +25,7 @@ class TestStratigraphicPile: @pytest.fixture(autouse=True) def borehole_set(self): reader: GenericReaderFilesHelper = GenericReaderFilesHelper( - file_or_buffer=os.getenv("PATH_TO_SPREMBERG_STRATIGRAPHY"), + file_or_buffer=os.getenv("PATH_TO_SPREMBERG") + "Spremberg_stratigraphy.csv", columns_map={ 'hole_id' : 'id', 'depth_from': 'top', @@ -36,7 +36,7 @@ def borehole_set(self): lith: pd.DataFrame = read_lith(reader) reader: GenericReaderFilesHelper = GenericReaderFilesHelper( - file_or_buffer=os.getenv("PATH_TO_SPREMBERG_SURVEY"), + file_or_buffer=os.getenv("PATH_TO_SPREMBERG") + "Spremberg_survey.csv", columns_map={ 'depth' : 'md', 'dip' : 'dip', @@ -49,7 +49,7 @@ def borehole_set(self): survey.update_survey_with_lith(lith) reader_collar: GenericReaderFilesHelper = GenericReaderFilesHelper( - file_or_buffer=os.getenv("PATH_TO_SPREMBERG_COLLAR"), + file_or_buffer=os.getenv("PATH_TO_SPREMBERG") + "Spremberg_collar_updated.csv", header=0, usecols=[0, 1, 2, 4], columns_map={ diff --git a/test/verify_helper.py b/test/verify_helper.py index bc6e3c2be..5a1874516 100644 --- a/test/verify_helper.py +++ b/test/verify_helper.py @@ -1,3 +1,5 @@ +from typing import Literal + import subprocess import numpy as np @@ -8,6 +10,10 @@ from approvaltests.namer import NamerFactory from approvaltests.reporters import GenericDiffReporter, GenericDiffReporterConfig +from gempy.core.data import GeoModel +from gempy.modules.serialization.save_load import _to_binary, _deserialize_binary_file +from gempy.optional_dependencies import require_zlib + class WSLWindowsDiffReporter(GenericDiffReporter): def get_command(self, received, approved): @@ -94,4 +100,49 @@ def get_default_extension(self) -> str: def write(self, received, received_path: str) -> None: with open(received_path, "w", encoding="utf-8") as f: - json.dump(received, f, indent=2, ensure_ascii=False) \ No newline at end of file + json.dump(received, f, indent=2, ensure_ascii=False) + + +def verify_model_serialization(model: GeoModel, verify_moment: Literal["before", "after"], file_name: str): + """ + Verifies the serialization and deserialization process of a GeoModel instance + by ensuring the serialized JSON and binary data match during either the + initial or post-process phase, based on the specified verification moment. + + Args: + model: The GeoModel instance to be verified. + verify_moment: A literal value specifying whether to verify the model + before or after the deserialization process. Accepts "before" + or "after" as valid inputs. + file_name: The filename to associate with the verification process for + logging or output purposes. + + Raises: + ValueError: If `verify_moment` is not set to "before" or "after". + """ + model_json = model.model_dump_json(by_alias=True, indent=4) + + # Compress the binary data + zlib = require_zlib() + compressed_binary = zlib.compress(model.structural_frame.input_tables_binary) + + binary_file = _to_binary(model_json, compressed_binary) + + + original_model = model + original_model.meta.creation_date = "" + + if verify_moment == "before": + verify_json( + item=original_model.model_dump_json(by_alias=True, indent=4), + name=file_name + ) + elif verify_moment == "after": + model_deserialized = _deserialize_binary_file(binary_file) + model_deserialized.meta.creation_date = "" + verify_json( + item=model_deserialized.model_dump_json(by_alias=True, indent=4), + name=file_name + ) + else: + raise ValueError("Invalid model parameter")