Source code for obiwan.qa.visual

"""
Helper functions for plotting fits and numpy 2D arrays as images
"""

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle,Wedge
from matplotlib.collections import PatchCollection
import os
import skimage.io
import fitsio

[docs]class plotImage(object): """Helper functions for displaying image and overlaying circles around sources Args: img: need to give as initial input b/c some helper funcs that dont directly use img, need its shape at least, see circles() """ def imshow(self,img,ax,qs=[0.5,99.5]): if (img.shape[-1] == 3) | (img.shape[0] == 3): #minmax=np.percentile(np.sum(img,axis=2),q=qs) minmax=[None,None] cmap=None elif qs is None: minmax=[None,None] cmap='gray' else: minmax=np.percentile(img,q=qs) cmap='gray' ax.imshow(img, interpolation='none', origin='lower', cmap=cmap,vmin=minmax[0],vmax=minmax[1]) ax.tick_params(direction='out')
[docs] def circles(self,xs,ys,ax, img_shape=None, xslice=None,yslice=None, r_pixels=5./0.262,color='y'): """ xs,ys: x,y positions of sources in pixels, e.g. tractor.bx or simcat.x img_shape: needed when xslice or yslice is None xlice,yslice: slice() objects into the image array r_pixels: radius circles in pixels """ if (xslice is None) | (yslice is None): assert(not img_shape is None) if xslice is None: xslice= slice(0,img_shape[0]) if yslice is None: yslice= slice(0,img_shape[1]) keep= self.justInSlice(xs,ys,xslice,yslice) xpos,ypos= xs[keep]-xslice.start,ys[keep]-yslice.start dr= r_pixels/ 20 patches=[Wedge((x, y), r_pixels + dr, 0, 360,dr) for x,y in zip(xpos, ypos) ] coll = PatchCollection(patches, color=color) #,alpha=1) ax.add_collection(coll)
[docs] def justInSlice(self,x,y,xslice,yslice): """Returns bool array of x,y positions in the slice()""" return ((x >= xslice.start) & (x <= xslice.stop) & (y >= yslice.start) & (y <= yslice.stop))
[docs]def readImage(fn,jpeg=False,ext=1): """Reads FITS and jpeg images so that x,y indices refer to the same pixels regardless of image format. x,y and fits correspond so the jpeg is rotated and flipped to align with fits Args: fn: image filename jpeg: bool, is is a jpeg? """ if jpeg: img= skimage.io.imread(fn) for i in range(3): img[:,:,i]= np.rot90(img[:,:,i].T,1) else: img= fitsio.FITS(fn)[ext].read() return img
[docs]def sliceImage(img, xslice=slice(None,None),yslice=slice(None,None)): """Not sure why, but simcat.x[xslice],simcat.y[yslice] corresponds to img[yslice,xslice], eg inverted for the image""" return img[yslice,xslice,...]
def flux2mag(flux): return -2.5*np.log10(1e-9 * flux)