FeaturesBase Class¶
Abstract Base Class for audio feature extraction. Defines an interface for audio feature extraction algorithms.
Inheriting classes must override get_features()
and implement feature
extraction in that function, which accepts an AudioBuffer and
must return a np.ndarray containing results.
Feature extraction is run in inheriting classes through the __call__ member function.
Example of FFT, which inherits from FeaturesBase:
audio = AudioBuffer('./some_audio.wav')
fft = FFT()
# Now feature extraction is run by treating
# the FFT instance, fft, like a function
spectrum = fft(audio)
-
class
spiegelib.features.
FeaturesBase
(sample_rate=44100, time_major=False, scale=False, scale_axis=(0, ))¶ Bases:
abc.ABC
- Parameters
sample_rate (int, optional) – audio sample rate, defaults to 44100
time_major (bool, optional) – for audio feature extraction that uses time slices, used to indicate the orientation of features and time slices in the resulting matrix. True indicates that results should be returned with an orientation like (time_slices, features). False (default) refers to an orientation of (features, time_slices).
scale (bool, optional) – whether to scale the results of feature extraction. The scaler must be set before scaling called be applied. See
fit_scaler()
orset_scaler()
.scale_axis (int, tuple, None, optional) – indicates the axis to use for fitting scalers and applying data scaling. A value of None flattens the dataset and calculates scaling parameters on that. A value of an int or a tuple indicates the axis or axes to use. Defaults to (0,) which causes scaling variables to be calculated on each feature independently.
-
__call__
(audio, scale=None)¶ Run this feature extraction pipeline.
Applies functions in this order: input modifiers > feature extraction > prescale modifiers > data scaling > output modifiers
- Parameters
audio (AudioBuffer) – Audio to extract features from
scale (bool, optional) – If set, will override scale attribute set during construction.
- Returns
results from audio feature extraction with modifiers and scaling.
- Return type
np.ndarray
-
abstract
get_features
(audio)¶ Must be implemented. Run audio feature extraction on audio provided as parameter. Must check the time_major attribute and return data in the correct orientation.
- Parameters
audio (AudioBuffer) – Audio to extract features from
- Returns
Results of audio feature extraction
- Return type
np.ndarray
-
add_modifier
(modifier, type)¶ Add a data modifier to the feature extraction pipeline.
Input modifiers are applied to raw AudioBuffers <audio_buffer> prior to feature extraction. Prescale modifiers are applied to results of audio feature extraction and before data scaling (if applicable). Output modifiers are applied after data scaling.
- Parameters
modifier (lambda) – data modifier function. Should accept an np.array, apply some modification to that, and return a np.array
type (str) – Where to add function into pipeline. Must be one of (‘input’, ‘prescale’, or ‘output’)
-
fit_scaler
(data, transform=True)¶ Fit scaler to dataset for future transforms.
- Parameters
data (np.ndarray) – data to train (fit) scaler on
transform (bool, optional) – if True will also apply scaling to the data
- Returns
Scaled data if transform parameter is True, otherwise returns None.
- Return type
np.ndarray, None
-
has_scaler
()¶ - Returns
whether or not the scaler has been set
- Return type
bool
-
set_scaler
(scaler)¶ Set a scaler for a dimension, this will be used to normalize that dimension
- Parameters
scaler (object) – a Scaler object to use to scale data. Must be a DataScalerBase type to use the
fit_scaler()
method. Otherwise, other scalers like sklearn scalers could potentially be fit and then passed in here – just needs to have a transform method to apply scaling. (note, using other objects outside of DataScalerBase type has not been tested!)
-
scale
(data)¶ Scale features using pre-trained scaler
- Parameters
data (np.ndarray) – data to be scaled
- Returns
scaled data
- Return type
np.ndarray
-
load_scaler
(location)¶ Load trained scaler from a pickled file.
- Parameters
location (str) – Location of pickled scaler object
-
save_scaler
(location)¶ Save the trained scaler for these features as a pickle for later use
- Parameters
location (str) – Location to save pickled scaler object