def read(self, start=None, stop=None, step=1): """Get data in the array as a list of objects of the current flavor. Please note that, as the lengths of the different rows are variable, the returned value is a *Python list* (not an array of the current flavor), with as many entries as specified rows in the range parameters. The start, stop and step parameters can be used to select only a *range of rows* in the array. Their meanings are the same as in the built-in range() Python function, except that negative values of step are not allowed yet. Moreover, if only start is specified, then stop will be set to start+1. If you do not specify neither start nor stop, then *all the rows* in the array are selected. """ start, stop, step = self._processRangeRead(start, stop, step) if start == stop: listarr = [] else: listarr = self._readArray(start, stop, step) atom = self.atom if not hasattr(atom, 'size'): # it is a pseudo-atom outlistarr = [atom.fromarray(arr) for arr in listarr] else: # Convert the list to the right flavor flavor = self.flavor outlistarr = [internal_to_flavor(arr, flavor) for arr in listarr] return outlistarr
def read(self, start=None, stop=None, step=1): """Get data in the array as a list of objects of the current flavor. Please note that, as the lengths of the different rows are variable, the returned value is a *Python list* (not an array of the current flavor), with as many entries as specified rows in the range parameters. The start, stop and step parameters can be used to select only a *range of rows* in the array. Their meanings are the same as in the built-in range() Python function, except that negative values of step are not allowed yet. Moreover, if only start is specified, then stop will be set to start + 1. If you do not specify neither start nor stop, then *all the rows* in the array are selected. """ self._g_check_open() start, stop, step = self._process_range_read(start, stop, step) if start == stop: listarr = [] else: listarr = self._read_array(start, stop, step) atom = self.atom if not hasattr(atom, 'size'): # it is a pseudo-atom outlistarr = [atom.fromarray(arr) for arr in listarr] else: # Convert the list to the right flavor flavor = self.flavor outlistarr = [internal_to_flavor(arr, flavor) for arr in listarr] return outlistarr
def next(self): """ Get the next element of the array during an iteration. The element is returned as an object of the current flavor. """ if self._nrowsread >= self._stop: self._init = False raise StopIteration # end of iteration else: # Read a chunk of rows if self._row+1 >= self.nrowsinbuf or self._row < 0: self._stopb = self._startb+self._step*self.nrowsinbuf # Protection for reading more elements than needed if self._stopb > self._stop: self._stopb = self._stop listarr = self._read(self._startb, self._stopb, self._step) # Swap the axes to easy the return of elements if self.extdim > 0: listarr = listarr.swapaxes(self.extdim, 0) self.listarr = internal_to_flavor(listarr, self.flavor) self._row = -1 self._startb = self._stopb self._row += 1 self.nrow += self._step self._nrowsread += self._step # Fixes bug #968132 #if self.listarr.shape: if self.shape: return self.listarr[self._row] else: return self.listarr # Scalar case
def read(self, start=None, stop=None, step=None): """Get data in the array as an object of the current flavor. The start, stop and step parameters can be used to select only a *range of rows* in the array. Their meanings are the same as in the built-in range() Python function, except that negative values of step are not allowed yet. Moreover, if only start is specified, then stop will be set to start+1. If you do not specify neither start nor stop, then *all the rows* in the array are selected. """ (start, stop, step) = self._processRangeRead(start, stop, step) arr = self._read(start, stop, step) return internal_to_flavor(arr, self.flavor)
def __getitem__(self, key): """Get a row, a range of rows or a slice from the array. The set of tokens allowed for the key is the same as that for extended slicing in Python (including the Ellipsis or ... token). The result is an object of the current flavor; its shape depends on the kind of slice used as key and the shape of the array itself. Furthermore, NumPy-style fancy indexing, where a list of indices in a certain axis is specified, is also supported. Note that only one list per selection is supported right now. Finally, NumPy-style point and boolean selections are supported as well. Examples -------- :: array1 = array[4] # simple selection array2 = array[4:1000:2] # slice selection array3 = array[1, ..., ::2, 1:4, 4:] # general slice selection array4 = array[1, [1,5,10], ..., -1] # fancy selection array5 = array[np.where(array[:] > 4)] # point selection array6 = array[array[:] > 4] # boolean selection """ self._g_check_open() try: # First, try with a regular selection startl, stopl, stepl, shape = self._interpret_indexing(key) arr = self._read_slice(startl, stopl, stepl, shape) except TypeError: # Then, try with a point-wise selection try: coords = self._point_selection(key) arr = self._read_coords(coords) except TypeError: # Finally, try with a fancy selection selection, reorder, shape = self._fancy_selection(key) arr = self._read_selection(selection, reorder, shape) if self.flavor == "numpy" or not self._v_convert: return arr return internal_to_flavor(arr, self.flavor)
def __getitem__(self, key): """ Get a row, a range of rows or a slice from the array. The set of tokens allowed for the `key` is the same as that for extended slicing in Python (including the ``Ellipsis`` or ``...`` token). The result is an object of the current flavor; its shape depends on the kind of slice used as `key` and the shape of the array itself. Example of use:: array1 = array[4] # array1.shape == array.shape[1:] array2 = array[4:1000:2] # len(array2.shape) == len(array.shape) array3 = array[::2, 1:4, :] array4 = array[1, ..., ::2, 1:4, 4:] # general slice selection """ startl, stopl, stepl, shape = self._interpret_indexing(key) arr = self._readSlice(startl, stopl, stepl, shape) if not self._v_convert: return arr return internal_to_flavor(arr, self.flavor)
def read(self, start=None, stop=None, step=None, out=None): """Get data in the array as an object of the current flavor. The start, stop and step parameters can be used to select only a *range of rows* in the array. Their meanings are the same as in the built-in range() Python function, except that negative values of step are not allowed yet. Moreover, if only start is specified, then stop will be set to start + 1. If you do not specify neither start nor stop, then *all the rows* in the array are selected. The out parameter may be used to specify a NumPy array to receive the output data. Note that the array must have the same size as the data selected with the other parameters. Note that the array's datatype is not checked and no type casting is performed, so if it does not match the datatype on disk, the output will not be correct. Also, this parameter is only valid when the array's flavor is set to 'numpy'. Otherwise, a TypeError will be raised. When data is read from disk in NumPy format, the output will be in the current system's byteorder, regardless of how it is stored on disk. The exception is when an output buffer is supplied, in which case the output will be in the byteorder of that output buffer. .. versionchanged:: 3.0 Added the *out* parameter. """ self._g_check_open() if out is not None and self.flavor != "numpy": msg = ( "Optional 'out' argument may only be supplied if array " "flavor is 'numpy', currently is {0}" ).format(self.flavor) raise TypeError(msg) (start, stop, step) = self._process_range_read(start, stop, step) arr = self._read(start, stop, step, out) return internal_to_flavor(arr, self.flavor)
def read(self, start=None, stop=None, step=None, out=None): """Get data in the array as an object of the current flavor. The start, stop and step parameters can be used to select only a *range of rows* in the array. Their meanings are the same as in the built-in range() Python function, except that negative values of step are not allowed yet. Moreover, if only start is specified, then stop will be set to start + 1. If you do not specify neither start nor stop, then *all the rows* in the array are selected. The out parameter may be used to specify a NumPy array to receive the output data. Note that the array must have the same size as the data selected with the other parameters. Note that the array's datatype is not checked and no type casting is performed, so if it does not match the datatype on disk, the output will not be correct. Also, this parameter is only valid when the array's flavor is set to 'numpy'. Otherwise, a TypeError will be raised. When data is read from disk in NumPy format, the output will be in the current system's byteorder, regardless of how it is stored on disk. The exception is when an output buffer is supplied, in which case the output will be in the byteorder of that output buffer. .. versionchanged:: 3.0 Added the *out* parameter. """ self._g_check_open() if out is not None and self.flavor != 'numpy': msg = ("Optional 'out' argument may only be supplied if array " "flavor is 'numpy', currently is {0}").format(self.flavor) raise TypeError(msg) (start, stop, step) = self._process_range_read(start, stop, step) arr = self._read(start, stop, step, out) return internal_to_flavor(arr, self.flavor)