Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shape attribute to numpy's equivalent of array scalar type #156

Open
arkanoid87 opened this issue Apr 24, 2022 · 1 comment
Open

Add shape attribute to numpy's equivalent of array scalar type #156

arkanoid87 opened this issue Apr 24, 2022 · 1 comment

Comments

@arkanoid87
Copy link

I'm trying to use numpy + uncertainties + pint library together

To accomplish this I've wrapped test to check bind spots in numpy python array protocol when using pint alone, uncertainties alone and both combined

on the uncertainties side, shape attribute seems missing for array scalar type

see:
https://numpy.org/doc/stable/reference/arrays.interface.html#python-side
https://numpy.org/doc/stable/glossary.html#term-array_like

Test summary

========================================================================================= short test summary info ==========================================================================================
FAILED science_check.py::test_numpy_array_interface[pint-scalar-typestr] - AssertionError: typestr not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[pint-scalar-version] - AssertionError: version not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[pint-d0-typestr] - AssertionError: typestr not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[pint-d0-version] - AssertionError: version not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[pint-d1-typestr] - AssertionError: typestr not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[pint-d1-version] - AssertionError: version not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[pint-dn-typestr] - AssertionError: typestr not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[pint-dn-version] - AssertionError: version not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[uncertainties-scalar-shape] - AssertionError: shape not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[uncertainties-scalar-typestr] - AssertionError: typestr not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[uncertainties-scalar-version] - AssertionError: version not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[pint+uncertainties-scalar-shape] - AssertionError: shape not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[pint+uncertainties-scalar-typestr] - AssertionError: typestr not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[pint+uncertainties-scalar-version] - AssertionError: version not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[pint+uncertainties-d0-typestr] - AssertionError: typestr not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[pint+uncertainties-d0-version] - AssertionError: version not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[pint+uncertainties-d1-typestr] - AssertionError: typestr not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[pint+uncertainties-d1-version] - AssertionError: version not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[pint+uncertainties-dn-typestr] - AssertionError: typestr not in attributes nor __array_interface__
FAILED science_check.py::test_numpy_array_interface[pint+uncertainties-dn-version] - AssertionError: version not in attributes nor __array_interface__

run with pytest thisfile.py

import pytest
import numpy as np
from uncertainties import ufloat
from pint import UnitRegistry



ureg = UnitRegistry()
Q_ = ureg.Quantity


# ------------------------------------


def add(a):
    return a + a


def mul(a):
    return a * a


def sum(a):
    return np.sum(a)


def div(a):
    return np.divide(a, a)


# ------------------------------------


NUMPY_ARRAY_SCALAR = np.float_(15.0)
UNCERTAINTIES_ARRAY_SCALAR = ufloat(NUMPY_ARRAY_SCALAR,  np.float_(1.0))

NUMPY_D0_ARRAY = np.array(NUMPY_ARRAY_SCALAR)
UNCERTAINTIES_D0_ARRAY = np.array(UNCERTAINTIES_ARRAY_SCALAR)

NUMPY_D1_ARRAY = np.tile(NUMPY_ARRAY_SCALAR, 5)
UNCERTAINTIES_D1_ARRAY = np.tile(UNCERTAINTIES_ARRAY_SCALAR, 5)

NUMPY_DN_ARRAY = np.tile(NUMPY_ARRAY_SCALAR, (3,3))
UNCERTAINTIES_DN_ARRAY = np.tile(UNCERTAINTIES_ARRAY_SCALAR, (3,3))


NUMPY_PARAMS = [
    NUMPY_ARRAY_SCALAR,
    NUMPY_D0_ARRAY,
    NUMPY_D1_ARRAY,
    NUMPY_DN_ARRAY,
]

PINT_PARAMS = [
    Q_(NUMPY_ARRAY_SCALAR, "meter"),
    Q_(NUMPY_D0_ARRAY, "meter"),
    Q_(NUMPY_D1_ARRAY, "meter"),
    Q_(NUMPY_DN_ARRAY, "meter"),
]

UNCERTAINTIES_PARAMS = [
    UNCERTAINTIES_ARRAY_SCALAR,
    UNCERTAINTIES_D0_ARRAY,
    UNCERTAINTIES_D1_ARRAY,
    UNCERTAINTIES_DN_ARRAY,
]

PINT_UNCERTAINTIES_PARAMS = [
    Q_(UNCERTAINTIES_ARRAY_SCALAR, "meter"),
    Q_(UNCERTAINTIES_D0_ARRAY, "meter"),
    Q_(UNCERTAINTIES_D1_ARRAY, "meter"),
    Q_(UNCERTAINTIES_DN_ARRAY, "meter"),
]

ALL_PARAMS = NUMPY_PARAMS + PINT_PARAMS + UNCERTAINTIES_PARAMS + PINT_UNCERTAINTIES_PARAMS


GROUP = ["numpy", "pint", "uncertainties", "pint+uncertainties"]
SUBGROUP = ["scalar", "d0", "d1", "dn"]
IDS = [f"{g1}-{g2}" for g1 in GROUP for g2 in SUBGROUP]


@pytest.fixture(params=NUMPY_PARAMS, ids=SUBGROUP)
def numpy_array(request):
    return request.param


@pytest.fixture(params=PINT_PARAMS, ids=SUBGROUP)
def pint_array(request):
    return request.param


@pytest.fixture(params=UNCERTAINTIES_PARAMS, ids=SUBGROUP)
def uncertainties_array(request):
    return request.param


@pytest.fixture(params=PINT_UNCERTAINTIES_PARAMS, ids=SUBGROUP)
def pint_uncertainties_array(request):
    return request.param


@pytest.fixture(params=[add, mul, sum, div])
def operation(request):
    return request.param


@pytest.fixture(params=["shape", "typestr", "version"])
def interface_key(request):
    return request.param


@pytest.fixture(params=ALL_PARAMS, ids=IDS)
def array(request):
    return request.param


# ------------------------------------


# https://numpy.org/doc/stable/reference/arrays.interface.html#python-side
def test_numpy_array_interface(array, interface_key):
    assert hasattr(array, interface_key) \
        or (hasattr(array, "__array_interface__") and interface_key in array.__array_interface__),\
            f"{interface_key} not in attributes nor __array_interface__"


def test_numpy_array(array, operation):
    result = operation(array)
    print(result)
@lebigot
Copy link
Collaborator

lebigot commented Apr 24, 2022

I haven't looked at the details yet, but uncertainties doesn't have a dedicated NumPy array type (I wrote it before the array interface existed or was stable, and so NumPy delegates calculations individually to each array value): I'm guessing that the issue lies with Pint. Did you report this in this project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants