-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Change Cosmology name when parameters change in clone
#11536
Conversation
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
clone
clone
clone
Signed-off-by: Nathaniel Starkman (@nstarman) <nstarkman@protonmail.com>
@@ -157,6 +157,11 @@ def clone(self, **kwargs): | |||
if len(kwargs) == 0: | |||
return self | |||
|
|||
# There are changed parameter values. | |||
# The name needs to be changed accordingly, if it wasn't already. | |||
kwargs.setdefault("name", (self.name + " (modified)" |
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'm curious why you use "setdefault" here since the return value if the function is not used.
kwargs.setdefault("name", (self.name + " (modified)" | |
kwargs['name'] = None if self.name is None else self.name + " (modified)" |
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.
Note that this suggestion can't be applied directly, since the line below will also have to be removed.
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 used setdefault
because it only sets the value if one is not already present. The suggested code doesn't quite work because it doesn't check if kwargs already has "name", in which case it should not be changed. An alternative one-liner is
kwargs["name"] = kwargs.get("name", self.name + " (modified)" if self.name is not None else None)
.
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.
It was mostly just a lazy way of skipping an if-block to make getting that 100% codecov easier. This doesn't need multiple test cases, unlike the equivalent code.
if "name" not in kwargs:
kwargs['name'] = None if self.name is None else self.name + " (modified)"
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.
Great, thank you for the explanation. I did not know about "setdefault" before, I will definitely use that more often in the future myself!
Thanks @hamogu for the review! |
Description
Cloning a cosmology with changed parameters now appends "(modified)" to the
new instance's name, unless a name is explicitly passed to
clone
.The problem can be seen when cloning a built-in cosmology and changing a parameter — eg
Planck18.clone(H0=10)
. This should NOT have the same name because it is not the same thing.