Esempio n. 1
0
def arange(start, stop=None, step=1, dtype=None):
    '''Create a 1D dataset of given type where values range from specified start up to
    but not including stop in given steps

    Arguments:
    start -- optional starting value, defaults to 0
    stop  -- exclusive stop value
    step  -- difference between neighbouring values, defaults to 1
    dtype -- defaults to None which means the type is inferred from given start, stop, step values
    '''
    if stop is None:
        stop = start
        start = 0
    if dtype is None:
        if type(start) is _types.ComplexType or type(stop) is _types.ComplexType or type(step) is _types.ComplexType: 
            dtype = complex128
        elif type(start) is _types.FloatType or type(stop) is _types.FloatType or type(step) is _types.FloatType: 
            dtype = float64
        elif type(start) is _types.IntType or type(stop) is _types.IntType or type(step) is _types.IntType: 
            dtype = int32
        else:
            raise ValueError, "Unknown or invalid type of input value"
    else:
        dtype = _translatenativetype(dtype)
    if dtype == bool:
        return None

    return _df.createRange(start, stop, step, dtype.value)
Esempio n. 2
0
def arange(start, stop=None, step=1, dtype=None):
    '''Create a 1D dataset of given type where values range from specified start up to
    but not including stop in given steps

    Arguments:
    start -- optional starting value, defaults to 0
    stop  -- exclusive stop value
    step  -- difference between neighbouring values, defaults to 1
    dtype -- defaults to None which means the type is inferred from given start, stop, step values
    '''
    if stop is None:
        stop = start
        start = 0
    if dtype is None:
        if type(start) is _types.ComplexType or type(
                stop) is _types.ComplexType or type(
                    step) is _types.ComplexType:
            dtype = complex128
        elif type(start) is _types.FloatType or type(
                stop) is _types.FloatType or type(step) is _types.FloatType:
            dtype = float64
        elif type(start) is _types.IntType or type(
                stop) is _types.IntType or type(step) is _types.IntType:
            dtype = int32
        else:
            raise ValueError, "Unknown or invalid type of input value"
    else:
        dtype = _translatenativetype(dtype)
    if dtype == bool:
        return None

    return _df.createRange(start, stop, step, dtype.value)
Esempio n. 3
0
 def __init__(self, shape=None, dtype=None, buffer=None, copy=False):
     # check what buffer is and convert if necessary
     if buffer is not None:
         self.__dataset = __cvt_jobj(_jinput(buffer), dtype=dtype, copy=copy, force=True)
         if shape is not None:
             self.__dataset.setShape(asIterable(shape))
     else:
         dtype = _translatenativetype(dtype)
         self.__dataset = _df.zeros(dtype.elements, asIterable(shape), dtype.value)
Esempio n. 4
0
def zeros(shape, dtype=float64, elements=None):
    '''Create a dataset filled with 0'''
    dtype = _translatenativetype(dtype)
    if elements is not None:
        if type(dtype) is _types.FunctionType:
            dtype = dtype(elements)
        else:
            dtype.elements = elements
    elif type(dtype) is _types.FunctionType:
        raise ValueError, "Given data-type is a function and needs elements defining"

    return _df.zeros(dtype.elements, asIterable(shape), dtype.value)
Esempio n. 5
0
def zeros(shape, dtype=float64, elements=None):
    '''Create a dataset filled with 0'''
    dtype = _translatenativetype(dtype)
    if elements is not None:
        if type(dtype) is _types.FunctionType:
            dtype = dtype(elements)
        else:
            dtype.elements = elements
    elif type(dtype) is _types.FunctionType:
        raise ValueError, "Given data-type is a function and needs elements defining"

    return _df.zeros(dtype.elements, asIterable(shape), dtype.value)
Esempio n. 6
0
 def __init__(self, shape=None, dtype=None, buffer=None, copy=False):
     # check what buffer is and convert if necessary
     if buffer is not None:
         self.__dataset = __cvt_jobj(_jinput(buffer),
                                     dtype=dtype,
                                     copy=copy,
                                     force=True)
         if shape is not None:
             self.__dataset.setShape(asIterable(shape))
     else:
         dtype = _translatenativetype(dtype)
         self.__dataset = _df.zeros(dtype.elements, asIterable(shape),
                                    dtype.value)
Esempio n. 7
0
def __cvt_jobj(obj, dtype=None, copy=True, force=False):
    '''Convert object to java object'''
    if isinstance(obj, ndarray):
        obj = obj._jdataset()

    if isinstance(obj, _ds):
        if copy:
            if dtype is None or _translatenativetype(dtype).value == obj.dtype:
                return obj.clone()
            else:
                return obj.cast(_translatenativetype(dtype).value)
        else:
            if dtype is None:
                return obj
            return obj.cast(_translatenativetype(dtype).value)

    if not isinstance(obj, list):
        if isinstance(obj, _matrix): # cope with JAMA matrices
            if dtype is None:
                dtype = float64
            obj = obj.getArray()
    elif len(obj) == 0 and dtype is None:
        dtype = float64

    obj = _cvt2j(obj)
    try:
        iter(obj)
    except:
        if not force:
            if isinstance(obj, complex):
                return _jcomplex(obj.real, obj.imag)
            return obj

    if dtype is None:
        dtype = _getdtypefromobj(obj)
    else:
        dtype = _translatenativetype(dtype)

    return _df.createFromObject(obj, dtype.value)
Esempio n. 8
0
def __cvt_jobj(obj, dtype=None, copy=True, force=False):
    '''Convert object to java object'''
    if isinstance(obj, ndarray):
        obj = obj._jdataset()

    if isinstance(obj, _ds):
        if copy:
            if dtype is None or _translatenativetype(dtype).value == obj.dtype:
                return obj.clone()
            else:
                return obj.cast(_translatenativetype(dtype).value)
        else:
            if dtype is None:
                return obj
            return obj.cast(_translatenativetype(dtype).value)

    if not isinstance(obj, list):
        if isinstance(obj, _matrix):  # cope with JAMA matrices
            if dtype is None:
                dtype = float64
            obj = obj.getArray()

    obj = _cvt2j(obj)
    try:
        iter(obj)
    except:
        if not force:
            if isinstance(obj, complex):
                return _jcomplex(obj.real, obj.imag)
            return obj

    if dtype is None:
        dtype = _getdtypefromobj(obj)
    else:
        dtype = _translatenativetype(dtype)

    return _df.createFromObject(obj, dtype.value)
Esempio n. 9
0
def zeros_like(a):
    return _df.zeros(a)
Esempio n. 10
0
def ones_like(a):
    return _df.zeros(a).fill(1)
Esempio n. 11
0
def ones(shape, dtype=float64):
    '''Create a dataset filled with 1'''
    dtype = _translatenativetype(dtype)
    return _df.ones(dtype.elements, asIterable(shape), dtype.value)
Esempio n. 12
0
def zeros_like(a):
    return _df.zeros(a)
Esempio n. 13
0
def ones_like(a):
    return _df.zeros(a).fill(1)
Esempio n. 14
0
def ones(shape, dtype=float64):
    '''Create a dataset filled with 1'''
    dtype = _translatenativetype(dtype)
    return _df.ones(dtype.elements, asIterable(shape), dtype.value)