# Using a multi-dimensional sparse array in python

**URL:** <https://forum.tiledb.com/t/using-a-multi-dimensional-sparse-array-in-python/99>\
**Category:** Uncategorized\
**Created:** [July 29, 2019, 1:57am UTC](https://forum.tiledb.com/t/using-a-multi-dimensional-sparse-array-in-python/99 "2019-07-29T01:57:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![haehn](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.tiledb.com/haehn/32/33_2.png) [@haehn](https://forum.tiledb.com/u/haehn)\
**Post date:** [July 29, 2019, 1:57am UTC](https://forum.tiledb.com/t/using-a-multi-dimensional-sparse-array-in-python/99/1 "2019-07-29T01:57:06Z")

</div>

Hello!

I have data including 190 lines with each up to 180 points with x,y,z coordinates and I try to store it as a sparse array with tiledb.

```
dom = tiledb.Domain(tiledb.Dim(name="fibers", domain=(0,number_of_streamlines-1), tile=4, dtype=np.float32),
                        tiledb.Dim(name="points", domain=(0,max_line_length-1), tile=4, dtype=np.float32),
    # tiledb.Dim(name="coords", domain=(0,2), tile=4, dtype=np.float32), 
                       )

schema = tiledb.ArraySchema(ctx, domain=dom, sparse=True,
                                attrs=[tiledb.Attr(ctx, name="x", dtype=np.float32), 
                                       tiledb.Attr(ctx, name="y", dtype=np.float32),
                                       tiledb.Attr(ctx, name="z", dtype=np.float32)])

```

TypeError: **init** () got multiple values for keyword argument ‘name’

```
schema = tiledb.ArraySchema(ctx, domain=dom, sparse=True,
                                attrs=[tiledb.Attr(ctx, name="xyz", dtype=np.dtype([("", np.float32),
                                                                                    ("", np.float32),
                                                                                    ("", np.float32)]))])

```

TypeError: **init** () got multiple values for keyword argument ‘name’

This is a noob questions so I really don’t know what is going on.

Please help!

Thank you so much!

---

<div class="post-metadata">

**Author:** ![ihnorton](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.tiledb.com/ihnorton/32/23_2.png) [@ihnorton](https://forum.tiledb.com/u/ihnorton)\
**Post date:** [July 29, 2019, 1:11pm UTC](https://forum.tiledb.com/t/using-a-multi-dimensional-sparse-array-in-python/99/2 "2019-07-29T13:11:04Z")

</div>

Hi Daniel,

Nice to see you on here!

The issue here is that the `ctx` argument should be passed as a keyword, if at all, rather than positionally. However, with Python you can omit it for routine use.

The following works for me with TileDB-Py 0.4.3 from Conda:

```auto
schema = tiledb.ArraySchema(domain=dom, sparse=True,
                                attrs=[tiledb.Attr(name="x", dtype=np.float32),
                                       tiledb.Attr(name="y", dtype=np.float32),
                                       tiledb.Attr(name="z", dtype=np.float32)])

```

The [current version](https://docs.tiledb.io/en/stable/tutorials/sparse-arrays.html) of the sparse tutorial and [quickstart code](https://github.com/TileDB-Inc/TileDB-Py/blob/0.4.3/examples/quickstart_sparse.py) look up-to-date, but please let me know any doc URLs you were working from, and I will make sure to update them.

Cheers,  
Isaiah

---

<div class="post-metadata">

**Author:** ![haehn](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.tiledb.com/haehn/32/33_2.png) [@haehn](https://forum.tiledb.com/u/haehn)\
**Post date:** [July 30, 2019, 2:10am UTC](https://forum.tiledb.com/t/using-a-multi-dimensional-sparse-array-in-python/99/3 "2019-07-30T02:10:14Z")

</div>

Thank you so much! This part works now! 🙂

Now, I tried the following schemas:

```
schema = tiledb.ArraySchema(domain=dom, sparse=True,
                                attrs=[tiledb.Attr(name="x", dtype=np.float32),
                                       tiledb.Attr(name="y", dtype=np.float32),
                                       tiledb.Attr(name="z", dtype=np.float32)])

```

and

```
schema = tiledb.ArraySchema(domain=dom, sparse=True,
                                attrs=[tiledb.Attr(name="coordinates", dtype=np.dtype([("", np.float32),
                                                                                       ("", np.float32), 
                                                                                       ("", np.float32)]))])

```

In both cases, I get this error whenever I want to write the data:  
`ValueError: all the input array dimensions except for the concatenation axis must match exactly`

with

```
with tiledb.SparseArray(array_name, mode='w') as A:
        A[I,J] = {"x":current_points[:,0], "y":current_points[:,1], "z":current_points[:,2]}

```

and

```
with tiledb.SparseArray(array_name, mode='w') as A:
    print(A)
    A[I, J] = current_points

```

Here is the code: [https://github.com/haehn/ABCDTRKV/blob/master/IPY/TileDB\_SparseArrayTest2.ipynb](https://github.com/haehn/ABCDTRKV/blob/master/IPY/TileDB_SparseArrayTest2.ipynb) (see bottom two sections)

Tried many things but no luck ☹ What is wrong or how can I debug the dimensions of A[I, J] versus current\_points? I would think they match!

Best,  
Daniel

---

<div class="post-metadata">

**Author:** ![ihnorton](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.tiledb.com/ihnorton/32/23_2.png) [@ihnorton](https://forum.tiledb.com/u/ihnorton)\
**Post date:** [July 30, 2019, 9:15pm UTC](https://forum.tiledb.com/t/using-a-multi-dimensional-sparse-array-in-python/99/4 "2019-07-30T21:15:27Z")

</div>

Hi Daniel,

Right now the API doesn’t automatically broadcast along indices like that, so each point id needs to also have the corresponding fiber id set (even though in this case it is the same)

The quick fix is in the following snippet (cells copied from your notebook in order to recreate the data, and slightly modified):

> <https://gist.github.com/ihnorton/9cb65817c4bfa0846eb8fb013a484cac>

The important change is this line:

> I, J = np.repeat(line\_index-1, line\_index), list(range(current\_points.shape[0]))

---

<div class="post-metadata">

**Author:** ![haehn](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.tiledb.com/haehn/32/33_2.png) [@haehn](https://forum.tiledb.com/u/haehn)\
**Post date:** [July 31, 2019, 12:10am UTC](https://forum.tiledb.com/t/using-a-multi-dimensional-sparse-array-in-python/99/5 "2019-07-31T00:10:03Z")

</div>

Got it! You make it look easy, Thank you!!
