|
| 1 | +"""Run simple cell optimisation""" |
| 2 | + |
| 3 | +""" |
| 4 | +Copyright (c) 2016, EPFL/Blue Brain Project |
| 5 | + This file is part of BluePyOpt <https://github.com/BlueBrain/BluePyOpt> |
| 6 | + This library is free software; you can redistribute it and/or modify it under |
| 7 | + the terms of the GNU Lesser General Public License version 3.0 as published |
| 8 | + by the Free Software Foundation. |
| 9 | + This library is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 12 | + details. |
| 13 | + You should have received a copy of the GNU Lesser General Public License |
| 14 | + along with this library; if not, write to the Free Software Foundation, Inc., |
| 15 | + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | +""" |
| 17 | +# pylint: disable=R0914 |
| 18 | + |
| 19 | +import os |
| 20 | +import json |
| 21 | + |
| 22 | +#import l5pc_model # NOQA |
| 23 | + |
| 24 | +import bluepyopt.ephys as ephys |
| 25 | + |
| 26 | +script_dir = os.path.dirname(__file__) |
| 27 | +config_dir = os.path.join(script_dir, 'config') |
| 28 | + |
| 29 | +# TODO store definition dicts in json |
| 30 | +# TODO rename 'score' into 'objective' |
| 31 | +# TODO add functionality to read settings of every object from config format |
| 32 | + |
| 33 | +def define_protocols(): |
| 34 | + """Define protocols""" |
| 35 | + |
| 36 | + protocol_definitions = json.load( |
| 37 | + open( |
| 38 | + os.path.join( |
| 39 | + config_dir, |
| 40 | + 'protocols.json'))) |
| 41 | + |
| 42 | + protocols = {} |
| 43 | + |
| 44 | + soma_loc = ephys.locations.NrnSeclistCompLocation( |
| 45 | + name='soma', |
| 46 | + seclist_name='somatic', |
| 47 | + sec_index=0, |
| 48 | + comp_x=0.5) |
| 49 | + |
| 50 | + for protocol_name, protocol_definition in protocol_definitions.iteritems(): |
| 51 | + # By default include somatic recording |
| 52 | + somav_recording = ephys.recordings.CompRecording( |
| 53 | + name='%s.soma.v' % |
| 54 | + protocol_name, |
| 55 | + location=soma_loc, |
| 56 | + variable='v') |
| 57 | + |
| 58 | + recordings = [somav_recording] |
| 59 | + |
| 60 | + if 'extra_recordings' in protocol_definition: |
| 61 | + for recording_definition in protocol_definition['extra_recordings']: |
| 62 | + if recording_definition['type'] == 'somadistance': |
| 63 | + location = ephys.locations.NrnSomaDistanceCompLocation( |
| 64 | + name=recording_definition['name'], |
| 65 | + soma_distance=recording_definition['somadistance'], |
| 66 | + seclist_name=recording_definition['seclist_name']) |
| 67 | + var = recording_definition['var'] |
| 68 | + recording = ephys.recordings.CompRecording( |
| 69 | + name='%s.%s.%s' % (protocol_name, location.name, var), |
| 70 | + location=location, |
| 71 | + variable=recording_definition['var']) |
| 72 | + |
| 73 | + recordings.append(recording) |
| 74 | + else: |
| 75 | + raise Exception( |
| 76 | + 'Recording type %s not supported' % |
| 77 | + recording_definition['type']) |
| 78 | + |
| 79 | + stimuli = [] |
| 80 | + for stimulus_definition in protocol_definition['stimuli']: |
| 81 | + stimuli.append(ephys.stimuli.NrnSquarePulse( |
| 82 | + step_amplitude=stimulus_definition['amp'], |
| 83 | + step_delay=stimulus_definition['delay'], |
| 84 | + step_duration=stimulus_definition['duration'], |
| 85 | + location=soma_loc, |
| 86 | + total_duration=stimulus_definition['totduration'])) |
| 87 | + |
| 88 | + protocols[protocol_name] = ephys.protocols.SweepProtocol( |
| 89 | + protocol_name, |
| 90 | + stimuli, |
| 91 | + recordings) |
| 92 | + |
| 93 | + return protocols |
| 94 | + |
| 95 | + |
| 96 | +def define_fitness_calculator(protocols): |
| 97 | + """Define fitness calculator""" |
| 98 | + |
| 99 | + feature_definitions = json.load( |
| 100 | + open( |
| 101 | + os.path.join( |
| 102 | + config_dir, |
| 103 | + 'features.json'))) |
| 104 | + |
| 105 | + # TODO: add bAP stimulus |
| 106 | + objectives = [] |
| 107 | + |
| 108 | + for protocol_name, locations in feature_definitions.iteritems(): |
| 109 | + for location, features in locations.iteritems(): |
| 110 | + for efel_feature_name, meanstd in features.iteritems(): |
| 111 | + feature_name = '%s.%s.%s' % ( |
| 112 | + protocol_name, location, efel_feature_name) |
| 113 | + recording_names = {'': '%s.%s.v' % (protocol_name, location)} |
| 114 | + stimulus = protocols[protocol_name].stimuli[0] |
| 115 | + |
| 116 | + stim_start = stimulus.step_delay |
| 117 | + |
| 118 | + if location == 'soma': |
| 119 | + threshold = -20 |
| 120 | + elif 'dend' in location: |
| 121 | + threshold = -55 |
| 122 | + |
| 123 | + if protocol_name == 'bAP': |
| 124 | + stim_end = stimulus.total_duration |
| 125 | + else: |
| 126 | + stim_end = stimulus.step_delay + stimulus.step_duration |
| 127 | + |
| 128 | + feature = ephys.efeatures.eFELFeature( |
| 129 | + feature_name, |
| 130 | + efel_feature_name=efel_feature_name, |
| 131 | + recording_names=recording_names, |
| 132 | + stim_start=stim_start, |
| 133 | + stim_end=stim_end, |
| 134 | + exp_mean=meanstd[0], |
| 135 | + exp_std=meanstd[1], |
| 136 | + threshold=threshold) |
| 137 | + objective = ephys.objectives.SingletonObjective( |
| 138 | + feature_name, |
| 139 | + feature) |
| 140 | + objectives.append(objective) |
| 141 | + |
| 142 | + fitcalc = ephys.objectivescalculators.ObjectivesCalculator(objectives) |
| 143 | + |
| 144 | + return fitcalc |
| 145 | + |
| 146 | + |
| 147 | +# def create(): |
| 148 | +# """Setup""" |
| 149 | + |
| 150 | +# l5pc_cell = l5pc_model.create() |
| 151 | + |
| 152 | +# fitness_protocols = define_protocols() |
| 153 | +# fitness_calculator = define_fitness_calculator(fitness_protocols) |
| 154 | + |
| 155 | +# param_names = [param.name |
| 156 | +# for param in l5pc_cell.params.values() |
| 157 | +# if not param.frozen] |
| 158 | + |
| 159 | +# sim = ephys.simulators.NrnSimulator() |
| 160 | + |
| 161 | +# return ephys.evaluators.CellEvaluator( |
| 162 | +# cell_model=l5pc_cell, |
| 163 | +# param_names=param_names, |
| 164 | +# fitness_protocols=fitness_protocols, |
| 165 | +# fitness_calculator=fitness_calculator, |
| 166 | +# sim=sim) |
0 commit comments