示例#1
0
    def testBasic(self):
        """
        Test functionality with a basic json output
        """

        # Test basic read
        data = mooseutils.ReporterReader(self._basicfile)

        # Test __getitem__
        self.assertEqual(data[('constant', 'str_vec')],
                         ['ten', 'eleven', 'twelve', 'thirteen'])
        self.assertEqual(data[[('constant', 'num_2'), ('constant', 'int_3')]],
                         [5.0, -3])

        # Test __bool__
        self.assertTrue(data)

        # Test __contains__
        self.assertTrue(('constant', 'int_vec') in data)
        self.assertFalse(('foo', 'bar') in data)
        self.assertFalse(('foo', 'int_vec') in data)
        self.assertFalse(('constant', 'bar') in data)

        # Test clear()
        tmp = mooseutils.ReporterReader(self._basicfile)
        tmp.clear()
        self.assertFalse(tmp)

        # Test variables()
        vars = data.variables()
        self.assertEqual(len(vars), 10)
        self.assertEqual(vars[0], ('constant', 'int_1'))
        self.assertEqual(vars[1], ('constant', 'int_2'))
        self.assertEqual(vars[2], ('constant', 'int_3'))
        self.assertEqual(vars[3], ('constant', 'int_vec'))
        self.assertEqual(vars[4], ('constant', 'num_1'))
        self.assertEqual(vars[5], ('constant', 'num_2'))
        self.assertEqual(vars[6], ('constant', 'str'))
        self.assertEqual(vars[7], ('constant', 'str_vec'))
        self.assertEqual(vars[8], ('constant', 'vec_1'))
        self.assertEqual(vars[9], ('constant', 'vec_2'))

        # Test repr()
        output, imports = data.repr()
        output += ["print('Success' if data else 'Fail')"]
        script = '{}_repr.py'.format(self.__class__.__name__)
        with open(script, 'w') as fid:
            fid.write('\n'.join(imports))
            fid.write('\n'.join(output))
        self.assertTrue(os.path.exists(script))
        out = subprocess.check_output(['python', script])
        self.assertIn('Success', out.decode())
        os.remove(script)
示例#2
0
    def testTimeData(self):
        """
        Test functionality with one file per timestep
        """

        data = mooseutils.ReporterReader(self._onepertimefile)
        self.assertEqual(data.times(), [0.0, 1.0, 2.0, 3.0])
示例#3
0
    def testParts(self):
        """
        Test functionality for reading different parts
        """

        data = mooseutils.ReporterReader(self._partsfile)
        self.assertEqual(data._data['number_of_parts'], 2)
        self.assertEqual(data._data['part'], 0)

        data.update(part=1)
        self.assertEqual(data._data['part'], 1)
示例#4
0
    def testTimeData(self):
        """
        Test functionality with time dependent data
        """

        data = mooseutils.ReporterReader(self._timefile)

        # Test __getitem__
        self.assertEqual(data[('accumulate', 'rep:str')],
                         ["two", "two", "two", "two", "two", "two"])

        # Test times()
        self.assertEqual(data.times(), [0.0, 1.0, 2.0, 3.0, 4.0, 5.0])

        # Test update()
        pp = []
        for time in data.times():
            pp.append(time)
            data.update(time)
            self.assertEqual(data[('accumulate', 'pp:value')], pp)
示例#5
0
def main():

    # Command-line options
    opt = command_line_options()

    # Make sure stat is uppercase
    opt.stat = opt.stat.upper()
    # Define value_name-display_name map
    opt.names = json.loads(opt.names)

    # Read reporter data
    data = mooseutils.ReporterReader(opt.filename)
    if opt.time is not None:
        data.update(opt.time)
    if opt.object is None:
        opt.object = next(var[0] for var in data.variables() if data.info(var[0])['type'] == 'SobolReporter')
    repinfo = data.info(opt.object)
    if not len(opt.values):
        opt.values = [var[1] for var in data.variables() if var[0] == opt.object]

    # Dictionary for data storage
    frame = dict(param_names=[], ci_levels=None, vector_names=[])
    frame['param_names'] = [(opt.param_names[i] if i < len(opt.param_names) else str(i)) for i in range(repinfo['num_params'])]
    frame['indices'] = repinfo['indices']
    levels = False
    if not opt.ignore_ci and 'confidence_intervals' in repinfo:
        levels = repinfo['confidence_intervals']['levels']
        frame['ci_levels'] = (min(levels), max(levels)) if opt.confidence_interval is None else tuple(opt.confidence_interval)
    for val in opt.values:
        vn = opt.names[val] if val in opt.names else val
        rep = data[(opt.object, val)]
        frame['vector_names'].append(vn)
        repv = data[(opt.object, val)][opt.stat]
        frame[vn] = dict()
        frame[vn]['val'] = np.array(repv[0])
        if levels:
            frame[vn]['ci_minus'] = np.array(repv[1][levels.index(frame['ci_levels'][0])])
            frame[vn]['ci_plus'] = np.array(repv[1][levels.index(frame['ci_levels'][1])])

    # Tables
    if opt.format < 3:

        if opt.stat == 'TOTAL':
            tab = totalTable(frame, opt.number_format)
        elif opt.stat == 'FIRST_ORDER':
            tab = firstOrderTable(frame, opt.number_format)
        elif opt.stat == 'SECOND_ORDER':
            tab = secondOrderTable(frame, opt.number_format)

        if opt.format == 0:
            out = tab.to_string(index=False)
        elif opt.format == 1:
            out = tab.to_markdown(index=False)
        elif opt.format == 2:
            out = tab.to_latex(index=False)

        if opt.output is None:
            print(out)
        else:
            with open(os.path.abspath(opt.output), "w") as fid:
                fid.write(out)


    elif opt.format >= 3:

        if opt.format == 3:
            fig = barPlot(frame, opt.stat, opt.log_scale)
        elif opt.format == 4:
            fig = heatmap(frame, opt.stat, opt.log_scale)

        if opt.output is None:
            fig.show()
        else:
            plotly.io.write_image(fig, os.path.abspath(opt.output))

    return 0
示例#6
0
                for vec in vars:
                    obj_val = vec.split(':')
                    if obj_val[-1] == 'converged':
                        ind[obj_val[0]] = np.where(vdata[vec])[0]
                for vec in vars:
                    obj_val = vec.split(':')
                    if obj_val[-1] == 'converged':
                        continue
                    val = vdata[vec][ind[obj_val[0]]].tolist() if obj_val[0] in ind else vdata[vec].tolist()
                    if vec in data:
                        data[vec].extend(val)
                    else:
                        data[vec] = val

            else:
                rdata = mooseutils.ReporterReader(fname)
                vars = rdata.variables()
                ind = dict()
                for vec in vars:
                    obj_val = vec[1].split(':')
                    if obj_val[-1] == 'converged':
                        ind[obj_val[0]] = np.where(rdata[vec])[0]
                for vec in vars:
                    obj_val = vec[1].split(':')
                    if obj_val[-1] == 'converged' or rdata.info(vec)['type'] != 'std::vector<double>':
                        continue
                    val = np.array(rdata[vec])[ind[obj_val[0]]].tolist() if obj_val[0] in ind else rdata[vec]
                    if vec[1] in data:
                        data[vec[1]].extend(val)
                    else:
                        data[vec[1]] = val
示例#7
0
def main():

    # Command-line options
    opt = command_line_options()

    # Convert stats to uppercase
    opt.stats = [stat.upper() for stat in opt.stats]

    # Define value_name-display_name map
    opt.names = json.loads(opt.names)
    opt.stat_names = dict((key.upper(), value) for (key, value) in json.loads(opt.stat_names).items())

    # Grab ALL the data
    frame = dict(time=[], object_name=[], vector_name=[], statistic=[], ci_levels=[], value=[], confidence_interval=[])
    for file in opt.filenames:
        data = mooseutils.ReporterReader(file)

        times = opt.times if len(opt.times) else data.times()
        for time in times:
            data.update(time)
            for var in data.variables():
                # Object name
                objname = var[0]
                if (len(opt.objects) and objname not in opt.objects) or data.info(objname)['type'] != 'StatisticsReporter':
                    continue

                # Split value name into vector name and stat
                tmp = var[1].split('_')
                vecname = '_'.join(tmp[:-1])
                if len(opt.values) and vecname not in opt.values:
                    continue
                stat = tmp[-1]
                if len(opt.stats) and stat not in opt.stats:
                    continue

                # Checks are done so start pushing back
                frame['time'].append(time)
                frame['object_name'].append(objname)
                frame['vector_name'].append(opt.names[vecname] if vecname in opt.names else vecname)
                frame['statistic'].append(opt.stat_names[stat] if stat in opt.stat_names else stat)

                # Grab value and confidence interval
                val = data[var]
                repinfo = data.info(objname)
                ci_levels = np.nan
                ci_val = np.nan
                if not opt.ignore_ci and 'confidence_intervals' in repinfo:
                    levels = repinfo['confidence_intervals']['levels']
                    ci_levels = (min(levels), max(levels)) if opt.confidence_interval is None else tuple(opt.confidence_interval)
                    ci1, ci2 = (val[1][levels.index(ci_levels[0])], val[1][levels.index(ci_levels[1])])
                    if isinstance(val[0], list):
                        val[0] = tuple(val[0])
                        ci1 = tuple(ci1)
                        ci2 = tuple(ci2)
                    ci_val = (ci1, ci2)
                frame['value'].append(val[0])
                frame['ci_levels'].append(ci_levels)
                frame['confidence_interval'].append(ci_val)


    xname = opt.names[opt.xvalue] if opt.xvalue in opt.names else opt.xvalue
    if opt.xlabel is None:
        opt.xlabel = xname

    if opt.xvalue != 'Time' and xname not in frame['vector_name']:
        for file in opt.filenames:
            data = mooseutils.ReporterReader(file)

            times = opt.times if len(opt.times) else data.times()
            for time in times:
                data.update(time)
                for var in data.variables():
                    if var[1] == opt.xvalue:
                        for stat in set(frame['statistic']):
                            frame['time'].append(time)
                            frame['object_name'].append(var[0])
                            frame['vector_name'].append(opt.xlabel)
                            frame['statistic'].append(stat)
                            frame['value'].append(tuple(data[var]))
                            frame['ci_levels'].append(np.nan)
                            frame['confidence_interval'].append(np.nan)
                        break
    elif xname in frame['vector_name'] and xname != opt.xlabel:
        for vn in frame['vector_name']:
            if vn == xname:
                vn = opt.xlabel


    frame = pd.DataFrame(frame)

    if opt.format == 0:
        textTable(frame, opt.number_format, opt.output)
    elif opt.format == 1:
        markdownTable(frame, opt.number_format, opt.output)
    elif opt.format == 2:
        latexTable(frame, opt.number_format, opt.output)
    elif opt.format == 3:
        barPlot(frame, opt.output)
    elif opt.format == 4:
        linePlot(frame, opt.xlabel, opt.output)