Exemple #1
0
 def test_polytrim(self) :
     coef = [2, -1, 1, 0]
     # Test exceptions
     assert_raises(ValueError, poly.polytrim, coef, -1)
     # Test results
     assert_equal(poly.polytrim(coef), coef[:-1])
     assert_equal(poly.polytrim(coef, 1), coef[:-3])
     assert_equal(poly.polytrim(coef, 2), [0])
Exemple #2
0
 def test_polytrim(self) :
     coef = [2, -1, 1, 0]
     # Test exceptions
     assert_raises(ValueError, poly.polytrim, coef, -1)
     # Test results
     assert_equal(poly.polytrim(coef), coef[:-1])
     assert_equal(poly.polytrim(coef, 1), coef[:-3])
     assert_equal(poly.polytrim(coef, 2), [0])
Exemple #3
0
def get_wavelength_per_pixel(da):
    """
    Computes the wavelength for each pixel along the C dimension

    :param da: (model.DataArray of shape C...): the DataArray with metadata
        either MD_WL_POLYNOMIAL or MD_WL_LIST
    :return: (list of float of length C): the wavelength (in m) for each pixel
        in C
    :raises:
        AttributeError: if no metadata is present
        KeyError: if no metadata is available
        ValueError: if the metadata doesn't provide enough information
    """

    if not hasattr(da, 'metadata'):
        raise AttributeError("No metadata found in data array")

    # check dimension of data
    dims = da.metadata.get(model.MD_DIMS, "CTZYX"[-da.ndim:])
    if len(dims) == 3 and dims == "YXC" and da.shape[2] in (3, 4):  # RGB?
        # This is a hack to handle RGB projections of CX (ie, line spectrum)
        # and CT (temporal spectrum) data. In theory the MD_DIMS should be
        # XCR and TCR (where the R is about the RGB channels). However,
        # this is confusing, and the GUI would not know how to display it.
        ci = 1
    else:
        try:
            ci = dims.index("C")  # get index of dimension C
        except ValueError:
            raise ValueError(
                "Dimension 'C' not in dimensions, so skip computing wavelength list."
            )

    # MD_WL_LIST has priority
    if model.MD_WL_LIST in da.metadata:
        wl = da.metadata[model.MD_WL_LIST]

        if len(wl) != da.shape[ci]:
            raise ValueError(
                "Length of wavelength list does not match length of wavelength data."
            )
        return wl
    elif model.MD_WL_POLYNOMIAL in da.metadata:
        pn = da.metadata[model.MD_WL_POLYNOMIAL]
        pn = polynomial.polytrim(pn)
        if len(pn) >= 2:
            npn = polynomial.Polynomial(
                pn,  #pylint: disable=E1101
                domain=[0, da.shape[ci] - 1],
                window=[0, da.shape[ci] - 1])
            ret = npn.linspace(da.shape[ci])[1]
            return ret.tolist()
        else:
            # a polynomial of 0 or 1 value is useless
            raise ValueError("Wavelength polynomial has only %d degree" %
                             len(pn))

    raise KeyError("No MD_WL_* metadata available")
Exemple #4
0
def get_wavelength_per_pixel(da):
    """
    Computes the wavelength for each pixel along the C dimension

    :param da: (model.DataArray of shape C...): the DataArray with metadata
        either MD_WL_POLYNOMIAL or MD_WL_LIST
    :return: (list of float of length C): the wavelength (in m) for each pixel
        in C
    :raises:
        AttributeError: if no metadata is present
        KeyError: if no metadata is available
        ValueError: if the metadata doesn't provide enough information
    """

    if not hasattr(da, 'metadata'):
        raise AttributeError("No metadata found in data array")

    # MD_WL_LIST has priority
    if model.MD_WL_LIST in da.metadata:
        wl = da.metadata[model.MD_WL_LIST]
        if len(wl) == da.shape[0]:
            return wl
        else:
            raise ValueError(
                "Wavelength metadata (MD_WL_LIST) is not the same "
                "length as the data")

    if model.MD_WL_POLYNOMIAL in da.metadata:
        pn = da.metadata[model.MD_WL_POLYNOMIAL]
        pn = polynomial.polytrim(pn)
        if len(pn) >= 2:
            npn = polynomial.Polynomial(
                pn,  #pylint: disable=E1101
                domain=[0, da.shape[0] - 1],
                window=[0, da.shape[0] - 1])
            return npn.linspace(da.shape[0])[1]
        else:
            # a polynomial of 0 or 1 value is useless
            raise ValueError("Wavelength polynomial has only %d degree" %
                             len(pn))

    raise KeyError("No MD_WL_* metadata available")
Exemple #5
0
def get_wavelength_per_pixel(da):
    """
    Computes the wavelength for each pixel along the C dimension

    :param da: (model.DataArray of shape C...): the DataArray with metadata
        either MD_WL_POLYNOMIAL or MD_WL_LIST
    :return: (list of float of length C): the wavelength (in m) for each pixel
        in C
    :raises:
        AttributeError: if no metadata is present
        KeyError: if no metadata is available
        ValueError: if the metadata doesn't provide enough information
    """

    if not hasattr(da, 'metadata'):
        raise AttributeError("No metadata found in data array")

    # MD_WL_LIST has priority
    if model.MD_WL_LIST in da.metadata:
        wl = da.metadata[model.MD_WL_LIST]
        if len(wl) == da.shape[0]:
            return wl
        else:
            raise ValueError("Wavelength metadata (MD_WL_LIST) is not the same "
                             "length as the data")

    if model.MD_WL_POLYNOMIAL in da.metadata:
        pn = da.metadata[model.MD_WL_POLYNOMIAL]
        pn = polynomial.polytrim(pn)
        if len(pn) >= 2:
            npn = polynomial.Polynomial(pn,  #pylint: disable=E1101
                                        domain=[0, da.shape[0] - 1],
                                        window=[0, da.shape[0] - 1])
            return npn.linspace(da.shape[0])[1]
        else:
            # a polynomial of 0 or 1 value is useless
            raise ValueError("Wavelength polynomial has only %d degree"
                             % len(pn))

    raise KeyError("No MD_WL_* metadata available")
Exemple #6
0
def trim(x) :
    return poly.polytrim(x, tol=1e-6)
Exemple #7
0
def trim(x):
    return poly.polytrim(x, tol=1e-6)