Skip to content

Commit afe8b56

Browse files
committed
Merge pull request plotly#246 from plotly/streamline
TraceFactory
2 parents b18e871 + 053e4b6 commit afe8b56

File tree

6 files changed

+990
-1
lines changed

6 files changed

+990
-1
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from unittest import TestCase
2+
from plotly.graph_objs import graph_objs, Line
3+
from plotly.exceptions import PlotlyError
4+
5+
import plotly.tools as tls
6+
import math
7+
from nose.tools import raises
8+
9+
10+
class TestQuiver(TestCase):
11+
12+
def test_unequal_xy_length(self):
13+
14+
# check: PlotlyError if x and y are not the same length
15+
16+
kwargs = {'x': [1, 2], 'y': [1], 'u': [1, 2], 'v': [1, 2]}
17+
self.assertRaises(PlotlyError, tls.TraceFactory.create_quiver,
18+
**kwargs)
19+
20+
def test_wrong_scale(self):
21+
22+
# check: ValueError if scale is <= 0
23+
24+
kwargs = {'x': [1, 2], 'y': [1, 2],
25+
'u': [1, 2], 'v': [1, 2],
26+
'scale': -1}
27+
self.assertRaises(ValueError, tls.TraceFactory.create_quiver,
28+
**kwargs)
29+
30+
def test_wrong_arrow_scale(self):
31+
32+
# check: ValueError if arrow_scale is <= 0
33+
34+
kwargs = {'x': [1, 2], 'y': [1, 2],
35+
'u': [1, 2], 'v': [1, 2],
36+
'arrow_scale': -1}
37+
self.assertRaises(ValueError, tls.TraceFactory.create_quiver,
38+
**kwargs)
39+
40+
def test_one_arrow(self):
41+
42+
# we should be able to create a single arrow using create_quiver
43+
44+
quiver = tls.TraceFactory.create_quiver(x=[1], y=[1],
45+
u=[1], v=[1],
46+
scale=1)
47+
expected_quiver = {
48+
'y': [1, 2, None, 1.615486170766527, 2, 1.820698256761928, None],
49+
'x': [1, 2, None, 1.820698256761928, 2, 1.615486170766527, None],
50+
'type': 'scatter',
51+
'mode': 'lines'
52+
}
53+
self.assertEqual(quiver, expected_quiver)
54+
55+
def test_more_kwargs(self):
56+
57+
# we should be able to create 2 arrows and change the arrow_scale,
58+
# angle, and arrow using create_quiver
59+
60+
quiver = tls.TraceFactory.create_quiver(x=[1, 2],
61+
y=[1, 2],
62+
u=[math.cos(1),
63+
math.cos(2)],
64+
v=[math.sin(1),
65+
math.sin(2)],
66+
arrow_scale=.4,
67+
angle=math.pi / 6,
68+
line=Line(color='purple',
69+
width=3))
70+
expected_quiver = {
71+
'y': [1, 1.0841470984807897,
72+
None, 2,
73+
2.0909297426825684, None,
74+
1.044191642387781, 1.0841470984807897,
75+
1.0658037346225067, None,
76+
2.0677536925644366, 2.0909297426825684,
77+
2.051107819102551, None],
78+
'x': [1, 1.0540302305868139,
79+
None, 2,
80+
1.9583853163452858, None,
81+
1.052143029378767, 1.0540302305868139,
82+
1.0184841899864512, None,
83+
1.9909870141679737, 1.9583853163452858,
84+
1.9546151170949464, None],
85+
'line': {'color': 'purple',
86+
'width': 3},
87+
'type': 'scatter',
88+
'mode': 'lines', }
89+
self.assertEqual(quiver, expected_quiver)
90+
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import math
2+
from unittest import TestCase
3+
4+
from nose.tools import raises
5+
6+
import plotly.tools as tls
7+
from plotly.exceptions import PlotlyError
8+
from plotly.graph_objs import Line, graph_objs
9+
10+
11+
class TestQuiver(TestCase):
12+
13+
def test_unequal_xy_length(self):
14+
15+
# check: PlotlyError if x and y are not the same length
16+
17+
kwargs = {'x': [1, 2], 'y': [1], 'u': [1, 2], 'v': [1, 2]}
18+
self.assertRaises(PlotlyError, tls.TraceFactory.create_quiver,
19+
**kwargs)
20+
21+
def test_wrong_scale(self):
22+
23+
# check: ValueError if scale is <= 0
24+
25+
kwargs = {'x': [1, 2], 'y': [1, 2],
26+
'u': [1, 2], 'v': [1, 2],
27+
'scale': -1}
28+
self.assertRaises(ValueError, tls.TraceFactory.create_quiver,
29+
**kwargs)
30+
31+
kwargs = {'x': [1, 2], 'y': [1, 2],
32+
'u': [1, 2], 'v': [1, 2],
33+
'scale': 0}
34+
self.assertRaises(ValueError, tls.TraceFactory.create_quiver,
35+
**kwargs)
36+
37+
def test_wrong_arrow_scale(self):
38+
39+
# check: ValueError if arrow_scale is <= 0
40+
41+
kwargs = {'x': [1, 2], 'y': [1, 2],
42+
'u': [1, 2], 'v': [1, 2],
43+
'arrow_scale': -1}
44+
self.assertRaises(ValueError, tls.TraceFactory.create_quiver,
45+
**kwargs)
46+
47+
kwargs = {'x': [1, 2], 'y': [1, 2],
48+
'u': [1, 2], 'v': [1, 2],
49+
'arrow_scale': 0}
50+
self.assertRaises(ValueError, tls.TraceFactory.create_quiver,
51+
**kwargs)
52+
53+
def test_one_arrow(self):
54+
55+
# we should be able to create a single arrow using create_quiver
56+
57+
quiver = tls.TraceFactory.create_quiver(x=[1], y=[1],
58+
u=[1], v=[1],
59+
scale=1)
60+
expected_quiver = {
61+
'y': [1, 2, None, 1.615486170766527, 2, 1.820698256761928, None],
62+
'x': [1, 2, None, 1.820698256761928, 2, 1.615486170766527, None],
63+
'type': 'scatter',
64+
'mode': 'lines'
65+
}
66+
self.assertEqual(quiver, expected_quiver)
67+
68+
def test_more_kwargs(self):
69+
70+
# we should be able to create 2 arrows and change the arrow_scale,
71+
# angle, and arrow using create_quiver
72+
73+
quiver = tls.TraceFactory.create_quiver(x=[1, 2],
74+
y=[1, 2],
75+
u=[math.cos(1),
76+
math.cos(2)],
77+
v=[math.sin(1),
78+
math.sin(2)],
79+
arrow_scale=.4,
80+
angle=math.pi / 6,
81+
line=Line(color='purple',
82+
width=3))
83+
expected_quiver = {
84+
'y': [1, 1.0841470984807897,
85+
None, 2,
86+
2.0909297426825684, None,
87+
1.044191642387781, 1.0841470984807897,
88+
1.0658037346225067, None,
89+
2.0677536925644366, 2.0909297426825684,
90+
2.051107819102551, None],
91+
'x': [1, 1.0540302305868139,
92+
None, 2,
93+
1.9583853163452858, None,
94+
1.052143029378767, 1.0540302305868139,
95+
1.0184841899864512, None,
96+
1.9909870141679737, 1.9583853163452858,
97+
1.9546151170949464, None],
98+
'line': {'color': 'purple',
99+
'width': 3},
100+
'type': 'scatter',
101+
'mode': 'lines', }
102+
self.assertEqual(quiver, expected_quiver)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from unittest import TestCase
2+
from plotly.graph_objs import graph_objs, Line
3+
from plotly.exceptions import PlotlyError
4+
5+
import plotly.tools as tls
6+
import math
7+
from nose.tools import raises
8+
9+
import numpy as np
10+
11+
12+
class TestStreamline(TestCase):
13+
14+
def test_wrong_arrow_scale(self):
15+
16+
# check for ValueError if arrow_scale is <= 0
17+
18+
kwargs = {'x': [0, 2], 'y': [0, 2],
19+
'u': [[-1, -5], [-1, -5]],
20+
'v': [[1, 1], [-3, -3]],
21+
'arrow_scale': 0}
22+
self.assertRaises(ValueError, tls.TraceFactory.create_streamline,
23+
**kwargs)
24+
25+
def test_wrong_density(self):
26+
27+
# check for ValueError if density is <= 0
28+
29+
kwargs = {'x': [0, 2], 'y': [0, 2],
30+
'u': [[-1, -5], [-1, -5]],
31+
'v': [[1, 1], [-3, -3]],
32+
'density': 0}
33+
self.assertRaises(ValueError, tls.TraceFactory.create_streamline,
34+
**kwargs)
35+
36+
def test_uneven_x(self):
37+
38+
# check for PlotlyError if x is not evenly spaced
39+
40+
kwargs = {'x': [0, 2, 7, 9], 'y': [0, 2, 4, 6],
41+
'u': [[-1, -5], [-1, -5]],
42+
'v': [[1, 1], [-3, -3]]}
43+
self.assertRaises(PlotlyError, tls.TraceFactory.create_streamline,
44+
**kwargs)
45+
46+
def test_uneven_y(self):
47+
48+
# check for PlotlyError if y is not evenly spaced
49+
50+
kwargs = {'x': [0, 2, 4, 6], 'y': [1.5, 2, 3, 3.5],
51+
'u': [[-1, -5], [-1, -5]],
52+
'v': [[1, 1], [-3, -3]]}
53+
self.assertRaises(PlotlyError, tls.TraceFactory.create_streamline,
54+
**kwargs)
55+
56+
def test_unequal_shape_u(self):
57+
58+
# check for PlotlyError if u and v are not the same length
59+
60+
kwargs = {'x': [0, 2, 4, 6], 'y': [1.5, 2, 3, 3.5],
61+
'u': [[-1, -5], [-1, -5], [-1, -5]],
62+
'v': [[1, 1], [-3, -3]]}
63+
self.assertRaises(PlotlyError, tls.TraceFactory.create_streamline,
64+
**kwargs)
65+
66+
def test_simple_streamline(self):
67+
68+
# Need np to check streamline data,
69+
# this checks that the first 101 x and y values from streamline are
70+
# what we expect for a simple streamline where:
71+
# x = np.linspace(-1, 1, 3)
72+
# y = np.linspace(-1, 1, 3)
73+
# Y, X = np.meshgrid(x, y)
74+
# u = X**2
75+
# v = Y**2
76+
# u = u.T #transpose
77+
# v = v.T #transpose
78+
79+
strmln = tls.TraceFactory.create_streamline(x=[-1., 0., 1.],
80+
y=[-1., 0., 1.],
81+
u=[[1., 0., 1.],
82+
[1., 0., 1.],
83+
[1., 0., 1.]],
84+
v=[[1., 1., 1.],
85+
[0., 0., 0.],
86+
[1., 1., 1.]])
87+
expected_strmln_0_100 = {
88+
'y': [-1.0, -0.9788791845863757, -0.9579399744939614, -0.9371777642073374, -0.9165881396413338, -0.8961668671832106, -0.8759098835283448, -0.8558132862403048, -0.835873324973195, -0.8160863933003534, -0.7964490210989816, -0.7769578674451656, -0.7576097139780906, -0.7384014586961288, -0.7193301101509343, -0.7003927820087748, -0.681586687951103, -0.6629091368888596, -0.64435752846723, -0.6259293488396024, -0.6076221666912738, -0.5894336294951057, -0.5713614599827976, -0.5534034528167977, -0.5355574714490806, -0.5178214451541254, -0.5001933662244311, -0.4826712873178177, -0.4652533189465894, -0.44793762709939944, -0.4307224309873414, -0.4136060009064273, -0.39658665620919065, -0.3796627633786812, -0.3628327341986042, -0.34609502401380254, -0.3294481300756896, -0.31289058996761565, -0.2964209801054992, -0.28003791430937197, -0.2637400424417804, -0.24752604910925968, -0.23139465242334434, -0.21534460281781365, -0.19937468191908325, -0.18348370146685278, -0.1676705022823033, -0.15193395328130999, -0.13627295053029143, -0.1206864163424669, -0.10517329841242584, -0.08973256898704507, -0.07436322407090357, -0.05906428266445696, -0.04383478603333624, -0.028673797007230273, -0.013580399306900914, 0.0014484211645073852, 0.01648792568956914, 0.03159429687713278, 0.04676843461935776, 0.062011259175942746, 0.07732371182540754, 0.09270675554339824, 0.10816137570939799, 0.12368858084331191, 0.1392894033734846, 0.1549649004378033, 0.1707161547196483, 0.1865442753205595, 0.20245039867161063, 0.21843568948560943, 0.23450134175238246, 0.25064857977955146, 0.26687865928136767, 0.2831928685183458, 0.29959252949062387, 0.3160789991881776, 0.33265367090123643, 0.3493179755944802, 0.366073383348855, 0.3829214048751186, 0.39986359310352526, 0.41690154485438513, 0.4340369025945845, 0.4512713562855355, 0.46860664532844054, 0.4860445606132082, 0.5035869466778524, 0.5212357039857456, 0.5389927913286829, 0.5568602283643591, 0.5748400982975623, 0.5929345507151613, 0.6111458045858065, 0.6294761514361948, 0.6479279587167714, 0.6665036733708583, 0.6852058256224467, 0.704037032999252],
89+
'x': [-1.0, -0.9788791845863756, -0.9579399744939614, -0.9371777642073374, -0.9165881396413338, -0.8961668671832106, -0.8759098835283448, -0.8558132862403048, -0.835873324973195, -0.8160863933003534, -0.7964490210989816, -0.7769578674451656, -0.7576097139780906, -0.7384014586961289, -0.7193301101509344, -0.7003927820087748, -0.6815866879511031, -0.6629091368888596, -0.6443575284672302, -0.6259293488396025, -0.6076221666912739, -0.5894336294951058, -0.5713614599827976, -0.5534034528167978, -0.5355574714490807, -0.5178214451541254, -0.5001933662244312, -0.4826712873178177, -0.4652533189465894, -0.44793762709939944, -0.4307224309873414, -0.4136060009064273, -0.39658665620919065, -0.3796627633786812, -0.3628327341986042, -0.34609502401380254, -0.3294481300756896, -0.31289058996761565, -0.2964209801054992, -0.28003791430937197, -0.2637400424417804, -0.24752604910925968, -0.23139465242334434, -0.21534460281781365, -0.19937468191908325, -0.18348370146685278, -0.1676705022823033, -0.15193395328130999, -0.13627295053029143, -0.1206864163424669, -0.10517329841242584, -0.08973256898704507, -0.07436322407090357, -0.05906428266445696, -0.04383478603333624, -0.028673797007230273, -0.013580399306900914, 0.0014484211645073852, 0.01648792568956914, 0.03159429687713278, 0.04676843461935776, 0.062011259175942746, 0.07732371182540754, 0.09270675554339824, 0.10816137570939799, 0.12368858084331191, 0.1392894033734846, 0.1549649004378033, 0.1707161547196483, 0.1865442753205595, 0.20245039867161063, 0.21843568948560943, 0.23450134175238246, 0.25064857977955146, 0.26687865928136767, 0.2831928685183458, 0.29959252949062387, 0.3160789991881776, 0.33265367090123643, 0.3493179755944802, 0.366073383348855, 0.3829214048751186, 0.39986359310352526, 0.41690154485438513, 0.4340369025945845, 0.4512713562855355, 0.46860664532844054, 0.4860445606132082, 0.5035869466778524, 0.5212357039857456, 0.5389927913286829, 0.5568602283643591, 0.5748400982975623, 0.5929345507151613, 0.6111458045858065, 0.6294761514361948, 0.6479279587167714, 0.6665036733708583, 0.6852058256224467, 0.704037032999252],
90+
'type': 'scatter',
91+
'mode': 'lines'
92+
}
93+
self.assertListEqual(strmln['y'][0:100], expected_strmln_0_100['y'])
94+
self.assertListEqual(strmln['x'][0:100], expected_strmln_0_100['x'])
95+

plotly/tests/test_optional/test_tracefactory/TraceFactory_optional_tests.py

Whitespace-only changes.

0 commit comments

Comments
 (0)