-1

I'm trying to train an AI algorithm to determine photometric redshifts of galaxies, and to do so I have a FITS file which contains the training data. I need to convert this FITS file into a format which can be manipulated easily in Python, specifically a numpy array. I have already tried using astropy and followed the below youtube video:

https://www.youtube.com/watch?v=goH9yXu4jWw

however, when I attempt to convert the file and then inspect the data type, it is still a FITS file and not a numpy array. If anyone can help it'd be much appreciated!

import astropy.io
from astropy.io import fits


truth_north = fits.open('dr9_pz_truth_north.fits')

data = truth_north[1].data  

when I then print the data type, it gives astropy.io.fits.fitsrec.FITS_rec

I have been told that the FITS_rec class behaves as a numpy array, however it is essential that I actually convert the file to a numpy array.

NB: I have already posted this question on Physics Stack Exchange, however my question wasn't really answered.

Thank you!

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
Lara
  • 33
  • 4
  • 1
    FITS_rec means your data is tabular data, and nearly equivalent to a NumPy record array. Depending on what you want to do, you can use columns of the FITS data as input for your next steps. Without seeing those next steps, and without knowing the precise data structure of your FITS data, it is hard to give you clear guidance. Yes, you *could* convert your data to a 2D NumPy array, but that would be less meaningful than the current data structure. Also, have a look at https://docs.astropy.org/en/latest/io/fits/index.html#working-with-table-data . – 9769953 Jan 04 '22 at 11:35
  • Following on the above comment, FITS_rec *is* a NumPy array. It's a subclass of `ndarray`, the class of NumPy arrays, with some extra bits specific to FITS tables. When you write that it is "essential that I actually convert the file to a numpy array" what is it you're actually trying to do? – Iguananaut Jan 05 '22 at 19:49

2 Answers2

0

Do you mean you want to get rid of all the metadata and retain only the values in the table? In that case, is this what you are looking for?

data = np.array(truth_north[1].data)
Marco
  • 21
  • 4
0

FITS_rec is a table, but might not cast directly into a numpy array, e.g. if it's a multicolumn table. If you're after the numerical data only, you can extract the data in an astropy table first, then cast the columns into numpy array

# Extract the data in an astropy table
from astropy.table import Table, Column
my_table = Table(truth_north[1].data)

# Extract the names of the columns
colnames = my_table.colnames

# Cast the columns into a numpy array
npdata = np.zeros([len(my_table[colnames[0]), len(colnames)])
for col in range(len(colnames)):
    npdata[:,col] = my_table[colnames[col]]
Oath
  • 67
  • 4