question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

User defined units fail to parse when reading into QTable

See original GitHub issue

I construct a QTable that takes in data as a user defined unit and output it to the current directory.

bandpass_sol_lum = u.def_unit('bandpass_sol_lum') 
lum_array = np.ones((5,5)) * bandpass_sol_lum

qt = QTable()
qt['Luminosity'] = QTable.Column(lum_array) 
qt.write('output_lum.fits',overwrite=True,format='fits')

When I read it back in:

qt_readIn = QTable()
qt_readIn['Luminosity'] = QTable.read('output_lum.fits',format='fits') # <--Exception thrown

I get:

YamlParseError: 'bandpass_sol_lum' did not parse as unit: At col 0, bandpass_sol_lum is not a valid unit. 

Is it possible at all to parse user defined units when reading them back in from a FITS file? @zpace

My environment: macOS 10.14.3 python 3.6.8 astropy 3.2 numpy 1.16.4

[Edit]: To address this issue, read the comments below and add the example to the documentation as discussed.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
CSwiggcommented, Jun 21, 2019

I could make the documentation changes if that’s alright.

2reactions
mhvkcommented, Jun 21, 2019

Fortunately, it is easier than that! The trick is to tell the whole system about the existence of this new unit using u.add_enabled_units(bandpass_sol_lum):

import numpy as np
from astropy import units as u
from astropy.table import QTable

bandpass_sol_lum = u.def_unit('bandpass_sol_lum') 
lum_array = np.ones((5,5)) * bandpass_sol_lum
qt = QTable()
qt['Luminosity'] = lum_array 
qt.write('output_lum.fits',overwrite=True,format='fits')

u.add_enabled_units(bandpass_sol_lum)
QTable.read('output_lum.fits', format='fits')
<QTable length=5>
 Luminosity [5] 
bandpass_sol_lum
    float64     
----------------
      1.0 .. 1.0
      1.0 .. 1.0
      1.0 .. 1.0
      1.0 .. 1.0
      1.0 .. 1.0

One thing that is odd - likely a bug - is that if I add the unit before writing the fits file, I get an error; e.g., if after the above I try rewriting:


WARNING: The unit 'bandpass_sol_lum' could not be saved to FITS format [astropy.io.fits.convenience]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-c1bb5ecc6cf2> in <module>()
----> 1 qt.write('output_lum.fits',overwrite=True,format='fits')

/data/mhvk/venv/numpy-dev/lib/python3.7/site-packages/numpy-1.17.0.dev0+0dcd300-py3.7-linux-x86_64.egg/astropy/table/connect.py in __call__(self, *args, **kwargs)
    112         instance = self._instance
    113         with serialize_method_as(instance, serialize_method):
--> 114             registry.write(instance, *args, **kwargs)

/data/mhvk/venv/numpy-dev/lib/python3.7/site-packages/numpy-1.17.0.dev0+0dcd300-py3.7-linux-x86_64.egg/astropy/io/registry.py in write(data, format, *args, **kwargs)
    564 
    565     writer = get_writer(format, data.__class__)
--> 566     writer(data, *args, **kwargs)
    567 
    568 

/data/mhvk/venv/numpy-dev/lib/python3.7/site-packages/numpy-1.17.0.dev0+0dcd300-py3.7-linux-x86_64.egg/astropy/io/fits/connect.py in write_table_fits(input, output, overwrite)
    385     input = _encode_mixins(input)
    386 
--> 387     table_hdu = table_to_hdu(input, character_as_bytes=True)
    388 
    389     # Check if output file already exists

/data/mhvk/venv/numpy-dev/lib/python3.7/site-packages/numpy-1.17.0.dev0+0dcd300-py3.7-linux-x86_64.egg/astropy/io/fits/convenience.py in table_to_hdu(table, character_as_bytes)
    530 
    531             # Try creating a Unit to issue a warning if the unit is not FITS compliant
--> 532             Unit(col.unit, format='fits', parse_strict='warn')
    533 
    534     # Column-specific override keywords for coordinate columns

/data/mhvk/venv/numpy-dev/lib/python3.7/site-packages/numpy-1.17.0.dev0+0dcd300-py3.7-linux-x86_64.egg/astropy/units/core.py in __call__(self, s, represents, format, namespace, doc, parse_strict)
   1843 
   1844         elif s is None:
-> 1845             raise TypeError("None is not a valid Unit")
   1846 
   1847         else:

TypeError: None is not a valid Unit

This is weird. And separate from your issue, so let’s make it a new one: #8887

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unified File Read/Write Interface — Astropy v5.2
Reading and writing image data in the unified I/O interface is supported though ... To use this interface, first import the Table class,...
Read more >
Using quantities to parse data with units and errors - r-spatial
The problem of reading and parsing quantities with errors and units depends on the reporting scheme used. Let us consider errors first.
Read more >
Advanced Queuing & PL/SQL Notification - Ask TOM
Every example I read take a record type as payload parameter, when I replicate them but use a varray type as payload it...
Read more >
Model/View Tutorial | Qt Widgets 6.4.1
A table widget is a 2D array of the data elements that the user can change. The table widget can be integrated into...
Read more >
Let's Write a Simple JPEG Library, Part-II: The Decoder
The JPEG compression algorithm: After reading the encoded data from the JFIF file, it has to replay the steps of encoding in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found