/usr/lib/python2.7/dist-packages/pint/testsuite/test_numpy.py is in python-pint 0.7.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | # -*- coding: utf-8 -*-
from __future__ import division, unicode_literals, print_function, absolute_import
import copy
import operator as op
from pint import DimensionalityError
from pint.compat import np, unittest
from pint.testsuite import QuantityTestCase, helpers
from pint.testsuite.test_umath import TestUFuncs
@helpers.requires_numpy()
class TestNumpyMethods(QuantityTestCase):
FORCE_NDARRAY = True
@classmethod
def setUpClass(cls):
from pint import _DEFAULT_REGISTRY
cls.ureg = _DEFAULT_REGISTRY
cls.Q_ = cls.ureg.Quantity
@property
def q(self):
return [[1,2],[3,4]] * self.ureg.m
def test_tolist(self):
self.assertEqual(self.q.tolist(), [[1*self.ureg.m, 2*self.ureg.m], [3*self.ureg.m, 4*self.ureg.m]])
def test_sum(self):
self.assertEqual(self.q.sum(), 10*self.ureg.m)
self.assertQuantityEqual(self.q.sum(0), [4, 6]*self.ureg.m)
self.assertQuantityEqual(self.q.sum(1), [3, 7]*self.ureg.m)
def test_fill(self):
tmp = self.q
tmp.fill(6 * self.ureg.ft)
self.assertQuantityEqual(tmp, [[6, 6], [6, 6]] * self.ureg.ft)
tmp.fill(5 * self.ureg.m)
self.assertQuantityEqual(tmp, [[5, 5], [5, 5]] * self.ureg.m)
def test_reshape(self):
self.assertQuantityEqual(self.q.reshape([1,4]), [[1, 2, 3, 4]] * self.ureg.m)
def test_transpose(self):
self.assertQuantityEqual(self.q.transpose(), [[1, 3], [2, 4]] * self.ureg.m)
def test_flatten(self):
self.assertQuantityEqual(self.q.flatten(), [1, 2, 3, 4] * self.ureg.m)
def test_ravel(self):
self.assertQuantityEqual(self.q.ravel(), [1, 2, 3, 4] * self.ureg.m)
def test_squeeze(self):
self.assertQuantityEqual(
self.q.reshape([1,4]).squeeze(),
[1, 2, 3, 4] * self.ureg.m
)
def test_take(self):
self.assertQuantityEqual(self.q.take([0,1,2,3]), self.q.flatten())
def test_put(self):
q = [1., 2., 3., 4.] * self.ureg.m
q.put([0, 2], [10.,20.]*self.ureg.m)
self.assertQuantityEqual(q, [10., 2., 20., 4.]*self.ureg.m)
q = [1., 2., 3., 4.] * self.ureg.m
q.put([0, 2], [1., 2.]*self.ureg.mm)
self.assertQuantityEqual(q, [0.001, 2., 0.002, 4.]*self.ureg.m)
q = [1., 2., 3., 4.] * self.ureg.m / self.ureg.mm
q.put([0, 2], [1., 2.])
self.assertQuantityEqual(q, [0.001, 2., 0.002, 4.]*self.ureg.m/self.ureg.mm)
q = [1., 2., 3., 4.] * self.ureg.m
self.assertRaises(ValueError, q.put, [0, 2], [4., 6.] * self.ureg.J)
self.assertRaises(ValueError, q.put, [0, 2], [4., 6.])
def test_repeat(self):
self.assertQuantityEqual(self.q.repeat(2), [1,1,2,2,3,3,4,4]*self.ureg.m)
def test_sort(self):
q = [4, 5, 2, 3, 1, 6] * self.ureg.m
q.sort()
self.assertQuantityEqual(q, [1, 2, 3, 4, 5, 6] * self.ureg.m)
def test_argsort(self):
q = [1, 4, 5, 6, 2, 9] * self.ureg.MeV
np.testing.assert_array_equal(q.argsort(), [0, 4, 1, 2, 3, 5])
def test_diagonal(self):
q = [[1, 2, 3], [1, 2, 3], [1, 2, 3]] * self.ureg.m
self.assertQuantityEqual(q.diagonal(offset=1), [2, 3] * self.ureg.m)
def test_compress(self):
self.assertQuantityEqual(self.q.compress([False, True], axis=0),
[[3, 4]] * self.ureg.m)
self.assertQuantityEqual(self.q.compress([False, True], axis=1),
[[2], [4]] * self.ureg.m)
def test_searchsorted(self):
q = self.q.flatten()
np.testing.assert_array_equal(q.searchsorted([1.5, 2.5] * self.ureg.m),
[1, 2])
q = self.q.flatten()
self.assertRaises(ValueError, q.searchsorted, [1.5, 2.5])
def test_nonzero(self):
q = [1, 0, 5, 6, 0, 9] * self.ureg.m
np.testing.assert_array_equal(q.nonzero()[0], [0, 2, 3, 5])
def test_max(self):
self.assertEqual(self.q.max(), 4*self.ureg.m)
def test_argmax(self):
self.assertEqual(self.q.argmax(), 3)
def test_min(self):
self.assertEqual(self.q.min(), 1 * self.ureg.m)
def test_argmin(self):
self.assertEqual(self.q.argmin(), 0)
def test_ptp(self):
self.assertEqual(self.q.ptp(), 3 * self.ureg.m)
def test_clip(self):
self.assertQuantityEqual(
self.q.clip(max=2*self.ureg.m),
[[1, 2], [2, 2]] * self.ureg.m
)
self.assertQuantityEqual(
self.q.clip(min=3*self.ureg.m),
[[3, 3], [3, 4]] * self.ureg.m
)
self.assertQuantityEqual(
self.q.clip(min=2*self.ureg.m, max=3*self.ureg.m),
[[2, 2], [3, 3]] * self.ureg.m
)
self.assertRaises(ValueError, self.q.clip, self.ureg.J)
self.assertRaises(ValueError, self.q.clip, 1)
def test_round(self):
q = [1, 1.33, 5.67, 22] * self.ureg.m
self.assertQuantityEqual(q.round(0), [1, 1, 6, 22] * self.ureg.m)
self.assertQuantityEqual(q.round(-1), [0, 0, 10, 20] * self.ureg.m)
self.assertQuantityEqual(q.round(1), [1, 1.3, 5.7, 22] * self.ureg.m)
def test_trace(self):
self.assertEqual(self.q.trace(), (1+4) * self.ureg.m)
def test_cumsum(self):
self.assertQuantityEqual(self.q.cumsum(), [1, 3, 6, 10] * self.ureg.m)
def test_mean(self):
self.assertEqual(self.q.mean(), 2.5 * self.ureg.m)
def test_var(self):
self.assertEqual(self.q.var(), 1.25*self.ureg.m**2)
def test_std(self):
self.assertQuantityAlmostEqual(self.q.std(), 1.11803*self.ureg.m, rtol=1e-5)
def test_prod(self):
self.assertEqual(self.q.prod(), 24 * self.ureg.m**4)
def test_cumprod(self):
self.assertRaises(ValueError, self.q.cumprod)
self.assertQuantityEqual((self.q / self.ureg.m).cumprod(), [1, 2, 6, 24])
@helpers.requires_numpy_previous_than('1.10')
def test_integer_div(self):
a = [1] * self.ureg.m
b = [2] * self.ureg.m
c = a/b # Should be float division
self.assertEqual(c.magnitude[0], 0.5)
a /= b # Should be integer division
self.assertEqual(a.magnitude[0], 0)
def test_conj(self):
self.assertQuantityEqual((self.q*(1+1j)).conj(), self.q*(1-1j))
self.assertQuantityEqual((self.q*(1+1j)).conjugate(), self.q*(1-1j))
def test_getitem(self):
self.assertRaises(IndexError, self.q.__getitem__, (0,10))
self.assertQuantityEqual(self.q[0], [1,2]*self.ureg.m)
self.assertEqual(self.q[1,1], 4*self.ureg.m)
def test_setitem(self):
self.assertRaises(ValueError, self.q.__setitem__, (0,0), 1)
self.assertRaises(ValueError, self.q.__setitem__, (0,0), 1*self.ureg.J)
self.assertRaises(ValueError, self.q.__setitem__, 0, 1)
self.assertRaises(ValueError, self.q.__setitem__, 0, np.ndarray([1, 2]))
self.assertRaises(ValueError, self.q.__setitem__, 0, 1*self.ureg.J)
q = self.q.copy()
q[0] = 1*self.ureg.m
self.assertQuantityEqual(q, [[1,1],[3,4]]*self.ureg.m)
q = self.q.copy()
q.__setitem__(Ellipsis, 1*self.ureg.m)
self.assertQuantityEqual(q, [[1,1],[1,1]]*self.ureg.m)
q = self.q.copy()
q[:] = 1*self.ureg.m
self.assertQuantityEqual(q, [[1,1],[1,1]]*self.ureg.m)
# check and see that dimensionless num bers work correctly
q = [0,1,2,3]*self.ureg.dimensionless
q[0] = 1
self.assertQuantityEqual(q, np.asarray([1,1,2,3]))
q[0] = self.ureg.m/self.ureg.mm
self.assertQuantityEqual(q, np.asarray([1000, 1,2,3]))
q = [0.,1.,2.,3.] * self.ureg.m / self.ureg.mm
q[0] = 1.
self.assertQuantityEqual(q, [0.001,1,2,3]*self.ureg.m / self.ureg.mm)
def test_iterator(self):
for q, v in zip(self.q.flatten(), [1, 2, 3, 4]):
self.assertEqual(q, v * self.ureg.m)
def test_reversible_op(self):
"""
"""
x = self.q.magnitude
u = self.Q_(np.ones(x.shape))
self.assertQuantityEqual(x / self.q, u * x / self.q)
self.assertQuantityEqual(x * self.q, u * x * self.q)
self.assertQuantityEqual(x + u, u + x)
self.assertQuantityEqual(x - u, -(u - x))
def test_pickle(self):
import pickle
def pickle_test(q):
pq = pickle.loads(pickle.dumps(q))
np.testing.assert_array_equal(q.magnitude, pq.magnitude)
self.assertEqual(q.units, pq.units)
pickle_test([10,20]*self.ureg.m)
def test_equal(self):
x = self.q.magnitude
u = self.Q_(np.ones(x.shape))
self.assertQuantityEqual(u, u)
self.assertQuantityEqual(u == u, u.magnitude == u.magnitude)
self.assertQuantityEqual(u == 1, u.magnitude == 1)
@helpers.requires_numpy()
class TestNumpyNeedsSubclassing(TestUFuncs):
FORCE_NDARRAY = True
@property
def q(self):
return [1. ,2., 3., 4.] * self.ureg.J
@unittest.expectedFailure
def test_unwrap(self):
"""unwrap depends on diff
"""
self.assertQuantityEqual(np.unwrap([0,3*np.pi]*self.ureg.radians), [0,np.pi])
self.assertQuantityEqual(np.unwrap([0,540]*self.ureg.deg), [0,180]*self.ureg.deg)
@unittest.expectedFailure
def test_trapz(self):
"""Units are erased by asanyarray, Quantity does not inherit from NDArray
"""
self.assertQuantityEqual(np.trapz(self.q, dx=1*self.ureg.m), 7.5 * self.ureg.J*self.ureg.m)
@unittest.expectedFailure
def test_diff(self):
"""Units are erased by asanyarray, Quantity does not inherit from NDArray
"""
self.assertQuantityEqual(np.diff(self.q, 1), [1, 1, 1] * self.ureg.J)
@unittest.expectedFailure
def test_ediff1d(self):
"""Units are erased by asanyarray, Quantity does not inherit from NDArray
"""
self.assertQuantityEqual(np.ediff1d(self.q, 1 * self.ureg.J), [1, 1, 1] * self.ureg.J)
@unittest.expectedFailure
def test_fix(self):
"""Units are erased by asanyarray, Quantity does not inherit from NDArray
"""
self.assertQuantityEqual(np.fix(3.14 * self.ureg.m), 3.0 * self.ureg.m)
self.assertQuantityEqual(np.fix(3.0 * self.ureg.m), 3.0 * self.ureg.m)
self.assertQuantityEqual(
np.fix([2.1, 2.9, -2.1, -2.9] * self.ureg.m),
[2., 2., -2., -2.] * self.ureg.m
)
@unittest.expectedFailure
def test_gradient(self):
"""shape is a property not a function
"""
l = np.gradient([[1,1],[3,4]] * self.ureg.J, 1 * self.ureg.m)
self.assertQuantityEqual(l[0], [[2., 3.], [2., 3.]] * self.ureg.J / self.ureg.m)
self.assertQuantityEqual(l[1], [[0., 0.], [1., 1.]] * self.ureg.J / self.ureg.m)
@unittest.expectedFailure
def test_cross(self):
"""Units are erased by asarray, Quantity does not inherit from NDArray
"""
a = [[3,-3, 1]] * self.ureg.kPa
b = [[4, 9, 2]] * self.ureg.m**2
self.assertQuantityEqual(np.cross(a, b), [-15, -2, 39] * self.ureg.kPa * self.ureg.m**2)
@unittest.expectedFailure
def test_power(self):
"""This is not supported as different elements might end up with different units
eg. ([1, 1] * m) ** [2, 3]
Must force exponent to single value
"""
self._test2(np.power, self.q1,
(self.qless, np.asarray([1., 2, 3, 4])),
(self.q2, ),)
@unittest.expectedFailure
def test_ones_like(self):
"""Units are erased by emptyarra, Quantity does not inherit from NDArray
"""
self._test1(np.ones_like,
(self.q2, self.qs, self.qless, self.qi),
(),
2)
@unittest.skip
class TestBitTwiddlingUfuncs(TestUFuncs):
"""Universal functions (ufuncs) > Bittwiddling functions
http://docs.scipy.org/doc/numpy/reference/ufuncs.html#bittwiddlingfunctions
bitwise_and(x1, x2[, out]) Compute the bitwise AND of two arrays elementwise.
bitwise_or(x1, x2[, out]) Compute the bitwise OR of two arrays elementwise.
bitwise_xor(x1, x2[, out]) Compute the bitwise XOR of two arrays elementwise.
invert(x[, out]) Compute bitwise inversion, or bitwise NOT, elementwise.
left_shift(x1, x2[, out]) Shift the bits of an integer to the left.
right_shift(x1, x2[, out]) Shift the bits of an integer to the right.
"""
@property
def qless(self):
return np.asarray([1, 2, 3, 4], dtype=np.uint8) * self.ureg.dimensionless
@property
def qs(self):
return 8 * self.ureg.J
@property
def q1(self):
return np.asarray([1, 2, 3, 4], dtype=np.uint8) * self.ureg.J
@property
def q2(self):
return 2 * self.q1
@property
def qm(self):
return np.asarray([1, 2, 3, 4], dtype=np.uint8) * self.ureg.m
def test_bitwise_and(self):
self._test2(np.bitwise_and,
self.q1,
(self.q2, self.qs,),
(self.qm, ),
'same')
def test_bitwise_or(self):
self._test2(np.bitwise_or,
self.q1,
(self.q1, self.q2, self.qs, ),
(self.qm,),
'same')
def test_bitwise_xor(self):
self._test2(np.bitwise_xor,
self.q1,
(self.q1, self.q2, self.qs, ),
(self.qm, ),
'same')
def test_invert(self):
self._test1(np.invert,
(self.q1, self.q2, self.qs, ),
(),
'same')
def test_left_shift(self):
self._test2(np.left_shift,
self.q1,
(self.qless, 2),
(self.q1, self.q2, self.qs, ),
'same')
def test_right_shift(self):
self._test2(np.right_shift,
self.q1,
(self.qless, 2),
(self.q1, self.q2, self.qs, ),
'same')
class TestNDArrayQunatityMath(QuantityTestCase):
@helpers.requires_numpy()
def test_exponentiation_array_exp(self):
arr = np.array(range(3), dtype=np.float)
q = self.Q_(arr, None)
for op_ in [op.pow, op.ipow]:
q_cp = copy.copy(q)
self.assertRaises(DimensionalityError, op_, 2., q_cp)
arr_cp = copy.copy(arr)
arr_cp = copy.copy(arr)
q_cp = copy.copy(q)
self.assertRaises(DimensionalityError, op_, q_cp, arr_cp)
q_cp = copy.copy(q)
q2_cp = copy.copy(q)
self.assertRaises(DimensionalityError, op_, q_cp, q2_cp)
@unittest.expectedFailure
@helpers.requires_numpy()
def test_exponentiation_array_exp_2(self):
arr = np.array(range(3), dtype=np.float)
#q = self.Q_(copy.copy(arr), None)
q = self.Q_(copy.copy(arr), 'meter')
arr_cp = copy.copy(arr)
q_cp = copy.copy(q)
# this fails as expected since numpy 1.8.0 but...
self.assertRaises(DimensionalityError, op.pow, arr_cp, q_cp)
# ..not for op.ipow !
# q_cp is treated as if it is an array. The units are ignored.
# _Quantity.__ipow__ is never called
arr_cp = copy.copy(arr)
q_cp = copy.copy(q)
self.assertRaises(DimensionalityError, op.ipow, arr_cp, q_cp)
|