Exemple #1
0
def make_iraf_slicemap(filename, N, savename=None):
    if len(filename) + len('[*,*,]') + len(str(N)) > 23:
        warn('Filenames + slice is larger that 23 characters! ==>  IRAF will truncate theses '
             'filenames when writing to database.  YES, IRAF SUCKS!!!' )
    slicemap = lambda i: '%s[*,*,%i]'%(filename, i)
    slices =list( map(slicemap, range(1,N+1)) )
    if not savename is None:
        np.savetxt( str(savename), slices, fmt='%s' )
    return list(slices)
Exemple #2
0
def WindowOutlierDetection(data, nwindow, noverlap, method, weight_kernel=None, 
                           return_index=True, return_mask=False, return_masked_data=False,
                           *args, **kwargs): #recur=None
    
    noverlap = Spectral.get_overlap(nwindow, noverlap)
    
    N = data.shape[-1]
    if N<nwindow:
        warn( 'Data length smaller than window size! No clipping done!')
        return data.mask
        
    step = nwindow - noverlap
    noc = Div.get_nocc(N, nwindow, noverlap)

    if weight_kernel is None:
        weight_kernel = 'boxcar'
    weights = get_window( weight_kernel, nwindow )

    S = defaultdict(int)
    #q = Div.div(data, nwindow, noverlap)
    
    #if data.ndim>1:
        #q = q.transpose(1,0,2) #swap the axes around so we can enumerate easily 
    
    #if np.ma.is_masked(data):
        #embed()
    
    for i, dat in enumerate( Div.div(data, nwindow, noverlap) ):
        widx = method(dat.T, *args, **kwargs)  #indeces of outliers relative to this window
        if len(widx):
            didx = i*step + np.array(widx)     #indeces relative to data
            didx = didx[didx<N]                #remove indeces that exceed array dimensions
            for ii,jj in zip(widx, didx):
                S[jj] += weights[ii] / noc[jj]  #mean probability that points where flagged as outliers

    IDX = np.sort( [idx for idx,p in S.items() if p>0.5] )
    
    if return_index:
        return IDX
    
    if  return_mask:
        mask = np.zeros(data.shape, bool)
        if len(IDX):
            mask[IDX] = True
        return mask
    
    if return_masked_data:
        if np.ma.isMA(data):
            data.mask[IDX] = True
        else:
            mask = np.zeros_like(data, bool)
            mask[IDX] = True
            data = np.ma.masked_where(mask, data)

        return data
Exemple #3
0
 def __init__(self, data, title=None, title_props=(),
                 col_headers=None, col_head_props='bold', col_widths=None, col_borders='|', col_sort=None,
                 row_headers=None, row_head_props='bold', where_row_borders=None, row_sort=None,
                 align='left', num_prec=2, enumera=False,
                 ignore_keys=None  ):
     '''
     Class to create and optionally colourise tabular representation of data for terminal output.
     Parameters
     ----------
     data            :       input data - must be 1D, 2D or dict
                             if dict, keys will be used as row_headers, and values as data
     title           :       The table title
     title_props     :       str, tuple or dict with ANSICodes property descriptors
     
     col_headers     :       column headers.
     col_head_props  :       str, tuple or dict with ANSICodes property descriptors to use as global column header properties
                             TODO: OR a sequence of these, one for each column
     col_widths      :       numerical column width
     col_borders     :       character used as column seperator
     col_sort        :       TODO callable that operates on strings and returns column sorting order
     
     row_headers     :
     row_head_props  :       see col_head_props
     where_row_borders:      sequence with row numbers below which a solid border will be drawn.  
                             Default is after column headers and after last line
     row_sort        :       TODO callable that operates on strings and returns row sorting order
     
     align           :       column alignment - ('left', 'right', or 'center')
     num_prec        :       integer precision to use for number representation FIXME!!!!!!!!!!!!!
     enumera         :       bool
         Will number the rows if True
     
     ignore_keys     :       if dictionary is passed as data, optionally specify the keys that will not be printed in table 
     '''
     
     #self.datatypes = np.vectorize(type)(data)
     self.original_data = data
     
     if isinstance(data, dict):
         if not row_headers is None:
             warn( "Dictionary keys will be superceded by 'row_headers'." )
         row_headers, data = self.convert_dict( data, ignore_keys )
     
     #convert to array of SuperStrings
     self.data = as_superstrings( data )
     
     #check data shape
     dim = np.ndim(self.data)
     if dim == 1:
         self.data = self.data[None].T               #default for 1D data is to display in a column with row_headers
     if dim > 2:
         raise ValueError( 'Only 2D data can be tabelised!  Data is {}D'.format(dim) )
     
     #title
     self.title = title
     self.title_props =  title_props
     
     self.enumera = enumera
     
     #row and column headers
     self.has_row_head = not row_headers is None
     self.has_col_head = not col_headers is None
     
     if self.has_col_head:
         self.col_headers = self.apply_props(col_headers, col_head_props)
     if self.has_col_head and self.has_row_head:
         #TODO:  when both are given, the 0,0 table position is ambiguously both column and row header
         #TODO:  allow user to specify either
         if len(row_headers) == self.data.shape[0]:
             row_headers = [''] + list(row_headers)
     if self.has_row_head:
         Nrows = len(row_headers)
         self.row_headers = self.apply_props(row_headers, row_head_props).reshape(Nrows,1)
     
     self.pre_table = self.add_headers()
     Nrows, Ncols = self.pre_table.shape
     
     #Column specs
     if col_widths is None:
         self.col_widths = np.vectorize(len)( self.pre_table ).max(axis=0) + 1
         self.col_widths_no_ansi = np.vectorize(SuperString.len_no_ansi)( self.pre_table ).max(axis=0) + 1
     else:
         self.col_widths = col_widths
     
     self.col_borders = col_borders
     
     #column alignment
     almap = { 'r' : '>', 'l' : '<', 'c' : '^' }     #map to alignment characters
     if align.lower()[0] in almap:
         self.align = almap[ align.lower()[0] ]
     else:
         raise ValueError( 'Unrecognised alignment {!r}'.format(align) )
     
     #The column format specification. 0 - item; 1 - fill; 2 - alignment character
     self.col_fmt = '{0:{2}{1}}'     
     self.num_prec = num_prec
     
     #Row specs
     self.rows = []
     self.row_fmt = ('{}'+self.col_borders) * Ncols
     
     if where_row_borders:
         self.where_row_borders = where_row_borders
     else:
         if self.has_col_head:
             self.where_row_borders = [0, Nrows-1]
         else:
             self.where_row_borders = [Nrows-1]
Exemple #4
0
    def load_image(self, filename=None, data=None, **kw):
        """
        Load the image for display, applying IRAF's zscale algorithm for colour limits.
        """

        print("Loading image {}".format(filename))

        ##### Load the image #####
        if filename and data is None:
            self.filename = filename
            FF = FITSFrame(filename)
            self.image_data = data = FF[1]
            self.image_header = FF.master_header

            # get readout noise and saturation if available
            self.ron = self.image_header.get("ron")
            try:  # Only works for SHOC data
                self.saturation = RNT.get_saturation(
                    filename
                )  # NOTE: THIS MEANS RNT NEEDS TO BE INSTANTIATED BEFORE StarSelector!!!!!!!!
            except Exception as err:
                warn("Error in retrieving saturation value:\n" + str(err))
                self.saturation = "INDEF"

        elif not data is None:
            self.image_data = data
            self.ron = self.saturation = None
        else:
            raise ValueError("Please provide either a FITS filename, or the actual data")

        # if kw.get( 'fliplr' , False ):
        if self.image_header["flipx"]:
            data = np.fliplr(data)

        # if WCS:            data = np.fliplr( data )
        self.image_data_cleaned = data.copy()
        r, c = self.image_shape = data.shape
        self.pixels = np.mgrid[:r, :c]  # the pixel grid

        ##### Plot star field #####
        # zilms = zrange(data, sigma_clip=5., maxiter=5, contrast=1./99)
        zlims = np.percentile(data, (0.25, 99.75))

        fig, ax = self.figure, self.ax
        self.canvas = self.figure.canvas
        self.image = ax.imshow(data, origin="lower", cmap="gist_heat", vmin=zlims[0], vmax=zlims[1])  # gist_earth, hot
        # ax.set_title( 'PSF Measure' )

        # TODO: #PRINT TARGET NAME AND COORDINATES ON IMAGE!! DATE, TIME, ETC.....
        # TODO: Axes Sliders
        # TODO: Tight colourbar + switchable maps
        self.colour_bar = fig.colorbar(self.image, ax=ax)
        # self.stars.zlims = zlims
        # box = ax.get_position()
        # ax.set_position([box.x0, box.y0 + box.height * 0.1,
        # box.width, box.height * 0.9])

        # create the canvas backgrounds for blitting
        # canvas = FigureCanvas(self.figure)
        # self.backgrounds = [canvas.copy_from_bbox( ax.bbox ) for ax in fig.axes]

        self.snap = Snapper(
            self.image_data_cleaned,
            window=self.window,
            snap="peak",
            edge="shift",
            noise_threshold=2.5,
            offset_tolerance=3,
        )

        self.psf = self.PSFClass()
        self.fit = StarFit(self.psf, hints=False)
        self.fitplotter = psfPlotFactory(self.plot_fits)()

        ##### Add ApertureCollection to axes #####
        self.stars = Stars(self.idx, window=self.window)
        self.stars.axadd(ax)

        # initialize model plots
        self.stars.model.init_plots(fig)