selections2.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. Implements a portion of the selection operations.
  11. """
  12. import numpy as np
  13. from .. import h5s
  14. def read_dtypes(dataset_dtype, names):
  15. """ Returns a 2-tuple containing:
  16. 1. Output dataset dtype
  17. 2. Dtype containing HDF5-appropriate description of destination
  18. """
  19. if len(names) == 0: # Not compound, or all fields needed
  20. format_dtype = dataset_dtype
  21. elif dataset_dtype.names is None:
  22. raise ValueError("Field names only allowed for compound types")
  23. elif any(x not in dataset_dtype.names for x in names):
  24. raise ValueError("Field does not appear in this type.")
  25. else:
  26. format_dtype = np.dtype([(name, dataset_dtype.fields[name][0]) for name in names])
  27. if len(names) == 1:
  28. # We don't preserve the field information if only one explicitly selected.
  29. output_dtype = format_dtype.fields[names[0]][0]
  30. else:
  31. output_dtype = format_dtype
  32. return output_dtype, format_dtype
  33. def read_selections_scalar(dsid, args):
  34. """ Returns a 2-tuple containing:
  35. 1. Output dataset shape
  36. 2. HDF5 dataspace containing source selection.
  37. Works for scalar datasets.
  38. """
  39. if dsid.shape != ():
  40. raise RuntimeError("Illegal selection function for non-scalar dataset")
  41. if args == ():
  42. # This is a signal that an array scalar should be returned instead
  43. # of an ndarray with shape ()
  44. out_shape = None
  45. elif args == (Ellipsis,):
  46. out_shape = ()
  47. else:
  48. raise ValueError("Illegal slicing argument for scalar dataspace")
  49. source_space = dsid.get_space()
  50. source_space.select_all()
  51. return out_shape, source_space
  52. class ScalarReadSelection(object):
  53. """
  54. Implements slicing for scalar datasets.
  55. """
  56. def __init__(self, fspace, args):
  57. if args == ():
  58. self.mshape = None
  59. elif args == (Ellipsis,):
  60. self.mshape = ()
  61. else:
  62. raise ValueError("Illegal slicing argument for scalar dataspace")
  63. self.mspace = h5s.create(h5s.SCALAR)
  64. self.fspace = fspace
  65. def __iter__(self):
  66. self.mspace.select_all()
  67. yield self.fspace, self.mspace
  68. def select_read(fspace, args):
  69. """ Top-level dispatch function for reading.
  70. At the moment, only supports reading from scalar datasets.
  71. """
  72. if fspace.shape == ():
  73. return ScalarReadSelection(fspace, args)
  74. raise NotImplementedError()