2017-01-06 24 views
1

我正在尋找一些幫助/建議使用數據繪製器來繪製一個大的二維數據陣列作爲一系列的點,由振幅着色。我處理的數據存放在幾個2D HDF5數據集中,時間索引存儲在一個單獨的數據集中。數據的第二個維度是空間維度(以米爲單位的距離),這是一個非均勻的階梯式浮點系列。 數據集通常非常大(〜1000 x> 1000000),所以我想要處理構建的核心數據框,其中數據的y位置存儲爲列標題, x-location是框架索引,我想將這些點顏色映射到數據值 當我想從dask數據框將其繪製在數據瀏覽器中時,我遇到了問題,目前,我發現的唯一方法是平整數據框並創建兩個對應的'x'和'y'列以容納索引和y位置。 任何人都可以幫助我理解這種繪圖是否可能,而沒有將數據展平的步驟?Python Datashader繪製大的二維點陣列

這是我迄今所做的例子:

import datashader as ds 
import datashader.transfer_functions as tf 
import numpy as np 
import pandas as pd 
import dask.dataframe as dd 
import dask.array as da 

import bokeh.plotting as bk 
from bokeh.palettes import viridis 

from datashader.bokeh_ext import InteractiveImage 

bk.output_notebook() 

# ------------------------ 
# This is a proxy for a function, which creates a delayed frame from 
# a series of delayed pandas dataframes, each reading from a separate 
# h5 dataset. 
random_data = da.random.random((10000, 1000), chunks = (1000, 100)) 
frame = dd.from_array(random_data) 
# ------------------------ 

# ------------------------ 
# Flatten the dataframe and create two additional arrays holding the x and y 
# locations. 
a = frame.compute() # I want to avoid this call on the whole dataframe 
index = [a.index] * len(a.columns) 
index = np.vstack(index).reshape((-1), order = 'F') 
columns = [a.columns] * len(a.index) 
columns = [item for sublist in columns for item in sublist] 
data = a.values.flatten() 

# ------------------------ 
# Now creating an in-memory frame for the data 
plot_frame = pd.DataFrame(columns = ['x', 'y', 'z']) # Empty frame 
plot_frame.x = index 
plot_frame.y = columns[::-1] #Reverse column order to plot 
plot_frame.z = data 
# ------------------------ 

x_range = [a.index[0], a.index[-1]] 
y_range = [a.columns[0], a.columns[-1]] 

def create_image(x_range = x_range, y_range = y_range[::-1], w=500, h=500): 
    cvs = ds.Canvas(x_range=x_range, y_range=y_range, plot_height=h, plot_width=w) 
    agg = cvs.points(plot_frame, 'x', 'y', ds.mean('z')) 
    return tf.shade(agg, cmap = viridis(256)) 

def base_plot(tools='pan,wheel_zoom,reset, box_zoom, save'): 
    p = bk.figure(x_range = x_range, y_range = y_range, tools=tools, 
        plot_width=900, plot_height=500, outline_line_color=None, 
     min_border=0, min_border_left=0, min_border_right=0, 
     min_border_top=0, min_border_bottom=0, x_axis_type = 'datetime') 
    p.xgrid.grid_line_color = None 
    p.ygrid.grid_line_color = None 
    return p 

p = base_plot() 
InteractiveImage(p, create_image) 

任何人都可以推薦通過datashader管道更有效地處理這一種方法?

提前致謝!

回答

0

我的迴應只是爲了讓你知道你的問題已經被datashader維護者看到了,但不幸的是,我不知道最好的方法來完成你所要求的。正如您在OSM example in datashader中看到的那樣,使用dask + datashader,核外數據集已經可以很好地工作,但在這種情況下,數據首先被放入合適的塊和列尋址格式(castra最初,但現在我個人推薦fastparquet根據我的benchmarking)。在這裏,您似乎試圖將原始組織保留在磁盤上,同時也使它看起來像一個平坦的數據框,我不確定如何實現。你可以考慮直接詢問dask維護者;這絕對不是數據管理團隊目前正在研究的內容。

+0

謝謝詹姆斯,我很欣賞這個反饋。我一直在生根,並得出類似的結論。我正在查看'canvas.raster'方法,並試圖查看是否可以通過一個dask數組和兩個索引數組來傳遞變量,然後將'resize'調用映射到數組上,以及是否有類似這樣的想法可能工作?或者我誤解了(非常合理的)柵格方法? –

+0

我猜這樣的東西應該可以工作,但我不認爲它會出現在Canvas.raster中,這是關於對已經柵格化的數據進行重新柵格化,而在這裏您需要使用datashader從點數據執行柵格化。 –