Вам, вероятно, лучше всего держать данные заголовка в dict. Вам действительно нужно это как массив? (Если да, то почему? Есть некоторые преимущества наличия заголовка в массиве numpy, но он более сложный, чем простой dict
, и он не такой гибкий.)
Одним из недостатков dict
является отсутствие предсказуемого по его ключам. Если вам нужно записать свой заголовок обратно на диск в обычном порядке (аналогично C struct), вам нужно отдельно хранить порядок полей, а также их значения. Если это так, вы можете рассмотреть упорядоченный dict (collections.OrderedDict
) или просто собрать простой класс для хранения данных заголовка и сохранения порядка там.
Если есть веская причина поместить его в массив с множеством цифр, вам может и не понадобиться.
Однако структурированный массив сохранит порядок вашего заголовка и упростит запись двоичного представления его на диск, но он негибкий другими способами.
Если вы действительно хотели, чтобы сделать заголовок массив, вы могли бы сделать что-то вроде этого:
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...
Ваша ошибка не имеет ничего общего с NumPy. Он исходит из '(k,) + v' в' [(k,) + v для k, v в fileheader.iteritems()] '. Похоже, вы хотите использовать имена ключей в качестве имен записей в массиве numpy? Если это так, вам нужно создать dtype для использования этих имен. Также имейте в виду, что 'dict's неупорядочены, что может вызвать проблемы с тем, как вы сейчас написали вещи. –
Спасибо! Как я могу поместить все значения в упорядоченный формат? (У меня практически нет опыта python) –