多維陣列

在 Python 中使用 Dask 進行平行程式設計

James Fulton

Climate Informatics Researcher

多維資料的類型

  • 天氣預報/觀測
  • 3D 生醫掃描
  • 衛星影像
  • 其他科學儀器資料
在 Python 中使用 Dask 進行平行程式設計

HDF5

HDF 標誌

  • 階層式資料格式(Hierarchical Data Format)
  • 以階層式結構儲存,類似(子)目錄
在 Python 中使用 Dask 進行平行程式設計

HDF5 檔案長什麼樣?

單一資料夾

在 Python 中使用 Dask 進行平行程式設計

HDF5 檔案長什麼樣?

一個資料夾,包含四個不同的資料集與一些中繼資料。

在 Python 中使用 Dask 進行平行程式設計

用 h5py 瀏覽 HDF5 檔案

import h5py

# Open the HDF5 file
file = h5py.File('data.hdf5')


# Print the available datasets inside the file print(file.keys())
<KeysViewHDF5 ['A', 'B', 'C', 'D']>
在 Python 中使用 Dask 進行平行程式設計

用 h5py 瀏覽 HDF5 檔案

import h5py

# Open the HDF5 file
file = h5py.File('data.hdf5')


# Select dataset A dataset_a = file['/A']
print(dataset_a)
<HDF5 dataset "A": shape (10000, 100, 100), type "<f4">
在 Python 中使用 Dask 進行平行程式設計

從 HDF5 載入

import dask.array as da

# Load dataset into a Dask array a = da.from_array(dataset_a, chunks=(100, 20, 20))
print(a)
dask.array<array, shape=(10000, 100, 100), dtype=float32, chunksize=(100, 20, 20),
    chunktype=numpy.ndarray>
在 Python 中使用 Dask 進行平行程式設計

Zarr

  • 類似 HDF5 的階層式資料集
  • 為分塊設計
  • 適合透過雲端服務(如 AWS、Google Cloud 等)串流
  • 可瀏覽的檔案結構
在 Python 中使用 Dask 進行平行程式設計

從 Zarr 載入

import dask.array as da

a = da.from_zarr("dataset.zarr", component="A")


print(a)
dask.array<from-zarr, shape=(10000, 100, 100), dtype=float32,
    chunksize=(100, 20, 20), chunktype=numpy.ndarray>
在 Python 中使用 Dask 進行平行程式設計

一起來練習吧!

在 Python 中使用 Dask 進行平行程式設計

Preparing Video For Download...