Replies: 5 comments 8 replies
-
@mate377 And I get that you want to convert a Parameters object to (and perhaps from?) a pandas Dataframe, but I don't (yet?) know why anyone else wants that or why that should be done within lmfit.
How do they seem identical to you? A DataFrame is a pretty generic table of values, typically with uniform datatypes for columns and a number of rows for "instances". Apparently, a lot of people like to use these. A lmfit.Parameters object contains a number of individual Parameter objects, each of which has a name and value, and a handful of attributes, with a variety of data types (str, float, bool, dict), though some of these may not be set to a valid or clear value at all times. An individual Parameter must be mutable. The set of individual Parameter objects in a Parameters object can have an arbitrary order, but that order must be preserved throughout the lifetime of the Parameters object. A Parameters object also contains an instance of an One can certainly serialize a Parameters object (we do that using JSON), but I'm not certain how serialization to a DataFrame would work. For completeness, you need not only the attributes of the individual Parameter objects, but the set of user-defined symbols in the
I don't know that this difference is important. How would having the same flexibility as a DataFrame help a Parameters object? What replaces the flexibility of being an inspectable Python object?
Perhaps something like
I'm not sure I see how that would work.
What math would that be?
Well, you have a structured Parameters object with a set of accessible attributes. I can certainly believe that "dump to" a DataFrame could work, but less sure about "load from" a DataFrame in a way that is reliable. |
Beta Was this translation helpful? Give feedback.
-
I think the combination of lmfit with pandas can be rather useful. A common workflow of mine is where I have serveral spectra and fit them all with the same model. Then I put the results into a dataframe to compare the different fit results, since it is much more handy than just using dict. This way, it is easy to add depenend columns or additional info about the different spectra. Also plotting is simplyfies. Here a reallife example i use to put the data into a dataframe: # fit_dict is a dict containing the ModelResults of different fits. The key is the sample name.
fit_vals = defaultdict(list)
for r in fit_dict:
fit_vals['name'].append(r)
for p in fit_dict[r].params:
fit_vals[p].append(fit_dict[r].params[p].value)
for p in fit_dict[r].params:
if fit_dict[r].ci_out is not None and p in fit_dict[r].ci_out:
fit_vals[p + '_err'].append((fit_dict[r].ci_out[p][2][1] -
fit_dict[r].ci_out[p][0][1])/2)
fit_vals[p + '_err_u'].append(fit_dict[r].ci_out[p][2][1])
fit_vals[p + '_err_l'].append(fit_dict[r].ci_out[p][0][1])
fit_vals[p + '_err_std'].append(fit_dict[r].ci_out[p][0][1])
else:
fit_vals[p + '_err'].append(fit_dict[r].params[p].stderr)
for p in fit_vals:
fit_vals[p] = np.array(fit_vals[p])
df = pd.DataFrame(fit_vals) I am +0.5 to adding such a function to lmfit, since I think it rather useful in a large number of usecases. On the other hand, it simple to roll your own. I am definitly for adding an example, maybe I can find some time. |
Beta Was this translation helpful? Give feedback.
-
@mate377 @Tillsten I would definitely be ok with adding methods to convert Parameters and/or ModelResult to a tabular format: A table is a fine way to visualize and easily compare results from multiple fits. I'm a little less sure about having a single Parameters or single ModelResult as a table - what sort of work would be done on that? Is CSV or Pandas DataFrame a better choice for a "tabular" format? |
Beta Was this translation helpful? Give feedback.
-
@mate377
OK, I generally associate "representation" with something more than "what is displayed". Because, like displayed where and how?
Well, that seems like a fine thing to do, but I don't really see any math there. And Till's code added in other results, say from This is kind of the heart of my concern here. You can certainly convert Parameters to DataFrames -- that's a fine choice, though certainly not the only one. Anyway, it is not very complicated, and might look something like: import pandas as pd
def params_to_dataframe(params):
"convert parametes to a dataframe"
par_attrs = ('name', 'value', 'stderr', 'vary', 'expr', 'init_value',
'min', 'max', 'brute_step', 'correl')
dat = {attr: [] for attr in par_attrs}
for par in params.values():
for attr in par_attrs:
dat[attr].append(getattr(par, attr, None))
return pd.DataFrame(dat, columns=par_attrs) I think you are saying that you want us to maintain that as a You might have a list (or a dict or a sqlite database) of fit results using And you want to compare the various fits - perfectly reasonable . But what do want to display and sort on? Maybe you just want parameter name, value, stderr. Maybe you also need dataset name. Maybe you need a name or index for the different models and/or starting values. Do you always sneed the starting values? Do you care about The code above is basically what As it turns out, we're working to release version 1.1.0 very soon (like Monday or Tuesday). The above code could go in, but we need to hear a resounding yes very, very soon (approximately "right now") for that to happen. |
Beta Was this translation helpful? Give feedback.
-
@newville @mate377 this is something worth thinking about - even though I am not really sold on the idea. For sure it should not be pushed quickly without thinking it through so that it possibly could go into the next release. Our plan is (or should be) to release minor point releases more quickly so this is a perfect thing to go in later if we would decide to do so. |
Beta Was this translation helpful? Give feedback.
-
Hi,
the representation of the params object seems to me identical to a pandas DataFrame, however the params object doesn't have the same flexibility as a DataFrame.
With the params in a DataFrame one could for example:
All this would be accessible from a method "to_pandas()" or "to_dataframe" in the same way as other libraries like xarray. I think it would simplify the work of those not stopping at the "fit report" but going ahead with more analysis!
Beta Was this translation helpful? Give feedback.
All reactions