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 logging
import shutil
import tempfile
import webbrowser
from pathlib import Path

import numpy as np
import omf
import pandas as pd

from omfpandas import OMFDataConverter, OMFPandasReader, OMFPandasWriter

Instantiate

Create the object OMFPandas with the path to the OMF file.

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(levelname)s %(module)s - %(funcName)s: %(message)s',
                    datefmt='%Y-%m-%dT%H:%M:%S%z')

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

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 = omf.TensorGridBlockModel(
        name='BlockModel1',
        tensor_u=np.full(10, 10, dtype='float32'),
        tensor_v=np.full(10, 10, dtype='float32'),
        tensor_w=np.full(10, 10, dtype='float32'),
        attributes=[
            omf.NumericAttribute(name='attr1', location="cells", array=np.random.rand(10 * 10 * 10).ravel()),
            omf.NumericAttribute(name='attr2', location="cells", array=np.random.rand(10 * 10 * 10).ravel()),
        ]
    )

    block_model_2 = omf.TensorGridBlockModel(
        name='BlockModel2',
        tensor_u=np.full(10, 10, dtype='float32'),
        tensor_v=np.full(10, 10, dtype='float32'),
        tensor_w=np.full(10, 10, dtype='float32'),
        attributes=[
            omf.NumericAttribute(name='attr3', location="cells", array=np.random.rand(10 * 10 * 10).ravel()),
            omf.NumericAttribute(name='attr4', location="cells", array=np.random.rand(10 * 10 * 10).ravel()),
        ]
    )

    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)

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

temp_omf_path = create_temp_omf_file()
reader = OMFPandasReader(temp_omf_path)

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.042 seconds)

Gallery generated by Sphinx-Gallery