Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/h5py/_hl/selections2.py: 21%
42 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-05 06:32 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-05 06:32 +0000
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.
10"""
11 Implements a portion of the selection operations.
12"""
14import numpy as np
15from .. import h5s
17def read_dtypes(dataset_dtype, names):
18 """ Returns a 2-tuple containing:
20 1. Output dataset dtype
21 2. Dtype containing HDF5-appropriate description of destination
22 """
24 if len(names) == 0: # Not compound, or all fields needed
25 format_dtype = dataset_dtype
27 elif dataset_dtype.names is None:
28 raise ValueError("Field names only allowed for compound types")
30 elif any(x not in dataset_dtype.names for x in names):
31 raise ValueError("Field does not appear in this type.")
33 else:
34 format_dtype = np.dtype([(name, dataset_dtype.fields[name][0]) for name in names])
36 if len(names) == 1:
37 # We don't preserve the field information if only one explicitly selected.
38 output_dtype = format_dtype.fields[names[0]][0]
40 else:
41 output_dtype = format_dtype
43 return output_dtype, format_dtype
46def read_selections_scalar(dsid, args):
47 """ Returns a 2-tuple containing:
49 1. Output dataset shape
50 2. HDF5 dataspace containing source selection.
52 Works for scalar datasets.
53 """
55 if dsid.shape != ():
56 raise RuntimeError("Illegal selection function for non-scalar dataset")
58 if args == ():
59 # This is a signal that an array scalar should be returned instead
60 # of an ndarray with shape ()
61 out_shape = None
63 elif args == (Ellipsis,):
64 out_shape = ()
66 else:
67 raise ValueError("Illegal slicing argument for scalar dataspace")
69 source_space = dsid.get_space()
70 source_space.select_all()
72 return out_shape, source_space
74class ScalarReadSelection:
76 """
77 Implements slicing for scalar datasets.
78 """
80 def __init__(self, fspace, args):
81 if args == ():
82 self.mshape = None
83 elif args == (Ellipsis,):
84 self.mshape = ()
85 else:
86 raise ValueError("Illegal slicing argument for scalar dataspace")
88 self.mspace = h5s.create(h5s.SCALAR)
89 self.fspace = fspace
91 def __iter__(self):
92 self.mspace.select_all()
93 yield self.fspace, self.mspace
95def select_read(fspace, args):
96 """ Top-level dispatch function for reading.
98 At the moment, only supports reading from scalar datasets.
99 """
100 if fspace.shape == ():
101 return ScalarReadSelection(fspace, args)
103 raise NotImplementedError()