Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add python dynamical feature registration #391

Merged
merged 6 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions efel/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,12 @@ def get_mean_feature_values(
return featureDicts # type: ignore


def register_feature(feature_function: Callable):
"""Register a new feature."""
pyfeatures.all_pyfeatures.append(feature_function.__name__)
setattr(pyfeatures, feature_function.__name__, feature_function)


reset()


Expand Down
27 changes: 27 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from efel.api import get_feature_values
from efel.api import set_setting
from efel.api import get_distance
from efel.api import register_feature


testdata_dir = Path(__file__).parent / 'testdata'
Expand Down Expand Up @@ -4522,3 +4523,29 @@ def test_spontaneous_firing():
numpy.testing.assert_allclose(ap_fall_time, ap_fall_time_expected)
numpy.testing.assert_allclose(ap_rise_rate, ap_rise_rate_expected)
numpy.testing.assert_allclose(ap_fall_rate, ap_fall_rate_expected)


def test_register_feature():
"""basic: Test register feature"""
import efel
efel.reset()

trace = {}
trace['T'] = numpy.arange(0, 100, 0.1)
trace['V'] = numpy.ones(len(trace['T'])) * -80.0
trace['stim_start'] = [25]
trace['stim_end'] = [75]

def new_feature():
v = efel.pyfeatures.get_cpp_feature("voltage")
stim_start = efel.pyfeatures.pyfeatures._get_cpp_data("stim_start")
return numpy.array([v[0], stim_start])

efel.register_feature(new_feature)
result = efel.get_feature_values([trace], ["new_feature"])[0]["new_feature"]
assert result[0] == -80.0
assert result[1] == 25.0

# remove it to keep other tests untouched
del efel.pyfeatures.new_feature
del efel.pyfeatures.all_pyfeatures[-1]
Loading