# 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/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)


import numpy as np
import numpy.ma as ma
import pytest
import eofs
    from iris.cube import Cube
from eofs.tests import EofsTest
from .reference import reference_solution
from .utils import sign_adjustments
        try:
            cls.solution = reference_solution(cls.interface, cls.weights)
        except ValueError:
            pytest.skip('missing dependencies required to test '
                        'the {!s} interface'.format(cls.interface))
        cls.neofs = cls.solution['eigenvalues'].shape[0]
        try:
            cls.solver = solvers[cls.interface](
                cls.solution['sst'], weights=cls.solution['weights'])
            cls.tools = {'covariance': tools[cls.interface].covariance_map,
                         'correlation': tools[cls.interface].correlation_map}
        except KeyError:
            pytest.skip('missing dependencies required to test '
                        'the {!s} interface'.format(cls.interface))

    def test_covariance_map(self):
        # covariance maps should match reference EOFs as covariance
        pcs = self.solver.pcs(npcs=self.neofs, pcscaling=1)
        cov = self.tools['covariance'](pcs, self.solution['sst'])
        eofs = self._tomasked(self.solver.eofs(neofs=self.neofs))
        reofs = self._tomasked(self.solution['eofs'])
        cov = self._tomasked(cov) * sign_adjustments(eofs, reofs)
        self.assert_array_almost_equal(cov, self.solution['eofscov'])

    def test_correlation_map(self):
        # correlation maps should match reference EOFs as correlation
        pcs = self.solver.pcs(npcs=self.neofs, pcscaling=1)
        cor = self.tools['correlation'](pcs, self.solution['sst'])
        eofs = self._tomasked(self.solver.eofs(neofs=self.neofs))
        reofs = self._tomasked(self.solution['eofs'])

# 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/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/test_tools.py
import numpy as np
import numpy.ma as ma
import pytest
import eofs
    from iris.cube import Cube
from eofs.tests import EofsTest
from .reference import reference_solution
from .utils import sign_adjustments

        try:
            cls.solution = reference_solution(cls.interface, cls.weights)
        except ValueError:
            pytest.skip('missing dependencies required to test '
                        'the {!s} interface'.format(cls.interface))
        cls.neofs = cls.solution['eigenvalues'].shape[0]
        try:
            cls.solver = solvers[cls.interface](
                cls.solution['sst'], weights=cls.solution['weights'])
            cls.tools = {'covariance': tools[cls.interface].covariance_map,
                         'correlation': tools[cls.interface].correlation_map}
        except KeyError:
            pytest.skip('missing dependencies required to test '
                        'the {!s} interface'.format(cls.interface))

    def test_covariance_map(self):
        # covariance maps should match reference EOFs as covariance
        pcs = self.solver.pcs(npcs=self.neofs, pcscaling=1)
        cov = self.tools['covariance'](pcs, self.solution['sst'])
        eofs = self._tomasked(self.solver.eofs(neofs=self.neofs))
        reofs = self._tomasked(self.solution['eofs'])
        cov = self._tomasked(cov) * sign_adjustments(eofs, reofs)
        self.assert_array_almost_equal(cov, self.solution['eofscov'])

    def test_correlation_map(self):
        # correlation maps should match reference EOFs as correlation
        pcs = self.solver.pcs(npcs=self.neofs, pcscaling=1)
        cor = self.tools['correlation'](pcs, self.solution['sst'])
        eofs = self._tomasked(self.solver.eofs(neofs=self.neofs))
        reofs = self._tomasked(self.solution['eofs'])

        cor = self._tomasked(cor) * sign_adjustments(eofs, reofs)