New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CompositeModel depends on return type of model functions #875
Comments
@Julian-Hochhaus Yup, Model functions need to return ndarrays with datatype of Float64. |
But that is not always the case, it still works for using a single function as modelfunction or using mixed return types ( That's a bit confusing. |
@Julian-Hochhaus I view this as similar to the discussion at #873. We have tried to support "array-like" data, such as lists of numbers or several third-party objects (pandas.Series, HDF5 Datasets, xarray, ....) that can be "obviously converted" into numpy ndarrays with dtype Float64. We could try to support these better. Like, in the case here, it would not be too hard to apply But why would a Model function return a list? Why would someone use list comprehension instead of ndarrays and ufuncs? Do we want to permit this poor usage or flag it? For input data as in #873, we could try to store in the On the other hand, we could also just be less permissive. If we're thinking about adding |
Thanks, I get the point. I still find it a bit unsatisfying that it works for single models but not for composite models. One of my colleagues originally had the problem while using |
@Julian-Hochhaus I was hoping that #899 would have helped here, but it doesn't, at least not yet (it gets ~halfway there). But, we will fix this for 1.2.2. |
This should now be fixed in the master branch. A variation of your example was added as a test. |
Description
When using a single model, whether the return type of the model function is a
numpy.ndarray
or alist
does not affect the result. However, when using a CompositeModel that combines two models (e.g. by+
operation), fitting fails if both model functions return alist
. In contrast, if at least one of the model functions returns anumpy.ndarray
, the fit succeeds.The issue arises due to the
_residual()
method of the model class, which throws an error message while calculatingdiff = model - data
. Here, model is the output ofeval()
. The problem appears to be that the length ofmodel
is twice that ofdata
and the output of each model function when using two lists as the return type. It is likely that the bug is caused by appending the lists instead of adding them element-wise during Model evaluation.Using two lists as return:
Error message:
Using at least one
numpy.ndarray
as return:Fit succeeds!
Version information
Python: 3.8.3 (default, Jul 2 2020, 16:21:59)
[GCC 7.3.0]
lmfit: 1.2.1, scipy: 1.10.1, numpy: 1.23.4,asteval: 0.9.29, uncertainties: 3.1.6
If you need any further information, please let me know,
Kind regards,
Julian
The text was updated successfully, but these errors were encountered: