2012-05-31 67 views
2

我有一個文件頭值的字典(時間,幀數,年份,月份等),我想寫入一個numpy數組。我目前代碼如下:從字典中寫入numpy數組

arr=np.array([(k,)+v for k,v in fileheader.iteritems()],dtype=["a3,a,i4,i4,i4,i4,f8,i4,i4,i4,i4,i4,i4,a10,a26,a33,a235,i4,i4,i4,i4,i4,i4"]) 

但我得到一個錯誤,「只能串聯元組(未‘INT’)到元組

基本上,最終結果需要是陣列存儲整個文件頭信息(它是512字節)和每個幀的數據(頭和數據,每幀49408字節)。有沒有更簡單的方法來做到這一點?

編輯:澄清(爲我自己) ,我需要將數據從文件的每一幀寫入一個數組,我給出了matlab代碼作爲基礎,下面是給出的代碼的一個大概的概念:

data.frame=zeros([512 96]) 
frame=uint8(fread(fid,[data.numbeams,512]),'uint8')) 
data.frame=frame 

如何將「frame」轉換爲python?

+0

你的錯誤無關,與numpy的。它來自'[(k,)+ v中的'(k,)+ v',fileheader.iteritems()]中的v。這聽起來像你想要使用鍵的名稱作爲numpy數組中的記錄名稱?如果是這樣,您需要構建dtype以使用這些名稱。另外請記住,'字典是無序的,這可能會導致你現在寫的東西出現問題。 –

+0

謝謝!我怎樣才能把所有的值以有序的格式? (我基本上沒有python經驗) –

回答

2

你可能會更好的只是保持頭文件數據字典。你真的需要它作爲一個數組嗎? (如果是這樣,爲什麼呢?有一個優點,那就是將標題放在一個數組中,但它比簡單的dict更復雜,並且不那麼靈活。)

dict的一個缺點是沒有可預測性爲了它的鑰匙。如果您需要按常規順序將您的標題寫回到磁盤(類似於C結構),則需要單獨存儲字段順序及其值。如果是這樣的話,你可以考慮一個有序的字典(collections.OrderedDict)或者只是把一個簡單的類放在一起來保存你的標題數據並在那裏存儲訂單。

除非有充分的理由將它放入一個numpy數組中,否則您可能不想。

但是,結構化數組將保留您的頭的順序,並且可以更容易地將它的二進制表示寫入磁盤,但在其他方面不靈活。

如果你確實想使頭一個數組,你會做這樣的事情:

import numpy as np 

# Lists can be modified, but preserve order. That's important in this case. 
names = ['Name1', 'Name2', 'Name3'] 
# It's "S3" instead of "a3" for a string field in numpy, by the way 
formats = ['S3', 'i4', 'f8'] 

# It's often cleaner to specify the dtype this way instead of as a giant string 
dtype = dict(names=names, formats=formats) 

# This won't preserve the order we're specifying things in!! 
# If we iterate through it, things may be in any order. 
header = dict(Name1='abc', Name2=456, Name3=3.45) 

# Therefore, we'll be sure to pass things in in order... 
# Also, np.array will expect a tuple instead of a list for a structured array... 
values = tuple(header[name] for name in names) 
header_array = np.array(values, dtype=dtype) 

# We can access field in the array like this... 
print header_array['Name2'] 

# And dump it to disk (similar to a C struct) with 
header_array.tofile('test.dat') 

在另一方面,如果你只是想訪問該值在頭,只是不停地作爲dict。這樣更簡單。


基於聽起來像你在做什麼,我會做這樣的事情。我使用numpy數組來讀取標題,但標題值實際上是作爲類屬性(以及標題數組)存儲的。

這看起來比實際上更復雜。

我只是定義了兩個新類,一個用於父文件,另一個用於框架。你可以用少一點的代碼來做同樣的事情,但是這給了你更復雜事物的基礎。

import numpy as np 

class SonarFile(object): 
    # These define the format of the file header 
    header_fields = ('num_frames', 'name1', 'name2', 'name3') 
    header_formats = ('i4', 'f4', 'S10', '>I4') 

    def __init__(self, filename): 
     self.infile = open(filename, 'r') 
     dtype = dict(names=self.header_fields, formats=self.header_formats) 

     # Read in the header as a numpy array (count=1 is important here!) 
     self.header = np.fromfile(self.infile, dtype=dtype, count=1) 

     # Store the position so we can "rewind" to the end of the header 
     self.header_length = self.infile.tell() 

     # You may or may not want to do this (If the field names can have 
     # spaces, it's a bad idea). It will allow you to access things with 
     # sonar_file.Name1 instead of sonar_file.header['Name1'], though. 
     for field in self.header_fields: 
      setattr(self, field, self.header[field]) 

    # __iter__ is a special function that defines what should happen when we 
    # try to iterate through an instance of this class. 
    def __iter__(self): 
     """Iterate through each frame in the dataset.""" 
     # Rewind to the end of the file header 
     self.infile.seek(self.header_length) 

     # Iterate through frames... 
     for _ in range(self.num_frames): 
      yield Frame(self.infile) 

    def close(self): 
     self.infile.close() 

class Frame(object): 
    header_fields = ('width', 'height', 'name') 
    header_formats = ('i4', 'i4', 'S20') 
    data_format = 'f4' 

    def __init__(self, infile): 
     dtype = dict(names=self.header_fields, formats=self.header_formats) 
     self.header = np.fromfile(infile, dtype=dtype, count=1) 

     # See discussion above... 
     for field in self.header_fields: 
      setattr(self, field, self.header[field]) 

     # I'm assuming that the size of the frame is in the frame header... 
     ncols, nrows = self.width, self.height 

     # Read the data in 
     self.data = np.fromfile(infile, self.data_format, count=ncols * nrows) 

     # And reshape it into a 2d array. 
     # I'm assuming C-order, instead of Fortran order. 
     # If it's fortran order, just do "data.reshape((ncols, nrows)).T" 
     self.data = self.data.reshape((nrows, ncols)) 

你會用它與此類似:

dataset = SonarFile('input.dat') 

for frame in dataset: 
    im = frame.data 
    # Do something... 
+0

那麼,我想頭部信息不需要在一個數組中。但是,我確實需要數組中的幀信息來創建圖像。在這裏忍受着我 - 我被拋到了最深處,並負責翻譯matlab代碼以處理數據圖像。我知道以下內容:文件頭是512字節,每個幀的大小是49408字節,其中256個是幀頭,編寫matlab代碼的人設置了一個初始的零維數組[512,96](這是一個96波束的聲納)。我需要處理每個文件的每個幀。 –

+0

您是否需要將其寫回原始文件格式? –

+0

否定的,我想最終將最終數據導出到.txt文件中。現在,我需要讀入每個文件和相關的圖像數據;它的格式是二進制的。我們最終的結果(拍攝線路)是將這些圖像文件(通過聲納相機收集)自動定位在聲納光束中(用於可視化,實質上是黑色背景上的白色圓圈)。如果這有意義,我需要最終將檢測到的目標的座標位置存儲在一個文件中。非常感謝你的幫助,我是一個初學者! –

1

問題似乎是vint而不是tuple。嘗試:

arr=np.array([(k,v) for k,v in fileheader.iteritems()],dtype=["a3,a,i4,i4,i4,i4,f8,i4,i4,i4,i4,i4,i4,a10,a26,a33,a235,i4,i4,i4,i4,i4,i4"]) 
+0

'k'將會是一個字符串,在這裏,給出數組的dtype沒有任何意義。 OP可能只是想'v',但我不確定.... –

+0

我從fileheader中的變量獲得了每個dtype ... a3是3字符串文件類型,a是版本,i4是int32幀數等等。文件中的一些頭文件對應於字符串,一些是浮點數,大多數是32位整數。這有幫助嗎? –