Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def g(self, other, *args):
other = np.asanyarray(other)
if not isinstance(other, Quantity):
other = other.view(type=Quantity)
if other._dimensionality != self._dimensionality:
other = other.rescale(self.units)
return f(self, other, *args)
return g
or 'sosfiltfilt'.
When both `highpass_freq` and `lowpass_freq` are None.
"""
available_filters = 'lfilter', 'filtfilt', 'sosfiltfilt'
if filter_function not in available_filters:
raise ValueError("Invalid `filter_function`: {filter_function}. "
"Available filters: {available_filters}".format(
filter_function=filter_function,
available_filters=available_filters))
# design filter
if hasattr(signal, 'sampling_rate'):
fs = signal.sampling_rate.rescale(pq.Hz).magnitude
if isinstance(highpass_freq, pq.quantity.Quantity):
highpass_freq = highpass_freq.rescale(pq.Hz).magnitude
if isinstance(lowpass_freq, pq.quantity.Quantity):
lowpass_freq = lowpass_freq.rescale(pq.Hz).magnitude
Fn = fs / 2.
# filter type is determined according to the values of cut-off
# frequencies
if lowpass_freq and highpass_freq:
if highpass_freq < lowpass_freq:
Wn = (highpass_freq / Fn, lowpass_freq / Fn)
btype = 'bandpass'
else:
Wn = (lowpass_freq / Fn, highpass_freq / Fn)
btype = 'bandstop'
elif lowpass_freq:
Wn = lowpass_freq / Fn
btype = 'lowpass'
elif highpass_freq:
Wn = highpass_freq / Fn
ValueError
If `filter_function` is not one of 'lfilter', 'filtfilt',
or 'sosfiltfilt'.
When both `highpass_freq` and `lowpass_freq` are None.
"""
available_filters = 'lfilter', 'filtfilt', 'sosfiltfilt'
if filter_function not in available_filters:
raise ValueError("Invalid `filter_function`: {filter_function}. "
"Available filters: {available_filters}".format(
filter_function=filter_function,
available_filters=available_filters))
# design filter
if hasattr(signal, 'sampling_rate'):
fs = signal.sampling_rate.rescale(pq.Hz).magnitude
if isinstance(highpass_freq, pq.quantity.Quantity):
highpass_freq = highpass_freq.rescale(pq.Hz).magnitude
if isinstance(lowpass_freq, pq.quantity.Quantity):
lowpass_freq = lowpass_freq.rescale(pq.Hz).magnitude
Fn = fs / 2.
# filter type is determined according to the values of cut-off
# frequencies
if lowpass_freq and highpass_freq:
if highpass_freq < lowpass_freq:
Wn = (highpass_freq / Fn, lowpass_freq / Fn)
btype = 'bandpass'
else:
Wn = (lowpass_freq / Fn, highpass_freq / Fn)
btype = 'bandstop'
elif lowpass_freq:
Wn = lowpass_freq / Fn
btype = 'lowpass'
def tan(x, out=None):
"""
Raises a ValueError if input cannot be rescaled to radians.
Returns a dimensionless quantity.
"""
if not isinstance(x, Quantity):
return np.tan(x, out)
return Quantity(np.tan(x.rescale(radian).magnitude), copy=False)
or 'sosfiltfilt'.
When both `highpass_freq` and `lowpass_freq` are None.
"""
available_filters = 'lfilter', 'filtfilt', 'sosfiltfilt'
if filter_function not in available_filters:
raise ValueError("Invalid `filter_function`: {filter_function}. "
"Available filters: {available_filters}".format(
filter_function=filter_function,
available_filters=available_filters))
# design filter
if hasattr(signal, 'sampling_rate'):
fs = signal.sampling_rate.rescale(pq.Hz).magnitude
if isinstance(highpass_freq, pq.quantity.Quantity):
highpass_freq = highpass_freq.rescale(pq.Hz).magnitude
if isinstance(lowpass_freq, pq.quantity.Quantity):
lowpass_freq = lowpass_freq.rescale(pq.Hz).magnitude
Fn = fs / 2.
# filter type is determined according to the values of cut-off
# frequencies
if lowpass_freq and highpass_freq:
if highpass_freq < lowpass_freq:
Wn = (highpass_freq / Fn, lowpass_freq / Fn)
btype = 'bandpass'
else:
Wn = (lowpass_freq / Fn, highpass_freq / Fn)
btype = 'bandstop'
elif lowpass_freq:
Wn = lowpass_freq / Fn
btype = 'lowpass'
elif highpass_freq:
Wn = highpass_freq / Fn
def ptp(self, axis=None, out=None):
ret = self.magnitude.ptp(axis, None if out is None else out.magnitude)
dim = self.dimensionality
if out is None:
return Quantity(ret, dim, copy=False)
if not isinstance(out, Quantity):
raise TypeError("out parameter must be a Quantity")
out._dimensionality = dim
return out
def hypot(x1, x2, out = None):
"""
Raises a ValueError if inputs do not have identical units.
"""
if not (isinstance(x1, Quantity) and isinstance(x2, Quantity)):
return np.hypot(x1, x2, out)
if not isinstance(x1, Quantity):
x1 = Quantity(x1, dimensionless, copy=False)
if not isinstance(x2, Quantity):
x2 = Quantity(x2, dimensionless, copy=False)
if x1._dimensionality != x2._dimensionality:
raise ValueError(
'x1 and x2 must have identical units, got "%s" and "%s"'\
% (str(x1._dimensionality), str(x2._dimensionality))
)
return Quantity(
np.hypot(x1.magnitude, x2.magnitude, out),
x1.dimensionality,
copy = False
)
def put(self, indicies, values, mode='raise'):
"""
performs the equivalent of ndarray.put() but enforces units
values - must be an Quantity with the same units as self
"""
if not isinstance(values, Quantity):
values = Quantity(values)
if values._dimensionality != self._dimensionality:
values = values.rescale(self.units)
self.magnitude.put(indicies, values, mode)
def real(self, r):
self.magnitude.real = Quantity(r, self.dimensionality).magnitude
def astype(self, dtype=None, **kwargs):
'''Scalars are returned as scalar Quantity arrays.'''
ret = super(Quantity, self.view(Quantity)).astype(dtype, **kwargs)
# scalar quantities get converted to plain numbers, so we fix it
# might be related to numpy ticket # 826
if not isinstance(ret, type(self)):
if self.__array_priority__ >= Quantity.__array_priority__:
ret = type(self)(ret, self._dimensionality)
else:
ret = Quantity(ret, self._dimensionality)
return ret