Example #1
0
def fromnumpy(array):
    """
    Create a new instance of a `RecArray` from NumPy `array`.

    If nested records are present, a `NestedRecArray` is returned.  The
    input `array` must be a NumPy array or record (it is not checked).
    """
    # Convert the original description based in the array protocol in
    # something that can be understood by the NestedRecArray
    # constructor.
    descr = [i for i in convertFromAPDescr(array.dtype.descr)]
    # Flat the description
    flatDescr = [i for i in nriterators.flattenDescr(descr)]
    # Flat the structure descriptors
    flatFormats = [i for i in nriterators.getFormatsFromDescr(flatDescr)]
    flatNames = [i for i in nriterators.getNamesFromDescr(flatDescr)]
    # Create a regular RecArray
    if array.shape == ():
        shape = 1  # Scalar case. Shape = 1 will provide an adequate buffer.
    else:
        shape = array.shape
    rarray = numarray.records.array(
        array.data, formats=flatFormats, names=flatNames, shape=shape, byteorder=sys.byteorder, aligned=False
    )  # aligned RecArrays are not supported yet

    # A ``NestedRecArray`` is only needed if there are nested fields.
    # This check has been disabled because a NestedRecArray does offer
    # more features that the users of PyTables 1.x are used to, most
    # specially, the __getitem__(fieldname) special method.
    #     if '/' in ''.join(flatNames):
    #         return NestedRecArray(rarray, descr)
    #     return rarray
    return NestedRecArray(rarray, descr)
Example #2
0
    def testNamesFromDescr(self):
        """Retrieves the names list from the descr list.
        """

        # Check getNamesFromDescr function
        common.verbosePrint('\nTesting getNamesFromDescr function')
        new_names = \
            [item for item in nriterators.getNamesFromDescr(self.descr)]
        self.assertEqual(self.names, new_names)
Example #3
0
    def testNamesFromDescr(self):
        """Retrieves the names list from the descr list.
        """

        # Check getNamesFromDescr function
        common.verbosePrint('\nTesting getNamesFromDescr function')
        new_names = \
            [item for item in nriterators.getNamesFromDescr(self.descr)]
        self.assertEqual(self.names, new_names)
Example #4
0
def _checkFieldsInDescr(descr):
    """
    Check that field names do not contain the ``/`` character.

    The checking is done on the most deeply nested field names.  For an
    explanation of argument meanings see the `array()` function.
    """

    names = [item for item in nriterators.getNamesFromDescr(descr)]
    _checkNames(names)
Example #5
0
def _checkFieldsInDescr(descr):
    """
    Check that field names do not contain the ``/`` character.

    The checking is done on the most deeply nested field names.  For an
    explanation of argument meanings see the `array()` function.
    """

    names = [item for item in nriterators.getNamesFromDescr(descr)]
    _checkNames(names)
Example #6
0
    def field(self, fieldName):
        """
        Get field data as an array.

        `fieldName` can be the name or the index of a field in the
        record array.  If it is not nested, a ``NumArray`` or
        ``CharArray`` object representing the values in that field is
        returned.  Else, a `NestedRecArray` object is returned.

        `fieldName` can be used to provide the name of sub-fields.  In
        that case, it will consist of several field name components
        separated by the string ``'/'``.  For instance, if there is a
        nested field named ``x`` with a sub-field named ``y``, the last
        one can be accesed by using ``'x/y'`` as the value of
        `fieldName`.
        """

        # fieldName can be an integer, get the corresponding name
        if isinstance(fieldName, int):
            fieldName = self.descr[fieldName][0]

        # The descr list of the field whose content is being extracted
        fieldDescr = [
            item for item in nriterators.getFieldDescr(fieldName, self.descr)
        ]
        if fieldDescr == []:
            raise ValueError("there is no field named ``%s``" % (fieldName, ))
        fieldDescr = fieldDescr[0][1]

        # Case 1) non nested fields (bottom level)
        if isinstance(fieldDescr, str):
            # The field content is returned as numarray or chararray
            return self._flatArray.field(fieldName)

        # Case 2) nested fields (both top and intermediate levels)
        # We need fully qualified names to access the flat array fields
        fieldNames = [
            name for name in nriterators.getNamesFromDescr(fieldDescr)
        ]
        flatNames = [name for name in nriterators.flattenNames(fieldNames)]

        # This is the flattened name of the original first bottom field.
        startField = '%s/%s' % (fieldName, flatNames[0])
        # Get the requested fields from the flat array and build a nested one.
        newFlatArray = _narrowRecArray(self._flatArray, startField, flatNames)
        return NestedRecArray(newFlatArray, fieldDescr)
Example #7
0
    def field(self, fieldName):
        """
        Get field data as an array.

        `fieldName` can be the name or the index of a field in the
        record array.  If it is not nested, a ``NumArray`` or
        ``CharArray`` object representing the values in that field is
        returned.  Else, a `NestedRecArray` object is returned.

        `fieldName` can be used to provide the name of sub-fields.  In
        that case, it will consist of several field name components
        separated by the string ``'/'``.  For instance, if there is a
        nested field named ``x`` with a sub-field named ``y``, the last
        one can be accesed by using ``'x/y'`` as the value of
        `fieldName`.
        """

        # fieldName can be an integer, get the corresponding name
        if isinstance(fieldName, int):
            fieldName = self.descr[fieldName][0]

        # The descr list of the field whose content is being extracted
        fieldDescr = [
            item for item in nriterators.getFieldDescr(fieldName, self.descr)]
        if fieldDescr == []:
            raise ValueError("there is no field named ``%s``" % (fieldName,))
        fieldDescr = fieldDescr[0][1]

        # Case 1) non nested fields (bottom level)
        if isinstance(fieldDescr, str):
            # The field content is returned as numarray or chararray
            return self._flatArray.field(fieldName)

        # Case 2) nested fields (both top and intermediate levels)
        # We need fully qualified names to access the flat array fields
        fieldNames = [
            name for name in nriterators.getNamesFromDescr(fieldDescr)]
        flatNames = [
            name for name in nriterators.flattenNames(fieldNames)]

        # This is the flattened name of the original first bottom field.
        startField = '%s/%s' % (fieldName, flatNames[0])
        # Get the requested fields from the flat array and build a nested one.
        newFlatArray = _narrowRecArray(self._flatArray, startField, flatNames)
        return NestedRecArray(newFlatArray, fieldDescr)
Example #8
0
def fromnumpy(array):
    """
    Create a new instance of a `RecArray` from NumPy `array`.

    If nested records are present, a `NestedRecArray` is returned.  The
    input `array` must be a NumPy array or record (it is not checked).
    """
    # Convert the original description based in the array protocol in
    # something that can be understood by the NestedRecArray
    # constructor.
    descr = [i for i in convertFromAPDescr(array.dtype.descr)]
    # Flat the description
    flatDescr = [i for i in nriterators.flattenDescr(descr)]
    # Flat the structure descriptors
    flatFormats = [i for i in nriterators.getFormatsFromDescr(flatDescr)]
    flatNames = [i for i in nriterators.getNamesFromDescr(flatDescr)]
    # Create a regular RecArray
    if array.shape == ():
        shape = 1  # Scalar case. Shape = 1 will provide an adequate buffer.
    else:
        shape = array.shape
    rarray = numarray.records.array(
        array.data,
        formats=flatFormats,
        names=flatNames,
        shape=shape,
        byteorder=sys.byteorder,
        aligned=False)  # aligned RecArrays are not supported yet

    # A ``NestedRecArray`` is only needed if there are nested fields.
    # This check has been disabled because a NestedRecArray does offer
    # more features that the users of PyTables 1.x are used to, most
    # specially, the __getitem__(fieldname) special method.
    #     if '/' in ''.join(flatNames):
    #         return NestedRecArray(rarray, descr)
    #     return rarray
    return NestedRecArray(rarray, descr)
Example #9
0
def makeNames(descr):
    """Create a ``names`` list for the array."""

    return [item for item in nriterators.getNamesFromDescr(descr)]
Example #10
0
def makeNames(descr):
    """Create a ``names`` list for the array."""

    return [item for item in nriterators.getNamesFromDescr(descr)]