Skip to content

Commit 7b46c7b

Browse files
authored
Merge pull request #61 from QuantStack/improve_data_components
Improve Data and Component widgets
2 parents eaf45ce + 754321f commit 7b46c7b

File tree

3 files changed

+82
-33
lines changed

3 files changed

+82
-33
lines changed

ipygany/ipygany.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ class Component(_GanyWidgetBase):
4747
min = CFloat(allow_none=True, default_value=None)
4848
max = CFloat(allow_none=True, default_value=None)
4949

50-
# TODO Compute min and max in the constructor?
50+
def __init__(self, name, array, **kwargs):
51+
"""Create a new Component instance given its name and array."""
52+
super(Component, self).__init__(name=name, array=array, **kwargs)
53+
54+
if self.min is None:
55+
self.min = np.min(self.array)
56+
57+
if self.max is None:
58+
self.max = np.max(self.array)
5159

5260

5361
class Data(_GanyWidgetBase):
@@ -56,7 +64,11 @@ class Data(_GanyWidgetBase):
5664
_model_name = Unicode('DataModel').tag(sync=True)
5765

5866
name = Unicode().tag(sync=True)
59-
components = List(Instance(Component)).tag(sync=True, **widget_serialization)
67+
components = Union((Dict(), List(Instance(Component)))).tag(sync=True, **widget_serialization)
68+
69+
def __init__(self, name, components, **kwargs):
70+
"""Create a new Data instance given its name and components."""
71+
super(Data, self).__init__(name=name, components=components, **kwargs)
6072

6173
def __getitem__(self, key):
6274
"""Get a component by name or index."""
@@ -71,6 +83,15 @@ def __getitem__(self, key):
7183

7284
raise KeyError('Invalid key {}.'.format(key))
7385

86+
@validate('components')
87+
def _validate_components(self, proposal):
88+
components = proposal['value']
89+
90+
if isinstance(components, dict):
91+
return [Component(name, array) for name, array in components.items()]
92+
93+
return components
94+
7495

7596
def _grid_data_to_data_widget(grid_data):
7697
"""Turn a vtk grid into Data widgets."""
@@ -136,7 +157,7 @@ def _validate_data(self, proposal):
136157
data = proposal['value']
137158

138159
if isinstance(data, dict):
139-
return [Data(name=name, components=components) for name, components in data.items()]
160+
return [Data(name, components) for name, components in data.items()]
140161

141162
return data
142163

tests/test_input.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

tests/test_ipygany.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pytest
2+
3+
import numpy as np
4+
5+
from ipygany import PolyMesh, Data, Component
6+
7+
8+
vertices = np.array([
9+
[0., 0., 0.],
10+
[0., 0., 0.],
11+
[0., 0., 0.],
12+
])
13+
14+
triangles = np.array([
15+
[0, 1, 2],
16+
])
17+
18+
data_1d = Data(name='1d', components=[Component('x', np.array([0., 0., 0.]))])
19+
data_3d = Data('3d', [
20+
Component(name='x', array=np.array([1., 1., 1.])),
21+
Component('y', np.array([2., 2., 2.])),
22+
Component('z', np.array([3., 3., 3.])),
23+
])
24+
25+
26+
def test_data_access():
27+
poly = PolyMesh(vertices=vertices, triangle_indices=triangles, data=[data_1d, data_3d])
28+
29+
assert np.all(np.equal(poly['1d', 'x'].array, np.array([0., 0., 0.])))
30+
assert np.all(np.equal(poly['3d', 'y'].array, np.array([2., 2., 2.])))
31+
32+
33+
def test_mesh_data_creation():
34+
poly = PolyMesh(vertices=vertices, triangle_indices=triangles, data={
35+
'1d': [Component('x', np.array([0., 0., 0.]))],
36+
'2d': [Component('x', np.array([1., 1., 1.])), Component('y', np.array([2., 2., 2.]))]
37+
})
38+
39+
assert np.all(np.equal(poly['1d', 'x'].array, np.array([0., 0., 0.])))
40+
assert np.all(np.equal(poly['2d', 'x'].array, np.array([1., 1., 1.])))
41+
assert np.all(np.equal(poly['2d', 'y'].array, np.array([2., 2., 2.])))
42+
43+
poly = PolyMesh(vertices=vertices, triangle_indices=triangles, data={
44+
'1d': {'x': np.array([0., 0., 0.])},
45+
'2d': {'x': np.array([1., 1., 1.]), 'y': np.array([2., 2., 2.])}
46+
})
47+
48+
assert np.all(np.equal(poly['1d', 'x'].array, np.array([0., 0., 0.])))
49+
assert np.all(np.equal(poly['2d', 'x'].array, np.array([1., 1., 1.])))
50+
assert np.all(np.equal(poly['2d', 'y'].array, np.array([2., 2., 2.])))
51+
52+
53+
def test_component_creation():
54+
comp = Component('z', np.array([1., 2., 3.]))
55+
56+
assert comp.name == 'z'
57+
assert comp.min == 1.
58+
assert comp.max == 3.

0 commit comments

Comments
 (0)