Unidata / MetPy

MetPy is a collection of tools in Python for reading, visualizing and performing calculations with weather data.
https://unidata.github.io/MetPy/
BSD 3-Clause "New" or "Revised" License
1.25k stars 416 forks source link

Improve units behavior for new users #175

Closed dopplershift closed 7 years ago

dopplershift commented 8 years ago

Units, while a good way to enforce correctness (especially where, unlike GEMPAK, we're trying to bring in data from many different sources), can really trip up new users and get in the way.

One way to improve things might be to have the calculations (via decorator of course) issue better diagnostics:

  1. Check for units on parameters
  2. Raise exception on missing units which allows us to be more explicit in what's wrong

Another option would be to issue a warning and attach some default units, but I'd have to be convinced--from the zen of Python "in the face of ambiguity, resist the temptation to guess". Functions we add this to would lose the ability to work with unit-free values, but that might be good regardless.

@deeplycloudy, @kpozsonyi any thoughts to add here?

kpozsonyi commented 8 years ago

I feel like attaching default units could be tricky and cause issues, but could also be a good thing as well. I like the idea of checking for units on parameters though, since that is something I personally forget about when using units in functions

deeplycloudy commented 8 years ago

Once I learned how to use the units support and tried it for a meaningful project, I got excited about it and started to use it for peace of mind. But I was initially tripped up by it, too. It's almost entirely without precedent in common practice.

I like the idea of checking and raising. If the decorator were passed the necessary unit hints (@hasunits('length/time', 'length/time'), that error message could dynamically generate instructions that would get the user over the hump, perhaps like so:

Argument 2 to function get_wind_speed requires units with dimensions length/time. Any variable x can be assigned a unit as follows: from metpy.units import units x = x*units.meter/units.second

For more deeply nested function calls, the disconnect between the raising function and the user code could be lengthy, so it won't be possible to instruct the user as to the exact steps, but I bet something lengthly like the above could go a long way.

On Thu, Jun 30, 2016 at 3:51 PM, kpozsonyi notifications@github.com wrote:

I feel like attaching default units could be tricky and cause issues, but could also be a good thing as well. I like the idea of checking for units on parameters though, since that is something I personally forget about when using units in functions

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/metpy/MetPy/issues/175#issuecomment-229785232, or mute the thread https://github.com/notifications/unsubscribe/ABQ6y3qRNQ6hyI1E8N4-qp3jvZWSBSScks5qRCxugaJpZM4JCkCQ .