Пример #1
0
def test_volume_grid():
    proj = omf.Project(origin=[5., 5, 5])
    vol = omf.VolumeElement(
        name='my elem',
        description='my desc',
        geometry=omf.VolumeGridGeometry(
            tensor_u=[1., 1., 1., 1., 1.],
            tensor_v=[1., 1., 1., 1., 1.],
            tensor_w=[1., 1., 1., 1., 1.],
            origin=[5., 5, 5],
        ),
        color='red',
    )
    proj.elements = [vol]
    proj.validate()
    view = get_view_from_proj(proj)
    view.validate()
    assert len(view.contents) == 1
    vol = find(view, spatial.ElementVolumeGrid)
    defaults = vol.defaults.serialize()
    assert defaults['__class__'] == 'OptionsBlockModel'
    assert defaults['color']['value'] == '#FF0000'
    assert list(vol.origin) == [10., 10, 10]
    assert vol.name == 'my elem'
    assert vol.description == 'my desc'
        def test_from_omf(self):
            omf_element = omf.VolumeElement(
                name="vol_ir",
                geometry=omf.VolumeGridGeometry(
                    axis_u=[1, 1, 0],
                    axis_v=[0, 0, 1],
                    axis_w=[1, -1, 0],
                    tensor_u=np.ones(10).astype(float),
                    tensor_v=np.ones(15).astype(float),
                    tensor_w=np.ones(20).astype(float),
                    origin=[10.0, 10.0, -10],
                ),
                data=[
                    omf.ScalarData(
                        name="Random Data",
                        location="cells",
                        array=np.random.rand(10, 15, 20).flatten(),
                    )
                ],
            )

            # Make a discretize mesh
            mesh, models = discretize.TensorMesh.from_omf(omf_element)

            geom = omf_element.geometry
            # Check geometry
            self.assertEqual(mesh.nC, geom.num_cells)
            self.assertEqual(mesh.nN, geom.num_nodes)
            self.assertTrue(np.allclose(mesh.hx, geom.tensor_u))
            self.assertTrue(np.allclose(mesh.hy, geom.tensor_v))
            self.assertTrue(np.allclose(mesh.hz, geom.tensor_w))
            self.assertTrue(np.allclose(mesh.axis_u, geom.axis_u))
            self.assertTrue(np.allclose(mesh.axis_v, geom.axis_v))
            self.assertTrue(np.allclose(mesh.axis_w, geom.axis_w))
            self.assertTrue(np.allclose(mesh.x0, geom.origin))

            # Check data arrays
            self.assertEqual(len(models.keys()), len(omf_element.data))
            for i in range(len(omf_element.data)):
                name = list(models.keys())[i]
                scalar_data = omf_element.data[i]
                self.assertEqual(name, scalar_data.name)
                arr = ravel_data_array(
                    models[name],
                    len(geom.tensor_u),
                    len(geom.tensor_v),
                    len(geom.tensor_w),
                )
                self.assertTrue(np.allclose(np.array(scalar_data.array), arr))
Пример #3
0
 def to_omf(self):
     self.validate()
     omf_grid_volume = omf.VolumeElement(
         name=self.name or '',
         description=self.description or '',
         geometry=omf.VolumeGridGeometry(
             origin=self.origin,
             tensor_u=self.tensor_u,
             tensor_v=self.tensor_v,
             tensor_w=self.tensor_w,
             axis_u=self.axis_u,
             axis_v=self.axis_v,
             axis_w=self.axis_w,
         ),
         data=[attr.to_omf(cell_location='cells') for attr in self.data],
         color=self.defaults.color.value,
     )
     return omf_grid_volume
Пример #4
0
 def _to_omf(self):
     import omf
     element = omf.VolumeElement(
         name=self.title or '',
         description=self.description or '',
         geometry=self.mesh._to_omf(),
         color=self.opts.color or 'random',
         data=[],
     )
     for data in self.data:
         if data.location == 'CC':
             location = 'cells'
         else:
             location = 'vertices'
         omf_data = data.data._to_omf()
         omf_data.location = location
         element.data.append(omf_data)
     return element
Пример #5
0
    #     omf.ImageTexture(
    #         name='test image',
    #         image='test_image.png',
    #         origin=[2., 2., 2.],
    #         axis_u=[5., 0, 0],
    #         axis_v=[0, 2., 5.]
    #     )
    # ]
)

VOLUME = omf.VolumeElement(
    name='vol',
    geometry=omf.VolumeGridGeometry(tensor_u=np.ones(10).astype(float),
                                    tensor_v=np.ones(15).astype(float),
                                    tensor_w=np.ones(20).astype(float),
                                    origin=[10., 10., -10]),
    data=[
        omf.ScalarData(name='Random Data',
                       location='cells',
                       array=np.random.rand(10, 15, 20).flatten())
    ])

VOLUME_IR = omf.VolumeElement(
    name='vol_ir',
    geometry=omf.VolumeGridGeometry(axis_u=[1, 1, 0],
                                    axis_v=[0, 0, 1],
                                    axis_w=[1, -1, 0],
                                    tensor_u=np.ones(10).astype(float),
                                    tensor_v=np.ones(15).astype(float),
                                    tensor_w=np.ones(20).astype(float),
                                    origin=[10., 10., -10]),
Пример #6
0
    def _tensor_mesh_to_omf(mesh, models=None):
        """
        Constructs an :class:`omf.VolumeElement` object of this tensor mesh and
        the given models as cell data of that grid.

        Parameters
        ----------

        mesh : discretize.TensorMesh
            The tensor mesh to convert to a :class:`omf.VolumeElement`

        models : dict(numpy.ndarray)
            Name('s) and array('s). Match number of cells

        """
        if models is None:
            models = {}
        # Make the geometry
        geometry = omf.VolumeGridGeometry()
        # Set tensors
        tensors = mesh.h
        if len(tensors) < 1:
            raise RuntimeError(
                "Your mesh is empty... fill it out before converting to OMF")
        elif len(tensors) == 1:
            geometry.tensor_u = tensors[0]
            geometry.tensor_v = np.array([
                0.0,
            ])
            geometry.tensor_w = np.array([
                0.0,
            ])
        elif len(tensors) == 2:
            geometry.tensor_u = tensors[0]
            geometry.tensor_v = tensors[1]
            geometry.tensor_w = np.array([
                0.0,
            ])
        elif len(tensors) == 3:
            geometry.tensor_u = tensors[0]
            geometry.tensor_v = tensors[1]
            geometry.tensor_w = tensors[2]
        else:
            raise RuntimeError("This mesh is too high-dimensional for OMF")
        # Set rotation axes
        geometry.axis_u = mesh.axis_u
        geometry.axis_v = mesh.axis_v
        geometry.axis_w = mesh.axis_w
        # Set the origin
        geometry.origin = mesh.origin
        # Make sure the geometry is built correctly
        geometry.validate()
        # Make the volume elemet (the OMF object)
        omfmesh = omf.VolumeElement(geometry=geometry, )
        # Add model data arrays onto the cells of the mesh
        omfmesh.data = []
        for name, arr in models.items():
            data = omf.ScalarData(
                name=name,
                array=ravel_data_array(arr, *mesh.shape_cells),
                location="cells",
            )
            omfmesh.data.append(data)
        # Validate to make sure a proper OMF object is returned to the user
        omfmesh.validate()
        return omfmesh
Пример #7
0
#  Now we need to convert the model to the OMF files specification

# MINUS ONE BECASE WE DEFINE CELL DATA
ncx, ncy, ncz = np.array(grid.dimensions) - 1
sx, sy, sz = grid.spacing

temp_model = omf.VolumeElement(
    name='kriged_temperature_model',
    description='kriged temoerature model built from temperature probe data',
    geometry=omf.VolumeGridGeometry(
        tensor_u=np.full(ncx, sx),
        tensor_v=np.full(ncy, sy),
        tensor_w=np.full(ncz, sz),
        origin=grid.origin,
    ),
    data=[
        omf.ScalarData(name='temperature (C)',
                       array=grid.cell_arrays['Temperature'].reshape(
                           (ncz, ncy, ncx), order='F').ravel(),
                       location='cells'),
        omf.ScalarData(name='Temperature_var',
                       array=grid.cell_arrays['Temperature_var'].reshape(
                           (ncz, ncy, ncx), order='F').ravel(),
                       location='cells'),
    ],
)
temp_model.validate()

###############################################################################
# And one final sanity check

omfvtk.wrap(temp_model).clip_box(gdc19.get_roi_bounds(),
Пример #8
0
    def test_doc_ex(self):
        dirname, _ = os.path.split(os.path.abspath(__file__))
        pngfile = os.path.sep.join(dirname.split(os.path.sep)[:-1] +
                                   ['docs', 'images', 'PointSetGeometry.png'])

        proj = omf.Project(
            name='Test project',
            description='Just some assorted elements'
        )

        pts = omf.PointSetElement(
            name='Random Points',
            description='Just random points',
            geometry=omf.PointSetGeometry(
                vertices=np.random.rand(100, 3)
            ),
            data=[
                omf.ScalarData(
                    name='rand data',
                    array=np.random.rand(100),
                    location='vertices'
                ),
                omf.ScalarData(
                    name='More rand data',
                    array=np.random.rand(100),
                    location='vertices'
                )
            ],
            textures=[
                omf.ImageTexture(
                    name='test image',
                    image=pngfile,
                    origin=[0, 0, 0],
                    axis_u=[1, 0, 0],
                    axis_v=[0, 1, 0]
                ),
                omf.ImageTexture(
                    name='test image',
                    image=pngfile,
                    origin=[0, 0, 0],
                    axis_u=[1, 0, 0],
                    axis_v=[0, 0, 1]
                )
            ],
            color='green'
        )

        lin = omf.LineSetElement(
            name='Random Line',
            geometry=omf.LineSetGeometry(
                vertices=np.random.rand(100, 3),
                segments=np.floor(np.random.rand(50, 2)*100).astype(int)
            ),
            data=[
                omf.ScalarData(
                    name='rand vert data',
                    array=np.random.rand(100),
                    location='vertices'
                ),
                omf.ScalarData(
                    name='rand segment data',
                    array=np.random.rand(50),
                    location='segments'
                )
            ],
            color='#0000FF'
        )

        surf = omf.SurfaceElement(
            name='trisurf',
            geometry=omf.SurfaceGeometry(
                vertices=np.random.rand(100, 3),
                triangles=np.floor(np.random.rand(50, 3)*100).astype(int)
            ),
            data=[
                omf.ScalarData(
                    name='rand vert data',
                    array=np.random.rand(100),
                    location='vertices'
                ),
                omf.ScalarData(
                    name='rand face data',
                    array=np.random.rand(50),
                    location='faces'
                )
            ],
            color=[100, 200, 200]
        )

        grid = omf.SurfaceElement(
            name='gridsurf',
            geometry=omf.SurfaceGridGeometry(
                tensor_u=np.ones(10).astype(float),
                tensor_v=np.ones(15).astype(float),
                origin=[50., 50., 50.],
                axis_u=[1., 0, 0],
                axis_v=[0, 0, 1.],
                offset_w=np.random.rand(11, 16).flatten()
            ),
            data=[
                omf.ScalarData(
                    name='rand vert data',
                    array=np.random.rand(11, 16).flatten(),
                    location='vertices'
                ),
                omf.ScalarData(
                    name='rand face data',
                    array=np.random.rand(10, 15).flatten(order='f'),
                    location='faces'
                )
            ],
            textures=[
                omf.ImageTexture(
                    name='test image',
                    image=pngfile,
                    origin=[2., 2., 2.],
                    axis_u=[5., 0, 0],
                    axis_v=[0, 2., 5.]
                )
            ]
        )

        vol = omf.VolumeElement(
            name='vol',
            geometry=omf.VolumeGridGeometry(
                tensor_u=np.ones(10).astype(float),
                tensor_v=np.ones(15).astype(float),
                tensor_w=np.ones(20).astype(float),
                origin=[10., 10., -10]
            ),
            data=[
                omf.ScalarData(
                    name='Random Data',
                    location='cells',
                    array=np.random.rand(10, 15, 20).flatten()
                )
            ]
        )

        proj.elements = [pts, lin, surf, grid, vol]

        assert proj.validate()

        serial_file = os.path.sep.join([dirname, 'out.omf'])
        omf.OMFWriter(proj, serial_file)
        reader = omf.OMFReader(serial_file)
        new_proj = reader.get_project()

        assert new_proj.validate()
        assert str(new_proj.elements[3].textures[0].uid) == \
            str(proj.elements[3].textures[0].uid)
        del reader
        os.remove(serial_file)