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.

Round tripping non-float Quantities from QTable to fits converts all Quantities to floats

See original GitHub issue

Description

Trying to serialise a QTable to and from fits the non-floats are always converted to floats rather then keeping the original dtypes

Expected behavior

The created QTable to have the same dtype as the underling fits column.

Actual behavior

The created QTable has converted the uint and int to float64

Steps to Reproduce

import astropy.units as u
import numpy as np

from astropy.table import QTable
from astropy.io import fits

my_table = QTable()

my_table['uint'] = (list(range(10)) * u.m).astype(np.uint16)
my_table['int'] = (list(range(10)) * u.m).astype(np.int16)
my_table['float32'] = (list(range(10)) * u.m).astype(np.float32)

my_table.dtype
# dtype([('uint', '<u2'), ('int', '<i2'), ('float32', '<f4')])

my_table.write('test.fits')

table_fromfits = QTable.read('test.fits')
table_fromfits.dtype
#dtype([('uint', '<f8'), ('int', '<f8'), ('float32', '>f4')])

A simple but not elegant solution is to use the dtype info from the HDUL to cast the Quantity back to the desired dtype This actually doesn’t work for the uint case or cases with mixed quantities an non-quantities dtype([('uint', '<i2'), ('int', '<i2'), ('float32', '<f4')]) should be dtype([('uint', '<u2'), ('int', '<i2'), ('float32', '<f4')])

hdul = fits.open('test.fits')

hdul[1].data.columns

#ColDefs(
#    name = 'uint'; format = 'I'; unit = 'm'; bzero = 32768
#    name = 'int'; format = 'I'; unit = 'm'
#    name = 'float32'; format = 'E'; unit = 'm'
#)

fixed_table = QTable([u.Quantity(table_fromfits[c.name], dtype=c.dtype) for c in hdul[1].data.columns])

fixed_table.dtype
# dtype([('uint', '<i2'), ('int', '<i2'), ('float32', '<f4')])

System Details

Linux-5.4.0-33-generic-x86_64-with-glibc2.29 Python 3.8.10 (default, Sep 28 2021, 16:10:42) [GCC 9.3.0] Numpy 1.20.2 pyerfa 1.7.3 astropy 4.3.1 Scipy 1.6.3 Matplotlib 3.4.1

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
samaloneycommented, Nov 25, 2021

Sorry for the delay @dhomeier I’ve just loaded some of my fits files using your branch seems to work as expected the dtypes are respected.

1reaction
dhomeiercommented, Nov 19, 2021

@samaloney I have pushed a fix for correctly reading back the mixins in #12505 ; if you have chance to test if this addresses your problems, feedback would be great.

For a temporary workaround, as you noted that your use cases typically involve reading in large volumes of data, I strongly recommend to use getheader and getdata to read the tables in their original dtypes and then extract the units to directly construct u.Quantity(tab[col], dtype=tab[col].dtype, name=col, unit=u.Unit(header.get('TUNITn'), format='fits')). It’s a bit cumbersome, but would avoid conversion losses and in particular the temporary creation of arrays 4x the original size or larger, in particular since the type conversion does not let you take advantage of memmap=True.

Read more comments on GitHub >

github_iconTop Results From Across the Web

QTable not always converting to Quantity? #4497 - GitHub
I've noticed some odd behavior with QTable that seems counter-intuitive, but I'm not sure whether it's a bug or a feature.
Read more >
astropy.io.fits History
Improved round-tripping and preservation of manually assigned column attributes ... FITS files that contain non-float NULL values in an ASCII table but are ......
Read more >
Full Changelog — Astropy v3.2.dev994
Mixin columns like Time and Quantity can now be converted to pandas by flattening the columns ... and round-tripping to the corresponding FITS...
Read more >
Hypothesis Documentation - Read the Docs
The @given decorator then takes our test function and turns it into a ... Returns a strategy that generates complex numbers. ... round-trip....
Read more >
Roadway Delineation Practices Handbook
Physical quantities related to roadway retroreflection measurement . ... All beads float so that half of the bead is exposed regardless of variations....
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