class DepletedSamplerTester(TestCase):
    """
    Class that reads two similar files and validates the averaging
    and uncertainty propagation.
    """
    def setUp(self):
        self.reader0 = DepletionReader(DEP_FILES['0'])
        self.reader0.read()
        self.reader1 = DepletionReader(DEP_FILES['1'])
        self.reader1.read()
        self.sampler = DepletionSampler([DEP_FILES[x] for x in ('0', '1')])

    def test_depSamplerValidCalcs(self):
        """
        Run through all variables and materials and check error propagation
        and averaging.
        """
        errMsg = "{varN} {qty} for material {matN}"
        for name, material in self.sampler.iterMaterials():
            for varName, varData in iteritems(material.data):
                r0 = self.reader0.materials[name].data[varName]
                r1 = self.reader1.materials[name].data[varName]
                samplerUnc = material.uncertainties[varName]
                expectedMean, expectedStd = computeMeansErrors(r0, r1)
                for qty, actual, expected in zip(('mean', 'uncertainty'),
                                                 (varData, samplerUnc),
                                                 (expectedMean, expectedStd)):
                    msg = errMsg.format(qty=qty, varN=varName, matN=material)
                    try:
                        assert_allclose(actual,
                                        expected,
                                        err_msg=msg,
                                        rtol=1E-5)
                    except AssertionError as ae:
                        critical("\nMaterial {} - value {}".format(
                            name, varName))
                        for aRow, eRow in zip(actual, expected):
                            critical('Actual:   {}'.format(aRow))
                            critical('Expected: {}'.format(eRow))
                            den = aRow.copy()
                            if isinstance(den, ndarray):
                                den[where(den == 0)] = 1
                            else:
                                den = den or 1
                            rDiff = (fabs(aRow - eRow) / den)
                            critical('Relative: {}'.format(rDiff))

                        raise ae

    def test_getitem(self):
        """Verify the getitem method for extracting materials."""
        with self.assertRaises(KeyError):
            self.sampler['this should fail']
        for name, mat in iteritems(self.sampler.materials):
            fromGetItem = self.sampler[name]
            self.assertIs(fromGetItem, mat, msg=name)
 def test_readerWithUpdatedSettings(self):
     """Verify the settings passed to the reader reflect the update."""
     from serpentTools.parsers.depletion import DepletionReader
     with settings.rc as tempRC:
         tempRC['depletion.metadataKeys'] = ['ZAI']
         reader = DepletionReader('None')
     self.assertTrue(reader.settings['metadataKeys'] == ['ZAI'])
    def setUpClass(cls):
        # Silence the debugger but we don't care about checking messages
        logger = LoggerMixin()
        logger.attach()

        cls.expectedMaterials = {
            cls.MATERIAL,
        }
        if cls.PROCESS_TOTAL:
            cls.expectedMaterials.add('total')
        # Set some settings to control the output
        with rc as tempRC:
            tempRC['verbosity'] = 'debug'
            tempRC['depletion.materials'] = [
                cls.MATERIAL,
            ]
            tempRC['depletion.processTotal'] = cls.PROCESS_TOTAL
            tempRC['depletion.materialVariables'] = (cls.EXPECTED_VARIABLES)
            cls.reader = DepletionReader(cls.FILE)
            cls.reader.read()
        logger.detach()
 def setUpClass(cls):
     cls.filePath = os.path.join(TEST_ROOT, 'ref_dep.m')
     cls.processTotal = True
     cls.materials = ['fuel']
     cls.expectedMaterials = set(cls.materials)
     cls.expectedVariables = {'BURNUP', 'ADENS', 'ING_TOX'}
     if cls.processTotal:
         cls.expectedMaterials.add('total')
     with rc as tempRC:
         tempRC['verbosity'] = 'debug'
         tempRC['depletion.processTotal'] = True
         tempRC['depletion.materials'] = ['fuel', ]
         tempRC['depletion.materialVariables'] = [
             'BURNUP', 'ADENS', 'ING_TOX']
         tempRC['depletion.processTotal'] = cls.processTotal
         tempRC['depletion.materials'] = cls.materials
         tempRC['depletion.materialVariables'] = (
             list(cls.expectedVariables)
         )
         cls.reader = DepletionReader(cls.filePath)
     cls.reader.read()
     read(cls.filePath, DepletionReader)
 def setUp(self):
     self.reader0 = DepletionReader(DEP_FILES['0'])
     self.reader0.read()
     self.reader1 = DepletionReader(DEP_FILES['1'])
     self.reader1.read()
     self.sampler = DepletionSampler([DEP_FILES[x] for x in ('0', '1')])
 def setUpClass(cls):
     cls.reader = DepletionReader(DEP_FILE_PATH)
     cls.reader.read()