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 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 _mismatchedFiles(self,
                      badFilePath,
                      errorType=MismatchedContainersError):
     files = [badFilePath, DEP_FILES['0']]
     with self.assertRaises(errorType):
         DepletionSampler(files)