我0.12.0
在 Ubuntu 上使用 Pandas 版本13.04
。我正在尝试创建一个 5D 面板对象来包含一些按条件拆分的 EEG 数据。
我如何选择构建我的数据:
让我首先展示我对pandas.core.panelnd.creat_nd_panel_factory
.
Subject = panelnd.create_nd_panel_factory(
klass_name='Subject',
axis_orders=['setsize', 'location', 'vfield', 'channels', 'samples'],
axis_slices={'labels': 'location',
'items': 'vfield',
'major_axis': 'major_axis',
'minor_axis': 'minor_axis'},
slicer=pd.Panel4D,
axis_aliases={'ss': 'setsize',
'loc': 'location',
'vf': 'vfield',
'major': 'major_axis',
'minor': 'minor_axis'}
# stat_axis=2 # dafuq is this?
)
本质上,组织如下:
setsize
:一个实验条件,可以是1
或2
location
:实验条件,可以是"same"
,"diff"
或None
vfield
:一个实验条件,可以是"lvf"
或“rvf
”
最后两个轴对应于DataFrame
amajor_axis
和minor_axis
。为了清楚起见,它们已重命名:
channels
:列,EEG 通道(其中 129 个)samples
:行,单个样本。samples
可以作为时间轴。
我正在尝试做的事情:
每个实验条件 ( subject
x setsize
x location
x vfield
) 都存储在它自己的制表符分隔文件中,我正在读取该文件pandas.read_table
,获取一个DataFrame
对象。我想为每个主题创建一个 5 维面板(即Subject
),其中将包含该主题的所有实验条件(即DataFrame
s)。
首先,我正在为每个 subject/ 构建一个嵌套字典Subject
:
# ... do some boring stuff to get the text files, etc...
for _, factors in df.iterrows():
# `factors` is a 4-tuple containing
# (subject number, setsize, location, vfield,
# and path to the tab-delimited file).
sn, ss, loc, vf, path = factors
eeg = pd.read_table(path, sep='\t', names=range(1, 129) + ['ref'], header=None)
# build nested dict
subjects.setdefault(sn, {}).setdefault(ss, {}).setdefault(loc, {})[vf] = eeg
# and now attempt to build `Subject`
for sn, d in subjects.iteritems():
subjects[sn] = Subject(d)
完整的堆栈跟踪
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-2-831fa603ca8f> in <module>()
----> 1 import_data()
/home/louist/Dropbox/Research/VSTM/scripts/vstmlib.py in import_data()
64
65 import ipdb; ipdb.set_trace()
---> 66 for sn, d in subjects.iteritems():
67 subjects[sn] = Subject(d)
68
/usr/local/lib/python2.7/dist-packages/pandas/core/panelnd.pyc in __init__(self, *args, **kwargs)
65 if 'dtype' not in kwargs:
66 kwargs['dtype'] = None
---> 67 self._init_data(*args, **kwargs)
68 klass.__init__ = __init__
69
/usr/local/lib/python2.7/dist-packages/pandas/core/panel.pyc in _init_data(self, data, copy, dtype, **kwargs)
250 mgr = data
251 elif isinstance(data, dict):
--> 252 mgr = self._init_dict(data, passed_axes, dtype=dtype)
253 copy = False
254 dtype = None
/usr/local/lib/python2.7/dist-packages/pandas/core/panel.pyc in _init_dict(self, data, axes, dtype)
293 raxes = [self._extract_axis(self, data, axis=i)
294 if a is None else a for i, a in enumerate(axes)]
--> 295 raxes_sm = self._extract_axes_for_slice(self, raxes)
296
297 # shallow copy
/usr/local/lib/python2.7/dist-packages/pandas/core/panel.pyc in _extract_axes_for_slice(self, axes)
1477 """ return the slice dictionary for these axes """
1478 return dict([(self._AXIS_SLICEMAP[i], a) for i, a
-> 1479 in zip(self._AXIS_ORDERS[self._AXIS_LEN - len(axes):], axes)])
1480
1481 @staticmethod
KeyError: 'location'
我知道这panelnd
是一个实验性功能,但我相当肯定我做错了什么。有人可以指出我正确的方向吗?如果它是一个错误,有什么可以解决的吗?
像往常一样,提前非常感谢您!