# 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_multivariate_solution(container_type, weights):
#     """Obtain a reference multivariate EOF analysis solution.
# 
#     **Arguments:**
# 
#     *container_type*
#         Either 'standard', 'cdms', or 'iris'.
# 
#     *weights*
#         Weights method. One of 'equal', 'latitude', 'area',
#         'area_multi', or 'area_multi_mix'.
# 
#     """
#     if weights.lower() == 'area':
#         weights = 'area_multi'
#     if weights.lower() == 'none_area':
#         weights = 'area_multi_mix'
#     solution = reference_solution(container_type, weights)
#     nlon = len(solution['longitude'])
#     slice1 = slice(0, nlon // 2)
#     slice2 = slice(nlon // 2, None)
#     for var in ('longitude',
#                 'sst',
#                 'eofs',
#                 'eofscor',
#                 'eofscov',
#                 'weights',
#                 'rcon',):
#         try:
#             solution[var] = (solution[var][..., slice1],
#                              solution[var][..., slice2])
#         except TypeError:
#             solution[var] = None, None
#     return solution


import numpy as np
import pytest
import eofs.multivariate as multivariate
    from iris.cube import Cube
from eofs.tests import EofsTest
from .utils import sign_adjustments
from .reference import reference_multivariate_solution
    def setup_class(cls):
        try:
            cls.solution = reference_multivariate_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]
        if cls.alternate_weights_arg is not None:
            weights = cls.alternate_weights_arg
        else:
            weights = cls.solution['weights']
        try:
            cls.solver = solvers[cls.interface](cls.solution['sst'],
                                                weights=weights)
        except KeyError:
            pytest.skip('missing dependencies required to test '
                        'the {!s} interface'.format(cls.interface))

    def test_eigenvalues(self):
        self.assert_array_almost_equal(
            self.solver.eigenvalues(neigs=self.neofs),
            self.solution['eigenvalues'])

    @pytest.mark.parametrize('eofscaling', (0, 1, 2))
    def test_eofs(self, eofscaling):
        eofs = [self._tomasked(e)
                for e in self.solver.eofs(neofs=self.neofs,
                                          eofscaling=eofscaling)]
        reofs = [self._tomasked(e).copy() for e in self.solution['eofs']]

# 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_multivariate_solution(container_type, weights):
#     """Obtain a reference multivariate EOF analysis solution.
# 
#     **Arguments:**
# 
#     *container_type*
#         Either 'standard', 'cdms', or 'iris'.
# 
#     *weights*
#         Weights method. One of 'equal', 'latitude', 'area',
#         'area_multi', or 'area_multi_mix'.
# 
#     """
#     if weights.lower() == 'area':
#         weights = 'area_multi'
#     if weights.lower() == 'none_area':
#         weights = 'area_multi_mix'
#     solution = reference_solution(container_type, weights)
#     nlon = len(solution['longitude'])
#     slice1 = slice(0, nlon // 2)
#     slice2 = slice(nlon // 2, None)
#     for var in ('longitude',
#                 'sst',
#                 'eofs',
#                 'eofscor',
#                 'eofscov',
#                 'weights',
#                 'rcon',):
#         try:
#             solution[var] = (solution[var][..., slice1],
#                              solution[var][..., slice2])
#         except TypeError:
#             solution[var] = None, None
#     return solution


# Path: lib/eofs/tests/test_multivariate_solution.py
import numpy as np
import pytest
import eofs.multivariate as multivariate
    from iris.cube import Cube
from eofs.tests import EofsTest
from .utils import sign_adjustments
from .reference import reference_multivariate_solution

    def setup_class(cls):
        try:
            cls.solution = reference_multivariate_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]
        if cls.alternate_weights_arg is not None:
            weights = cls.alternate_weights_arg
        else:
            weights = cls.solution['weights']
        try:
            cls.solver = solvers[cls.interface](cls.solution['sst'],
                                                weights=weights)
        except KeyError:
            pytest.skip('missing dependencies required to test '
                        'the {!s} interface'.format(cls.interface))

    def test_eigenvalues(self):
        self.assert_array_almost_equal(
            self.solver.eigenvalues(neigs=self.neofs),
            self.solution['eigenvalues'])

    @pytest.mark.parametrize('eofscaling', (0, 1, 2))
    def test_eofs(self, eofscaling):
        eofs = [self._tomasked(e)
                for e in self.solver.eofs(neofs=self.neofs,
                                          eofscaling=eofscaling)]
        reofs = [self._tomasked(e).copy() for e in self.solution['eofs']]

        eofs = [e * sign_adjustments(e, r) for e, r in zip(eofs, reofs)]