Example #1
0
def norm_iv(data: gen.TensArr, keep_freq_axis=False,
            parts: Union[str, List[str], Tuple[str]]='all') -> gen.TensArr:
    dim = gen.ndim(data)
    if dim != 3 and dim != 4:
        raise f'Dimension Mismatch: {dim}'

    DICT_IDX = {'I': range(0, 3),
                'a': range(3, 4),
                'all': range(0, 4),
                }

    if type(parts) == str:
        parts = [parts]

    result = []
    for part in parts:
        if part in DICT_IDX.keys():
            squared = data[..., DICT_IDX[part]]**2
            if dim == 3:
                axis = (0, 2)  # N_freq, channel
            else:
                axis = (1, 2, 3)  # N_freq, L_cut, channel
            if keep_freq_axis:
                axis = axis[1:]
            norm = gen.sum_axis(squared, axis=axis)
            if dim == 3:
                norm = gen.transpose(norm)
            result.append(norm)
        else:
            raise ValueError('"parts" should be "I", "a", or "all" '
                             'or an array of them')

    return result[0] if len(result) == 1 else gen.stack(result)
Example #2
0
    def unstack_x(cls, x: gen.TensArr) -> gen.TensArr:
        if gen.ndim(x) != 4 or gen.shape(x)[2] <= cls.L_cut_x//2:
            raise Exception('Dimension/Size Mismatch')

        x = x[:, :, cls.L_cut_x//2, :]

        return gen.transpose(x, (1, 0, 2))
Example #3
0
def put(array, indices, values, axis=0):
    """put stores 'values' into 'array' at 'indices...'.

    parameters which must be specified by keyword:

    array           data to be indexed & stuffed (put to).

    indices         An integer sequence, or tuple of integer sequences
                    specifying the array coordinates to hich data
                    is to be put.  Partial indices result in entire
                    inner blocks being overwritten.

    values          A sequence of values to be written to the specified
                    indices of array.

    axis=0          selects the axis along which the put should be performed.

    >>> o = fromlist(range(10))
    >>> a = arange(5)*2
    >>> put(o, a, 0); o
    ObjectArray([0, 1, 0, 3, 0, 5, 0, 7, 0, 9])
    """
    work = asarray(array)
    if axis == 0:
        work._put((indices,), values)
    elif isinstance(axis, (int, long)):
        if isinstance(indices, (int, long, float)):
            raise ValueError("indices must be a sequence")
        work = _gen.swapaxes(work, 0, axis)
        work._put((indices,), values)
        work = _gen.swapaxes(work, 0, axis)
    else:
        def_axes = range(work.rank)
        for x in axis:
            def_axes.remove(x)
        axis = list(axis) + def_axes
        work = _gen.transpose(work, axis)
        work._put(indices, values)
        work = _gen.transpose(work, axis)
    if work is not array:
        if isinstance(array, _gen.NDArray):
            array[:] = work
Example #4
0
def put(array, indices, values, axis=0):
    """put stores 'values' into 'array' at 'indices...'.

    parameters which must be specified by keyword:

    array           data to be indexed & stuffed (put to).

    indices         An integer sequence, or tuple of integer sequences
                    specifying the array coordinates to hich data
                    is to be put.  Partial indices result in entire
                    inner blocks being overwritten.

    values          A sequence of values to be written to the specified
                    indices of array.

    axis=0          selects the axis along which the put should be performed.

    >>> o = fromlist(range(10))
    >>> a = arange(5)*2
    >>> put(o, a, 0); o
    ObjectArray([0, 1, 0, 3, 0, 5, 0, 7, 0, 9])
    """
    work = asarray(array)
    if axis == 0:
        work._put((indices, ), values)
    elif isinstance(axis, (int, long)):
        if isinstance(indices, (int, long, float)):
            raise ValueError("indices must be a sequence")
        work = _gen.swapaxes(work, 0, axis)
        work._put((indices, ), values)
        work = _gen.swapaxes(work, 0, axis)
    else:
        def_axes = range(work.rank)
        for x in axis:
            def_axes.remove(x)
        axis = list(axis) + def_axes
        work = _gen.transpose(work, axis)
        work._put(indices, values)
        work = _gen.transpose(work, axis)
    if work is not array:
        if isinstance(array, _gen.NDArray):
            array[:] = work
Example #5
0
def take(array, indices, axis=0):
    """take picks elements of 'array' specified by 'indices'.

    parameters which must be specified by keyword:

    array           data to be indexed & collected (taken from).

    indices         An integer sequence, or tuple of integer sequences
                    specifying the array coordinates from which data
                    is to be taken.  Partial indices result in entire
                    inner blocks being taken.

    axis=0          selects the axis (or axes) along which the take
                    should be performed.

    >>> o = fromlist(range(10))
    >>> a = arange(5)*2
    >>> take(o, a)
    ObjectArray([0, 2, 4, 6, 8])
    """
    if axis == 0:
        array = asarray(array)
        return array._take((indices,))
    elif isinstance(axis, (int, long)):
        if isinstance(indices, (int, long, float)):
            raise ValueError("indices must be a sequence")
        work = _gen.swapaxes(array, 0, axis)
        work = work._take((indices,))
        return _gen.swapaxes(work, 0, axis)
    else:
        def_axes = range(array.rank)
        for x in axis:
            def_axes.remove(x)
        axis = list(axis) + def_axes
        work = _gen.transpose(array, axis)
        return work._take(indices)
Example #6
0
def take(array, indices, axis=0):
    """take picks elements of 'array' specified by 'indices'.

    parameters which must be specified by keyword:

    array           data to be indexed & collected (taken from).

    indices         An integer sequence, or tuple of integer sequences
                    specifying the array coordinates from which data
                    is to be taken.  Partial indices result in entire
                    inner blocks being taken.

    axis=0          selects the axis (or axes) along which the take
                    should be performed.

    >>> o = fromlist(range(10))
    >>> a = arange(5)*2
    >>> take(o, a)
    ObjectArray([0, 2, 4, 6, 8])
    """
    if axis == 0:
        array = asarray(array)
        return array._take((indices, ))
    elif isinstance(axis, (int, long)):
        if isinstance(indices, (int, long, float)):
            raise ValueError("indices must be a sequence")
        work = _gen.swapaxes(array, 0, axis)
        work = work._take((indices, ))
        return _gen.swapaxes(work, 0, axis)
    else:
        def_axes = range(array.rank)
        for x in axis:
            def_axes.remove(x)
        axis = list(axis) + def_axes
        work = _gen.transpose(array, axis)
        return work._take(indices)
Example #7
0
    def unstack_y(cls, y: gen.TensArr) -> gen.TensArr:
        if gen.ndim(y) != 4 or gen.shape(y)[2] != 1:
            raise Exception('Dimension/Size Mismatch')

        return gen.transpose(gen.squeeze(y, axis=2), (1, 0, 2))