Skip to content
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

Unpin pint to be able to use units supported in more recent version of pint #100

Open
ericpre opened this issue Mar 12, 2024 · 3 comments
Open

Comments

@ericpre
Copy link
Contributor

ericpre commented Mar 12, 2024

It seems that pint is pinned to 0.17 and it would be convenient to unpin it be able to use ppm unit, which was introduced in pint 0.21 https://pint.readthedocs.io/en/stable/changes.html#id3.

@ericpre ericpre changed the title Unpin pint Unpin pint to be able to use units supported in more recent version of pint Mar 12, 2024
@ericpre
Copy link
Contributor Author

ericpre commented Mar 12, 2024

year is another unit that is not supported in that version of pint.

Here is the log showing the error when parsing data with a schema containing the unit um / year:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/nomad/processing/data.py", line 1203, in parsing
    parser.parse(self.mainfile_file.os_path, self._parser_results, logger=logger, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/nomad/parsing/parser.py", line 505, in parse
    self.parse_file(mainfile, f, archive, logger)
  File "/usr/local/lib/python3.9/site-packages/nomad/parsing/parser.py", line 494, in parse_file
    archive.definitions = Package.m_from_dict(
  File "/usr/local/lib/python3.9/site-packages/nomad/metainfo/metainfo.py", line 4120, in m_from_dict
    return super(Package, cls).m_from_dict(data, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/nomad/metainfo/metainfo.py", line 2371, in m_from_dict
    return MSection.from_dict(data, cls=cls, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/nomad/metainfo/metainfo.py", line 2436, in from_dict
    section.m_update_from_dict(dct)
  File "/usr/local/lib/python3.9/site-packages/nomad/metainfo/metainfo.py", line 2322, in m_update_from_dict
    sub_section = sub_section_cls.m_from_dict(
  File "/usr/local/lib/python3.9/site-packages/nomad/metainfo/metainfo.py", line 4007, in m_from_dict
    return super(Section, cls).m_from_dict(data, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/nomad/metainfo/metainfo.py", line 2371, in m_from_dict
    return MSection.from_dict(data, cls=cls, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/nomad/metainfo/metainfo.py", line 2436, in from_dict
    section.m_update_from_dict(dct)
  File "/usr/local/lib/python3.9/site-packages/nomad/metainfo/metainfo.py", line 2322, in m_update_from_dict
    sub_section = sub_section_cls.m_from_dict(
  File "/usr/local/lib/python3.9/site-packages/nomad/metainfo/metainfo.py", line 2371, in m_from_dict
    return MSection.from_dict(data, cls=cls, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/nomad/metainfo/metainfo.py", line 2436, in from_dict
    section.m_update_from_dict(dct)
  File "/usr/local/lib/python3.9/site-packages/nomad/metainfo/metainfo.py", line 2357, in m_update_from_dict
    section.__dict__[property_def.name] = self.__deserialize(section, quantity_def, quantity_value)
  File "/usr/local/lib/python3.9/site-packages/nomad/metainfo/metainfo.py", line 2285, in __deserialize
    return __type_specific_deserialize(quantity_value)
  File "/usr/local/lib/python3.9/site-packages/nomad/metainfo/metainfo.py", line 2280, in __type_specific_deserialize
    return tgt_type.deserialize(section, quantity_def, v)
  File "/usr/local/lib/python3.9/site-packages/nomad/metainfo/metainfo.py", line 347, in deserialize
    value = units.parse_units(value)
  File "/usr/local/lib/python3.9/site-packages/pint/registry.py", line 1093, in parse_units
    units = self._parse_units(input_string, as_delta, case_sensitive)
  File "/usr/local/lib/python3.9/site-packages/pint/registry.py", line 1306, in _parse_units
    return super()._parse_units(input_string, as_delta, case_sensitive)
  File "/usr/local/lib/python3.9/site-packages/pint/registry.py", line 1121, in _parse_units
    cname = self.get_name(name, case_sensitive=case_sensitive)
  File "/usr/local/lib/python3.9/site-packages/pint/registry.py", line 643, in get_name
    raise UndefinedUnitError(name_or_alias)
pint.errors.UndefinedUnitError: 'year' is not defined in the unit registry

@lauri-codes
Copy link
Contributor

lauri-codes commented Mar 13, 2024

Thanks for bringing this up. We can definitely try updating to pint==0.21, especially since it seems to bring support to "%" which will be very useful. I can try the update and see how smoothly it goes: we also have to check that the code we use in the browser to deal with unit transformations works with these new units.

In general, we like to pin many of our dependencies, because we have had so many problems with unpinned libraries breaking our code. But a controlled update of the pinned version is totally fine.

@lauri-codes
Copy link
Contributor

An update: unfortunately pint>=0.21 seems to have broken certain math operations with numpy arrays that have units (see issue here). This is currently preventing us from updating until the problem gets fixed or we refactor several of our parsers to avoid these operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants