Skip to content

Commit

Permalink
Clean up config handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ihnorton committed Apr 2, 2019
1 parent 18752e9 commit d58c69c
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions tiledb/highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def open(uri, mode='r', config=None):
ctx = tiledb.Ctx(cfg)
else:
ctx = default_ctx()

schema = ArraySchema.load(uri, ctx=ctx)
if not schema:
raise Exception("Unable to load tiledb ArraySchema from URI: '{}'".format(uri))
Expand All @@ -36,7 +37,7 @@ def save(uri, array, config=None, **kw):
def view(array, **kw):
return View(array, **kw)

def schema_like(*args, ctx=default_ctx(), **kw):
def schema_like(*args, shape=None, dtype=None, ctx=default_ctx(), **kw):
"""
Return an ArraySchema corresponding to a NumPy-like object or
a `shape` and `dtype`. Users are encouraged to pass 'tile' and
Expand All @@ -46,20 +47,18 @@ def schema_like(*args, ctx=default_ctx(), **kw):
:param T: NumPy array or TileDB URI
:return: tiledb.ArraySchema
"""
def ndarray_like(obj):
def is_ndarray_like(obj):
return hasattr(arr, 'shape') and hasattr(arr, 'dtype') and hasattr(arr, 'ndim')

if len(args) == 1:
arr = args[0]
if ndarray_like(arr):
schema = schema_like_numpy(arr, **kw)
if is_ndarray_like(arr):
schema = schema_like_numpy(arr, key=key)
else:
raise ValueError("expected ndarray-like object")

elif kw and ('shape' in kw) and ('dtype' in kw):
shape = kw.pop('shape')
elif shape and dtype:
ndim = len(shape)
dtype = kw.pop('dtype')
tiling = regularize_tiling(kw.pop('tile', None), ndim)
dims = []
for d in range(ndim):
Expand All @@ -72,16 +71,16 @@ def ndarray_like(obj):
att = Attr(dtype=dtype, ctx=ctx)
dom = Domain(*dims, ctx=ctx)
schema = ArraySchema(ctx=ctx, domain=dom, attrs=(att,), **kw)
elif kw:
raise ValueError("Unsupported keyword arguments")
elif kw is not None:
raise ValueError
else:
raise ValueError("Must provide either ndarray-like object or 'shape' "
"and 'dtype' keyword arguments")

return schema


def empty_like(uri, arr, ctx=default_ctx(), **kw):
def empty_like(uri, arr, config=None, key=None, tile=None):
"""
Create and return an empty, writeable DenseArray with schema based on
a NumPy-array like object.
Expand All @@ -92,13 +91,19 @@ def empty_like(uri, arr, ctx=default_ctx(), **kw):
:param kw:
:return:
"""
if config:
cfg = tiledb.Config(config)
ctx = tiledb.Ctx(cfg)
else:
ctx = default_ctx()

if arr is ArraySchema:
schema = arr
else:
schema = schema_like(arr, ctx=ctx, **kw)
schema = schema_like(arr, key=key, ctx=ctx)

tiledb.DenseArray.create(uri, schema=schema)
return tiledb.DenseArray(uri, 'w', ctx=ctx)
return tiledb.DenseArray(uri, mode='w', key=key, ctx=ctx)


def from_numpy(uri, array, ctx=default_ctx(), **kw):
Expand Down

0 comments on commit d58c69c

Please sign in to comment.