Module: safe.common.numerics

Numerical tools

safe.common.numerics.axes2points(x, y)[source]

Generate all combinations of grid point coordinates from x and y axes

Args:
  • x: x coordinates (array)
  • y: y coordinates (array)
Returns:
  • P: Nx2 array consisting of coordinates for all

    grid points defined by x and y axes. The x coordinate will vary the fastest to match the way 2D numpy arrays are laid out by default (‘C’ order). That way, the x and y coordinates will match a corresponding 2D array A when flattened (A.flat[:] or A.reshape(-1))

Note:

Example

x = [1, 2, 3] y = [10, 20]

P = [[1, 10],
[2, 10], [3, 10], [1, 20], [2, 20], [3, 20]]
safe.common.numerics.ensure_numeric(A, typecode=None)[source]

Ensure that sequence is a numeric array.

Args:
  • A: Sequence. If A is already a numeric array it will be returned

    unaltered If not, an attempt is made to convert it to a numeric array

  • A: Scalar. Return 0-dimensional array containing that value. Note

    that a 0-dim array DOES NOT HAVE A LENGTH UNDER numpy.

  • A: String. Array of ASCII values (numpy can’t handle this)

  • typecode: numeric type. If specified, use this in the conversion.

    If not, let numeric package decide. typecode will always be one of num.float, num.int, etc.

Note :

that numpy.array(A, dtype) will sometimes copy. Use ‘copy=False’ to copy only when required.

This function is necessary as array(A) can cause memory overflow.

safe.common.numerics.erf(z)[source]

Approximation to ERF

Note:

from: http://www.cs.princeton.edu/introcs/21function/ErrorFunction.java.html Implements the Gauss error function. erf(z) = 2 / sqrt(pi) * integral(exp(-t*t), t = 0..z)

Fractional error in math formula less than 1.2 * 10 ^ -7. although subject to catastrophic cancellation when z in very close to 0 from Chebyshev fitting formula for erf(z) from Numerical Recipes, 6.2

Source: http://stackoverflow.com/questions/457408/ is-there-an-easily-available-implementation-of-erf-for-python

safe.common.numerics.geotransform2axes(G, nx, ny)[source]

Convert geotransform to coordinate axes

Args:
  • G: GDAL geotransform (6-tuple).
    (top left x, w-e pixel resolution, rotation,

    top left y, rotation, n-s pixel resolution).

  • nx: Number of cells in the w-e direction

  • ny: Number of cells in the n-s direction

Returns:
  • Return two vectors (longitudes and latitudes) representing the grid defined by the geotransform.

    The values are offset by half a pixel size to correspond to pixel registration.

    I.e. If the grid origin (top left corner) is (105, 10) and the resolution is 1 degrees in each direction, then the vectors will take the form

    longitudes = [100.5, 101.5, ..., 109.5] latitudes = [0.5, 1.5, ..., 9.5]

safe.common.numerics.grid2points(A, x, y)[source]

Convert grid data to point data

Args:
  • A: Array of pixel values
  • x: Longitudes corresponding to columns in A (left->right)
  • y: Latitudes corresponding to rows in A (top->bottom)
Returns:
  • P: Nx2 array of point coordinates
  • V: N array of point values
safe.common.numerics.lognormal_cdf(x, median=1, sigma=1)[source]

Cumulative Log Normal Distribution Function

Args:
  • x: scalar or array of real numbers
  • mu: Median (exp(mean of log(x)). Default 1
  • sigma: Log normal standard deviation. Default 1
Returns:
  • An approximation of the cdf of the normal
Note:
CDF of the normal distribution is defined as

rac12 [1 + erf( rac{x - mu}{sigma sqrt{2}})], x in R

safe.common.numerics.nanallclose(x, y, rtol=1e-05, atol=1e-08)[source]

Numpy allclose function which allows NaN

Args:
  • x, y: Either scalars or numpy arrays
Returns:
  • True or False
Note:
Returns True if all non-nan elements pass.
safe.common.numerics.normal_cdf(x, mu=0, sigma=1)[source]

Cumulative Normal Distribution Function

Args:
  • x: scalar or array of real numbers
  • mu: Mean value. Default 0
  • sigma: Standard deviation. Default 1
Returns:
  • An approximation of the cdf of the normal
Note:
CDF of the normal distribution is defined as

rac12 [1 + erf( rac{x - mu}{sigma sqrt{2}})], x in R

This module forms part of the InaSAFE tool.

Previous topic

Module: safe.common.test_tables

Next topic

Module: safe.common.tables

This Page