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

[MRG] Replace np.in1d() with np.isin() #799

Merged
merged 5 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ Bug
- Fix loading of drives in the GUI: drives are now overwritten instead of updated,
by `Mainak Jas`_ in :gh:`795`.

- Use `np.isin()` in place of `np.in1d()` to address numpy deprecation,
by `Nick Tolley`_ in :gh:`799.

API
~~~
- :func:`~hnn_core.CellResponse.write` and :func:`~hnn_core.Cell_response.read_spikes`
Expand Down
8 changes: 4 additions & 4 deletions hnn_core/cell_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def __getitem__(self, gid_item):
vsoma_slice = list()
isoma_slice = list()
for trial_idx in range(n_trials):
gid_mask = np.in1d(self._spike_gids[trial_idx], gid_item)
gid_mask = np.isin(self._spike_gids[trial_idx], gid_item)
times_trial = np.array(
self._spike_times[trial_idx])[gid_mask].tolist()
gids_trial = np.array(
Expand Down Expand Up @@ -258,7 +258,7 @@ def update_types(self, gid_ranges):
spike_types_trial = np.empty_like(self._spike_times[trial_idx],
dtype='<U36')
for gidtype, gids in gid_ranges.items():
spike_gids_mask = np.in1d(self._spike_gids[trial_idx], gids)
spike_gids_mask = np.isin(self._spike_gids[trial_idx], gids)
spike_types_trial[spike_gids_mask] = gidtype
spike_types += [list(spike_types_trial)]
self._spike_types = spike_types
Expand Down Expand Up @@ -309,11 +309,11 @@ def mean_rates(self, tstart, tstop, gid_ranges, mean_type='all'):

trial_data = zip(self._spike_types, self._spike_gids)
for trial_idx, (spike_types, spike_gids) in enumerate(trial_data):
trial_type_mask = np.in1d(spike_types, cell_type)
trial_type_mask = np.isin(spike_types, cell_type)
gids, gid_counts = np.unique(np.array(
spike_gids)[trial_type_mask], return_counts=True)

gid_spike_rate[trial_idx, np.in1d(cell_type_gids, gids)] = (
gid_spike_rate[trial_idx, np.isin(cell_type_gids, gids)] = (
gid_counts / (tstop - tstart)) * 1000

if mean_type == 'all':
Expand Down
15 changes: 7 additions & 8 deletions hnn_core/gui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
from hnn_core.gui._logging import logger
from hnn_core.gui._viz_manager import _VizManager, _idx2figname
from hnn_core.network import pick_connection
from hnn_core.params import (_extract_drive_specs_from_hnn_params, _read_json,
_read_legacy_params)
from hnn_core.params import _extract_drive_specs_from_hnn_params
from hnn_core.dipole import _read_dipole_txt

import base64
Expand Down Expand Up @@ -1227,8 +1226,8 @@ def add_connectivity_tab(params, connectivity_out,
return net


def add_drive_tab(params, log_out, drives_out, drive_widgets, drive_boxes, tstop,
layout):
def add_drive_tab(params, log_out, drives_out, drive_widgets, drive_boxes,
tstop, layout):
net = jones_2009_model(params)

drive_specs = _extract_drive_specs_from_hnn_params(
Expand Down Expand Up @@ -1277,8 +1276,8 @@ def load_drive_and_connectivity(params, log_out, drives_out,
# Add connectivity
add_connectivity_tab(params, connectivity_out, connectivity_textfields)
# Add drives
add_drive_tab(params, log_out, drives_out, drive_widgets, drive_boxes, tstop,
layout)
add_drive_tab(params, log_out, drives_out, drive_widgets, drive_boxes,
tstop, layout)


def on_upload_data_change(change, data, viz_manager, log_out):
Expand Down Expand Up @@ -1339,8 +1338,8 @@ def on_upload_params_change(change, tstop, dt, log_out, drive_boxes,
add_connectivity_tab(params, connectivity_out, connectivity_textfields)
elif load_type == 'drives':
with log_out:
add_drive_tab(params, log_out, drives_out, drive_widgets, drive_boxes, tstop,
layout)
add_drive_tab(params, log_out, drives_out, drive_widgets,
drive_boxes, tstop, layout)
else:
raise ValueError
# Resets file counter to 0
Expand Down
2 changes: 1 addition & 1 deletion hnn_core/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ def add_connection(self, src_gids, target_gids, loc, receptor,
gid_pairs = dict()
for src_gid, target_src_pair in zip(src_gids, target_gids):
if not allow_autapses:
mask = np.in1d(target_src_pair, src_gid, invert=True)
mask = np.isin(target_src_pair, src_gid, invert=True)
target_src_pair = np.array(target_src_pair)[mask].tolist()
gid_pairs[src_gid] = target_src_pair

Expand Down
8 changes: 4 additions & 4 deletions hnn_core/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ def plot_spikes_hist(cell_response, trial_idx=None, ax=None, spike_types=None,
spike_types_data = np.array([])

unique_types = np.unique(spike_types_data)
spike_types_mask = {s_type: np.in1d(spike_types_data, s_type)
spike_types_mask = {s_type: np.isin(spike_types_data, s_type)
for s_type in unique_types}
cell_types = ['L5_pyramidal', 'L5_basket', 'L2_pyramidal', 'L2_basket']
input_types = np.setdiff1d(unique_types, cell_types)
Expand Down Expand Up @@ -1018,7 +1018,7 @@ def plot_connectivity_matrix(net, conn_idx, ax=None, show_weight=True,

for src_gid, target_src_pair in conn['gid_pairs'].items():
src_idx = np.where(src_range == src_gid)[0][0]
target_indeces = np.where(np.in1d(target_range, target_src_pair))[0]
target_indeces = np.where(np.isin(target_range, target_src_pair))[0]
for target_idx in target_indeces:
src_pos = src_type_pos[src_idx]
target_pos = target_type_pos[target_idx]
Expand Down Expand Up @@ -1068,7 +1068,7 @@ def _update_target_plot(ax, conn, src_gid, src_type_pos, target_type_pos,
# Extract indices to get position in network
# Index in gid range aligns with net.pos_dict
target_src_pair = conn['gid_pairs'][src_gid]
target_indeces = np.where(np.in1d(target_range, target_src_pair))[0]
target_indeces = np.where(np.isin(target_range, target_src_pair))[0]

src_idx = np.where(src_range == src_gid)[0][0]
src_pos = src_type_pos[src_idx]
Expand Down Expand Up @@ -1155,7 +1155,7 @@ def plot_cell_connectivity(net, conn_idx, src_gid=None, axes=None,
src_range = np.array(net.gid_ranges[conn['src_type']])

valid_src_gids = list(net.connectivity[conn_idx]['gid_pairs'].keys())
src_pos_valid = src_type_pos[np.in1d(src_range, valid_src_gids)]
src_pos_valid = src_type_pos[np.isin(src_range, valid_src_gids)]

if src_gid is None:
src_gid = valid_src_gids[0]
Expand Down
Loading