safe.gis.numerics module

Numerical tools

safe.gis.numerics.axes_to_points(x, y)[source]

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

Parameters:
  • x (numpy.ndarray) – x coordinates (array)
  • y (numpy.ndarray) – 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.gis.numerics.ensure_numeric(A, typecode=None)[source]

Ensure that sequence is a numeric array.

Parameters:
  • A

    Is one of the following:

    • 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
    • Scalar. Return 0-dimensional array containing that value. Note
      that a 0-dim array DOES NOT HAVE A LENGTH UNDER numpy.
    • 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.
Raises:

Exception

Returns:

An array of A if it is found to be of a numeric type

Return type:

numpy.ndarray

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.gis.numerics.erf(z)[source]

Approximation to ERF

Parameters:z (numpy.ndarray, float) – input array or scalar to perform erf on
Returns:the approximate error
Return type:numpy.ndarray, float
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.gis.numerics.geotransform_to_axes(G, nx, ny)[source]

Convert geotransform to coordinate axes

Parameters:
  • G (tuple) – GDAL geotransform (6-tuple). (top left x, w-e pixel resolution, rotation, top left y, rotation, n-s pixel resolution).
  • nx (int) – Number of cells in the w-e direction
  • ny – Number of cells in the n-s direction
Returns:

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.gis.numerics.grid_to_points(A, x, y)[source]

Convert grid data to point data

Parameters:
  • A (numpy.ndarray) – Array of pixel values
  • x (numpy.ndarray) – Longitudes corresponding to columns in A (west->east)
  • y (numpy.ndarray) – Latitudes corresponding to rows in A (south->north)
Returns:
  • P: Nx2 array of point coordinates
  • V: N array of point values
safe.gis.numerics.log_normal_cdf(x, median=1, sigma=1)[source]

Cumulative Log Normal Distribution Function

Parameters:
  • x (numpy.ndarray, float) – scalar or array of real numbers
  • median (float) – Median (exp(mean of log(x)). Default 1
  • sigma (float) – Log normal standard deviation. Default 1
Returns:

An approximation of the cdf of the normal

Return type:

numpy.ndarray

Note

CDF of the normal distribution is defined as frac12 [1 + erf(frac{x - mu}{sigma sqrt{2}})], x in R

Source: http://en.wikipedia.org/wiki/Normal_distribution

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

Does element comparison within a tolerance, excludes overlapped NaN.

Parameters:
  • x (numpy.ndarray, float) – scalar or numpy array
  • y (numpy.ndarray, float) – scalar or numpy array
  • rtol (float) – The relative tolerance parameter
  • atol (float) – The absolute tolerance parameter
Raises:
Returns:

The result of the allclose on non NaN elements

Return type:

bool

Note:
Returns True if all non-nan elements pass.
safe.gis.numerics.normal_cdf(x, mu=0, sigma=1)[source]

Cumulative Normal Distribution Function

Parameters:
  • x (numpy.ndarray, float) – scalar or array of real numbers
  • mu (float, numpy.ndarray) – Mean value. Default 0
  • sigma (float) – Standard deviation. Default 1
Returns:

An approximation of the cdf of the normal

Return type:

numpy.ndarray

Note:

CDF of the normal distribution is defined as frac12 [1 + erf(frac{x - mu}{sigma sqrt{2}})], x in R

Source: http://en.wikipedia.org/wiki/Normal_distribution