mayavi icon indicating copy to clipboard operation
mayavi copied to clipboard

tvtk MVector is broken with numpy >= 1.18.x

Open dhabbyc opened this issue 4 years ago • 7 comments

Current version of class constructor `(tvtk.visual.MVector):


class MVector(numpy.ndarray):
    """MVector class gives many of the functionalities given by Vector of VPython"""
    def __new__(subtype, x = 0.0, y = 0.0, z = 0.0):
        data = numpy.array((x, y, z), float)
         ret = numpy.ndarray.__new__(subtype, shape = (3,), buffer = data, dtype=float, order = False)

The "order" parameter on numpy.ndarray.__new__ should be "C" or "F" or None, according to numpy's documentation. Current version crashes on any call of tvtk.visual.vector:

from tvtk.tools import visual

vec = visual.vector(1,2,3)

Tested on Mayavi 4.6.x, 4.7,x using numpy 1.19.x, 1.17.x, 1.14.x on a Python 3.6.8 (windows).

dhabbyc avatar Dec 22 '20 16:12 dhabbyc

Thanks for the thorough report @dhabbyc but I am unable to reproduce this issue at the moment. Note that I am using an EDM environment and EDM eggs instead of a virtual environment and eggs from PyPI.

> python
Enthought Deployment Manager -- https://www.enthought.com
Python 3.6.12 |Enthought, Inc. (x86_64)| (remotes/origin/update-build-num:20e9974, Sep 16 2020, 22:26:10) [MSC v.1900 64 bit (AMD64)] on win32
>>> import tvtk
>>> from tvtk.tools import visual
>>> visual.vector(1, 2, 3)
MVector([1., 2., 3.])
>>> import numpy
>>> numpy.__version__
'1.17.4'

rahulporuri avatar Dec 23 '20 09:12 rahulporuri

@rahulporuri you are correct. My mistake, I did not check the code on neither 1.7.x nor 1.4.x (only through documentation).

Nevertheless, I can state that on numpy 1.19.0 the crash occurs:

Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tvtk
>>> from tvtk.tools import visual
>>> visual.vector(1,2,3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python36\lib\site-packages\tvtk\tools\visual.py", line 375, in __new__
    order = False)
TypeError: order must be str, not bool
>>> import numpy
>>> numpy.__version__
'1.19.0'
>>> import mayavi
>>> mayavi.__version__
'4.7.2'

dhabbyc avatar Dec 23 '20 12:12 dhabbyc

Nevertheless, I can state that on numpy 1.19.0 the crash occurs:

I can reproduce what you're seeing with python 3.6.12 (via EDM) on windows. Note that I only used pip i.e. pip install numpy==1.19.0 and pip install mayavi afterwards is what I did to reproduce the issue.

But, I don't see this issue with numpy 1.17.4 (installed using pip)

Enthought Deployment Manager -- https://www.enthought.com
Python 3.6.12 |Enthought, Inc. (x86_64)| (remotes/origin/update-build-num:20e9974, Sep 16 2020, 22:26:10) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from tvtk.tools import visual
>>> visual.vector(1, 2, 3)
MVector([1., 2., 3.])
>>> import mayavi
>>> mayavi.__version__
'4.7.2'
>>> import numpy
>>> numpy.__version__
'1.17.4'

xref https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray

rahulporuri avatar Dec 23 '20 13:12 rahulporuri

Finally. Looks like mayavi was depending on behavior that was deprecated in numpy in 1.11.0 - which was finally removed in numpy 1.18.0. Ref https://numpy.org/doc/stable/release/1.18.0-notes.html#expired-deprecations

Array order only accepts ‘C’, ‘F’, ‘A’, and ‘K’. More permissive options were deprecated in NumPy 1.11. (https://github.com/numpy/numpy/pull/14596)

Enthought Deployment Manager -- https://www.enthought.com
Python 3.6.12 |Enthought, Inc. (x86_64)| (remotes/origin/update-build-num:20e9974, Sep 16 2020, 22:26:10) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import mayavi
>>> mayavi.__version__
'4.7.2'
>>> import numpy
>>> numpy.__version__
'1.18.0'
>>> from tvtk.tools import visual
>>> visual.vector(1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\rporuri\.edm\envs\mayavi-test\lib\site-packages\tvtk\tools\visual.py", line 375, in __new__
    order = False)
ValueError: Non-string object detected for the array ordering. Please pass in 'C', 'F', 'A', or 'K' instead

rahulporuri avatar Dec 23 '20 14:12 rahulporuri

@dhabbyc i updated the issue title to reflect the issue better.

rahulporuri avatar Dec 23 '20 14:12 rahulporuri

I'm not entirely sure what the right fix is here. I don't know if we can simply remove order completely or set it to the appropriate values. @prabhuramachandran

rahulporuri avatar Dec 23 '20 14:12 rahulporuri

I downgraded to NumPy 1.17.5, but the problem persists. Is there an easy solution?

giammi56 avatar Oct 10 '22 14:10 giammi56