Skip to content

Commit 2baad07

Browse files
committed
Reject unevenly spaced x/y in create_streamline when steps shrink
validate_streamline compared each step with the first one without taking the absolute value, so only steps larger than the first were caught and x such as [0, 2, 3, 3.5] passed as evenly spaced.
1 parent 73f86fd commit 2baad07

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎plotly/figure_factory/_streamline.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ def validate_streamline(x, y):
2323
if np is False:
2424
raise ImportError("FigureFactory.create_streamline requires numpy")
2525
for index in range(len(x) - 1):
26-
if ((x[index + 1] - x[index]) - (x[1] - x[0])) > 0.0001:
26+
if abs((x[index + 1] - x[index]) - (x[1] - x[0])) > 0.0001:
2727
raise exceptions.PlotlyError(
2828
"x must be a 1 dimensional, evenly spaced array"
2929
)
3030
for index in range(len(y) - 1):
31-
if ((y[index + 1] - y[index]) - (y[1] - y[0])) > 0.0001:
31+
if abs((y[index + 1] - y[index]) - (y[1] - y[0])) > 0.0001:
3232
raise exceptions.PlotlyError(
3333
"y must be a 1 dimensional, evenly spaced array"
3434
)

‎tests/test_optional/test_figure_factory/test_figure_factory.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ def test_uneven_y(self):
6464
}
6565
self.assertRaises(PlotlyError, ff.create_streamline, **kwargs)
6666

67+
def test_uneven_x_shrinking_steps(self):
68+
# check for PlotlyError if x steps get smaller than the first one
69+
70+
kwargs = {
71+
"x": [0, 2, 3, 3.5],
72+
"y": [0, 1, 2, 3],
73+
"u": [[1, 1, 1, 1]] * 4,
74+
"v": [[1, 1, 1, 1]] * 4,
75+
}
76+
self.assertRaises(PlotlyError, ff.create_streamline, **kwargs)
77+
78+
def test_uneven_y_shrinking_steps(self):
79+
# check for PlotlyError if y steps get smaller than the first one
80+
81+
kwargs = {
82+
"x": [0, 1, 2, 3],
83+
"y": [0, 2, 3, 3.5],
84+
"u": [[1, 1, 1, 1]] * 4,
85+
"v": [[1, 1, 1, 1]] * 4,
86+
}
87+
self.assertRaises(PlotlyError, ff.create_streamline, **kwargs)
88+
6789
def test_unequal_length_xy(self):
6890
# check for PlotlyError if u and v are not the same length
6991

0 commit comments

Comments
 (0)