Example #1
0
def window(a, b, bottom, left, height=100, width=100, searchWindowHeight=9,\
          searchWindowWidth=9, display=True, toFolder=''):
    # 1. create folder a.outputFolder/observation/time
    # 2. create windows aa bb and write image for aa, bb, bb-aa
    # 3. regress and write result
    # 4. get prediction and write image
    if toFolder=="":
        #toFolder = a.outputFolder + "bottom%d_left%d_height_%d_width_%d_searchHeight%d_searchWidth_%d" % (bottom,left,height,width, searchWindowHeight, searchWindowWidth)
        toFolder = a.outputFolder + str(int(time.time()) %  100000)
    if toFolder!=None and toFolder!=False:
        try:
            os.makedirs(toFolder)
            print a.outputFolder, "folder created!"
        except OSError:
            print a.outputFolder, "exists!"
            raise OSError

    # initialise the output string
    outputFolder =  toFolder        #alias
    output  = time.asctime()
    output += "\narmor.observe.window: \na = " + a.name + ", b = " + b.name
    output += "\nwindow: bottom=%d, left=%d, height=%d, width=%d" % (bottom, left, height, width)

    # create the windows and save
    aa = a.getWindow(bottom, left, width, height)
    bb = b.getWindow(bottom, left, width, height)
    diff = (bb-aa)
    aa.imagePath = outputFolder + 'a.window%d.png' % height
    bb.imagePath = outputFolder + 'b.window%d.png' % height
    aa.saveImage()
    bb.saveImage()
    
    # compute basic properties
    output += "mean of a.window = %f; mean of b.window = %f" %(aa.matrix.mean(),bb.matrix.mean())
    output += "Number of data points: a.window: %d, b.window %d" %\
                ( (1-aa.matrix.mask).sum(), (1-bb.matrix.mask).sum() )
    output += "common region: %d" % (1- (aa.matrix.mask+bb.matrix.mask)).sum()                
    
    corr = aa.corr(bb)[0,1]
    output += "\nCorrelation: %f;  Correlation-squared: %f" % (corr, corr**2)

    # regress
    regressionResults = cflfree.regressLocal(a=aa, b=b, gridSize=5,bottom=bottom,left=left,
                                            height=height,width=width,\
                                              searchWindowHeight=searchWindowHeight,\
                                              searchWindowWidth=searchWindowWidth,\
                                              display=False) 
    mn          = [v[0] for v in regressionResults]
    C           = [v[1] for v in regressionResults]
    Rsquared    = [v[2] for v in regressionResults]
    
    output += "\nTop results from CFL-relaxed regression (search window height: %d, width: %d)" %\
                                              (searchWindowHeight, searchWindowWidth)
    output += "\n(m, n),\tRsquared,\tc1,...,c9"
    for v in regressionResults[:12]:
        output += "\n(%d,%d),\t%f, %f %f %f %f %f %f %f %f %f" % (v[0][0],v[0][1],v[2],\
                 v[1][0], v[1][1],v[1][2],v[1][3],v[1][4],v[1][5],v[1][6],v[1][7],\
                 v[1][8])
    
    #get prediction
    (m,n), C, Rsquared = regressionResults[0]
    aa1 = getPrediction(C, aa)
    bb1 = b.getWindow(bottom=bottom+m, left=left+n, height=height, width=width)
    diff = bb1-aa1
    aa1.imagePath = outputFolder + "aa1.window.shiiba.prediction.png"
    bb1.imagePath = outputFolder + "bb1.window.shiiba.data.png"
    diff.imagePath = outputFolder + "bb1-aa1.window.shiiba.data.png"
    aa1.saveImage()
    bb1.saveImage()
    diff.saveImage()
    
    # compute correlation
    corr = aa1.corr(bb1)[0,1]
    output += "\nCorrelation between prediction and data in the window: %f;  Correlation-squared: %f" % (corr, corr**2)

    # output    
    if display:
        print output 
    open(toFolder+'.log.txt', 'w').write(output)
    return output
from armor.examples import *
##compare with local regression:
import numpy as np
import numpy.ma as ma
import armor.pattern as pattern
dbz= pattern.DBZ
a = dbz('20120612.0300')
a.load()
a.coordinateOrigin = (440,460)

vect = antiClockwiseField(magnitude = 0.002)  
vect.U *=2    # vect.V.max() = 0.88 ; vect.U.max() = 1.76
a_rotated = a.advect(vect, scope = (11,11))

import armor.shiiba.regressionCFLfree as cflfree
x_550= cflfree.regressLocal(a,a_rotated,550,500,100,100, searchWindowWidth=13,\
                            useRecursion=True, display=False)


                    
#x_550 = a.shiibaLocal(b=a_rotated,iRange=[550], jRange=[550])

##################################################
# 5. do as 3. (shiiba regress with fixed origin) above, 
#    and the flipped axes where appropriate and regress locally
#    and compute the correlations thereof
# 8 April 2013



from armor.examples import *
#import armor.shiiba.regression2 as regression