Skip to content

Commit

Permalink
Added a new fromiter() constructor. Can use a Fixed shape or TypeVar …
Browse files Browse the repository at this point in the history
…(unknown length).
  • Loading branch information
Francesc Alted authored and sdiehl committed Dec 15, 2012
1 parent 1586c3d commit 302008e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion blaze/__init__.py
Expand Up @@ -14,7 +14,7 @@
dtype = dshape

from params import params
from toplevel import open, zeros, ones
from toplevel import open, zeros, ones, fromiter

# Shorthand namespace dump
from datashape.shorthand import *
Expand Down
2 changes: 1 addition & 1 deletion blaze/carray/__init__.py
Expand Up @@ -11,7 +11,7 @@
# _cparams as cparams,
# blosc_version, _blosc_set_nthreads as blosc_set_nthreads
)
from toplevel import cparams, open, zeros, ones
from toplevel import cparams, open, zeros, ones, fromiter
from version import __version__

# doesn't work for reasons I don't understand
Expand Down
10 changes: 4 additions & 6 deletions blaze/carray/toplevel.py
Expand Up @@ -161,16 +161,14 @@ def fromiter(iterable, dtype, count, **kwargs):
dtype = np.dtype(dtype)
if dtype.kind == "V":
# A ctable
obj = ca.ctable(np.array([], dtype=dtype),
expectedlen=expectedlen,
**kwargs)
obj = ctable(np.array([], dtype=dtype),
expectedlen=expectedlen, **kwargs)
chunklen = sum(obj.cols[name].chunklen
for name in obj.names) // len(obj.names)
else:
# A carray
obj = ca.carray(np.array([], dtype=dtype),
expectedlen=expectedlen,
**kwargs)
obj = carray(np.array([], dtype=dtype),
expectedlen=expectedlen, **kwargs)
chunklen = obj.chunklen

# Then fill it
Expand Down
10 changes: 10 additions & 0 deletions blaze/tests/test_quickstart.py
Expand Up @@ -22,6 +22,16 @@ def test_simple_persistence():
# Remove everything under the temporary dir
shutil.rmtree(td)

def test_ones():
from blaze import ones

a = ones('10, float64', params=params(clevel=5))

def test_fromiter():
from blaze import fromiter

a = fromiter(xrange(10), 'x, float64', params=params(clevel=5))

def test_custom_dshape():
from blaze import Array, RecordDecl
from blaze import int32, string
Expand Down
30 changes: 29 additions & 1 deletion blaze/toplevel.py
Expand Up @@ -7,7 +7,7 @@
from sources.chunked import CArraySource

from table import NDArray, Array
from blaze.datashape.coretypes import from_numpy, to_numpy
from blaze.datashape.coretypes import from_numpy, to_numpy, TypeVar, Fixed
from blaze import carray, dshape as _dshape

import numpy as np
Expand Down Expand Up @@ -71,6 +71,34 @@ def ones(dshape, params=None):
params=params)
return Array(source)

def fromiter(iterable, dshape, params=None):
""" Create an Array and fill it with values from `iterable`.
"""
if isinstance(dshape, basestring):
dshape = _dshape(dshape)
shape, dtype = dshape.parameters[:-1], dshape.parameters[-1]
# Check the shape part
if len(shape) > 1:
raise ValueError("shape can be only 1-dimensional")
length = shape[0]
count = -1
if type(length) == TypeVar:
count = -1
elif type(length) == Fixed:
count = length.val

dtype = dtype.to_dtype()
# Now, create the Array itself (using the carray backend)
cparams, rootdir, format_flavor = to_cparams(params or _params())
if rootdir is not None:
carray.fromiter(iterable, dtype, count=count,
rootdir=rootdir, cparams=cparams)
return open(rootdir)
else:
ica = carray.fromiter(iterable, dtype, count=count, cparams=cparams)
source = CArraySource(ica, params=params)
return Array(source)

def loadtxt(filetxt, storage):
""" Convert txt file into Blaze native format """
Array(np.loadtxt(filetxt), params=params(storage=storage))

0 comments on commit 302008e

Please sign in to comment.