|
| 1 | +"""Rules classes""" |
| 2 | + |
| 3 | +""" |
| 4 | +Copyright (c) 2016-2020, EPFL/Blue Brain Project |
| 5 | +
|
| 6 | + This file is part of BluePyOpt <https://github.com/BlueBrain/BluePyOpt> |
| 7 | +
|
| 8 | + This library is free software; you can redistribute it and/or modify it under |
| 9 | + the terms of the GNU Lesser General Public License version 3.0 as published |
| 10 | + by the Free Software Foundation. |
| 11 | +
|
| 12 | + This library is distributed in the hope that it will be useful, but WITHOUT |
| 13 | + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 14 | + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 15 | + details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General Public License |
| 18 | + along with this library; if not, write to the Free Software Foundation, Inc., |
| 19 | + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | +""" |
| 21 | +import logging |
| 22 | + |
| 23 | +import bluepyopt |
| 24 | +import math |
| 25 | + |
| 26 | +logger = logging.getLogger(__name__) |
| 27 | + |
| 28 | + |
| 29 | +def sigmoid(x): |
| 30 | + """Sigmoid function""" |
| 31 | + return 1 / (1 + math.exp(-x)) |
| 32 | + |
| 33 | + |
| 34 | +class Rule(): |
| 35 | + |
| 36 | + """Abstract Rule class""" |
| 37 | + |
| 38 | + name = "" |
| 39 | + |
| 40 | + def __init__( |
| 41 | + self, |
| 42 | + name=None, |
| 43 | + force_max_score=False, |
| 44 | + max_score=250 |
| 45 | + ): |
| 46 | + """Constructor |
| 47 | + Args: |
| 48 | + name (str): name of the eFELFeature object |
| 49 | + force_max_score (bool): should the max_score limit be applied to |
| 50 | + the score |
| 51 | + max_score (float): upper bound for the score |
| 52 | + """ |
| 53 | + |
| 54 | + if name: |
| 55 | + self.name = name |
| 56 | + |
| 57 | + self.force_max_score = force_max_score |
| 58 | + self.max_score = max_score |
| 59 | + |
| 60 | + def _rule(self, cell_model): |
| 61 | + return None |
| 62 | + |
| 63 | + def _loss_function(self, value): |
| 64 | + return None |
| 65 | + |
| 66 | + def calculate_value(self, cell_model): |
| 67 | + """Calculate rule value""" |
| 68 | + |
| 69 | + if cell_model is None: |
| 70 | + rule_value = None |
| 71 | + else: |
| 72 | + rule_value = self._rule(cell_model) |
| 73 | + |
| 74 | + logger.debug( |
| 75 | + 'Calculated value for %s: %s', |
| 76 | + self.name, |
| 77 | + str(rule_value) |
| 78 | + ) |
| 79 | + |
| 80 | + return rule_value |
| 81 | + |
| 82 | + def calculate_score(self, cell_model): |
| 83 | + """Calculate the score""" |
| 84 | + |
| 85 | + if cell_model is None: |
| 86 | + score = self.max_score |
| 87 | + else: |
| 88 | + score = self._loss_function(self._rule(cell_model)) |
| 89 | + |
| 90 | + if self.force_max_score: |
| 91 | + score = min(score, self.max_score) |
| 92 | + |
| 93 | + logger.debug('Calculated score for %s: %f', self.name, score) |
| 94 | + |
| 95 | + return score |
| 96 | + |
| 97 | + def __str__(self): |
| 98 | + """String representation""" |
| 99 | + |
| 100 | + return "Rule %s " % (self.name) |
| 101 | + |
| 102 | + |
| 103 | +class SumConductivityRule(Rule): |
| 104 | + |
| 105 | + """SumConductivityRule class |
| 106 | +
|
| 107 | + Punish high sum of conductivity based on a sigmoid loss function |
| 108 | + """ |
| 109 | + |
| 110 | + name = "SumConductivity" |
| 111 | + |
| 112 | + def __init__( |
| 113 | + self, |
| 114 | + name=None, |
| 115 | + force_max_score=False, |
| 116 | + max_score=250, |
| 117 | + conductivity_target=2. |
| 118 | + ): |
| 119 | + """Constructor |
| 120 | + Args: |
| 121 | + name (str): name of the eFELFeature object |
| 122 | + force_max_score (bool): should the max_score limit be applied to |
| 123 | + the score |
| 124 | + max_score (float): upper bound for the score |
| 125 | + """ |
| 126 | + |
| 127 | + super(SumConductivityRule, self).__init__( |
| 128 | + name=name, |
| 129 | + force_max_score=force_max_score, |
| 130 | + max_score=max_score |
| 131 | + ) |
| 132 | + |
| 133 | + self.conductivity_target = conductivity_target |
| 134 | + |
| 135 | + def _rule(self, cell_model): |
| 136 | + |
| 137 | + sum_g = 0 |
| 138 | + for param in cell_model.params.values(): |
| 139 | + if param.name[0] == "g": |
| 140 | + sum_g += param.value |
| 141 | + |
| 142 | + return sum_g |
| 143 | + |
| 144 | + def _loss_function(self, value): |
| 145 | + |
| 146 | + # Center on conductivity_target |
| 147 | + _ = (value / self.conductivity_target) - 1. |
| 148 | + |
| 149 | + # Take the sigmoid |
| 150 | + return self.max_score * sigmoid(_) |
| 151 | + |
0 commit comments