【发布时间】:2018-10-25 20:26:42
【问题描述】:
我正在尝试运行一堆(电力系统)模拟并将所有结果保存到字典中。这是数据组织:
由于我没有那么复杂的对象结构,我决定使用 dill 来存储包含一堆字典的字典(每个字典的键都包含一个类)
import dill as pickle
class Results():
def __init__(self):
self.volt = []
self.angle = []
self.freq = []
def save_obj(obj, name ):
# save as pickle object
currentdir = os.getcwd()
objDir = currentdir + '/obj'
if not os.path.isdir(objDir):
os.mkdir(objDir)
with open(objDir+ '/' + name + '.pkl', 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL,recurse = 'True')
EventDict = {}
########### conceptual code to get all the data
# simList is a list of approximately 7200 events
for event in simList:
ResultsDict = {}
for element in network: # 24 elements in network (23 buses,or nodes, and time)
# code to get voltage, angle and frequency (each of which is a list of 1200 elements)
if element == 'time':
ResultsDict['time'] = element
else:
ResultsDict[element] = Results()
ResultsDict[element].volt = element.volt
ResultsDict[element].angle = element.angle
ResultsDict[element].freq = element.freq
EventDict[event] = ResultsDict
save_obj(EventDict,'EventData')
生成的 pickle 对象就像 5 gigs,当我尝试加载时,我收到以下错误,说它内存不足:
Traceback (most recent call last):
File "combineEventPkl.py", line 39, in <module>
EventDict = load_obj(objStr)
File "combineEventPkl.py", line 8, in load_obj
return pickle.load(f)
File "C:\Python27\lib\site-packages\dill\_dill.py", line 304, in load
obj = pik.load()
File "C:\Python27\lib\pickle.py", line 864, in load
dispatch[key](self)
File "C:\Python27\lib\pickle.py", line 964, in load_binfloat
self.append(unpack('>d', self.read(8))[0])
MemoryError
no mem for new parser
MemoryError
另外,在我得到这个回溯之前,unpickling 需要很长时间。 我意识到这个问题是因为 EventDict 很大。 所以,我想我在问是否有更好的方法来存储这样的时间序列数据,具有一些用键标记每个数据的功能,以便我知道它代表什么?我愿意接受除了 pickle 以外的其他建议,只要它加载速度快并且在加载到 python 时不需要太多努力。
【问题讨论】: