brightway-lca / brightway2-parameters

Library for storing, validating, and calculating with parameters
BSD 3-Clause "New" or "Revised" License
1 stars 2 forks source link

Automatic unit conversion using pint #5

Closed BenPortner closed 1 year ago

BenPortner commented 1 year ago

This PR introduces a new class PintParameterSet, which can solve formulas including units.

The unit parsing is handled by the external pint package, which is included as an optional dependency. To activate, users will have to do pip install bw2parameters[pint]. The new class should not break any existing functionality. Tests for the new class have been included and should be skipped if pint is not installed in the environment. Calls to evaluate return a dictionary with pint.Quantity values.

Example

    from bw2parameters import PintParameterSet
    ps = PintParameterSet({
        'A': {'formula': '1 m'},
        'B': {'formula': 'A + 200 mm'},
        'C': {'formula': 'B * kg/m'},
        'D': {'formula': 'A * B * C'},
    })
    ps.evaluate() 
# {'A': 1 <Unit('meter')>,
# 'B': 1.2 <Unit('meter')>,
# 'C': 1.2 <Unit('kilogram')>,
# 'D': 1.44 <Unit('kilogram * meter ** 2')>}
tngTUDOR commented 1 year ago

👋 @BenPortner

It would be great if you add to the docs of the package an example that uses the pint based parameters you're adding here.

tngTUDOR commented 1 year ago

The pipelines are failing here with py27 @cmutel , would it be safe to remove the py27 support of brightway2-parameters ? I would advice in favor (of removing py27 support) because:

tngTUDOR commented 1 year ago

The pipelines are failing here with py27 @cmutel , would it be safe to remove the py27 support of brightway2-parameters ? I would advice in favor (of removing py27 support) because:

  • There would be a version bump when adding this feature, making it clear that after a certain version, we don't support py27 anymore
  • all other py3x envs pass the tests

also, pint seems to be "python 3.8+" compatible, so maybe, we shouldn't even be testing against python<3.8

BenPortner commented 1 year ago

@tngTUDOR: Updated the docs. @cmutel: waiting for your approval to drop py27 support

cmutel commented 1 year ago

@BenPortner Awesome work! Dropping 2.7 is fine.

BenPortner commented 1 year ago

@cmutel @tngTUDOR @michaelweinold ready to merge in my opinion. please review below changes and give me feedback.

open question:

change log:

cmutel commented 1 year ago

Hey, it looks like you are done making changes for now, but could you quickly confirm?

can appveyor setup be removed as well?

Yes, Github Actions builds on all OSes and it is simpler to maintain one CI.

who will publish on pypi / conda-forge? if you give me access i can handle this.

Conda-forge will generate update PRs automatically, and the maintainers will be notified, once we release on pypi.

I can make you a collaborator on PyPI if you tell me your username or associated email.

BenPortner commented 1 year ago

Hey, it looks like you are done making changes for now, but could you quickly confirm?

Confirmed!

can appveyor setup be removed as well?

Yes, Github Actions builds on all OSes and it is simpler to maintain one CI.

Done.

who will publish on pypi / conda-forge? if you give me access i can handle this.

Conda-forge will generate update PRs automatically, and the maintainers will be notified, once we release on pypi.

I can make you a collaborator on PyPI if you tell me your username or associated email.

I would like to be a collaborator. This is me: https://pypi.org/user/BenPortner/

BenPortner commented 1 year ago

@cmutel @tngTUDOR @michaelweinold

I had to fix PintInterpreter.eval(). The following raised an error before but works now:

from bw2parameters import PintInterpreter
i = PintInterpreter()
i("1 kg + 200 g")
# 1.2 kg
BenPortner commented 1 year ago

Minor changes: