How to overide numpy ufunc with __array_ufunc__How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory?How to get the current time in PythonHow can I make a time delay in Python?How do I sort a dictionary by value?How to make a chain of function decorators?How to make a flat list out of list of listsHow do I list all files of a directory?
Should I ask for a raise one month before the end of an internship?
What's the point of fighting monsters in Zelda BotW?
Within what limits can the prime minister ask the queen to prorogue parliament?
In Endgame, wouldn't Stark have remembered Hulk busting out of the stairwell?
Group riding etiquette
Is there a word or phrase that means "use other people's wifi or Internet service without consent"?
Why does a sticker slowly peel off, but if it is pulled quickly it tears?
Should I use the words "pyromancy" and "necromancy" even if they don't mean what people think they do?
Half filled water bottle
Employing a contractor proving difficult
How to deal with anxiety caused by dangerous riding conditions stemming from poor lane design and inconsiderate fellow road users?
How many petaflops does it take to land on the moon? What does Artemis need with an Aitken?
Is there anyway to repent for proselytizing for idol worship?
Why can't you say don't instead of won't?
What is the name of this plot that has rows with two connected dots?
Stolen MacBook should I worry about my data?
Notice period 60 days but I need to join in 45 days
Is there a better way to use C# dictionaries than TryGetValue?
Why did the population of Bhutan drop by 70% between 2007 and 2008?
The meaning of asynchronous vs synchronous
Is Nikon D500 a good fit for nature and ambient-lighting portraits and occasional other uses?
Would it be better to write a trilogy over a much longer series?
Could the UK amend the European Withdrawal Act and revoke the Article 50 invocation?
How to prevent a hosting company from accessing a VM's encryption keys?
How to overide numpy ufunc with __array_ufunc__
How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory?How to get the current time in PythonHow can I make a time delay in Python?How do I sort a dictionary by value?How to make a chain of function decorators?How to make a flat list out of list of listsHow do I list all files of a directory?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm trying to implement numpy's ufunc to work with a class, using the __array_ufunc__ method introduced in numpy v1.13.
To simplify, here's what the class could look like :
class toto():
def __init__(self, value, name):
self.value = value
self.name = name
def __add__(self, other):
"""add values and concatenate names"""
return toto(self.value + other.value, self.name + other.name)
def __sub__(self, other):
"""sub values and concatenate names"""
return toto(self.value - other.value, self.name + other.name)
tata = toto(5, "first")
titi = toto(1, "second")
Now if I try to apply np.add between these two, I get the expected result, as np.add relies on add. But if I call say np.exp, I get an error as expected :
>>> np.exp(tata)
AttributeError: 'toto' object has no attribute 'exp'
Now what I would like to do is to "override" all numpy ufuncs to work smoothly with this class without having to redefine every methods (exp(self), log(self), ...) in the class.
I was planning to use numpy ufunc's [__array_ufunc__]1 to do this, but I don't really understand the doc as it doesn't provide a simple exemple of implementation.
If anyone has had any experience with this new functionnality that looks promising, could you provide a simple example ?
python numpy numpy-ufunc
add a comment |
I'm trying to implement numpy's ufunc to work with a class, using the __array_ufunc__ method introduced in numpy v1.13.
To simplify, here's what the class could look like :
class toto():
def __init__(self, value, name):
self.value = value
self.name = name
def __add__(self, other):
"""add values and concatenate names"""
return toto(self.value + other.value, self.name + other.name)
def __sub__(self, other):
"""sub values and concatenate names"""
return toto(self.value - other.value, self.name + other.name)
tata = toto(5, "first")
titi = toto(1, "second")
Now if I try to apply np.add between these two, I get the expected result, as np.add relies on add. But if I call say np.exp, I get an error as expected :
>>> np.exp(tata)
AttributeError: 'toto' object has no attribute 'exp'
Now what I would like to do is to "override" all numpy ufuncs to work smoothly with this class without having to redefine every methods (exp(self), log(self), ...) in the class.
I was planning to use numpy ufunc's [__array_ufunc__]1 to do this, but I don't really understand the doc as it doesn't provide a simple exemple of implementation.
If anyone has had any experience with this new functionnality that looks promising, could you provide a simple example ?
python numpy numpy-ufunc
add a comment |
I'm trying to implement numpy's ufunc to work with a class, using the __array_ufunc__ method introduced in numpy v1.13.
To simplify, here's what the class could look like :
class toto():
def __init__(self, value, name):
self.value = value
self.name = name
def __add__(self, other):
"""add values and concatenate names"""
return toto(self.value + other.value, self.name + other.name)
def __sub__(self, other):
"""sub values and concatenate names"""
return toto(self.value - other.value, self.name + other.name)
tata = toto(5, "first")
titi = toto(1, "second")
Now if I try to apply np.add between these two, I get the expected result, as np.add relies on add. But if I call say np.exp, I get an error as expected :
>>> np.exp(tata)
AttributeError: 'toto' object has no attribute 'exp'
Now what I would like to do is to "override" all numpy ufuncs to work smoothly with this class without having to redefine every methods (exp(self), log(self), ...) in the class.
I was planning to use numpy ufunc's [__array_ufunc__]1 to do this, but I don't really understand the doc as it doesn't provide a simple exemple of implementation.
If anyone has had any experience with this new functionnality that looks promising, could you provide a simple example ?
python numpy numpy-ufunc
I'm trying to implement numpy's ufunc to work with a class, using the __array_ufunc__ method introduced in numpy v1.13.
To simplify, here's what the class could look like :
class toto():
def __init__(self, value, name):
self.value = value
self.name = name
def __add__(self, other):
"""add values and concatenate names"""
return toto(self.value + other.value, self.name + other.name)
def __sub__(self, other):
"""sub values and concatenate names"""
return toto(self.value - other.value, self.name + other.name)
tata = toto(5, "first")
titi = toto(1, "second")
Now if I try to apply np.add between these two, I get the expected result, as np.add relies on add. But if I call say np.exp, I get an error as expected :
>>> np.exp(tata)
AttributeError: 'toto' object has no attribute 'exp'
Now what I would like to do is to "override" all numpy ufuncs to work smoothly with this class without having to redefine every methods (exp(self), log(self), ...) in the class.
I was planning to use numpy ufunc's [__array_ufunc__]1 to do this, but I don't really understand the doc as it doesn't provide a simple exemple of implementation.
If anyone has had any experience with this new functionnality that looks promising, could you provide a simple example ?
python numpy numpy-ufunc
python numpy numpy-ufunc
asked Mar 27 at 21:18
mocquinmocquin
205 bronze badges
205 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
If I extend your class with a __array_ufunc__ method (and __repr__):
class toto():
def __init__(self, value, name):
self.value = value
self.name = name
def __add__(self, other):
"""add values and concatenate names"""
return toto(self.value + other.value, self.name + other.name)
def __sub__(self, other):
"""sub values and concatenate names"""
return toto(self.value - other.value, self.name + other.name)
def __repr__(self):
return f"toto: self.value, self.name"
def __array_ufunc__(self, *args, **kwargs):
print(args)
print(kwargs)
And try some ufunc calls:
In [458]: np.exp(tata)
(<ufunc 'exp'>, '__call__', toto: 5, first)
In [459]: np.exp.reduce(tata)
(<ufunc 'exp'>, 'reduce', toto: 5, first)
In [460]: np.multiply.reduce(tata)
(<ufunc 'multiply'>, 'reduce', toto: 5, first)
In [461]: np.exp.reduce(tata,axes=(1,2))
(<ufunc 'exp'>, 'reduce', toto: 5, first)
'axes': (1, 2)
In [463]: np.exp.reduce(tata,axes=(1,2),out=np.arange(3))
(<ufunc 'exp'>, 'reduce', toto: 5, first)
'axes': (1, 2), 'out': (array([0, 1, 2]),)
That shows the information that your class receives. Evidently you can do what you want that. It can return NotImplemented. I suppose in your case it could apply the first argument to your self.value, or do some custom calculation.
For example if I add
val = args[0].__call__(self.value)
return toto(val, self.name)
I get:
In [468]: np.exp(tata)
(<ufunc 'exp'>, '__call__', toto: 5, first)
Out[468]: toto: 148.4131591025766, first
In [469]: np.sin(tata)
(<ufunc 'sin'>, '__call__', toto: 5, first)
Out[469]: toto: -0.9589242746631385, first
However if I put the object in an array, I still get the method error
In [492]: np.exp(np.array(tata))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-492-4dc37eb0ffe3> in <module>
----> 1 np.exp(np.array(tata))
AttributeError: 'toto' object has no attribute 'exp'
Apparently ufunc on an object dtype array iterates on the elements of the array, expecting to use a 'relevant' method. For np.add (+) it looks for the __add__ method. For np.exp it looks for an exp method. This __array_ufunc__ isn't called.
So it looks like it's intended more for a subclass of ndarray, or something equivalent. You, I think, are trying to implement a class that can work as elements of an object dtype array.
Thanks for your insights. Although they do mention in the doc that this method can be defined to extend any class, ndarray sub-classed or not.
– mocquin
Mar 28 at 10:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55386602%2fhow-to-overide-numpy-ufunc-with-array-ufunc%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I extend your class with a __array_ufunc__ method (and __repr__):
class toto():
def __init__(self, value, name):
self.value = value
self.name = name
def __add__(self, other):
"""add values and concatenate names"""
return toto(self.value + other.value, self.name + other.name)
def __sub__(self, other):
"""sub values and concatenate names"""
return toto(self.value - other.value, self.name + other.name)
def __repr__(self):
return f"toto: self.value, self.name"
def __array_ufunc__(self, *args, **kwargs):
print(args)
print(kwargs)
And try some ufunc calls:
In [458]: np.exp(tata)
(<ufunc 'exp'>, '__call__', toto: 5, first)
In [459]: np.exp.reduce(tata)
(<ufunc 'exp'>, 'reduce', toto: 5, first)
In [460]: np.multiply.reduce(tata)
(<ufunc 'multiply'>, 'reduce', toto: 5, first)
In [461]: np.exp.reduce(tata,axes=(1,2))
(<ufunc 'exp'>, 'reduce', toto: 5, first)
'axes': (1, 2)
In [463]: np.exp.reduce(tata,axes=(1,2),out=np.arange(3))
(<ufunc 'exp'>, 'reduce', toto: 5, first)
'axes': (1, 2), 'out': (array([0, 1, 2]),)
That shows the information that your class receives. Evidently you can do what you want that. It can return NotImplemented. I suppose in your case it could apply the first argument to your self.value, or do some custom calculation.
For example if I add
val = args[0].__call__(self.value)
return toto(val, self.name)
I get:
In [468]: np.exp(tata)
(<ufunc 'exp'>, '__call__', toto: 5, first)
Out[468]: toto: 148.4131591025766, first
In [469]: np.sin(tata)
(<ufunc 'sin'>, '__call__', toto: 5, first)
Out[469]: toto: -0.9589242746631385, first
However if I put the object in an array, I still get the method error
In [492]: np.exp(np.array(tata))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-492-4dc37eb0ffe3> in <module>
----> 1 np.exp(np.array(tata))
AttributeError: 'toto' object has no attribute 'exp'
Apparently ufunc on an object dtype array iterates on the elements of the array, expecting to use a 'relevant' method. For np.add (+) it looks for the __add__ method. For np.exp it looks for an exp method. This __array_ufunc__ isn't called.
So it looks like it's intended more for a subclass of ndarray, or something equivalent. You, I think, are trying to implement a class that can work as elements of an object dtype array.
Thanks for your insights. Although they do mention in the doc that this method can be defined to extend any class, ndarray sub-classed or not.
– mocquin
Mar 28 at 10:59
add a comment |
If I extend your class with a __array_ufunc__ method (and __repr__):
class toto():
def __init__(self, value, name):
self.value = value
self.name = name
def __add__(self, other):
"""add values and concatenate names"""
return toto(self.value + other.value, self.name + other.name)
def __sub__(self, other):
"""sub values and concatenate names"""
return toto(self.value - other.value, self.name + other.name)
def __repr__(self):
return f"toto: self.value, self.name"
def __array_ufunc__(self, *args, **kwargs):
print(args)
print(kwargs)
And try some ufunc calls:
In [458]: np.exp(tata)
(<ufunc 'exp'>, '__call__', toto: 5, first)
In [459]: np.exp.reduce(tata)
(<ufunc 'exp'>, 'reduce', toto: 5, first)
In [460]: np.multiply.reduce(tata)
(<ufunc 'multiply'>, 'reduce', toto: 5, first)
In [461]: np.exp.reduce(tata,axes=(1,2))
(<ufunc 'exp'>, 'reduce', toto: 5, first)
'axes': (1, 2)
In [463]: np.exp.reduce(tata,axes=(1,2),out=np.arange(3))
(<ufunc 'exp'>, 'reduce', toto: 5, first)
'axes': (1, 2), 'out': (array([0, 1, 2]),)
That shows the information that your class receives. Evidently you can do what you want that. It can return NotImplemented. I suppose in your case it could apply the first argument to your self.value, or do some custom calculation.
For example if I add
val = args[0].__call__(self.value)
return toto(val, self.name)
I get:
In [468]: np.exp(tata)
(<ufunc 'exp'>, '__call__', toto: 5, first)
Out[468]: toto: 148.4131591025766, first
In [469]: np.sin(tata)
(<ufunc 'sin'>, '__call__', toto: 5, first)
Out[469]: toto: -0.9589242746631385, first
However if I put the object in an array, I still get the method error
In [492]: np.exp(np.array(tata))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-492-4dc37eb0ffe3> in <module>
----> 1 np.exp(np.array(tata))
AttributeError: 'toto' object has no attribute 'exp'
Apparently ufunc on an object dtype array iterates on the elements of the array, expecting to use a 'relevant' method. For np.add (+) it looks for the __add__ method. For np.exp it looks for an exp method. This __array_ufunc__ isn't called.
So it looks like it's intended more for a subclass of ndarray, or something equivalent. You, I think, are trying to implement a class that can work as elements of an object dtype array.
Thanks for your insights. Although they do mention in the doc that this method can be defined to extend any class, ndarray sub-classed or not.
– mocquin
Mar 28 at 10:59
add a comment |
If I extend your class with a __array_ufunc__ method (and __repr__):
class toto():
def __init__(self, value, name):
self.value = value
self.name = name
def __add__(self, other):
"""add values and concatenate names"""
return toto(self.value + other.value, self.name + other.name)
def __sub__(self, other):
"""sub values and concatenate names"""
return toto(self.value - other.value, self.name + other.name)
def __repr__(self):
return f"toto: self.value, self.name"
def __array_ufunc__(self, *args, **kwargs):
print(args)
print(kwargs)
And try some ufunc calls:
In [458]: np.exp(tata)
(<ufunc 'exp'>, '__call__', toto: 5, first)
In [459]: np.exp.reduce(tata)
(<ufunc 'exp'>, 'reduce', toto: 5, first)
In [460]: np.multiply.reduce(tata)
(<ufunc 'multiply'>, 'reduce', toto: 5, first)
In [461]: np.exp.reduce(tata,axes=(1,2))
(<ufunc 'exp'>, 'reduce', toto: 5, first)
'axes': (1, 2)
In [463]: np.exp.reduce(tata,axes=(1,2),out=np.arange(3))
(<ufunc 'exp'>, 'reduce', toto: 5, first)
'axes': (1, 2), 'out': (array([0, 1, 2]),)
That shows the information that your class receives. Evidently you can do what you want that. It can return NotImplemented. I suppose in your case it could apply the first argument to your self.value, or do some custom calculation.
For example if I add
val = args[0].__call__(self.value)
return toto(val, self.name)
I get:
In [468]: np.exp(tata)
(<ufunc 'exp'>, '__call__', toto: 5, first)
Out[468]: toto: 148.4131591025766, first
In [469]: np.sin(tata)
(<ufunc 'sin'>, '__call__', toto: 5, first)
Out[469]: toto: -0.9589242746631385, first
However if I put the object in an array, I still get the method error
In [492]: np.exp(np.array(tata))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-492-4dc37eb0ffe3> in <module>
----> 1 np.exp(np.array(tata))
AttributeError: 'toto' object has no attribute 'exp'
Apparently ufunc on an object dtype array iterates on the elements of the array, expecting to use a 'relevant' method. For np.add (+) it looks for the __add__ method. For np.exp it looks for an exp method. This __array_ufunc__ isn't called.
So it looks like it's intended more for a subclass of ndarray, or something equivalent. You, I think, are trying to implement a class that can work as elements of an object dtype array.
If I extend your class with a __array_ufunc__ method (and __repr__):
class toto():
def __init__(self, value, name):
self.value = value
self.name = name
def __add__(self, other):
"""add values and concatenate names"""
return toto(self.value + other.value, self.name + other.name)
def __sub__(self, other):
"""sub values and concatenate names"""
return toto(self.value - other.value, self.name + other.name)
def __repr__(self):
return f"toto: self.value, self.name"
def __array_ufunc__(self, *args, **kwargs):
print(args)
print(kwargs)
And try some ufunc calls:
In [458]: np.exp(tata)
(<ufunc 'exp'>, '__call__', toto: 5, first)
In [459]: np.exp.reduce(tata)
(<ufunc 'exp'>, 'reduce', toto: 5, first)
In [460]: np.multiply.reduce(tata)
(<ufunc 'multiply'>, 'reduce', toto: 5, first)
In [461]: np.exp.reduce(tata,axes=(1,2))
(<ufunc 'exp'>, 'reduce', toto: 5, first)
'axes': (1, 2)
In [463]: np.exp.reduce(tata,axes=(1,2),out=np.arange(3))
(<ufunc 'exp'>, 'reduce', toto: 5, first)
'axes': (1, 2), 'out': (array([0, 1, 2]),)
That shows the information that your class receives. Evidently you can do what you want that. It can return NotImplemented. I suppose in your case it could apply the first argument to your self.value, or do some custom calculation.
For example if I add
val = args[0].__call__(self.value)
return toto(val, self.name)
I get:
In [468]: np.exp(tata)
(<ufunc 'exp'>, '__call__', toto: 5, first)
Out[468]: toto: 148.4131591025766, first
In [469]: np.sin(tata)
(<ufunc 'sin'>, '__call__', toto: 5, first)
Out[469]: toto: -0.9589242746631385, first
However if I put the object in an array, I still get the method error
In [492]: np.exp(np.array(tata))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-492-4dc37eb0ffe3> in <module>
----> 1 np.exp(np.array(tata))
AttributeError: 'toto' object has no attribute 'exp'
Apparently ufunc on an object dtype array iterates on the elements of the array, expecting to use a 'relevant' method. For np.add (+) it looks for the __add__ method. For np.exp it looks for an exp method. This __array_ufunc__ isn't called.
So it looks like it's intended more for a subclass of ndarray, or something equivalent. You, I think, are trying to implement a class that can work as elements of an object dtype array.
edited Mar 28 at 2:05
answered Mar 28 at 1:22
hpauljhpaulj
127k7 gold badges101 silver badges179 bronze badges
127k7 gold badges101 silver badges179 bronze badges
Thanks for your insights. Although they do mention in the doc that this method can be defined to extend any class, ndarray sub-classed or not.
– mocquin
Mar 28 at 10:59
add a comment |
Thanks for your insights. Although they do mention in the doc that this method can be defined to extend any class, ndarray sub-classed or not.
– mocquin
Mar 28 at 10:59
Thanks for your insights. Although they do mention in the doc that this method can be defined to extend any class, ndarray sub-classed or not.
– mocquin
Mar 28 at 10:59
Thanks for your insights. Although they do mention in the doc that this method can be defined to extend any class, ndarray sub-classed or not.
– mocquin
Mar 28 at 10:59
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55386602%2fhow-to-overide-numpy-ufunc-with-array-ufunc%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown