问题
我使用pyimfit软件包来拟合星系图像。我想将它们存储在硬盘驱动器上,所以我尝试使用pickle。但是后面的代码给了我错误。
文件“ /home/myusername/PycharmProjects/Master/FitContainer.py”,第56行,保存 pickle.dump(self,f,pickle.HIGHEST_PROTOCOL) pyimfit.pyimfit_lib.ModelObjectWrapper中第2行的文件“ stringsource”。 reduce_cython TypeError:self._fitErrorsVect,self._fitResult,self._model,self._modelFluxes,self._paramVect,self._solverResults无法转换为用于腌制的Python对象
是否有解决此问题的方法?我想到的一种替代方法是,通过将它们存储在字典或类似的替代数据类型中,从而从Imfit类中仅获取一些所需的属性,但我的最初目标是通过将对象直接存储在Container中来存储尽可能多的信息类,这样结构便干净又容易。
其他信息
应该注意,代码给了我
文件“ /home/myusername/anaconda3/lib/python3.7/site-packages/pyimfit/descriptions.py”,第1039行,位于 getattr 返回self._functionSets [0] [attr] TypeError:“ FunctionSetDescription”对象不可下标
之前。因为我认为这应该是(相当新的)模块的错误,所以我注释掉了SimpleModelDescription类的 getattr 方法。再次运行代码给了我第一条错误消息。据我了解,如果我注释掉 getattr ,它应该使用内置的python方法从此类中获取属性,如果我正确解释了第一条错误消息,则应该可以正常工作,而问题出在包的基础C库的Python包装器。
直接腌制SimpleModelDescription类(而不是Container类)会导致相同的错误消息。wra
代码
import pickle
import pyimfit
import numpy as np
class TestContainer:
# class for storing previously fitted images
def __init__(self, fitter):
self.fitter = fitter
def save(self, path):
with open(path, 'wb') as f:
pickle.dump(self, f, pickle.HIGHEST_PROTOCOL)
def sersic(x0, y0, PA, ell, I_e, r_e, n):
# define pyimfit ModelDescription instance
model = pyimfit.SimpleModelDescription()
bulge = pyimfit.make_imfit_function('Sersic', label='bulge')
model.addFunction(bulge)
return model
image = 1e6 * np.ones((150, 150)) # just random image to perform fit on
test_model = sersic(75, 75, 0, 0.5, 1e6, 20, 1.3) # dummy ModelDescription
fitter = pyimfit.Imfit(test_model) # create Imfit instance
fitter.loadData(image) # load image which is to be fitted
fitter.doFit(solver='LM') # perform fit with Levenberg-Marquard algorithm
test_container = TestContainer(fitter) # create container class instance
# save with pickle to ./test.pkl. Here: TypeError
test_container.save('test.pkl')