Sử dụng dữ liệu bất kỳ trong Dask bag

Lập trình song song với Dask trong Python

James Fulton

Climate Informatics Researcher

Định dạng dữ liệu hỗn hợp phức tạp

Sơ đồ minh họa tập dữ liệu gồm cả video và âm thanh.

Lập trình song song với Dask trong Python

Tạo Dask bag

import glob

video_filenames = glob.glob("*.mp4")
print(video_filenames)
['me_at_the_zoo.mp4', 'life_goes_on.mp4', 'guitar.mp4', 'hurt.mp4', ...]
Lập trình song song với Dask trong Python

Tạo Dask bag

import glob

video_filenames = glob.glob("*.mp4")
import dask.bag as db

filename_bag = db.from_sequence(video_filenames)
filename_bag.take(1)[0]
'me_at_the_zoo.mp4'
Lập trình song song với Dask trong Python

Nạp dữ liệu tùy chỉnh

# Nạp một video
load_mp4("video.mp4")
{'video': array(
         [[[ 51,  57,  37, ..., 227, 238, 168],
         ...,
         [ 83, 125, 129, ..., 222, 148, 208]]]),
 'audio': array([   7. ,    9. ,    9.5, ..., -544.5, -551. , -558. ]),
 'filename': 'video.mp4'}
Lập trình song song với Dask trong Python

Nạp dữ liệu tùy chỉnh

data_bag = filename_bag.map(load_mp4)
data_bag.take(1)[0]
{'video': array(
         [126, 162, 203, ...,  63,  58,   8],
         ...,
         [ 58, 222, 170, ..., 234,  63,  81]]]),
 'audio': array([-203.5, -209. , -207. , ..., -222.5, -233. , -248.5]),
 'filename': 'me_at_the_zoo.mp4'}
Lập trình song song với Dask trong Python

Nạp dữ liệu tùy chỉnh

data_bag = filename_bag.map(load_mp4)
# Tạo danh sách rỗng
data_list = []

# Thêm các tệp nạp trễ vào danh sách
for file in video_filenames:
    data_list.append(dask.delayed(load_mp4)(file))
Lập trình song song với Dask trong Python

Danh sách đối tượng delayed vs. Dask bag

# Chuyển danh sách đối tượng delayed thành dask bag
data_bag = db.from_delayed(data_list)
# Chuyển dask bag thành danh sách đối tượng delayed
data_list = data_bag.to_delayed()
Lập trình song song với Dask trong Python

Phân tích tiếp theo

transcribed_bag = data_bag.map(transcribe_audio)
transcribed_bag.take(1)[0]
{'video': array(
         [126, 162, 203, ...,  63,  58,   8],
         ...,
         [ 58, 222, 170, ..., 234,  63,  81]]]),
 'audio': array([-203.5, -209. , -207. , ..., -222.5, -233. , -248.5]),
 'filename': 'me_at_the_zoo.mp4'
 'transcript': "All right, so here we are in front of the, uh, elephants ...",
}
Lập trình song song với Dask trong Python

Phân tích tiếp theo

# Áp dụng hàm tùy chỉnh để loại video không có lời nói
clean_bag = transcribed_bag.filter(transcript_is_not_blank)

# Phân tích cảm xúc bản chép lời
sentiment_bag = clean_bag.map(analyze_transcript_sentiment)
# Loại phần tử không cần thiết khỏi bag
keys_to_drop = ['video', 'audio']
final_bag = sentiment_bag.map(filter_dictionary, keys_to_drop=keys_to_drop)

# Chuyển thành Dask DataFrame
df = final_bag.to_dataframe()
Lập trình song song với Dask trong Python

Kết quả

df.compute()
            filename              transcript        sentiment
0  me_at_the_zoo.mp4  All right, so here ...         positive
...              ...                     ...              ...
Lập trình song song với Dask trong Python

Sử dụng tệp .wav

# Nhập mô-đun scipy cho tệp .wav
from scipy.io import wavfile

# Nạp tần số lấy mẫu và mảng âm thanh
sample_freq, audio = wavfile.read(filename)
Lập trình song song với Dask trong Python

Sử dụng tệp .wav

# Mẫu mỗi giây
print(sample_freq)
44100
# Dữ liệu âm thanh
print(audio)
array([ 148,  142,  150, ..., -542, -546, -559], dtype=int16)
Lập trình song song với Dask trong Python

Hãy luyện tập!

Lập trình song song với Dask trong Python

Preparing Video For Download...