Example #1
0
def get_roi(template, vct_src, group_by = ''):
    
    # retrieving output specifications from template raster data set
    tpl_ds = gdal.Open(tpl)
    tpl_geo_transform = tpl_ds.GetGeoTransform()
    tpl_x_size, tpl_y_size = tpl_ds.RasterXSize, tpl_ds.RasterYSize
    #tpl_ds = None
    tpl_bd = tpl_ds.GetRasterBand(12)
    tpl_data = tpl_bd.ReadAsArray()

    # opening vector base
    vct_ds = ogr.Open(vct_src)
    vct_ly = vct_ds.GetLayer(0)
    
    polygon_row_cols = dict()
    roi_groups = dict()
    
    for ft in vct_ly:
        fid = ft.GetFID()
        print "Working on FID %d..." % fid
        if group_by:
            if not roi_groups.has_key(group_by):
                roi_groups[group_by] = list()
            roi_groups[group_by].append(fid)
        mem_ds = gdal.GetDriverByName('MEM').Create("", tpl_x_size, tpl_y_size, 1, gdal.GDT_Byte)
        mem_ds.SetGeoTransform(tpl_geo_transform)
        mem_ds.SetProjection(vct_ly.GetSpatialRef().ExportToWkt())

        where_clause = "WHERE '%s' = %i" % ('FID', fid)   
        query = "SELECT * FROM '%s' %s" % (vct_ly.GetName(), where_clause)
        tmp_ds = ogr.Open(vct_src)
        sel_ly = tmp_ds.ExecuteSQL(query)

        gdal.RasterizeLayer(mem_ds, (1,), sel_ly, burn_values = (255,))
        mem_data = mem_ds.GetRasterBand(1).ReadAsArray()

        xxx = ma.masked_where(mem_data != 255, tpl_data)
        mask = ma.masked_where(mem_data != 255, mem_data)

        rows_cols = list()

        shape = list(mask.shape)
        shape.reverse()

        non_mask_indices = ma.flatnotmasked_contiguous(mask)
        for sl in non_mask_indices:
            start_row_col = np.unravel_index(sl.start, tuple(shape), order = 'F')
            stop_row_col = np.unravel_index(sl.stop, tuple(shape), order = 'F')
            #print sl, np.unravel_index(sl.start, mask.shape), np.unravel_index(sl.stop, mask.shape)
            rows_cols.append((start_row_col, stop_row_col))
        else:
            polygon_row_cols[fid] = rows_cols

    return polygon_row_cols, roi_groups
Example #2
0
def axvspan_mask(x,mask):
    """
    Plot these three quantities
   
    x : independent variable
    mask : what ranges are masked out
    """
    
    sL = ma.flatnotmasked_contiguous(ma.masked_array(mask,~mask))
    if sL is not None:
        for s in sL:
            plt.axvspan(x[s.start],x[s.stop-1],color='LightGrey')
Example #3
0
def axvspan_mask(x,mask):
    """
    Plot these three quantities
   
    x : independent variable
    mask : what ranges are masked out
    """
    
    sL = ma.flatnotmasked_contiguous(ma.masked_array(mask,~mask))
    if sL is not None:
        for s in sL:
            plt.axvspan(x[s.start],x[s.stop-1],color='LightGrey')
Example #4
0
def plotspecmodel(w,tspec,mspec,res,mask):
    """
    Plot these three quantities
    
    tspec : target spectrum
    mspec : model spectrum
    res : residuals
    mask : wavelenth mask (plot as regions)
    """
    plt.plot(w,tspec)
    plt.plot(w,mspec)
    plt.plot(w,res)
    
    sL = ma.flatnotmasked_contiguous(ma.masked_array(mask,~mask))
    if sL is not None:
        for s in sL:
            plt.axvspan(w[s.start],w[s.stop-1],color='LightGrey')
    plt.ylim(-0.2,1.2)
    plt.xlim(min(w), max(w))
Example #5
0
def plotspecmodel(w,tspec,mspec,res,mask):
    """
    Plot these three quantities
    
    tspec : target spectrum
    mspec : model spectrum
    res : residuals
    mask : wavelenth mask (plot as regions)
    """
    plt.plot(w,tspec)
    plt.plot(w,mspec)
    plt.plot(w,res)
    
    sL = ma.flatnotmasked_contiguous(ma.masked_array(mask,~mask))
    if sL is not None:
        for s in sL:
            plt.axvspan(w[s.start],w[s.stop-1],color='LightGrey')
    plt.ylim(-0.2,1.2)
    plt.xlim(min(w), max(w))
Example #6
0
def measure(mode, x, y, x0, x1, thresh=0):
    """ return the a measure of y in the window x0 to x1
    """
    xt = x.view(numpy.ndarray)  # strip Metaarray stuff -much faster!
    v = y.view(numpy.ndarray)

    xm = ma.masked_outside(xt, x0, x1).T
    ym = ma.array(v, mask=ma.getmask(xm))
    if mode == 'mean':
        r1 = ma.mean(ym)
        r2 = ma.std(ym)
    if mode == 'max' or mode == 'maximum':
        r1 = ma.max(ym)
        r2 = xm[ma.argmax(ym)]
    if mode == 'min' or mode == 'minimum':
        r1 = ma.min(ym)
        r2 = xm[ma.argmin(ym)]
    if mode == 'median':
        r1 = ma.median(ym)
        r2 = 0
    if mode == 'p2p':  # peak to peak
        r1 = ma.ptp(ym)
        r2 = 0
    if mode == 'std':  # standard deviation
        r1 = ma.std(ym)
        r2 = 0
    if mode == 'var':  # variance
        r1 = ma.var(ym)
        r2 = 0
    if mode == 'cumsum':  # cumulative sum
        r1 = ma.cumsum(ym)  # Note: returns an array
        r2 = 0
    if mode == 'anom':  # anomalies = difference from averge
        r1 = ma.anom(ym)  # returns an array
        r2 = 0
    if mode == 'sum':
        r1 = ma.sum(ym)
        r2 = 0
    if mode == 'area' or mode == 'charge':
        r1 = ma.sum(ym) / (ma.max(xm) - ma.min(xm))
        r2 = 0
    if mode == 'latency':  # return first point that is > threshold
        sm = ma.nonzero(ym > thresh)
        r1 = -1  # use this to indicate no event detected
        r2 = 0
        if ma.count(sm) > 0:
            r1 = sm[0][0]
            r2 = len(sm[0])
    if mode == 'count':
        r1 = ma.count(ym)
        r2 = 0
    if mode == 'maxslope':
        return (0, 0)
        slope = numpy.array([])
        win = ma.flatnotmasked_contiguous(ym)
        st = int(len(win) / 20)  # look over small ranges
        for k in win:  # move through the slope measurementwindow
            tb = range(k - st, k + st)  # get tb array
            newa = numpy.array(self.dat[i][j, thisaxis, tb])
            ppars = numpy.polyfit(
                x[tb], ym[tb],
                1)  # do a linear fit - smooths the slope measures
            slope = numpy.append(slope, ppars[0])  # keep track of max slope
        r1 = numpy.amax(slope)
        r2 = numpy.argmax(slope)
    return (r1, r2)
Example #7
0
    def rasterize_polygon_rois(self, img_src, output=False):
        u"""
        Convert polygons of interests from vector coordinates to pixel
        coordinates in the specified source image.
        """
        # declaring source imagery as template raster data set
        tpl_ds = gdal.Open(img_src)
        tpl_geo_transform = tpl_ds.GetGeoTransform()
        tpl_x_size, tpl_y_size = tpl_ds.RasterXSize, tpl_ds.RasterYSize

        # opening vector roi data source
        roi_ds = ogr.Open(self.roi_src)
        roi_ly = roi_ds.GetLayer(0)

        img_coordinates = dict()

        if output:
            out_ds = gdal.GetDriverByName("MEM").Create("", tpl_x_size, tpl_y_size, 1, gdal.GDT_Int16)
            out_ds.SetGeoTransform(tpl_geo_transform)
            out_ds.SetProjection(roi_ly.GetSpatialRef().ExportToWkt())
            out_data = out_ds.GetRasterBand(1).ReadAsArray()
            out_data[:] = -9999

        for ft in roi_ly:
            fid = ft.GetFID()
            print "Working on FID %d..." % fid
            mem_ds = gdal.GetDriverByName("MEM").Create("", tpl_x_size, tpl_y_size, 1, gdal.GDT_Byte)
            mem_ds.SetGeoTransform(tpl_geo_transform)
            mem_ds.SetProjection(roi_ly.GetSpatialRef().ExportToWkt())

            where_clause = "WHERE '%s' = %i" % ("FID", fid)
            query = "SELECT * FROM '%s' %s" % (roi_ly.GetName(), where_clause)
            tmp_ds = ogr.Open(self.roi_src)
            sel_ly = tmp_ds.ExecuteSQL(query)

            gdal.RasterizeLayer(mem_ds, (1,), sel_ly, burn_values=(255,))
            mem_data = mem_ds.GetRasterBand(1).ReadAsArray()

            mask = ma.masked_where(mem_data != 255, mem_data)
            if output:
                out_data[~mask.mask] = fid

            pixels = list()

            # reversing image shape to make it column-major
            shape = list(mask.shape)
            shape.reverse()

            non_mask_indices = ma.flatnotmasked_contiguous(mask)
            # iterating over each slice
            if non_mask_indices is None:
                continue
            for sl in non_mask_indices:
                start_pixel = np.unravel_index(sl.start, tuple(shape), order="F")
                stop_pixel = np.unravel_index(sl.stop, tuple(shape), order="F")
                # print sl, np.unravel_index(sl.start, mask.shape), np.unravel_index(sl.stop, mask.shape)
                pixels.append((start_pixel, stop_pixel))
            else:
                img_coordinates[fid] = pixels

        if output:
            gdal_utils.export_data(out_data, r"z:\mem_out_raster.img", tpl_ds)

        return img_coordinates
Example #8
0
def measure(mode, x, y, x0, x1, thresh = 0):
    """ return the a measure of y in the window x0 to x1
    """
    xt = x.view(numpy.ndarray) # strip Metaarray stuff -much faster!
    v = y.view(numpy.ndarray)
    
    xm = ma.masked_outside(xt, x0, x1).T
    ym = ma.array(v, mask = ma.getmask(xm))
    if mode == 'mean':
        r1 = ma.mean(ym)
        r2 = ma.std(ym)
    if mode == 'max' or mode == 'maximum':
        r1 = ma.max(ym)
        r2 = xm[ma.argmax(ym)]
    if mode == 'min' or mode == 'minimum':
        r1 = ma.min(ym)
        r2 = xm[ma.argmin(ym)]
    if mode == 'median':
        r1 = ma.median(ym)
        r2 = 0
    if mode == 'p2p': # peak to peak
        r1 = ma.ptp(ym)
        r2 = 0
    if mode == 'std': # standard deviation
        r1 = ma.std(ym)
        r2 = 0
    if mode == 'var': # variance
        r1 = ma.var(ym)
        r2 = 0
    if mode == 'cumsum': # cumulative sum
        r1 = ma.cumsum(ym) # Note: returns an array
        r2 = 0
    if mode == 'anom': # anomalies = difference from averge
        r1 = ma.anom(ym) # returns an array
        r2 = 0
    if mode == 'sum':
        r1 = ma.sum(ym)
        r2 = 0
    if mode == 'area' or mode == 'charge':
        r1 = ma.sum(ym)/(ma.max(xm)-ma.min(xm))
        r2 = 0
    if mode == 'latency': # return first point that is > threshold
        sm = ma.nonzero(ym > thresh)
        r1 = -1  # use this to indicate no event detected
        r2 = 0
        if ma.count(sm) > 0:
            r1 = sm[0][0]
            r2 = len(sm[0])
    if mode == 'count':
        r1 = ma.count(ym)
        r2 = 0
    if mode == 'maxslope':
        return(0,0)
        slope = numpy.array([])
        win = ma.flatnotmasked_contiguous(ym)
        st = int(len(win)/20) # look over small ranges
        for k in win: # move through the slope measurementwindow
            tb = range(k-st, k+st) # get tb array
            newa = numpy.array(self.dat[i][j, thisaxis, tb])
            ppars = numpy.polyfit(x[tb], ym[tb], 1) # do a linear fit - smooths the slope measures
            slope = numpy.append(slope, ppars[0]) # keep track of max slope
        r1 = numpy.amax(slope)
        r2 = numpy.argmax(slope)
    return(r1, r2)
Example #9
0
def measure(mode, x, y, x0, x1, thresh=0):
    """ return the a measure of y in the window x0 to x1
    """
    xm = ma.masked_outside(x, x0, x1)  # .compressed()
    ym = ma.array(y, mask=ma.getmask(xm))  # .compressed()
    if mode == 'mean':
        r1 = np.mean(ym)
        r2 = np.std(ym)
    if mode == 'max' or mode == 'maximum':
        r1 = ma.max(ym)
        r2 = xm[ma.argmax(ym)]
    if mode == 'min' or mode == 'minimum':
        r1 = ma.min(ym)
        r2 = xm[ma.argmin(ym)]
    if mode == 'minormax':
        r1p = ma.max(ym)
        r1n = ma.min(ym)
        if ma.abs(r1p) > ma.abs(r1n):
            r1 = r1p
            r2 = xm[ma.argmax(ym)]

        else:
            r1 = r1n
            r2 = xm[ma.argmin(ym)]

    if mode == 'median':
        r1 = ma.median(ym)
        r2 = 0
    if mode == 'p2p':  # peak to peak
        r1 = ma.ptp(ym)
        r2 = 0
    if mode == 'std':  # standard deviation
        r1 = ma.std(ym)
        r2 = 0
    if mode == 'var':  # variance
        r1 = ma.var(ym)
        r2 = 0
    if mode == 'cumsum':  # cumulative sum
        r1 = ma.cumsum(ym)  # Note: returns an array
        r2 = 0
    if mode == 'anom':  # anomalies = difference from averge
        r1 = ma.anom(ym)  # returns an array
        r2 = 0
    if mode == 'sum':
        r1 = ma.sum(ym)
        r2 = 0
    if mode == 'area' or mode == 'charge':
        r1 = ma.sum(ym) / (ma.max(xm) - ma.min(xm))
        r2 = 0
    if mode == 'latency':  # return first point that is > threshold
        sm = ma.nonzero(ym > thresh)
        r1 = -1  # use this to indicate no event detected
        r2 = 0
        if ma.count(sm) > 0:
            r1 = sm[0][0]
            r2 = len(sm[0])
    if mode == '1090':  #measure 10-90% time, also returns max
        r1 = ma.max(ym)
        r2 = xm[ma.argmax(ym)]
        y10 = 0.1 * r1
        y90 = 0.9 * r1
        sm1 = ma.nonzero(ym >= y10)
        sm9 = ma.nonzero(ym >= y90)
        r1 = xm[sm9] - xm[sm1]

    if mode == 'count':
        r1 = ma.count(ym)
        r2 = 0
    if mode == 'maxslope':
        return (0, 0)
        slope = np.array([])
        win = ma.flatnotmasked_contiguous(ym)
        st = int(len(win) / 20)  # look over small ranges
        for k in win:  # move through the slope measurementwindow
            tb = range(k - st, k + st)  # get tb array
            newa = np.array(self.dat[i][j, thisaxis, tb])
            ppars = np.polyfit(
                x[tb], ym[tb],
                1)  # do a linear fit - smooths the slope measures
            slope = np.append(slope, ppars[0])  # keep track of max slope
        r1 = np.amax(slope)
        r2 = np.argmax(slope)
    return (r1, r2)
Example #10
0
def measure(mode, x, y, x0, x1, thresh=0):
    """ return the a measure of y in the window x0 to x1
    """
    xm = ma.masked_outside(x, x0, x1)  # .compressed()
    ym = ma.array(y, mask=ma.getmask(xm))  # .compressed()
    if mode == "mean":
        r1 = np.mean(ym)
        r2 = np.std(ym)
    if mode == "max" or mode == "maximum":
        r1 = ma.max(ym)
        r2 = xm[ma.argmax(ym)]
    if mode == "min" or mode == "minimum":
        r1 = ma.min(ym)
        r2 = xm[ma.argmin(ym)]
    if mode == "minormax":
        r1p = ma.max(ym)
        r1n = ma.min(ym)
        if ma.abs(r1p) > ma.abs(r1n):
            r1 = r1p
            r2 = xm[ma.argmax(ym)]

        else:
            r1 = r1n
            r2 = xm[ma.argmin(ym)]

    if mode == "median":
        r1 = ma.median(ym)
        r2 = 0
    if mode == "p2p":  # peak to peak
        r1 = ma.ptp(ym)
        r2 = 0
    if mode == "std":  # standard deviation
        r1 = ma.std(ym)
        r2 = 0
    if mode == "var":  # variance
        r1 = ma.var(ym)
        r2 = 0
    if mode == "cumsum":  # cumulative sum
        r1 = ma.cumsum(ym)  # Note: returns an array
        r2 = 0
    if mode == "anom":  # anomalies = difference from averge
        r1 = ma.anom(ym)  # returns an array
        r2 = 0
    if mode == "sum":
        r1 = ma.sum(ym)
        r2 = 0
    if mode == "area" or mode == "charge":
        r1 = ma.sum(ym) / (ma.max(xm) - ma.min(xm))
        r2 = 0
    if mode == "latency":  # return first point that is > threshold
        sm = ma.nonzero(ym > thresh)
        r1 = -1  # use this to indicate no event detected
        r2 = 0
        if ma.count(sm) > 0:
            r1 = sm[0][0]
            r2 = len(sm[0])
    if mode == "1090":  # measure 10-90% time, also returns max
        r1 = ma.max(ym)
        r2 = xm[ma.argmax(ym)]
        y10 = 0.1 * r1
        y90 = 0.9 * r1
        sm1 = ma.nonzero(ym >= y10)
        sm9 = ma.nonzero(ym >= y90)
        r1 = xm[sm9] - xm[sm1]

    if mode == "count":
        r1 = ma.count(ym)
        r2 = 0
    if mode == "maxslope":
        return (0, 0)
        slope = np.array([])
        win = ma.flatnotmasked_contiguous(ym)
        st = int(len(win) / 20)  # look over small ranges
        for k in win:  # move through the slope measurementwindow
            tb = range(k - st, k + st)  # get tb array
            newa = np.array(self.dat[i][j, thisaxis, tb])
            ppars = np.polyfit(x[tb], ym[tb], 1)  # do a linear fit - smooths the slope measures
            slope = np.append(slope, ppars[0])  # keep track of max slope
        r1 = np.amax(slope)
        r2 = np.argmax(slope)
    return (r1, r2)
Example #11
0
def measure(mode, x, y, x0, x1, thresh=0, slopewin=1.0):
    """ return the a measure of y in the window x0 to x1
    """
    xm = ma.masked_outside(x, x0, x1)# .compressed()
    ym = ma.array(y, mask = ma.getmask(xm))# .compressed()
    if mode == 'mean':
        r1 = np.mean(ym)
        r2 = np.std(ym)
    if mode == 'max' or mode == 'maximum':
        r1 = ma.max(ym)
        r2 = xm[ma.argmax(ym)]
    if mode == 'min' or mode == 'minimum':
        r1 = ma.min(ym)
        r2 = xm[ma.argmin(ym)]
    if mode == 'minormax':
        r1p = ma.max(ym)
        r1n = ma.min(ym)
        if ma.abs(r1p) > ma.abs(r1n):
            r1 = r1p
            r2 = xm[ma.argmax(ym)]

        else:
            r1 = r1n
            r2 = xm[ma.argmin(ym)]

    if mode == 'median':
        r1 = ma.median(ym)
        r2 = 0
    if mode == 'p2p': # peak to peak
        r1 = ma.ptp(ym)
        r2 = 0
    if mode == 'std': # standard deviation
        r1 = ma.std(ym)
        r2 = 0
    if mode == 'var': # variance
        r1 = ma.var(ym)
        r2 = 0
    if mode == 'cumsum': # cumulative sum
        r1 = ma.cumsum(ym) # Note: returns an array
        r2 = 0
    if mode == 'anom': # anomalies = difference from averge
        r1 = ma.anom(ym) # returns an array
        r2 = 0
    if mode == 'sum':
        r1 = ma.sum(ym)
        r2 = 0
    if mode == 'area' or mode == 'charge':
        r1 = ma.sum(ym)/(ma.max(xm)-ma.min(xm))
        r2 = 0
    if mode == 'latency': # return first point that is > threshold
        sm = ma.nonzero(ym > thresh)
        r1 = -1  # use this to indicate no event detected
        r2 = 0
        if ma.count(sm) > 0:
            r1 = sm[0][0]
            r2 = len(sm[0])
    if mode == '1090': #measure 10-90% time, also returns max
        r1 = ma.max(ym)
        r2 = xm[ma.argmax(ym)]
        y10 = 0.1*r1
        y90 = 0.9*r1
        sm1 = ma.nonzero(ym >= y10)
        sm9 = ma.nonzero(ym >= y90)
        r1 = xm[sm9] - xm[sm1]

    if mode == 'count':
        r1 = ma.count(ym)
        r2 = 0
    if mode == 'maxslope':
        slope = []
        win = ma.flatnotmasked_contiguous(ym)
        dt = x[1]-x[0]
        st = int(slopewin/dt) # use slopewin duration window for fit.
        print('st: ', st)
        for k, w in enumerate(win): # move through the slope measurementwindow
            tb = range(k-st, k+st) # get tb array
            ppars = np.polyfit(x[tb], ym[tb], 1) # do a linear fit - smooths the slope measures
            slope.append(ppars[0]) # keep track of max slope
        r1 = np.max(slope)
        r2 = np.argmax(slope)
    return(r1, r2)
Example #12
0
    def rasterize_polygon_rois(self, img_src, output = False):
        u"""
        Convert polygons of interests from vector coordinates to pixel
        coordinates in the specified source image.
        """
        # declaring source imagery as template raster data set
        tpl_ds = gdal.Open(img_src)
        tpl_geo_transform = tpl_ds.GetGeoTransform()
        tpl_x_size, tpl_y_size = tpl_ds.RasterXSize, tpl_ds.RasterYSize
    
        # opening vector roi data source
        roi_ds = ogr.Open(self.roi_src)
        roi_ly = roi_ds.GetLayer(0)
        
        img_coordinates = dict()

        if output:
            out_ds = gdal.GetDriverByName('MEM').Create("", tpl_x_size, tpl_y_size, 1, gdal.GDT_Int16)
            out_ds.SetGeoTransform(tpl_geo_transform)
            out_ds.SetProjection(roi_ly.GetSpatialRef().ExportToWkt())
            out_data = out_ds.GetRasterBand(1).ReadAsArray()
            out_data[:] = -9999
        
        for ft in roi_ly:
            fid = ft.GetFID()
            print "Working on FID %d..." % fid
            mem_ds = gdal.GetDriverByName('MEM').Create("", tpl_x_size, tpl_y_size, 1, gdal.GDT_Byte)
            mem_ds.SetGeoTransform(tpl_geo_transform)
            mem_ds.SetProjection(roi_ly.GetSpatialRef().ExportToWkt())
    
            where_clause = "WHERE '%s' = %i" % ('FID', fid)   
            query = "SELECT * FROM '%s' %s" % (roi_ly.GetName(), where_clause)
            tmp_ds = ogr.Open(self.roi_src)
            sel_ly = tmp_ds.ExecuteSQL(query)
    
            gdal.RasterizeLayer(mem_ds, (1,), sel_ly, burn_values = (255,))
            mem_data = mem_ds.GetRasterBand(1).ReadAsArray()
    
            mask = ma.masked_where(mem_data != 255, mem_data)
            if output:
                out_data[~mask.mask] = fid

            pixels = list()
    
            # reversing image shape to make it column-major
            shape = list(mask.shape)
            shape.reverse()
    
            non_mask_indices = ma.flatnotmasked_contiguous(mask)
            # iterating over each slice
            if non_mask_indices is None:
                continue
            for sl in non_mask_indices:
                start_pixel = np.unravel_index(sl.start, tuple(shape), order = 'F')
                stop_pixel = np.unravel_index(sl.stop, tuple(shape), order = 'F')
                #print sl, np.unravel_index(sl.start, mask.shape), np.unravel_index(sl.stop, mask.shape)
                pixels.append((start_pixel, stop_pixel))
            else:
                img_coordinates[fid] = pixels

        if output:
            gdal_utils.export_data(out_data, r"z:\mem_out_raster.img", tpl_ds)
    
        return img_coordinates