fatiando.utils)¶Miscellaneous utility functions and classes.
Mathematical functions
Unit conversion
Coordinate system conversions
Others
fromimage: Load a matrix from an image filecontaminate: Contaminate a vector with pseudo-random
Gaussian noisedircos: Get the 3 coordinates of a unit vectorang2vec: Convert intensity, inclination and
declination to a 3-component vectorvecnorm: Get the norm of a vector or list of vectorsvecmean: Take the mean array out of a list of arraysvecstd: Take the standard deviation array out of a
list of arraysSparseList: Store only non-zero elements on an
immutable listsec2hms: Convert seconds to hours, minutes, and
secondssec2year: Convert seconds to Julian yearsyear2sec: Convert Julian years to secondsfatiando.utils.SparseList(size, elements=None)[source]¶Bases: object
Store only non-zero elements on an immutable list.
Can iterate over and access elements just like if it were a list.
Parameters:
Size of the list.
Dictionary used to initialize the list. Keys are the index of the elements and values are their respective values.
Example:
>>> l = SparseList(5)
>>> l[3] = 42.0
>>> print len(l)
5
>>> print l[1], l[3]
0.0 42.0
>>> l[1] += 3.0
>>> for i in l:
...     print i,
0.0 3.0 0.0 42.0 0.0
>>> l2 = SparseList(4, elements={1:3.2, 3:2.8})
>>> for i in l2:
...     print i,
0.0 3.2 0.0 2.8
fatiando.utils.ang2vec(intensity, inc, dec)[source]¶Convert intensity, inclination and declination to a 3-component vector
Note
Coordinate system is assumed to be x->North, y->East, z->Down. Inclination is positive down and declination is measured with respect to x (North).
Parameter:
The intensity (norm) of the vector
The inclination of the vector (in degrees)
The declination of the vector (in degrees)
Returns:
The vector
Examples:
>>> import numpy
>>> print ang2vec(3, 45, 45)
[ 1.5         1.5         2.12132034]
>>> print ang2vec(numpy.arange(4), 45, 45)
[[ 0.          0.          0.        ]
 [ 0.5         0.5         0.70710678]
 [ 1.          1.          1.41421356]
 [ 1.5         1.5         2.12132034]]
fatiando.utils.contaminate(data, stddev, percent=False, return_stddev=False, seed=None)[source]¶Add pseudorandom gaussian noise to an array.
Noise added is normally distributed with zero mean.
Parameters:
Data to contaminate
Standard deviation of the Gaussian noise that will be added to data
If True, will consider stddev as a decimal percentage and the
standard deviation of the Gaussian noise will be this percentage of
the maximum absolute value of data
If True, will return also the standard deviation used to
contaminate data
Seed used to generate the pseudo-random numbers. If None, will use a different seed every time. Use the same seed to generate the same random sequence to contaminate the data.
Returns:
if return_stddev is False:
The contaminated data array
else:
The contaminated data array and the standard deviation used to contaminate it.
Examples:
>>> import numpy as np
>>> data = np.ones(5)
>>> noisy = contaminate(data, 0.1, seed=0)
>>> print noisy
[ 1.03137726  0.89498775  0.95284582  1.07906135  1.04172782]
>>> noisy, std = contaminate(data, 0.05, seed=0, percent=True,
...                          return_stddev=True)
>>> print std
0.05
>>> print noisy
[ 1.01568863  0.94749387  0.97642291  1.03953067  1.02086391]
>>> data = [np.zeros(5), np.ones(3)]
>>> noisy = contaminate(data, [0.1, 0.2], seed=0)
>>> print noisy[0]
[ 0.03137726 -0.10501225 -0.04715418  0.07906135  0.04172782]
>>> print noisy[1]
[ 0.81644754  1.20192079  0.98163167]
fatiando.utils.dircos(inc, dec)[source]¶Returns the 3 coordinates of a unit vector given its inclination and declination.
Note
Coordinate system is assumed to be x->North, y->East, z->Down. Inclination is positive down and declination is measured with respect to x (North).
Parameter:
The inclination of the vector (in degrees)
The declination of the vector (in degrees)
Returns:
The unit vector
fatiando.utils.eotvos2si(value)[source]¶Convert a value from Eotvos to SI units.
Parameters:
The value in Eotvos
Returns:
The value in SI
fatiando.utils.fromimage(fname, ranges=None, shape=None)[source]¶Load an array of normalized gray-scale values from an image file.
The values will be in the range [0, 1]. The shape of the array is the shape of the image (ny, nx), i.e., number of pixels in vertical (height) and horizontal (width) dimensions.
Parameters:
Name of the image file
If not None, will set the gray-scale values to this range.
If not None, will interpolate the array to match this new shape
Returns:
The array of gray-scale values
fatiando.utils.gaussian(x, mean, std)[source]¶Non-normalized Gaussian function
Parameters:
Values at which to calculate the Gaussian function
The mean of the distribution \(\bar{x}\)
The standard deviation of the distribution \(\sigma\)
Returns:
Gaussian function evaluated at x
fatiando.utils.gaussian2d(x, y, sigma_x, sigma_y, x0=0, y0=0, angle=0.0)[source]¶Non-normalized 2D Gaussian function
Parameters:
Coordinates at which to calculate the Gaussian function
Standard deviation in the x and y directions
Coordinates of the center of the distribution
Rotation angle of the gaussian measure from the x axis (north) growing positive to the east (positive y axis)
Returns:
Gaussian function evaluated at x, y
fatiando.utils.mgal2si(value)[source]¶Convert a value from mGal to SI units.
Parameters:
The value in mGal
Returns:
The value in SI
fatiando.utils.normal(x, mean, std)[source]¶Normal distribution.
Parameters:
Value at which to calculate the normal distribution
The mean of the distribution \(\bar{x}\)
The standard deviation of the distribution \(\sigma\)
Returns:
Normal distribution evaluated at x
fatiando.utils.nt2si(value)[source]¶Convert a value from nanoTesla to SI units.
Parameters:
The value in nanoTesla
Returns:
The value in SI
fatiando.utils.safe_diagonal(matrix)[source]¶Get the diagonal of a matrix using the appropriate method.
Parameters:
The matrix...
Returns:
A numpy array with the diagonal of the matrix
fatiando.utils.safe_dot(a, b)[source]¶Make the dot product using the appropriate method.
If a and b are dense, will use numpy.dot. If either is sparse
(from scipy.sparse) will use the multiplication operator (i.e., *).
Parameters:
The vectors/matrices to take the dot product of.
Returns:
The dot product of a and b
fatiando.utils.safe_inverse(matrix)[source]¶Calculate the inverse of a matrix using an apropriate algorithm.
Uses the standard numpy.linalg.inv if matrix is dense.
If it is sparse (from scipy.sparse) then will use
scipy.sparse.linalg.inv.
Parameters:
The matrix
Returns:
The inverse of matrix
fatiando.utils.safe_solve(matrix, vector)[source]¶Solve a linear system using an apropriate algorithm.
Uses the standard numpy.linalg.solve if both matrix and vector
are dense.
If any of the two is sparse (from scipy.sparse) then will use the
Conjugate Gradient Method (scipy.sparse.cgs).
Parameters:
The matrix defining the linear system
The right-side vector of the system
Returns:
The solution of the linear system
fatiando.utils.sec2hms(seconds)[source]¶Convert seconds into a string with hours, minutes and seconds.
Parameters:
Time in seconds
Returns:
String in the format '%dh %dm %2.5fs'
Example:
>>> print sec2hms(62.2)
0h 1m 2.20000s
>>> print sec2hms(3862.12345678)
1h 4m 22.12346s
fatiando.utils.sec2year(seconds)[source]¶Convert seconds into decimal Julian years.
Julian years have 365.25 days.
Parameters:
Time in seconds
Returns:
Time in years
Example:
>>> print sec2year(31557600)
1.0
fatiando.utils.si2eotvos(value)[source]¶Convert a value from SI units to Eotvos.
Parameters:
The value in SI
Returns:
The value in Eotvos
fatiando.utils.si2mgal(value)[source]¶Convert a value from SI units to mGal.
Parameters:
The value in SI
Returns:
The value in mGal
fatiando.utils.si2nt(value)[source]¶Convert a value from SI units to nanoTesla.
Parameters:
The value in SI
Returns:
The value in nanoTesla
fatiando.utils.sph2cart(lon, lat, height)[source]¶Convert spherical coordinates to Cartesian geocentric coordinates.
Parameters:
Spherical coordinates. lon and lat in degrees, height in meters. height is the height above mean Earth radius.
Returns:
Converted Cartesian coordinates
fatiando.utils.vec2ang(vector)[source]¶Convert a 3-component vector to intensity, inclination and declination.
Note
Coordinate system is assumed to be x->North, y->East, z->Down. Inclination is positive down and declination is measured with respect to x (North).
Parameter:
The vector
Returns:
The intensity, inclination and declination (in degrees)
Examples:
>>> s = vec2ang([1.5, 1.5, 2.121320343559643])
>>> print "%.3f %.3f %.3f" % tuple(s)
3.000 45.000 45.000
fatiando.utils.vecmean(arrays)[source]¶Take the mean array out of a list of arrays.
Parameter:
List of arrays
Returns:
The mean of each element in the arrays
Example:
>>> print vecmean([[1, 1, 2], [2, 3, 5]])
[ 1.5  2.   3.5]
fatiando.utils.vecnorm(vectors)[source]¶Get the l2 norm of each vector in a list.
Use this to get, for example, the magnetization intensity from a list of magnetization vectors.
Parameters:
The vector
Returns:
The norms of the vectors
Examples:
>>> v = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
>>> print vecnorm(v)
[ 1.73205081  3.46410162  5.19615242]