pymt.mappers package¶
Submodules¶
pymt.mappers.celltopoint module¶
pymt.mappers.esmp module¶
- class pymt.mappers.esmp.EsmpCellToCell[source]¶
Bases:
EsmpMapper
pymt.mappers.imapper module¶
- class pymt.mappers.imapper.IGridMapper[source]¶
Bases:
object
Interface for a grid mapper.
- exception pymt.mappers.imapper.IncompatibleGridError(dst, src)[source]¶
Bases:
MapperError
Error to indicate that the source grid cannot be mapped to the destination.
- exception pymt.mappers.imapper.MapperError[source]¶
Bases:
Exception
Base class for error in this package.
- exception pymt.mappers.imapper.NoMapperError(dst, src)[source]¶
Bases:
MapperError
pymt.mappers.mapper module¶
Examples
Point-to-point Mapping¶
>>> import numpy as np
>>> from pymt.grids.map import RectilinearMap as Rectilinear
>>> src = Rectilinear([0, 1, 2], [0, 2])
>>> dst = Rectilinear([.5, 1.5, 2.5], [.25, 1.25])
>>> src.get_x()
array([ 0., 2., 0., 2., 0., 2.])
>>> src.get_y()
array([ 0., 0., 1., 1., 2., 2.])
>>> dst.get_x()
array([ 0.25, 1.25, 0.25, 1.25, 0.25, 1.25])
>>> dst.get_y()
array([ 0.5, 0.5, 1.5, 1.5, 2.5, 2.5])
>>> src_vals = np.arange(src.get_point_count(), dtype=np.float64)
Map the source values on the source points to the destination grid using nearest neighbor.
>>> from pymt.mappers import NearestVal
>>> mapper = NearestVal()
>>> mapper.initialize(dst, src)
>>> mapper.run(src_vals)
array([ 0., 1., 2., 3., 4., 5.])
>>> mappers = find_mapper(dst, src)
>>> len(mappers)
3
>>> mappers[0].name
'PointToPoint'
>>> src_vals[2] = -999
>>> dst_vals = np.full(dst.get_point_count(), -1.)
>>> mapper.run(src_vals, dst_vals=dst_vals)
array([ 0., 1., -1., 3., 4., 5.])
Cell-to-point Mapping¶
The source grid looks like,
(0) ------ (1)
| |
| |
(2) ------ (3)
| |
| |
(4) ------ (5)
| |
| |
(7) ------ (7)
>>> from pymt.mappers import CellToPoint
>>> from pymt.grids.map import UniformRectilinearMap as UniformRectilinear
>>> from pymt.grids.map import UnstructuredPointsMap as UnstructuredPoints
>>> (dst_x, dst_y) = (np.array([.45, 1.25, 3.5]), np.array([.75, 2.25, 3.25]))
>>> src = UniformRectilinear((2,4), (2, 1), (0, 0))
>>> dst = UnstructuredPoints(dst_x, dst_y)
>>> src_vals = np.arange(src.get_cell_count(), dtype=np.float64)
>>> mapper = CellToPoint()
>>> mapper.initialize(dst, src)
>>> mapper.run(src_vals, bad_val=-999)
array([ 0., 2., -999.])
>>> src_vals = np.arange(src.get_cell_count(), dtype=np.float64)
>>> src_vals[0] = -9999
>>> dst_vals = np.zeros(dst.get_point_count()) + 100
>>> _ = mapper.run(src_vals, dst_vals=dst_vals)
>>> dst_vals
array([ 100., 2., -999.])
Point-to-cell Mapping¶
>>> from pymt.mappers.pointtocell import PointToCell
>>> (src_x, src_y) = (np.array ([.45, 1.25, 3.5, .0, 1.]),
... np.array ([.75, 2.25, 3.25, .9, 1.1]))
>>> src = UnstructuredPoints(src_x, src_y)
>>> dst = UniformRectilinear((2,4), (2, 1), (0, 0))
>>> src_vals = np.arange(src.get_point_count(), dtype=np.float64)
>>> mapper = PointToCell()
>>> mapper.initialize(dst, src)
>>> mapper.run(src_vals, bad_val=-999)
array([ 1.5, 4. , 1. ])
>>> mapper.run(src_vals, bad_val=-999, method=np.sum)
array([ 3., 4., 1.])
>>> src_vals[0] = -9999
>>> dst_vals = np.zeros(dst.get_cell_count ()) - 1
>>> _ = mapper.run(src_vals, dst_vals=dst_vals)
>>> dst_vals
array([-1., 4., 1.])
Point on cell edges¶
>>> (src_x, src_y) = (np.array ([0, .5, 1., 2, 3.5]),
... np.array ([1., 1., .0, 3, 3.]))
>>> src = UnstructuredPoints(src_x, src_y)
>>> dst = UniformRectilinear((2,4), (2, 1), (0, 0))
>>> mapper = PointToCell()
>>> mapper.initialize(dst, src)
>>> src_vals = np.arange(src.get_point_count(), dtype=np.float64)
>>> dst_vals = np.zeros(dst.get_cell_count()) - 1
>>> _ = mapper.run(src_vals, dst_vals=dst_vals)
>>> dst_vals
array([ 1. , 0.5, 3. ])
A big mapper¶
>>> (m, n) = (20, 40)
>>> (src_x, src_y) = np.meshgrid(range(m), range(n))
>>> src = UnstructuredPoints(src_y, src_x)
>>> dst = UniformRectilinear((n + 1, m + 1), (1, 1), (-.5, -.5))
>>> mapper = PointToCell()
>>> mapper.initialize(dst, src)
>>> src_vals = np.arange(src.get_point_count(), dtype=np.float64)
>>> dst_vals = np.zeros(dst.get_cell_count(), dtype=np.float64) - 1
>>> _ = mapper.run(src_vals, dst_vals=dst_vals)
>>> from numpy.testing import assert_array_equal
>>> assert_array_equal(dst_vals, src_vals)
pymt.mappers.pointtocell module¶
pymt.mappers.pointtopoint module¶
- class pymt.mappers.pointtopoint.NearestVal[source]¶
Bases:
IGridMapper
Examples
>>> import numpy as np >>> from pymt.grids.map import RectilinearMap >>> from pymt.mappers.pointtopoint import NearestVal >>> src = RectilinearMap([0, 1, 2], [0, 2]) >>> dst = RectilinearMap([.25, 1.25, 2.25], [.25, 1.25]) >>> mapper = NearestVal() >>> mapper.initialize(dst, src) >>> mapper.run(np.arange(6.)) array([ 0., 1., 2., 3., 4., 5.])
- initialize(dest_grid, src_grid, **kwds)[source]¶
Map points on one grid to the nearest points on another.
- Parameters:
dest_grid (grid_like) – Grid onto which points are mapped
src_grid (grid_like) – Grid from which points are taken.
var_names (iterable of tuples (optional)) – Iterable of (dest, src) variable names.
- property name¶
Name of the grid mapper.
- pymt.mappers.pointtopoint.copy_good_values(src, dst, bad_val=-999)[source]¶
Copy only the good values from one array to another.
- Parameters:
src (array_like) – Array of source values.
dst (array_like) – Array into which to copy the good values.
bad_val (float (optional)) – Value below which indicates a bad value.
- Returns:
dst – The destination array.
- Return type:
array_like
Examples
>>> import numpy as np >>> from pymt.mappers.pointtopoint import copy_good_values >>> x = np.arange(6) >>> y = np.zeros((2, 3)) >>> rv = copy_good_values(x, y) >>> rv is y True >>> y array([[ 0., 1., 2.], [ 3., 4., 5.]]) >>> copy_good_values(x, np.zeros(6), bad_val=3.) array([ 0., 0., 0., 0., 4., 5.])
Module contents¶
- class pymt.mappers.CellToPoint[source]¶
Bases:
IGridMapper
- initialize(dest_grid, src_grid, **kwds)[source]¶
Initialize the mapper to map from a source grid to a destination grid.
- property name¶
- exception pymt.mappers.IncompatibleGridError(dst, src)[source]¶
Bases:
MapperError
Error to indicate that the source grid cannot be mapped to the destination.
- class pymt.mappers.NearestVal[source]¶
Bases:
IGridMapper
Examples
>>> import numpy as np >>> from pymt.grids.map import RectilinearMap >>> from pymt.mappers.pointtopoint import NearestVal >>> src = RectilinearMap([0, 1, 2], [0, 2]) >>> dst = RectilinearMap([.25, 1.25, 2.25], [.25, 1.25]) >>> mapper = NearestVal() >>> mapper.initialize(dst, src) >>> mapper.run(np.arange(6.)) array([ 0., 1., 2., 3., 4., 5.])
- initialize(dest_grid, src_grid, **kwds)[source]¶
Map points on one grid to the nearest points on another.
- Parameters:
dest_grid (grid_like) – Grid onto which points are mapped
src_grid (grid_like) – Grid from which points are taken.
var_names (iterable of tuples (optional)) – Iterable of (dest, src) variable names.
- property name¶
Name of the grid mapper.