eofs.standard

EOF analysis for data in numpy arrays.

class eofs.standard.Eof(dataset, weights=None, center=True, ddof=1)[source]

EOF analysis (numpy interface)

Create an Eof object.

The EOF solution is computed at initialization time. Method calls are used to retrieve computed quantities.

Arguments:

dataset

A numpy.ndarray, numpy.ma.MaskedArray or dask.array.Array with two or more dimensions containing the data to be analysed. The first dimension is assumed to represent time. Missing values are permitted, either in the form of a masked array, or numpy.nan values. Missing values must be constant with time (e.g., values of an oceanographic field over land).

Optional arguments:

weights

An array of weights whose shape is compatible with those of the input array dataset. The weights can have the same shape as dataset or a shape compatible with an array broadcast (i.e., the shape of the weights can can match the rightmost parts of the shape of the input array dataset). If the input array dataset does not require weighting then the value None may be used. Defaults to None (no weighting).

center

If True, the mean along the first axis of dataset (the time-mean) will be removed prior to analysis. If False, the mean along the first axis will not be removed. Defaults to True (mean is removed).

The covariance interpretation relies on the input data being anomaly data with a time-mean of 0. Therefore this option should usually be set to True. Setting this option to True has the useful side effect of propagating missing values along the time dimension, ensuring that a solution can be found even if missing values occur in different locations at different times.

ddof

‘Delta degrees of freedom’. The divisor used to normalize the covariance matrix is N - ddof where N is the number of samples. Defaults to 1.

Returns:

solver

An Eof instance.

Examples:

EOF analysis with no weighting:

from eofs.standard import Eof
solver = Eof(data)

EOF analysis of a data array with spatial dimensions that represent latitude and longitude with weighting. In this example the data array is dimensioned (ntime, nlat, nlon), and in order for the latitude weights to be broadcastable to this shape, an extra length-1 dimension is added to the end:

from eofs.standard import Eof
import numpy as np
latitude = np.linspace(-90, 90, 73)
weights_array = np.cos(np.deg2rad(latitude))[:, np.newaxis]
solver = Eof(data, weights=weight_array)
pcs(pcscaling=0, npcs=None)[source]

Principal component time series (PCs).

Optional arguments:

pcscaling

Set the scaling of the retrieved PCs. The following values are accepted:

  • 0 : Un-scaled PCs (default).

  • 1 : PCs are scaled to unit variance (divided by the square-root of their eigenvalue).

  • 2 : PCs are multiplied by the square-root of their eigenvalue.

npcs

Number of PCs to retrieve. Defaults to all the PCs. If the number of PCs requested is more than the number that are available, then all available PCs will be returned.

Returns:

pcs

An array where the columns are the ordered PCs.

Examples:

All un-scaled PCs:

pcs = solver.pcs()

First 3 PCs scaled to unit variance:

pcs = solver.pcs(npcs=3, pcscaling=1)
eofs(eofscaling=0, neofs=None)[source]

Empirical orthogonal functions (EOFs).

Optional arguments:

eofscaling

Sets the scaling of the EOFs. The following values are accepted:

  • 0 : Un-scaled EOFs (default).

  • 1 : EOFs are divided by the square-root of their eigenvalues.

  • 2 : EOFs are multiplied by the square-root of their eigenvalues.

neofs

Number of EOFs to return. Defaults to all EOFs. If the number of EOFs requested is more than the number that are available, then all available EOFs will be returned.

Returns:

eofs

An array with the ordered EOFs along the first dimension.

Examples:

All EOFs with no scaling:

eofs = solver.eofs()

The leading EOF with scaling applied:

eof1 = solver.eofs(neofs=1, eofscaling=1)
eofsAsCorrelation(neofs=None)[source]

Correlation map EOFs.

Empirical orthogonal functions (EOFs) expressed as the correlation between the principal component time series (PCs) and the time series of the Eof input dataset at each grid point.

Note

These are not related to the EOFs computed from the correlation matrix.

Optional argument:

neofs

Number of EOFs to return. Defaults to all EOFs. If the number of EOFs requested is more than the number that are available, then all available EOFs will be returned.

Returns:

eofs

An array with the ordered EOFs along the first dimension.

Examples:

All EOFs:

eofs = solver.eofsAsCorrelation()

The leading EOF:

eof1 = solver.eofsAsCorrelation(neofs=1)
eofsAsCovariance(neofs=None, pcscaling=1)[source]

Covariance map EOFs.

Empirical orthogonal functions (EOFs) expressed as the covariance between the principal component time series (PCs) and the time series of the Eof input dataset at each grid point.

Optional arguments:

neofs

Number of EOFs to return. Defaults to all EOFs. If the number of EOFs requested is more than the number that are available, then all available EOFs will be returned.

pcscaling

Set the scaling of the PCs used to compute covariance. The following values are accepted:

  • 0 : Un-scaled PCs.

  • 1 : PCs are scaled to unit variance (divided by the square-root of their eigenvalue) (default).

  • 2 : PCs are multiplied by the square-root of their eigenvalue.

The default is to divide PCs by the square-root of their eigenvalue so that the PCs are scaled to unit variance (option 1).

Returns:

eofs

An array with the ordered EOFs along the first dimension.

Examples:

All EOFs:

eofs = solver.eofsAsCovariance()

The leading EOF:

eof1 = solver.eofsAsCovariance(neofs=1)

The leading EOF using un-scaled PCs:

eof1 = solver.eofsAsCovariance(neofs=1, pcscaling=0)
eigenvalues(neigs=None)[source]

Eigenvalues (decreasing variances) associated with each EOF.

Optional argument:

neigs

Number of eigenvalues to return. Defaults to all eigenvalues. If the number of eigenvalues requested is more than the number that are available, then all available eigenvalues will be returned.

Returns:

eigenvalues

An array containing the eigenvalues arranged largest to smallest.

Examples:

All eigenvalues:

eigenvalues = solver.eigenvalues()

The first eigenvalue:

eigenvalue1 = solver.eigenvalues(neigs=1)
varianceFraction(neigs=None)[source]

Fractional EOF mode variances.

The fraction of the total variance explained by each EOF mode, values between 0 and 1 inclusive.

Optional argument:

neigs

Number of eigenvalues to return the fractional variance for. Defaults to all eigenvalues. If the number of eigenvalues requested is more than the number that are available, then fractional variances for all available eigenvalues will be returned.

Returns:

variance_fractions

An array containing the fractional variances.

Examples:

The fractional variance represented by each EOF mode:

variance_fractions = solver.varianceFraction()

The fractional variance represented by the first EOF mode:

variance_fraction_mode_1 = solver.VarianceFraction(neigs=1)
totalAnomalyVariance()[source]

Total variance associated with the field of anomalies (the sum of the eigenvalues).

Returns:

total_variance

A scalar value.

Example:

Get the total variance:

total_variance = solver.totalAnomalyVariance()
northTest(neigs=None, vfscaled=False)[source]

Typical errors for eigenvalues.

The method of North et al. (1982) is used to compute the typical error for each eigenvalue. It is assumed that the number of times in the input data set is the same as the number of independent realizations. If this assumption is not valid then the result may be inappropriate.

Optional arguments:

neigs

The number of eigenvalues to return typical errors for. Defaults to typical errors for all eigenvalues. If the number of eigenvalues requested is more than the number that are available, then typical errors for all available eigenvalues will be returned.

vfscaled

If True scale the errors by the sum of the eigenvalues. This yields typical errors with the same scale as the values returned by Eof.varianceFraction. If False then no scaling is done. Defaults to False (no scaling).

Returns:

errors

An array containing the typical errors.

References

North G.R., T.L. Bell, R.F. Cahalan, and F.J. Moeng (1982) Sampling errors in the estimation of empirical orthogonal functions. Mon. Weather. Rev., 110, pp 669-706.

Examples:

Typical errors for all eigenvalues:

errors = solver.northTest()

Typical errors for the first 5 eigenvalues scaled by the sum of the eigenvalues:

errors = solver.northTest(neigs=5, vfscaled=True)
reconstructedField(neofs)[source]

Reconstructed data field based on a subset of EOFs.

If weights were passed to the Eof instance the returned reconstructed field will automatically have this weighting removed. Otherwise the returned field will have the same weighting as the Eof input dataset.

Argument:

neofs

Number of EOFs to use for the reconstruction. If the number of EOFs requested is more than the number that are available, then all available EOFs will be used for the reconstruction. Alternatively this argument can be an iterable of mode numbers (where the first mode is 1) in order to facilitate reconstruction with arbitrary modes.

Returns:

reconstruction

An array the same shape as the Eof input dataset contaning the reconstruction using neofs EOFs.

Examples:

Reconstruct the input field using 3 EOFs:

reconstruction = solver.reconstructedField(3)

Reconstruct the input field using EOFs 1, 2 and 5:

reconstruction = solver.reconstuctedField([1, 2, 5])
projectField(field, neofs=None, eofscaling=0, weighted=True)[source]

Project a field onto the EOFs.

Given a data set, projects it onto the EOFs to generate a corresponding set of pseudo-PCs.

Argument:

field

A numpy.ndarray or numpy.ma.MaskedArray with two or more dimensions containing the data to be projected onto the EOFs. It must have the same corresponding spatial dimensions (including missing values in the same places) as the Eof input dataset. field may have a different length time dimension to the Eof input dataset or no time dimension at all.

Optional arguments:

neofs

Number of EOFs to project onto. Defaults to all EOFs. If the number of EOFs requested is more than the number that are available, then the field will be projected onto all available EOFs.

eofscaling

Set the scaling of the EOFs that are projected onto. The following values are accepted:

  • 0 : Un-scaled EOFs (default).

  • 1 : EOFs are divided by the square-root of their eigenvalue.

  • 2 : EOFs are multiplied by the square-root of their eigenvalue.

weighted

If True then field is weighted using the same weights used for the EOF analysis prior to projection. If False then no weighting is applied. Defaults to True (weighting is applied). Generally only the default setting should be used.

Returns:

pseudo_pcs

An array where the columns are the ordered pseudo-PCs.

Examples:

Project a data set onto all EOFs:

pseudo_pcs = solver.projectField(data)

Project a data set onto the four leading EOFs:

pseudo_pcs = solver.projectField(data, neofs=4)
getWeights()[source]

Weights used for the analysis.

Returns:

weights

An array containing the analysis weights.

Example:

The weights used for the analysis:

weights = solver.getWeights()