Skip to content

Commit

Permalink
Add JSON rendering method for notebooks (#134)
Browse files Browse the repository at this point in the history
* Add JSON rendering method for notebooks

A new method `_repr_json_` was added to the base model in the SEG-Y schema. This method allows the model to be rendered as a JSON dictionary when using notebooks, improving data visualization during development.

* Add test for JSON representation validation

A test has been added in 'test_segy_file.py' to check the validity of the JSON-able dictionary representation. The test asserts the equivalence of the output from '_repr_json_' method and 'model_dump' method in 'json' mode.

* remove manual JSON conversion since its now in models.

---------

Co-authored-by: Altay Sansal <altay.sansal@tgs.com>
  • Loading branch information
tasansal and Altay Sansal authored Jun 20, 2024
1 parent d2978fd commit d716a62
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
14 changes: 6 additions & 8 deletions docs/tutorials/quickstart.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"from IPython.display import JSON\n",
"from numpy.random import default_rng\n",
"\n",
"from segy import SegyFile\n",
Expand Down Expand Up @@ -70,7 +69,7 @@
"S3 object store with the `segy` library.\n",
"\n",
"The `SegyFile` class uses information from the binary file header to construct a SEG-Y\n",
"descriptor, allowing it to read the file. The SEG-Y Revision is inferred from the binary\n",
"spec, allowing it to read the file. The SEG-Y Revision is inferred from the binary\n",
"header by default, but can be manually set by providing a custom spec or adjusting settings.\n",
"\n",
"Since this is a public bucket and an object, we need to tell `S3` that we want anonymous\n",
Expand Down Expand Up @@ -107,7 +106,7 @@
"3. We can check that some headers can be defined in the wrong byte locations.\n",
"4. There are too many headers to deal with in the default schema.\n",
"\n",
"Note that we can build this JSON independently, load it into the descriptor\n",
"Note that we can build this JSON independently, load it into the spec\n",
"and open any SEG-Y with a schema."
]
},
Expand All @@ -118,7 +117,7 @@
"metadata": {},
"outputs": [],
"source": [
"JSON(sgy.spec.model_dump(mode=\"json\"))"
"sgy.spec"
]
},
{
Expand Down Expand Up @@ -320,7 +319,7 @@
"id": "de33a63c81b2072c",
"metadata": {},
"source": [
"Now let's look at the JSON for the desciptor again. It is a lot more compact."
"Now let's look at the spec again. It is a lot more compact."
]
},
{
Expand All @@ -330,7 +329,7 @@
"metadata": {},
"outputs": [],
"source": [
"JSON(sgy.spec.model_dump(mode=\"json\"))"
"sgy.spec"
]
},
{
Expand Down Expand Up @@ -469,8 +468,7 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
Expand Down
4 changes: 4 additions & 0 deletions src/segy/schema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def model_dump_json(self, *args: Any, **kwargs: Any) -> str: # noqa: ANN401
"""Dump the model into a JSON string by alias."""
return super().model_dump_json(*args, **kwargs, by_alias=True)

def _repr_json_(self) -> dict[str, Any]:
"""Return JSON-able dictionary form of model to render in notebooks."""
return self.model_dump(mode="json")


class BaseDataType(CamelCaseModel):
"""A base model for all SEG-Y Ninja types."""
Expand Down
3 changes: 3 additions & 0 deletions tests/test_segy_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ def test_infer_spec(
assert segy_file.num_ext_text == 0
assert_array_equal(segy_file.sample_labels, EXPECTED_SAMPLE_LABELS)

# Check if JSON-able dict representation is valid
assert segy_file.spec._repr_json_() == segy_file.spec.model_dump(mode="json")

def test_text_file_header(
self, mock_filesystem: MemoryFileSystem, default_text: str
) -> None:
Expand Down

0 comments on commit d716a62

Please sign in to comment.