Ignore:
Timestamp:
12/08/20 08:45:53 (4 years ago)
Author:
Mathieu Morlighem
Message:

merged trunk-jpl and trunk for revision 25834

Location:
issm/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk

  • issm/trunk/src

  • issm/trunk/src/m/qmu/dakota_out_parse.py

    r24313 r25836  
    88from helpers import *
    99
    10 #Note: this may be re-written later to take advantage of Python's file i / o mechanics
    11 #    as it is written now it is often difficult to work with, but is analagous to
    12 #    the Matlab version of dakota_out_parse
     10# NOTE: May be rewritten later to take advantage of Python's file I/O
     11# mechanics. As it is written now, it is often difficult to work with, but is
     12# analagous to the MATLAB version of dakota_out_parse.
    1313
    1414
    1515def dakota_out_parse(filei):  # {{{
    16     '''
    17   read a Dakota .out or .dat output file and parse it.
    18 
    19   [method, dresp, scm, pcm, srcm, prcm] = dakota_out_parse(filei)
    20 
    21   where the required input is:
    22     filei         (character, name of .out file)
    23 
    24   the required output is:
    25     method        (character, dakota method name)
    26     dresp         (structure array, responses)
    27 
    28   and the optional output is:
    29     scm           (double array, simple correlation matrix)
    30     pcm           (double array, partial correlation matrix)
    31     srcm          (double array, simple rank correlation matrix)
    32     prcm          (double array, partial rank correlation matrix)
    33 
    34   the filei will be prompted if empty.  the fields of dresp
    35   are particular to the data contained within the file.  the
    36   scm, pcm, srcm, and prcm are output by dakota only for the
    37   sampling methods.
    38 
    39   this function reads a dakota .out output file and parses it
    40   into the matlab workspace.  it operates in a content - driven
    41   fashion, where it skips the intermediate data and then parses
    42   whatever output data it encounters in the order in which it
    43   exists in the file, rather than searching for data based on
    44   the particular method.  (this makes it independent of method.)
    45   it also can read and parse the .dat tabular_output file.
    46 
    47   this data would typically be used for plotting and other
    48   post - processing within matlab or excel.
    49 '''
     16    """DAKOTA_OUT_PARSE - read a Dakota .out or .dat output file and parse it.
     17
     18    Usage:
     19        [method, dresp, scm, pcm, srcm, prcm] = dakota_out_parse(filei)
     20
     21    where the required input is,
     22        filei         (character, name of .out file)
     23
     24    the required output is,
     25        method        (character, Dakota method name)
     26        dresp         (structure array, responses)
     27
     28    and the optional output is,
     29        scm           (double array, simple correlation matrix)
     30        pcm           (double array, partial correlation matrix)
     31        srcm          (double array, simple rank correlation matrix)
     32        prcm          (double array, partial rank correlation matrix)
     33
     34    The filei will be prompted for if empty. The fields of dresp are particular
     35    to the data contained within the file. The scm, pcm, srcm, and prcm are
     36    output by Dakota only for the sampling methods.
     37
     38    This function reads a Dakota .out output file and parses it into the Python
     39    runtime. It operates in a content-driven fashion, where it skips the
     40    intermediate data and then parses whatever output data it encounters in the
     41    order in which it exists in the file, rather than searching for data based
     42    on the particular method (this makes it independent of method). It also can
     43    read and parse the .dat tabular_output file.
     44
     45    This data would typically be used for plotting and other postprocessing
     46    within MATLAB or Excel.
     47
     48    TODO:
     49    - Figure out why output from Dakota is different under MATLAB and Python
     50    (is it the input file that we write?)
     51
     52    "Copyright 2009, by the California Institute of Technology. ALL RIGHTS
     53    RESERVED. United States Government Sponsorship acknowledged. Any commercial
     54    use must be negotiated with the Office of Technology Transfer at the
     55    California Institute of Technology. (NTR 47078)
     56
     57    This software may be subject to U.S. export control laws. By accepting this
     58    software, the user agrees to comply with all applicable U.S. export laws
     59    and regulations. User has the responsibility to obtain export licenses, or
     60    other export authority as may be required before exporting such information
     61    to foreign countries or providing access to foreign persons."
     62    """
     63
    5064    if filei is None:
    5165        help(dakota_out_parse)
     
    5367
    5468    if not isfile(filei) or getsize(filei) == 0:
    55         filei = str(eval(input('Input file?  ')))
     69        filei = str(eval(input('Input file? ')))
    5670
    5771    #fidi = fopen(sprintf('%s', filei), 'r')
     
    6276        fline = fidi.readline()
    6377        if getsize(filei) == 0 or fline == '':
    64             raise RuntimeError('File ' + filei + ' is empty.')
    65 
    66         dresp = []  # of struct()
     78            raise RuntimeError('File ' + filei + ' is empty')
     79
     80        dresp = [] # of struct()
    6781        scm = struct()
    6882        pcm = struct()
     
    7791            fidi.seek(0, 0)
    7892
    79     # loop through the file to find the Dakota method name
     93        # loop through the file to find the Dakota method name
    8094        fline = findline(fidi, 'method', True)
    8195        if fline is None:
     
    87101                [ntokens, tokens] = fltokens(fline)
    88102                method = tokens[0].strip()
    89                 print('Dakota method =\'' + method + '\'.')
     103                print('Dakota method = \'' + method + '\'')
    90104            elif fline[6] in ['N', 'n']:
    91105                fline = findline(fidi, 'methodName = ')
    92106                [ntokens, tokens] = fltokens(fline)
    93107                method = tokens[2].strip()
    94                 print('Dakota methodName = "' + method + '".')
    95 
    96     # loop through the file to find the function evaluation summary
     108                print('Dakota methodName = \'' + method + '\'')
     109
     110        # loop through the file to find the function evaluation summary
    97111        counter = 0
    98112        fline = ''
    99113        nfeval = nfeval_read(fidi, fline)
    100114
    101         #  process each results section based on content of the file
     115        # process each results section based on content of the file
    102116        while counter < 10:
    103             # because python makes file i / o difficult
     117            # because python makes file I/O difficult
    104118            # if we see 10 + blank lines in a row then we have reached EOF
    105119            # (tests show actual maximum number of blank lines is around 5)
     
    108122            else:
    109123                counter = 0
    110     #    ipos = ftell(fidi)
     124            # ipos = ftell(fidi)
    111125            fline = fidi.readline()
    112126            if fline == '' or fline.isspace():
     
    147161                'Unexpected line: ' + str(fline)
    148162
    149     #    fidi.seek(ipos, 0)
    150 
    151     #  loop through the file to verify the end
     163            # fidi.seek(ipos, 0)
     164
     165    # loop through the file to verify the end
    152166
    153167    # fline = findline(fidi, '<<<<< Single Method Strategy completed')
     
    155169    #     return
    156170    #
    157         print('End of file successfully reached.')
     171        print('End of file successfully reached')
    158172    #close(fidi)
    159173    #except Exception as err:
    160174    #print "ERROR in dakota_out_parse: " + err
    161175    #raise err
    162     #raise RuntimeError(filei + ' could not be opened.')
     176    #raise RuntimeError(filei + ' could not be opened')
    163177
    164178    return [method, dresp, scm, pcm, srcm, prcm]
     
    167181
    168182def dak_tab_out(fidi, fline):  # {{{
    169     #  function to parse the dakota tabular output file
    170 
    171     print('Reading Dakota tabular output file.')
    172 
    173     #  process column headings of matrix (skipping eval_id)
     183    """DAK_TAB_OUT - function to parse the Dakota tabular output file
     184    """
     185
     186    print('Reading Dakota tabular output file')
     187
     188    # Process column headings of matrix (skipping eval_id)
    174189    [ntokens, tokens] = fltokens(fline)
    175190
    176     # New file DAKOTA versions > 6
    177     if strncmpi(fline, '%eval_id interface', 18):
     191    if strncmpi(fline, '%eval_id interface', 18): # Dakota versions >= 6
    178192        offset = 2
    179     else:  #DAKOTA versions < 6
     193    else:  # Dakota versions < 6
    180194        offset = 1
    181195
    182     desc = [['' for i in range(ntokens - offset)]]
     196    desc = ['' for i in range(ntokens - offset)]
    183197    data = np.zeros((1, ntokens - offset))
    184198
    185199    for i in range(ntokens - offset):
    186         desc[0][i] = str(tokens[i + offset])
    187 
    188     print("Number of columns (Dakota V + R)=" + str(ntokens - 2) + '.')
    189 
    190     #  process rows of matrix
     200        desc[i] = str(tokens[i + offset])
     201
     202    print('Number of columns (Dakota V + R) = {}'.format(ntokens - 2))
     203
     204    # Process rows of matrix
    191205    nrow = 0
    192206    while True:
    193 
    194207        fline = fidi.readline()
    195208
     
    202215        [ntokens, tokens] = fltokens(fline)
    203216
    204     #  add row values to matrix (skipping eval_id)
    205 
     217        # Add row values to matrix (skipping eval_id)
    206218        for i in range(ntokens - offset):
    207219            data[nrow, i] = tokens[i + offset]
     
    209221        nrow = nrow + 1
    210222
    211     print('Number of rows (Dakota func evals) = ' + str(nrow) + '.')
    212 
    213     #  calculate statistics
    214 
    215     #  since normfit doesn't have a dim argument, and matlab isvector is True
    216     #  for a 1xn matrix, handle the case of one row explicitly
    217 
    218     #  Update: normfit_issm.py does handle this case
     223    print('Number of rows (Dakota func evals) = ' + str(nrow))
     224
     225    # Calculate statistics
    219226    if (np.size(data, 0) > 1):
    220         #dmean  =mean   (data)
    221         #dstddev = std    (data, 0)
     227        #dmean = mean(data)
     228        #dstddev = std(data, 0)
    222229        [dmean, dstddev, dmeanci, dstddevci] = normfit_issm(data, 0.05)
    223230    else:
     
    229236            [dmean[0, i], dstddev[0, i], dmeanci[:, i], dstddevci[:, i]] = normfit_issm(data[:, i], 0.05)
    230237
    231     dmin = data.min(0)
     238    dmin = data.min(axis=0)
    232239    dquart1 = prctile_issm(data, 25, 0)
    233     dmedian = np.median(data, 0)
     240    dmedian = np.median(data, axis=0)
    234241    dquart3 = prctile_issm(data, 75, 0)
    235     dmax = data.max(0)
     242    dmax = data.max(axis=0)
    236243    dmin95 = prctile_issm(data, 5, 0)
    237244    dmax95 = prctile_issm(data, 95, 0)
    238245
    239     # Note: the following line may cause the following warning
    240     #    (should not crash or invalidate results) when one of
    241     #    the inputs does not change with respect to the
    242     #    other / s causing an internal divide-by - zero error
    243 
    244     # / usr / local / lib / python2.7 / dist - packages / numpy / lib / function_base.py:3163:
    245     #    RuntimeWarning: invalid value encountered in true_divide
    246     #    c / = stddev[:, None]
    247 
    248     #    (and / or the same but with "c / = stddev[None, :]")
    249 
     246    # NOTE: The following line may cause the following warning (should not
     247    # crash or invalidate results) when one of the inputs does not change with
     248    # respect to the other(s), causing an internal divide-by-zero error,
     249    #
     250    #       /usr/local/lib/python2.7/dist-packages/numpy/lib/function_base.py:3163:
     251    #       RuntimeWarning: invalid value encountered in true_divide
     252    #       c /= stddev[:, None]
     253    #
     254    # (and/or the same but with "c /= stddev[None, :]")
     255
     256    # Equivalent to Dakota scm, MATLAB corrcoef, and Excel correl
    250257    dcorrel = np.corrcoef(data.T)
    251258
    252     #  divide the data into structures for consistency
     259    # Divide the data into structures for consistency
    253260    dresp = []
    254 
    255261    for i in range(len(desc)):
    256262        dresp.append(struct())
    257         dresp[i].descriptor = str(desc[i])
     263        dresp[i].descriptor = desc[i]
    258264        dresp[i].sample = data[:, i]
    259265        dresp[i].mean = dmean[i]
     
    294300    [ntokens, tokens] = fltokens(fline)
    295301    nfeval = tokens[4]
    296     print('  Dakota function evaluations = ' + str(int(nfeval)) + '.')
     302    print('  Dakota function evaluations = ' + str(int(nfeval)))
    297303
    298304    return nfeval
     
    309315    [ntokens, tokens] = fltokens(fline)
    310316    nsamp = tokens[3]
    311     print('  Dakota samples = ' + str(int(nsamp)) + '.')
     317    print('  Dakota samples = ' + str(int(nsamp)))
    312318
    313319    return nsamp
     
    340346        dresp[-1].coefvar = tokens[12]
    341347
    342     print('  Number of Dakota response functions = ' + str(len(dresp)) + '.')
     348    print('  Number of Dakota response functions = ' + str(len(dresp)))
    343349
    344350    return dresp
     
    350356
    351357    if fline is None or fline == '' or fline.isspace():
    352         fline = findline(fidi, 'Moment - based statistics for each response function')
     358        fline = findline(fidi, 'Moment-based statistics for each response function')
    353359        return
    354360
    355     print('Reading moment - based statistics for response functions:')
     361    print('Reading moment-based statistics for response functions:')
    356362
    357363    #  skip column headings of moment - based statistics
     
    376382        dresp[-1].kurtosis = tokens[4]
    377383
    378     print('  Number of Dakota response functions = ' + str(len(dresp)) + '.')
     384    print('  Number of Dakota response functions = ' + str(len(dresp)))
    379385
    380386    return dresp
     
    433439            dresp[i].stddevci[1, 0] = tokens[4]
    434440
    435     print('  Number of Dakota response functions = ' + str(len(dresp)) + '.')
     441    print('  Number of Dakota response functions = ' + str(len(dresp)))
    436442
    437443    return dresp
     
    495501                fline = fidi.readline()
    496502
    497     print('  Number of Dakota response functions = ' + str(len(dresp)) + '.')
     503    print('  Number of Dakota response functions = ' + str(len(dresp)))
    498504
    499505    return dresp
     
    550556                fline = fidi.readline()
    551557
    552     print('  Number of Dakota response functions = ' + str(len(dresp)) + '.')
     558    print('  Number of Dakota response functions = ' + str(len(dresp)))
    553559
    554560    return dresp
     
    565571            return
    566572
    567     print('Reading ' + fline + '.')
     573    print('Reading ' + fline)
    568574
    569575    cmat.title = fline
     
    657663        #  if importance factors missing, skip to cdf
    658664        if not idvar:
    659             print('    Importance Factors not available.')
     665            print('    Importance Factors not available')
    660666            dresp[-1].var = []
    661667            dresp[-1].impfac = []
     
    716722        #  if cdf missing, skip to end of response function
    717723        if not icdf:
    718             print('    Cumulative Distribution Function not available.')
     724            print('    Cumulative Distribution Function not available')
    719725            dresp[ndresp].cdf = []
    720726            while (fline != '' and not fline.isspace()) and not strncmpi(fline, 'MV Statistics for ', 18) and not strncmp(fline, ' - ', 1):
    721727                fline = fidi.readline()
    722728
    723     print('  Number of Dakota response functions = ' + str(len(dresp)) + '.')
     729    print('  Number of Dakota response functions = ' + str(len(dresp)))
    724730
    725731    return dresp
     
    845851        dresp[-1].vum = []
    846852
    847     print('Reading measures for volumetric uniformity.')
     853    print('Reading measures for volumetric uniformity')
    848854    fline = fidi.readline()
    849855    fline = fidi.readline()
     
    881887    [ntokens, tokens] = fltokens(fline)
    882888    method = tokens[2]
    883     print('Dakota iterator \'' + str(method) + '\' completed.')
     889    print('Dakota iterator \'' + str(method) + '\' completed')
    884890
    885891    return method
     
    905911
    906912    #  issue warning and reset file position
    907     print('Warning: findline:str_not_found: String ' + str(string) + ' not found in file.')
     913    print('Warning: findline:str_not_found: String ' + str(string) + ' not found in file')
    908914    fidi.seek(ipos, 0)
    909915    return None
Note: See TracChangeset for help on using the changeset viewer.