audeering / opensmile-python

Python package for openSMILE
https://audeering.github.io/opensmile-python/
Other
243 stars 32 forks source link

How to extract only a subset of features? #107

Open oscars47 opened 4 months ago

oscars47 commented 4 months ago

Hello,

My problem is I want to start with, say, the ComParE-2016 features, but instead of actually computing all 6373 of them at once, pass in a list of strings custom_feature_ls which are a subset of the full feature list, and only actually perform the computation of these specific features. I haven't seen any direct support for this in the source code, nor can I quite figure out how to specify a config file to do this. Thanks!

maxschmitt commented 3 months ago

The most straightforward way is to simply compute all features and select a subset in the returned DataFrame, e.g., if you want F0 mean and range:

import numpy as np
import opensmile

sampling_rate = 16000
signal = np.sin(np.arange(sampling_rate)*440*2*np.pi/sampling_rate)

smile = opensmile.Smile(
    feature_set=opensmile.FeatureSet.ComParE_2016,
    feature_level=opensmile.FeatureLevel.Functionals,
)

features = smile.process_signal(
    signal,
    sampling_rate
)

custom_feature_ls = ["F0final_sma_amean",
                     "F0final_sma_range",
                     ]

features_selected = features[custom_feature_ls]
print(features_selected)

#                         F0final_sma_amean  F0final_sma_range
# start  end                                                  
# 0 days 0 days 00:00:01         439.721619            0.03595

Adapting the config file is also possible, of course, but much more complex.

oscars47 commented 3 months ago

Hi, thank you Max for getting back to me and including this example. Right of course we can postselect, but is there a way to preselect what components are calculated in the first place from the config file?

maxschmitt commented 3 months ago

Yes, you need to go through the config file and remove components/features that are not required. It is not very straightforward as you need to get familiar with the structure of the config file and as they are quite long for this particular feature set.