Esempio n. 1
0
def assert_array_greater(x, y, err_msg='', verbose=True):
    assert_array_compare(operator.__gt__,
                         x,
                         y,
                         err_msg=err_msg,
                         verbose=verbose,
                         header='Arrays are not greater-ordered')
Esempio n. 2
0
def test_gaussian_process_deep_copyable(
        gpr_interface_factory: ModelFactoryType) -> None:
    x = tf.constant(np.arange(5).reshape(-1, 1), dtype=gpflow.default_float())
    model, _ = gpr_interface_factory(x, fnc_2sin_x_over_3(x))
    model_copy = copy.deepcopy(model)
    x_predict = tf.constant([[50.5]], gpflow.default_float())

    # check deepcopy predicts same values as original
    mean_f, variance_f = model.predict(x_predict)
    mean_f_copy, variance_f_copy = model_copy.predict(x_predict)
    npt.assert_equal(mean_f, mean_f_copy)
    npt.assert_equal(variance_f, variance_f_copy)

    # check that updating the original doesn't break or change the deepcopy
    x_new = tf.concat(
        [x, tf.constant([[10.0], [11.0]], dtype=gpflow.default_float())], 0)
    new_data = Dataset(x_new, fnc_2sin_x_over_3(x_new))
    model.update(new_data)
    model.optimize(new_data)

    mean_f_updated, variance_f_updated = model.predict(x_predict)
    mean_f_copy_updated, variance_f_copy_updated = model_copy.predict(
        x_predict)
    npt.assert_equal(mean_f_copy_updated, mean_f_copy)
    npt.assert_equal(variance_f_copy_updated, variance_f_copy)
    npt.assert_array_compare(operator.__ne__, mean_f_updated, mean_f)
    npt.assert_array_compare(operator.__ne__, variance_f_updated, variance_f)
def assert_array_less_equal(x, y, err_msg="", verbose=True):
    """Assert array x <= y.  Based on numpy.testing.assert_array_less."""
    assert_array_compare(
        operator.le,
        x,
        y,
        err_msg=err_msg,
        verbose=verbose,
        header="Arrays are not less-ordered",
        equal_inf=False,
    )
Esempio n. 4
0
def test_downscale_resp_timeseries(flux_resp, temperature):
    """Test downscaling of Respiration."""
    flux_resp_downscaled = olsen_randerson.fisher.downscale_resp_timeseries(
        flux_resp, temperature
    )
    assert flux_resp_downscaled.shape == temperature.shape
    # assert np.all(flux_resp_downscaled.iloc[1:-1, :] >= 0)
    np_tst.assert_array_compare(
        operator.ge,
        flux_resp_downscaled.iloc[1:-1, :],
        0
    )
    flux_resp_downscaled_upscaled = flux_resp_downscaled.resample(
        olsen_randerson.fisher.INPUT_FREQUENCY
    ).sum()
    assert flux_resp_downscaled_upscaled.shape == flux_resp.shape
Esempio n. 5
0
def test_zero_padding():
    """Ensure that the implicit zero-padding does not cause problems."""
    # Ensure that large integers are inserted in little-endian fashion to avoid
    # trailing 0s.
    ss0 = SeedSequence(42)
    ss1 = SeedSequence(42 << 32)
    assert_array_compare(np.not_equal, ss0.generate_state(4),
                         ss1.generate_state(4))

    # Ensure backwards compatibility with the original 0.17 release for small
    # integers and no spawn key.
    expected42 = np.array([3444837047, 2669555309, 2046530742, 3581440988],
                          dtype=np.uint32)
    assert_array_equal(SeedSequence(42).generate_state(4), expected42)

    # Regression test for gh-16539 to ensure that the implicit 0s don't
    # conflict with spawn keys.
    assert_array_compare(np.not_equal,
                         SeedSequence(42, spawn_key=(0, )).generate_state(4),
                         expected42)
Esempio n. 6
0
def test_apply_affine_to_actor(interactive=False):
    text_act = actor.text_3d("ALIGN TOP RIGHT",
                             justification='right',
                             vertical_justification='top')

    text_act2 = TextActor3D()
    text_act2.SetInput("ALIGN TOP RIGHT")
    text_act2.GetTextProperty().SetFontFamilyToArial()
    text_act2.GetTextProperty().SetFontSize(24)
    text_act2.SetScale((1. / 24. * 12, ) * 3)

    if interactive:
        scene = window.Scene()
        scene.add(text_act, text_act2)
        window.show(scene)

    text_bounds = [0, 0, 0, 0]
    text_act2.GetBoundingBox(text_bounds)
    initial_bounds = text_act2.GetBounds()

    affine = np.eye(4)
    affine[:3, -1] += (-text_bounds[1], 0, 0)
    affine[:3, -1] += (0, -text_bounds[3], 0)
    affine[:3, -1] *= text_act2.GetScale()
    apply_affine_to_actor(text_act2, affine)
    text_act2.GetBoundingBox(text_bounds)

    if interactive:
        scene = window.Scene()
        scene.add(text_act, text_act2)
        window.show(scene)

    updated_bounds = text_act2.GetBounds()
    original_bounds = text_act.GetBounds()
    npt.assert_array_almost_equal(updated_bounds, original_bounds, decimal=0)

    def compare(x, y):
        return np.isclose(x, y, rtol=1)

    npt.assert_array_compare(compare, updated_bounds, original_bounds)
Esempio n. 7
0
def assert_array_not_equal(x, y, err_msg='', verbose=True):
    """
    Raises an AssertionError if two array_like objects are equal.

    Given two array_like objects, check that the shape is equal and all
    elements of these objects are not equal. An exception is raised at
    shape mismatch or conflicting values. In contrast to the standard usage
    in numpy, NaNs are compared like numbers, no assertion is raised if
    both objects have NaNs in the same positions (ToDo: Check 2 NaNs).

    The usual caution for verifying equality with floating point numbers is
    advised.

    Parameters
    ----------
    x : array_like
        The actual object to check.
    y : array_like
        The desired, expected object.
    err_msg : str, optional
        The error message to be printed in case of failure.
    verbose : bool, optional
        If True, the conflicting values are appended to the error message.

    Raises
    ------
    AssertionError
        If actual and desired objects are equal.

    See Also
    --------
    assert_array_equal

    """
    assert_array_compare(operator.__ne__,
                         x,
                         y,
                         err_msg=err_msg,
                         verbose=verbose,
                         header='Arrays are equal')
Esempio n. 8
0
def assert_array_compare(
    comparison,
    x,
    y,
    err_msg="",
    verbose=True,
    header="",
    precision=6,
    equal_nan=True,
    equal_inf=True,
):
    npt.assert_array_compare(
        comparison,
        __np_array_from_torch__(x),
        __np_array_from_torch__(y),
        err_msg=err_msg,
        verbose=verbose,
        header=header,
        precision=precision,
        equal_nan=equal_nan,
        equal_inf=equal_inf,
    )
Esempio n. 9
0
def assert_array_nanequal(a, b, *args, **kwargs):
    """
    Similar as np.testing.assert_array_equal but with better handling of
    object arrays.

    Note
    ----
    Is not fast!

    Parameters
    ----------
    a : array-like
    b : array-like
    """
    return assert_array_compare(naneq, a, b, *args, **kwargs)
Esempio n. 10
0
def compare_datasets(this, other, approx=False, decimal=6, data_only=False):
    from spectrochempy.core.dataset.ndarray import NDArray
    from spectrochempy.units import ur, Quantity

    def compare(x, y):
        from numpy.core import number, float_, result_type, array
        from numpy.core.numerictypes import issubdtype
        from numpy.core.fromnumeric import any as npany

        try:
            if npany(gisinf(x)) or npany(gisinf(y)):
                xinfid = gisinf(x)
                yinfid = gisinf(y)
                if not (xinfid == yinfid).all():
                    return False
                # if one item, x and y is +- inf
                if x.size == y.size == 1:
                    return x == y
                x = x[~xinfid]
                y = y[~yinfid]
        except (TypeError, NotImplementedError):
            pass

        # make sure y is an inexact type to avoid abs(MIN_INT); will cause
        # casting of x later.
        dtype = result_type(y, 1.)
        y = array(y, dtype=dtype, copy=False, subok=True)
        z = abs(x - y)

        if not issubdtype(z.dtype, number):
            z = z.astype(float_)  # handle object arrays

        return z < 1.5 * 10.0**(-decimal)

    eq = True

    if not isinstance(other, NDArray):
        # try to make some assumption to make useful comparison.
        if isinstance(other, Quantity):
            otherdata = other.magnitude
            otherunits = other.units
        elif isinstance(other, (float, int, np.ndarray)):
            otherdata = other
            otherunits = False
        else:
            raise AssertionError(
                f'{this} and {other} objects are too different to be '
                f'compared.')

        if not this.has_units and not otherunits:
            eq = np.all(this._data == otherdata)
        elif this.has_units and otherunits:
            eq = np.all(this._data * this._units == otherdata * otherunits)
        else:
            raise AssertionError(
                f'units of {this} and {other} objects does not match')
        return eq

    thistype = this.implements()

    if data_only:
        attrs = ['data']
    else:
        attrs = this.__dir__()
        exclude = ('filename', 'preferences', 'description', 'history', 'date',
                   'modified', 'origin', 'roi', 'linear', 'offset',
                   'increment', 'size', 'name', 'show_datapoints', 'modeldata',
                   'processeddata', 'baselinedata', 'referencedata', 'state')
        for attr in exclude:
            # these attibutes are not used for comparison (comparison based on
            # data and units!)
            if attr in attrs:
                if attr in attrs:
                    attrs.remove(attr)

        # if 'title' in attrs:  #    attrs.remove('title')  #TODO: should we use title for comparison?

    for attr in attrs:
        if attr != 'units':
            sattr = getattr(this, f'_{attr}')
            if this.linear and attr == 'data':  # allow comparison of
                # LinearCoord
                # and Coord
                sattr = this.data
            if hasattr(other, f'_{attr}'):
                oattr = getattr(other, f'_{attr}')
                if other.linear and attr == 'data':
                    oattr = other.data
                # to avoid deprecation warning issue for unequal array
                if sattr is None and oattr is not None:
                    raise AssertionError(f'`{attr}` of {this} is None.')
                if oattr is None and sattr is not None:
                    raise AssertionError(f'{attr} of {other} is None.')
                if hasattr(oattr, 'size') and hasattr(
                        sattr, 'size') and oattr.size != sattr.size:
                    # particular case of mask
                    if attr != 'mask':
                        raise AssertionError(
                            f'{thistype}.{attr} sizes are different.')
                    else:
                        assert_array_equal(
                            other.mask, this.mask,
                            f'{this} and {other} masks are different.')
                if attr in ['data', 'mask']:
                    if approx:
                        assert_array_compare(
                            compare,
                            sattr,
                            oattr,
                            header=(f'{thistype}.{attr} attributes ar'
                                    f'e not almost equal to %d decimals' %
                                    decimal),
                            precision=decimal)
                    else:
                        assert_array_compare(operator.__eq__,
                                             sattr,
                                             oattr,
                                             header=f'{thistype}.{attr} '
                                             f'attributes are not '
                                             f'equal')

                elif attr in ['coordset']:
                    if (sattr is None
                            and oattr is not None) or (oattr is None
                                                       and sattr is not None):
                        raise AssertionError('One of the coordset is None')
                    elif sattr is None and oattr is None:
                        res = True
                    else:
                        for item in zip(sattr, oattr):
                            res = compare_datasets(*item,
                                                   approx=approx,
                                                   decimal=decimal)
                            if not res:
                                raise AssertionError(f'coords differs:\n{res}')
                else:
                    eq &= np.all(sattr == oattr)
                if not eq:
                    raise AssertionError(
                        f'The {attr} attributes of {this} and {other} are '
                        f'different.')
            else:
                return False
        else:
            # unitlesss and dimensionless are supposed equals
            sattr = this._units
            if sattr is None:
                sattr = ur.dimensionless
            if hasattr(other, '_units'):
                oattr = other._units
                if oattr is None:
                    oattr = ur.dimensionless

                eq &= np.all(sattr == oattr)
                if not eq:
                    raise AssertionError(
                        f"attributes `{attr}` are not equals or one is "
                        f"missing: \n{sattr} != {oattr}")
            else:
                raise AssertionError(f'{other} has no units')

    return True
Esempio n. 11
0
def compare_datasets(this, other, approx=False, decimal=6, data_only=False):
    from spectrochempy.core.units import ur

    def compare(x, y):
        return _compare(x, y, decimal)

    eq = True

    # if not isinstance(other, NDArray):
    #     # try to make some assumption to make useful comparison.
    #     if isinstance(other, Quantity):
    #         otherdata = other.magnitude
    #         otherunits = other.units
    #     elif isinstance(other, (float, int, np.ndarray)):
    #         otherdata = other
    #         otherunits = False
    #     else:
    #         raise AssertionError(
    #             f"{this} and {other} objects are too different to be " f"compared."
    #         )
    #
    #     if not this.has_units and not otherunits:
    #         eq = np.all(this._data == otherdata)
    #     elif this.has_units and otherunits:
    #         eq = np.all(this._data * this._units == otherdata * otherunits)
    #     else:
    #         raise AssertionError(f"units of {this} and {other} objects does not match")
    #     return eq

    thistype = this.implements()

    if other.data is None and this.data is None and data_only:
        attrs = ["labels"]
    elif data_only:
        attrs = ["data"]
    else:
        attrs = this.__dir__()
        exclude = (
            "filename",
            "preferences",
            "description",
            "history",
            "date",
            "modified",
            "origin",
            "roi",
            "size",
            "name",
            "show_datapoints",
            "modeldata",
            "processeddata",
            "baselinedata",
            "referencedata",
            "state",
        )
        for attr in exclude:
            # these attributes are not used for comparison (comparison based on
            # data and units!)
            if attr in attrs:
                if attr in attrs:
                    attrs.remove(attr)

        # if 'title' in attrs:  #    attrs.remove('title')  #TODO: should we use title for comparison?

    for attr in attrs:
        if attr != "units":
            sattr = getattr(this, f"_{attr}")
            if hasattr(other, f"_{attr}"):
                oattr = getattr(other, f"_{attr}")
                if sattr is None and oattr is not None:
                    raise AssertionError(f"`{attr}` of {this} is None.")
                if oattr is None and sattr is not None:
                    raise AssertionError(f"{attr} of {other} is None.")
                if (
                    hasattr(oattr, "size")
                    and hasattr(sattr, "size")
                    and oattr.size != sattr.size
                ):
                    # particular case of mask
                    if attr != "mask":
                        raise AssertionError(f"{thistype}.{attr} sizes are different.")
                    else:
                        assert_array_equal(
                            other.mask,
                            this.mask,
                            f"{this} and {other} masks are different.",
                        )
                if attr in ["data", "mask"]:
                    if approx:
                        assert_array_compare(
                            compare,
                            sattr,
                            oattr,
                            header=(
                                f"{thistype}.{attr} attributes ar"
                                f"e not almost equal to %d decimals" % decimal
                            ),
                            precision=decimal,
                        )
                    else:
                        assert_array_compare(
                            operator.__eq__,
                            sattr,
                            oattr,
                            header=f"{thistype}.{attr} "
                            f"attributes are not "
                            f"equal",
                        )

                elif attr in ["coordset"]:
                    if (sattr is None and oattr is not None) or (
                        oattr is None and sattr is not None
                    ):
                        raise AssertionError("One of the coordset is None")
                    elif sattr is None and oattr is None:
                        pass
                    else:
                        for item in zip(sattr, oattr):
                            res = compare_coords(*item, approx=approx, decimal=decimal)
                            if not res:
                                raise AssertionError(f"coords differs:\n{res}")
                else:
                    eq &= np.all(sattr == oattr)
                if not eq:
                    raise AssertionError(
                        f"The {attr} attributes of {this} and {other} are "
                        f"different."
                    )
            else:
                return False
        else:
            # unitlesss and dimensionless are supposed equals
            sattr = this._units
            if sattr is None:
                sattr = ur.dimensionless
            if hasattr(other, "_units"):
                oattr = other._units
                if oattr is None:
                    oattr = ur.dimensionless

                eq &= np.all(sattr == oattr)
                if not eq:
                    raise AssertionError(
                        f"attributes `{attr}` are not equals or one is "
                        f"missing: \n{sattr} != {oattr}"
                    )
            else:
                raise AssertionError(f"{other} has no units")

    return True
Esempio n. 12
0
def compare_coords(this, other, approx=False, decimal=6, data_only=False):

    from spectrochempy.core.units import ur

    def compare(x, y):
        return _compare(x, y, decimal)

    eq = True
    thistype = this.implements()

    if other.data is None and this.data is None and data_only:
        attrs = ["labels"]
    elif data_only:
        attrs = ["data"]
    else:
        attrs = ["data", "labels", "units", "meta", "title"]
        # if 'title' in attrs:  #    attrs.remove('title')  #TODO: should we use title for comparison?

    if other.linear == this.linear:
        # To còmpare linear coordinates
        attrs += ["offset", "increment", "linear", "size"]

    for attr in attrs:
        if attr != "units":
            sattr = getattr(this, f"_{attr}")
            if this.linear and attr == "data":
                # allow comparison of LinearCoord and Coord
                sattr = this.data
            if hasattr(other, f"_{attr}"):
                oattr = getattr(other, f"_{attr}")
                if other.linear and attr == "data":
                    oattr = other.data
                # to avoid deprecation warning issue for unequal array
                if sattr is None and oattr is not None:
                    raise AssertionError(f"`{attr}` of {this} is None.")
                if oattr is None and sattr is not None:
                    raise AssertionError(f"{attr} of {other} is None.")
                if (
                    hasattr(oattr, "size")
                    and hasattr(sattr, "size")
                    and oattr.size != sattr.size
                ):
                    raise AssertionError(f"{thistype}.{attr} sizes are different.")

                if attr == "data":
                    if approx:
                        assert_array_compare(
                            compare,
                            sattr,
                            oattr,
                            header=(
                                f"{thistype}.{attr} attributes ar"
                                f"e not almost equal to %d decimals" % decimal
                            ),
                            precision=decimal,
                        )
                    else:
                        assert_array_compare(
                            operator.__eq__,
                            sattr,
                            oattr,
                            header=f"{thistype}.{attr} "
                            f"attributes are not "
                            f"equal",
                        )

                elif attr in ["offset", "increment"] and approx:
                    assert_approx_equal(
                        sattr,
                        oattr,
                        significant=decimal,
                        err_msg=f"{thistype}.{attr} attributes "
                        f"are not almost equal to %d decimals" % decimal,
                    )

                else:
                    eq &= np.all(sattr == oattr)

                if not eq:
                    raise AssertionError(
                        f"The {attr} attributes of {this} and {other} are "
                        f"different."
                    )
            else:
                return False
        else:
            # unitless and dimensionless are supposed equals
            sattr = this._units
            if sattr is None:
                sattr = ur.dimensionless
            if hasattr(other, "_units"):
                oattr = other._units
                if oattr is None:
                    oattr = ur.dimensionless

                eq &= np.all(sattr == oattr)
                if not eq:
                    raise AssertionError(
                        f"attributes `{attr}` are not equals or one is "
                        f"missing: \n{sattr} != {oattr}"
                    )
            else:
                raise AssertionError(f"{other} has no units")

    return True
Esempio n. 13
0
def compare_ndarrays(this, other, approx=False, decimal=6, data_only=False):

    # Comparison based on attributes:
    #        data, dims, mask, labels, units, meta

    from spectrochempy.core.units import ur

    def compare(x, y):
        return _compare(x, y, decimal)

    eq = True
    thistype = this.implements()

    if other.data is None and this.data is None and data_only:
        attrs = ["labels"]
    elif data_only:
        attrs = ["data"]
    else:
        attrs = (
            "data",
            "dims",
            "mask",
            "labels",
            "units",
            "meta",
        )

    for attr in attrs:
        if attr != "units":
            sattr = getattr(this, f"_{attr}")
            if hasattr(other, f"_{attr}"):
                oattr = getattr(other, f"_{attr}")
                if sattr is None and oattr is not None:
                    raise AssertionError(f"`{attr}` of {this} is None.")
                if oattr is None and sattr is not None:
                    raise AssertionError(f"{attr} of {other} is None.")
                if (
                    hasattr(oattr, "size")
                    and hasattr(sattr, "size")
                    and oattr.size != sattr.size
                ):
                    # particular case of mask
                    if attr != "mask":
                        raise AssertionError(f"{thistype}.{attr} sizes are different.")
                    else:
                        assert_array_equal(
                            other.mask,
                            this.mask,
                            f"{this} and {other} masks are different.",
                        )
                if attr in ["data", "mask"]:
                    if approx:
                        assert_array_compare(
                            compare,
                            sattr,
                            oattr,
                            header=(
                                f"{thistype}.{attr} attributes ar"
                                f"e not almost equal to %d decimals" % decimal
                            ),
                            precision=decimal,
                        )
                    else:
                        assert_array_compare(
                            operator.__eq__,
                            sattr,
                            oattr,
                            header=f"{thistype}.{attr} "
                            f"attributes are not "
                            f"equal",
                        )
                else:
                    eq &= np.all(sattr == oattr)
                if not eq:
                    raise AssertionError(
                        f"The {attr} attributes of {this} and {other} are "
                        f"different."
                    )
            else:
                return False
        else:
            # unitless and dimensionless are supposed equal units
            sattr = this._units
            if sattr is None:
                sattr = ur.dimensionless
            if hasattr(other, "_units"):
                oattr = other._units
                if oattr is None:
                    oattr = ur.dimensionless

                eq &= np.all(sattr == oattr)
                if not eq:
                    raise AssertionError(
                        f"attributes `{attr}` are not equals or one is "
                        f"missing: \n{sattr} != {oattr}"
                    )
            else:
                raise AssertionError(f"{other} has no units")

    return True