Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k3d: TraitError: colors has wrong size: 4000000 (1000000 required) #198

Open
rikigigi opened this issue Aug 24, 2020 · 2 comments
Open

k3d: TraitError: colors has wrong size: 4000000 (1000000 required) #198

rikigigi opened this issue Aug 24, 2020 · 2 comments

Comments

@rikigigi
Copy link

Copying and paste the example https://github.com/marcomusy/vedo/blob/master/examples/basic/manypoints.py does not work (is that supposed to work in a jupyter notebook?). By the way, is the k3d backend supporting transparency?

"""Colorize a large cloud of 1M points by passing
colors and transparencies in the format (R,G,B,A)
"""
from vedo import *
import numpy as np
import time

settings.renderPointsAsSpheres = False
settings.pointSmoothing = False
settings.xtitle = 'red axis'
settings.ytitle = 'green axis'
settings.ztitle = 'blue*alpha axis'

N = 1000000

pts = np.random.rand(N, 3)
RGB = pts * 255
Alpha = pts[:, 2] * 255
RGBA = np.c_[RGB, Alpha]  # concatenate

print("clock starts")
t0 = time.time()

# passing c in format (R,G,B,A) is ~50x faster
pts = Points(pts, r=2, c=RGBA) #fast
#pts = Points(pts, r=2, c=pts, alpha=pts[:, 2]) #slow

t1 = time.time()
print("-> elapsed time:", t1-t0, "seconds for N:", N)

show(pts, __doc__, axes=True)

into a jupyter notebook produces the error


clock starts
-> elapsed time: 1.2373969554901123 seconds for N: 1000000
---------------------------------------------------------------------------
TraitError                                Traceback (most recent call last)
<ipython-input-1-2c67a1dbfaf3> in <module>
     29 print("-> elapsed time:", t1-t0, "seconds for N:", N)
     30 
---> 31 show(pts, __doc__, axes=True)

~/.virtualenvs/aiida/lib/python3.7/site-packages/vedo/plotter.py in show(*actors, **options)
    310             bg2=bg2,
    311             axes=axes,
--> 312             q=q,
    313         )
    314 

~/.virtualenvs/aiida/lib/python3.7/site-packages/vedo/plotter.py in show(self, *actors, **options)
   1550         #########################################################################
   1551         if settings.notebookBackend and settings.notebookBackend != "panel" and settings.notebookBackend != "2d":
-> 1552             return backends.getNotebookBackend(actors2show, zoom, viewup)
   1553         #########################################################################
   1554 

~/.virtualenvs/aiida/lib/python3.7/site-packages/vedo/backends.py in getNotebookBackend(actors2show, zoom, viewup)
    197                                   shader="3d",
    198                                   point_size=iap.GetPointSize()*sqsize/800,
--> 199                                   name=name,
    200                                   #compression_level=9,
    201                                   )

~/.virtualenvs/aiida/lib/python3.7/site-packages/k3d/factory.py in points(positions, colors, color, point_size, shader, opacity, opacities, name, compression_level, mesh_detail, **kwargs)
    262                opacity=opacity, opacities=opacities,
    263                mesh_detail=mesh_detail, name=name,
--> 264                compression_level=compression_level),
    265         **kwargs
    266     )

~/.virtualenvs/aiida/lib/python3.7/site-packages/k3d/objects.py in __init__(self, **kwargs)
    439 
    440     def __init__(self, **kwargs):
--> 441         super(Points, self).__init__(**kwargs)
    442 
    443         self.set_trait('type', 'Points')

~/.virtualenvs/aiida/lib/python3.7/site-packages/k3d/objects.py in __init__(self, **kwargs)
     79         self.id = id(self)
     80 
---> 81         super(Drawable, self).__init__(**kwargs)
     82 
     83     def __iter__(self):

~/.virtualenvs/aiida/lib/python3.7/site-packages/ipywidgets/widgets/widget.py in __init__(self, **kwargs)
    410         """Public constructor"""
    411         self._model_id = kwargs.pop('model_id', None)
--> 412         super(Widget, self).__init__(**kwargs)
    413 
    414         Widget._call_widget_constructed(self)

~/.virtualenvs/aiida/lib/python3.7/site-packages/traitlets/traitlets.py in __init__(self, *args, **kwargs)
    998                 else:
    999                     # passthrough args that don't set traits to super
-> 1000                     super_kwargs[key] = value
   1001         try:
   1002             super(HasTraits, self).__init__(*super_args, **super_kwargs)

~/miniconda3/lib/python3.7/contextlib.py in __exit__(self, type, value, traceback)
    117         if type is None:
    118             try:
--> 119                 next(self.gen)
    120             except StopIteration:
    121                 return False

~/.virtualenvs/aiida/lib/python3.7/site-packages/traitlets/traitlets.py in hold_trait_notifications(self)
   1120                                 self._trait_values.pop(name)
   1121                 cache = {}
-> 1122                 raise e
   1123             finally:
   1124                 self._cross_validation_lock = False

~/.virtualenvs/aiida/lib/python3.7/site-packages/traitlets/traitlets.py in hold_trait_notifications(self)
   1106                 for name in list(cache.keys()):
   1107                     trait = getattr(self.__class__, name)
-> 1108                     value = trait._cross_validate(self, getattr(self, name))
   1109                     self.set_trait(name, value)
   1110             except TraitError as e:

~/.virtualenvs/aiida/lib/python3.7/site-packages/traitlets/traitlets.py in _cross_validate(self, obj, value)
    597         if self.name in obj._trait_validators:
    598             proposal = Bunch({'trait': self, 'value': value, 'owner': obj})
--> 599             value = obj._trait_validators[self.name](obj, proposal)
    600         elif hasattr(obj, '_%s_validate' % self.name):
    601             meth_name = '_%s_validate' % self.name

~/.virtualenvs/aiida/lib/python3.7/site-packages/traitlets/traitlets.py in __call__(self, *args, **kwargs)
    905         """Pass `*args` and `**kwargs` to the handler's function if it exists."""
    906         if hasattr(self, 'func'):
--> 907             return self.func(*args, **kwargs)
    908         else:
    909             return self._init_call(*args, **kwargs)

~/.virtualenvs/aiida/lib/python3.7/site-packages/k3d/objects.py in _validate_colors(self, proposal)
    451         actual = proposal['value'].size
    452         if actual != 0 and required != actual:
--> 453             raise TraitError('colors has wrong size: %s (%s required)' % (actual, required))
    454         return proposal['value']
    455 

TraitError: colors has wrong size: 4000000 (1000000 required)

installed k3d package:
K3D-2.9.0-py2.py3-none-any.whl
ipydatawidgets-4.0.1-py2.py3-none-any.whl
vedo package:

$ pip show vedo
Name: vedo
Version: 2020.3.4
.
.
.

python 3.7.3

@marcomusy
Copy link
Owner

Hi Riccardo,
thanks for reporting this, I'm afraid that k3d cannot cope with that for various reasons (nr of points, trasparency).
The only way round this is to allow vedo to pop up an external window by setting:

from vedo import embedWindow
embedWindow(False)

@dvillevald
Copy link

dvillevald commented Nov 11, 2020

I get the same error when I try to run this example in Jupyter (embedWindow(False) does not help, unfortunately.) However, the example works fine in Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants