建立延遲的管線

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

James Fulton

Climate Informatics Researcher

資料分塊(Chunks)

一張圖:資料集可放進硬碟,但太大無法放進記憶體。

一張圖:資料集被切成多個區塊。整體太大無法放進記憶體,但每個區塊可以。

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

Spotify 歌曲資料集

files = [
  '2005_tracks.csv',
  '2006_tracks.csv',
  '2007_tracks.csv',
  '2008_tracks.csv',
  '2009_tracks.csv',
  '2010_tracks.csv',
  ...
  '2020_tracks.csv',
]
在 Python 中使用 Dask 進行平行程式設計

Spotify 歌曲資料集

                       name  duration_ms release_date  ...
0     Aldrig (feat. Carmon)       247869   2019-01-01  ...
2  2019 - The Year to Build       288105   2019-01-01  ...
3                 Na zawsze       186812   2019-01-01  ...
4         Humo en la Trampa       258354   2019-01-01  ...
5                     Au Au       176000   2019-01-01  ...
...                     ...          ...          ...  ...
在 Python 中使用 Dask 進行平行程式設計

分析資料

import pandas as pd

maximums = []

for file in files:
    # Load each file
    df = pd.read_csv(file)

# Find maximum track length in each file max_length = df['duration_ms'].max()
# Store this maximum maximums.append(max_length)
# Find the maximum of all the maximum lengths absolute_maximum = max(maximums)
在 Python 中使用 Dask 進行平行程式設計

分析資料

import pandas as pd

maximums = []

for file in files:
    # Load each file
    df = delayed(pd.read_csv)(file) # <------- delay loading
    # Find maximum track length in each file
    max_length = df['duration_ms'].max()
    # Store this maximum
    maximums.append(max_length)

# Find the maximum of all the maximum lengths
absolute_maximum = max(maximums)
在 Python 中使用 Dask 進行平行程式設計

分析資料

import pandas as pd

maximums = []

for file in files:
    # Load each file
    df = delayed(pd.read_csv)(file) # <------- delay loading
    # Find maximum track length in each file
    max_length = df['duration_ms'].max()
    # Store this maximum
    maximums.append(max_length)

# Find the maximum of all the maximum lengths
absolute_maximum = delayed(max)(maximums) # <------- delay max() function
在 Python 中使用 Dask 進行平行程式設計

使用延遲物件的方法

import pandas as pd

maximums = []

for file in files:
    df = delayed(pd.read_csv)(file)
    # Use the .max() method
    max_length = df['duration_ms'].max()

    maximums.append(max_length)


absolute_maximum = delayed(max)(maximums)
print(max_length)
Delayed('max-0602855d-3ee6-4c43-a4d2-...')
  • 延遲物件的方法與屬性會回傳新的延遲物件
print(df.shape)
print(df.shape.compute())
Delayed('getattr-bc1e8838ab...')
(11907, 12)
在 Python 中使用 Dask 進行平行程式設計

使用延遲物件的方法

import pandas as pd

maximums = []

for file in files:
    df = delayed(pd.read_csv)(file)
    # Use a method which doesn't exist
    max_length = df['duration_ms'].fake()

    maximums.append(max_length)


absolute_maximum = delayed(max)(maximums)
print(max_length)
Delayed('max-6c026036-5daf-4b2-...')
  • 直到呼叫 .compute() 之後,方法才會執行
print(max_length.compute())
...
AttributeError: 'Series' object has no 
attribute 'fake'
在 Python 中使用 Dask 進行平行程式設計

使用延遲物件的方法

import pandas as pd

maximums = []

for file in files:
    df = delayed(pd.read_csv)(file)

    max_length = df['duration_ms'].max()
    # Add delayed object to list
    maximums.append(max_length)

# Run delayed max on delayed objects list
absolute_maximum = delayed(max)(maximums)

maximums 是延遲物件的清單

print(maximums)
[Delayed('max-80b...'), 
Delayed('max-fa15d...', 
...]
在 Python 中使用 Dask 進行平行程式設計

計算延遲物件清單

import pandas as pd

maximums = []

for file in files:
    df = delayed(pd.read_csv)(file)

    max_length = df['duration_ms'].max()
    # Add dalayed object to list
    maximums.append(max_length)

# Compute all the maximums
all_maximums = dask.compute(maximums)
print(all_maximums)
([2539418, 4368000, ...
... 4511716, 4864333],)
在 Python 中使用 Dask 進行平行程式設計

計算延遲物件清單

import pandas as pd

maximums = []

for file in files:
    df = delayed(pd.read_csv)(file) 

    max_length = df['duration_ms'].max()

    maximums.append(max_length)

# Compute all the maximums
all_maximums = dask.compute(maximums)[0]
print(all_maximums)
[2539418, 4368000, ...
... 4511716, 4864333]
在 Python 中使用 Dask 進行平行程式設計

該延遲,還是不延遲

def get_max_track(df):
    return df['duration_ms'].max()

for file in files:
    df = delayed(pd.read_csv)(file) 
    # Use function to find max
    max_length = get_max_track(df)

    maximums.append(max_length)


absolute_maximum = delayed(max)(maximums)
在 Python 中使用 Dask 進行平行程式設計

更深入的任務圖

absolute_maximum.visualize()

一個任務圖,描述跨檔案計算整體最大值的步驟。任務圖很大但結構簡單。

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

一起來練習吧!

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

Preparing Video For Download...