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
2 changes: 1 addition & 1 deletion examples/howto/plot_connectivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
# directly.
def get_network(probability=1.0):
net = jones_2009_model(add_drives_from_params=True)
net.clear_connectivity()
net.connectivity = list()
Copy link
Collaborator

Choose a reason for hiding this comment

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

undo this since you have clear_connectivity function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure will do

Copy link
Contributor Author

@raj1701 raj1701 Mar 9, 2023

Choose a reason for hiding this comment

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

Here net.connectivity needs to be completely cleared.
So both clear_connectivity() and clear_drives() will need to be called one by one.

  • clear_connectivity() will leave connections of external drives open. For deleting them clear_drives will need to be called.
  • But if there are existing external drive connections only clear_connectivity needs to be called....

Which direction is better?


# Pyramidal cell connections
location, receptor = 'distal', 'ampa'
Expand Down
50 changes: 38 additions & 12 deletions hnn_core/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,23 +1253,49 @@ 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
"""
connectivity = list()
if src_types is None:
src_types = list()
# Storing all external drives
src_types_external_drives = self.external_drives.keys()
for conn in self.connectivity:
src_type = conn['src_type']
if src_type not in src_types_external_drives:
src_types.append(src_type) # Storing drives 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 = list(self.external_drives.keys())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
drive_names = list(self.external_drives.keys())
drive_names = self.drive_names

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure will do

_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