Dask pole

Parallel Programming with Dask in Python

James Fulton

Climate informatics researcher

Rozdělení polí na bloky

Zobrazuje pole jako jeden celek

Parallel Programming with Dask in Python

Rozdělení polí na bloky

Zobrazuje pole rozdělené do více bloků

Parallel Programming with Dask in Python

NumPy vs. Dask pole

Zobrazuje pole jako jeden celek

import numpy as np

x = np.ones((4000, 6000))
print(x.sum())
24000000.0
  • Výpočet trvá 740 milisekund

Zobrazuje pole rozdělené do více bloků

import dask.array as da

x = da.ones((4000, 6000), chunks=(1000,2000))
print(x.sum().compute())
24000000.0
  • Výpočet trvá 60 milisekund
Parallel Programming with Dask in Python

Graf úloh Dask pole

Zobrazuje, jak se větve grafu úloh spojují do výsledné hodnoty.

Parallel Programming with Dask in Python

Metody Dask polí

Dask pole mají téměř všechny metody NumPy polí.

  • x.max()
  • x.min()
  • x.sum()
  • x.mean()
  • atd.
print(sum_down_columns.compute())
array([1000., 1000., 1000., 1000., 
    1000., 1000., 1000., 1000., 1000.,
    1000.])
Parallel Programming with Dask in Python

Práce s Dask poli jako s NumPy poli

# Lazy mathematics with Dask array
y1 = x**2 + 2*x + 1

# Lazy slicing
y2 = x[:10]

# Applying NumPy functions is lazy too
y3 = np.sin(x)
print(y1)
dask.array<add, shape=(1000, 10), ...
print(y2)
dask.array<getitem, shape=(10, 10), ...
print(y3)
dask.array<sin, shape=(1000, 10), ...
Parallel Programming with Dask in Python

Načítání polí obrázků

import dask.array as da

import da.image
image_array = da.image.imread('images/*.png')
print(image_array)
dask.array<imread, shape=(40000, 256, 256, 3), dtype=uint8, 
    chunksize=(1, 256, 256, 3), chunktype=numpy.ndarray>
Parallel Programming with Dask in Python

Aplikace vlastních funkcí na bloky

def instagram_filter(image):
    ...
    return pretty_image

# Apply function to each image independently pretty_image_array = image_array.map_blocks(instagram_filter)
print(pretty_image_array)
dask.array<instagram_filter, shape=(40000, 256, 256, 3), dtype=uint8, 
    chunksize=(1, 256, 256, 3), chunktype=numpy.ndarray>
Parallel Programming with Dask in Python

Lass uns üben!

Parallel Programming with Dask in Python

Preparing Video For Download...