# Numpy vnl bridge unexpected behavior

**URL:** https://discourse.itk.org/t/numpy-vnl-bridge-unexpected-behavior/1659
**Category:** Uncategorized
**Created:** [March 13, 2019, 2:27pm UTC](https://discourse.itk.org/t/numpy-vnl-bridge-unexpected-behavior/1659 "2019-03-13T14:27:45Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)
#### Post date: [March 13, 2019, 2:27pm UTC](https://discourse.itk.org/t/numpy-vnl-bridge-unexpected-behavior/1659/1 "2019-03-13T14:27:45Z")

</div>

Hi,  
I tend to write too many things on the same line in Python and this caused an unexpected behavior for the numpy vnl bridge. It occurred in a different context but I have the following minimal example

```python
import itk
                                                                                                                                                                                                                                                                                                                                                                                                                                         
m = itk.Matrix[itk.D,4,4]()
m.SetIdentity()
m2 = m+m
print(itk.GetArrayFromVnlMatrix((m+m).GetVnlMatrix().as_matrix()))
print(itk.GetArrayFromVnlMatrix(m2.GetVnlMatrix().as_matrix()))

```

which should show the same two matrices but actually shows

```auto
[[4.68850067e-310 0.00000000e+000 0.00000000e+000 0.00000000e+000]
 [0.00000000e+000 2.00000000e+000 0.00000000e+000 0.00000000e+000]
 [0.00000000e+000 0.00000000e+000 2.00000000e+000 0.00000000e+000]
 [0.00000000e+000 0.00000000e+000 0.00000000e+000 2.00000000e+000]]
[[2. 0. 0. 0.]
 [0. 2. 0. 0.]
 [0. 0. 2. 0.]
 [0. 0. 0. 2.]]

```

Any clue what’s going on and if this can and should be corrected? It seems to be some memory release before the deep copy of the numpy array but I could not see what was wrong in the bridge code.  
Thanks in advance!  
Simon

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [March 15, 2019, 9:42pm UTC](https://discourse.itk.org/t/numpy-vnl-bridge-unexpected-behavior/1659/2 "2019-03-15T21:42:06Z")

</div>

@fbudin any ideas?

---

<div class="post-metadata">

### Author: ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)
#### Post date: [March 18, 2019, 8:37pm UTC](https://discourse.itk.org/t/numpy-vnl-bridge-unexpected-behavior/1659/3 "2019-03-18T20:37:53Z")

</div>

No idea, but it is worth the look. Interestingly enough, since the implementation between images and VNL matrices are similar for the NumPy bridge, this problem could potentially arise with images…

---

<div class="post-metadata">

### Author: ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)
#### Post date: [March 18, 2019, 8:50pm UTC](https://discourse.itk.org/t/numpy-vnl-bridge-unexpected-behavior/1659/4 "2019-03-18T20:50:07Z")

</div>

As a follow up, trying:

```
In [7]: a=(m+m).GetVnlMatrix()

In [8]: print(itk.GetArrayFromVnlMatrix(a.as_matrix()))
[[4.65803687e-310 6.90632463e-310 6.90632461e-310 6.90632465e-310]
 [6.90632463e-310 6.90632463e-310 6.90632464e-310 6.90632464e-310]
 [6.90632463e-310 6.90632464e-310 6.90632463e-310 6.90632463e-310]
 [6.90632462e-310 6.90632462e-310 6.90632461e-310 6.90632464e-310]]
```

also returns garbage, which makes me think that the problem is in `GetVnlMatrix()`, not in the bridge part.

This is most likely due to the return by reference [here](https://github.com/InsightSoftwareConsortium/ITK/blob/master/Modules/Core/Common/include/itkMatrix.h#L168-L177).

---

<div class="post-metadata">

### Author: ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)
#### Post date: [March 19, 2019, 1:03pm UTC](https://discourse.itk.org/t/numpy-vnl-bridge-unexpected-behavior/1659/5 "2019-03-19T13:03:24Z")

</div>

This [PR](https://github.com/InsightSoftwareConsortium/ITK/pull/601) solves the problem on my computer. @simon.rit: Could you test it on your machine? It is not the solution I wanted to implement (I wanted to use a SWIG `typemap`), but I couldn’t get the other solution to work. So I think that for now this could be a reasonable patch.

If this works for you, would you mind improving the PR with a test?

Thanks!

---

<div class="post-metadata">

### Author: ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)
#### Post date: [March 19, 2019, 7:47pm UTC](https://discourse.itk.org/t/numpy-vnl-bridge-unexpected-behavior/1659/6 "2019-03-19T19:47:14Z")

</div>

Great, thanks a lot for your efforts. Yes, I’ll test and provide a test. I now understand the reference issue and can provide a simpler example for the problem, without some one line operation:

```auto
>>> m = itk.Matrix[itk.D,3,3]()
>>> m.SetIdentity()
>>> v = m.GetVnlMatrix()
>>> del m
>>> print(itk.GetArrayFromVnlMatrix(v.as_matrix()))
[[4.65884433e-310 0.00000000e+000 0.00000000e+000]
 [0.00000000e+000 1.00000000e+000 0.00000000e+000]
 [0.00000000e+000 0.00000000e+000 1.00000000e+000]]

```

I’ll base my test on this, just the time to recompile the wrapping with your master branch and your PR.

---

<div class="post-metadata">

### Author: ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)
#### Post date: [March 20, 2019, 12:32am UTC](https://discourse.itk.org/t/numpy-vnl-bridge-unexpected-behavior/1659/7 "2019-03-20T00:32:35Z")

</div>

I created another PR to add helper functions to convert NumPy array to ITK matrices (and back) directly, without having to worry about VNL matrices:

> <https://github.com/InsightSoftwareConsortium/ITK/pull/603>

---

<div class="post-metadata">

### Author: ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)
#### Post date: [March 20, 2019, 8:48am UTC](https://discourse.itk.org/t/numpy-vnl-bridge-unexpected-behavior/1659/8 "2019-03-20T08:48:03Z")

</div>

See my PR

> <https://github.com/InsightSoftwareConsortium/ITK/pull/605>

  
I wasn’t sure if a new commit was in order. Let me know if this is not how it should be done, I’m happy to correct the PR if you tell me how.
