Changeset 25698


Ignore:
Timestamp:
10/20/20 19:29:01 (4 years ago)
Author:
jdquinn
Message:

CHG: Reworked results structure for Python

Location:
issm/trunk-jpl
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/classes/qmustatistics.py

    r25688 r25698  
    8585            for f in range(len(m['fields'])):
    8686                if not isinstance(m['fields'][f], str):
    87                     raise Exception('qmustatistics consistency check error: qmu.statistics.method[{}][\'fields\'[{}] is not a string!'.format(i, f))
     87                    raise Exception('qmustatistics consistency check error: qmu.statistics.method[{}][\'fields\'][{}] is not a string!'.format(i, f))
    8888            for s in range(len(m['steps'])):
    8989                if m['steps'][s] <= 0:
    90                     raise Exception('qmustatistics consistency check error: qmu.statistics.method[{}][\'steps\'[{}] should be > 0!'.format(i, s))
     90                    raise Exception('qmustatistics consistency check error: qmu.statistics.method[{}][\'steps\'][{}] should be > 0!'.format(i, s))
    9191                if m['steps'][s] > md.mesh.numberofvertices:
    92                     raise Exception('qmustatistics consistency check error: qmu.statistics.method[{}][\'steps\'[{}] should be < md.mesh.numberofvertices!'.format(i, s))
     92                    raise Exception('qmustatistics consistency check error: qmu.statistics.method[{}][\'steps\'][{}] should be < md.mesh.numberofvertices!'.format(i, s))
    9393    #}}}
    9494
  • issm/trunk-jpl/src/m/classes/results.py

    r25688 r25698  
    22
    33
    4 class results(object):
     4class results(object): #{{{
    55    """RESULTS class definition
    66
    77    Usage:
    8         results = results()
    9 
    10     TODO:
    11     - Modify output so that it matches that of
    12 
    13         disp(md.results.<<solutionstring>>)
    14 
    15     where <<solutionstring>> is one of the values from solve.m
     8        md.results = results()
    169    """
    1710
    18     def __init__(self, *args):  # {{{
     11    def __init__(self, *args): #{{{
    1912        pass
    20     # }}}
     13    #}}}
    2114
    22     def __repr__(self):  # {{{
    23         s = "   Model results:\n"
    24 
     15    def __repr__(self): #{{{
     16        s = ''
    2517        if 'step' in self.__dict__:
    26             s += "%s\n" % fielddisplay(self, 'step', "step number")
     18            s += '{}\n'.format(fielddisplay(self, 'step', "step number"))
    2719        if 'time' in self.__dict__:
    28             s += "%s\n" % fielddisplay(self, 'time', "time value")
     20            s += '{}\n'.format(fielddisplay(self, 'time', "time value"))
    2921        if 'SolutionType' in self.__dict__:
    30             s += "%s\n" % fielddisplay(self, 'SolutionType', "solution type")
     22            s += '{}\n'.format(fielddisplay(self, 'SolutionType', "solution type"))
    3123
    3224        for name in list(self.__dict__.keys()):
    3325            if name not in ['step', 'time', 'SolutionType', 'errlog', 'outlog']:
    3426                if isinstance(getattr(self, name), list):
    35                     s += "%s\n" % fielddisplay(self, name, "model results list")
     27                    s += '{}\n'.format(fielddisplay(self, name, "model results list"))
    3628                elif isinstance(getattr(self, name), results):
    37                     s += "%s\n" % fielddisplay(self, name, "model results case")
     29                    s += '{}\n'.format(fielddisplay(self, name, "model results case"))
    3830                else:
    39                     s += "%s\n" % fielddisplay(self, name, "")
     31                    s += '{}\n'.format(fielddisplay(self, name, ""))
    4032
    4133        if 'errlog' in self.__dict__:
    42             s += "%s\n" % fielddisplay(self, 'errlog', "error log file")
     34            s += '{}\n'.format(fielddisplay(self, 'errlog', "error log file"))
    4335        if 'outlog' in self.__dict__:
    44             s += "%s\n" % fielddisplay(self, 'outlog', "output log file")
     36            s += '{}\n'.format(fielddisplay(self, 'outlog', "output log file"))
    4537
    4638        return s
    47     # }}}
     39    #}}}
    4840
    49     def setdefaultparameters(self):  # {{{
     41    def setdefaultparameters(self): #{{{
    5042        #do nothing
    5143        return self
    52     # }}}
     44    #}}}
    5345
    54     def checkconsistency(self, md, solution, analyses):  # {{{
     46    def checkconsistency(self, md, solution, analyses): #{{{
    5547        return md
    56     # }}}
     48    #}}}
    5749
    58     def marshall(self, prefix, md, fid):  # {{{
     50    def marshall(self, prefix, md, fid): #{{{
    5951        pass
    60     # }}}
     52    #}}}
     53#}}}
     54
     55class resultselement(object): #{{{
     56    """RESULTSELEMENT class definition - Value of attribute of results
     57
     58    Usage:
     59        setattr(md.results, '<solution_type>', resultselement())
     60    """
     61
     62    def __init__(self, *args): #{{{
     63        self.elements = [result()]
     64    #}}}
     65
     66    def __repr__(self): #{{{
     67        s = '  1x{} struct array with fields:\n'.format(len(self.elements))
     68        s += '\n'
     69
     70        if len(self.elements) >= 1:
     71            for name in list(self.elements[0].__dict__.keys()):
     72                s += '    {}\n'.format(name)
     73
     74        return s
     75    #}}}
     76
     77    def __len__(self): #{{{
     78        return len(self.elements)
     79    #}}}
     80
     81    def __getitem__(self, index): #{{{
     82        while True:
     83            try:
     84                return self.elements[index]
     85            except:
     86                self.elements.append(result())
     87    #}}}
     88
     89    def __setitem__(self, index, value): #{{{
     90        while True:
     91            try:
     92                self.elements[index] = value
     93                break
     94            except:
     95                self.elements.append(result())
     96    #}}}
     97
     98    def setdefaultparameters(self): #{{{
     99        return self
     100    #}}}
     101
     102    def checkconsistency(self, md, solution, analyses): #{{{
     103        return md
     104    #}}}
     105
     106    def marshall(self, prefix, md, fid): #{{{
     107        pass
     108    #}}}
     109#}}}
     110
     111class result(object): #{{{
     112    """RESULT class definition - Element of resultselement::elements
     113
     114    Usage:
     115        resultselement.elements.append(result())
     116    """
     117
     118    def __init__(self, *args): #{{{
     119        pass
     120    #}}}
     121
     122    def setdefaultparameters(self): #{{{
     123        return self
     124    #}}}
     125
     126    def checkconsistency(self, md, solution, analyses): #{{{
     127        return md
     128    #}}}
     129
     130    def marshall(self, prefix, md, fid): #{{{
     131        pass
     132    #}}}
     133#}}}
  • issm/trunk-jpl/src/m/solve/loadresultsfromdisk.m

    r25688 r25698  
    6565        end
    6666
    67 %post processes qmu results if necessary
     67%postprocess qmu results if necessary
    6868else
    6969        md=postqmu(md);
  • issm/trunk-jpl/src/m/solve/loadresultsfromdisk.py

    r25688 r25698  
    33import numpy as np # Remove: used temporarily for debugging
    44
    5 from helpers import fieldnames
     5from helpers import fieldnames, struct
    66from parseresultsfromdisk import parseresultsfromdisk
    77from postqmu import postqmu
     
    3434            raise OSError(err_msg)
    3535
    36         # Initialize md.results if not a structure yet
    37         if not isinstance(md.results, results):
     36        # # Initialize md.results if not a structure yet
     37        if not isinstance(md.results, struct):
    3838            md.results = results()
    3939
     
    7373            setattr(md.results, structure[0].SolutionType, structure[0])
    7474
    75     # Post processes QMU results if necessary
     75    # Postprocess QMU results if necessary
    7676    else:
    7777        md = postqmu(md)
  • issm/trunk-jpl/src/m/solve/parseresultsfromdisk.py

    r25694 r25698  
    44import numpy as np
    55
    6 import results as resultsclass
     6from results import resultselement
    77
    88
     
    2525    saveres = []
    2626
    27     #if we have done split I / O, ie, we have results that are fragmented across patches,
     27    #if we have done split I/O, ie, we have results that are fragmented across patches,
    2828    #do a first pass, and figure out the structure of results
    2929    loadres = ReadDataDimensions(fid)
     
    8383    except IOError as e:
    8484        raise IOError("parseresultsfromdisk error message: could not open {} for binary reading".format(filename))
    85        
     85
    8686    # Collect all results in a list
    8787    allresults = []
     
    108108
    109109    # Ok, now construct structure
    110     results = []
    111     for i in range(len(allsteps)):
    112         results.append(resultsclass.results())
     110    results = resultselement()
    113111    for i in range(numresults):
    114112        result = allresults[i]
  • issm/trunk-jpl/test/NightlyRun/test2006.m

    r25688 r25698  
    182182md=solve(md,'Transient');
    183183
    184 disp(mds.results.StatisticsSolution)
    185 
    186184%compare statistics with our own here:
    187 svalues=mds.results.StatisticsSolution(end).SealevelSamples; %all values at locations. 
     185svalues=mds.results.StatisticsSolution(end).SealevelSamples; %all values at locations.
    188186
    189187dvalues=zeros(md.qmu.method.params.samples,length(locations));
     
    191189        dvalues(i,:)=md.results.dakota.modelresults{i}.TransientSolution(end).Sealevel(locations);
    192190end
    193 
    194 disp(dvalues)
    195 disp(size(dvalues))
    196191
    197192samplesnorm=norm(dvalues-svalues,'fro');
  • issm/trunk-jpl/test/NightlyRun/test2006.py

    r25688 r25698  
    135135md.qmu.variables.surfaceload = qmuvar.surfaceload
    136136
    137 locations = [1, 5, 10, 15, 20]
     137locations = np.array([1, 5, 10, 15, 20])
    138138
    139139# Responses #{{{
     
    208208md.qmu.statistics.method[2]['fields'] = ['Sealevel', 'BslrIce']
    209209md.qmu.statistics.method[2]['steps'] = np.arange(1, 10 + 1).reshape(1, -1)
    210 md.qmu.statistics.method[2]['indices'] = np.asarray(locations).reshape(1, -1)
     210md.qmu.statistics.method[2]['indices'] = locations.reshape(1, -1)
    211211#}}}
    212212
     
    219219
    220220# Compare statistics with our own here
    221 #
    222 # TODO: SealevelSamples should be an attribute of mds.results.StatisticsSolution[-1] (see test2006.m)
    223 #
    224 svalues = mds.results.StatisticsSolution[0].SealevelSamples # all values at locations
     221svalues = mds.results.StatisticsSolution[-1].SealevelSamples # all values at locations
    225222
    226223dvalues = np.zeros((md.qmu.method.params.samples, len(locations)))
    227 
    228224for i in range(md.qmu.method.params.samples):
    229     dvalues[i, :] = md.results.dakota.modelresults[i].TransientSolution[-1].Sealevel[locations].flatten()
     225    dvalues[i, :] = md.results.dakota.modelresults[i].TransientSolution[-1].Sealevel[locations - 1].flatten()
    230226
    231227samplesnorm = np.linalg.norm(dvalues - svalues, 'fro')
Note: See TracChangeset for help on using the changeset viewer.