Giter VIP home page Giter VIP logo

Comments (2)

RJaBi avatar RJaBi commented on June 12, 2024

I've just come across an issue with numpy's gradient. I can fix it for my use case, but not for the general.

f = np.array([1, 2, 4, 7, 11, 16], dtype=float) * units.one
print( np.gradient(f) )

gives the error

File "analysis.py", line 151, in getW0
print( np.gradient(f) )
File "<array_function internals>", line 5, in gradient
File "/home/ryan/Documents/GithubProjects/ILAFF/ILAFF-F/src/ilaff/units/quantity.py", line 323, in array_function
return func_units(args, kwargs)
File "/home/ryan/Documents/GithubProjects/ILAFF/ILAFF-F/src/ilaff/units/quantity.py", line 1057, in wrapper
raise RuntimeError(f"Return value of {func.name} does not match expected format")
RuntimeError: Return value of gradient does not match expected format

If I specify the spacing for the gradient (between f values), i.e.

f = np.array([1, 2, 4, 7, 11, 16], dtype=float) * units.one
print( np.gradient(f,1) )

it works. However this does not seem to apply to the two-dimensional case:

    dx = 2. * units.one
    y = [1., 1.5, 3.5] * units.one
    f = np.array([[1, 2, 6], [3, 4, 5]], dtype=float) * units.one
    print( np.gradient(f, dx, y) )

which has the same error as before.

The first of these problems can be fixed by changing the gradient array func in quantity.py to

@_array_func(numpy.gradient)
def gradient(f: Quantity, *varargs: Quantity, axis: Optional[Union[int, Sequence[int]]] = None, edge_order: int = 1) -> List[Tuple[Dimension, Scale]]:
    if len(varargs) == 1:
        shape = numpy.shape(f.value)
        N = len(shape)
        return [_divide_units(numpy.gradient, f, varargs[0], labels={1: "spacing"})] * N
    elif len(varargs) == 0:
	shape = numpy.shape(f.value)
	N = len(shape)
        return [_divide_units(numpy.gradient, f, labels={1: "spacing"})] * N
    else:
        return [_divide_units(numpy.gradient, f, arg, labels={1: "spacing"}) for arg in varargs]

I.e. the len(varargs) == 0 section.

from ilaff.

FinnStokes avatar FinnStokes commented on June 12, 2024

Nice report, I've fixed both problems in 20a17d6

from ilaff.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.