lsst-epo / citizen-science-notebooks

A collection Jupyter notebooks that can be used to associate Rubin Science Platform data to a Zooniverse citizen science project.
3 stars 1 forks source link

Test out the `astropy.visualization` library for converting a FITS to PNG #105

Closed ericdrosas87 closed 2 months ago

ericdrosas87 commented 2 months ago

User story

As a PI using the citizen science pipeline, I want to know if astropy.visualization module is performant, efficient, and thread-safe for converting FITS to PNGs for curating data for my citSci project.

Definition of done

I have tested out the astropy.visualization module and assessed the following areas:

ericdrosas87 commented 2 months ago

I was not able to get the astropy.visualization module to produce a comparable PNG to what lsst.afw.display does, nor using Matplotlib as a rendering tool. I'm closing this ticket out as this does not seem to be a valid option for us.

parejkoj commented 2 months ago

Can you show some of the examples you tried? I'd like it if we could use this as an opportunity to get astropy.visualization to be more functional (there are some astropy Issues related to this), so that we can drop those portions of afw.display.

ericdrosas87 commented 2 months ago

Sorry @parejkoj I was out all last week on vacation and I'm just now getting to this. You can see the example I have on this branch in the 13th cell.

If you want to run it yourself you shouldn't need to run the Zooniverse login cell. Also this branch does not show the various other rendering options I've tried, but basically after curating the data (results)...:

from pyvo.dal.adhoc import DatalinkResults, SodaQuery
dataLinkUrl = results[0].getdataurl()
auth_session = service._session
dl_results = DatalinkResults.from_result_url(dataLinkUrl,
                                             session=auth_session)

f"Datalink status: {dl_results.status}. Datalink service url: {dataLinkUrl}"

sq = SodaQuery.from_resource(dl_results,
                             dl_results.get_adhocservice_by_id("cutout-sync"),
                             session=auth_session)

from lsst.afw.image import ExposureF
from astropy.io import fits

# sphereRadius = 0.05 * u.deg
sphereRadius = 1.0

sq.circle = (spherePoint.getRa().asDegrees() * u.deg,
             spherePoint.getDec().asDegrees() * u.deg,
             sphereRadius)

# first write the file to disk:
cutout_path = f'{batch_dir}soda-cutout-circle.fits'
with open(cutout_path, 'bw') as f:
     f.write(sq.execute_stream().read())

# then read file and extract pixel arr
fits_file = fits.open(cutout_path)
pixel_array = fits_file[1].data

# finally render img
import astropy.visualization as aviz
norm = aviz.ImageNormalize(stretch=aviz.AsinhStretch(0.5))
scaled = norm(pixel_array)
plt.imshow(scaled)
ericdrosas87 commented 2 months ago

The 9th cell - where the FITS is saved to the filesystem - takes about 14 seconds though, which is far too long for what we're looking for (the butler can retrieve the in-memory FITS file in about 3 seconds). Simply reading the FITS into memory takes about 14 seconds as well.