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

"µm" is not recognized as a unit symbol #34

Closed
mrakitin opened this issue Jul 20, 2018 · 5 comments · Fixed by #59
Closed

"µm" is not recognized as a unit symbol #34

mrakitin opened this issue Jul 20, 2018 · 5 comments · Fixed by #59

Comments

@mrakitin
Copy link

  • unyt version: v1.0.4
  • Python version: Python 3.6.4
  • Operating System: macOS High Sierra 10.13.6 (17G65)

Description

I was trying to convert meters to microns using the unicode symbol "µ", but the library failed to convert it (see the error below), though it works with um.
What you expected to happen: conversion is done successfully!

What I Did

$ ipython
Python 3.6.4 |Anaconda, Inc.| (default, Mar 12 2018, 20:05:31)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import unyt as u

In [2]: d = 1 * u.m

In [3]: d
Out[3]: unyt_quantity(1., 'm')

In [4]: d.to('um')
Out[4]: unyt_quantity(1000000., 'um')

In [5]: d.to('µm')
---------------------------------------------------------------------------
UnitParseError                            Traceback (most recent call last)
<ipython-input-5-7faeac57c1a5> in <module>()
----> 1 d.to('µm')

~/anaconda3/envs/sirepo_bluesky/lib/python3.6/site-packages/unyt/array.py in to(self, units, equivalence, **kwargs)
    828         898755178736817.6 J
    829         """
--> 830         return self.in_units(units, equivalence=equivalence, **kwargs)
    831
    832     def to_value(self, units=None, equivalence=None, **kwargs):

~/anaconda3/envs/sirepo_bluesky/lib/python3.6/site-packages/unyt/array.py in in_units(self, units, equivalence, **kwargs)
    760         898755178736817.6 J
    761         """
--> 762         units = _sanitize_units_convert(units, self.units.registry)
    763         if equivalence is None:
    764             try:

~/anaconda3/envs/sirepo_bluesky/lib/python3.6/site-packages/unyt/array.py in _sanitize_units_convert(possible_units, registry)
    254
    255     # let Unit() try to parse this if it's not already a Unit
--> 256     unit = Unit(possible_units, registry=registry)
    257
    258     return unit

~/anaconda3/envs/sirepo_bluesky/lib/python3.6/site-packages/unyt/unit_object.py in __new__(cls, unit_expr, base_value, base_offset, dimensions, registry, latex_repr)
    301         else:
    302             # lookup the unit symbols
--> 303             unit_data = _get_unit_data_from_expr(unit_expr, registry.lut)
    304             base_value = unit_data[0]
    305             dimensions = unit_data[1]

~/anaconda3/envs/sirepo_bluesky/lib/python3.6/site-packages/unyt/unit_object.py in _get_unit_data_from_expr(unit_expr, unit_symbol_lut)
    862
    863     if isinstance(unit_expr, Symbol):
--> 864         return _lookup_unit_symbol(str(unit_expr), unit_symbol_lut)
    865
    866     if isinstance(unit_expr, Pow):

~/anaconda3/envs/sirepo_bluesky/lib/python3.6/site-packages/unyt/unit_object.py in _lookup_unit_symbol(symbol_str, unit_symbol_lut)
    962     else:
    963         raise UnitParseError("Could not find unit symbol '%s' in the provided "
--> 964                              "symbols." % symbol_str)
    965
    966

UnitParseError: Could not find unit symbol 'µm' in the provided symbols.

In [6]:
@ngoldbaum
Copy link
Member

Agreed it’s worth adding a special case in the parser for this.

@ksunden
Copy link
Contributor

ksunden commented Jul 26, 2018

Note that there are two (identical glyph in most fonts) symbols for mu:

'MICRO SIGN' (U+00B5)
'GREEK SMALL LETTER MU' (U+03BC)

Could these two just be added as aliases in the SI prefix system?

@ksunden
Copy link
Contributor

ksunden commented Jul 26, 2018

'u': 1e-6, # micro

@ngoldbaum
Copy link
Member

Yes, definitely, I just haven't gotten around to doing this. I think it should be as simple as adding two new entries to that table. I have no idea if that would blow up on python2.7 though. Feel free to make it a python3-only feature if supporting python2.7 is a pain.

@ngoldbaum
Copy link
Member

I opened #59 to address this.

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

Successfully merging a pull request may close this issue.

3 participants