-
Notifications
You must be signed in to change notification settings - Fork 49
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
define_unit override argument #239
base: main
Are you sure you want to change the base?
Conversation
…y. Not nearly a complete PR since I'm not fully sure this is desirable beyond me (and possibly I should be doing this another way...)
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.
Hi @rjfarber
This looks like a reasonable request to me and your implementation is sound. I'll take a deeper look tomorrow to make sure this is the simplest way to do it (but it seems likely).
For now I just have a couple minor suggestions:
force_override
orallow_override
would make slightly more sense to me as an argument name, becauseoverride=True
reads as something that's never a no-op.- I'd like that any new arguments was keyword only, because it's much easier to maintain for us in the future.
So I don't think there's any documented/supported way to override pre-defined units at the moment. If you want to pursue this, feel free to. Most importantly we would need some documentation and tests. |
So the Lines 199 to 229 in 10ab887
I'm not sure if this does what you want it to or not. |
Oh I missed that, thanks for pointing it out !
|
I guess I have a question about the use case--@rjfarber, if you messed up the unit, is there a reason we don't just fix the unit in the script and then re-run? Or is there a reason we should start with an incorrect value and then change it? I'm only a bit hesitant to introduce this functionality because in normal cases we actually don't want to redefine units--a kpc is going to be a kpc, and if the standard changes then we change the hard-coded value instead. However, maybe I am not understanding the use case well enough. |
Hi John,
Regarding use case, if you're running in a jupyter notebook and already
have a decent amount of data loaded (can take minutes to load) it's rather
annoying to shutdown the script, reload the data, etc. Really slows down
the development process. And I ran into this for dealing with
non-dimensional units (cloud radii, cloud mass etc) - which is a problem
for dimensionalized codes like FLASH as much as for code-unit codes like
Athena.
It does sound like UnitRegistry.modify can handle this case, but I agree
with Clément that I think it'd be nicer to have it all under define_unit.
Although I'll definitely to the judgement of you two and the other
long-time yt devs here :)
Regarding making it too easy to change units, well if the user decides to
use a "force_override" method hopefully they know what they're doing.
Personally I'm a fan of giving the user absolute power to `sudo rm -rf /`
vs. coddling the user, but that gets philosophical I'll admit :)
Also sorry everyone for being MIA! I'll plan to return to this tomorrow :)
Best,
--------
Ryan
…On Sat, Jul 16, 2022 at 2:40 PM John ZuHone ***@***.***> wrote:
I guess I have a question about the use ***@***.***
<https://github.com/rjfarber>, if you messed up the unit, is there a
reason we don't just fix the unit in the script and then re-run? Or is
there a reason we should start with an incorrect value and then change it?
I'm only a bit hesitant to introduce this functionality because in normal
cases we actually don't want to redefine units--a kpc is going to be a kpc,
and if the standard changes then we change the hard-coded value instead.
UnitRegistry.modify is there mainly for code units. I worry making it too
easy to change units could lead to unpredictable behavior.
However, maybe I am not understanding the use case well enough.
—
Reply to this email directly, view it on GitHub
<#239 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGNJQVZ2ZNCVDPFHDSNYPPTVUKUVFANCNFSM53ATGXSA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Furthermore, giving that power to users is perfectly in line with how |
@rjfarber @neutrinoceros ok, so I am now convinced of the utility of this (and thanks for the specific Jupyter example, I totally get it). so I actually think what would be preferable in this situation is a new function, What do you guys think? One issue that might be tricky to solve (as I discovered recently myself) is making sure that when you modify a unit it propagates to the units that you can get from the top level, i.e. I'm happy to take a crack at this if you're interested, or you can feel free to do so! |
TBH I'm not a fan, it'd be less discoverable and even if you know about it, you'd need to change your code to run it again.
Indeed this sounds like a major challenge, regardless the approach. |
@rjfarber I haven't forgotten about this, sorry for the delay. I think in a couple of weeks we should try getting it merged in |
Okay cool! Did you and Clément come to a decision/understanding about a
"allow_override" argument vs. modify_unit new function regarding
implementation?
Best,
--------
Ryan
…On Wed, Sep 7, 2022 at 3:15 AM John ZuHone ***@***.***> wrote:
@rjfarber <https://github.com/rjfarber> I haven't forgotten about this,
sorry for the delay. I think in a couple of weeks we should try getting it
merged in
—
Reply to this email directly, view it on GitHub
<#239 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGNJQV5CX3HMYFG7DP44FX3V47UBJANCNFSM53ATGXSA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
We haven't discussed it in other channels. I'm still inclined to think an |
@rjfarber so I've come around to this idea--but I would like to see a couple of things (potentially) added here:
|
Agreed that adding a logger for this seems like a long shot, and a plain print statement would be terrible style, but what's wrong with a warning ? If you fear this would be too noisy, I can see two ways we could add a warning and keep it "quiet" in most cases:
|
Yup, I'll definitely add to the docs!
Regarding adding some indication to the user that an override is taking
place, I agree a print statement would not be ideal. I'm cool with
Clément's suggestion of using a warning. I guess you both will be horrified
to hear this, but one my favorite stackoverflow answers is:
I don't condone it, but you could just *suppress all warnings* with this:
import warnings
warnings.filterwarnings("ignore")
(https://stackoverflow.com/questions/14463277/how-to-disable-python-warnings
)
Best,
--------
Ryan
…On Wed, Sep 14, 2022 at 9:46 PM Clément Robert ***@***.***> wrote:
we don't have a logger, and I am hesitant to add one, especially since we
don't have more use cases. And we shouldn't use a print statement or issue
a warning either. So not sure if there is a good way to log this, open to
suggestions.
Agreed that adding a logger for this seems like a long shot, and a plain
print statement would be terrible style, but what's wrong with a warning ?
If you fear this would be too noisy, I can see two ways we could add a
warning and keep it "quiet" in most cases:
- we could subclass (or use) builtin ResourceWarning, which aren't
showed by default but will be treated as errors in CI here and downstream,
greatly reducing the risk that this ends up in production code by accident
https://docs.python.org/3/library/exceptions.html#ResourceWarning
- or we could add *another* option to silence the warning. I must say
I'm not a fan of this option.
—
Reply to this email directly, view it on GitHub
<#239 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGNJQV7YXZQ76AYK7RDPAE3V6ITPPANCNFSM53ATGXSA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
First of all: 😱 |
…the official test_define_unit.py one. Modified docstring and removed an if in define_unit. Commiting to push and then reinstall to test.
Don't worry I mostly do that because of matplotlib I think 😅
Anyway, I made modifications to the define_unit docstring and now I
understand John's concern better I think - since I guess the define_unit
was intended as creating brand new units rather than modifying them, so I
had to change the wording kind of repetitively.
I thought this modification had worked in my original use case, but I'm not
sure where that script is. And when trying to write a non-Jupyter example
(to add to the examples in the docstring), it doesn't seem like my unit
actually gets updated 🤔
Although also the tests/define_unit_test.py is failing now on the "Test
custom registry" part.
Just thought I'd update you all that I'm back at this! Hopefully I can
figure the above problems out with fresh eyes tomorrow :)
Best,
--------
Ryan
…On Thu, Sep 15, 2022 at 12:18 PM Clément Robert ***@***.***> wrote:
I guess you both will be horrified
to hear this, but one my favorite stackoverflow answers is:
I don't condone it, but you could just *suppress all warnings* with this:
First of all: 😱
Second of all: we're well aware that's a thing, and we'd prefer to not put
our users in a position where they would need or want this.
—
Reply to this email directly, view it on GitHub
<#239 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGNJQV63CZXUSZ6JHFLC6O3V6LZXNANCNFSM53ATGXSA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
I'm not sure this is fully desirable (or maybe this capability exists and I'm just missing it) but I'm currently in the situation where I messed up a custom defined unit and wanted to simply override the definition (similar to how derived_fields can be redefined).
So I added an optional argument in
def define_unit(...,override=False)
and don't trigger the raise if override is True. I have to run to a meeting but thought I might as well kick off this discussion rather than spend a lot of time on making this a complete PR right now in case this is not desirable.Anyway happy Friday!