Skip to content

Commit aa0f1d8

Browse files
committed
test: refactor and add test for get_overlapping_regions
1 parent bc4a7be commit aa0f1d8

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

src/diffpy/morph/morphs/morphsqueeze.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def _set_squeeze_info(self, x, x_sorted):
100100
"and stretch parameter for a1."
101101
)
102102
else:
103-
overlapping_regions = self._get_overlapping_regions(x)
103+
overlapping_regions = self.get_overlapping_regions(x)
104104
self.squeeze_info["monotonic"] = False
105105
self.squeeze_info["overlapping_regions"] = overlapping_regions
106106

@@ -111,27 +111,29 @@ def _sort_squeeze(self, x, y):
111111
x_sorted, y_sorted = list(zip(*xy_sorted))
112112
return x_sorted, y_sorted
113113

114-
def _get_overlapping_regions(self, x):
114+
def get_overlapping_regions(self, x):
115115
diffx = numpy.diff(x)
116-
monotomic_regions = []
117-
monotomic_signs = [numpy.sign(diffx[0])]
118-
current_region = [x[0], x[1]]
119-
for i in range(1, len(diffx)):
120-
if numpy.sign(diffx[i]) == monotomic_signs[-1]:
121-
current_region.append(x[i + 1])
122-
else:
123-
monotomic_regions.append(current_region)
124-
monotomic_signs.append(numpy.sign(diffx[i]))
125-
current_region = [x[i + 1]]
126-
monotomic_regions.append(current_region)
116+
diffx_sign = numpy.sign(diffx)
117+
local_min_or_max_index = (
118+
numpy.where(numpy.diff(diffx_sign) != 0)[0] + 1
119+
)
120+
monotonic_regions_x = numpy.concatenate(
121+
(
122+
[x[0]],
123+
numpy.repeat(
124+
numpy.array(x)[local_min_or_max_index], 2
125+
).tolist()[:-1],
126+
)
127+
).reshape(-1, 2)
128+
monotinic_regions_sign = diffx_sign[local_min_or_max_index - 1]
129+
127130
overlapping_regions_sign = -1 if x[0] < x[-1] else 1
128-
overlapping_regions_x = [
129-
monotomic_regions[i]
130-
for i in range(len(monotomic_regions))
131-
if monotomic_signs[i] == overlapping_regions_sign
132-
]
131+
overlapping_regions_index = numpy.where(
132+
monotinic_regions_sign == overlapping_regions_sign
133+
)[0]
134+
overlapping_regions = monotonic_regions_x[overlapping_regions_index]
133135
overlapping_regions = [
134-
(min(region), max(region)) for region in overlapping_regions_x
136+
sorted(region) for region in overlapping_regions
135137
]
136138
return overlapping_regions
137139

tests/test_morphsqueeze.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,26 @@ def test_sort_squeeze_bad(user_filesystem, squeeze_coeffs, x_morph):
261261
assert expected_emsg in actual_emsg
262262

263263

264+
@pytest.mark.parametrize(
265+
"turning_points, expected_overlapping_regions",
266+
[
267+
# x[-1] > x[0], monotonically decreasing regions are overlapping
268+
([0, 10, 7, 12], [[7, 10]]),
269+
# x[-1] < x[0], monotonically increasing regions are overlapping
270+
([0, 5, 2, 4, -10], [[0, 5], [2, 4]])
271+
],
272+
)
273+
def test_get_overlapping_regions(turning_points, expected_overlapping_regions):
274+
morph = MorphSqueeze()
275+
regions = (
276+
np.linspace(turning_points[i], turning_points[i + 1], 20)
277+
for i in range(len(turning_points) - 1)
278+
)
279+
x_value = np.concatenate(list(regions))
280+
actual_overlaping_regions = morph.get_overlapping_regions(x_value)
281+
assert expected_overlapping_regions == actual_overlaping_regions
282+
283+
264284
def create_morph_data_file(
265285
data_dir_path, x_morph, y_morph, x_target, y_target
266286
):

0 commit comments

Comments
 (0)