Ejemplo n.º 1
0
    def _validate_xdata(self, xdata):
        """
        Validates the xdata passed, does typechecking and necessary casting.
        If a string is passed, this will request the proper values based on the
        parameters.

        Parameters
        ----------
        xdata : str, list, numpy.ndarray
            The xdata used as x values on the spectrum plot.

        Returns
        -------
        xdata_values : numpy.ndarray
            The xdata values of proper length and casted to a Numpy array.
        """
        if isinstance(xdata, str):
            if self.dataseries.parameters.get(xdata, None) is None:
                raise ValueError(
                    f"Provided key xdata='{xdata}' is not in parameters: \n"
                    f"{self.dataseries.parameters.keys()}")
            xdata_values = self.dataseries.parameters[xdata]
        elif isinstance(xdata, (list, np.ndarray)):
            xdata_values = transform_to_numpy(xdata)
            if len(xdata_values) != len(self.dataseries):
                raise ValueError(
                    f"Lengts of xdata do not match: "
                    f"{len(xdata_values)} vs {len(self.dataseries)}")
        else:
            raise TypeError(
                f"xdata should be a string, list or numpy array but got {type(xdata)}."
            )
        return xdata_values
Ejemplo n.º 2
0
    def get_nearest_eigenvalues(self, ev_guesses):
        """
        Calculates the eigenvalues nearest to a given guess. This calculates
        the nearest eigenvalue based on the distance between two points.

        Parameters
        ----------
        ev_guesses : float, complex, list of float, list of complex
            The guesses for the eigenvalues. These can be a single float/complex value,
            or a list/Numpy array of floats/complex values.

        Returns
        -------
        idxs : numpy.ndarray(dtype=int, ndim=1)
            The indices of the nearest eigenvalues in the :attr:`eigenvalues` array.
        eigenvalues: numpy.ndarray(dtype=complex, ndim=1)
            The nearest eigenvalues to the provided guesses, corresponding with the
            indices `idxs`.
        """
        ev_guesses = transform_to_numpy(ev_guesses)
        idxs = np.empty(shape=len(ev_guesses), dtype=int)
        eigenvals = np.empty(shape=len(ev_guesses), dtype=complex)
        for i, ev_guess in enumerate(ev_guesses):
            # distance from guess to all eigenvalues
            distances = (self.eigenvalues.real - ev_guess.real) ** 2 + (
                self.eigenvalues.imag - ev_guess.imag
            ) ** 2
            # closest distance (squared)
            idx = np.nanargmin(distances)
            idxs[i] = idx
            eigenvals[i] = self.eigenvalues[idx]
        return idxs, eigenvals
Ejemplo n.º 3
0
    def get_eigenfunctions(self, ev_guesses=None, ev_idxs=None):
        """
        Returns the eigenfunctions based on given eigenvalue guesses or their
        indices. An array will be returned where every item is a dictionary, containing
        both the eigenvalue and its eigenfunctions. Either eigenvalue guesses or
        indices can be supplied, but not both.

        Parameters
        ----------
        ev_guesses : (list of) int, float, complex
            Eigenvalue guesses.
        ev_idxs : (list of) int
            Indices corresponding to the eigenvalues that need to be retrieved.

        Returns
        -------
        eigenfuncs : numpy.ndarray(dtype=dict, ndim=1)
            Array containing the eigenfunctions and eigenvalues corresponding to the
            supplied indices. Every index in this array contains a dictionary with the
            eigenfunctions and corresponding eigenvalue.
            The keys of each dictionary are the eigenfunction names.
        """
        if ev_guesses is not None and ev_idxs is not None:
            raise ValueError(
                "get_eigenfunctions: either provide guesses or indices but not both"
            )
        if ev_guesses is not None:
            idxs, _ = self.get_nearest_eigenvalues(ev_guesses)
        else:
            idxs = transform_to_numpy(ev_idxs)
            for idx in idxs:
                if not isinstance(idx, (int, np.int64)):
                    raise ValueError("get_eigenfunctions: ev_idxs should be integers")
        eigenfuncs = np.array([{}] * len(idxs), dtype=dict)
        with open(self.datfile, "rb") as istream:
            for dict_idx, ef_idx in enumerate(idxs):
                efs = read_eigenfunction(istream, self.header, ef_idx)
                efs.update({"eigenvalue": self.eigenvalues[ef_idx]})
                eigenfuncs[dict_idx] = efs
        return eigenfuncs
Ejemplo n.º 4
0
def test_none_tonumpy():
    result = toolbox.transform_to_numpy(None)
    assert isinstance(result, np.ndarray)
    assert result[0] is None
Ejemplo n.º 5
0
def test_list_tonumpy():
    result = toolbox.transform_to_numpy([0, 0.25, 0.5, 0.75, 1])
    assert isinstance(result, np.ndarray)
    assert result == pytest.approx(np.linspace(0, 1, 5))
Ejemplo n.º 6
0
def test_numpy_tonumpy():
    expected = np.linspace(0, 1, 10)
    result = toolbox.transform_to_numpy(expected)
    assert isinstance(result, np.ndarray)
    assert result == pytest.approx(expected)
Ejemplo n.º 7
0
def test_cmplx_tonumpy():
    result = toolbox.transform_to_numpy(3 - 5j)
    assert isinstance(result, np.ndarray)
    assert result[0] == 3 - 5j
Ejemplo n.º 8
0
def test_float_tonumpy():
    result = toolbox.transform_to_numpy(8)
    assert isinstance(result, np.ndarray)
    assert result[0] == 8