.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/06_reading_multiple_blockmodels.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_06_reading_multiple_blockmodels.py: Reading Multiple Block Models ============================= OMF allows storage of multiple block models in a single file. This example demonstrates how to read multiple block models to return a single pandas dataframe .. GENERATED FROM PYTHON SOURCE LINES 8-18 .. code-block:: Python import tempfile from pathlib import Path import numpy as np import omf from omfpandas import OMFPandasReader .. GENERATED FROM PYTHON SOURCE LINES 19-22 Instantiate ----------- Create a function to generate a temporary omf file containing two block models. .. GENERATED FROM PYTHON SOURCE LINES 22-60 .. code-block:: Python def create_demo_tensor_model(shape: tuple[int, int, int], cell_size: tuple[float, float, float], name: str, attr_names: list[str]) -> omf.TensorGridBlockModel: """Create a tensor grid block model with random attributes""" return omf.TensorGridBlockModel( name=name, tensor_u=np.full(shape[0], cell_size[0], dtype='float32'), tensor_v=np.full(shape[1], cell_size[1], dtype='float32'), tensor_w=np.full(shape[2], cell_size[2], dtype='float32'), attributes=[omf.NumericAttribute(name=attr_name, location="cells", array=np.random.rand(shape[0] * shape[1] * shape[2]).ravel()) for attr_name in attr_names]) def create_temp_omf_file() -> Path: """Creates an omf file directly using omf and numpy""" project = omf.Project(name='Test Project') # Create block models block_model_1 = create_demo_tensor_model((10, 10, 10), (10, 10, 10), 'BlockModel1', ['attr1', 'attr2']) block_model_2 = create_demo_tensor_model((10, 10, 10), (10, 10, 10), 'BlockModel2', ['attr3', 'attr4']) project.elements = [block_model_1, block_model_2] # Save to a temporary file temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.omf') omf.save(project, temp_file.name, mode='w') return Path(temp_file.name) temp_omf_path = create_temp_omf_file() reader = OMFPandasReader(temp_omf_path) reader .. rst-class:: sphx-glr-script-out .. code-block:: none OMF file(/tmp/tmp2q8nao1s.omf) Elements: {'BlockModel1': 'TensorGridBlockModel', 'BlockModel2': 'TensorGridBlockModel'} .. GENERATED FROM PYTHON SOURCE LINES 61-67 Read from block models ---------------------- We'll demonstrate how to selectively merge attributes from each of the two block models to create a single dataframe. The `read_block_models` method accepts a dictionary where the keys are the block model names and the values are lists of attribute names to include in the dataframe. If None is provided for the attribute list, all attributes .. GENERATED FROM PYTHON SOURCE LINES 67-73 .. code-block:: Python blockmodel_attributes = {'BlockModel1': None, 'BlockModel2': ['attr4']} df = reader.read_block_models(blockmodel_attributes) df.head(20) .. raw:: html
attr1 attr2 attr4
x y z dx dy dz
5.0 5.0 5.0 10.0 10.0 10.0 0.672703 0.571996 0.648257
15.0 10.0 10.0 10.0 0.796681 0.805432 0.172386
25.0 10.0 10.0 10.0 0.250468 0.760161 0.872395
35.0 10.0 10.0 10.0 0.624874 0.153900 0.613116
45.0 10.0 10.0 10.0 0.571746 0.149249 0.157204
55.0 10.0 10.0 10.0 0.832830 0.268174 0.962338
65.0 10.0 10.0 10.0 0.906087 0.361075 0.518365
75.0 10.0 10.0 10.0 0.012157 0.408456 0.072898
85.0 10.0 10.0 10.0 0.674020 0.679697 0.626833
95.0 10.0 10.0 10.0 0.051836 0.056680 0.253199
15.0 5.0 10.0 10.0 10.0 0.548859 0.034673 0.803693
15.0 10.0 10.0 10.0 0.287633 0.391911 0.817822
25.0 10.0 10.0 10.0 0.306777 0.697164 0.978947
35.0 10.0 10.0 10.0 0.352959 0.193435 0.501870
45.0 10.0 10.0 10.0 0.621292 0.641504 0.454957
55.0 10.0 10.0 10.0 0.334050 0.259828 0.753476
65.0 10.0 10.0 10.0 0.732699 0.886086 0.132471
75.0 10.0 10.0 10.0 0.404527 0.895690 0.546916
85.0 10.0 10.0 10.0 0.068353 0.297287 0.546248
95.0 10.0 10.0 10.0 0.783760 0.229994 0.089473


.. GENERATED FROM PYTHON SOURCE LINES 74-75 Check if the DataFrame contains the expected columns .. GENERATED FROM PYTHON SOURCE LINES 75-79 .. code-block:: Python expected_columns = ['attr1', 'attr2', 'attr4'] assert all(col in df.columns for col in expected_columns) .. GENERATED FROM PYTHON SOURCE LINES 80-81 Clean up temporary file .. GENERATED FROM PYTHON SOURCE LINES 81-83 .. code-block:: Python temp_omf_path.unlink() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.050 seconds) .. _sphx_glr_download_auto_examples_06_reading_multiple_blockmodels.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 06_reading_multiple_blockmodels.ipynb <06_reading_multiple_blockmodels.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 06_reading_multiple_blockmodels.py <06_reading_multiple_blockmodels.py>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_