test_selections.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # This file is part of h5py, a Python interface to the HDF5 library.
  2. #
  3. # http://www.h5py.org
  4. #
  5. # Copyright 2008-2013 Andrew Collette and contributors
  6. #
  7. # License: Standard 3-clause BSD; see "license.txt" for full license terms
  8. # and contributor agreement.
  9. """
  10. Tests for the (internal) selections module
  11. """
  12. import numpy as np
  13. import h5py
  14. import h5py._hl.selections as sel
  15. import h5py._hl.selections2 as sel2
  16. from .common import TestCase, ut
  17. class BaseSelection(TestCase):
  18. def setUp(self):
  19. self.f = h5py.File(self.mktemp(), 'w')
  20. self.dsid = self.f.create_dataset('x', ()).id
  21. def tearDown(self):
  22. if self.f:
  23. self.f.close()
  24. class TestTypeGeneration(BaseSelection):
  25. """
  26. Internal feature: Determine output types from dataset dtype and fields.
  27. """
  28. def test_simple(self):
  29. """ Non-compound types are handled appropriately """
  30. dt = np.dtype('i')
  31. out, format = sel2.read_dtypes(dt, ())
  32. self.assertEqual(out, format)
  33. self.assertEqual(out, np.dtype('i'))
  34. def test_simple_fieldexc(self):
  35. """ Field names for non-field types raises ValueError """
  36. dt = np.dtype('i')
  37. with self.assertRaises(ValueError):
  38. out, format = sel2.read_dtypes(dt, ('a',))
  39. def test_compound_simple(self):
  40. """ Compound types with elemental subtypes """
  41. dt = np.dtype( [('a','i'), ('b','f'), ('c','|S10')] )
  42. # Implicit selection of all fields -> all fields
  43. out, format = sel2.read_dtypes(dt, ())
  44. self.assertEqual(out, format)
  45. self.assertEqual(out, dt)
  46. # Explicit selection of fields -> requested fields
  47. out, format = sel2.read_dtypes(dt, ('a','b'))
  48. self.assertEqual(out, format)
  49. self.assertEqual(out, np.dtype( [('a','i'), ('b','f')] ))
  50. # Explicit selection of exactly one field -> no fields
  51. out, format = sel2.read_dtypes(dt, ('a',))
  52. self.assertEqual(out, np.dtype('i'))
  53. self.assertEqual(format, np.dtype( [('a','i')] ))
  54. # Field does not apear in named typed
  55. with self.assertRaises(ValueError):
  56. out, format = sel2.read_dtypes(dt, ('j', 'k'))
  57. class TestScalarSliceRules(BaseSelection):
  58. """
  59. Internal feature: selections rules for scalar datasets
  60. """
  61. def test_args(self):
  62. """ Permissible arguments for scalar slicing """
  63. shape, selection = sel2.read_selections_scalar(self.dsid, ())
  64. self.assertEqual(shape, None)
  65. self.assertEqual(selection.get_select_npoints(), 1)
  66. shape, selection = sel2.read_selections_scalar(self.dsid, (Ellipsis,))
  67. self.assertEqual(shape, ())
  68. self.assertEqual(selection.get_select_npoints(), 1)
  69. with self.assertRaises(ValueError):
  70. shape, selection = sel2.read_selections_scalar(self.dsid, (1,))
  71. dsid = self.f.create_dataset('y', (1,)).id
  72. with self.assertRaises(RuntimeError):
  73. shape, selection = sel2.read_selections_scalar(dsid, (1,))
  74. class TestSelection(BaseSelection):
  75. """ High-level routes to generate a selection
  76. """
  77. def test_selection(self):
  78. dset = self.f.create_dataset('dset', (100,100))
  79. regref = dset.regionref[0:100, 0:100]
  80. # args is list, return a FancySelection
  81. st = sel.select((10,), list([1,2,3]), dset)
  82. self.assertIsInstance(st, sel.FancySelection)
  83. # args is a Boolean mask, return a PointSelection
  84. st1 = sel.select((5,), np.array([True,False,False,False,True]), dset)
  85. self.assertIsInstance(st1, sel.PointSelection)
  86. # args is int, return a SimpleSelection
  87. st2 = sel.select((10,), 1, dset)
  88. self.assertIsInstance(st2, sel.SimpleSelection)
  89. # args is RegionReference, return a Selection instance
  90. st3 = sel.select((100,100), regref, dset)
  91. self.assertIsInstance(st3, sel.Selection)
  92. # args is RegionReference, but dataset is None
  93. with self.assertRaises(TypeError):
  94. sel.select((100,), regref, None)
  95. # args is RegionReference, but its shape doesn't match dataset shape
  96. with self.assertRaises(TypeError):
  97. sel.select((100,), regref, dset)
  98. # args is a single Selection instance, return the arg
  99. st4 = sel.select((100,100), st3, dset)
  100. self.assertEqual(st4,st3)
  101. # args is a single Selection instance, but args shape doesn't match Shape
  102. with self.assertRaises(TypeError):
  103. sel.select((100,), st3, dset)