noncontiguous array support

Issue #18 closed
Anonymous created an issue

Passing noncontiguous 1D arrays to pointsToVTK silently gives incorrect output. It would be nice if there was a check or a conversion somewhere to prevent this. Thanks for this very useful code!

#! /usr/bin/env python
from evtk.hl import pointsToVTK
import numpy as np

pos = np.array(
    ((0, 0, 0),
     (1, 0, 0),
     (1, 1, 0),
     (0, 1, 0),
     (0, 0, 1),
     (1, 0, 1),
     (1, 1, 1),
     (0, 1, 1)), dtype=np.float)

x = pos.T[0]
y = pos.T[1]
z = pos.T[2]
print 'F_CONTIGUOUS =', x.flags['F_CONTIGUOUS']
# produces incorrect output on my machine
pointsToVTK('noncontiguous', x, y, z, data=None)

x = np.ascontiguousarray(pos.T[0])
y = np.ascontiguousarray(pos.T[1])
z = np.ascontiguousarray(pos.T[2])
print 'F_CONTIGUOUS =', x.flags['F_CONTIGUOUS']
# produces correct output on my machine
pointsToVTK('contiguous', x, y, z, data=None)

Comments (3)

  1. Matt Wood

    This solved my problem, too - thank you. I have a trivial code to read an ascii file with data in columns, then use pointsToVTK to write the VTU file for ParaView input. W/o 'ascontiguousarray' my points were not reading correctly. This can be confirmed by changing pressure and temp in the example to be say 100*np.random.rand(npoints). Here is the code that works. Thanks again to poster above, and to Paulo for the package - this greatly simplifies my life :-). If I can simplify/optimize further, please let me know. I'm quite new to Python.

    import sys
    from evtk.hl import pointsToVTK
    import numpy as np
    if (len(sys.argv) > 1):
        infile = sys.argv[1]
        tmp = np.loadtxt(infile,unpack=True,usecols=(0,1,2,3))
        x = np.ascontiguousarray(tmp[0])
        y = np.ascontiguousarray(tmp[1])
        z = np.ascontiguousarray(tmp[2])
        temp = np.ascontiguousarray(tmp[3])
        pointsToVTK("junk", x, y, z, data = {"temp" : temp})
        print "syntax: orb2vtu <infile>"
  2. Paulo Herrera repo owner


    EVTK expects arrays that contiguous in memory (see issue #11). I just added a check that will produce an error message when passing non-contiguous arrays.

    Thanks for your comments.


  3. Log in to comment