[yt-dev] Issue #1074: LaTeX representations of units are not seriailized (yt_analysis/yt)

Nathan Goldbaum issues-reply at bitbucket.org
Wed Aug 26 11:57:19 PDT 2015


New issue 1074: LaTeX representations of units are not seriailized
https://bitbucket.org/yt_analysis/yt/issues/1074/latex-representations-of-units-are-not

Nathan Goldbaum:

This issue is illustrated by the following example (which needs to happen over the course of two python scripts).

save.py:


```
#!python

import yt
ds = yt.load('enzo_tiny_cosmology/DD0000/DD0000')
data = ds.arr([1,2,3], 'Mpccm')
data.write_hdf5('my_data')
```

load.py:


```
#!python

import yt
yt.YTArray.from_hdf5('my_data')
```

This will crash with the following traceback:


```
#!python

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-2-a3d4124ff75c> in <module>()
----> 1 yt.YTArray.from_hdf5('my_data')

/Users/goldbaum/Documents/yt-hg/yt/units/yt_array.pyc in from_hdf5(cls, filename, dataset_name)
    706         f.close()
    707         registry = UnitRegistry(lut=unit_lut, add_default_symbols=False)
--> 708         return cls(data, units, registry=registry)
    709
    710     #

/Users/goldbaum/Documents/yt-hg/yt/units/yt_array.pyc in __new__(cls, input_array, input_units, registry, dtype, bypass_validation)
    356             # don't handle all the cases here, let the Unit class handle if
    357             # it's a str.
--> 358             units = Unit(input_units, registry=registry)
    359
    360         # Attach the units

/Users/goldbaum/Documents/yt-hg/yt/units/unit_object.pyc in __new__(cls, unit_expr, base_value, base_offset, dimensions, registry, **assumptions)
    202         else:
    203             # lookup the unit symbols
--> 204             unit_data = _get_unit_data_from_expr(unit_expr, registry.lut)
    205             base_value = unit_data[0]
    206             dimensions = unit_data[1]

/Users/goldbaum/Documents/yt-hg/yt/units/unit_object.pyc in _get_unit_data_from_expr(unit_expr, unit_symbol_lut)
    476     # Now for the sympy possibilities
    477     if isinstance(unit_expr, Symbol):
--> 478         return _lookup_unit_symbol(str(unit_expr), unit_symbol_lut)
    479
    480     if isinstance(unit_expr, Number):

/Users/goldbaum/Documents/yt-hg/yt/units/unit_object.pyc in _lookup_unit_symbol(symbol_str, unit_symbol_lut)
    539                     sstr = symbol_str
    540                 latex_symbol_lut[symbol_str] = \
--> 541                     latex_symbol_lut[symbol_wo_prefix].replace(
    542                                    '{'+symbol_wo_prefix+'}', '{'+sstr+'}')
    543

KeyError: 'pccm'
```

The issue is that the LaTeX version of symbols is saved in a singleton dictionary (`latex_symbol_lut`) which is not persisted between python sessions. Coming units specifically define a special LaTeX represetation, which is not available in the python session for load.py. Annoyingly, it *would* work if load.py were executed in the same python session as save.py, since the singleton dictionary would be populated with the correct values.

I think the correct thing to do is to store the LaTeX representations of units in the `UnitRegistry` object itself rather than in a global singleton.

Responsible: ngoldbaum



More information about the yt-dev mailing list