示例#1
0
def extract(INPUT, xmin, xmax, xstep):
    """Get covariance matrix, coordinates.
    This is the meta-extractor.  It processes both individual files and
    folders.
    """
    # result is returned as a named tuple: RESRET
    RESRET = namedtuple("ret", ["coord", "covar", "numblocks"])
    if os.path.isfile(INPUT):
        RESRET = simple_proc_file(INPUT, xmin, xmax, EIGCUT)
        COV = RESRET.covar
        COORDS = RESRET.coord
        # DIMCOV is dimensions of the covariance matrix
        DIMCOV = RESRET.numblocks
        # then find out domain of files to process
    ####process individual files in dir 5ab
    # error handling, test to see if time value goes out of range,
    # i.e. if data isn't available to match the requested time domain
    # i,j are new indices, shifting xmin to the origin
    # j = 0 # initialized below
    # test if directory
    elif os.path.isdir(INPUT):
        i = 0
        # DIMCOV is dimensions of the covariance matrix
        DIMCOV = int((xmax - xmin) / xstep + 1)
        # cov is the covariance matrix
        COV = [[[0] for _ in range(DIMCOV)] for _ in range(DIMCOV)]
        # COORDS are the coordinates to be plotted.
        # the ith point with the jth value
        COORDS = [[[0] for _ in range(2)] for _ in range(DIMCOV)]
        for time in np.arange(xmin, xmax + 1, xstep):
            COORDS[i][0] = time
            j = 0
            for time2 in np.arange(xmin, xmax + 1, xstep):
                IFILE = proc_folder(INPUT, time)
                JFILE = proc_folder(INPUT, time2)
                IFILE = INPUT + "/" + IFILE
                JFILE = INPUT + "/" + JFILE
                try:
                    TRIAL = open(IFILE, "r")
                    TRIAL2 = open(JFILE, "r")
                except TypeError:
                    STR1 = "Either domain is invalid,"
                    print STR1, "or folder is invalid."
                    print "Double check contents of folder."
                    print "Offending file(s):"
                    print IFILE
                    print JFILE
                    sys.exit(1)
                RESRET = proc_file(IFILE, JFILE)
                # fill in the covariance matrix
                COV[i][j] = RESRET.covar
                # only store coordinates once.  each file is read many times
                if j == 0:
                    COORDS[i][1] = RESRET.coord
                j += 1
            i += 1
    return COORDS, COV, DIMCOV
示例#2
0
def main():
    ####set up 1ab
    OPTIONS = namedtuple('ops', ['xmin', 'xmax', 'xstep', 'trials'])

    
    ###error processing, parameter extractions
    INPUT, OPTIONS = procargs(sys.argv[1:])
    XMIN, XMAX = xlim_err(OPTIONS.xmin, OPTIONS.xmax)
    XSTEP = xstep_err(OPTIONS.xstep, INPUT)
    TRIALS = trials_err(OPTIONS.trials)

    if TRIALS == -1:
        RESULT_MIN, PARAM_ERR, COORDS, COV = singlefit(INPUT, 
                                                       XMIN, XMAX, XSTEP)
        printerr(RESULT_MIN.x, PARAM_ERR)
        mkplot(COORDS, COV, RESULT_MIN)
        sys.exit(0)
    else:
        list_fit_params = []
        for ctime in range(TRIALS):
           IFILE = proc_folder(INPUT, ctime, "blk")
           NINPUT = os.path.join(INPUT, IFILE)
           RESULT_MIN, PARAM_ERR, COORDS, COV = singlefit(NINPUT, 
                                                          XMIN, XMAX, XSTEP)
           list_fit_params.append(RESULT_MIN.x)
        #todo: make plot section
        TRANSPOSE = np.array(list_fit_params).T.tolist()
        avg_fit_params = [sum(TRANSPOSE[i])/len(TRANSPOSE[i]) for i in range(
        len(TRANSPOSE))]
        if JACKKNIFE == "YES":
            prefactor = (TRIALS-1.0)/(1.0*TRIALS)
        elif JACKKNIFE == "NO":
            prefactor = (1.0)/((TRIALS-1.0)*(1.0*TRIALS))
        else:
            print "***ERROR***"
            print "JACKKNIFE value should be a string with value either"
            print "YES or NO"
            print "Please examine the config file."
            sys.exit(1)
        err_fit_params = [sqrt(sum([(TRANSPOSE[i][j]-avg_fit_params[i])**2
                                    for j in range(len(
                                            TRANSPOSE[i]))])*prefactor)
                          for i in range(len(TRANSPOSE))]
        printerr(avg_fit_params, err_fit_params)
        sys.exit(0)