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