# Problems using vtkFloatArray?

**URL:** https://discourse.vtk.org/t/problems-using-vtkfloatarray/12416
**Category:** Support
**Created:** [October 4, 2023, 3:31pm UTC](https://discourse.vtk.org/t/problems-using-vtkfloatarray/12416 "2023-10-04T15:31:48Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [October 4, 2023, 3:31pm UTC](https://discourse.vtk.org/t/problems-using-vtkfloatarray/12416/1 "2023-10-04T15:31:48Z")

</div>

Ok - I read in some data from a file using:

```auto
import numpy as np
with open("binary_stuff.dat", "rb") as f:
    d = np.fromfile(f, dtype=">f4", count=1024)

# verify no NaNs, etc:
assert not np.any(np.isnan(d))
print(f"(min, max): ({d.min()}, {d.max()})")

# which returns:
# -769.35077, 142235120000.0

```

Note: the datafile is _big endian_, while my computer is _little endian_.  
I had been trying to add this to a VTK dataset using something like this:

```auto
a = vtk.vtkFloatArray()
a.SetNumberOfComponents(1)
a.SetNumberOfTuples(d.size)
a.SetName("Weirdness")
for s in d.ravel():
    a.InsertNextValue(s)

print(a.GetValueRange())
(-1.9957235928163046e+38, 1.3434351138279377e+38)

# I also tried:
# a = numpy_to_vtk(d.ravel())

```

I’m sure whatever it is that is messing me up, is likely simple … I hope.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [October 4, 2023, 4:37pm UTC](https://discourse.vtk.org/t/problems-using-vtkfloatarray/12416/2 "2023-10-04T16:37:01Z")

</div>

It’s because you call `SetNumberOfTuples()` before calling `InsertNextValue()`.

Calling `SetNumberOfTuples(n)` will set the size of the array to `n`. All `n` values in the array will be left uninitialized. Then, `InsertNextValue(x)` will add `x` to the end of the array, giving an array size of `n+1`.

Here are two simple ways to fix this:

1. remove `SetNumberOfTuples()`, or
2. use `for i,s in enumerate(d.ravel()): a.SetValue(i, s)`

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [October 4, 2023, 5:53pm UTC](https://discourse.vtk.org/t/problems-using-vtkfloatarray/12416/3 "2023-10-04T17:53:06Z")

</div>

That worked! Thanks!

Any idea why `a = numpy_to_vtk(d.ravel())` didn’t work?

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [October 4, 2023, 7:33pm UTC](https://discourse.vtk.org/t/problems-using-vtkfloatarray/12416/4 "2023-10-04T19:33:14Z")

</div>

> [@rexthor](#):
>
> Any idea why `a = numpy_to_vtk(d.ravel())` didn’t work?

Probably because `d.ravel()` is a temporary array. The purpose of `numpy_to_vtk()` is to create VTK array whose contents are just a pointer to the internal buffer of the numpy array. So if the numpy array is deleted or resized, then the VTK array ends up with a dangling pointer.

It should work if you do this, which will force VTK to make a copy of the array memory:

```python
    a = numpy_to_vtk(d.ravel(), deep=True)

```

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [October 4, 2023, 7:45pm UTC](https://discourse.vtk.org/t/problems-using-vtkfloatarray/12416/5 "2023-10-04T19:45:37Z")

</div>

Ok - the first method you mentioned, worked, but I tried three different ways with `numpy_to_vtk`:

Just using deep copy:

```auto
a = numpy_to_vtk(d.ravel(), deep=True)
DEBUG:root:b'e': min:-0.4677600860595703, max:1000000.0
DEBUG:root:vtk b'e': min:-3.385234763601098e+38, max:3.3478228449666822e+38

```

Storing a reference to the `ravel`-ed array:

```auto
b = d.ravel()
a = numpy_to_vtk(b)
DEBUG:root:b'e': min:-0.4677600860595703, max:1000000.0
DEBUG:root:vtk b'e': min:-3.385234763601098e+38, max:3.3478228449666822e+38

```

Storing a reference to the `ravel`-ed array and deep copying:

```auto
b = d.ravel()
a = numpy_to_vtk(b, deep=True)
DEBUG:root:b'e': min:-0.46879827976226807, max:1282962.25
DEBUG:root:vtk b'e': min:-3.388351155836699e+38, max:3.324693596351062e+38

```

Also, even with the first working(?) example, when I pull up the resulting file in ParaView, the ranges are all +/- 1e38 …

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [October 4, 2023, 8:03pm UTC](https://discourse.vtk.org/t/problems-using-vtkfloatarray/12416/6 "2023-10-04T20:03:53Z")

</div>

Try with a simple hand-built array to see if that works:

```plaintext
b = np.array([1, 2], dtype=np.float32)
a = numpy_to_vtk(b)
print(a.GetValue(0), a.GetValue(1))

```

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [October 4, 2023, 8:07pm UTC](https://discourse.vtk.org/t/problems-using-vtkfloatarray/12416/7 "2023-10-04T20:07:50Z")

</div>

Yours works fine:

```auto
rexthor@workstation:~ $ python3
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> from vtk.util.numpy_support import numpy_to_vtk
>>> b = np.array([1, 2], dtype=np.float32)
>>> a = numpy_to_vtk(b)
>>> a.GetValue(0)
1.0
>>> a.GetValue(1)
2.0
>>> a.GetValueRange()
(1.0, 2.0)

```

However, when you throw big-endian stuff into the mix:

```auto
import struct
buffer = struct.pack(">ff", 1, 2)
a = np.frombuffer(buffer, dtype=">f4")
c = numpy_to_vtk(a)
c.GetValueRange()

(8.96831017167883e-44, 4.600602988224807e-41)

```

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [October 4, 2023, 8:17pm UTC](https://discourse.vtk.org/t/problems-using-vtkfloatarray/12416/9 "2023-10-04T20:17:12Z")

</div>

Try byteswapping the numpy array before converting to a VTK array.

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [October 4, 2023, 8:24pm UTC](https://discourse.vtk.org/t/problems-using-vtkfloatarray/12416/10 "2023-10-04T20:24:00Z")

</div>

Wow - I didn’t even know numpy did that …

```auto
import numpy as np
from vtk.util.numpy_support import numpy_to_vtk
import struct
buffer = struct.pack(">ff", 1, 2)
a = np.frombuffer(buffer, dtype=">f4").byteswap()
c = numpy_to_vtk(a)
print(c.GetValueRange())

```

That gives the correct value range, and, fixes another issue I was having … Thanks!
