Help researchers by exploring cool packages written in Python.
Learn how to write functions that handle units.
Roy Hyunjin Han
When applying mathematics to the real world, it is important to be mindful of the units for each quantity.
20170605-2000 - 20170605-2100: 1 hour estimated
20170605-2000 - 20170605-2130: 1.5 hours actual
pint
package.pint
package.pint
package.20170605-2000 - 20170605-2030: 30 minutes
import pip
pip.main(['install', 'pint'])
+ Make sure the `pint` package is installed
I would really like to use Jupyter with Vim key bindings next time.
Katy Huff mentioned the pint
package in her keynote at PyCon 2017.
I took some time to work through the documentation. The package is written by Hernan E. Grecco, who is a professor and researcher at the University of Buenos Aires in Argentina.
20170605-2030 - 20170605-2100: 30 minutes
Let's look at why this package is cool.
from pint import UnitRegistry
u = UnitRegistry()
It comes predefined with a lot of units.
' '.join(dir(u))
distance = 10 * u.kilometer
print(distance)
print(distance + 500 * u.meter)
print(distance * distance)
print(distance.to(u.mile))
print(distance.to(u.foot))
Use ito
for in-place conversions.
distance.ito(u.mile)
distance
Parse strings.
Q = u.Quantity
Q('1 km/s').to(u.mile / u.hour)
Format strings.
x = Q('100 km/hr ** 2').to(u.mile / u.hour ** 2)
print('{:P}'.format(x)) # Pretty Print
print('{:L}'.format(x)) # LaTeX
print('{:H}'.format(x)) # HTML
Use arrays.
[[2, 3], [4, 5]] * u.lb + [[6, 7], [8, 9]] * u.kg
Define functions.
help(u.wraps)
@u.wraps(u.kilowatt_hour, u.hour)
def compute_kwh(time_in_hours):
return time_in_hours * 10
compute_kwh(Q('180 minutes'))
@u.wraps(u.meter / u.second, (u.meter, u.meter, u.second))
def compute_velocity(distance1, distance2, time_interval):
return (distance2 - distance1) / time_interval
compute_velocity(Q('5 meters'), Q('1 mile'), Q('1 minute'))
+ List which aspects of the package we would like to feature
+ Write an expression
+ Write a function
20170605-2100 - 20170605-2130: 30 minutes
The documentation notes that one cannot combine objects that are initialized using different UnitRegistry
instances.
ValueError: Cannot operate with Quantity and Quantity of different registries.
Thus it is important to use a single UnitRegistry
for an entire application.
Use set_application_registry
when unpickling unit-aware objects to ensure that all objects are instantiated from the same UnitRegistry
class.
from pint import set_application_registry
set_application_registry(u)
I think we can design a simple CrossCompute tool that converts units. What is a unit conversion that I do frequently? Well for now, let's just convert temperatures.
+ Write a tool
We made a rough and fun tool. We should eventually let people deploy the tool directly.
What are some exercises that people could do to practice using this package?
# Write an expression that adds three different units of time
# Write a function that takes the number of years and returns the number of hours
# Add a decorator using u.wraps that sets the following units:
# initial_velocity in meters per second
# final_velocity in meters per second
# time_interval in seconds
# acceleration in meters per second squared
def compute_acceleration(
initial_velocity, final_velocity, time_interval):
return (final_velocity - initial_velocity) / time_interval
x = compute_acceleration(
Q('0.5 km/hr'), Q('5 mile/second'), Q('7 microseconds'))
# assert x.magnitude == 1149511587.3015873
+ Create some exercises so that people can practice