我有非常大的數據集存儲在硬盤上的二進制文件。這裏是文件結構的一個例子:從二進制文件創建Numpy數組的有效方法
文件頭
149 Byte ASCII Header
記錄開始
4 Byte Int - Record Timestamp
樣品開始
2 Byte Int - Data Stream 1 Sample
2 Byte Int - Data Stream 2 Sample
2 Byte Int - Data Stream 3 Sample
2 Byte Int - Data Stream 4 Sample
樣品結束
每個記錄有122,880個樣本和每個文件有713個記錄。這產生了700,910,521字節的總大小。採樣率和記錄數量有時會有所不同,所以我必須編碼以檢測每個文件的每個數量。
目前我使用這個數據導入到陣列中的代碼是這樣的:
from time import clock
from numpy import zeros , int16 , int32 , hstack , array , savez
from struct import unpack
from os.path import getsize
start_time = clock()
file_size = getsize(input_file)
with open(input_file,'rb') as openfile:
input_data = openfile.read()
header = input_data[:149]
record_size = int(header[23:31])
number_of_records = (file_size - 149)/record_size
sample_rate = ((record_size - 4)/4)/2
time_series = zeros(0,dtype=int32)
t_series = zeros(0,dtype=int16)
x_series = zeros(0,dtype=int16)
y_series = zeros(0,dtype=int16)
z_series = zeros(0,dtype=int16)
for record in xrange(number_of_records):
time_stamp = array(unpack('<l' , input_data[ 149 + (record * record_size) : 149 + (record * record_size) + 4 ]) , dtype = int32)
unpacked_record = unpack('<' + str(sample_rate * 4) + 'h' , input_data[ 149 + (record * record_size) + 4 : 149 + ((record + 1) * record_size) ])
record_t = zeros(sample_rate , dtype=int16)
record_x = zeros(sample_rate , dtype=int16)
record_y = zeros(sample_rate , dtype=int16)
record_z = zeros(sample_rate , dtype=int16)
for sample in xrange(sample_rate):
record_t[sample] = unpacked_record[ (sample * 4) + 0 ]
record_x[sample] = unpacked_record[ (sample * 4) + 1 ]
record_y[sample] = unpacked_record[ (sample * 4) + 2 ]
record_z[sample] = unpacked_record[ (sample * 4) + 3 ]
time_series = hstack ((time_series , time_stamp))
t_series = hstack ((t_series , record_t))
x_series = hstack ((x_series , record_x))
y_series = hstack ((y_series , record_y))
z_series = hstack ((z_series , record_z))
savez(output_file, t=t_series , x=x_series ,y=y_series, z=z_series, time=time_series)
end_time = clock()
print 'Total Time',end_time - start_time,'seconds'
目前這需要每個700 MB的文件約250秒,這對我來說似乎是非常高的。有沒有更有效的方法可以做到這一點?
最終解決
使用與自定義的numpy的FROMFILE方法D型運行時切斷至9秒,比上述原始代碼27倍快。最終的代碼如下。
from numpy import savez, dtype , fromfile
from os.path import getsize
from time import clock
start_time = clock()
file_size = getsize(input_file)
openfile = open(input_file,'rb')
header = openfile.read(149)
record_size = int(header[23:31])
number_of_records = (file_size - 149)/record_size
sample_rate = ((record_size - 4)/4)/2
record_dtype = dtype([ ('timestamp' , '<i4') , ('samples' , '<i2' , (sample_rate , 4)) ])
data = fromfile(openfile , dtype = record_dtype , count = number_of_records)
time_series = data['timestamp']
t_series = data['samples'][:,:,0].ravel()
x_series = data['samples'][:,:,1].ravel()
y_series = data['samples'][:,:,2].ravel()
z_series = data['samples'][:,:,3].ravel()
savez(output_file, t=t_series , x=x_series ,y=y_series, z=z_series, fid=time_series)
end_time = clock()
print 'It took',end_time - start_time,'seconds'
它是醫療數據? EDF?如果你不知道我在說什麼,不要介意......; o)無論如何,看看我的答案,根據這個問題,我用它來打開醫療數據二進制文件:http://stackoverflow.com/q/5804052/401828。那裏有一個有趣的討論。 – heltonbiker
不是地球物理數據。在發佈之前我在研究過程中看到了您的問題。您的數據只包含簡短的整數,其中不幸的是整個流中分散了4個字節的整數時間戳。 – Stu
對於它的價值,numpy結構化數組上的許多操作比常規numpy數組慢得多。導入時間可能會更快,但計算時間可能會延長10-100倍:( –