# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 3.8.2
#     language: python
#     name: 3.8.2
# ---

# + papermill={"duration": 0.356807, "end_time": "2020-07-01T21:57:20.467394", "exception": false, "start_time": "2020-07-01T21:57:20.110587", "status": "completed"} tags=[]
from dask.array import from_array
from numpy import array

A = array(range(36)).reshape((6,6))
d = from_array(A, chunks=(3,3))
d

# + [markdown] papermill={"duration": 0.013713, "end_time": "2020-07-01T21:57:20.495329", "exception": false, "start_time": "2020-07-01T21:57:20.481616", "status": "completed"} tags=[]
# ## `scipy.sparse`

# + papermill={"duration": 0.051382, "end_time": "2020-07-01T21:57:20.560967", "exception": false, "start_time": "2020-07-01T21:57:20.509585", "status": "completed"} tags=[]
from scipy.sparse import spmatrix, coo_matrix, csr_matrix, csc_matrix, dia_matrix

sps = d.map_blocks(coo_matrix, chunks=(3,3))
spsc = sps.compute(scheduler="synchronous")
spsc

# + papermill={"duration": 0.02682, "end_time": "2020-07-01T21:57:20.602002", "exception": false, "start_time": "2020-07-01T21:57:20.575182", "status": "completed"} tags=[]
spsc.todense()

# + [markdown] papermill={"duration": 0.014072, "end_time": "2020-07-01T21:57:20.629306", "exception": false, "start_time": "2020-07-01T21:57:20.615234", "status": "completed"} tags=[]
# ### `axis=None`

# + [markdown] papermill={"duration": 0.013582, "end_time": "2020-07-01T21:57:20.657050", "exception": false, "start_time": "2020-07-01T21:57:20.643468", "status": "completed"} tags=[]
# Dask dense blocks:

# + papermill={"duration": 0.045208, "end_time": "2020-07-01T21:57:20.715680", "exception": false, "start_time": "2020-07-01T21:57:20.670472", "status": "completed"} tags=[]
d.sum().compute(), d.sum(keepdims=False).compute(), d.sum(keepdims=True).compute()

# + [markdown] papermill={"duration": 0.01538, "end_time": "2020-07-01T21:57:20.745500", "exception": false, "start_time": "2020-07-01T21:57:20.730120", "status": "completed"} tags=[]
# Dask scipy.sparse blocks:

# + papermill={"duration": 0.049385, "end_time": "2020-07-01T21:57:20.809746", "exception": false, "start_time": "2020-07-01T21:57:20.760361", "status": "completed"} tags=[]
sps.sum().compute(), sps.sum(keepdims=False).compute(), sps.sum(keepdims=True).compute()

# + [markdown] papermill={"duration": 0.017039, "end_time": "2020-07-01T21:57:20.841423", "exception": false, "start_time": "2020-07-01T21:57:20.824384", "status": "completed"} tags=[]
# scipy.sparse, sans Dask:

# + papermill={"duration": 0.032058, "end_time": "2020-07-01T21:57:20.889613", "exception": false, "start_time": "2020-07-01T21:57:20.857555", "status": "completed"} tags=[]
spsc.sum(), spsc.sum(keepdims=False), spsc.sum(keepdims=True)

# + [markdown] papermill={"duration": 0.015578, "end_time": "2020-07-01T21:57:20.920313", "exception": false, "start_time": "2020-07-01T21:57:20.904735", "status": "completed"} tags=[]
# ### `axis=0`

# + [markdown] papermill={"duration": 0.016251, "end_time": "2020-07-01T21:57:20.951688", "exception": false, "start_time": "2020-07-01T21:57:20.935437", "status": "completed"} tags=[]
# Dask dense blocks:

# + papermill={"duration": 0.043464, "end_time": "2020-07-01T21:57:21.011608", "exception": false, "start_time": "2020-07-01T21:57:20.968144", "status": "completed"} tags=[]
d.sum(axis=0).compute(), d.sum(axis=0, keepdims=False).compute(), d.sum(axis=0, keepdims=True).compute()

# + [markdown] papermill={"duration": 0.01525, "end_time": "2020-07-01T21:57:21.042524", "exception": false, "start_time": "2020-07-01T21:57:21.027274", "status": "completed"} tags=[]
# Dask scipy.sparse blocks:

# + papermill={"duration": 0.054799, "end_time": "2020-07-01T21:57:21.112333", "exception": false, "start_time": "2020-07-01T21:57:21.057534", "status": "completed"} tags=[]
sps.sum(axis=0).compute(), sps.sum(axis=0, keepdims=False).compute(), sps.sum(axis=0, keepdims=True).compute()

# + [markdown] papermill={"duration": 0.017029, "end_time": "2020-07-01T21:57:21.144274", "exception": false, "start_time": "2020-07-01T21:57:21.127245", "status": "completed"} tags=[]
# scipy.sparse, sans Dask:

# + papermill={"duration": 0.034129, "end_time": "2020-07-01T21:57:21.193918", "exception": false, "start_time": "2020-07-01T21:57:21.159789", "status": "completed"} tags=[]
spsc.sum(axis=0), spsc.sum(axis=0, keepdims=False), spsc.sum(axis=0, keepdims=True)

# + [markdown] papermill={"duration": 0.015816, "end_time": "2020-07-01T21:57:21.226067", "exception": false, "start_time": "2020-07-01T21:57:21.210251", "status": "completed"} tags=[]
# ### `axis=1`

# + [markdown] papermill={"duration": 0.017408, "end_time": "2020-07-01T21:57:21.260154", "exception": false, "start_time": "2020-07-01T21:57:21.242746", "status": "completed"} tags=[]
# Dask dense blocks:

# + papermill={"duration": 0.048616, "end_time": "2020-07-01T21:57:21.326152", "exception": false, "start_time": "2020-07-01T21:57:21.277536", "status": "completed"} tags=[]
d.sum(axis=1).compute(), d.sum(axis=1, keepdims=False).compute(), d.sum(axis=1, keepdims=True).compute()

# + [markdown] papermill={"duration": 0.018309, "end_time": "2020-07-01T21:57:21.364412", "exception": false, "start_time": "2020-07-01T21:57:21.346103", "status": "completed"} tags=[]
# Dask scipy.sparse blocks:

# + papermill={"duration": 0.052337, "end_time": "2020-07-01T21:57:21.433147", "exception": false, "start_time": "2020-07-01T21:57:21.380810", "status": "completed"} tags=[]
sps.sum(axis=1).compute(), sps.sum(axis=1, keepdims=False).compute(), sps.sum(axis=1, keepdims=True).compute()

# + [markdown] papermill={"duration": 0.016834, "end_time": "2020-07-01T21:57:21.468289", "exception": false, "start_time": "2020-07-01T21:57:21.451455", "status": "completed"} tags=[]
# scipy.sparse, sans Dask:

# + papermill={"duration": 0.030581, "end_time": "2020-07-01T21:57:21.515189", "exception": false, "start_time": "2020-07-01T21:57:21.484608", "status": "completed"} tags=[]
spsc.sum(axis=1), spsc.sum(axis=1, keepdims=False), spsc.sum(axis=1, keepdims=True)

StarcoderdataJupyterScriptsDedupFiltered