Skip to content

Commit 3f401b0

Browse files
committed
improve y-axis range update algorithm; streamline time data handling
1 parent 3fb199b commit 3f401b0

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

src/ansys/systemcoupling/core/charts/plotter.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,31 +70,45 @@ def _process_timestep_data(
7070
def _calc_new_ylimits_linear(
7171
ynew: list[float], old_lim: Optional[tuple[float, float]]
7272
) -> tuple[float, float]:
73-
resize_tol = 0.02
74-
resize_delta = 0.1
73+
74+
resize_factor = 0.1
75+
resize_tol = 0.01
7576

7677
min_y = min(ynew)
7778
max_y = max(ynew)
7879

79-
# Try to do something reasonable while we still don't have much data.
80-
# Try to account for case where we don't have much data and range & value both zero.
81-
data_range = min_y if abs(max_y - min_y) < 1.0e-7 else max_y - min_y
82-
data_range = abs(data_range)
83-
if data_range < 1.0e-7:
84-
delta_limits = 1.0e-7
80+
data_range = abs(max_y - min_y)
81+
if data_range == 0:
82+
# NB: min and max are equal - use the value to define the range
83+
if abs(min_y) > 0:
84+
delta_limits = abs(min_y) * resize_factor
85+
else:
86+
# Arbitrary value for now (will need to make sure it doesn't "stick")
87+
delta_limits = 1e-7
8588
else:
86-
delta_limits = resize_delta * data_range
89+
delta_limits = data_range * resize_factor
8790

91+
delta_tol = resize_tol * data_range
92+
force_tol = 2 * delta_tol + 1
8893
if old_lim is None:
89-
# First update - force calculation
90-
old_l = min_y + 1
91-
old_u = max_y - 1
94+
# Force calculation on first update
95+
old_l = min_y + force_tol
96+
old_u = max_y - force_tol
9297
else:
9398
new_l, new_u = old_l, old_u = old_lim
94-
95-
if min_y < old_l + resize_tol * data_range:
99+
# In the case where we guessed limits for zero data and now have
100+
# some non-zero data, need to adjust limits downwards if the actual
101+
# data range is significantly smaller than current limits range.
102+
old_delta = old_u - old_l
103+
if data_range < old_delta * resize_factor:
104+
# Force recalculation
105+
old_l = min_y + force_tol
106+
old_u = max_y - force_tol
107+
108+
# Only extend the limits if we are getting close to the old ones
109+
if min_y < old_l + delta_tol:
96110
new_l = min_y - delta_limits
97-
if max_y > old_u - resize_tol * data_range:
111+
if max_y > old_u - delta_tol:
98112
new_u = max_y + delta_limits
99113

100114
return new_l, new_u
@@ -201,12 +215,8 @@ def _init_from_metadata(self):
201215
self._init_plots()
202216
self._fig.suptitle(f"Interface: {self._metadata.name}", fontsize=10)
203217

204-
def set_timestep_data(self, timestep_data: TimestepData):
205-
206-
if timestep_data.timestep and not self._metadata.is_transient:
207-
raise RuntimeError("Attempt to set timestep data on non-transient case")
208-
209-
self._time_indexes, self._times = _process_timestep_data(timestep_data)
218+
def set_timestep_data(self, timestep_data: tuple[list[int], list[float]]):
219+
self._time_indexes, self._times = timestep_data
210220

211221
def update_line_series(self, series_data: SeriesData):
212222
"""Update the line series determined by the provided ``series_data`` with the
@@ -381,8 +391,9 @@ def set_timestep_data(self, timestep_data: TimestepData):
381391
if timestep_data.timestep and not self._is_transient:
382392
raise RuntimeError("Attempt to set timestep data on non-transient case")
383393

394+
processed_timestep_data = _process_timestep_data(timestep_data)
384395
for fig in self._figures:
385-
fig.set_timestep_data(timestep_data)
396+
fig.set_timestep_data(processed_timestep_data)
386397

387398
def update_line_series(self, series_data: SeriesData):
388399
"""Update the line series determined by the provided ``series_data`` with the

tests/charts/test_plotter.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,19 @@
4343
(0.45, 0.55),
4444
),
4545
([0.0, 0.0], (-1e-7, 1e-7)),
46+
([0.0, 1e-8], (-1e-9, 1.1e-8)),
4647
([0.5, 0.5], (0.45, 0.55)),
4748
([-0.1, 10.0], (-1.11, 11.01)),
4849
([0.1, 0.5, 0.92], (0.018, 1.002)),
4950
([0.0, 1.0], (-0.1, 1.1)),
51+
(
52+
[0.0, 1.0, 0.9],
53+
(-0.1, 1.1),
54+
),
55+
(
56+
[0.0, 1.0, 0.9],
57+
(-0.1, 1.1),
58+
),
5059
],
5160
)
5261
def test_new_linear_limits_uninitialised(ynew, expected):
@@ -86,7 +95,7 @@ def test_new_linear_limits_uninitialised(ynew, expected):
8695
(
8796
(0.0, 0.9),
8897
[1.0],
89-
(0.0, 1.1),
98+
(0.9, 1.1),
9099
),
91100
],
92101
)

0 commit comments

Comments
 (0)