test_recfunctions.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. from __future__ import division, absolute_import, print_function
  2. import pytest
  3. import numpy as np
  4. import numpy.ma as ma
  5. from numpy.ma.mrecords import MaskedRecords
  6. from numpy.ma.testutils import assert_equal
  7. from numpy.testing import assert_, assert_raises
  8. from numpy.lib.recfunctions import (
  9. drop_fields, rename_fields, get_fieldstructure, recursive_fill_fields,
  10. find_duplicates, merge_arrays, append_fields, stack_arrays, join_by,
  11. repack_fields, unstructured_to_structured, structured_to_unstructured,
  12. apply_along_fields, require_fields, assign_fields_by_name)
  13. get_fieldspec = np.lib.recfunctions._get_fieldspec
  14. get_names = np.lib.recfunctions.get_names
  15. get_names_flat = np.lib.recfunctions.get_names_flat
  16. zip_descr = np.lib.recfunctions._zip_descr
  17. zip_dtype = np.lib.recfunctions._zip_dtype
  18. class TestRecFunctions(object):
  19. # Misc tests
  20. def setup(self):
  21. x = np.array([1, 2, ])
  22. y = np.array([10, 20, 30])
  23. z = np.array([('A', 1.), ('B', 2.)],
  24. dtype=[('A', '|S3'), ('B', float)])
  25. w = np.array([(1, (2, 3.0)), (4, (5, 6.0))],
  26. dtype=[('a', int), ('b', [('ba', float), ('bb', int)])])
  27. self.data = (w, x, y, z)
  28. def test_zip_descr(self):
  29. # Test zip_descr
  30. (w, x, y, z) = self.data
  31. # Std array
  32. test = zip_descr((x, x), flatten=True)
  33. assert_equal(test,
  34. np.dtype([('', int), ('', int)]))
  35. test = zip_descr((x, x), flatten=False)
  36. assert_equal(test,
  37. np.dtype([('', int), ('', int)]))
  38. # Std & flexible-dtype
  39. test = zip_descr((x, z), flatten=True)
  40. assert_equal(test,
  41. np.dtype([('', int), ('A', '|S3'), ('B', float)]))
  42. test = zip_descr((x, z), flatten=False)
  43. assert_equal(test,
  44. np.dtype([('', int),
  45. ('', [('A', '|S3'), ('B', float)])]))
  46. # Standard & nested dtype
  47. test = zip_descr((x, w), flatten=True)
  48. assert_equal(test,
  49. np.dtype([('', int),
  50. ('a', int),
  51. ('ba', float), ('bb', int)]))
  52. test = zip_descr((x, w), flatten=False)
  53. assert_equal(test,
  54. np.dtype([('', int),
  55. ('', [('a', int),
  56. ('b', [('ba', float), ('bb', int)])])]))
  57. def test_drop_fields(self):
  58. # Test drop_fields
  59. a = np.array([(1, (2, 3.0)), (4, (5, 6.0))],
  60. dtype=[('a', int), ('b', [('ba', float), ('bb', int)])])
  61. # A basic field
  62. test = drop_fields(a, 'a')
  63. control = np.array([((2, 3.0),), ((5, 6.0),)],
  64. dtype=[('b', [('ba', float), ('bb', int)])])
  65. assert_equal(test, control)
  66. # Another basic field (but nesting two fields)
  67. test = drop_fields(a, 'b')
  68. control = np.array([(1,), (4,)], dtype=[('a', int)])
  69. assert_equal(test, control)
  70. # A nested sub-field
  71. test = drop_fields(a, ['ba', ])
  72. control = np.array([(1, (3.0,)), (4, (6.0,))],
  73. dtype=[('a', int), ('b', [('bb', int)])])
  74. assert_equal(test, control)
  75. # All the nested sub-field from a field: zap that field
  76. test = drop_fields(a, ['ba', 'bb'])
  77. control = np.array([(1,), (4,)], dtype=[('a', int)])
  78. assert_equal(test, control)
  79. test = drop_fields(a, ['a', 'b'])
  80. assert_(test is None)
  81. def test_rename_fields(self):
  82. # Test rename fields
  83. a = np.array([(1, (2, [3.0, 30.])), (4, (5, [6.0, 60.]))],
  84. dtype=[('a', int),
  85. ('b', [('ba', float), ('bb', (float, 2))])])
  86. test = rename_fields(a, {'a': 'A', 'bb': 'BB'})
  87. newdtype = [('A', int), ('b', [('ba', float), ('BB', (float, 2))])]
  88. control = a.view(newdtype)
  89. assert_equal(test.dtype, newdtype)
  90. assert_equal(test, control)
  91. def test_get_names(self):
  92. # Test get_names
  93. ndtype = np.dtype([('A', '|S3'), ('B', float)])
  94. test = get_names(ndtype)
  95. assert_equal(test, ('A', 'B'))
  96. ndtype = np.dtype([('a', int), ('b', [('ba', float), ('bb', int)])])
  97. test = get_names(ndtype)
  98. assert_equal(test, ('a', ('b', ('ba', 'bb'))))
  99. ndtype = np.dtype([('a', int), ('b', [])])
  100. test = get_names(ndtype)
  101. assert_equal(test, ('a', ('b', ())))
  102. ndtype = np.dtype([])
  103. test = get_names(ndtype)
  104. assert_equal(test, ())
  105. def test_get_names_flat(self):
  106. # Test get_names_flat
  107. ndtype = np.dtype([('A', '|S3'), ('B', float)])
  108. test = get_names_flat(ndtype)
  109. assert_equal(test, ('A', 'B'))
  110. ndtype = np.dtype([('a', int), ('b', [('ba', float), ('bb', int)])])
  111. test = get_names_flat(ndtype)
  112. assert_equal(test, ('a', 'b', 'ba', 'bb'))
  113. ndtype = np.dtype([('a', int), ('b', [])])
  114. test = get_names_flat(ndtype)
  115. assert_equal(test, ('a', 'b'))
  116. ndtype = np.dtype([])
  117. test = get_names_flat(ndtype)
  118. assert_equal(test, ())
  119. def test_get_fieldstructure(self):
  120. # Test get_fieldstructure
  121. # No nested fields
  122. ndtype = np.dtype([('A', '|S3'), ('B', float)])
  123. test = get_fieldstructure(ndtype)
  124. assert_equal(test, {'A': [], 'B': []})
  125. # One 1-nested field
  126. ndtype = np.dtype([('A', int), ('B', [('BA', float), ('BB', '|S1')])])
  127. test = get_fieldstructure(ndtype)
  128. assert_equal(test, {'A': [], 'B': [], 'BA': ['B', ], 'BB': ['B']})
  129. # One 2-nested fields
  130. ndtype = np.dtype([('A', int),
  131. ('B', [('BA', int),
  132. ('BB', [('BBA', int), ('BBB', int)])])])
  133. test = get_fieldstructure(ndtype)
  134. control = {'A': [], 'B': [], 'BA': ['B'], 'BB': ['B'],
  135. 'BBA': ['B', 'BB'], 'BBB': ['B', 'BB']}
  136. assert_equal(test, control)
  137. # 0 fields
  138. ndtype = np.dtype([])
  139. test = get_fieldstructure(ndtype)
  140. assert_equal(test, {})
  141. def test_find_duplicates(self):
  142. # Test find_duplicates
  143. a = ma.array([(2, (2., 'B')), (1, (2., 'B')), (2, (2., 'B')),
  144. (1, (1., 'B')), (2, (2., 'B')), (2, (2., 'C'))],
  145. mask=[(0, (0, 0)), (0, (0, 0)), (0, (0, 0)),
  146. (0, (0, 0)), (1, (0, 0)), (0, (1, 0))],
  147. dtype=[('A', int), ('B', [('BA', float), ('BB', '|S1')])])
  148. test = find_duplicates(a, ignoremask=False, return_index=True)
  149. control = [0, 2]
  150. assert_equal(sorted(test[-1]), control)
  151. assert_equal(test[0], a[test[-1]])
  152. test = find_duplicates(a, key='A', return_index=True)
  153. control = [0, 1, 2, 3, 5]
  154. assert_equal(sorted(test[-1]), control)
  155. assert_equal(test[0], a[test[-1]])
  156. test = find_duplicates(a, key='B', return_index=True)
  157. control = [0, 1, 2, 4]
  158. assert_equal(sorted(test[-1]), control)
  159. assert_equal(test[0], a[test[-1]])
  160. test = find_duplicates(a, key='BA', return_index=True)
  161. control = [0, 1, 2, 4]
  162. assert_equal(sorted(test[-1]), control)
  163. assert_equal(test[0], a[test[-1]])
  164. test = find_duplicates(a, key='BB', return_index=True)
  165. control = [0, 1, 2, 3, 4]
  166. assert_equal(sorted(test[-1]), control)
  167. assert_equal(test[0], a[test[-1]])
  168. def test_find_duplicates_ignoremask(self):
  169. # Test the ignoremask option of find_duplicates
  170. ndtype = [('a', int)]
  171. a = ma.array([1, 1, 1, 2, 2, 3, 3],
  172. mask=[0, 0, 1, 0, 0, 0, 1]).view(ndtype)
  173. test = find_duplicates(a, ignoremask=True, return_index=True)
  174. control = [0, 1, 3, 4]
  175. assert_equal(sorted(test[-1]), control)
  176. assert_equal(test[0], a[test[-1]])
  177. test = find_duplicates(a, ignoremask=False, return_index=True)
  178. control = [0, 1, 2, 3, 4, 6]
  179. assert_equal(sorted(test[-1]), control)
  180. assert_equal(test[0], a[test[-1]])
  181. def test_repack_fields(self):
  182. dt = np.dtype('u1,f4,i8', align=True)
  183. a = np.zeros(2, dtype=dt)
  184. assert_equal(repack_fields(dt), np.dtype('u1,f4,i8'))
  185. assert_equal(repack_fields(a).itemsize, 13)
  186. assert_equal(repack_fields(repack_fields(dt), align=True), dt)
  187. # make sure type is preserved
  188. dt = np.dtype((np.record, dt))
  189. assert_(repack_fields(dt).type is np.record)
  190. def test_structured_to_unstructured(self):
  191. a = np.zeros(4, dtype=[('a', 'i4'), ('b', 'f4,u2'), ('c', 'f4', 2)])
  192. out = structured_to_unstructured(a)
  193. assert_equal(out, np.zeros((4,5), dtype='f8'))
  194. b = np.array([(1, 2, 5), (4, 5, 7), (7, 8 ,11), (10, 11, 12)],
  195. dtype=[('x', 'i4'), ('y', 'f4'), ('z', 'f8')])
  196. out = np.mean(structured_to_unstructured(b[['x', 'z']]), axis=-1)
  197. assert_equal(out, np.array([ 3. , 5.5, 9. , 11. ]))
  198. out = np.mean(structured_to_unstructured(b[['x']]), axis=-1)
  199. assert_equal(out, np.array([ 1. , 4. , 7. , 10. ]))
  200. c = np.arange(20).reshape((4,5))
  201. out = unstructured_to_structured(c, a.dtype)
  202. want = np.array([( 0, ( 1., 2), [ 3., 4.]),
  203. ( 5, ( 6., 7), [ 8., 9.]),
  204. (10, (11., 12), [13., 14.]),
  205. (15, (16., 17), [18., 19.])],
  206. dtype=[('a', 'i4'),
  207. ('b', [('f0', 'f4'), ('f1', 'u2')]),
  208. ('c', 'f4', (2,))])
  209. assert_equal(out, want)
  210. d = np.array([(1, 2, 5), (4, 5, 7), (7, 8 ,11), (10, 11, 12)],
  211. dtype=[('x', 'i4'), ('y', 'f4'), ('z', 'f8')])
  212. assert_equal(apply_along_fields(np.mean, d),
  213. np.array([ 8.0/3, 16.0/3, 26.0/3, 11. ]))
  214. assert_equal(apply_along_fields(np.mean, d[['x', 'z']]),
  215. np.array([ 3. , 5.5, 9. , 11. ]))
  216. # check that for uniform field dtypes we get a view, not a copy:
  217. d = np.array([(1, 2, 5), (4, 5, 7), (7, 8 ,11), (10, 11, 12)],
  218. dtype=[('x', 'i4'), ('y', 'i4'), ('z', 'i4')])
  219. dd = structured_to_unstructured(d)
  220. ddd = unstructured_to_structured(dd, d.dtype)
  221. assert_(dd.base is d)
  222. assert_(ddd.base is d)
  223. # including uniform fields with subarrays unpacked
  224. d = np.array([(1, [2, 3], [[ 4, 5], [ 6, 7]]),
  225. (8, [9, 10], [[11, 12], [13, 14]])],
  226. dtype=[('x0', 'i4'), ('x1', ('i4', 2)),
  227. ('x2', ('i4', (2, 2)))])
  228. dd = structured_to_unstructured(d)
  229. ddd = unstructured_to_structured(dd, d.dtype)
  230. assert_(dd.base is d)
  231. assert_(ddd.base is d)
  232. # test that nested fields with identical names don't break anything
  233. point = np.dtype([('x', int), ('y', int)])
  234. triangle = np.dtype([('a', point), ('b', point), ('c', point)])
  235. arr = np.zeros(10, triangle)
  236. res = structured_to_unstructured(arr, dtype=int)
  237. assert_equal(res, np.zeros((10, 6), dtype=int))
  238. # test nested combinations of subarrays and structured arrays, gh-13333
  239. def subarray(dt, shape):
  240. return np.dtype((dt, shape))
  241. def structured(*dts):
  242. return np.dtype([('x{}'.format(i), dt) for i, dt in enumerate(dts)])
  243. def inspect(dt, dtype=None):
  244. arr = np.zeros((), dt)
  245. ret = structured_to_unstructured(arr, dtype=dtype)
  246. backarr = unstructured_to_structured(ret, dt)
  247. return ret.shape, ret.dtype, backarr.dtype
  248. dt = structured(subarray(structured(np.int32, np.int32), 3))
  249. assert_equal(inspect(dt), ((6,), np.int32, dt))
  250. dt = structured(subarray(subarray(np.int32, 2), 2))
  251. assert_equal(inspect(dt), ((4,), np.int32, dt))
  252. dt = structured(np.int32)
  253. assert_equal(inspect(dt), ((1,), np.int32, dt))
  254. dt = structured(np.int32, subarray(subarray(np.int32, 2), 2))
  255. assert_equal(inspect(dt), ((5,), np.int32, dt))
  256. dt = structured()
  257. assert_raises(ValueError, structured_to_unstructured, np.zeros(3, dt))
  258. # these currently don't work, but we may make it work in the future
  259. assert_raises(NotImplementedError, structured_to_unstructured,
  260. np.zeros(3, dt), dtype=np.int32)
  261. assert_raises(NotImplementedError, unstructured_to_structured,
  262. np.zeros((3,0), dtype=np.int32))
  263. def test_field_assignment_by_name(self):
  264. a = np.ones(2, dtype=[('a', 'i4'), ('b', 'f8'), ('c', 'u1')])
  265. newdt = [('b', 'f4'), ('c', 'u1')]
  266. assert_equal(require_fields(a, newdt), np.ones(2, newdt))
  267. b = np.array([(1,2), (3,4)], dtype=newdt)
  268. assign_fields_by_name(a, b, zero_unassigned=False)
  269. assert_equal(a, np.array([(1,1,2),(1,3,4)], dtype=a.dtype))
  270. assign_fields_by_name(a, b)
  271. assert_equal(a, np.array([(0,1,2),(0,3,4)], dtype=a.dtype))
  272. # test nested fields
  273. a = np.ones(2, dtype=[('a', [('b', 'f8'), ('c', 'u1')])])
  274. newdt = [('a', [('c', 'u1')])]
  275. assert_equal(require_fields(a, newdt), np.ones(2, newdt))
  276. b = np.array([((2,),), ((3,),)], dtype=newdt)
  277. assign_fields_by_name(a, b, zero_unassigned=False)
  278. assert_equal(a, np.array([((1,2),), ((1,3),)], dtype=a.dtype))
  279. assign_fields_by_name(a, b)
  280. assert_equal(a, np.array([((0,2),), ((0,3),)], dtype=a.dtype))
  281. # test unstructured code path for 0d arrays
  282. a, b = np.array(3), np.array(0)
  283. assign_fields_by_name(b, a)
  284. assert_equal(b[()], 3)
  285. class TestRecursiveFillFields(object):
  286. # Test recursive_fill_fields.
  287. def test_simple_flexible(self):
  288. # Test recursive_fill_fields on flexible-array
  289. a = np.array([(1, 10.), (2, 20.)], dtype=[('A', int), ('B', float)])
  290. b = np.zeros((3,), dtype=a.dtype)
  291. test = recursive_fill_fields(a, b)
  292. control = np.array([(1, 10.), (2, 20.), (0, 0.)],
  293. dtype=[('A', int), ('B', float)])
  294. assert_equal(test, control)
  295. def test_masked_flexible(self):
  296. # Test recursive_fill_fields on masked flexible-array
  297. a = ma.array([(1, 10.), (2, 20.)], mask=[(0, 1), (1, 0)],
  298. dtype=[('A', int), ('B', float)])
  299. b = ma.zeros((3,), dtype=a.dtype)
  300. test = recursive_fill_fields(a, b)
  301. control = ma.array([(1, 10.), (2, 20.), (0, 0.)],
  302. mask=[(0, 1), (1, 0), (0, 0)],
  303. dtype=[('A', int), ('B', float)])
  304. assert_equal(test, control)
  305. class TestMergeArrays(object):
  306. # Test merge_arrays
  307. def setup(self):
  308. x = np.array([1, 2, ])
  309. y = np.array([10, 20, 30])
  310. z = np.array(
  311. [('A', 1.), ('B', 2.)], dtype=[('A', '|S3'), ('B', float)])
  312. w = np.array(
  313. [(1, (2, 3.0)), (4, (5, 6.0))],
  314. dtype=[('a', int), ('b', [('ba', float), ('bb', int)])])
  315. self.data = (w, x, y, z)
  316. def test_solo(self):
  317. # Test merge_arrays on a single array.
  318. (_, x, _, z) = self.data
  319. test = merge_arrays(x)
  320. control = np.array([(1,), (2,)], dtype=[('f0', int)])
  321. assert_equal(test, control)
  322. test = merge_arrays((x,))
  323. assert_equal(test, control)
  324. test = merge_arrays(z, flatten=False)
  325. assert_equal(test, z)
  326. test = merge_arrays(z, flatten=True)
  327. assert_equal(test, z)
  328. def test_solo_w_flatten(self):
  329. # Test merge_arrays on a single array w & w/o flattening
  330. w = self.data[0]
  331. test = merge_arrays(w, flatten=False)
  332. assert_equal(test, w)
  333. test = merge_arrays(w, flatten=True)
  334. control = np.array([(1, 2, 3.0), (4, 5, 6.0)],
  335. dtype=[('a', int), ('ba', float), ('bb', int)])
  336. assert_equal(test, control)
  337. def test_standard(self):
  338. # Test standard & standard
  339. # Test merge arrays
  340. (_, x, y, _) = self.data
  341. test = merge_arrays((x, y), usemask=False)
  342. control = np.array([(1, 10), (2, 20), (-1, 30)],
  343. dtype=[('f0', int), ('f1', int)])
  344. assert_equal(test, control)
  345. test = merge_arrays((x, y), usemask=True)
  346. control = ma.array([(1, 10), (2, 20), (-1, 30)],
  347. mask=[(0, 0), (0, 0), (1, 0)],
  348. dtype=[('f0', int), ('f1', int)])
  349. assert_equal(test, control)
  350. assert_equal(test.mask, control.mask)
  351. def test_flatten(self):
  352. # Test standard & flexible
  353. (_, x, _, z) = self.data
  354. test = merge_arrays((x, z), flatten=True)
  355. control = np.array([(1, 'A', 1.), (2, 'B', 2.)],
  356. dtype=[('f0', int), ('A', '|S3'), ('B', float)])
  357. assert_equal(test, control)
  358. test = merge_arrays((x, z), flatten=False)
  359. control = np.array([(1, ('A', 1.)), (2, ('B', 2.))],
  360. dtype=[('f0', int),
  361. ('f1', [('A', '|S3'), ('B', float)])])
  362. assert_equal(test, control)
  363. def test_flatten_wflexible(self):
  364. # Test flatten standard & nested
  365. (w, x, _, _) = self.data
  366. test = merge_arrays((x, w), flatten=True)
  367. control = np.array([(1, 1, 2, 3.0), (2, 4, 5, 6.0)],
  368. dtype=[('f0', int),
  369. ('a', int), ('ba', float), ('bb', int)])
  370. assert_equal(test, control)
  371. test = merge_arrays((x, w), flatten=False)
  372. controldtype = [('f0', int),
  373. ('f1', [('a', int),
  374. ('b', [('ba', float), ('bb', int)])])]
  375. control = np.array([(1., (1, (2, 3.0))), (2, (4, (5, 6.0)))],
  376. dtype=controldtype)
  377. assert_equal(test, control)
  378. def test_wmasked_arrays(self):
  379. # Test merge_arrays masked arrays
  380. (_, x, _, _) = self.data
  381. mx = ma.array([1, 2, 3], mask=[1, 0, 0])
  382. test = merge_arrays((x, mx), usemask=True)
  383. control = ma.array([(1, 1), (2, 2), (-1, 3)],
  384. mask=[(0, 1), (0, 0), (1, 0)],
  385. dtype=[('f0', int), ('f1', int)])
  386. assert_equal(test, control)
  387. test = merge_arrays((x, mx), usemask=True, asrecarray=True)
  388. assert_equal(test, control)
  389. assert_(isinstance(test, MaskedRecords))
  390. def test_w_singlefield(self):
  391. # Test single field
  392. test = merge_arrays((np.array([1, 2]).view([('a', int)]),
  393. np.array([10., 20., 30.])),)
  394. control = ma.array([(1, 10.), (2, 20.), (-1, 30.)],
  395. mask=[(0, 0), (0, 0), (1, 0)],
  396. dtype=[('a', int), ('f1', float)])
  397. assert_equal(test, control)
  398. def test_w_shorter_flex(self):
  399. # Test merge_arrays w/ a shorter flexndarray.
  400. z = self.data[-1]
  401. # Fixme, this test looks incomplete and broken
  402. #test = merge_arrays((z, np.array([10, 20, 30]).view([('C', int)])))
  403. #control = np.array([('A', 1., 10), ('B', 2., 20), ('-1', -1, 20)],
  404. # dtype=[('A', '|S3'), ('B', float), ('C', int)])
  405. #assert_equal(test, control)
  406. # Hack to avoid pyflakes warnings about unused variables
  407. merge_arrays((z, np.array([10, 20, 30]).view([('C', int)])))
  408. np.array([('A', 1., 10), ('B', 2., 20), ('-1', -1, 20)],
  409. dtype=[('A', '|S3'), ('B', float), ('C', int)])
  410. def test_singlerecord(self):
  411. (_, x, y, z) = self.data
  412. test = merge_arrays((x[0], y[0], z[0]), usemask=False)
  413. control = np.array([(1, 10, ('A', 1))],
  414. dtype=[('f0', int),
  415. ('f1', int),
  416. ('f2', [('A', '|S3'), ('B', float)])])
  417. assert_equal(test, control)
  418. class TestAppendFields(object):
  419. # Test append_fields
  420. def setup(self):
  421. x = np.array([1, 2, ])
  422. y = np.array([10, 20, 30])
  423. z = np.array(
  424. [('A', 1.), ('B', 2.)], dtype=[('A', '|S3'), ('B', float)])
  425. w = np.array([(1, (2, 3.0)), (4, (5, 6.0))],
  426. dtype=[('a', int), ('b', [('ba', float), ('bb', int)])])
  427. self.data = (w, x, y, z)
  428. def test_append_single(self):
  429. # Test simple case
  430. (_, x, _, _) = self.data
  431. test = append_fields(x, 'A', data=[10, 20, 30])
  432. control = ma.array([(1, 10), (2, 20), (-1, 30)],
  433. mask=[(0, 0), (0, 0), (1, 0)],
  434. dtype=[('f0', int), ('A', int)],)
  435. assert_equal(test, control)
  436. def test_append_double(self):
  437. # Test simple case
  438. (_, x, _, _) = self.data
  439. test = append_fields(x, ('A', 'B'), data=[[10, 20, 30], [100, 200]])
  440. control = ma.array([(1, 10, 100), (2, 20, 200), (-1, 30, -1)],
  441. mask=[(0, 0, 0), (0, 0, 0), (1, 0, 1)],
  442. dtype=[('f0', int), ('A', int), ('B', int)],)
  443. assert_equal(test, control)
  444. def test_append_on_flex(self):
  445. # Test append_fields on flexible type arrays
  446. z = self.data[-1]
  447. test = append_fields(z, 'C', data=[10, 20, 30])
  448. control = ma.array([('A', 1., 10), ('B', 2., 20), (-1, -1., 30)],
  449. mask=[(0, 0, 0), (0, 0, 0), (1, 1, 0)],
  450. dtype=[('A', '|S3'), ('B', float), ('C', int)],)
  451. assert_equal(test, control)
  452. def test_append_on_nested(self):
  453. # Test append_fields on nested fields
  454. w = self.data[0]
  455. test = append_fields(w, 'C', data=[10, 20, 30])
  456. control = ma.array([(1, (2, 3.0), 10),
  457. (4, (5, 6.0), 20),
  458. (-1, (-1, -1.), 30)],
  459. mask=[(
  460. 0, (0, 0), 0), (0, (0, 0), 0), (1, (1, 1), 0)],
  461. dtype=[('a', int),
  462. ('b', [('ba', float), ('bb', int)]),
  463. ('C', int)],)
  464. assert_equal(test, control)
  465. class TestStackArrays(object):
  466. # Test stack_arrays
  467. def setup(self):
  468. x = np.array([1, 2, ])
  469. y = np.array([10, 20, 30])
  470. z = np.array(
  471. [('A', 1.), ('B', 2.)], dtype=[('A', '|S3'), ('B', float)])
  472. w = np.array([(1, (2, 3.0)), (4, (5, 6.0))],
  473. dtype=[('a', int), ('b', [('ba', float), ('bb', int)])])
  474. self.data = (w, x, y, z)
  475. def test_solo(self):
  476. # Test stack_arrays on single arrays
  477. (_, x, _, _) = self.data
  478. test = stack_arrays((x,))
  479. assert_equal(test, x)
  480. assert_(test is x)
  481. test = stack_arrays(x)
  482. assert_equal(test, x)
  483. assert_(test is x)
  484. def test_unnamed_fields(self):
  485. # Tests combinations of arrays w/o named fields
  486. (_, x, y, _) = self.data
  487. test = stack_arrays((x, x), usemask=False)
  488. control = np.array([1, 2, 1, 2])
  489. assert_equal(test, control)
  490. test = stack_arrays((x, y), usemask=False)
  491. control = np.array([1, 2, 10, 20, 30])
  492. assert_equal(test, control)
  493. test = stack_arrays((y, x), usemask=False)
  494. control = np.array([10, 20, 30, 1, 2])
  495. assert_equal(test, control)
  496. def test_unnamed_and_named_fields(self):
  497. # Test combination of arrays w/ & w/o named fields
  498. (_, x, _, z) = self.data
  499. test = stack_arrays((x, z))
  500. control = ma.array([(1, -1, -1), (2, -1, -1),
  501. (-1, 'A', 1), (-1, 'B', 2)],
  502. mask=[(0, 1, 1), (0, 1, 1),
  503. (1, 0, 0), (1, 0, 0)],
  504. dtype=[('f0', int), ('A', '|S3'), ('B', float)])
  505. assert_equal(test, control)
  506. assert_equal(test.mask, control.mask)
  507. test = stack_arrays((z, x))
  508. control = ma.array([('A', 1, -1), ('B', 2, -1),
  509. (-1, -1, 1), (-1, -1, 2), ],
  510. mask=[(0, 0, 1), (0, 0, 1),
  511. (1, 1, 0), (1, 1, 0)],
  512. dtype=[('A', '|S3'), ('B', float), ('f2', int)])
  513. assert_equal(test, control)
  514. assert_equal(test.mask, control.mask)
  515. test = stack_arrays((z, z, x))
  516. control = ma.array([('A', 1, -1), ('B', 2, -1),
  517. ('A', 1, -1), ('B', 2, -1),
  518. (-1, -1, 1), (-1, -1, 2), ],
  519. mask=[(0, 0, 1), (0, 0, 1),
  520. (0, 0, 1), (0, 0, 1),
  521. (1, 1, 0), (1, 1, 0)],
  522. dtype=[('A', '|S3'), ('B', float), ('f2', int)])
  523. assert_equal(test, control)
  524. def test_matching_named_fields(self):
  525. # Test combination of arrays w/ matching field names
  526. (_, x, _, z) = self.data
  527. zz = np.array([('a', 10., 100.), ('b', 20., 200.), ('c', 30., 300.)],
  528. dtype=[('A', '|S3'), ('B', float), ('C', float)])
  529. test = stack_arrays((z, zz))
  530. control = ma.array([('A', 1, -1), ('B', 2, -1),
  531. (
  532. 'a', 10., 100.), ('b', 20., 200.), ('c', 30., 300.)],
  533. dtype=[('A', '|S3'), ('B', float), ('C', float)],
  534. mask=[(0, 0, 1), (0, 0, 1),
  535. (0, 0, 0), (0, 0, 0), (0, 0, 0)])
  536. assert_equal(test, control)
  537. assert_equal(test.mask, control.mask)
  538. test = stack_arrays((z, zz, x))
  539. ndtype = [('A', '|S3'), ('B', float), ('C', float), ('f3', int)]
  540. control = ma.array([('A', 1, -1, -1), ('B', 2, -1, -1),
  541. ('a', 10., 100., -1), ('b', 20., 200., -1),
  542. ('c', 30., 300., -1),
  543. (-1, -1, -1, 1), (-1, -1, -1, 2)],
  544. dtype=ndtype,
  545. mask=[(0, 0, 1, 1), (0, 0, 1, 1),
  546. (0, 0, 0, 1), (0, 0, 0, 1), (0, 0, 0, 1),
  547. (1, 1, 1, 0), (1, 1, 1, 0)])
  548. assert_equal(test, control)
  549. assert_equal(test.mask, control.mask)
  550. def test_defaults(self):
  551. # Test defaults: no exception raised if keys of defaults are not fields.
  552. (_, _, _, z) = self.data
  553. zz = np.array([('a', 10., 100.), ('b', 20., 200.), ('c', 30., 300.)],
  554. dtype=[('A', '|S3'), ('B', float), ('C', float)])
  555. defaults = {'A': '???', 'B': -999., 'C': -9999., 'D': -99999.}
  556. test = stack_arrays((z, zz), defaults=defaults)
  557. control = ma.array([('A', 1, -9999.), ('B', 2, -9999.),
  558. (
  559. 'a', 10., 100.), ('b', 20., 200.), ('c', 30., 300.)],
  560. dtype=[('A', '|S3'), ('B', float), ('C', float)],
  561. mask=[(0, 0, 1), (0, 0, 1),
  562. (0, 0, 0), (0, 0, 0), (0, 0, 0)])
  563. assert_equal(test, control)
  564. assert_equal(test.data, control.data)
  565. assert_equal(test.mask, control.mask)
  566. def test_autoconversion(self):
  567. # Tests autoconversion
  568. adtype = [('A', int), ('B', bool), ('C', float)]
  569. a = ma.array([(1, 2, 3)], mask=[(0, 1, 0)], dtype=adtype)
  570. bdtype = [('A', int), ('B', float), ('C', float)]
  571. b = ma.array([(4, 5, 6)], dtype=bdtype)
  572. control = ma.array([(1, 2, 3), (4, 5, 6)], mask=[(0, 1, 0), (0, 0, 0)],
  573. dtype=bdtype)
  574. test = stack_arrays((a, b), autoconvert=True)
  575. assert_equal(test, control)
  576. assert_equal(test.mask, control.mask)
  577. with assert_raises(TypeError):
  578. stack_arrays((a, b), autoconvert=False)
  579. def test_checktitles(self):
  580. # Test using titles in the field names
  581. adtype = [(('a', 'A'), int), (('b', 'B'), bool), (('c', 'C'), float)]
  582. a = ma.array([(1, 2, 3)], mask=[(0, 1, 0)], dtype=adtype)
  583. bdtype = [(('a', 'A'), int), (('b', 'B'), bool), (('c', 'C'), float)]
  584. b = ma.array([(4, 5, 6)], dtype=bdtype)
  585. test = stack_arrays((a, b))
  586. control = ma.array([(1, 2, 3), (4, 5, 6)], mask=[(0, 1, 0), (0, 0, 0)],
  587. dtype=bdtype)
  588. assert_equal(test, control)
  589. assert_equal(test.mask, control.mask)
  590. def test_subdtype(self):
  591. z = np.array([
  592. ('A', 1), ('B', 2)
  593. ], dtype=[('A', '|S3'), ('B', float, (1,))])
  594. zz = np.array([
  595. ('a', [10.], 100.), ('b', [20.], 200.), ('c', [30.], 300.)
  596. ], dtype=[('A', '|S3'), ('B', float, (1,)), ('C', float)])
  597. res = stack_arrays((z, zz))
  598. expected = ma.array(
  599. data=[
  600. (b'A', [1.0], 0),
  601. (b'B', [2.0], 0),
  602. (b'a', [10.0], 100.0),
  603. (b'b', [20.0], 200.0),
  604. (b'c', [30.0], 300.0)],
  605. mask=[
  606. (False, [False], True),
  607. (False, [False], True),
  608. (False, [False], False),
  609. (False, [False], False),
  610. (False, [False], False)
  611. ],
  612. dtype=zz.dtype
  613. )
  614. assert_equal(res.dtype, expected.dtype)
  615. assert_equal(res, expected)
  616. assert_equal(res.mask, expected.mask)
  617. class TestJoinBy(object):
  618. def setup(self):
  619. self.a = np.array(list(zip(np.arange(10), np.arange(50, 60),
  620. np.arange(100, 110))),
  621. dtype=[('a', int), ('b', int), ('c', int)])
  622. self.b = np.array(list(zip(np.arange(5, 15), np.arange(65, 75),
  623. np.arange(100, 110))),
  624. dtype=[('a', int), ('b', int), ('d', int)])
  625. def test_inner_join(self):
  626. # Basic test of join_by
  627. a, b = self.a, self.b
  628. test = join_by('a', a, b, jointype='inner')
  629. control = np.array([(5, 55, 65, 105, 100), (6, 56, 66, 106, 101),
  630. (7, 57, 67, 107, 102), (8, 58, 68, 108, 103),
  631. (9, 59, 69, 109, 104)],
  632. dtype=[('a', int), ('b1', int), ('b2', int),
  633. ('c', int), ('d', int)])
  634. assert_equal(test, control)
  635. def test_join(self):
  636. a, b = self.a, self.b
  637. # Fixme, this test is broken
  638. #test = join_by(('a', 'b'), a, b)
  639. #control = np.array([(5, 55, 105, 100), (6, 56, 106, 101),
  640. # (7, 57, 107, 102), (8, 58, 108, 103),
  641. # (9, 59, 109, 104)],
  642. # dtype=[('a', int), ('b', int),
  643. # ('c', int), ('d', int)])
  644. #assert_equal(test, control)
  645. # Hack to avoid pyflakes unused variable warnings
  646. join_by(('a', 'b'), a, b)
  647. np.array([(5, 55, 105, 100), (6, 56, 106, 101),
  648. (7, 57, 107, 102), (8, 58, 108, 103),
  649. (9, 59, 109, 104)],
  650. dtype=[('a', int), ('b', int),
  651. ('c', int), ('d', int)])
  652. def test_join_subdtype(self):
  653. # tests the bug in https://stackoverflow.com/q/44769632/102441
  654. from numpy.lib import recfunctions as rfn
  655. foo = np.array([(1,)],
  656. dtype=[('key', int)])
  657. bar = np.array([(1, np.array([1,2,3]))],
  658. dtype=[('key', int), ('value', 'uint16', 3)])
  659. res = join_by('key', foo, bar)
  660. assert_equal(res, bar.view(ma.MaskedArray))
  661. def test_outer_join(self):
  662. a, b = self.a, self.b
  663. test = join_by(('a', 'b'), a, b, 'outer')
  664. control = ma.array([(0, 50, 100, -1), (1, 51, 101, -1),
  665. (2, 52, 102, -1), (3, 53, 103, -1),
  666. (4, 54, 104, -1), (5, 55, 105, -1),
  667. (5, 65, -1, 100), (6, 56, 106, -1),
  668. (6, 66, -1, 101), (7, 57, 107, -1),
  669. (7, 67, -1, 102), (8, 58, 108, -1),
  670. (8, 68, -1, 103), (9, 59, 109, -1),
  671. (9, 69, -1, 104), (10, 70, -1, 105),
  672. (11, 71, -1, 106), (12, 72, -1, 107),
  673. (13, 73, -1, 108), (14, 74, -1, 109)],
  674. mask=[(0, 0, 0, 1), (0, 0, 0, 1),
  675. (0, 0, 0, 1), (0, 0, 0, 1),
  676. (0, 0, 0, 1), (0, 0, 0, 1),
  677. (0, 0, 1, 0), (0, 0, 0, 1),
  678. (0, 0, 1, 0), (0, 0, 0, 1),
  679. (0, 0, 1, 0), (0, 0, 0, 1),
  680. (0, 0, 1, 0), (0, 0, 0, 1),
  681. (0, 0, 1, 0), (0, 0, 1, 0),
  682. (0, 0, 1, 0), (0, 0, 1, 0),
  683. (0, 0, 1, 0), (0, 0, 1, 0)],
  684. dtype=[('a', int), ('b', int),
  685. ('c', int), ('d', int)])
  686. assert_equal(test, control)
  687. def test_leftouter_join(self):
  688. a, b = self.a, self.b
  689. test = join_by(('a', 'b'), a, b, 'leftouter')
  690. control = ma.array([(0, 50, 100, -1), (1, 51, 101, -1),
  691. (2, 52, 102, -1), (3, 53, 103, -1),
  692. (4, 54, 104, -1), (5, 55, 105, -1),
  693. (6, 56, 106, -1), (7, 57, 107, -1),
  694. (8, 58, 108, -1), (9, 59, 109, -1)],
  695. mask=[(0, 0, 0, 1), (0, 0, 0, 1),
  696. (0, 0, 0, 1), (0, 0, 0, 1),
  697. (0, 0, 0, 1), (0, 0, 0, 1),
  698. (0, 0, 0, 1), (0, 0, 0, 1),
  699. (0, 0, 0, 1), (0, 0, 0, 1)],
  700. dtype=[('a', int), ('b', int), ('c', int), ('d', int)])
  701. assert_equal(test, control)
  702. def test_different_field_order(self):
  703. # gh-8940
  704. a = np.zeros(3, dtype=[('a', 'i4'), ('b', 'f4'), ('c', 'u1')])
  705. b = np.ones(3, dtype=[('c', 'u1'), ('b', 'f4'), ('a', 'i4')])
  706. # this should not give a FutureWarning:
  707. j = join_by(['c', 'b'], a, b, jointype='inner', usemask=False)
  708. assert_equal(j.dtype.names, ['b', 'c', 'a1', 'a2'])
  709. def test_duplicate_keys(self):
  710. a = np.zeros(3, dtype=[('a', 'i4'), ('b', 'f4'), ('c', 'u1')])
  711. b = np.ones(3, dtype=[('c', 'u1'), ('b', 'f4'), ('a', 'i4')])
  712. assert_raises(ValueError, join_by, ['a', 'b', 'b'], a, b)
  713. @pytest.mark.xfail(reason="See comment at gh-9343")
  714. def test_same_name_different_dtypes_key(self):
  715. a_dtype = np.dtype([('key', 'S5'), ('value', '<f4')])
  716. b_dtype = np.dtype([('key', 'S10'), ('value', '<f4')])
  717. expected_dtype = np.dtype([
  718. ('key', 'S10'), ('value1', '<f4'), ('value2', '<f4')])
  719. a = np.array([('Sarah', 8.0), ('John', 6.0)], dtype=a_dtype)
  720. b = np.array([('Sarah', 10.0), ('John', 7.0)], dtype=b_dtype)
  721. res = join_by('key', a, b)
  722. assert_equal(res.dtype, expected_dtype)
  723. def test_same_name_different_dtypes(self):
  724. # gh-9338
  725. a_dtype = np.dtype([('key', 'S10'), ('value', '<f4')])
  726. b_dtype = np.dtype([('key', 'S10'), ('value', '<f8')])
  727. expected_dtype = np.dtype([
  728. ('key', '|S10'), ('value1', '<f4'), ('value2', '<f8')])
  729. a = np.array([('Sarah', 8.0), ('John', 6.0)], dtype=a_dtype)
  730. b = np.array([('Sarah', 10.0), ('John', 7.0)], dtype=b_dtype)
  731. res = join_by('key', a, b)
  732. assert_equal(res.dtype, expected_dtype)
  733. def test_subarray_key(self):
  734. a_dtype = np.dtype([('pos', int, 3), ('f', '<f4')])
  735. a = np.array([([1, 1, 1], np.pi), ([1, 2, 3], 0.0)], dtype=a_dtype)
  736. b_dtype = np.dtype([('pos', int, 3), ('g', '<f4')])
  737. b = np.array([([1, 1, 1], 3), ([3, 2, 1], 0.0)], dtype=b_dtype)
  738. expected_dtype = np.dtype([('pos', int, 3), ('f', '<f4'), ('g', '<f4')])
  739. expected = np.array([([1, 1, 1], np.pi, 3)], dtype=expected_dtype)
  740. res = join_by('pos', a, b)
  741. assert_equal(res.dtype, expected_dtype)
  742. assert_equal(res, expected)
  743. def test_padded_dtype(self):
  744. dt = np.dtype('i1,f4', align=True)
  745. dt.names = ('k', 'v')
  746. assert_(len(dt.descr), 3) # padding field is inserted
  747. a = np.array([(1, 3), (3, 2)], dt)
  748. b = np.array([(1, 1), (2, 2)], dt)
  749. res = join_by('k', a, b)
  750. # no padding fields remain
  751. expected_dtype = np.dtype([
  752. ('k', 'i1'), ('v1', 'f4'), ('v2', 'f4')
  753. ])
  754. assert_equal(res.dtype, expected_dtype)
  755. class TestJoinBy2(object):
  756. @classmethod
  757. def setup(cls):
  758. cls.a = np.array(list(zip(np.arange(10), np.arange(50, 60),
  759. np.arange(100, 110))),
  760. dtype=[('a', int), ('b', int), ('c', int)])
  761. cls.b = np.array(list(zip(np.arange(10), np.arange(65, 75),
  762. np.arange(100, 110))),
  763. dtype=[('a', int), ('b', int), ('d', int)])
  764. def test_no_r1postfix(self):
  765. # Basic test of join_by no_r1postfix
  766. a, b = self.a, self.b
  767. test = join_by(
  768. 'a', a, b, r1postfix='', r2postfix='2', jointype='inner')
  769. control = np.array([(0, 50, 65, 100, 100), (1, 51, 66, 101, 101),
  770. (2, 52, 67, 102, 102), (3, 53, 68, 103, 103),
  771. (4, 54, 69, 104, 104), (5, 55, 70, 105, 105),
  772. (6, 56, 71, 106, 106), (7, 57, 72, 107, 107),
  773. (8, 58, 73, 108, 108), (9, 59, 74, 109, 109)],
  774. dtype=[('a', int), ('b', int), ('b2', int),
  775. ('c', int), ('d', int)])
  776. assert_equal(test, control)
  777. def test_no_postfix(self):
  778. assert_raises(ValueError, join_by, 'a', self.a, self.b,
  779. r1postfix='', r2postfix='')
  780. def test_no_r2postfix(self):
  781. # Basic test of join_by no_r2postfix
  782. a, b = self.a, self.b
  783. test = join_by(
  784. 'a', a, b, r1postfix='1', r2postfix='', jointype='inner')
  785. control = np.array([(0, 50, 65, 100, 100), (1, 51, 66, 101, 101),
  786. (2, 52, 67, 102, 102), (3, 53, 68, 103, 103),
  787. (4, 54, 69, 104, 104), (5, 55, 70, 105, 105),
  788. (6, 56, 71, 106, 106), (7, 57, 72, 107, 107),
  789. (8, 58, 73, 108, 108), (9, 59, 74, 109, 109)],
  790. dtype=[('a', int), ('b1', int), ('b', int),
  791. ('c', int), ('d', int)])
  792. assert_equal(test, control)
  793. def test_two_keys_two_vars(self):
  794. a = np.array(list(zip(np.tile([10, 11], 5), np.repeat(np.arange(5), 2),
  795. np.arange(50, 60), np.arange(10, 20))),
  796. dtype=[('k', int), ('a', int), ('b', int), ('c', int)])
  797. b = np.array(list(zip(np.tile([10, 11], 5), np.repeat(np.arange(5), 2),
  798. np.arange(65, 75), np.arange(0, 10))),
  799. dtype=[('k', int), ('a', int), ('b', int), ('c', int)])
  800. control = np.array([(10, 0, 50, 65, 10, 0), (11, 0, 51, 66, 11, 1),
  801. (10, 1, 52, 67, 12, 2), (11, 1, 53, 68, 13, 3),
  802. (10, 2, 54, 69, 14, 4), (11, 2, 55, 70, 15, 5),
  803. (10, 3, 56, 71, 16, 6), (11, 3, 57, 72, 17, 7),
  804. (10, 4, 58, 73, 18, 8), (11, 4, 59, 74, 19, 9)],
  805. dtype=[('k', int), ('a', int), ('b1', int),
  806. ('b2', int), ('c1', int), ('c2', int)])
  807. test = join_by(
  808. ['a', 'k'], a, b, r1postfix='1', r2postfix='2', jointype='inner')
  809. assert_equal(test.dtype, control.dtype)
  810. assert_equal(test, control)
  811. class TestAppendFieldsObj(object):
  812. """
  813. Test append_fields with arrays containing objects
  814. """
  815. # https://github.com/numpy/numpy/issues/2346
  816. def setup(self):
  817. from datetime import date
  818. self.data = dict(obj=date(2000, 1, 1))
  819. def test_append_to_objects(self):
  820. "Test append_fields when the base array contains objects"
  821. obj = self.data['obj']
  822. x = np.array([(obj, 1.), (obj, 2.)],
  823. dtype=[('A', object), ('B', float)])
  824. y = np.array([10, 20], dtype=int)
  825. test = append_fields(x, 'C', data=y, usemask=False)
  826. control = np.array([(obj, 1.0, 10), (obj, 2.0, 20)],
  827. dtype=[('A', object), ('B', float), ('C', int)])
  828. assert_equal(test, control)