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

Add feature computation functions to CellEvaluator, ObjectivesCalculator and Objectives #350

Merged
merged 4 commits into from
Feb 24, 2021
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
22 changes: 16 additions & 6 deletions bluepyopt/ephys/evaluators.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,13 @@ def run_protocols(self, protocols, param_values):

return responses

def evaluate_with_dicts(self, param_dict=None):
def evaluate_with_dicts(self, param_dict=None, target='scores'):
"""Run evaluation with dict as input and output"""

if target not in ['scores', 'values']:
raise Exception(
'CellEvaluator: target has to be "scores" or "values".')

if self.fitness_calculator is None:
raise Exception(
'CellEvaluator: need fitness_calculator to evaluate')
Expand All @@ -195,21 +199,27 @@ def evaluate_with_dicts(self, param_dict=None):
self.fitness_protocols.values(),
param_dict)

return self.fitness_calculator.calculate_scores(responses)
if target == 'scores':
return self.fitness_calculator.calculate_scores(responses)

elif target == 'values':
return self.fitness_calculator.calculate_values(responses)

def evaluate_with_lists(self, param_list=None):
def evaluate_with_lists(self, param_list=None, target='scores'):
"""Run evaluation with lists as input and outputs"""

param_dict = self.param_dict(param_list)

obj_dict = self.evaluate_with_dicts(param_dict=param_dict)
obj_dict = self.evaluate_with_dicts(
param_dict=param_dict, target=target
)

return self.objective_list(obj_dict)

def evaluate(self, param_list=None):
def evaluate(self, param_list=None, target='scores'):
"""Run evaluation with lists as input and outputs"""

return self.evaluate_with_lists(param_list)
return self.evaluate_with_lists(param_list, target=target)

def __str__(self):

Expand Down
14 changes: 14 additions & 0 deletions bluepyopt/ephys/objectives.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ def calculate_feature_scores(self, responses):

return scores

def calculate_feature_values(self, responses):
"""Calculate the value of an individual features"""

values = []
for feature in self.features:
values.append(feature.calculate_feature(responses))

return values


class SingletonObjective(EFeatureObjective):

Expand All @@ -67,6 +76,11 @@ def calculate_score(self, responses):

return self.calculate_feature_scores(responses)[0]

def calculate_value(self, responses):
"""Objective value"""

return self.calculate_feature_values(responses)[0]

def __str__(self):
"""String representation"""

Expand Down
6 changes: 6 additions & 0 deletions bluepyopt/ephys/objectivescalculators.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ def calculate_scores(self, responses):
return {objective.name: objective.calculate_score(responses)
for objective in self.objectives}

def calculate_values(self, responses):
"""Calculator the value of each objective"""

return {objective.name: objective.calculate_value(responses)
for objective in self.objectives}

def __str__(self):
return 'objectives:\n %s' % '\n '.join(
[str(obj) for obj in self.objectives]) \
Expand Down
7 changes: 7 additions & 0 deletions bluepyopt/tests/test_ephys/test_evaluators.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ def test_CellEvaluator_evaluate():
sim=sim)

responses = protocol.run(cell_model, {'cm': 1.0}, sim=sim)

feature_value = efeature.calculate_feature(responses)
feature_value_eva = evaluator.evaluate_with_dicts(
{'cm': 1.0}, target='values'
)

score = evaluator.evaluate([1.0])
expected_score = abs(mean - feature_value)
Expand All @@ -120,3 +124,6 @@ def test_CellEvaluator_evaluate():
score_dict = evaluator.objective_dict(score)

nt.assert_almost_equal(score_dict['singleton'], expected_score)
nt.assert_almost_equal(
feature_value, feature_value_eva['singleton']
)
2 changes: 2 additions & 0 deletions bluepyopt/tests/test_ephys/test_objectives.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ def test_SingletonObjective():
responses = {'square_pulse_step1.soma.v': response, }

efeature_value = efeature.calculate_feature(responses)
efeature_value_obj = s_obj.calculate_value(responses)

nt.assert_almost_equal(
s_obj.calculate_score(responses),
abs(efeature_value - mean))
nt.assert_almost_equal(efeature_value_obj, efeature_value)


@attr('unit')
Expand Down