8000 MAINT: in1d -> isin · tylerjereddy/mdanalysis@bf05436 · GitHub
[go: up one dir, main page]

Skip to content

Commit bf05436

Browse files
committed
MAINT: in1d -> isin
* replace deprecated `in1d` with `isin` calls per numpy/numpy#24445, as part of effort to remain NumPy `2.0` compliant * the testsuite seems happy locally--if there are any cases where `in1d` was potentially receiving arrays with `ndim > 1`, we may want to add `ravel()` to the outputs in these replacements just to be safe, but I've assumed the testsuite has us covered * for now, I've intentionally not adjusted the docstring of our Cython `_in2d`, which refers to `in1d`; seems less critical for now
1 parent 8923c4a commit bf05436

File tree

7 files changed

+39
-39
lines changed

7 files changed

+39
-39
lines changed

package/MDAnalysis/analysis/align.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,7 @@ def get_atoms_byres(g, match_mask=np.logical_not(mismatch_mask)):
14371437
good = ag.residues.resids[match_mask] # resid for each residue
14381438
resids = ag.resids # resid for each atom
14391439
# boolean array for all matching atoms
1440-
ix_good = np.in1d(resids, good)
1440+
ix_good = np.isin(resids, good)
14411441
return ag[ix_good]
14421442

14431443
_ag1 = get_atoms_byres(ag1)

package/MDAnalysis/analysis/hydrogenbonds/hbond_analysis.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -680,16 +680,16 @@ def _filter_atoms(self, donors, acceptors):
680680
# Find donors in G1 and acceptors in G2
681681
mask[
682682
np.logical_and(
683-
np.in1d(donors.indices, group1.indices),
684-
np.in1d(acceptors.indices, group2.indices)
683+
np.isin(donors.indices, group1.indices),
684+
np.isin(acceptors.indices, group2.indices)
685685
)
686686
] = True
687687

688688
# Find acceptors in G1 and donors in G2
689689
mask[
690690
np.logical_and(
691-
np.in1d(acceptors.indices, group1.indices),
692-
np.in1d(donors.indices, group2.indices)
691+
np.isin(acceptors.indices, group1.indices),
692+
np.isin(donors.indices, group2.indices)
693693
)
694694
] = True
695695

package/MDAnalysis/core/groups.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def get_connections(self, typename, outside=True):
422422
indices = self.atoms.ix_array
423423
except AttributeError: # if self is an Atom
424424
indices = self.ix_array
425-
seen = [np.in1d(col, indices) for col in ugroup._bix.T]
425+
seen = [np.isin(col, indices) for col in ugroup._bix.T]
426426
mask = func(seen, axis=0)
427427
return ugroup[mask]
428428

@@ -2154,7 +2154,7 @@ def subtract(self, other):
21542154
.. versionadded:: 0.16
21552155
"""
21562156
o_ix = other.ix_array
2157-
in_other = np.in1d(self.ix, o_ix) # mask of in self.ix AND other
2157+
in_other = np.isin(self.ix, o_ix) # mask of in self.ix AND other
21582158
return self[~in_other] # ie inverse of previous mask
21592159

21602160
@_only_same_level

package/MDAnalysis/core/selection.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def _apply(self, group):
190190
lsel = self.lsel.apply(group)
191191

192192
# Mask which lsel indices appear in rsel
193-
mask = np.in1d(rsel.indices, lsel.indices)
193+
mask = np.isin(rsel.indices, lsel.indices)
194194
# and mask rsel according to that
195195
return rsel[mask]
196196

@@ -267,7 +267,7 @@ class NotSelection(UnarySelection):
267267

268268
def _apply(self, group):
269269
notsel = self.sel.apply(group)
270-
return group[~np.in1d(group.indices, notsel.indices)]
270+
return group[~np.isin(group.indices, notsel.indices)]
271271

272272

273273
class GlobalSelection(UnarySelection):
@@ -292,7 +292,7 @@ class ByResSelection(UnarySelection):
292292
def _apply(self, group):
293293
res = self.sel.apply(group)
294294
unique_res = unique_int_1d(res.resindices)
295-
mask = np.in1d(group.resindices, unique_res)
295+
mask = np.isin(group.resindices, unique_res)
296296

297297
return group[mask]
298298

@@ -312,7 +312,7 @@ def _apply(self, group):
312312
indices = []
313313
sel = self.sel.apply(group)
314314
# All atoms in group that aren't in sel
315-
sys = group[~np.in1d(group.indices, sel.indices)]
315+
sys = group[~np.isin(group.indices, sel.indices)]
316316

317317
if not sys or not sel:
318318
return sys[[]]
@@ -372,7 +372,7 @@ def _apply(self, group):
372372
indices = []
373373
sel = self.sel.apply(group)
374374
# All atoms in group that aren't in sel
375-
sys = group[~np.in1d(group.indices, sel.indices)]
375+
sys = group[~np.isin(group.indices, sel.indices)]
376376

377377
if not sys or not sel:
378378
return sys[[]]
@@ -389,7 +389,7 @@ def _apply(self, group):
389389
sys_ind_outer = np.sort(np.unique(pairs_outer[:,1]))
390390
if pairs_inner.size > 0:
391391
sys_ind_inner = np.sort(np.unique(pairs_inner[:,1]))
392-
indices = sys_ind_outer[~np.in1d(sys_ind_outer, sys_ind_inner)]
392+
indices = sys_ind_outer[~np.isin(sys_ind_outer, sys_ind_inner)]
393393
else:
394394
indices = sys_ind_outer
395395

@@ -577,9 +577,9 @@ def _apply(self, group):
577577

578578
idx = []
579579
# left side
580-
idx.append(bix[:, 0][np.in1d(bix[:, 1], grpidx)])
580+
idx.append(bix[:, 0][np.isin(bix[:, 1], grpidx)])
581581
# right side
582-
idx.append(bix[:, 1][np.in1d(bix[:, 0], grpidx)])
582+
idx.append(bix[:, 1][np.isin(bix[:, 0], grpidx)])
583583

584584
idx = np.union1d(*idx)
585585

@@ -603,7 +603,7 @@ def __init__(self, parser, tokens):
603603
raise ValueError(errmsg) from None
604604

605605
def _apply(self, group):
606-
mask = np.in1d(group.indices, self.grp.indices)
606+
mask = np.isin(group.indices, self.grp.indices)
607607
return group[mask]
608608

609609

@@ -657,7 +657,7 @@ def _apply(self, group):
657657
# atomname indices for members of this group
658658
nmidx = nmattr.nmidx[getattr(group, self.level)]
659659

660-
return group[np.in1d(nmidx, matches)]
660+
return group[np.isin(nmidx, matches)]
661661

662662

663663
class AromaticSelection(Selection):
@@ -743,7 +743,7 @@ def _apply(self, group):
743743
# flatten all matches and remove duplicated indices
744744
indices = np.unique([idx for match in matches for idx in match])
745745
# create boolean mask for atoms based on index
746-
mask = np.in1d(range(group.n_atoms), indices)
746+
mask = np.isin(range(group.n_atoms), indices)
747747
return group[mask]
748748

749749

@@ -1053,7 +1053,7 @@ def _apply(self, group):
10531053
# index of each atom's resname
10541054
nmidx = resname_attr.nmidx[group.resindices]
10551055
# intersect atom's resname index and matches to prot_res
1056-
return group[np.in1d(nmidx, matches)]
1056+
return group[np.isin(nmidx, matches)]
10571057

10581058

10591059
class NucleicSelection(Selection):
@@ -1089,7 +1089,7 @@ def _apply(self, group):
10891089

10901090
matches = [ix for (nm, ix) in resnames.namedict.items()
10911091
if nm in self.nucl_res]
1092-
mask = np.in1d(nmidx, matches)
1092+
mask = np.isin(nmidx, matches)
10931093

10941094
return group[mask]
10951095

@@ -1116,13 +1116,13 @@ def _apply(self, group):
11161116
name_matches = [ix for (nm, ix) in atomnames.namedict.items()
11171117
if nm in self.bb_atoms]
11181118
nmidx = atomnames.nmidx[group.ix]
1119-
group = group[np.in1d(nmidx, name_matches)]
1119+
group = group[np.isin(nmidx, name_matches)]
11201120

11211121
# filter by resnames
11221122
resname_matches = [ix for (nm, ix) in resnames.namedict.items()
11231123
if nm in self.prot_res]
11241124
nmidx = resnames.nmidx[group.resindices]
1125-
group = group[np.in1d(nmidx, resname_matches)]
1125+
group = group[np.isin(nmidx, resname_matches)]
11261126

11271127
return group.unique
11281128

@@ -1149,13 +1149,13 @@ def _apply(self, group):
11491149
name_matches = [ix for (nm, ix) in atomnames.namedict.items()
11501150
if nm in self.bb_atoms]
11511151
nmidx = atomnames.nmidx[group.ix]
1152-
group = group[np.in1d(nmidx, name_matches)]
1152+
group = group[np.isin(nmidx, name_matches)]
11531153

11541154
# filter by resnames
11551155
resname_matches = [ix for (nm, ix) in resnames.namedict.items()
11561156
if nm in self.nucl_res]
11571157
nmidx = resnames.nmidx[group.resindices]
1158-
group = group[np.in1d(nmidx, resname_matches)]
1158+
group = group[np.isin(nmidx, resname_matches)]
11591159

11601160
return group.unique
11611161

@@ -1187,13 +1187,13 @@ def _apply(self, group):
11871187
name_matches = [ix for (nm, ix) in atomnames.namedict.items()
11881188
if nm in self.base_atoms]
11891189
nmidx = atomnames.nmidx[group.ix]
1190-
group = group[np.in1d(nmidx, name_matches)]
1190+
group = group[np.isin(nmidx, name_matches)]
11911191

11921192
# filter by resnames
11931193
resname_matches = [ix for (nm, ix) in resnames.namedict.items()
11941194
if nm in self.nucl_res]
11951195
nmidx = resnames.nmidx[group.resindices]
1196-
group = group[np.in1d(nmidx, resname_matches)]
1196+
group = group[np.isin(nmidx, resname_matches)]
11971197

11981198
return group.unique
11991199

@@ -1217,13 +1217,13 @@ def _apply(self, group):
12171217
name_matches = [ix for (nm, ix) in atomnames.namedict.items()
12181218
if nm in self.sug_atoms]
12191219
nmidx = atomnames.nmidx[group.ix]
1220-
group = group[np.in1d(nmidx, name_matches)]
1220+
group = group[np.isin(nmidx, name_matches)]
12211221

12221222
# filter by resnames
12231223
resname_matches = [ix for (nm, ix) in resnames.namedict.items()
12241224
if nm in self.nucl_res]
12251225
nmidx = resnames.nmidx[group.resindices]
1226-
group = group[np.in1d(nmidx, resname_matches)]
1226+
group = group[np.isin(nmidx, resname_matches)]
12271227

12281228
return group.unique
12291229

@@ -1408,7 +1408,7 @@ def _apply(self, group):
14081408
# indices are same as fragment(s) indices
14091409
allfrags = functools.reduce(lambda x, y: x + y, res.fragments)
14101410

1411-
mask = np.in1d(group.indices, allfrags.indices)
1411+
mask = np.isin(group.indices, allfrags.indices)
14121412
return group[mask]
14131413
# [xyz] must come before self.prop_trans lookups too!
14141414
try:
@@ -1419,7 +1419,7 @@ def _apply(self, group):
14191419
# KeyError at this point is impossible!
14201420
attrname = self.prop_trans[self.prop]
14211421
vals = getattr(res, attrname)
1422-
mask = np.in1d(getattr(group, attrname), vals)
1422+
mask = np.isin(getattr(group, attrname), vals)
14231423

14241424
return group[mask]
14251425
else:

package/MDAnalysis/core/topologyattrs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ def chi1_selection(residue, n_name='N', ca_name='CA', cb_name='CB',
12341234
"""
12351235
names = [n_name, ca_name, cb_name, cg_name]
12361236
atnames = residue.atoms.names
1237-
ags = [residue.atoms[np.in1d(atnames, n.split())] for n in names]
1237+
ags = [residue.atoms[np.isin(atnames, n.split())] for n in names]
12381238
if any(len(ag) != 1 for ag in ags):
12391239
return None
12401240
return sum(ags)
@@ -1267,13 +1267,13 @@ def chi1_selections(residues, n_name='N', ca_name='CA', cb_name='CB',
12671267
"""
12681268
results = np.array([None]*len(residues))
12691269
names = [n_name, ca_name, cb_name, cg_name]
1270-
keep = [all(sum(np.in1d(r.atoms.names, n.split())) == 1
1270+
keep = [all(sum(np.isin(r.atoms.names, n.split())) == 1
12711271
for n in names) for r in residues]
12721272
keepix = np.where(keep)[0]
12731273
residues = residues[keep]
12741274

12751275
atnames = residues.atoms.names
1276-
ags = [residues.atoms[np.in1d(atnames, n.split())] for n in names]
1276+
ags = [residues.atoms[np.isin(atnames, n.split())] for n in names]
12771277
results[keepix] = [sum(atoms) for atoms in zip(*ags)]
12781278
return list(results)
12791279

package/MDAnalysis/core/topologyobjects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ def atomgroup_intersection(self, ag, **kwargs):
694694
atom_idx = ag.indices
695695
# Create a list of boolean arrays,
696696
# each representing a column of bond indices.
697-
seen = [np.in1d(col, atom_idx) for col in self._bix.T]
697+
seen = [np.isin(col, atom_idx) for col in self._bix.T]
698698

699699
# Create final boolean mask by summing across rows
700700
mask = func(seen, axis=0)

testsuite/MDAnalysisTests/core/test_groups.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,7 +1646,7 @@ def test_connection_from_atoms_not_outside(self, tpr, typename,
16461646
cxns = ag.get_connections(typename, outside=False)
16471647
assert len(cxns) == n_atoms
16481648
indices = np.ravel(cxns.to_indices())
1649-
assert np.all(np.in1d(indices, ag.indices))
1649+
assert np.all(np.isin(indices, ag.indices))
16501650

16511651
@pytest.mark.parametrize("typename, n_atoms", [
16521652
("bonds", 13),
@@ -1658,7 +1658,7 @@ def test_connection_from_atoms_outside(self, tpr, typename, n_atoms):
16581658
cxns = ag.get_connections(typename, outside=True)
16591659
assert len(cxns) == n_atoms
16601660
indices = np.ravel(cxns.to_indices())
1661-
assert not np.all(np.in1d(indices, ag.indices))
1661+
assert not np.all(np.isin(indices, ag.indices))
16621662

16631663
def test_invalid_connection_error(self, tpr):
16641664
with pytest.raises(AttributeError, match="does not contain"):
@@ -1708,7 +1708,7 @@ def test_connection_from_residues_not_outside(self, tpr, typename,
17081708
cxns = ag.get_connections(typename, outside=False)
17091709
assert len(cxns) == n_atoms
17101710
indices = np.ravel(cxns.to_indices())
1711-
assert np.all(np.in1d(indices, ag.atoms.indices))
1711+
assert np.all(np.isin(indices, ag.atoms.indices))
17121712

17131713
@pytest.mark.parametrize("typename, n_atoms", [
17141714
("bonds", 158),
@@ -1720,7 +1720,7 @@ def test_connection_from_residues_outside(self, tpr, typename, n_atoms):
17201720
cxns = ag.get_connections(typename, outside=True)
17211721
assert len(cxns) == n_atoms
17221722
indices = np.ravel(cxns.to_indices())
1723-
assert not np.all(np.in1d(indices, ag.atoms.indices))
1723+
assert not np.all(np.isin(indices, ag.atoms.indices))
17241724

17251725
def test_invalid_connection_error(self, tpr):
17261726
with pytest.raises(AttributeError, match="does not contain"):
@@ -1746,7 +1746,7 @@ def test_topologygroup_gets_connections_inside(tpr, typename, n_inside):
17461746
cxns = getattr(ag, typename)
17471747
assert len(cxns) == n_inside
17481748
indices = np.ravel(cxns.to_indices())
1749-
assert np.all(np.in1d(indices, ag.indices))
1749+
assert np.all(np.isin(indices, ag.indices))
17501750

17511751

17521752
@pytest.mark.parametrize("typename, n_outside", [

0 commit comments

Comments
 (0)
0