示例#1
0
    def evaluate_transform(self, x, no_copy=False):
        r"""Call transform on the provided message.

        Args:
            x (object): Message object to transform.
            no_copy (bool, optional): If True, the transformation occurs in
                place. Otherwise a copy is created and transformed. Defaults
                to False.

        Returns:
            object: The transformed message.

        """
        out = x
        np_dtype = type2numpy(self.transformed_datatype)
        if isinstance(x, pandas.DataFrame):
            out = pandas2numpy(x).astype(np_dtype, copy=True)
        elif isinstance(x, np.ndarray):
            out = x.astype(np_dtype, copy=True)
        elif np_dtype and isinstance(x, (list, tuple, dict, np.ndarray)):
            if len(x) == 0:
                out = np.zeros(0, np_dtype)
            else:
                if isinstance(x, dict):
                    x = dict2list(x, order=np_dtype.names)
                out = consolidate_array(x, dtype=np_dtype)
        else:
            # warning?
            raise TypeError(("Cannot consolidate object of type %s "
                             "into a structured numpy array.") % type(x))
        if not no_copy:
            out = copy.deepcopy(out)
        return out
    def consolidate_array(self, out):
        r"""Consolidate message into a structure numpy array if possible.

        Args:
            out (list, tuple, np.ndarray): Object to consolidate into a
                structured numpy array.

        Returns:
            np.ndarray: Structured numpy array containing consolidated message.

        Raises:
            ValueError: If the array cannot be consolidated.

        """
        np_dtype = self.numpy_dtype
        if np_dtype and isinstance(out, (list, tuple, np.ndarray)):
            out = serialize.consolidate_array(out, dtype=np_dtype)
        else:
            warnings.warn(("Cannot consolidate message into a structured " +
                           "numpy array: %s") % str(out))
        return out
示例#3
0
def test_consolidate_array():
    r"""Test consolidation of array information in different forms."""
    names0 = ['f0', 'f1', 'f2', 'f3']
    # names1 = ["name", "number", "value", "complex"]
    dtypes = ['S5', 'i8', 'f8', 'c16']
    dtype0 = np.dtype([(n, f) for n, f in zip(names0, dtypes)])
    if platform._is_win:  # pragma: windows
        if backwards.PY2:  # pragma: Python 2
            dtype0_list = dtype0
        else:  # pragma: Python 3
            dtype0_list = np.dtype({
                'names': names0,
                'formats': ['S5', 'i4', 'f8', 'c16']
            })
    else:
        dtype0_list = dtype0
    # dtype1 = np.dtype([(n, f) for n, f in zip(names1, dtypes)])
    dtype2 = np.dtype([('f0', 'i8'), ('f1', 'f8')])
    dtype3 = np.dtype([('f0', 'i4'), ('f1', 'f4')])
    shape = (5, 5)
    # Create list of input variables
    dlist = []
    x0list = []
    x1list = []
    for dtype in [np.dtype('float'), dtype0]:
        x = np.zeros(shape, dtype=dtype)
        if dtype == dtype0:
            x['f0'][:] = 'hello'
        x_flat = x.flatten()
        dlist.append(dtype)
        x0list.append(x)
        x1list.append(x)
        if len(dtype) > 0:
            dlist += [dtype, dtype]
            if dtype == dtype0:
                dtype_list = dtype0_list
            else:  # pragma: debug
                dtype_list = dtype
            dlist.append(dtype_list)
            x0list += [x, x_flat, x_flat.astype(dtype_list)]
            # Tests with lists of arrays rows or columns
            x1list.append([x[n] for n in dtype.names])
            x1list.append([x_flat[i] for i in range(len(x_flat))])
            x1list.append([x_flat[i].tolist() for i in range(len(x_flat))])
    # Tests with single array mapped onto structured array
    nrow = 4
    x0 = np.zeros(nrow, dtype2)
    x1 = np.zeros((nrow, len(dtype2)))
    dlist += [None]
    x0list += [x0]
    x1list.append(x0)
    x2 = serialize.consolidate_array(x1, dtype=dtype2)
    np.testing.assert_array_equal(x0, x2)
    np.testing.assert_array_equal(
        x0, serialize.consolidate_array(np.zeros(nrow, dtype3), dtype=dtype2))
    # Test with list of arrays
    arr_void = np.ones(5, dtype0)
    x0list.append(arr_void)
    x1list.append([a for a in arr_void])
    dlist.append(dtype0)
    # Loop over different test inputs
    i = 0
    for x0, x1, dtype in zip(x0list, x1list, dlist):
        i += 1
        # Dtype provided
        x2 = serialize.consolidate_array(x1, dtype=dtype)
        np.testing.assert_array_equal(x0, x2)
        # Dtype not provided
        x2 = serialize.consolidate_array(x1)
        np.testing.assert_array_equal(x0, x2)
    # Error on incorrect data format
    assert_raises(ValueError,
                  serialize.consolidate_array,
                  np.zeros((4, 1)),
                  dtype=dtype2)
    # Error on incorrect type
    assert_raises(TypeError, serialize.consolidate_array, None)
    # Error on dtypes with differing numbers of fields
    assert_raises(ValueError,
                  serialize.consolidate_array,
                  np.zeros(3, dtype0),
                  dtype=dtype3)