Note that there are some explanatory texts on larger screens.

plurals
  1. POArgument types differ in normal and reflected operator overload (__sub__ / __rsub__)
    text
    copied!<p>How do I get access to the properties of an numpy array after passing it through an righthand operator like <code>__rsub__</code>?</p> <p>I wrote a very simple class in python that defines the two functions:</p> <pre><code>class test(object): def __sub__(self, other): return other def __rsub__(self, other): return other </code></pre> <p>Basically they should do the same. The left-hand operator <code>__sub__</code> works as expected, but it seems that the numpy array is stripped off its properties on the right-hand operator</p> <pre><code>from skimage import data from skimage.color import rgb2gray lena = data.lena() grayLena = rgb2gray(lena) t = test() ## overloaded - operator left_hand = t - grayLena print left_hand # Output: #array([[ 0.60802863, 0.60802863, 0.60779059, ..., 0.64137412, # 0.57998235, 0.46985725], # [ 0.60802863, 0.60802863, 0.60779059, ..., 0.64137412, # 0.57998235, 0.46985725], # [ 0.60802863, 0.60802863, 0.60779059, ..., 0.64137412, # 0.57998235, 0.46985725], # ..., # [ 0.13746353, 0.13746353, 0.16881412, ..., 0.37271804, # 0.35559529, 0.34377725], # [ 0.14617059, 0.14617059, 0.18730588, ..., 0.36788784, # 0.37292549, 0.38467529], # [ 0.14617059, 0.14617059, 0.18730588, ..., 0.36788784, # 0.37292549, 0.38467529]]) right_hand = grayLena - t print right_hand # Output: # array([[0.6080286274509803, 0.6080286274509803, 0.6077905882352941, ..., # 0.6413741176470589, 0.5799823529411765, 0.4698572549019608], # [0.6080286274509803, 0.6080286274509803, 0.6077905882352941, ..., # 0.6413741176470589, 0.5799823529411765, 0.4698572549019608], # [0.6080286274509803, 0.6080286274509803, 0.6077905882352941, ..., # 0.6413741176470589, 0.5799823529411765, 0.4698572549019608], # ..., # [0.1374635294117647, 0.1374635294117647, 0.1688141176470588, ..., # 0.3727180392156863, 0.35559529411764706, 0.34377725490196076], # [0.1461705882352941, 0.1461705882352941, 0.18730588235294118, ..., # 0.3678878431372549, 0.37292549019607846, 0.3846752941176471], # [0.1461705882352941, 0.1461705882352941, 0.18730588235294118, ..., # 0.3678878431372549, 0.37292549019607846, 0.3846752941176471]], dtype=object) </code></pre> <p>So the difference between both operations is that <code>__rsub__</code> receives an array of dtype=object. If I would just set the dtype of this array, everything would work fine.</p> <p>However, it works only with the return value, outside of <code>__rsub__</code>. Inside my <code>__rsub__</code> I get only rubbish, that I cannot convert back, namely if I do</p> <pre><code>npArray = np.array(other, dtype=type(other)) </code></pre> <p>I get a 1D array of the type (floats in my case). But for some reason, the shape information is lost. Has anyone done this or an idea how I can access the original properties of the array (shape and type)?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload