Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
Added hoc modifier to models
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanguy Damart committed Mar 5, 2021
1 parent 1c71bef commit db79093
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 20 deletions.
11 changes: 9 additions & 2 deletions bluepyopt/deapext/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import random
import logging
import os
import shutil

import deap.algorithms
import deap.tools
Expand Down Expand Up @@ -94,6 +96,9 @@ def eaAlphaMuPlusLambdaCheckpoint(
continue_cp(bool): whether to continue
"""

if cp_filename:
cp_filename_tmp = str(cp_filename) + '.tmp'

if continue_cp:
# A file name has been given, then load the data from the file
cp = pickle.load(open(cp_filename, "rb"))
Expand Down Expand Up @@ -149,8 +154,10 @@ def eaAlphaMuPlusLambdaCheckpoint(
history=history,
logbook=logbook,
rndstate=random.getstate())
pickle.dump(cp, open(cp_filename, "wb"))
logger.debug('Wrote checkpoint to %s', cp_filename)
pickle.dump(cp, open(cp_filename_tmp, "wb"))
if os.path.isfile(cp_filename_tmp):
shutil.copy(cp_filename_tmp, cp_filename)
logger.debug('Wrote checkpoint to %s', cp_filename)

gen += 1
stopping_params["gen"] = gen
Expand Down
12 changes: 9 additions & 3 deletions bluepyopt/deapext/optimisationsCMA.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import pickle
import random
import functools
import os
import shutil

import deap.tools

Expand Down Expand Up @@ -223,7 +225,9 @@ def run(self,
"""

stats = self.get_stats()

if cp_filename:
cp_filename_tmp = str(cp_filename) + '.tmp'

if continue_cp:

# A file name has been given, then load the data from the file
Expand Down Expand Up @@ -308,8 +312,10 @@ def run(self,
rndstate=random.getstate(),
np_rndstate=numpy.random.get_state(),
CMA_es=CMA_es)
pickle.dump(cp, open(cp_filename, "wb"))
logger.debug('Wrote checkpoint to %s', cp_filename)
pickle.dump(cp, open(cp_filename_tmp, "wb"))
if os.path.isfile(cp_filename_tmp):
shutil.copy(cp_filename_tmp, cp_filename)
logger.debug('Wrote checkpoint to %s', cp_filename)

CMA_es.map_function = temp_mf

Expand Down
58 changes: 47 additions & 11 deletions bluepyopt/ephys/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Cell template class"""

"""
Copyright (c) 2016, EPFL/Blue Brain Project
Copyright (c) 2016-2020, EPFL/Blue Brain Project
This file is part of BluePyOpt <https://github.com/BlueBrain/BluePyOpt>
Expand Down Expand Up @@ -65,12 +65,15 @@ class CellModel(Model):
"""Cell model class"""

def __init__(
self,
name,
morph=None,
mechs=None,
params=None,
gid=0):
self,
name,
morph=None,
mechs=None,
params=None,
gid=0,
seclist_names=None,
secarray_names=None
):
"""Constructor
Args:
Expand All @@ -83,8 +86,14 @@ def __init__(
Mechanisms associated with the cell
params (list of Parameters):
Parameters of the cell model
seclist_names (list of strings):
Names of the lists of sections
secarray_names (list of strings):
Names of the sections
"""

super(CellModel, self).__init__(name)

self.check_name()
self.morphology = morph
self.mechanisms = mechs
Expand All @@ -98,10 +107,20 @@ def __init__(

self.param_values = None
self.gid = gid
self.seclist_names = \
['all', 'somatic', 'basal', 'apical', 'axonal', 'myelinated']
self.secarray_names = \
['soma', 'dend', 'apic', 'axon', 'myelin']

if seclist_names is None:
self.seclist_names = [
'all', 'somatic', 'basal', 'apical', 'axonal', 'myelinated'
]
else:
self.seclist_names = seclist_names

if secarray_names is None:
self.secarray_names = [
'soma', 'dend', 'apic', 'axon', 'myelin'
]
else:
self.secarray_names = secarray_names

def check_name(self):
"""Check if name complies with requirements"""
Expand Down Expand Up @@ -282,6 +301,22 @@ def create_hoc(self, param_values,
else:
replace_axon = None

if (
self.morphology.morph_modifiers is not None
and self.morphology.morph_modifiers_hoc is None
):
logger.warning('You have provided custom morphology modifiers, \
but no corresponding hoc files.')
elif (
self.morphology.morph_modifiers is not None
and self.morphology.morph_modifiers_hoc is not None
):
if replace_axon is None:
replace_axon = ''
for morph_modifier_hoc in self.morphology.morph_modifiers_hoc:
replace_axon += '\n'
replace_axon += morph_modifier_hoc

ret = create_hoc.create_hoc(mechs=self.mechanisms,
parameters=self.params.values(),
morphology=morphology,
Expand Down Expand Up @@ -637,6 +672,7 @@ def instantiate(self, sim=None):
self.LFPyCell = LFPy.Cell(morphology=sim.neuron.h.allsec(),
dt=self.dt,
v_init=self.v_init,
pt3d=True,
delete_sections=False,
nsegs_method=None)

Expand Down
9 changes: 8 additions & 1 deletion bluepyopt/ephys/morphologies.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,14 @@ def instantiate(self, sim=None, icell=None):
if self.morph_modifiers is not None:
for morph_modifier in self.morph_modifiers:
morph_modifier(sim=sim, icell=icell)


if self.morph_modifiers_hoc is not None:
hoc_modifiers = ''
for morph_modifier_hoc in self.morph_modifiers_hoc:
hoc_modifiers += morph_modifier_hoc
hoc_modifiers += '\n'
sim.neuron.h(hoc_modifiers)

def destroy(self, sim=None):
"""Destroy morphology instantiation"""
pass
Expand Down
1 change: 1 addition & 0 deletions bluepyopt/ephys/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ def instantiate(self, sim=None, icell=None):
for location in self.locations:
iseclist = location.instantiate(sim=sim, icell=icell)
for section in iseclist:
print(section, self.param_name)
setattr(section, self.param_name,
self.value_scale_func(self.value, section, sim=sim))
logger.debug(
Expand Down
14 changes: 11 additions & 3 deletions bluepyopt/ephys/parameterscalers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Parameter scaler classes"""

"""
Copyright (c) 2016, EPFL/Blue Brain Project
Copyright (c) 2016-2020, EPFL/Blue Brain Project
This file is part of BluePyOpt <https://github.com/BlueBrain/BluePyOpt>
Expand Down Expand Up @@ -96,7 +96,8 @@ def __init__(
name=None,
distribution=None,
comment='',
dist_param_names=None):
dist_param_names=None,
soma_ref_location=0.5):
"""Constructor
Args:
Expand All @@ -110,12 +111,19 @@ def __init__(
object.
The distribution string should contain these names, and they
will be replaced by values of the corresponding attributes
soma_ref_location (float): location along the soma used as origin
from which to compute the distances. Expressed as a fraction
(between 0.0 and 1.0).
"""

super(NrnSegmentSomaDistanceScaler, self).__init__(name, comment)
self.distribution = distribution

self.dist_param_names = dist_param_names
self.soma_ref_location = soma_ref_location

if not(0. <= self.soma_ref_location <= 1.):
raise ValueError('soma_ref_location must be between 0 and 1.')

if self.dist_param_names is not None:
for dist_param_name in self.dist_param_names:
Expand Down Expand Up @@ -160,7 +168,7 @@ def scale(self, value, segment, sim=None):
soma = segment.sec.cell().soma[0]

# Initialise origin
sim.neuron.h.distance(0, 0.5, sec=soma)
sim.neuron.h.distance(0, self.soma_ref_location, sec=soma)

distance = sim.neuron.h.distance(1, segment.x, sec=segment.sec)

Expand Down

0 comments on commit db79093

Please sign in to comment.