Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit 0b17fda

Browse files
authored
Merge pull request #350 from BlueBrain/feature_values
Add feature computation functions to CellEvaluator, ObjectivesCalculator and Objectives
2 parents 5c0be1a + 4b05e8f commit 0b17fda

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

bluepyopt/ephys/evaluators.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,13 @@ def run_protocols(self, protocols, param_values):
182182

183183
return responses
184184

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

188+
if target not in ['scores', 'values']:
189+
raise Exception(
190+
'CellEvaluator: target has to be "scores" or "values".')
191+
188192
if self.fitness_calculator is None:
189193
raise Exception(
190194
'CellEvaluator: need fitness_calculator to evaluate')
@@ -195,21 +199,27 @@ def evaluate_with_dicts(self, param_dict=None):
195199
self.fitness_protocols.values(),
196200
param_dict)
197201

198-
return self.fitness_calculator.calculate_scores(responses)
202+
if target == 'scores':
203+
return self.fitness_calculator.calculate_scores(responses)
204+
205+
elif target == 'values':
206+
return self.fitness_calculator.calculate_values(responses)
199207

200-
def evaluate_with_lists(self, param_list=None):
208+
def evaluate_with_lists(self, param_list=None, target='scores'):
201209
"""Run evaluation with lists as input and outputs"""
202210

203211
param_dict = self.param_dict(param_list)
204212

205-
obj_dict = self.evaluate_with_dicts(param_dict=param_dict)
213+
obj_dict = self.evaluate_with_dicts(
214+
param_dict=param_dict, target=target
215+
)
206216

207217
return self.objective_list(obj_dict)
208218

209-
def evaluate(self, param_list=None):
219+
def evaluate(self, param_list=None, target='scores'):
210220
"""Run evaluation with lists as input and outputs"""
211221

212-
return self.evaluate_with_lists(param_list)
222+
return self.evaluate_with_lists(param_list, target=target)
213223

214224
def __str__(self):
215225

bluepyopt/ephys/objectives.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ def calculate_feature_scores(self, responses):
4747

4848
return scores
4949

50+
def calculate_feature_values(self, responses):
51+
"""Calculate the value of an individual features"""
52+
53+
values = []
54+
for feature in self.features:
55+
values.append(feature.calculate_feature(responses))
56+
57+
return values
58+
5059

5160
class SingletonObjective(EFeatureObjective):
5261

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

6877
return self.calculate_feature_scores(responses)[0]
6978

79+
def calculate_value(self, responses):
80+
"""Objective value"""
81+
82+
return self.calculate_feature_values(responses)[0]
83+
7084
def __str__(self):
7185
"""String representation"""
7286

bluepyopt/ephys/objectivescalculators.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ def calculate_scores(self, responses):
4141
return {objective.name: objective.calculate_score(responses)
4242
for objective in self.objectives}
4343

44+
def calculate_values(self, responses):
45+
"""Calculator the value of each objective"""
46+
47+
return {objective.name: objective.calculate_value(responses)
48+
for objective in self.objectives}
49+
4450
def __str__(self):
4551
return 'objectives:\n %s' % '\n '.join(
4652
[str(obj) for obj in self.objectives]) \

bluepyopt/tests/test_ephys/test_evaluators.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ def test_CellEvaluator_evaluate():
110110
sim=sim)
111111

112112
responses = protocol.run(cell_model, {'cm': 1.0}, sim=sim)
113+
113114
feature_value = efeature.calculate_feature(responses)
115+
feature_value_eva = evaluator.evaluate_with_dicts(
116+
{'cm': 1.0}, target='values'
117+
)
114118

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

122126
nt.assert_almost_equal(score_dict['singleton'], expected_score)
127+
nt.assert_almost_equal(
128+
feature_value, feature_value_eva['singleton']
129+
)

bluepyopt/tests/test_ephys/test_objectives.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ def test_SingletonObjective():
7979
responses = {'square_pulse_step1.soma.v': response, }
8080

8181
efeature_value = efeature.calculate_feature(responses)
82+
efeature_value_obj = s_obj.calculate_value(responses)
8283

8384
nt.assert_almost_equal(
8485
s_obj.calculate_score(responses),
8586
abs(efeature_value - mean))
87+
nt.assert_almost_equal(efeature_value_obj, efeature_value)
8688

8789

8890
@attr('unit')

0 commit comments

Comments
 (0)