Skip to content

Commit b394e45

Browse files
committed
minor cleanup
1 parent a8c8974 commit b394e45

File tree

11 files changed

+88
-79
lines changed

11 files changed

+88
-79
lines changed

waveform_editor/tendencies/base.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,26 +113,30 @@ def set_next_tendency(self, next_tendency):
113113

114114
@depends("values_changed", watch=True)
115115
def _calc_start_end_values(self):
116-
_, start_value_array = self.get_value(np.array([self.start]))
117-
self.start_value = start_value_array[0]
118-
start_derivative_array = self.get_derivative(np.array([self.start]))
119-
self.start_derivative = start_derivative_array[0]
116+
"""Calculate the values, as well as the derivatives, at the start and end
117+
of the tendency.
118+
"""
119+
self.start_value, self.start_derivative = self._get_value_and_derivative(
120+
self.start
121+
)
122+
self.end_value, self.end_derivative = self._get_value_and_derivative(self.end)
120123

121-
_, end_value_array = self.get_value(np.array([self.end]))
122-
self.end_value = end_value_array[0]
123-
end_derivative_array = self.get_derivative(np.array([self.end]))
124-
self.end_derivative = end_derivative_array[0]
124+
def _get_value_and_derivative(self, time):
125+
"""Get the value and derivative of the tendency at a given time."""
126+
_, value_array = self.get_value(np.array([time]))
127+
derivative_array = self.get_derivative(np.array([time]))
128+
return value_array[0], derivative_array[0]
125129

126130
@abstractmethod
127131
def get_value(
128132
self, time: Optional[np.ndarray] = None
129133
) -> tuple[np.ndarray, np.ndarray]:
130-
"""Get the values on the provided time array."""
134+
"""Get the tendency values at the provided time array."""
131135
return np.array([0]), np.array([0])
132136

133137
@abstractmethod
134138
def get_derivative(self, time: np.ndarray) -> np.ndarray:
135-
"""Get the derivative values on the provided time array."""
139+
"""Get the values of the derivatives at the provided time array."""
136140
return np.array([0])
137141

138142
@depends(

waveform_editor/tendencies/constant.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def __init__(self, **kwargs):
2424
def get_value(
2525
self, time: Optional[np.ndarray] = None
2626
) -> tuple[np.ndarray, np.ndarray]:
27-
"""Get the values of the provided time array. If no time array is provided,
28-
a constant line containing the start and end points will be generated.
27+
"""Get the tendency values at the provided time array. If no time array is
28+
provided, a constant line containing the start and end points will be generated.
2929
3030
Args:
3131
time: The time array on which to generate points.
@@ -39,7 +39,7 @@ def get_value(
3939
return time, values
4040

4141
def get_derivative(self, time: np.ndarray) -> np.ndarray:
42-
"""Get the derivative values on the provided time array.
42+
"""Get the values of the derivatives at the provided time array.
4343
4444
Args:
4545
time: The time array on which to generate points.

waveform_editor/tendencies/linear.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ def __init__(self, **kwargs):
3838
def get_value(
3939
self, time: Optional[np.ndarray] = None
4040
) -> tuple[np.ndarray, np.ndarray]:
41-
"""Get the values onf the provided time array. If no time array is provided,
42-
a line containing the start and end points will be generated.
41+
"""Get the tendency values at the provided time array. If no time array is
42+
provided, a line containing the start and end points will be generated.
4343
4444
Args:
4545
time: The time array on which to generate points.
@@ -54,7 +54,7 @@ def get_value(
5454
return time, values
5555

5656
def get_derivative(self, time: np.ndarray) -> np.ndarray:
57-
"""Get the derivative values on the provided time array.
57+
"""Get the values of the derivatives at the provided time array.
5858
5959
Args:
6060
time: The time array on which to generate points.

waveform_editor/tendencies/periodic/sawtooth_wave.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ class SawtoothWaveTendency(PeriodicBaseTendency):
1111
def get_value(
1212
self, time: Optional[np.ndarray] = None
1313
) -> tuple[np.ndarray, np.ndarray]:
14-
"""Generate time and values based on the tendency. If no time array is provided,
15-
a time array will be created from the start to the end of the tendency, where
16-
time points are defined for every peak and trough in the tendency.
14+
"""Get the tendency values at the provided time array. If no time array is
15+
provided, a time array will be created from the start to the end of the
16+
tendency, where time points are defined for every peak and trough in the
17+
tendency.
1718
1819
Args:
1920
time: The time array on which to generate points.
@@ -29,7 +30,7 @@ def get_value(
2930
return time, values
3031

3132
def get_derivative(self, time: np.ndarray) -> np.ndarray:
32-
"""Get the derivative values on the provided time array.
33+
"""Get the values of the derivatives at the provided time array.
3334
3435
Args:
3536
time: The time array on which to generate points.

waveform_editor/tendencies/periodic/sine_wave.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class SineWaveTendency(PeriodicBaseTendency):
1111
def get_value(
1212
self, time: Optional[np.ndarray] = None
1313
) -> tuple[np.ndarray, np.ndarray]:
14-
"""Generate time and values based on the tendency. If no time array is provided,
15-
a linearly spaced time array will be generated from the start to the end of the
16-
tendency.
14+
"""Get the tendency values at the provided time array. If no time array is
15+
provided, a linearly spaced time array will be generated from the start to the
16+
end of the tendency.
1717
1818
Args:
1919
time: The time array on which to generate points.
@@ -29,7 +29,7 @@ def get_value(
2929
return time, values
3030

3131
def get_derivative(self, time: np.ndarray) -> np.ndarray:
32-
"""Get the derivative values on the provided time array.
32+
"""Get the values of the derivatives at the provided time array.
3333
3434
Args:
3535
time: The time array on which to generate points.

waveform_editor/tendencies/periodic/square_wave.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ class SquareWaveTendency(PeriodicBaseTendency):
1111
def get_value(
1212
self, time: Optional[np.ndarray] = None
1313
) -> tuple[np.ndarray, np.ndarray]:
14-
"""Generate time and values based on the tendency. If no time array is provided,
15-
a time array will be created from the start to the end of the tendency, where
16-
time points are defined for every peak and trough in the tendency.
14+
"""Get the tendency values at the provided time array. If no time array is
15+
provided, a time array will be created from the start to the end of the
16+
tendency, where time points are defined for every peak and trough in the
17+
tendency.
1718
1819
Args:
1920
time: The time array on which to generate points.
@@ -29,7 +30,7 @@ def get_value(
2930
return time, values
3031

3132
def get_derivative(self, time: np.ndarray) -> np.ndarray:
32-
"""Get the derivative values on the provided time array.
33+
"""Get the values of the derivatives at the provided time array.
3334
3435
Args:
3536
time: The time array on which to generate points.

waveform_editor/tendencies/periodic/triangle_wave.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ class TriangleWaveTendency(PeriodicBaseTendency):
1111
def get_value(
1212
self, time: Optional[np.ndarray] = None
1313
) -> tuple[np.ndarray, np.ndarray]:
14-
"""Generate time and values based on the tendency. If no time array is provided,
15-
a time array will be created from the start to the end of the tendency, where
16-
time points are defined for every peak and trough in the tendency.
14+
"""Get the tendency values at the provided time array. If no time array is
15+
provided, a time array will be created from the start to the end of the
16+
tendency, where time points are defined for every peak and trough in the
17+
tendency.
1718
1819
Args:
1920
time: The time array on which to generate points.
@@ -27,7 +28,7 @@ def get_value(
2728
return time, values
2829

2930
def get_derivative(self, time: np.ndarray) -> np.ndarray:
30-
"""Get the derivative values on the provided time array.
31+
"""Get the values of the derivatives at the provided time array.
3132
3233
Args:
3334
time: The time array on which to generate points.

waveform_editor/tendencies/piecewise.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ def __init__(self, *, user_time=None, user_value=None):
2929
def get_value(
3030
self, time: Optional[np.ndarray] = None
3131
) -> tuple[np.ndarray, np.ndarray]:
32-
"""Generate time and values based on the tendency. If a time array is provided,
33-
the values will be linearly interpolated between the piecewise linear points.
32+
"""Get the tendency values at the provided time array. If a time array is
33+
provided, the values will be linearly interpolated between the piecewise linear
34+
points.
3435
3536
Args:
3637
time: The time array on which to generate points.
@@ -39,14 +40,14 @@ def get_value(
3940
Tuple containing the time and its tendency values.
4041
"""
4142
if time is None:
42-
time = self.time
43+
return self.time, self.value
4344

4445
self._validate_requested_time(time)
4546
interpolated_values = np.interp(time, self.time, self.value)
4647
return time, interpolated_values
4748

4849
def get_derivative(self, time: np.ndarray) -> np.ndarray:
49-
"""Get the derivative values on the provided time array.
50+
"""Get the values of the derivatives at the provided time array.
5051
5152
Args:
5253
time: The time array on which to generate points.
@@ -55,17 +56,14 @@ def get_derivative(self, time: np.ndarray) -> np.ndarray:
5556
numpy array containing the derivatives
5657
"""
5758
self._validate_requested_time(time)
58-
5959
derivatives = np.zeros_like(time)
6060

6161
for i, t in enumerate(time):
62-
# Find the segment for this particular time
6362
if t == self.time[-1]:
6463
derivatives[i] = (self.value[-1] - self.value[-2]) / (
6564
self.time[-1] - self.time[-2]
6665
)
6766
else:
68-
# Find the two indices surrounding the time point (t)
6967
index = np.searchsorted(self.time, t)
7068
dv = self.value[index + 1] - self.value[index]
7169
dt = self.time[index + 1] - self.time[index]
@@ -74,6 +72,11 @@ def get_derivative(self, time: np.ndarray) -> np.ndarray:
7472
return derivatives
7573

7674
def _validate_requested_time(self, time):
75+
"""Check if the requested time data falls within the piecewise tendency.
76+
77+
Args:
78+
time: The time array on which to generate points.
79+
"""
7780
if np.any(time < self.time[0]) or np.any(time > self.time[-1]):
7881
raise ValueError(
7982
f"The provided time array contains values outside the valid range "

waveform_editor/tendencies/repeat.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ def __init__(self, **kwargs):
4242
# - {type: linear, from: 2, to: 0, duration: 1}
4343
# - {type: smooth, duration: 3}
4444
#
45-
# Here, the smooth tendency would smoothly interpolate from 0 to 2
45+
# where, the smooth tendency would smoothly interpolate from 0 to 2
4646

4747
def get_value(
4848
self, time: Optional[np.ndarray] = None
4949
) -> tuple[np.ndarray, np.ndarray]:
50-
"""Generate time and values based on the tendency. If no time array is provided,
51-
the individual tendencies are responsible for creating a time array, and these
52-
are appended.
50+
"""Get the tendency values at the provided time array. If no time array is
51+
provided, the individual tendencies are responsible for creating a time array,
52+
and these are appended.
5353
5454
Args:
5555
time: The time array on which to generate points.
@@ -73,17 +73,18 @@ def get_value(
7373
values = values[: cut_index + 1]
7474
if times[-1] != self.end:
7575
times[-1] = self.end
76-
_, values[-1] = self.waveform.get_value(
76+
_, end_array = self.waveform.get_value(
7777
np.array([(self.end - self.start) % length])
7878
)
79+
values[-1] = end_array[0]
7980
else:
8081
times = np.atleast_1d(time)
8182
relative_times = (times - self.start) % length
8283
_, values = self.waveform.get_value(relative_times)
8384
return times, values
8485

8586
def get_derivative(self, time: np.ndarray) -> np.ndarray:
86-
"""Get the derivative values on the provided time array.
87+
"""Get the values of the derivatives at the provided time array.
8788
8889
Args:
8990
time: The time array on which to generate points.
@@ -92,7 +93,6 @@ def get_derivative(self, time: np.ndarray) -> np.ndarray:
9293
numpy array containing the derivatives
9394
"""
9495
length = self.waveform.calc_length()
95-
times = np.atleast_1d(time)
96-
relative_times = (times - self.start) % length
96+
relative_times = (time - self.start) % length
9797
derivatives = self.waveform.get_derivative(relative_times)
9898
return derivatives

waveform_editor/tendencies/smooth.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def __init__(self, **kwargs):
3232
def get_value(
3333
self, time: Optional[np.ndarray] = None
3434
) -> tuple[np.ndarray, np.ndarray]:
35-
"""Generate time and values based on the tendency. If no time array is provided,
36-
a linearly spaced time array will be generated from the start to the end of the
37-
tendency.
35+
"""Get the tendency values at the provided time array. If no time array is
36+
provided, a linearly spaced time array will be generated from the start to the
37+
end of the tendency.
3838
3939
Args:
4040
time: The time array on which to generate points.
@@ -56,7 +56,7 @@ def get_value(
5656
return time, values
5757

5858
def get_derivative(self, time: np.ndarray) -> np.ndarray:
59-
"""Get the derivative values on the provided time array.
59+
"""Get the values of the derivatives at the provided time array.
6060
6161
Args:
6262
time: The time array on which to generate points.

0 commit comments

Comments
 (0)