Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Fixed bug in clear_connectivity and improved tests for deleting drives #613

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions examples/howto/plot_connectivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
def get_network(probability=1.0):
net = jones_2009_model(add_drives_from_params=True)
net.clear_connectivity()
net.clear_drives()
jasmainak marked this conversation as resolved.
Show resolved Hide resolved

# Pyramidal cell connections
location, receptor = 'distal', 'ampa'
Expand Down
51 changes: 39 additions & 12 deletions hnn_core/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,23 +1253,50 @@ def add_connection(self, src_gids, target_gids, loc, receptor,

self.connectivity.append(deepcopy(conn))

def clear_connectivity(self):
"""Remove all connections defined in Network.connectivity
def clear_connectivity(self, src_types=None):
"""Remove connections with src_type in Network.connectivity.

Parameters
----------
src_types : list | None
Source types of connections to be removed
jasmainak marked this conversation as resolved.
Show resolved Hide resolved
None - Remove all connections other than those which have an
external drive as a source type
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@raj1701 I think you have been struggling with the logic because you are trying to preserve the old behavior and logic without fully understanding the purpose of that behavior. You need to clarify the behavior before writing the logic. To me, what makes the most sense is:

net.clear_connectivity()  # clear all connections
net.clear_connectivity(src_types='drives')  # clear connections from drives to cells
net.clear_connectivity(src_types='local')  # clear connections within cells
net.clear_connectivity(src_types=['L2_Pyramidal', 'L5_basket'])  # clear connection starting with cell populations L2_pyramidal and L5_basket

To do this, you should use pick_connection and avoid rewriting the logic of finding the appropriate connections. Once you have the indices of the connections, you can write a simple for loop to delete the items:

for conn_idx in conn_idxs:
   del net.connectivity[conn_idx]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes @jasmainak I was trying to preserve the old logic. If no argument is passed to clear_connectivity then the default value of src_types should be called "All" instead of "None". Will refactor the code accordingly and make the required changes.

"""
connectivity = list()
if src_types is None:
src_types = list()
for conn in self.connectivity:
src_type = conn['src_type']
# src_type should not be a external drive in this case
if src_type not in self.drive_names:
src_types.append(src_type) # Store src_types to be deleted
src_types = list(set(src_types)) # Removing duplicate entries
connectivity = list() # Initialize empty list
for conn in self.connectivity:
if conn['src_type'] in self.external_drives.keys():
# Removes connections in src_types
if conn['src_type'] not in src_types:
connectivity.append(conn)
self.connectivity = connectivity

def clear_drives(self):
"""Remove all drives defined in Network.connectivity"""
connectivity = list()
for conn in self.connectivity:
if conn['src_type'] not in self.external_drives.keys():
connectivity.append(conn)
self.external_drives = dict()
self.connectivity = connectivity
def clear_drives(self, drive_names='all'):
"""Remove all drives defined in Network.connectivity.

Parameters
----------
drive_names : list | 'all'
The drive_names to remove
"""
if drive_names == 'all':
drive_names = self.drive_names
_validate_type(drive_names, (list,))
for drive_name in drive_names:
del self.external_drives[drive_name]
self.clear_connectivity(src_types=drive_names)

@property
def drive_names(self):
"""Returns a list containing names of all external drives."""
return list(self.external_drives.keys())

def add_electrode_array(self, name, electrode_pos, *, conductivity=0.3,
method='psa', min_distance=0.5):
Expand Down
20 changes: 19 additions & 1 deletion hnn_core/tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,8 +784,26 @@ def test_network_connectivity():
# Needs to be updated if number of drives change in preceeding tests
net.clear_connectivity()
assert len(net.connectivity) == 4 # 2 drives x 2 target cell types

# Only external drive connections are left
# Testing deletion of a custom number of drives

# Deleting one external drive
all_drives = net.drive_names
drives_to_be_deleted = all_drives[0:1]
connectivities_to_be_deleted = 0
for drive in drives_to_be_deleted:
drive_connections = len(pick_connection(net, src_gids=drive))
connectivities_to_be_deleted = (connectivities_to_be_deleted +
drive_connections)
expected_connectivity_length = (len(net.connectivity) -
connectivities_to_be_deleted)
net.clear_drives(drive_names=drives_to_be_deleted)
assert len(net.connectivity) == expected_connectivity_length

# Deleting all remaining external drives
net.clear_drives()
assert len(net.connectivity) == 0
assert len(net.connectivity) == 0 # All connections are deleted
Copy link
Collaborator

@jasmainak jasmainak Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clear_drives should still leave the connections within the cortical column ... were there no connections within the cell populations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Earlier clear_connectivity() was called which deleted all connections other than the ones involving external drives. That was pushed by the previous pull request i think, the one I got merge conflict with so I kept that change


with pytest.warns(UserWarning, match='No connections'):
simulate_dipole(net, tstop=10)
Expand Down