# Path: lib/eofs/tests/utils.py
# def sign_adjustments(eofset, refeofset):
#     """Sign adjustments for EOFs/PCs.
# 
#     Create a matrix of sign weights used for adjusting the sign of a set
#     of EOFs or PCs to the sign of a reference set.
# 
#     The first dimension is assumed to be modes.
# 
#     **Arguments:**
# 
#     *eofset*
#         Set of EOFs.
# 
#     *refeofset*
#         Reference set of EOFs.
# 
#     """
#     if eofset.shape != refeofset.shape:
#         raise ValueError('input set has different shape from reference set')
#     eofset, refeofset = __tomasked(eofset, refeofset)
#     nmodes = eofset.shape[0]
#     signs = np.ones([nmodes])
#     shape = [nmodes] + [1] * (eofset.ndim - 1)
#     eofset = eofset.reshape([nmodes, np.prod(eofset.shape[1:], dtype=np.int)])
#     refeofset = refeofset.reshape([nmodes,
#                                    np.prod(refeofset.shape[1:],
#                                            dtype=np.int)])
#     for mode in range(nmodes):
#         i = 0
#         try:
#             while _close(eofset[mode, i], 0.) or \
#                   _close(refeofset[mode, i], 0.) \
#                   or np.ma.is_masked(eofset[mode, i]) or \
#                   np.ma.is_masked(refeofset[mode, i]):
#                 i += 1
#         except IndexError:
#             i = 0
#         if np.sign(eofset[mode, i]) != np.sign(refeofset[mode, i]):
#             signs[mode] = -1
#     return signs.reshape(shape)
# 
# Path: lib/eofs/tests/reference.py
# def reference_solution(container_type, weights):
#     """Obtain a reference EOF analysis solution.
# 
#     **Arguments:**
# 
#     *container_type*
#         Either 'standard', 'cdms', 'iris' or 'xarray'.
# 
#     *weights*
#         Weights method. One of 'equal', 'latitude', or 'area'.
# 
#     """
#     container_type = container_type.lower()
#     weights = weights.lower()
#     if container_type not in ('standard', 'iris', 'cdms', 'xarray'):
#         raise ValueError("unknown container type "
#                          "'{!s}'".format(container_type))
#     solution = _read_reference_solution(weights)
#     time_units = 'months since 0-1-1 00:00:0.0'
#     neofs = len(solution['eigenvalues'])
#     _get_wrapper(container_type)(solution, neofs, time_units)
#     return solution


import numpy as np
import numpy.ma as ma
import pytest
import eofs
        import dask
        import dask
        import dask
        import dask
        import dask
    from iris.cube import Cube
from eofs.tests import EofsTest
from .utils import sign_adjustments
from .reference import reference_solution
        elif eofscaling == 2:
            reofs *= np.sqrt(reigs)[:, np.newaxis, np.newaxis]
        self.assert_array_almost_equal(eofs, reofs)

    @pytest.mark.parametrize('eofscaling', (0, 1, 2))
    def test_eofs_orthogonal(self, eofscaling):
        # EOFs should be mutually orthogonal
        eofs = self._tomasked(self.solver.eofs(neofs=self.neofs,
                                               eofscaling=eofscaling))
        eofs = eofs.compressed()
        ns = eofs.shape[0] // self.neofs
        eofs = eofs.reshape([self.neofs, ns])
        dot = np.dot(eofs, eofs.T)
        residual = dot - np.diag(dot.diagonal())
        self.assert_array_almost_equal(residual, 0.)

    def test_eofsAsCovariance(self):
        # EOFs as covariance between PCs and input field should match the
        # reference solution
        eofs = self._tomasked(self.solver.eofsAsCovariance(neofs=self.neofs,
                                                           pcscaling=1))
        reofs = self._tomasked(self.solution['eofscov'])
        eofs *= sign_adjustments(eofs, reofs)
        self.assert_array_almost_equal(eofs, reofs)

    def test_eofsAsCorrelation(self):
        # EOFs as correlation between PCs and input field should match the
        # reference solution
        eofs = self._tomasked(self.solver.eofsAsCorrelation(neofs=self.neofs))
        reofs = self._tomasked(self.solution['eofscor'])

# Path: lib/eofs/tests/utils.py
# def sign_adjustments(eofset, refeofset):
#     """Sign adjustments for EOFs/PCs.
# 
#     Create a matrix of sign weights used for adjusting the sign of a set
#     of EOFs or PCs to the sign of a reference set.
# 
#     The first dimension is assumed to be modes.
# 
#     **Arguments:**
# 
#     *eofset*
#         Set of EOFs.
# 
#     *refeofset*
#         Reference set of EOFs.
# 
#     """
#     if eofset.shape != refeofset.shape:
#         raise ValueError('input set has different shape from reference set')
#     eofset, refeofset = __tomasked(eofset, refeofset)
#     nmodes = eofset.shape[0]
#     signs = np.ones([nmodes])
#     shape = [nmodes] + [1] * (eofset.ndim - 1)
#     eofset = eofset.reshape([nmodes, np.prod(eofset.shape[1:], dtype=np.int)])
#     refeofset = refeofset.reshape([nmodes,
#                                    np.prod(refeofset.shape[1:],
#                                            dtype=np.int)])
#     for mode in range(nmodes):
#         i = 0
#         try:
#             while _close(eofset[mode, i], 0.) or \
#                   _close(refeofset[mode, i], 0.) \
#                   or np.ma.is_masked(eofset[mode, i]) or \
#                   np.ma.is_masked(refeofset[mode, i]):
#                 i += 1
#         except IndexError:
#             i = 0
#         if np.sign(eofset[mode, i]) != np.sign(refeofset[mode, i]):
#             signs[mode] = -1
#     return signs.reshape(shape)
# 
# Path: lib/eofs/tests/reference.py
# def reference_solution(container_type, weights):
#     """Obtain a reference EOF analysis solution.
# 
#     **Arguments:**
# 
#     *container_type*
#         Either 'standard', 'cdms', 'iris' or 'xarray'.
# 
#     *weights*
#         Weights method. One of 'equal', 'latitude', or 'area'.
# 
#     """
#     container_type = container_type.lower()
#     weights = weights.lower()
#     if container_type not in ('standard', 'iris', 'cdms', 'xarray'):
#         raise ValueError("unknown container type "
#                          "'{!s}'".format(container_type))
#     solution = _read_reference_solution(weights)
#     time_units = 'months since 0-1-1 00:00:0.0'
#     neofs = len(solution['eigenvalues'])
#     _get_wrapper(container_type)(solution, neofs, time_units)
#     return solution


# Path: lib/eofs/tests/test_solution.py
import numpy as np
import numpy.ma as ma
import pytest
import eofs
        import dask
        import dask
        import dask
        import dask
        import dask
    from iris.cube import Cube
from eofs.tests import EofsTest
from .utils import sign_adjustments
from .reference import reference_solution

        elif eofscaling == 2:
            reofs *= np.sqrt(reigs)[:, np.newaxis, np.newaxis]
        self.assert_array_almost_equal(eofs, reofs)

    @pytest.mark.parametrize('eofscaling', (0, 1, 2))
    def test_eofs_orthogonal(self, eofscaling):
        # EOFs should be mutually orthogonal
        eofs = self._tomasked(self.solver.eofs(neofs=self.neofs,
                                               eofscaling=eofscaling))
        eofs = eofs.compressed()
        ns = eofs.shape[0] // self.neofs
        eofs = eofs.reshape([self.neofs, ns])
        dot = np.dot(eofs, eofs.T)
        residual = dot - np.diag(dot.diagonal())
        self.assert_array_almost_equal(residual, 0.)

    def test_eofsAsCovariance(self):
        # EOFs as covariance between PCs and input field should match the
        # reference solution
        eofs = self._tomasked(self.solver.eofsAsCovariance(neofs=self.neofs,
                                                           pcscaling=1))
        reofs = self._tomasked(self.solution['eofscov'])
        eofs *= sign_adjustments(eofs, reofs)
        self.assert_array_almost_equal(eofs, reofs)

    def test_eofsAsCorrelation(self):
        # EOFs as correlation between PCs and input field should match the
        # reference solution
        eofs = self._tomasked(self.solver.eofsAsCorrelation(neofs=self.neofs))
        reofs = self._tomasked(self.solution['eofscor'])

        eofs *= sign_adjustments(eofs, reofs)