Skip to content

Commit

Permalink
More NumPy 2.0 compatibility fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
levitsky committed Jul 14, 2024
1 parent ab0e9ad commit 669897d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
8 changes: 4 additions & 4 deletions pyteomics/auxiliary/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def linear_regression_vertical(x, y=None, a=None, b=None):
"""

import numpy as np
x = np.array(x, copy=False)
x = np.asarray(x)
if y is not None:
y = np.array(y, copy=False)
y = np.asarray(y)
else:
if len(x.shape) != 2 or x.shape[-1] != 2:
raise PyteomicsError(
Expand Down Expand Up @@ -76,9 +76,9 @@ def linear_regression_perpendicular(x, y=None):
"""

import numpy as np
x = np.array(x, copy=False)
x = np.asarray(x)
if y is not None:
y = np.array(y, copy=False)
y = np.asarray(y)
data = np.hstack((x.reshape((-1, 1)), y.reshape((-1, 1))))
else:
if len(x.shape) != 2 or x.shape[-1] != 2:
Expand Down
2 changes: 1 addition & 1 deletion pyteomics/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ def coverage(protein, peptides):
'(?={})'.format(re.sub(r'[^A-Z]', '', peptide)), protein)]
for i in indices:
mask[i:i + len(peptide)] = 1
return mask.sum(dtype=float) / mask.size
return float(mask.sum(dtype=float) / mask.size)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion pyteomics/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"""

__version__ = '4.7.2'
__version__ = '4.7.3a1'

from collections import namedtuple
import re
Expand Down
8 changes: 4 additions & 4 deletions tests/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

import numpy as np
from copy import deepcopy
from copy import copy
import sys
from pyteomics.auxiliary import basestring

Expand Down Expand Up @@ -1415,14 +1415,14 @@ def makeCA(arr):
'scans': '3',
'title': 'Spectrum 2'}}]

mgf_spectra_short_no_charges = deepcopy(mgf_spectra_short)
mgf_spectra_short_no_charges = list(map(copy, mgf_spectra_short))
for s in mgf_spectra_short_no_charges:
del s['charge array']
mgf_spectra_long_no_charges = deepcopy(mgf_spectra_long)
mgf_spectra_long_no_charges = list(map(copy, mgf_spectra_long))
for s in mgf_spectra_long_no_charges:
del s['charge array']

mgf_spectra_lists = deepcopy(mgf_spectra_long)
mgf_spectra_lists = list(map(copy, mgf_spectra_long))
for s in mgf_spectra_lists:
for key in ['m/z array', 'intensity array', 'charge array']:
s[key] = list(s[key])
Expand Down
18 changes: 9 additions & 9 deletions tests/test_mgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ def test_read_no_charges(self):
self.assertEqual(data.mgf_spectra_short_no_charges, list(reader))

def test_read_with_ions(self):
for spec_data, spec_read in zip(data.mgf_spectra_annotated_long, list(self.spectra_annotated)):
for spec_data, spec_read in zip(data.mgf_spectra_annotated_long, self.spectra_annotated):
# Check that the spectra have the same dict keys
self.assertEqual(spec_data.keys(), spec_read.keys())
for key in spec_data.keys():
if type(spec_data[key]) == dict:
self.assertDictEqual(spec_data[key], spec_read[key])
else:
for key in spec_data:
if key == 'ion array':
np.testing.assert_array_equal(spec_data[key], spec_read[key])
else:
self.assertEqual(spec_data[key], spec_read[key])

def test_read_write_with_ions(self):
formats = ['{:.6f} {:.6f} {}', '%.6f %.6f %s']
Expand All @@ -91,11 +91,11 @@ def test_read_write_with_ions(self):
for spec_data, spec_read in zip(data.mgf_spectra_annotated_long, spectra):
# Check that the spectra have the same dict keys
self.assertEqual(spec_data.keys(), spec_read.keys())
for key in spec_data.keys():
if type(spec_data[key]) == dict:
self.assertDictEqual(spec_data[key], spec_read[key])
else:
for key in spec_data:
if key == 'ion array':
np.testing.assert_array_equal(spec_data[key], spec_read[key])
else:
self.assertEqual(spec_data[key], spec_read[key])

def test_read_array_conversion(self):
with mgf.read(self.path, convert_arrays=0) as reader:
Expand Down
12 changes: 6 additions & 6 deletions tests/test_ms2.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ def test_read(self):

def test_read_no_charges(self):
with read(self.path, convert_arrays=False, read_charges=False) as reader:
lhs = copy.deepcopy(data.ms2_spectra_lists)
lhs = list(map(copy.copy, data.ms2_spectra_lists))
for spec in lhs:
del spec['charge array']
self.assertEqual(lhs, list(reader))

with read(self.path, convert_arrays=1, read_charges=False) as reader:
lhs = copy.deepcopy(data.ms2_spectra)
lhs = list(map(copy.copy, data.ms2_spectra))
for spec in lhs:
del spec['charge array']
self.assertEqual(lhs, list(reader))

def test_read_no_resolution(self):
with read(self.path, convert_arrays=False, read_resolutions=False) as reader:
lhs = copy.deepcopy(data.ms2_spectra_lists)
lhs = list(map(copy.copy, data.ms2_spectra_lists))
for spec in lhs:
del spec['resolution array']
self.assertEqual(lhs, list(reader))

with read(self.path, convert_arrays=1, read_resolutions=False) as reader:
lhs = copy.deepcopy(data.ms2_spectra)
lhs = list(map(copy.copy, data.ms2_spectra))
for spec in lhs:
del spec['resolution array']
self.assertEqual(lhs, list(reader))
Expand Down Expand Up @@ -77,7 +77,7 @@ def test_indexedms2_picklable(self):
self.assertEqual(reader.block_size, 12345)
self.assertEqual(reader._read_charges, False)
self.assertEqual(reader._read_resolutions, False)
lhs = copy.deepcopy(data.ms2_spectra)
lhs = list(map(copy.copy, data.ms2_spectra))
for spec in lhs:
del spec['resolution array']
del spec['charge array']
Expand All @@ -94,7 +94,7 @@ def test_ms2_picklable(self):
with pickle.loads(spec) as reader:
self.assertEqual(reader._read_charges, False)
self.assertEqual(reader._read_resolutions, False)
lhs = copy.deepcopy(data.ms2_spectra)
lhs = list(map(copy.copy, data.ms2_spectra))
for spec in lhs:
del spec['resolution array']
del spec['charge array']
Expand Down

0 comments on commit 669897d

Please sign in to comment.