-
Notifications
You must be signed in to change notification settings - Fork 277
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
Fix deepcopy of user data #837
Conversation
a12e395
to
d835112
Compare
exp_attr_values_A = ('a', 10.0, True, -100.0, 100.0, None, 5.0, 1) | ||
exp_attr_values_B = ('b', 20.0, False, -250.0, 250.0, "2.0*a", 25.0, 2.5) | ||
exp_attr_values_B = ('b', 20.0, False, -250.0, 250.0, "2.0*a", 25.0, {'test': 123}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the purpose of this change - why would one want to make things fail when user_data
is a dictionary? Or am I missing something here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this change, the test covers a case that was not covered before, where the user data is a mutable object such as a dict or list. See the example code in the PR: modifying the user data of the copied params2
modifies the user data of the original params
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you mean. In the current test "user_data" was an int/float value in both cases, replacing one with a more complicated type makes sense!
@@ -109,7 +109,7 @@ def __deepcopy__(self, memo): | |||
param.correl = par.correl | |||
param.init_value = par.init_value | |||
param.expr = par.expr | |||
param.user_data = par.user_data | |||
param.user_data = deepcopy(par.user_data) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a real issue with doing this, but why is it explicitly needed here and not for any of the other attributes - that doesn't make complete sense to me, but admittedly I haven't looked carefully yet...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am quite new to lmfit, so I'm not sure which of the other attributes are mutable types, maybe correl
is for example. I agree that it looks a bit asymmetric, but this at least fixes this bug.
@mgunyho @reneeotten Thanks - I do agree that
|
That seems reasonable. I am not familiar with the internals of |
@mgunyho @reneeotten will merge and update #844 |
Description
I was making use of the
user_data
attribute of aParameter
, and noticed that it is not deep-copied when using thecopy()
method ofParameters
. Here is a simple example:This PR fixes this behavior.
Type of Changes
Tested on
Python: 3.10.9 (main, Dec 08 2022, 14:49:06) [GCC]
lmfit: 1.1.0.post1+g1a3b1009, scipy: 1.10.0, numpy: 1.24.1, asteval: 0.9.28, uncertainties: 3.1.7
Verification
Have you