Пример #1
0
def testB(scores=""):

    """
    order/arrange the WRFs according to proximity to COMPREF
    """
    """
    USE
        from armor.tests import roughwork as rw
        reload(rw)
        rw.testB('/home/k/ARMOR/documents/2013-final-report/1383883165/testA.pydump')
    """
    #from armor.objects3 import kongrey     # the data 
    #kongrey.fix()
    from armor.objects3 import kongreywrf2  # the models
    kongreywrf2.fix()    #fixing the w.name's for w in kongreywrf
    
    testBfolder = outputFolder + 'testB' + timeString + '/'
    if not os.path.exists(testBfolder):
        os.makedirs(testBfolder)
    if scores == "" :
        testAfile = [v for v in sorted(os.listdir(outputFolder), reverse=True) if 'testA' in v][0]
        print 'loading', outputFolder + testAfile
        scores = pickle.load(open(outputFolder + testAfile))
    elif isinstance(scores, str):
        print 'loading', scores
        scores = pickle.load(open(scores))
        testAfile = scores
    else:
        testAfile = ""
    print 'sleeping 1 seconds'
    time.sleep(1)
    Tlist  = [v.dataTime for v in kongrey]
    top8matches = {}
    for T in Tlist:
        print '\n...............................\nTime:', T
        pairList = [v for v in scores.keys() if T in v[0]]
        print pairList[:5]
        if len(pairList) == 0:
            continue
        pairList.sort(reverse=True, key=lambda v:scores[v])
        top8matches[T] = [v[1] for v in pairList][:8]
        v0       = pairList[0][0]   # temporary variable
        print 'Top 8 matches:', '\n'.join([v+ '\t' + str(scores[(v0,v)]) for v in top8matches[T]])
        pickle.dump(top8matches, open(testBfolder+ 'top8matches.pydump', 'w'))

        # construct the 3x3 panel
        imList  = kongrey(T) + [kongreywrf2(M)[0]  for M in top8matches[T]]
        #kongrey.load(T)
        #for im in imList[1:]:
        #    im.load()
        #debug
        #print imList
        img9    = construct3by3(imList)
        img9.imagePath = testBfolder + 'best8matches' + T + '.png'
        print 'saving images to', img9.imagePath
        img9.saveImage(dpi=600)
    try:
        open(testBfolder + 'notes.txt').write('source:\n'+ testAfile + '\n\n' +str(scores))
    except:
        print 'error in writing ' + testBfolder + 'notes.txt' 
        pass
    return top8matches
Пример #2
0
def testA(startingFrom='20130828.0600', endsAt='20130830.0900', threshold=-9999):
    """
    testA : get all the correlations
    """

    """
    1.  apply LoG(sigma=100) + threshold to models (M01-M20, +- 6 hours) and observations
    2.  extract regional "features" and compare
    3.  output the results to '/home/k/ARMOR/documents/2013-final-report/'
    """
    #   1.  apply LoG(sigma=100) + threshold to models (M01-M20, +- 6 hours) and observations
    #   set up
    
    
    if not os.path.exists(outputFolder):
        os.makedirs(outputFolder)
    #   loading the stuff
    from armor.objects3 import kongrey     # the data 
    from armor.objects3 import kongreywrf2  # the models
    kongreywrf2.fix()    #fixing the w.name's for w in kongreywrf
    kongrey.fix()
    
    scores = {}
    for count, k in enumerate([v for v in kongrey if (v.dataTime>=startingFrom) and (v.dataTime<=endsAt)]):
        #   set up
        #   k = kongrey radar data
        #   w = wrf output
        #   to compare w to k, we use w.momentNormalise(k)
        k.load()
        k.setThreshold(0)
        k.backupMatrix()
        T0 = k.timeDiff(hours=-6)
        T1 = k.timeDiff(hours= 6)
        wrfs = [w for w in kongreywrf2 if w.dataTime>=T0 and w.dataTime<=T1]
        k.mask = k.gaussianMask(100)

        for w in wrfs:
            # loading
            #k.load()
            #k.setThreshold(0)
            k.restoreMatrix(0)                          # 2013-11-11
            w.load()
            w.setThreshold(0)
            if threshold != -9999:                      # added 2013-11-11
                if isinstance(threshold, int):
                    k.matrix *= (k.matrix>=threshold)
                    w.matrix *= (w.matrix>=threshold)
                elif threshold == "volume":

                    #k.matrix   *= (k.matrix>=threshold)
                    #vol_k       = k.volume(threshold)
                    #threshold_w =threshold-20
                    #vol_w   = w.volume(threshold_w)
                    #while vol_w > vol_k:
                    #    theshold_w +=5
                    #    vol_w       = w.volume(threshold_w)
                    #w.matrix    *= (w.matrix>=threhold_w)
                    #print "thresholds for RADAR and WRF:", threshold, threshold_w
                    #print "volumes for RADAR and WRF:"   , vol_k, vol_w

                    k.matrix   *= (k.matrix>=30)     # fixed 2013-11-15  ; default threshold=30
                    vol_k       = k.volume(30)
                    threshold_w =threshold-10
                    vol_w   = w.volume(threshold_w)
                    while vol_w > vol_k:
                        theshold_w +=5
                        vol_w       = w.volume(threshold_w)
                    w.matrix    *= (w.matrix>=threhold_w)
                    print "thresholds for RADAR and WRF:", threshold, threshold_w
                    print "volumes for RADAR and WRF:"   , vol_k, vol_w

                    
            # matching: 
                #   1. filter;  2. get component;  3.construct masking functions;
                #   4. mask;    5. normalise    ;  6. compare
            w.mask = w.gaussianMask(sigma=100, fraction=0.8)
            # compare
            k.matrix *= k.mask
            w.matrix *= w.mask
            w2 = w.momentNormalise(k)
            scores[(k.name, w.name)] = w2.corr(k)

            # ordering
            # output
            print k.name, ',', w.name, scores[(k.name, w.name)] 
            pickle.dump(scores, open(outputFolder+ 'testA' + timeString + 'from'+startingFrom + 'to' +endsAt +\
                       'threshold_' + str(threshold) + '.pydump', 'w'))
            kongreywrf2.unload(w.name)
    return scores