|
| 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 | + |
0 commit comments