|
23 | 23 | from diffpy.srfit.fitbase.parameter import Parameter
|
24 | 24 | from diffpy.srfit.fitbase.profile import Profile
|
25 | 25 |
|
26 |
| -from .utils import capturestdout |
27 |
| - |
28 | 26 |
|
29 | 27 | class TestFitRecipe(unittest.TestCase):
|
30 | 28 |
|
@@ -239,32 +237,51 @@ def testResidual(self):
|
239 | 237 |
|
240 | 238 | return
|
241 | 239 |
|
242 |
| - def testPrintFitHook(self): |
243 |
| - "check output from default PrintFitHook." |
244 |
| - self.recipe.addVar(self.fitcontribution.c) |
245 |
| - self.recipe.restrain("c", lb=5) |
246 |
| - (pfh,) = self.recipe.getFitHooks() |
247 |
| - out = capturestdout(self.recipe.scalarResidual) |
248 |
| - self.assertEqual("", out) |
249 |
| - pfh.verbose = 1 |
250 |
| - out = capturestdout(self.recipe.scalarResidual) |
251 |
| - self.assertTrue(out.strip().isdigit()) |
252 |
| - self.assertFalse("\nRestraints:" in out) |
253 |
| - pfh.verbose = 2 |
254 |
| - out = capturestdout(self.recipe.scalarResidual) |
255 |
| - self.assertTrue("\nResidual:" in out) |
256 |
| - self.assertTrue("\nRestraints:" in out) |
257 |
| - self.assertFalse("\nVariables" in out) |
258 |
| - pfh.verbose = 3 |
259 |
| - out = capturestdout(self.recipe.scalarResidual) |
260 |
| - self.assertTrue("\nVariables" in out) |
261 |
| - self.assertTrue("c = " in out) |
262 |
| - return |
263 |
| - |
264 | 240 |
|
265 | 241 | # End of class TestFitRecipe
|
266 | 242 |
|
| 243 | + |
267 | 244 | # ----------------------------------------------------------------------------
|
| 245 | +def testPrintFitHook(capturestdout): |
| 246 | + "check output from default PrintFitHook." |
| 247 | + recipe = FitRecipe("recipe") |
| 248 | + recipe.fithooks[0].verbose = 0 |
| 249 | + |
| 250 | + # Set up the Profile |
| 251 | + profile = Profile() |
| 252 | + x = linspace(0, pi, 10) |
| 253 | + y = sin(x) |
| 254 | + profile.setObservedProfile(x, y) |
| 255 | + |
| 256 | + # Set up the FitContribution |
| 257 | + fitcontribution = FitContribution("cont") |
| 258 | + fitcontribution.setProfile(profile) |
| 259 | + fitcontribution.setEquation("A*sin(k*x + c)") |
| 260 | + fitcontribution.A.setValue(1) |
| 261 | + fitcontribution.k.setValue(1) |
| 262 | + fitcontribution.c.setValue(0) |
| 263 | + |
| 264 | + recipe.addContribution(fitcontribution) |
| 265 | + |
| 266 | + recipe.addVar(fitcontribution.c) |
| 267 | + recipe.restrain("c", lb=5) |
| 268 | + (pfh,) = recipe.getFitHooks() |
| 269 | + out = capturestdout(recipe.scalarResidual) |
| 270 | + assert "" == out |
| 271 | + pfh.verbose = 1 |
| 272 | + out = capturestdout(recipe.scalarResidual) |
| 273 | + assert out.strip().isdigit() |
| 274 | + assert "\nRestraints:" not in out |
| 275 | + pfh.verbose = 2 |
| 276 | + out = capturestdout(recipe.scalarResidual) |
| 277 | + assert "\nResidual:" in out |
| 278 | + assert "\nRestraints:" in out |
| 279 | + assert "\nVariables" not in out |
| 280 | + pfh.verbose = 3 |
| 281 | + out = capturestdout(recipe.scalarResidual) |
| 282 | + assert "\nVariables" in out |
| 283 | + assert "c = " in out |
| 284 | + return |
268 | 285 |
|
269 | 286 |
|
270 | 287 | if __name__ == "__main__":
|
|
0 commit comments