From 270eca771212b1e2755f586183c17657b75022a2 Mon Sep 17 00:00:00 2001 From: raj1701 Date: Wed, 8 Mar 2023 23:08:33 +0530 Subject: [PATCH 01/10] Fixed bud and improved tests for deleting drives --- examples/howto/plot_connectivity.py | 2 +- hnn_core/network.py | 45 ++++++++++++++++++++--------- hnn_core/tests/test_network.py | 20 ++++++++++++- 3 files changed, 52 insertions(+), 15 deletions(-) diff --git a/examples/howto/plot_connectivity.py b/examples/howto/plot_connectivity.py index eec6e747f..e7cb22051 100644 --- a/examples/howto/plot_connectivity.py +++ b/examples/howto/plot_connectivity.py @@ -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() # Pyramidal cell connections location, receptor = 'distal', 'ampa' diff --git a/hnn_core/network.py b/hnn_core/network.py index 777406d1b..20fd87560 100644 --- a/hnn_core/network.py +++ b/hnn_core/network.py @@ -1253,23 +1253,42 @@ 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 - """ - connectivity = list() + def clear_connectivity(self, src_types=None): + """Remove connections with src_type in Network.connectivity.""" + 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()) + _validate_type(drive_names, (list,)) + for drive_name in drive_names: + del self.external_drives[drive_name] + self.clear_connectivity(src_types=drive_names) + + def get_external_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): diff --git a/hnn_core/tests/test_network.py b/hnn_core/tests/test_network.py index 3a96198ac..17034a19f 100644 --- a/hnn_core/tests/test_network.py +++ b/hnn_core/tests/test_network.py @@ -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.get_external_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 with pytest.warns(UserWarning, match='No connections'): simulate_dipole(net, tstop=10) From 2e64179db68e5728e2e27b323c81edf63298fbcd Mon Sep 17 00:00:00 2001 From: raj1701 Date: Thu, 9 Mar 2023 01:25:45 +0530 Subject: [PATCH 02/10] Added parameter description and refactored code --- hnn_core/network.py | 11 +++++++++-- hnn_core/tests/test_network.py | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/hnn_core/network.py b/hnn_core/network.py index 20fd87560..482dd0784 100644 --- a/hnn_core/network.py +++ b/hnn_core/network.py @@ -1254,7 +1254,13 @@ def add_connection(self, src_gids, target_gids, loc, receptor, self.connectivity.append(deepcopy(conn)) def clear_connectivity(self, src_types=None): - """Remove connections with src_type in Network.connectivity.""" + """Remove connections with src_type in Network.connectivity. + + Parameters + ---------- + src_types : list | None + Source types of connections to be removed + """ if src_types is None: src_types = list() # Storing all external drives @@ -1286,7 +1292,8 @@ def clear_drives(self, drive_names='all'): del self.external_drives[drive_name] self.clear_connectivity(src_types=drive_names) - def get_external_drive_names(self): + @property + def drive_names(self): """Returns a list containing names of all external drives.""" return list(self.external_drives.keys()) diff --git a/hnn_core/tests/test_network.py b/hnn_core/tests/test_network.py index 17034a19f..7710694da 100644 --- a/hnn_core/tests/test_network.py +++ b/hnn_core/tests/test_network.py @@ -789,7 +789,7 @@ def test_network_connectivity(): # Testing deletion of a custom number of drives # Deleting one external drive - all_drives = net.get_external_drive_names() + 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: From 894d386d2182250e3d2fdfe88d2f438c912b91ea Mon Sep 17 00:00:00 2001 From: raj1701 Date: Thu, 9 Mar 2023 12:24:15 +0530 Subject: [PATCH 03/10] Minor changes --- examples/howto/plot_connectivity.py | 3 ++- hnn_core/network.py | 9 ++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/howto/plot_connectivity.py b/examples/howto/plot_connectivity.py index e7cb22051..373cfa278 100644 --- a/examples/howto/plot_connectivity.py +++ b/examples/howto/plot_connectivity.py @@ -71,7 +71,8 @@ # directly. def get_network(probability=1.0): net = jones_2009_model(add_drives_from_params=True) - net.connectivity = list() + net.clear_connectivity() + net.clear_drives() # Pyramidal cell connections location, receptor = 'distal', 'ampa' diff --git a/hnn_core/network.py b/hnn_core/network.py index 482dd0784..c703f4511 100644 --- a/hnn_core/network.py +++ b/hnn_core/network.py @@ -1263,12 +1263,11 @@ def clear_connectivity(self, src_types=None): """ 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_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: @@ -1286,7 +1285,7 @@ def clear_drives(self, drive_names='all'): The drive_names to remove """ if drive_names == 'all': - drive_names = list(self.external_drives.keys()) + drive_names = self.drive_names _validate_type(drive_names, (list,)) for drive_name in drive_names: del self.external_drives[drive_name] From 28ebd503966ae558b87a6d21b0020115a07c35f6 Mon Sep 17 00:00:00 2001 From: Rajat Partani Date: Sat, 11 Mar 2023 00:37:53 +0530 Subject: [PATCH 04/10] Added source_types as None description --- hnn_core/network.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hnn_core/network.py b/hnn_core/network.py index c703f4511..54b09bbf1 100644 --- a/hnn_core/network.py +++ b/hnn_core/network.py @@ -1260,6 +1260,8 @@ def clear_connectivity(self, src_types=None): ---------- src_types : list | None Source types of connections to be removed + None - Remove all connections other than those which have an + external drive as a source type """ if src_types is None: src_types = list() From a12afca8df160484e9948230b5c25e7ff05374bf Mon Sep 17 00:00:00 2001 From: raj1701 Date: Mon, 20 Mar 2023 12:44:34 +0000 Subject: [PATCH 05/10] Revamped logic --- hnn_core/network.py | 46 +++++++++++++++---------- hnn_core/tests/test_network.py | 62 ++++++++++++++++++++++++++-------- 2 files changed, 77 insertions(+), 31 deletions(-) diff --git a/hnn_core/network.py b/hnn_core/network.py index 54b09bbf1..55523cd5b 100644 --- a/hnn_core/network.py +++ b/hnn_core/network.py @@ -1253,30 +1253,42 @@ def add_connection(self, src_gids, target_gids, loc, receptor, self.connectivity.append(deepcopy(conn)) - def clear_connectivity(self, src_types=None): + def clear_connectivity(self, src_types="all"): """Remove connections with src_type in Network.connectivity. Parameters ---------- - src_types : list | None - Source types of connections to be removed - None - Remove all connections other than those which have an - external drive as a source type + src_types : list | all | drives | local + Source types of connections to be cleared + all - Clear all connections (Default) + drives - Clear connections originating from external drives + local - Clear connections within cells + """ - if src_types is None: + + if src_types == "all": + src_types = list() + for conn in self.connectivity: + if conn['src_type'] not in src_types: + src_types.append(conn['src_type']) + elif src_types == "drives": + src_types = self.drive_names + elif src_types == "local": src_types = list() + external_drives = self.drive_names 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: - # Removes connections in src_types - if conn['src_type'] not in src_types: - connectivity.append(conn) - self.connectivity = connectivity + if (conn['src_type'] not in src_types and + conn['src_type'] not in external_drives): + src_types.append(conn['src_type']) + _validate_type(src_types, list, 'src_types', 'list, drives, local') + # Finding connection indices to be deleted + conn_idxs = list() + for src_type in src_types: + conn_idxs.extend(pick_connection(self, src_gids=src_type)) + + # Deleting the indices + for conn_idx in sorted(conn_idxs, reverse=True): + del self.connectivity[conn_idx] def clear_drives(self, drive_names='all'): """Remove all drives defined in Network.connectivity. diff --git a/hnn_core/tests/test_network.py b/hnn_core/tests/test_network.py index 7710694da..dbe49e919 100644 --- a/hnn_core/tests/test_network.py +++ b/hnn_core/tests/test_network.py @@ -510,6 +510,25 @@ def test_network_drives_legacy(): n_bursty_sources) +def get_expected_connectivities(net, src_types='all'): + """Return expected connectivities left after clearng + connections. + + Parameters + ---------- + net - The network instance + src_types = list | all + Connection source types to be cleared + all - return 0 as all connections cleared + """ + if src_types == 'all': + return 0 + deleted_connectivities = 0 + for src_type in src_types: + deleted_connectivities += len(pick_connection(net, src_gids=src_type)) + return len(net.connectivity) - deleted_connectivities + + def test_network_connectivity(): """Test manipulation of local network connectivity.""" params = read_params(params_fname) @@ -781,29 +800,44 @@ def test_network_connectivity(): pick_connection(**kwargs) # Test removing connections from net.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 + # Test invalid argument type + with pytest.raises(TypeError, match="src_types must be an instance of"): + net.clear_connectivity(src_types=10) + + # Test Clearing connections of local src_types + + # Deleting all connections with src_type as 'L2_pyramidal' + expected_connectivities = (get_expected_connectivities(net, + src_types=['L2_pyramidal'])) + net.clear_connectivity(src_types=['L2_pyramidal']) + assert len(net.connectivity) == expected_connectivities + + # Deleting all local connections + src_types = list() + external_drives = net.drive_names + for conn in net.connectivity: + if (conn['src_type'] not in src_types and + conn['src_type'] not in external_drives): + src_types.append(conn['src_type']) + expected_connectivities = (get_expected_connectivities(net, + src_types=src_types)) + net.clear_connectivity(src_types='local') + assert len(net.connectivity) == expected_connectivities - # 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) + expected_connectivities = (get_expected_connectivities(net, + src_types=drives_to_be_deleted)) net.clear_drives(drive_names=drives_to_be_deleted) - assert len(net.connectivity) == expected_connectivity_length + assert len(net.connectivity) == expected_connectivities - # Deleting all remaining external drives + # Deleting all external drives net.clear_drives() - assert len(net.connectivity) == 0 # All connections are deleted + # All local and external connections are deleted + assert len(net.connectivity) == 0 with pytest.warns(UserWarning, match='No connections'): simulate_dipole(net, tstop=10) From 1bf4d51cdae1128a2112ccb9210754828e2872d1 Mon Sep 17 00:00:00 2001 From: raj1701 Date: Thu, 23 Mar 2023 09:14:35 +0000 Subject: [PATCH 06/10] Simplified loops --- hnn_core/network.py | 15 ++++++--------- hnn_core/tests/test_network.py | 15 ++++++++++----- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/hnn_core/network.py b/hnn_core/network.py index 55523cd5b..51c711f2f 100644 --- a/hnn_core/network.py +++ b/hnn_core/network.py @@ -1265,21 +1265,18 @@ def clear_connectivity(self, src_types="all"): local - Clear connections within cells """ - if src_types == "all": src_types = list() - for conn in self.connectivity: - if conn['src_type'] not in src_types: - src_types.append(conn['src_type']) + for src_type in self.gid_ranges.keys(): + src_types.append(src_type) elif src_types == "drives": src_types = self.drive_names elif src_types == "local": src_types = list() - external_drives = self.drive_names - for conn in self.connectivity: - if (conn['src_type'] not in src_types and - conn['src_type'] not in external_drives): - src_types.append(conn['src_type']) + all_src_types = self.gid_ranges.keys() + local_src_types = all_src_types - self.drive_names + for src_type in local_src_types: + src_types.append(src_type) _validate_type(src_types, list, 'src_types', 'list, drives, local') # Finding connection indices to be deleted conn_idxs = list() diff --git a/hnn_core/tests/test_network.py b/hnn_core/tests/test_network.py index dbe49e919..b6cf9c7e4 100644 --- a/hnn_core/tests/test_network.py +++ b/hnn_core/tests/test_network.py @@ -511,15 +511,20 @@ def test_network_drives_legacy(): def get_expected_connectivities(net, src_types='all'): - """Return expected connectivities left after clearng - connections. + """Return expected connectivities left after clearng connections. Parameters ---------- - net - The network instance - src_types = list | all + net + The network instance + src_types : list | all Connection source types to be cleared - all - return 0 as all connections cleared + + Returns + ------- + int + Number of connections left after the deletion + operation """ if src_types == 'all': return 0 From 9b53e135f55f957d3ef669bc0f90e86261fd513c Mon Sep 17 00:00:00 2001 From: raj1701 Date: Fri, 24 Mar 2023 08:21:45 +0000 Subject: [PATCH 07/10] simplified code --- hnn_core/network.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/hnn_core/network.py b/hnn_core/network.py index 51c711f2f..cd3ff3c76 100644 --- a/hnn_core/network.py +++ b/hnn_core/network.py @@ -1258,7 +1258,7 @@ def clear_connectivity(self, src_types="all"): Parameters ---------- - src_types : list | all | drives | local + src_types : list | 'all' | 'drives' | 'local' Source types of connections to be cleared all - Clear all connections (Default) drives - Clear connections originating from external drives @@ -1266,17 +1266,12 @@ def clear_connectivity(self, src_types="all"): """ if src_types == "all": - src_types = list() - for src_type in self.gid_ranges.keys(): - src_types.append(src_type) + src_types = list(self.gid_ranges.keys()) elif src_types == "drives": src_types = self.drive_names elif src_types == "local": - src_types = list() - all_src_types = self.gid_ranges.keys() - local_src_types = all_src_types - self.drive_names - for src_type in local_src_types: - src_types.append(src_type) + src_types = list((src_type for src_type in self.gid_ranges.keys() + if src_type not in self.drive_names)) _validate_type(src_types, list, 'src_types', 'list, drives, local') # Finding connection indices to be deleted conn_idxs = list() From 446d6f97766ff909f244fbbf3a54da2f2f145ff0 Mon Sep 17 00:00:00 2001 From: raj1701 Date: Fri, 24 Mar 2023 08:23:55 +0000 Subject: [PATCH 08/10] Refactoring --- hnn_core/network.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hnn_core/network.py b/hnn_core/network.py index cd3ff3c76..d117dc6cd 100644 --- a/hnn_core/network.py +++ b/hnn_core/network.py @@ -1260,9 +1260,9 @@ def clear_connectivity(self, src_types="all"): ---------- src_types : list | 'all' | 'drives' | 'local' Source types of connections to be cleared - all - Clear all connections (Default) - drives - Clear connections originating from external drives - local - Clear connections within cells + 'all' - Clear all connections (Default) + 'drives' - Clear connections originating from external drives + 'local' - Clear connections within cells """ if src_types == "all": From c7c4a6ceacdfc393d9536821125d826469428352 Mon Sep 17 00:00:00 2001 From: raj1701 Date: Sun, 7 May 2023 19:40:17 +0000 Subject: [PATCH 09/10] Made function private and improved documentation --- hnn_core/network.py | 33 ++++++++++++++++++++++++++++----- hnn_core/tests/test_network.py | 34 +++++----------------------------- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/hnn_core/network.py b/hnn_core/network.py index d117dc6cd..dcf8904c5 100644 --- a/hnn_core/network.py +++ b/hnn_core/network.py @@ -1253,23 +1253,46 @@ def add_connection(self, src_gids, target_gids, loc, receptor, self.connectivity.append(deepcopy(conn)) + def _get_expected_connectivities(self, src_types='all'): + """Return expected connectivities left after clearng connections. + + Parameters + ---------- + src_types : list | all + Connection source types to be cleared + + Returns + ------- + int + Number of connections left after the deletion + operation + """ + if src_types == 'all': + return 0 + deleted_connectivities = 0 + for src_type in src_types: + deleted_connectivities += len(pick_connection(self, + src_gids=src_type)) + return len(self.connectivity) - deleted_connectivities + def clear_connectivity(self, src_types="all"): """Remove connections with src_type in Network.connectivity. Parameters ---------- - src_types : list | 'all' | 'drives' | 'local' + src_types : list | 'all' | 'external' | 'internal' Source types of connections to be cleared 'all' - Clear all connections (Default) - 'drives' - Clear connections originating from external drives - 'local' - Clear connections within cells + 'external' - Clear connections from the external drives to the + local network + 'internal' - Clear connections between cells of the local network """ if src_types == "all": src_types = list(self.gid_ranges.keys()) - elif src_types == "drives": + elif src_types == "external": src_types = self.drive_names - elif src_types == "local": + elif src_types == "internal": src_types = list((src_type for src_type in self.gid_ranges.keys() if src_type not in self.drive_names)) _validate_type(src_types, list, 'src_types', 'list, drives, local') diff --git a/hnn_core/tests/test_network.py b/hnn_core/tests/test_network.py index b6cf9c7e4..f680a0a95 100644 --- a/hnn_core/tests/test_network.py +++ b/hnn_core/tests/test_network.py @@ -510,30 +510,6 @@ def test_network_drives_legacy(): n_bursty_sources) -def get_expected_connectivities(net, src_types='all'): - """Return expected connectivities left after clearng connections. - - Parameters - ---------- - net - The network instance - src_types : list | all - Connection source types to be cleared - - Returns - ------- - int - Number of connections left after the deletion - operation - """ - if src_types == 'all': - return 0 - deleted_connectivities = 0 - for src_type in src_types: - deleted_connectivities += len(pick_connection(net, src_gids=src_type)) - return len(net.connectivity) - deleted_connectivities - - def test_network_connectivity(): """Test manipulation of local network connectivity.""" params = read_params(params_fname) @@ -812,7 +788,7 @@ def test_network_connectivity(): # Test Clearing connections of local src_types # Deleting all connections with src_type as 'L2_pyramidal' - expected_connectivities = (get_expected_connectivities(net, + expected_connectivities = (net._get_expected_connectivities( src_types=['L2_pyramidal'])) net.clear_connectivity(src_types=['L2_pyramidal']) assert len(net.connectivity) == expected_connectivities @@ -824,9 +800,9 @@ def test_network_connectivity(): if (conn['src_type'] not in src_types and conn['src_type'] not in external_drives): src_types.append(conn['src_type']) - expected_connectivities = (get_expected_connectivities(net, + expected_connectivities = (net._get_expected_connectivities( src_types=src_types)) - net.clear_connectivity(src_types='local') + net.clear_connectivity(src_types='internal') assert len(net.connectivity) == expected_connectivities # Testing deletion of a custom number of drives @@ -834,14 +810,14 @@ def test_network_connectivity(): # Deleting one external drive all_drives = net.drive_names drives_to_be_deleted = all_drives[0:1] - expected_connectivities = (get_expected_connectivities(net, + expected_connectivities = (net._get_expected_connectivities( src_types=drives_to_be_deleted)) net.clear_drives(drive_names=drives_to_be_deleted) assert len(net.connectivity) == expected_connectivities # Deleting all external drives net.clear_drives() - # All local and external connections are deleted + # All internal and external connections are deleted assert len(net.connectivity) == 0 with pytest.warns(UserWarning, match='No connections'): From e7d289f5e6bb4d4c2aaa85b4bf547a066a83b5d1 Mon Sep 17 00:00:00 2001 From: raj1701 Date: Mon, 15 May 2023 20:22:07 +0000 Subject: [PATCH 10/10] Separated clear_drives and clear_connectivity functionality --- hnn_core/network.py | 65 +++++++++++++++------------------- hnn_core/tests/test_network.py | 48 +++++++++++++++++++++---- 2 files changed, 70 insertions(+), 43 deletions(-) diff --git a/hnn_core/network.py b/hnn_core/network.py index dcf8904c5..33cca7ff1 100644 --- a/hnn_core/network.py +++ b/hnn_core/network.py @@ -1253,60 +1253,48 @@ def add_connection(self, src_gids, target_gids, loc, receptor, self.connectivity.append(deepcopy(conn)) - def _get_expected_connectivities(self, src_types='all'): - """Return expected connectivities left after clearng connections. + def _clear_connectivity(self, src_types): + """Remove connections with src_type in Network.connectivity. Parameters ---------- - src_types : list | all - Connection source types to be cleared + src_types : list + Source types of connections to be cleared - Returns - ------- - int - Number of connections left after the deletion - operation """ - if src_types == 'all': - return 0 - deleted_connectivities = 0 + _validate_type(src_types, list, 'src_types', 'list, drives, local') + # Finding connection indices to be deleted + conn_idxs = list() for src_type in src_types: - deleted_connectivities += len(pick_connection(self, - src_gids=src_type)) - return len(self.connectivity) - deleted_connectivities + conn_idxs.extend(pick_connection(self, src_gids=src_type)) - def clear_connectivity(self, src_types="all"): - """Remove connections with src_type in Network.connectivity. + # Deleting the indices + for conn_idx in sorted(conn_idxs, reverse=True): + del self.connectivity[conn_idx] + + def clear_connectivity(self, src_types='all'): + """Clear connections between cells of the local network Parameters ---------- - src_types : list | 'all' | 'external' | 'internal' + src_types : list | 'all' Source types of connections to be cleared - 'all' - Clear all connections (Default) - 'external' - Clear connections from the external drives to the - local network - 'internal' - Clear connections between cells of the local network + 'all' - Clear all connections between cells of the local network """ if src_types == "all": - src_types = list(self.gid_ranges.keys()) - elif src_types == "external": - src_types = self.drive_names - elif src_types == "internal": src_types = list((src_type for src_type in self.gid_ranges.keys() if src_type not in self.drive_names)) - _validate_type(src_types, list, 'src_types', 'list, drives, local') - # Finding connection indices to be deleted - conn_idxs = list() + _validate_type(src_types, list, 'src_types', 'list') + drive_names = self.drive_names for src_type in src_types: - conn_idxs.extend(pick_connection(self, src_gids=src_type)) - - # Deleting the indices - for conn_idx in sorted(conn_idxs, reverse=True): - del self.connectivity[conn_idx] + if src_type in drive_names: + raise ValueError('src_types contains %s which is an external ' + 'drive.' % (src_type,)) + self._clear_connectivity(src_types) def clear_drives(self, drive_names='all'): - """Remove all drives defined in Network.connectivity. + """Remove drives defined in Network.connectivity. Parameters ---------- @@ -1315,7 +1303,12 @@ def clear_drives(self, drive_names='all'): """ if drive_names == 'all': drive_names = self.drive_names - _validate_type(drive_names, (list,)) + _validate_type(drive_names, list, 'drive_names', 'list') + all_drive_names = self.drive_names + for drive_name in drive_names: + if drive_name not in all_drive_names: + raise ValueError('drive_names contains %s which is not an ' + 'external drive.' % (drive_name,)) for drive_name in drive_names: del self.external_drives[drive_name] self.clear_connectivity(src_types=drive_names) diff --git a/hnn_core/tests/test_network.py b/hnn_core/tests/test_network.py index f680a0a95..bcf27092d 100644 --- a/hnn_core/tests/test_network.py +++ b/hnn_core/tests/test_network.py @@ -510,6 +510,28 @@ def test_network_drives_legacy(): n_bursty_sources) +def get_expected_connectivities(net, src_types): + """Return expected connectivities left after clearing connections. + + Parameters + ---------- + net : The network instance + src_types : list + Connection source types to be cleared + + Returns + ------- + int + Number of connections left after the deletion + operation + """ + deleted_connectivities = 0 + for src_type in src_types: + deleted_connectivities += len(pick_connection(net, + src_gids=src_type)) + return len(net.connectivity) - deleted_connectivities + + def test_network_connectivity(): """Test manipulation of local network connectivity.""" params = read_params(params_fname) @@ -787,9 +809,15 @@ def test_network_connectivity(): # Test Clearing connections of local src_types + # Using clear_connections to delete a drive + with pytest.raises(ValueError, + match="src_types contains evdist1 which is an " + "external drive."): + net.clear_connectivity(src_types=['evdist1']) + # Deleting all connections with src_type as 'L2_pyramidal' - expected_connectivities = (net._get_expected_connectivities( - src_types=['L2_pyramidal'])) + expected_connectivities = (get_expected_connectivities( + net, src_types=['L2_pyramidal'])) net.clear_connectivity(src_types=['L2_pyramidal']) assert len(net.connectivity) == expected_connectivities @@ -800,18 +828,24 @@ def test_network_connectivity(): if (conn['src_type'] not in src_types and conn['src_type'] not in external_drives): src_types.append(conn['src_type']) - expected_connectivities = (net._get_expected_connectivities( - src_types=src_types)) - net.clear_connectivity(src_types='internal') + expected_connectivities = (get_expected_connectivities( + net, src_types=src_types)) + net.clear_connectivity(src_types='all') assert len(net.connectivity) == expected_connectivities # Testing deletion of a custom number of drives + # Using clear_drives to delete a local connection + with pytest.raises(ValueError, + match="drive_names contains L2_pyramidal which " + "is not an external drive."): + net.clear_drives(drive_names=['L2_pyramidal']) + # Deleting one external drive all_drives = net.drive_names drives_to_be_deleted = all_drives[0:1] - expected_connectivities = (net._get_expected_connectivities( - src_types=drives_to_be_deleted)) + expected_connectivities = (get_expected_connectivities( + net, src_types=drives_to_be_deleted)) net.clear_drives(drive_names=drives_to_be_deleted) assert len(net.connectivity) == expected_connectivities