2011-02-01 58 views
10

我期待使用Clojure和Incanter來處理大型科學數據集;具體而言,this dataset的0.5度版本(僅適用於二進制格式)。Clojure中的科學數據集操作 - 將ByteBuffers讀入矩陣

我的問題是,你有什麼建議,以優雅的方式來處理Java/Clojure這個問題?是否有一種簡單的方法將這個數據集放入Incanter或其他Java矩陣包中?

我設法使用下面的代碼讀取二進制數據轉換java.nio.ByteBuffer

(defn to-float-array [^String str] 
    (-> (io/to-byte-array (io/to-file str)) 
     java.nio.ByteBuffer/wrap 
     (.order java.nio.ByteOrder/LITTLE_ENDIAN))) 

現在,我真的我怎麼可以開始操作這個ByteBuffer作爲數組掙扎。我一直在使用Python的NumPy,這使得處理這些龐大的數據集變得非常容易。這裏是我想要做的python代碼:

// reshape row vector into (time, lat_slices, lon_slices) 
// then cut out every other row 
rain_data = np.fromfile("path/to/file", dtype="f") 
rain_data = rain_data.reshape(24, 360, 720); 
rain_data = rain_data[0:23:2,:,:]; 

在這個切片後,我想返回這十二個數組的向量。 (我需要將它們分別作爲未來的功能輸入進行操作。)

因此,有關如何將此數據集放入Incanter的任何建議將非常感謝。

+0

你有沒有找到解決這個問題的方法?我發現了一個很漂亮的便利函數,它可以讓你創建一個文件的內存映射字節緩衝區作爲一行代碼:https://github.com/pjstadig/nio(nio.core/mmap「/ path/to/file」) 。我仍然試圖找出如何將這個閱讀成正確形狀的jblas矩陣。 – Peter 2012-10-29 15:51:38

回答

6

我不知道如何將轉換ByteBuffer到一個數組,但這裏的reshape函數的實現:

(defn reshape [v c] 
    (if (= (count v) 1) 
    c 
    (recur (butlast v) 
      (partition (last v) c)))) 

(這在我有限的測試工作正常。)如果你的數據在矢量r那麼你就可以實現

rain_data = rain_data.reshape(24, 360, 720); 

(reshape '(24 360 720) r)