Standardizing on import numpy as np

This commit is contained in:
John Stilley
2024-08-30 07:13:36 -07:00
committed by Peter Cock
parent 7ba26eb1a3
commit 2907bcbd82
22 changed files with 124 additions and 124 deletions

View File

@ -451,8 +451,8 @@ PyDoc_STRVAR(
"b'ACAGTT'\n"
">>> parser.shape\n"
"(3, 7)\n"
">>> import numpy\n"
">>> coordinates = numpy.zeros((3, 7), int)\n"
">>> import numpy as np\n"
">>> coordinates = np.zeros((3, 7), int)\n"
">>> parser.fill(coordinates)\n"
">>> coordinates\n"
"array([[ 0, 2, 3, 4, 6, 8, 10],\n"

View File

@ -85,7 +85,7 @@ write in JSON format.
try:
# Both phen_micro.py and pm_fitting require NumPy, so require NumPy here
import numpy
import numpy as np
except ImportError:
from Bio import MissingPythonDependencyError

View File

@ -4704,11 +4704,11 @@ matches to any unknown nucleotides separately.
.. code:: pycon
>>> import numpy
>>> import numpy as np
>>> from Bio import Align
>>> query = "GGTGGGGG"
>>> target = "AAAAAAAggggGGNGAAAAA"
>>> coordinates = numpy.array([[0, 7, 15, 20], [0, 0, 8, 8]])
>>> coordinates = np.array([[0, 7, 15, 20], [0, 0, 8, 8]])
>>> alignment = Align.Alignment([target, query], coordinates)
>>> print(alignment)
target 0 AAAAAAAggggGGNGAAAAA 20

View File

@ -13,7 +13,7 @@ import unittest
import warnings
try:
import numpy
import numpy as np
from numpy import linalg # missing in PyPy's micronumpy
except ImportError:
from Bio import MissingExternalDependencyError

View File

@ -18,7 +18,7 @@ import unittest
import warnings
try:
import numpy
import numpy as np
except ImportError:
from Bio import MissingPythonDependencyError

View File

@ -11,7 +11,7 @@
import unittest
try:
import numpy
import numpy as np
except ImportError:
from Bio import MissingPythonDependencyError

View File

@ -13,7 +13,7 @@
import unittest
try:
import numpy
import numpy as np
except ImportError:
from Bio import MissingPythonDependencyError

View File

@ -15,7 +15,7 @@ import unittest
import warnings
try:
import numpy
import numpy as np
from numpy import dot # Missing on old PyPy's micronumpy
del dot
@ -150,9 +150,9 @@ class ParseReal(unittest.TestCase):
self.assertEqual(
[a.get_occupancy() for a in atoms[:5]], [1.0, 1.0, 1.0, 1.0, 1.0]
)
self.assertIsInstance(atoms[0].get_coord(), numpy.ndarray)
coord = numpy.array([19.594, 32.367, 28.012], dtype=numpy.float32)
numpy.testing.assert_array_equal(atoms[0].get_coord(), coord)
self.assertIsInstance(atoms[0].get_coord(), np.ndarray)
coord = np.array([19.594, 32.367, 28.012], dtype=np.float32)
np.testing.assert_array_equal(atoms[0].get_coord(), coord)
self.assertEqual(atoms[0].get_bfactor(), 18.03)
for atom in atoms:
@ -181,20 +181,20 @@ class ParseReal(unittest.TestCase):
self.assertEqual(
[a.get_occupancy() for a in atoms[:5]], [1.0, 1.0, 1.0, 1.0, 1.0]
)
self.assertIsInstance(atoms[0].get_coord(), numpy.ndarray)
coord = numpy.array([50.346, 19.287, 17.288], dtype=numpy.float32)
numpy.testing.assert_array_equal(atoms[0].get_coord(), coord)
self.assertIsInstance(atoms[0].get_coord(), np.ndarray)
coord = np.array([50.346, 19.287, 17.288], dtype=np.float32)
np.testing.assert_array_equal(atoms[0].get_coord(), coord)
self.assertEqual(atoms[0].get_bfactor(), 32.02)
ansiou = numpy.array(
[0.4738, -0.0309, -0.0231, 0.4524, 0.0036, 0.2904], dtype=numpy.float32
ansiou = np.array(
[0.4738, -0.0309, -0.0231, 0.4524, 0.0036, 0.2904], dtype=np.float32
)
numpy.testing.assert_array_equal(atoms[0].get_anisou(), ansiou)
ansiou = numpy.array(
[1.1242, 0.2942, -0.0995, 1.1240, -0.1088, 0.8221], dtype=numpy.float32
np.testing.assert_array_equal(atoms[0].get_anisou(), ansiou)
ansiou = np.array(
[1.1242, 0.2942, -0.0995, 1.1240, -0.1088, 0.8221], dtype=np.float32
)
atom_937 = list(f_structure[0]["A"])[114]["CB"]
numpy.testing.assert_array_equal(atom_937.get_anisou(), ansiou)
np.testing.assert_array_equal(atom_937.get_anisou(), ansiou)
def testModels(self):
"""Test file with multiple models."""

View File

@ -20,7 +20,7 @@ import subprocess
import unittest
try:
import numpy
import numpy as np
except ImportError:
from Bio import MissingPythonDependencyError

View File

@ -18,7 +18,7 @@ import warnings
from io import StringIO
try:
import numpy
import numpy as np
except ImportError:
from Bio import MissingPythonDependencyError

View File

@ -18,7 +18,7 @@ import warnings
from copy import deepcopy
try:
import numpy
import numpy as np
from numpy import dot # Missing on old PyPy's micronumpy
del dot
@ -467,7 +467,7 @@ class TransformTests(unittest.TestCase):
"""Sum of positions of atoms in an entity along with the number of atoms."""
if hasattr(o, "get_coord"):
return o.get_coord(), 1
total_pos = numpy.array((0.0, 0.0, 0.0))
total_pos = np.array((0.0, 0.0, 0.0))
total_count = 0
for p in o.get_list():
pos, count = self.get_total_pos(p)
@ -484,11 +484,11 @@ class TransformTests(unittest.TestCase):
"""Transform entities (rotation and translation)."""
for o in (self.s, self.m, self.c, self.r, self.a):
rotation = rotmat(Vector(1, 3, 5), Vector(1, 0, 0))
translation = numpy.array((2.4, 0, 1), "f")
translation = np.array((2.4, 0, 1), "f")
oldpos = self.get_pos(o)
o.transform(rotation, translation)
newpos = self.get_pos(o)
newpos_check = numpy.dot(oldpos, rotation) + translation
newpos_check = np.dot(oldpos, rotation) + translation
for i in range(3):
self.assertAlmostEqual(newpos[i], newpos_check[i])
@ -534,13 +534,13 @@ class CenterOfMassTests(unittest.TestCase):
"""Calculate Structure center of mass."""
com = self.structure.center_of_mass()
self.assertTrue(numpy.allclose(com, [19.870, 25.455, 28.753], atol=1e-3))
self.assertTrue(np.allclose(com, [19.870, 25.455, 28.753], atol=1e-3))
def test_structure_cog(self):
"""Calculate Structure center of geometry."""
cog = self.structure.center_of_mass(geometric=True)
self.assertTrue(numpy.allclose(cog, [19.882, 25.842, 28.333], atol=1e-3))
self.assertTrue(np.allclose(cog, [19.882, 25.842, 28.333], atol=1e-3))
def test_chain_cog(self):
"""Calculate center of geometry of individual chains."""
@ -552,7 +552,7 @@ class CenterOfMassTests(unittest.TestCase):
for chain in self.structure[0].get_chains(): # one model only
cog = chain.center_of_mass(geometric=True)
self.assertTrue(numpy.allclose(cog, expected[chain.id], atol=1e-3))
self.assertTrue(np.allclose(cog, expected[chain.id], atol=1e-3))
def test_com_empty_structure(self):
"""Center of mass of empty structure raises ValueError."""

View File

@ -12,7 +12,7 @@
import unittest
try:
import numpy
import numpy as np
except ImportError:
from Bio import MissingPythonDependencyError
@ -36,14 +36,14 @@ class SuperimposerTests(unittest.TestCase):
fixed = Selection.unfold_entities(s1, "A")
s2 = p.get_structure("MOVING", pdb1)
moving = Selection.unfold_entities(s2, "A")
rot = numpy.identity(3).astype("f")
tran = numpy.array((1.0, 2.0, 3.0), "f")
rot = np.identity(3).astype("f")
tran = np.array((1.0, 2.0, 3.0), "f")
for atom in moving:
atom.transform(rot, tran)
sup = Superimposer()
sup.set_atoms(fixed, moving)
self.assertTrue(numpy.allclose(sup.rotran[0], numpy.identity(3)))
self.assertTrue(numpy.allclose(sup.rotran[1], numpy.array([-1.0, -2.0, -3.0])))
self.assertTrue(np.allclose(sup.rotran[0], np.identity(3)))
self.assertTrue(np.allclose(sup.rotran[1], np.array([-1.0, -2.0, -3.0])))
self.assertAlmostEqual(sup.rms, 0.0, places=3)
# Turn black code style off
# fmt: off

View File

@ -9,7 +9,7 @@
import unittest
try:
import numpy
import numpy as np
from numpy.random import random
except ImportError:
from Bio import MissingPythonDependencyError
@ -49,39 +49,39 @@ class VectorTests(unittest.TestCase):
self.assertEqual(calc_angle(v1, v2, v3), 1.5707963267948966)
self.assertEqual(calc_dihedral(v1, v2, v3, v4), 1.5707963267948966)
self.assertTrue(
numpy.array_equal((v1 - v2).get_array(), numpy.array([0.0, 0.0, 1.0]))
np.array_equal((v1 - v2).get_array(), np.array([0.0, 0.0, 1.0]))
)
self.assertTrue(
numpy.array_equal((v1 - 1).get_array(), numpy.array([-1.0, -1.0, 0.0]))
np.array_equal((v1 - 1).get_array(), np.array([-1.0, -1.0, 0.0]))
)
self.assertTrue(
numpy.array_equal(
(v1 - (1, 2, 3)).get_array(), numpy.array([-1.0, -2.0, -2.0])
np.array_equal(
(v1 - (1, 2, 3)).get_array(), np.array([-1.0, -2.0, -2.0])
)
)
self.assertTrue(
numpy.array_equal((v1 + v2).get_array(), numpy.array([0.0, 0.0, 1.0]))
np.array_equal((v1 + v2).get_array(), np.array([0.0, 0.0, 1.0]))
)
self.assertTrue(
numpy.array_equal((v1 + 3).get_array(), numpy.array([3.0, 3.0, 4.0]))
np.array_equal((v1 + 3).get_array(), np.array([3.0, 3.0, 4.0]))
)
self.assertTrue(
numpy.array_equal(
(v1 + (1, 2, 3)).get_array(), numpy.array([1.0, 2.0, 4.0])
np.array_equal(
(v1 + (1, 2, 3)).get_array(), np.array([1.0, 2.0, 4.0])
)
)
self.assertTrue(numpy.array_equal(v1.get_array() / 2, numpy.array([0, 0, 0.5])))
self.assertTrue(numpy.array_equal(v1.get_array() / 2, numpy.array([0, 0, 0.5])))
self.assertTrue(np.array_equal(v1.get_array() / 2, np.array([0, 0, 0.5])))
self.assertTrue(np.array_equal(v1.get_array() / 2, np.array([0, 0, 0.5])))
self.assertEqual(v1 * v2, 0.0)
self.assertTrue(
numpy.array_equal((v1**v2).get_array(), numpy.array([0.0, -0.0, 0.0]))
np.array_equal((v1**v2).get_array(), np.array([0.0, -0.0, 0.0]))
)
self.assertTrue(
numpy.array_equal((v1**2).get_array(), numpy.array([0.0, 0.0, 2.0]))
np.array_equal((v1**2).get_array(), np.array([0.0, 0.0, 2.0]))
)
self.assertTrue(
numpy.array_equal(
(v1 ** (1, 2, 3)).get_array(), numpy.array([0.0, 0.0, 3.0])
np.array_equal(
(v1 ** (1, 2, 3)).get_array(), np.array([0.0, 0.0, 3.0])
)
)
self.assertEqual(v1.norm(), 1.0)
@ -93,24 +93,24 @@ class VectorTests(unittest.TestCase):
"""Test Vector normalization."""
v1 = Vector([2, 0, 0])
self.assertTrue(
numpy.array_equal(v1.normalized().get_array(), numpy.array([1, 0, 0]))
np.array_equal(v1.normalized().get_array(), np.array([1, 0, 0]))
)
# State of v1 should not be affected by `normalized`
self.assertTrue(numpy.array_equal(v1.get_array(), numpy.array([2, 0, 0])))
self.assertTrue(np.array_equal(v1.get_array(), np.array([2, 0, 0])))
v1.normalize()
# State of v1 should be affected by `normalize`
self.assertTrue(numpy.array_equal(v1.get_array(), numpy.array([1, 0, 0])))
self.assertTrue(np.array_equal(v1.get_array(), np.array([1, 0, 0])))
def test_refmat(self):
"""Test refmat can mirror one matrix to another."""
v1 = Vector(0, 0, 1)
v2 = Vector(0, 1, 0)
ref = refmat(v1, v2)
self.assertTrue(numpy.allclose(ref[0], [1.0, 0.0, 0.0]))
self.assertTrue(numpy.allclose(ref[1], [0.0, 0.0, 1.0]))
self.assertTrue(numpy.allclose(ref[2], [0.0, 1.0, 0.0]))
self.assertTrue(np.allclose(ref[0], [1.0, 0.0, 0.0]))
self.assertTrue(np.allclose(ref[1], [0.0, 0.0, 1.0]))
self.assertTrue(np.allclose(ref[2], [0.0, 1.0, 0.0]))
self.assertTrue(
numpy.allclose(v1.left_multiply(ref).get_array(), [0.0, 1.0, 0.0])
np.allclose(v1.left_multiply(ref).get_array(), [0.0, 1.0, 0.0])
)
def test_rotmat_90(self):
@ -118,15 +118,15 @@ class VectorTests(unittest.TestCase):
v1 = Vector(0, 0, 1)
v2 = Vector(0, 1, 0)
rot = rotmat(v1, v2)
self.assertTrue(numpy.allclose(rot[0], numpy.array([1.0, 0.0, 0.0])))
self.assertTrue(numpy.allclose(rot[1], numpy.array([0.0, 0.0, 1.0])))
self.assertTrue(numpy.allclose(rot[2], numpy.array([0.0, -1.0, 0.0])))
self.assertTrue(np.allclose(rot[0], np.array([1.0, 0.0, 0.0])))
self.assertTrue(np.allclose(rot[1], np.array([0.0, 0.0, 1.0])))
self.assertTrue(np.allclose(rot[2], np.array([0.0, -1.0, 0.0])))
self.assertTrue(
numpy.allclose(v1.left_multiply(rot).get_array(), [0.0, 1.0, 0.0])
np.allclose(v1.left_multiply(rot).get_array(), [0.0, 1.0, 0.0])
)
self.assertTrue(
numpy.allclose(
v1.right_multiply(numpy.transpose(rot)).get_array(),
np.allclose(
v1.right_multiply(np.transpose(rot)).get_array(),
[0.0, 1.0, 0.0],
)
)
@ -137,7 +137,7 @@ class VectorTests(unittest.TestCase):
v2 = Vector([-1.0, -0.8, 0])
rot = rotmat(v1, v2)
v3 = v1.left_multiply(rot)
self.assertTrue(numpy.allclose(v2.get_array(), v3.get_array()))
self.assertTrue(np.allclose(v2.get_array(), v3.get_array()))
def test_rotmat_0(self):
"""Test rotmat when the rotation is 0 deg (singularity)."""
@ -145,7 +145,7 @@ class VectorTests(unittest.TestCase):
v2 = Vector([1.0, 0.8, 0])
rot = rotmat(v1, v2)
v3 = v1.left_multiply(rot)
self.assertTrue(numpy.allclose(v1.get_array(), v3.get_array()))
self.assertTrue(np.allclose(v1.get_array(), v3.get_array()))
def test_m2rotaxis_90(self):
"""Test 90 deg rotation."""
@ -153,8 +153,8 @@ class VectorTests(unittest.TestCase):
v2 = Vector(0, 1, 0)
rot = rotmat(v1, v2)
angle, axis = m2rotaxis(rot)
self.assertTrue(numpy.allclose(axis.get_array(), [-1.0, 0.0, 0.0]))
self.assertLess(abs(angle - numpy.pi / 2), 1e-5)
self.assertTrue(np.allclose(axis.get_array(), [-1.0, 0.0, 0.0]))
self.assertLess(abs(angle - np.pi / 2), 1e-5)
def test_m2rotaxis_180(self):
"""Test 180 deg rotation."""
@ -163,7 +163,7 @@ class VectorTests(unittest.TestCase):
rot = rotmat(v1, v2)
angle, axis = m2rotaxis(rot)
self.assertLess(abs(axis * v1), 1e-5) # axis orthogonal to v1
self.assertLess(abs(angle - numpy.pi), 1e-5)
self.assertLess(abs(angle - np.pi), 1e-5)
def test_m2rotaxis_0(self):
"""Test 0 deg rotation. Axis must be [1, 0, 0] as per Vector docs."""
@ -171,29 +171,29 @@ class VectorTests(unittest.TestCase):
v2 = Vector([1.0, 0.8, 0])
rot = rotmat(v1, v2)
angle, axis = m2rotaxis(rot)
self.assertTrue(numpy.allclose(axis.get_array(), [1, 0, 0]))
self.assertTrue(np.allclose(axis.get_array(), [1, 0, 0]))
self.assertLess(abs(angle), 1e-5)
def test_Vector_angles(self):
"""Test Vector angles."""
angle = random() * numpy.pi
angle = random() * np.pi
axis = Vector(random(3) - random(3))
axis.normalize()
m = rotaxis(angle, axis)
cangle, caxis = m2rotaxis(m)
self.assertAlmostEqual(angle, cangle, places=3)
self.assertTrue(
numpy.allclose(list(map(int, (axis - caxis).get_array())), [0, 0, 0]),
np.allclose(list(map(int, (axis - caxis).get_array())), [0, 0, 0]),
f"Want {axis.get_array()!r} and {caxis.get_array()!r}"
" to be almost equal",
)
def test_get_spherical_coordinates(self):
"""Test spherical coordinates."""
srt22 = numpy.sqrt(2.0) / 2
r45 = numpy.radians(45)
# r90 = numpy.radians(90)
r135 = numpy.radians(135)
srt22 = np.sqrt(2.0) / 2
r45 = np.radians(45)
# r90 = np.radians(90)
r135 = np.radians(135)
for i in range(2):
for j in range(2):
for k in range(2):
@ -204,7 +204,7 @@ class VectorTests(unittest.TestCase):
(1 if k else -1) * srt22,
]
)
# print(sc[0], numpy.degrees(sc[1]), numpy.degrees(sc[2]))
# print(sc[0], np.degrees(sc[1]), np.degrees(sc[2]))
self.assertEqual(1.0, sc[0]) # r
self.assertEqual(
(1 if j else -1) * (r45 if i else r135), sc[1]
@ -215,15 +215,15 @@ class VectorTests(unittest.TestCase):
"""Confirm can generate coordinate space transform for 3 points."""
# start with 3 points already aligned to axes
point_set = (
numpy.array([[2.0], [0.0], [2.0], [1.0]]),
numpy.array([[0.0], [0.0], [0.0], [1.0]]),
numpy.array([[0.0], [0.0], [2.0], [1.0]]),
np.array([[2.0], [0.0], [2.0], [1.0]]),
np.array([[0.0], [0.0], [0.0], [1.0]]),
np.array([[0.0], [0.0], [2.0], [1.0]]),
)
# confirm get id matrix to transform to/from coord space
homog_id = numpy.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
homog_id = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
mtxs = coord_space(point_set[0], point_set[1], point_set[2], True)
for i in range(2):
self.assertTrue(numpy.array_equal(mtxs[i], homog_id))
self.assertTrue(np.array_equal(mtxs[i], homog_id))
# test in every quadrant
for i in range(2):
for j in range(2):
@ -241,17 +241,17 @@ class VectorTests(unittest.TestCase):
rslt = [1, 2, 3]
for i in range(3):
rslt[i] = mtxs[0].dot(ps2[i])
self.assertTrue(numpy.array_equal(rslt, point_set))
self.assertTrue(np.array_equal(rslt, point_set))
# confirm reverse transform returns translated points
for i in range(3):
rslt[i] = mtxs[1].dot(rslt[i])
self.assertTrue(numpy.array_equal(rslt, ps2))
self.assertTrue(np.array_equal(rslt, ps2))
def test_multi_coord_space(self):
"""Confirm multi_coord_space computes forward, reverse transforms."""
# start with 3 points already aligned to axes
point_set = numpy.array(
point_set = np.array(
[
[
[2.0, 0.0, 2.0, 1.0],
@ -261,12 +261,12 @@ class VectorTests(unittest.TestCase):
]
)
# confirm get id matrix to transform to/from coord space
homog_id = numpy.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
homog_id = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
mtxs = multi_coord_space(point_set, 1, True)
for i in range(2):
self.assertTrue(numpy.array_equal(mtxs[i][0], homog_id))
self.assertTrue(np.array_equal(mtxs[i][0], homog_id))
# test in every quadrant
test_set = numpy.empty([8, 3, 4], dtype=numpy.float64)
test_set = np.empty([8, 3, 4], dtype=np.float64)
m = 0
for i in range(2):
for j in range(2):
@ -285,12 +285,12 @@ class VectorTests(unittest.TestCase):
rslt = [1, 2, 3]
for i in range(3):
rslt[i] = mtxs[0][m].dot(test_set[m][i])
self.assertTrue(numpy.array_equal(rslt, point_set[0]))
self.assertTrue(np.array_equal(rslt, point_set[0]))
# confirm reverse transform returns translated points
for i in range(3):
rslt[i] = mtxs[1][m].dot(rslt[i])
self.assertTrue(numpy.array_equal(rslt, test_set[m]))
self.assertTrue(np.array_equal(rslt, test_set[m]))
if __name__ == "__main__":

View File

@ -12,7 +12,7 @@ import unittest
import warnings
try:
import numpy
import numpy as np
from numpy import dot # Missing on old PyPy's micronumpy
del dot

View File

@ -8,7 +8,7 @@ import unittest
import warnings
try:
import numpy
import numpy as np
from numpy import dot # Missing on PyPy's micronumpy
del dot

View File

@ -11,9 +11,9 @@ and confirms they are consistent using our different parsers.
import unittest
try:
import numpy
import numpy as np
except ImportError:
numpy = None # type: ignore
np = None # type: ignore
from Bio import BiopythonDeprecationWarning
from Bio import SeqIO
@ -245,8 +245,8 @@ Seq('ABCDEFGHIJKLMNOPQRSTUVWZYX')"""
self.assertEqual("BC", self.record[1:3].seq)
with self.assertRaises(ValueError):
c = self.record["a"].seq
if numpy is not None:
start, stop = numpy.array([1, 3]) # numpy integers
if np is not None:
start, stop = np.array([1, 3]) # numpy integers
self.assertEqual("B", self.record[start])
self.assertEqual("BC", self.record[start:stop].seq)

View File

@ -22,9 +22,9 @@ from Bio.Seq import UndefinedSequenceError
from Bio.SeqRecord import SeqRecord
try:
import numpy
import numpy as np
except ImportError:
numpy = None
np = None
# This is just the standard table with less stop codons
# (replaced with coding for O as an artificial example)
@ -89,9 +89,9 @@ class StringMethodTests(unittest.TestCase):
for seq in _examples[:]:
_examples.append(MutableSeq(seq))
_start_end_values = [0, 1, 2, 1000, -1, -2, -999, None]
if numpy is not None:
# test with numpy integers (numpy.int32, numpy.int64 etc.)
_start_end_values.extend(numpy.array([3, 5]))
if np is not None:
# test with numpy integers (np.int32, np.int64 etc.)
_start_end_values.extend(np.array([3, 5]))
def _test_method(self, method_name, start_end=False):
"""Check this method matches the plain string's method."""

View File

@ -453,11 +453,11 @@ class Test_dn_ds(unittest.TestCase):
try:
import numpy
import numpy as np
except ImportError:
numpy = None
np = None
if numpy:
if np:
class Test_MK(unittest.TestCase):
def test_mk(self):

View File

@ -12,9 +12,9 @@ import unittest
import warnings
try:
import numpy
import numpy as np
del numpy
del np
from numpy import asarray
del asarray

View File

@ -8,9 +8,9 @@
"""Tests for the Bio.phenotype module."""
try:
import numpy
import numpy as np
del numpy
del np
except ImportError:
from Bio import MissingExternalDependencyError

View File

@ -8,9 +8,9 @@
"""Tests for the Bio.phenotype module's fitting functionality."""
try:
import numpy
import numpy as np
del numpy
del np
except ImportError:
from Bio import MissingExternalDependencyError

View File

@ -9,9 +9,9 @@ import unittest
import warnings
try:
import numpy
import numpy as np
except ImportError:
numpy = None
np = None
from Bio import BiopythonWarning
from Bio import Seq
@ -469,8 +469,8 @@ class TestSeqMultiplication(unittest.TestCase):
"""Test mul method; relies on addition method."""
for seq in test_seqs + protein_seqs:
self.assertEqual(seq * 3, seq + seq + seq)
if numpy is not None:
factor = numpy.intc(3) # numpy integer
if np is not None:
factor = np.intc(3) # numpy integer
for seq in test_seqs + protein_seqs:
self.assertEqual(seq * factor, seq + seq + seq)
@ -486,8 +486,8 @@ class TestSeqMultiplication(unittest.TestCase):
"""Test rmul method; relies on addition method."""
for seq in test_seqs + protein_seqs:
self.assertEqual(3 * seq, seq + seq + seq)
if numpy is not None:
factor = numpy.intc(3) # numpy integer
if np is not None:
factor = np.intc(3) # numpy integer
for seq in test_seqs + protein_seqs:
self.assertEqual(factor * seq, seq + seq + seq)
@ -505,8 +505,8 @@ class TestSeqMultiplication(unittest.TestCase):
original_seq = seq * 1 # make a copy
seq *= 3
self.assertEqual(seq, original_seq + original_seq + original_seq)
if numpy is not None:
factor = numpy.intc(3) # numpy integer
if np is not None:
factor = np.intc(3) # numpy integer
for seq in test_seqs + protein_seqs:
original_seq = seq * 1 # make a copy
seq *= factor
@ -673,8 +673,8 @@ class TestMutableSeq(unittest.TestCase):
self.mutable_s,
"Set slice with MutableSeq",
)
if numpy is not None:
one, three, five, seven = numpy.array([1, 3, 5, 7]) # numpy integers
if np is not None:
one, three, five, seven = np.array([1, 3, 5, 7]) # numpy integers
self.assertEqual(
Seq.MutableSeq("AATA"), self.mutable_s[one:five], "Slice mutable seq"
)
@ -696,8 +696,8 @@ class TestMutableSeq(unittest.TestCase):
def test_setting_item(self):
self.mutable_s[3] = "G"
self.assertEqual(Seq.MutableSeq("TCAGAAGGATGCATCATG"), self.mutable_s)
if numpy is not None:
i = numpy.intc(3)
if np is not None:
i = np.intc(3)
self.mutable_s[i] = "X"
self.assertEqual(Seq.MutableSeq("TCAXAAGGATGCATCATG"), self.mutable_s)
@ -828,8 +828,8 @@ class TestMutableSeq(unittest.TestCase):
"""Test setting wobble codon to N (set slice with stride 3)."""
self.mutable_s[2::3] = "N" * len(self.mutable_s[2::3])
self.assertEqual(Seq.MutableSeq("TCNAANGGNTGNATNATN"), self.mutable_s)
if numpy is not None:
start, step = numpy.array([2, 3]) # numpy integers
if np is not None:
start, step = np.array([2, 3]) # numpy integers
self.mutable_s[start::step] = "X" * len(self.mutable_s[2::3])
self.assertEqual(Seq.MutableSeq("TCXAAXGGXTGXATXATX"), self.mutable_s)