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

Using numpy arrays as images #17

Closed
tjtuom opened this issue May 10, 2021 · 2 comments · Fixed by #22
Closed

Using numpy arrays as images #17

tjtuom opened this issue May 10, 2021 · 2 comments · Fixed by #22

Comments

@tjtuom
Copy link

tjtuom commented May 10, 2021

I get an exception when using numpy arrays as images with the Image component. I get my image from opencv imread. I guess the problem is that it is trying to compare the arrays when deciding whether to render or not.

Traceback (most recent call last): File "/Users/tjtuom/Code/takomo/experiments/venv/lib/python3.9/site-packages/edifice/base_components.py", line 255, in _mouse_release self._mouse_clicked(ev) File "/Users/tjtuom/Code/takomo/experiments/venv/lib/python3.9/site-packages/edifice/base_components.py", line 260, in _mouse_clicked self._on_click(ev) File "/Users/tjtuom/Code/takomo/annotate/annote_edifice.py", line 56, in <lambda> *[Label(text=f, on_click=lambda ev, f=f: self.on_click(f)) for f in self.props.files] File "/Users/tjtuom/Code/takomo/annotate/annote_edifice.py", line 52, in on_click self.props.on_image_selected(f) File "/Users/tjtuom/Code/takomo/annotate/annote_edifice.py", line 117, in on_image_selected self.current_image = path.join(IMAGE_DIR, f) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/contextlib.py", line 124, in __exit__ next(self.gen) File "/Users/tjtuom/Code/takomo/experiments/venv/lib/python3.9/site-packages/edifice/_component.py", line 373, in render_changes self.set_state(**changes_context) File "/Users/tjtuom/Code/takomo/experiments/venv/lib/python3.9/site-packages/edifice/_component.py", line 408, in set_state raise e File "/Users/tjtuom/Code/takomo/experiments/venv/lib/python3.9/site-packages/edifice/_component.py", line 404, in set_state self._controller._request_rerender([self], kwargs) File "/Users/tjtuom/Code/takomo/experiments/venv/lib/python3.9/site-packages/edifice/app.py", line 178, in _request_rerender render_result = self._render_engine._request_rerender(components) File "/Users/tjtuom/Code/takomo/experiments/venv/lib/python3.9/site-packages/edifice/engine.py", line 431, in _request_rerender commands = self._gen_commands(widget_trees, render_context) File "/Users/tjtuom/Code/takomo/experiments/venv/lib/python3.9/site-packages/edifice/engine.py", line 413, in _gen_commands commands.extend(widget_tree.gen_qt_commands(render_context)) File "/Users/tjtuom/Code/takomo/experiments/venv/lib/python3.9/site-packages/edifice/engine.py", line 75, in gen_qt_commands rendered = child.gen_qt_commands(render_context) File "/Users/tjtuom/Code/takomo/experiments/venv/lib/python3.9/site-packages/edifice/engine.py", line 75, in gen_qt_commands rendered = child.gen_qt_commands(render_context) File "/Users/tjtuom/Code/takomo/experiments/venv/lib/python3.9/site-packages/edifice/engine.py", line 75, in gen_qt_commands rendered = child.gen_qt_commands(render_context) File "/Users/tjtuom/Code/takomo/experiments/venv/lib/python3.9/site-packages/edifice/engine.py", line 82, in gen_qt_commands new_props = PropsDict({k: v for k, v in self.component.props._items if k not in old_props or _try_neq(old_props[k], v)}) File "/Users/tjtuom/Code/takomo/experiments/venv/lib/python3.9/site-packages/edifice/engine.py", line 82, in <dictcomp> new_props = PropsDict({k: v for k, v in self.component.props._items if k not in old_props or _try_neq(old_props[k], v)}) ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

@tjtuom
Copy link
Author

tjtuom commented May 11, 2021

So the problem is the or in new_props = PropsDict({k: v for k, v in self.component.props._items if k not in old_props or _try_neq(old_props[k], v)}). When comparing numpy arrays_try_neg returns another array instead of a boolean which causes the or operator to produce a value error.

>>> import numpy as np
>>> a = np.array([1, 2, 3])
>>> b = np.array([1,2, 3])

>>> a != b
array([False, False, False])

>>> a or b
Traceback (most recent call last):
File "", line 1, in
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I solved it with:
def _try_neq(a, b): try: if isinstance(a, np.ndarray) and isinstance(b, np.ndarray): return not np.array_equal(a, b) else: return a != b except: return a is not b

@fding
Copy link
Member

fding commented Jun 9, 2021

Thanks for the fix! I'll patch it in when I get back from vacation.

@fding fding linked a pull request Jun 17, 2021 that will close this issue
@fding fding closed this as completed in #22 Jun 17, 2021
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

Successfully merging a pull request may close this issue.

2 participants