Skip to content

Commit

Permalink
Fix accessing afferent_section_pos of synapse description (#169)
Browse files Browse the repository at this point in the history
* Fix accessing afferent_section_pos

* pin libsonata<=0.1.25

* bluepy_circuit_access with AFFERENT_SECTION_POS

* simplify bluepy access' afferent section pos logic

* handle bluepy's exception for afferent_section_pos in props from/to bluepy
  • Loading branch information
anilbey authored Apr 30, 2024
1 parent 187fce6 commit 8db0f26
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
26 changes: 9 additions & 17 deletions bluecellulab/circuit/circuit_access/bluepy_circuit_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,23 +191,15 @@ def extract_synapses(

if isinstance(connectome._impl, SonataConnectome):
logger.debug('Using sonata style synapse file, not nrn.h5')
# load 'afferent_section_pos' instead of '_POST_DISTANCE'
if 'afferent_section_pos' in connectome.available_properties:
connectome_properties[
connectome_properties.index(SynapseProperty.POST_SEGMENT_OFFSET)
] = 'afferent_section_pos'

connectome_properties = properties_to_bluepy(connectome_properties)
synapses = connectome.afferent_synapses(
gid, properties=connectome_properties
)
synapses.columns = properties_from_bluepy(synapses.columns)
else:
connectome_properties = properties_to_bluepy(connectome_properties)
synapses = connectome.afferent_synapses(
gid, properties=connectome_properties
)
synapses.columns = properties_from_bluepy(synapses.columns)
connectome_properties.remove(SynapseProperty.POST_SEGMENT_OFFSET)
else: # afferent section_pos will be computed via post_segment_offset
connectome_properties.remove(SynapseProperty.AFFERENT_SECTION_POS)

connectome_properties = properties_to_bluepy(connectome_properties)
synapses = connectome.afferent_synapses(
gid, properties=connectome_properties
)
synapses.columns = properties_from_bluepy(synapses.columns)

synapses = synapses.reset_index(drop=True)
synapses.index = pd.MultiIndex.from_tuples(
Expand Down
22 changes: 15 additions & 7 deletions bluecellulab/circuit/synapse_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,15 @@ def properties_from_bluepy(
'str's."""
if not BLUEPY_AVAILABLE:
raise ExtraDependencyMissingError("bluepy")
return [
SynapseProperty.from_bluepy(prop)
if isinstance(prop, BLPSynapse)
else prop
for prop in props
]
res: list[SynapseProperty | str] = []
for prop in props:
if isinstance(prop, BLPSynapse):
res.append(SynapseProperty.from_bluepy(prop))
elif prop == "afferent_section_pos": # jira_url/project/issues/browse/NSETM-2313
res.append(SynapseProperty.AFFERENT_SECTION_POS)
else:
res.append(prop)
return res


def properties_to_bluepy(props: list[SynapseProperty | str]) -> list[BLPSynapse | str]:
Expand All @@ -154,14 +157,19 @@ def properties_to_bluepy(props: list[SynapseProperty | str]) -> list[BLPSynapse
# bluepy does not have AFFERENT_SECTION_POS atm.
# jira_url/project/issues/browse/NSETM-2313
bluepy_recognised_props = props.copy()
removed_afferent_section_pos = False
if SynapseProperty.AFFERENT_SECTION_POS in bluepy_recognised_props:
removed_afferent_section_pos = True
bluepy_recognised_props.remove(SynapseProperty.AFFERENT_SECTION_POS)
return [
res = [
prop.to_bluepy()
if isinstance(prop, SynapseProperty)
else prop
for prop in bluepy_recognised_props
]
if removed_afferent_section_pos:
res.append("afferent_section_pos")
return res


def synapse_property_encoder(dct: dict[SynapseProperty | str, Any]) -> dict[str, Any]:
Expand Down
6 changes: 3 additions & 3 deletions bluecellulab/synapse/synapse_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ def determine_synapse_location(cls, syn_description: pd.Series, cell: bluecellul
section: NeuronSection = cell.get_psection(section_id=isec).hsection

# old circuits don't have it, it needs to be computed via synlocation_to_segx
if ("afferent_section_pos" in syn_description and
not np.isnan(syn_description["afferent_section_pos"])):
if (SynapseProperty.AFFERENT_SECTION_POS in syn_description and
not np.isnan(syn_description[SynapseProperty.AFFERENT_SECTION_POS])):
# position is pre computed in SONATA
location = syn_description["afferent_section_pos"]
location = syn_description[SynapseProperty.AFFERENT_SECTION_POS]
else:
ipt = syn_description[SynapseProperty.POST_SEGMENT_ID]
syn_offset = syn_description[SynapseProperty.POST_SEGMENT_OFFSET]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_synapse/test_synapse_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_determine_synapse_location(self):
res = SynapseFactory.determine_synapse_location(self.syn_description, self.cell)
assert res.location == 0.9999999
# set afferent_section_pos
self.syn_description["afferent_section_pos"] = 1.2
self.syn_description[SynapseProperty.AFFERENT_SECTION_POS] = 1.2
res = SynapseFactory.determine_synapse_location(self.syn_description, self.cell)
assert res.location == 1.2
assert res.section.L == pytest.approx(9.530376893488256)
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ deps =
pytest-timeout>=2.1.0
pytest-xdist>=3.3.1 # multiprocessing
pytest-forked>=1.6.0 # isolation
libsonata<=0.1.25 # 0.1.26 supports h5 synapse_replay only, some of our tests atm use .dat
download = true
allowlist_externals =
make
Expand Down

0 comments on commit 8db0f26

Please sign in to comment.