Unit Conversion in Python

前端 未结 9 1368
清歌不尽
清歌不尽 2020-12-07 11:05

I\'m working on a project that lets users track different data types over time. Part of the base idea is that a user should be able to enter data using any units that they n

相关标签:
9条回答
  • 2020-12-07 11:45

    You may want to look at a new package called natu. It addresses the three issues that @ChristopherBruns listed. It's available in PyPI.

    I'm the author of that package, and I'd appreciate any comments or suggestions.

    0 讨论(0)
  • 2020-12-07 11:45

    Thought to mention the units package which is part of the Astropy package.

    It's well maintained, easy to use, and has all the basic units (as well as astrophysics-related units). It provides tools for both units and quantities. And there's also a module for physical constants.

    0 讨论(0)
  • 2020-12-07 11:46

    Note that quantities has very bad support for temperature:

    >>> (100 * pq.degC).rescale(pq.degF)
    array(179.99999999999997) * degF
    >>> (0 * pq.degC).rescale(pq.degF)
    array(0.0) * degF
    

    0 degrees Celsius isn't 0 degrees Fahrenheit. Their framework doesn't support any kind of conversion that isn't just multiplying by a factor.

    0 讨论(0)
  • 2020-12-07 11:56

    I think you should use quantities, because a quantity has some units associated with it.

    Pressure, for example, will be a quantity that could be entered and converted in and to different units (Pa, psi, atm, etc). Probably you could create new quantities specifics for your application.

    0 讨论(0)
  • 2020-12-07 11:58

    I applaud use of explicit units in scientific computing applications. Using explicit units is analogous brushing your teeth. It adds some tedium up front, but the type safety you get can save a lot of trouble in the long run. Like, say, not crashing $125 million orbiters into planets.

    You should also probably check out these two other python unit/quantity packages:

    Unum

    Scientific.Physics.PhysicalQuantity

    I once investigated Scientific.Physics.PhysicalQuantity. It did not quite meet my needs, but might satisfy yours. It's hard to tell what features you need from your brief description.

    I ended up writing my own python package for unit conversion and dimensional analysis, but it is not properly packaged for release yet. We are using my unit system in the python bindings for our OpenMM system for GPU accelerated molecular mechanics. You can browse the svn repository of my python units code at:

    SimTK python units

    Eventually I intend to package it for distribution. If you find it interesting, please let me know. That might motivate me to package it up sooner. The features I was looking for when I was designing the SimTK python units system included the following:

    1. Units are NOT necessarily stored in terms of SI units internally. This is very important for me, because one important application area for us is at the molecular scale. Using SI units internally can lead to exponent overflow in commonly used molecular force calculations. Internally, all unit systems are equally fundamental in SimTK.
    2. I wanted similar power and flexibility to the Boost.Units system in C++. Both because I am familiar with that system, and because it was designed under the scrutiny of a large group of brilliant engineers. Boost.Units is a well crafted second generation dimensional analysis system. Thus I might argue that the SimTK units system is a third generation system :). Be aware that while Boost.Units is a "zero overhead" system with no runtime cost, all python quantity implementations, including SimTK units, probably exact a runtime cost.
    3. I want dimensioned Quantities that are compatible with numpy arrays, but do not necessarily require the python numpy package. In other words, Quantities can be based on either numpy arrays or on built in python types.

    What features are important to you?

    0 讨论(0)
  • 2020-12-07 12:01

    Pint has recently come onto the field. Anybody care to share their experiences? Looks good. FYI: It looks like Pint will be integrated with Uncertainties in the near future.

    0 讨论(0)
提交回复
热议问题