Skip to content

Commit

Permalink
Python 3 exception handling in protocols, better create_hoc error mes…
Browse files Browse the repository at this point in the history
…sages
  • Loading branch information
lukasgd committed Oct 23, 2022
1 parent ebd32d0 commit 9bbc68e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 36 deletions.
50 changes: 34 additions & 16 deletions bluepyopt/ephys/create_hoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@

import jinja2
import bluepyopt
from bluepyopt.ephys.locations import NrnSectionCompLocation
from . import mechanisms
from bluepyopt.ephys.locations import (NrnSeclistCompLocation,
NrnSeclistLocation,
NrnSectionCompLocation,
NrnSomaDistanceCompLocation,
NrnSecSomaDistanceCompLocation,
NrnTrunkSomaDistanceCompLocation,
ArbLocation)

from bluepyopt.ephys.mechanisms import (Mechanism,
NrnMODMechanism,
NrnMODPointProcessMechanism)

from bluepyopt.ephys.parameters import (NrnGlobalParameter,
NrnSectionParameter,
Expand Down Expand Up @@ -47,7 +56,7 @@ def _generate_channels_by_location(mechs, location_order, loc_desc):
for mech in mechs:
name = mech.suffix
for location in mech.locations:
if isinstance(mech, mechanisms.NrnMODPointProcessMechanism):
if isinstance(mech, NrnMODPointProcessMechanism):
point_channels[loc_desc(location, mech)].append(mech)
else:
channels[loc_desc(location, mech)].append(name)
Expand All @@ -58,7 +67,7 @@ def _generate_reinitrng(mechs):
"""Create re_init_rng function"""

for mech in mechs:
if isinstance(mech, mechanisms.NrnMODPointProcessMechanism):
if isinstance(mech, NrnMODPointProcessMechanism):
raise NotImplementedError(
'HOC generation for models with point process mechanisms'
' is not yet supported.')
Expand All @@ -68,9 +77,9 @@ def _generate_reinitrng(mechs):
for mech in mechs:
reinitrng_hoc_blocks += mech.generate_reinitrng_hoc_block()

reinitrng_content = mechanisms.NrnMODMechanism.hash_hoc_string
reinitrng_content = NrnMODMechanism.hash_hoc_string

reinitrng_content += mechanisms.NrnMODMechanism.reinitrng_hoc_string % {
reinitrng_content += NrnMODMechanism.reinitrng_hoc_string % {
'reinitrng_hoc_blocks': reinitrng_hoc_blocks}

return reinitrng_content
Expand All @@ -92,15 +101,24 @@ def _range_exprs_to_hoc(range_params):
def _loc_desc(location, param_or_mech):
"""Generate Neuron location description for HOC template"""

if isinstance(param_or_mech, mechanisms.NrnMODMechanism):
# TODO this is dangerous, implicitly assumes type of location
return getattr(location, 'seclist_name', 'all')
elif isinstance(param_or_mech, mechanisms.NrnMODPointProcessMechanism):
raise CreateHocException("%s is currently not supported." %
type(param_or_mech).__name__)
# FIXME: NrnSectionCompLocation has no member seclist_name
elif not isinstance(param_or_mech, NrnPointProcessParameter) or \
not isinstance(param_or_mech, NrnSectionCompLocation):
if isinstance(param_or_mech, Mechanism):
if isinstance(param_or_mech, NrnMODMechanism):
if isinstance(location, NrnSeclistLocation):
return location.seclist_name
else:
raise CreateHocException(
"%s is currently not supported for mechs." %
type(location).__name__)
elif isinstance(param_or_mech, NrnMODPointProcessMechanism):
raise CreateHocException("%s is currently not supported." %
type(param_or_mech).__name__)
elif not (isinstance(location, NrnSeclistCompLocation) or
isinstance(location, NrnSectionCompLocation) or
isinstance(location, NrnSomaDistanceCompLocation) or
isinstance(location, NrnSecSomaDistanceCompLocation) or
isinstance(location, NrnTrunkSomaDistanceCompLocation)) and \
not isinstance(location, ArbLocation) and \
not isinstance(param_or_mech, NrnPointProcessParameter):
return location.seclist_name
else:
raise CreateHocException("%s is currently not supported." %
Expand All @@ -119,7 +137,7 @@ def _generate_parameters(parameters, location_order, loc_desc):
else:
assert isinstance(
param.locations, (tuple, list)), 'Must have locations list'
for location in param.locations: # FIXME: NrnSectionCompLocation
for location in param.locations:
locs = loc_desc(location, param)
if not isinstance(locs, list):
param_locations[locs].append(param)
Expand Down
16 changes: 9 additions & 7 deletions bluepyopt/ephys/locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ def acc_label(self):
raise EPhysLocAccException(
'%s not supported in Arbor' % type(self).__name__ +
' (uses branches instead of NEURON sections).'
' Use ArbBranchLocation/ArbSegmentLocation/ArbLocsetLocation'
' instead (consider using the Arbor GUI to identify the'
' precise branch/segment index and relative position).')
' Use ArbBranchRelLocation/ArbSegmentRelLocation/'
'ArbLocsetLocation instead (consider using the'
' Arbor GUI to identify the precise branch/segment index'
' and relative position).')

def __str__(self):
"""String representation"""
Expand Down Expand Up @@ -170,9 +171,10 @@ def acc_label(self):
raise EPhysLocAccException(
'%s not supported in Arbor' % type(self).__name__ +
' (uses branches instead of NEURON sections).'
' Use ArbBranchLocation/ArbSegmentLocation/ArbLocsetLocation'
' instead (consider using the Arbor GUI to identify the'
' precise branch/segment index and relative position).')
' Use ArbBranchRelLocation/ArbSegmentRelLocation/'
'ArbLocsetLocation instead (consider using the'
' Arbor GUI to identify the precise branch/segment index'
' and relative position).')

def __str__(self):
return '%s(%s)' % (self.sec_name, self.comp_x)
Expand Down Expand Up @@ -286,7 +288,7 @@ def acc_label(self):
raise EPhysLocAccException(
'%s not supported in Arbor' % type(self).__name__ +
' (uses branches instead of NEURON sections).'
' Use ArbBranchLocation/ArbSegmentLocation/ArbLocsetLocation'
' Use ArbBranchLocation/ArbSegmentLocation/ArbRegionLocation'
' instead (consider using the Arbor GUI to identify the'
' precise branch/segment index).')

Expand Down
40 changes: 27 additions & 13 deletions bluepyopt/ephys/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,9 @@ def _run_func(self, cell_model, param_values, sim=None):
cell_model.unfreeze(param_values.keys())

return responses
except BaseException:
import sys
import traceback
raise Exception(
"".join(
traceback.format_exception(*sys.exc_info())))
except BaseException as e:
raise SweepProtocolException(
'Failed to run Neuron Sweep Protocol') from e

@adjust_stochasticity
def run(
Expand Down Expand Up @@ -431,7 +428,7 @@ def _run_func(self, cell_json, param_values, sim=None):

try:
sim.run(arb_cell_model, tstop=self.total_duration)
except (RuntimeError, simulators.NrnSimulatorException):
except (RuntimeError, simulators.ArbSimulatorException):
logger.debug(
'ArbSweepProtocol: Running of parameter set {%s} '
'generated an exception, returning None in responses',
Expand All @@ -451,12 +448,9 @@ def _run_func(self, cell_json, param_values, sim=None):
arb_cell_model.traces)}

return responses
except BaseException:
import sys
import traceback
raise Exception(
"".join(
traceback.format_exception(*sys.exc_info())))
except BaseException as e:
raise ArbSweepProtocolException(
'Failed to run Arbor Sweep Protocol') from e

def run(
self,
Expand Down Expand Up @@ -633,3 +627,23 @@ def __str__(self):
content += ' %s\n' % str(recording)

return content


class SweepProtocolException(Exception):

"""All exceptions generated by SweepProtocol"""

def __init__(self, message):
"""Constructor"""

super(SweepProtocolException, self).__init__(message)


class ArbSweepProtocolException(Exception):

"""All exceptions generated by ArbSweepProtocol"""

def __init__(self, message):
"""Constructor"""

super(ArbSweepProtocolException, self).__init__(message)

0 comments on commit 9bbc68e

Please sign in to comment.