From d99c5c88bcfc272fd6e179604fbe63beb1e0b838 Mon Sep 17 00:00:00 2001 From: Anil Tuncel Date: Wed, 14 Feb 2024 15:35:48 +0100 Subject: [PATCH 1/7] replace identity comprehension with list constructor --- bluecellulab/cell/core.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bluecellulab/cell/core.py b/bluecellulab/cell/core.py index 735e7304..b7ec5cf0 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) From 915cbd7b307d49eb4c8fcdbd722ca0e80928bc00 Mon Sep 17 00:00:00 2001 From: Anil Tuncel Date: Wed, 14 Feb 2024 15:45:48 +0100 Subject: [PATCH 2/7] simplify logic in add_replay_minis --- bluecellulab/cell/core.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/bluecellulab/cell/core.py b/bluecellulab/cell/core.py index b7ec5cf0..7ca9dd01 100644 --- a/bluecellulab/cell/core.py +++ b/bluecellulab/cell/core.py @@ -557,38 +557,32 @@ 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) + if mini_frequencies is not None else (None, None) synapse = self.synapses[synapse_id] # 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) # add the *minis*: spontaneous synaptic events self.ips[synapse_id] = neuron.h.\ - InhPoissonStim(location, sec=sec) + InhPoissonStim(location, sec=sec) self.syn_mini_netcons[synapse_id] = neuron.h.\ - NetCon(self.ips[synapse_id], synapse.hsynapse, sec=sec) + NetCon(self.ips[synapse_id], synapse.hsynapse, sec=sec) 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 @@ -596,7 +590,7 @@ def add_replay_minis(self, if self.rng_settings.mode == 'Random123': seed2 = source_popid * 65536 + target_popid \ - + self.rng_settings.minis_seed + + self.rng_settings.minis_seed self.ips[synapse_id].setRNGs( sid + 200, self.cell_id.id + 250, From 55f9c3b1c2ede2f580fac4863cf1f586d9fcb10a Mon Sep 17 00:00:00 2001 From: Anil Tuncel Date: Wed, 14 Feb 2024 15:46:11 +0100 Subject: [PATCH 3/7] replace range(0, val) with range(val) --- bluecellulab/cell/core.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bluecellulab/cell/core.py b/bluecellulab/cell/core.py index 7ca9dd01..a6da3afa 100644 --- a/bluecellulab/cell/core.py +++ b/bluecellulab/cell/core.py @@ -646,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 @@ -673,7 +673,7 @@ def getAxialCurrentRecording(self, section): def somatic_branches(self): """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: @@ -734,8 +734,8 @@ def apical_trunk(self): children = [ neuron.h.SectionRef(sec=max_diam_section).child[index] - for index in range(0, int(neuron.h.SectionRef( - sec=max_diam_section).nchild()))] + for index in range(int(neuron.h.SectionRef( + sec=max_diam_section).nchild()))] if len(children) == 0: break maxdiam = 0 From 6ff14918b2108498a0a42c8bb05b65a8409240bc Mon Sep 17 00:00:00 2001 From: Anil Tuncel Date: Wed, 14 Feb 2024 15:54:16 +0100 Subject: [PATCH 4/7] raise BluecellulabError instead of Exception --- bluecellulab/cell/core.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/bluecellulab/cell/core.py b/bluecellulab/cell/core.py index a6da3afa..b5f7600a 100644 --- a/bluecellulab/cell/core.py +++ b/bluecellulab/cell/core.py @@ -559,7 +559,7 @@ def add_replay_minis(self, 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) + if mini_frequencies is not None else (None, None) synapse = self.synapses[synapse_id] @@ -575,10 +575,10 @@ def add_replay_minis(self, sec = self.get_hsection(post_sec_id) # add the *minis*: spontaneous synaptic events self.ips[synapse_id] = neuron.h.\ - InhPoissonStim(location, sec=sec) + InhPoissonStim(location, sec=sec) self.syn_mini_netcons[synapse_id] = neuron.h.\ - NetCon(self.ips[synapse_id], synapse.hsynapse, sec=sec) + NetCon(self.ips[synapse_id], synapse.hsynapse, sec=sec) self.syn_mini_netcons[synapse_id].delay = 0.1 self.syn_mini_netcons[synapse_id].weight[0] = weight * weight_scalar # set netcon type @@ -590,7 +590,7 @@ def add_replay_minis(self, if self.rng_settings.mode == 'Random123': seed2 = source_popid * 65536 + target_popid \ - + self.rng_settings.minis_seed + + self.rng_settings.minis_seed self.ips[synapse_id].setRNGs( sid + 200, self.cell_id.id + 250, @@ -670,7 +670,7 @@ 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(int(nchild)): @@ -690,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.") @@ -735,7 +735,7 @@ def apical_trunk(self): children = [ neuron.h.SectionRef(sec=max_diam_section).child[index] for index in range(int(neuron.h.SectionRef( - sec=max_diam_section).nchild()))] + sec=max_diam_section).nchild()))] if len(children) == 0: break maxdiam = 0 From f3d966d6c82b5fbcb3072e348021da3133438179 Mon Sep 17 00:00:00 2001 From: Anil Tuncel Date: Wed, 14 Feb 2024 16:00:29 +0100 Subject: [PATCH 5/7] shorten info_dict with dict comprehension --- bluecellulab/cell/core.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/bluecellulab/cell/core.py b/bluecellulab/cell/core.py index b5f7600a..fd6778db 100644 --- a/bluecellulab/cell/core.py +++ b/bluecellulab/cell/core.py @@ -799,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.""" From d0a1dba5274b5880c7c1de3524339ca87fe24303 Mon Sep 17 00:00:00 2001 From: Anil Tuncel Date: Wed, 14 Feb 2024 16:16:06 +0100 Subject: [PATCH 6/7] replace identity comprehension with list constructor --- bluecellulab/circuit/circuit_access.py | 2 +- bluecellulab/plotwindow.py | 2 +- bluecellulab/psection.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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..70ef5601 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 From ab3bf30c8bdafbc397304226a29eba042b4ea73a Mon Sep 17 00:00:00 2001 From: Anil Tuncel Date: Wed, 14 Feb 2024 17:24:46 +0100 Subject: [PATCH 7/7] replace loop with sum-comprehensio --- bluecellulab/psection.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bluecellulab/psection.py b/bluecellulab/psection.py index 70ef5601..a9ab8c6c 100644 --- a/bluecellulab/psection.py +++ b/bluecellulab/psection.py @@ -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):