You can read array data that spans multiple ranges, rather than running several individual read queries.
How to run this tutorial
You can run this tutorial in two ways:
Locally on your machine.
On TileDB Cloud.
However, since TileDB Cloud has a free tier, we strongly recommend that you sign up and run everything there, as that requires no installations or deployment.
This tutorial shows how to perform multi-range slices. Although it focuses on a dense array, mutli-range slicing applies to sparse arrays as well. Visit the Key Concepts: Reads section for more details on multi-range slicing.
First, import the necessary libraries, set the array URI (that is, its path, which in this tutorial will be on local storage), and delete any previously created arrays with the same name.
# Create the two dimensionsd1 = tiledb.Dim(name="d1", domain=(1, 4), tile=2, dtype=np.int32)d2 = tiledb.Dim(name="d2", domain=(1, 4), tile=2, dtype=np.int32)# Create a domain using the two dimensionsdom = tiledb.Domain(d1, d2)# Create an attributea = tiledb.Attr(name="a", dtype=np.int32)# Create the array schema, setting `sparse=False` to indicate a dense arraysch = tiledb.ArraySchema(domain=dom, sparse=False, attrs=[a])# Create the array on disk (it will initially be empty)tiledb.Array.create(array_uri, sch)
# Create the two dimensionsd1 <-tiledb_dim("d1", c(1L, 4L), 2L, "INT32")d2 <-tiledb_dim("d2", c(1L, 4L), 2L, "INT32")# Create a domain using the two dimensionsdom <-tiledb_domain(dims =c(d1, d2))# Create an attributea <-tiledb_attr("a", type ="INT32")# Create the array schema, setting `sparse = FALSE` to indicate a dense arraysch <-tiledb_array_schema(dom, a, sparse =FALSE)# Create the array on disk (it will initially be empty)arr <-tiledb_array_create(array_uri, sch)
# Prepare some data in a NumPy arraydata = np.array( [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], dtype=np.int32)# Write data to the arraywith tiledb.open(array_uri, "w") as A: A[:] = data
# Prepare some data in an arraydata <-t(array(1:16, dim =c(4, 4)))# Open the array for writing and write data to the arrayarr <-tiledb_array(uri = array_uri, query_type ="WRITE", return_as ="data.frame")arr[] <- data# Close the arrayarr <-tiledb_array_close(arr)
Use multi-range slicing to read data from the array.
# Open the array in read modeA = tiledb.open(array_uri, "r")# Show the entire arrayprint("Entire array: ")print(A[:])print("\n")# Slice a multi-range subarray.# Note that `multi_index` uses closed ranges.print("Multi-range, rows 1,2 and 4, and columns 1-3: ")print(A.multi_index[[slice(1, 2), 4], slice(1, 3)]["a"])# Remember to close the arrayA.close()