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

import tempfile
from pathlib import Path

import numpy as np
import omf

from omfpandas import OMFPandasReader

Instantiate

Create a function to generate a temporary omf file containing two block models.

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
OMF file(/tmp/tmp3ghrjl03.omf)
Element Attributes: {'BlockModel1': ['attr1', 'attr2'], 'BlockModel2': ['attr3', 'attr4']}

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

blockmodel_attributes = {'BlockModel1': None, 'BlockModel2': ['attr4']}

df = reader.read_block_models(blockmodel_attributes)
df.head(20)
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 5.0 5.0 10.0 10.0 10.0 0.796681 0.805432 0.172386
25.0 5.0 5.0 10.0 10.0 10.0 0.250468 0.760161 0.872395
35.0 5.0 5.0 10.0 10.0 10.0 0.624874 0.153900 0.613116
45.0 5.0 5.0 10.0 10.0 10.0 0.571746 0.149249 0.157204
55.0 5.0 5.0 10.0 10.0 10.0 0.832830 0.268174 0.962338
65.0 5.0 5.0 10.0 10.0 10.0 0.906087 0.361075 0.518365
75.0 5.0 5.0 10.0 10.0 10.0 0.012157 0.408456 0.072898
85.0 5.0 5.0 10.0 10.0 10.0 0.674020 0.679697 0.626833
95.0 5.0 5.0 10.0 10.0 10.0 0.051836 0.056680 0.253199
5.0 15.0 5.0 10.0 10.0 10.0 0.548859 0.034673 0.803693
15.0 15.0 5.0 10.0 10.0 10.0 0.287633 0.391911 0.817822
25.0 15.0 5.0 10.0 10.0 10.0 0.306777 0.697164 0.978947
35.0 15.0 5.0 10.0 10.0 10.0 0.352959 0.193435 0.501870
45.0 15.0 5.0 10.0 10.0 10.0 0.621292 0.641504 0.454957
55.0 15.0 5.0 10.0 10.0 10.0 0.334050 0.259828 0.753476
65.0 15.0 5.0 10.0 10.0 10.0 0.732699 0.886086 0.132471
75.0 15.0 5.0 10.0 10.0 10.0 0.404527 0.895690 0.546916
85.0 15.0 5.0 10.0 10.0 10.0 0.068353 0.297287 0.546248
95.0 15.0 5.0 10.0 10.0 10.0 0.783760 0.229994 0.089473


Check if the DataFrame contains the expected columns

expected_columns = ['attr1', 'attr2', 'attr4']
assert all(col in df.columns for col in expected_columns)

Clean up temporary file

temp_omf_path.unlink()

Total running time of the script: (0 minutes 0.044 seconds)

Gallery generated by Sphinx-Gallery