From 413e0e4c8754f55f8a91e9bbb960c9f3a2167a52 Mon Sep 17 00:00:00 2001 From: anilbey Date: Fri, 16 Feb 2024 09:24:23 +0100 Subject: [PATCH] apply small refactorings (#132) * replace identity comprehension with list constructor * simplify logic in add_replay_minis * replace range(0, val) with range(val) * raise BluecellulabError instead of Exception * shorten info_dict with dict comprehension * replace identity comprehension with list constructor * replace loop with sum-comprehensio --- bluecellulab/cell/core.py | 60 +++++++++++--------------- bluecellulab/circuit/circuit_access.py | 2 +- bluecellulab/plotwindow.py | 2 +- bluecellulab/psection.py | 7 +-- 4 files changed, 29 insertions(+), 42 deletions(-) diff --git a/bluecellulab/cell/core.py b/bluecellulab/cell/core.py index 735e7304..fd6778db 100644 --- a/bluecellulab/cell/core.py +++ b/bluecellulab/cell/core.py @@ -118,11 +118,11 @@ def __init__(self, # time recording needs this push self.soma.push() self.hocname = neuron.h.secname(sec=self.soma).split(".")[0] - self.somatic = [x for x in public_hoc_cell(self.cell).somatic] - self.basal = [x for x in public_hoc_cell(self.cell).basal] # dend is same as basal - self.apical = [x for x in public_hoc_cell(self.cell).apical] - self.axonal = [x for x in public_hoc_cell(self.cell).axonal] - self.all = [x for x in public_hoc_cell(self.cell).all] + self.somatic = list(public_hoc_cell(self.cell).somatic) + self.basal = list(public_hoc_cell(self.cell).basal) # dend is same as basal + self.apical = list(public_hoc_cell(self.cell).apical) + self.axonal = list(public_hoc_cell(self.cell).axonal) + self.all = list(public_hoc_cell(self.cell).all) self.record_dt = record_dt self.add_recordings(['self.soma(0.5)._ref_v', 'neuron.h._ref_t'], dt=self.record_dt) @@ -557,11 +557,7 @@ def add_replay_minis(self, syn_description, self ) - if 'Weight' in connection_modifiers: - weight_scalar = connection_modifiers['Weight'] - else: - weight_scalar = 1.0 - + weight_scalar = connection_modifiers.get('Weight', 1.0) exc_mini_frequency, inh_mini_frequency = mini_frequencies \ if mini_frequencies is not None else (None, None) @@ -570,11 +566,10 @@ def add_replay_minis(self, # SpontMinis in sim config takes precedence of values in nodes file if 'SpontMinis' in connection_modifiers: spont_minis_rate = connection_modifiers['SpontMinis'] + elif synapse.mech_name in ["GluSynapse", "ProbAMPANMDA_EMS"]: + spont_minis_rate = exc_mini_frequency else: - if synapse.mech_name in ["GluSynapse", "ProbAMPANMDA_EMS"]: - spont_minis_rate = exc_mini_frequency - else: - spont_minis_rate = inh_mini_frequency + spont_minis_rate = inh_mini_frequency if spont_minis_rate is not None and spont_minis_rate > 0: sec = self.get_hsection(post_sec_id) @@ -587,8 +582,7 @@ def add_replay_minis(self, self.syn_mini_netcons[synapse_id].delay = 0.1 self.syn_mini_netcons[synapse_id].weight[0] = weight * weight_scalar # set netcon type - nc_param_name = 'nc_type_param_{}'.format( - synapse.hsynapse).split('[')[0] + nc_param_name = f'nc_type_param_{synapse.hsynapse}'.split('[')[0] if hasattr(neuron.h, nc_param_name): nc_type_param = int(getattr(neuron.h, nc_param_name)) # NC_SPONTMINI @@ -652,7 +646,7 @@ def get_childrensections(self, parentsection: HocObjectType) -> list[HocObjectTy """Get the children section of a neuron section.""" number_children = neuron.h.SectionRef(sec=parentsection).nchild() children = [] - for index in range(0, int(number_children)): + for index in range(int(number_children)): children.append(neuron.h.SectionRef(sec=self.soma).child[index]) return children @@ -676,10 +670,10 @@ def getAxialCurrentRecording(self, section): self.get_recording(secname) self.get_recording(child) - def somatic_branches(self): + def somatic_branches(self) -> None: """Show the index numbers.""" nchild = neuron.h.SectionRef(sec=self.soma).nchild() - for index in range(0, int(nchild)): + for index in range(int(nchild)): secname = neuron.h.secname(sec=neuron.h.SectionRef( sec=self.soma).child[index]) if "axon" not in secname: @@ -696,9 +690,9 @@ def somatic_branches(self): public_hoc_cell(self.cell).nSecBasal + apicnumber) logger.info((apicnumber, secnumber)) else: - raise Exception( - "somaticbranches: No apic or \ - dend found in section %s" % secname) + raise BluecellulabError( + f"somaticbranches: No apic or dend found in section {secname}" + ) @staticmethod @deprecated("Use bluecellulab.cell.section_distance.EuclideanSectionDistance instead.") @@ -740,7 +734,7 @@ def apical_trunk(self): children = [ neuron.h.SectionRef(sec=max_diam_section).child[index] - for index in range(0, int(neuron.h.SectionRef( + for index in range(int(neuron.h.SectionRef( sec=max_diam_section).nchild()))] if len(children) == 0: break @@ -805,18 +799,14 @@ def add_synapse_replay( @property def info_dict(self): """Return a dictionary with all the information of this cell.""" - - cell_info = {} - - cell_info['synapses'] = {} - for sid, synapse in self.synapses.items(): - cell_info['synapses'][sid] = synapse.info_dict - - cell_info['connections'] = {} - for sid, connection in self.connections.items(): - cell_info['connections'][sid] = connection.info_dict - - return cell_info + return { + 'synapses': { + sid: synapse.info_dict for sid, synapse in self.synapses.items() + }, + 'connections': { + sid: connection.info_dict for sid, connection in self.connections.items() + } + } def delete(self): """Delete the cell.""" diff --git a/bluecellulab/circuit/circuit_access.py b/bluecellulab/circuit/circuit_access.py index 59ecb268..e42468c4 100644 --- a/bluecellulab/circuit/circuit_access.py +++ b/bluecellulab/circuit/circuit_access.py @@ -557,7 +557,7 @@ def extract_synapses( edges = self._circuit.edges # select edges that are in the projections, if there are projections if projections is None or len(projections) == 0: - edge_population_names = [x for x in edges] + edge_population_names = list(edges) elif isinstance(projections, str): edge_population_names = [x for x in edges if edges[x].source.name == projections] else: diff --git a/bluecellulab/plotwindow.py b/bluecellulab/plotwindow.py index 4ce0e510..86dc8a6a 100644 --- a/bluecellulab/plotwindow.py +++ b/bluecellulab/plotwindow.py @@ -52,7 +52,7 @@ def __init__(self, var_list, cell, xlim, ylim, title): # Sorry, don't see a way but disable this warning to access this colors = pylab.rcParams['axes.prop_cycle'].by_key()['color'] - linecolors = [x for x in itertools.islice(itertools.cycle(colors), 0, 50)] + linecolors = list(itertools.islice(itertools.cycle(colors), 0, 50)) self.line[var_name] = pylab.Line2D( time, recording, label=var_name, diff --git a/bluecellulab/psection.py b/bluecellulab/psection.py index 8ad8db30..a9ab8c6c 100644 --- a/bluecellulab/psection.py +++ b/bluecellulab/psection.py @@ -124,7 +124,7 @@ def getTreeVarBounds(self, variable): def getAllPDescendants(self): """Return all the psection that are descendants of this psection.""" - pdescendants = [x for x in self.pchildren] + pdescendants = list(self.pchildren) for child in self.pchildren: pdescendants += child.getAllPDescendants() return pdescendants @@ -158,10 +158,7 @@ def treeWidth(self): if self.isLeaf: treeWidth = self.maxsegdiam + self.xSpacing else: - treeWidth = 0 - for child in self.pchildren: - treeWidth += child.treeWidth() - + treeWidth = sum(child.treeWidth() for child in self.pchildren) return max(self.diam + self.xSpacing, treeWidth) def treeHeight(self):