emcee with Model interface, version error with version 3.1.4

26 views
Skip to first unread message

peja...@gmail.com

unread,
Dec 16, 2023, 10:59:32 AM12/16/23
to lmfit-py
Hi all, I am kind of new with lmfit, so it may be trivial but despite my best efforts I can’t fix my issue. I tried to reproduce what is shown here

I use Spyder (Python 3.9) in Anaconda on a Mac and I have defined a model with

def Barrett_model(x, C, T0, T1, eps_cst):
    return eps_cst + C*1e4 / ( (T1/2) * np.cosh( T1/(2*x) )/np.sinh( T1/(2*x) ) - T0 )
bmodel = Model(Barrett_model)


I defined the parameters and carried out the fit with 

params = bmodel.make_params(C={'value': 6, 'min':1e-10, 'vary':True}, T1={'value': 51.39, 'min':1e-10, 'vary':True}, T0={'value': 11.80, 'min':1e-10, 'vary':True}, eps_cst={'value': 0.1, 'vary':True})
resultB_STO = bmodel.fit(y, params=params, x=x, weights=None, method='leastsq', iter_cb=None, scale_covar=True, verbose=False, fit_kws=None, nan_policy=None, calc_covar=True, max_nfev=None, coerce_farray=True)


So far, so good. I then try to use emcee with (copied and pasted from here, changing the variables for the parameters and model):

emcee_kws = dict(steps=5000, burn=500, thin=20, is_weighted=False,
                 progress=False)
emcee_params = resultB_STO.params.copy()
emcee_params.add('__lnsigma', value=np.log(0.1), min=np.log(0.001), max=np.log(2.0))
result_emcee = bmodel.fit(data=data1['epsr_STO'], x=data1['T(K)'], params=emcee_params, method='emcee’, nan_policy='omit', fit_kws=emcee_kws)


and I get an error about the emcee version. The full Traceback  is :

  Cell In[45], line 5
    result_emcee = bmodel.fit(data=data1['epsr_STO'], x=data1['T(K)'], params=emcee_params, method='emcee',

  File /opt/anaconda3/lib/python3.9/site-packages/lmfit/model.py:1103 in fit
    output.fit(data=data, weights=weights)

  File /opt/anaconda3/lib/python3.9/site-packages/lmfit/model.py:1467 in fit
    _ret = self.minimize(method=self.method)

  File /opt/anaconda3/lib/python3.9/site-packages/lmfit/minimizer.py:2345 in minimize
    return function(**kwargs)

  File /opt/anaconda3/lib/python3.9/site-packages/lmfit/minimizer.py:1279 in emcee
    raise NotImplementedError('emcee version 3 is required.')


NotImplementedError: emcee version 3 is required.

However, in the Spyder console, when I check the emcee version I get:
In[46]: emcee.__version__
Out[46]: '3.1.4'


I have read this post on the mailing list and run the following commands: 

import sys, lmfit, numpy, scipy, asteval, emcee, uncertainties
print(f"Python: {sys.version}\n\nlmfit: {lmfit.__version__}, scipy: {scipy.__version__}, numpy: {numpy.__version__}, asteval: {asteval.__version__}, uncertainties: {uncertainties.__version__}, emcee: {emcee.__version__}")
Python: 3.9.13 (main, Oct 13 2022, 16:12:30)
[Clang 12.0.0 ]

It does show that the emcee version is >3 :

lmfit: 1.2.2, scipy: 1.7.1, numpy: 1.23.5, asteval: 0.9.27, uncertainties: 3.1.7, emcee: 3.1.4

When I open a terminal on my Mac and run 


(base) me@me-MacBook-Pro ~ % python

Python 3.9.13 (main, Oct 13 2022, 16:12:30) 

[Clang 12.0.0 ] :: Anaconda, Inc. on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> import sys, lmfit, numpy, scipy, asteval, emcee, uncertainties

print(f"Python: {sys.version}\n\nlmfit: {lmfit.__version__}, scipy: {scipy.__version__}, numpy: {numpy.__version__}, asteval: {asteval.__version__}, uncertainties: {uncertainties.__version__}, emcee: {emcee.__version__}")

/opt/anaconda3/lib/python3.9/site-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.16.5 and <1.23.0 is required for this version of SciPy (detected version 1.23.5

  warnings.warn(f"A NumPy version >={np_minversion} and <{np_maxversion}"

>>> print(f"Python: {sys.version}\n\nlmfit: {lmfit.__version__}, scipy: {scipy.__version__}, numpy: {numpy.__version__}, asteval: {asteval.__version__}, uncertainties: {uncertainties.__version__}, emcee: {emcee.__version__}")

Python: 3.9.13 (main, Oct 13 2022, 16:12:30) 

[Clang 12.0.0 ]


lmfit: 1.2.2, scipy: 1.7.1, numpy: 1.23.5, asteval: 0.9.27, uncertainties: 3.1.7, emcee: 3.1.4


So the emcee version appears >3. Any idea what is going on and how I can fix it?
Thanks a lot for your help!

Matt Newville

unread,
Dec 16, 2023, 3:26:18 PM12/16/23
to lmfi...@googlegroups.com
Hi,

That seems very weird to me. I sort of suspect Spyder is not actually
using the right set of packages.

Since you can import emcee from a normal (non-Spyder) Python session,
can you run your fit from there?
> --
> You received this message because you are subscribed to the Google Groups "lmfit-py" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/edc8d060-6a92-4d8f-bea7-6059f83333b2n%40googlegroups.com.



--
--Matt Newville <newville at cars.uchicago.edu> 630-327-7411

peja...@gmail.com

unread,
Dec 17, 2023, 5:31:35 AM12/17/23
to lmfit-py
Hi Matt and thanks for your help (and the package, really really useful!)

I have run it from a terminal and it runs fine. I have therefore installed emcee again (should have updated it instead ...) for conda with 


conda install -c conda-forge emcee


closed and re-opened conda, check that emcee was not in the updatable packages in the anaconda environment where I run Spyder and re-run my program. It works like a charm. It seems indeed to be a Spyder-related issue. 
Thanks a lot for your help, and lmfit !!
Reply all
Reply to author
Forward
0 new messages