Changeset 25836 for issm/trunk/src/m/qmu/dakota_out_parse.py
- Timestamp:
- 12/08/20 08:45:53 (4 years ago)
- Location:
- issm/trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk
- Property svn:mergeinfo changed
-
issm/trunk/src
- Property svn:mergeinfo changed
-
issm/trunk/src/m/qmu/dakota_out_parse.py
r24313 r25836 8 8 from helpers import * 9 9 10 # Note: this may be re-written later to take advantage of Python's file i / o mechanics11 # as it is written now it is often difficult to work with, but is analagous to12 # the Matlab version of dakota_out_parse10 # 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. 13 13 14 14 15 15 def 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 50 64 if filei is None: 51 65 help(dakota_out_parse) … … 53 67 54 68 if not isfile(filei) or getsize(filei) == 0: 55 filei = str(eval(input('Input file? 69 filei = str(eval(input('Input file? '))) 56 70 57 71 #fidi = fopen(sprintf('%s', filei), 'r') … … 62 76 fline = fidi.readline() 63 77 if getsize(filei) == 0 or fline == '': 64 raise RuntimeError('File ' + filei + ' is empty .')65 66 dresp = [] 78 raise RuntimeError('File ' + filei + ' is empty') 79 80 dresp = [] # of struct() 67 81 scm = struct() 68 82 pcm = struct() … … 77 91 fidi.seek(0, 0) 78 92 79 #loop through the file to find the Dakota method name93 # loop through the file to find the Dakota method name 80 94 fline = findline(fidi, 'method', True) 81 95 if fline is None: … … 87 101 [ntokens, tokens] = fltokens(fline) 88 102 method = tokens[0].strip() 89 print('Dakota method = \'' + method + '\'.')103 print('Dakota method = \'' + method + '\'') 90 104 elif fline[6] in ['N', 'n']: 91 105 fline = findline(fidi, 'methodName = ') 92 106 [ntokens, tokens] = fltokens(fline) 93 107 method = tokens[2].strip() 94 print('Dakota methodName = "' + method + '".')95 96 #loop through the file to find the function evaluation summary108 print('Dakota methodName = \'' + method + '\'') 109 110 # loop through the file to find the function evaluation summary 97 111 counter = 0 98 112 fline = '' 99 113 nfeval = nfeval_read(fidi, fline) 100 114 101 # 115 # process each results section based on content of the file 102 116 while counter < 10: 103 # because python makes file i / odifficult117 # because python makes file I/O difficult 104 118 # if we see 10 + blank lines in a row then we have reached EOF 105 119 # (tests show actual maximum number of blank lines is around 5) … … 108 122 else: 109 123 counter = 0 110 #ipos = ftell(fidi)124 # ipos = ftell(fidi) 111 125 fline = fidi.readline() 112 126 if fline == '' or fline.isspace(): … … 147 161 'Unexpected line: ' + str(fline) 148 162 149 #fidi.seek(ipos, 0)150 151 # 163 # fidi.seek(ipos, 0) 164 165 # loop through the file to verify the end 152 166 153 167 # fline = findline(fidi, '<<<<< Single Method Strategy completed') … … 155 169 # return 156 170 # 157 print('End of file successfully reached .')171 print('End of file successfully reached') 158 172 #close(fidi) 159 173 #except Exception as err: 160 174 #print "ERROR in dakota_out_parse: " + err 161 175 #raise err 162 #raise RuntimeError(filei + ' could not be opened .')176 #raise RuntimeError(filei + ' could not be opened') 163 177 164 178 return [method, dresp, scm, pcm, srcm, prcm] … … 167 181 168 182 def 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) 174 189 [ntokens, tokens] = fltokens(fline) 175 190 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 178 192 offset = 2 179 else: # DAKOTAversions < 6193 else: # Dakota versions < 6 180 194 offset = 1 181 195 182 desc = [ ['' for i in range(ntokens - offset)]]196 desc = ['' for i in range(ntokens - offset)] 183 197 data = np.zeros((1, ntokens - offset)) 184 198 185 199 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 matrix200 desc[i] = str(tokens[i + offset]) 201 202 print('Number of columns (Dakota V + R) = {}'.format(ntokens - 2)) 203 204 # Process rows of matrix 191 205 nrow = 0 192 206 while True: 193 194 207 fline = fidi.readline() 195 208 … … 202 215 [ntokens, tokens] = fltokens(fline) 203 216 204 # add row values to matrix (skipping eval_id) 205 217 # Add row values to matrix (skipping eval_id) 206 218 for i in range(ntokens - offset): 207 219 data[nrow, i] = tokens[i + offset] … … 209 221 nrow = nrow + 1 210 222 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 219 226 if (np.size(data, 0) > 1): 220 #dmean =mean(data)221 #dstddev = std 227 #dmean = mean(data) 228 #dstddev = std(data, 0) 222 229 [dmean, dstddev, dmeanci, dstddevci] = normfit_issm(data, 0.05) 223 230 else: … … 229 236 [dmean[0, i], dstddev[0, i], dmeanci[:, i], dstddevci[:, i]] = normfit_issm(data[:, i], 0.05) 230 237 231 dmin = data.min( 0)238 dmin = data.min(axis=0) 232 239 dquart1 = prctile_issm(data, 25, 0) 233 dmedian = np.median(data, 0)240 dmedian = np.median(data, axis=0) 234 241 dquart3 = prctile_issm(data, 75, 0) 235 dmax = data.max( 0)242 dmax = data.max(axis=0) 236 243 dmin95 = prctile_issm(data, 5, 0) 237 244 dmax95 = prctile_issm(data, 95, 0) 238 245 239 # N ote: the following line may cause the following warning240 # (should not crash or invalidate results) when one of241 # the inputs does not change with respect to the242 # other / s causing an internal divide-by - zero error243 244 # / usr / local / lib / python2.7 / dist - packages / numpy / lib / function_base.py:3163:245 # RuntimeWarning: invalid value encountered in true_divide246 # 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 250 257 dcorrel = np.corrcoef(data.T) 251 258 252 # divide the data into structures for consistency259 # Divide the data into structures for consistency 253 260 dresp = [] 254 255 261 for i in range(len(desc)): 256 262 dresp.append(struct()) 257 dresp[i].descriptor = str(desc[i])263 dresp[i].descriptor = desc[i] 258 264 dresp[i].sample = data[:, i] 259 265 dresp[i].mean = dmean[i] … … 294 300 [ntokens, tokens] = fltokens(fline) 295 301 nfeval = tokens[4] 296 print(' Dakota function evaluations = ' + str(int(nfeval)) + '.')302 print(' Dakota function evaluations = ' + str(int(nfeval))) 297 303 298 304 return nfeval … … 309 315 [ntokens, tokens] = fltokens(fline) 310 316 nsamp = tokens[3] 311 print(' Dakota samples = ' + str(int(nsamp)) + '.')317 print(' Dakota samples = ' + str(int(nsamp))) 312 318 313 319 return nsamp … … 340 346 dresp[-1].coefvar = tokens[12] 341 347 342 print(' Number of Dakota response functions = ' + str(len(dresp)) + '.')348 print(' Number of Dakota response functions = ' + str(len(dresp))) 343 349 344 350 return dresp … … 350 356 351 357 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') 353 359 return 354 360 355 print('Reading moment -based statistics for response functions:')361 print('Reading moment-based statistics for response functions:') 356 362 357 363 # skip column headings of moment - based statistics … … 376 382 dresp[-1].kurtosis = tokens[4] 377 383 378 print(' Number of Dakota response functions = ' + str(len(dresp)) + '.')384 print(' Number of Dakota response functions = ' + str(len(dresp))) 379 385 380 386 return dresp … … 433 439 dresp[i].stddevci[1, 0] = tokens[4] 434 440 435 print(' Number of Dakota response functions = ' + str(len(dresp)) + '.')441 print(' Number of Dakota response functions = ' + str(len(dresp))) 436 442 437 443 return dresp … … 495 501 fline = fidi.readline() 496 502 497 print(' Number of Dakota response functions = ' + str(len(dresp)) + '.')503 print(' Number of Dakota response functions = ' + str(len(dresp))) 498 504 499 505 return dresp … … 550 556 fline = fidi.readline() 551 557 552 print(' Number of Dakota response functions = ' + str(len(dresp)) + '.')558 print(' Number of Dakota response functions = ' + str(len(dresp))) 553 559 554 560 return dresp … … 565 571 return 566 572 567 print('Reading ' + fline + '.')573 print('Reading ' + fline) 568 574 569 575 cmat.title = fline … … 657 663 # if importance factors missing, skip to cdf 658 664 if not idvar: 659 print(' Importance Factors not available .')665 print(' Importance Factors not available') 660 666 dresp[-1].var = [] 661 667 dresp[-1].impfac = [] … … 716 722 # if cdf missing, skip to end of response function 717 723 if not icdf: 718 print(' Cumulative Distribution Function not available .')724 print(' Cumulative Distribution Function not available') 719 725 dresp[ndresp].cdf = [] 720 726 while (fline != '' and not fline.isspace()) and not strncmpi(fline, 'MV Statistics for ', 18) and not strncmp(fline, ' - ', 1): 721 727 fline = fidi.readline() 722 728 723 print(' Number of Dakota response functions = ' + str(len(dresp)) + '.')729 print(' Number of Dakota response functions = ' + str(len(dresp))) 724 730 725 731 return dresp … … 845 851 dresp[-1].vum = [] 846 852 847 print('Reading measures for volumetric uniformity .')853 print('Reading measures for volumetric uniformity') 848 854 fline = fidi.readline() 849 855 fline = fidi.readline() … … 881 887 [ntokens, tokens] = fltokens(fline) 882 888 method = tokens[2] 883 print('Dakota iterator \'' + str(method) + '\' completed .')889 print('Dakota iterator \'' + str(method) + '\' completed') 884 890 885 891 return method … … 905 911 906 912 # 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') 908 914 fidi.seek(ipos, 0) 909 915 return None
Note:
See TracChangeset
for help on using the changeset viewer.