class TestCrossKernelReconstructor(TestCase): KERNEL = None MEASURED = [13, 37] CROSS_KERNEL = [[1, 3], [2, 2], [3, 1]] def setUp(self): self.kernel_solver = SpyKernelSolverClass() self.reconstructor = verbose._VerboseFunctionalFieldReconstructorBase._CrossKernelReconstructor( self.kernel_solver(self.KERNEL), self.CROSS_KERNEL) def testReconstructsSingleMeasurements(self): self.checkCall([1, 1]) def testReconstructsSingleMeasurementsWithRegularization(self): self.checkCall([1, 1], regularization_parameter=1) def testReconstructsSerialMeasurements(self): self.checkCall([[1, 1], [-1, 2]]) def test_leave_one_out_methodWithoutRegularization(self): self.check_leave_one_out_method() def test_leave_one_out_methodWithRegularization(self): self.check_leave_one_out_method(regularization_parameter=1) def checkCall(self, solution, regularization_parameter=None): self.kernel_solver.set_solution(solution) expected = np.matmul(self.CROSS_KERNEL, solution) self.checkArrayLikeAlmostEqual( expected, (self.reconstructor(self.MEASURED) if regularization_parameter is None else self.reconstructor( self.MEASURED, regularization_parameter=regularization_parameter))) self.assertEqual(1, self.kernel_solver.call_counter['__call__']) self.check_rhs_and_regularization_parameter_arguments( regularization_parameter) def check_rhs_and_regularization_parameter_arguments( self, regularization_parameter): self.checkArrayLikeAlmostEqual(self.MEASURED, self.kernel_solver.rhs) if regularization_parameter is None: self.assertIsNone(self.kernel_solver.regularization_parameter) else: self.assertEqual(regularization_parameter, self.kernel_solver.regularization_parameter) def check_leave_one_out_method(self, regularization_parameter=None): result = np.random.random(2) self.kernel_solver.set_leave_one_out_errors(result) self.checkArrayAlmostEqual( result, (self.reconstructor.leave_one_out_errors(self.MEASURED) if regularization_parameter is None else self.reconstructor.leave_one_out_errors( self.MEASURED, regularization_parameter=regularization_parameter))) self.assertEqual( 1, self.kernel_solver.call_counter['leave_one_out_errors']) self.check_rhs_and_regularization_parameter_arguments( regularization_parameter)
def setUp(self): self.measurement_manager = self.MatrixMeasurementManager(self.PROBED) self.kernel_solver = SpyKernelSolverClass() self.reconstructor = self.PlainKernelSolutionFFR( range(self.measurement_manager.number_of_measurements), self.measurement_manager, KernelSolverClass=self.kernel_solver)
class TestFunctionalFieldReconstructorMayUseArbitraryKernelSolverClass( _TestCase): class MatrixMeasurementManager( LoadableFunctionalFieldReconstructor.MeasurementManagerBase): def __init__(self, probed): self._probed = np.array(probed) @property def number_of_measurements(self): return self._probed.shape[1] def probe(self, field): return self._probed[field] class PlainKernelSolutionFFR(LoadableFunctionalFieldReconstructor): def _wrap_kernel_solution(self, solution): return solution PROBED = [[1], [2], [3]] def setUp(self): self.measurement_manager = self.MatrixMeasurementManager(self.PROBED) self.kernel_solver = SpyKernelSolverClass() self.reconstructor = self.createLoadableReconstructor( self.PlainKernelSolutionFFR, range(self.measurement_manager.number_of_measurements), self.measurement_manager, KernelSolverClass=self.kernel_solver) def testNoRegularization(self): measurements = [42] solution = 'The answer to the universe, life, and everything else' self.kernel_solver.set_solution(solution) self.assertIs(self.reconstructor(measurements), solution) self.assertEqual(1, self.kernel_solver.call_counter['__init__']) self.assertEqual(1, self.kernel_solver.call_counter['__call__']) self.checkArrayLikeAlmostEqual(self.reconstructor._kernel, self.kernel_solver.kernel) self.checkArrayLikeAlmostEqual(np.reshape(measurements, (-1, 1)), self.kernel_solver.rhs) self.assertEqual(0, self.kernel_solver.regularization_parameter) def testRegularization(self): measurements = [42] regularization_parameter = 1 solution = 'What do you get if you multiply six by nine?' self.kernel_solver.set_solution(solution) self.assertIs( self.reconstructor( measurements, regularization_parameter=regularization_parameter), solution) self.assertEqual(1, self.kernel_solver.call_counter['__init__']) self.assertEqual(1, self.kernel_solver.call_counter['__call__']) self.checkArrayLikeAlmostEqual(self.reconstructor._kernel, self.kernel_solver.kernel) self.checkArrayLikeAlmostEqual(np.reshape(measurements, (-1, 1)), self.kernel_solver.rhs) self.assertEqual(regularization_parameter, self.kernel_solver.regularization_parameter)
def testDeprecationWarning(self): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") verbose._CrossKernelReconstructor(SpyKernelSolverClass([[]]), [[]]) self.assertEqual(1, len(w)) self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
def setUp(self): self.kernel_solver = SpyKernelSolverClass() self.reconstructor = verbose._VerboseFunctionalFieldReconstructorBase._CrossKernelReconstructor( self.kernel_solver(self.KERNEL), self.CROSS_KERNEL)