Coverage for tests/test_fitter.py: 12%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

98 statements  

1from numpy.testing import run_module_suite 

2import numpy as np 

3from spectractor.fit.fitter import FitWorkspace, run_minimisation, run_emcee, run_minimisation_sigma_clipping 

4from spectractor.config import set_logger 

5from spectractor import parameters 

6 

7import os 

8 

9 

10class LineFitWorkspace(FitWorkspace): 

11 

12 def __init__(self, x, y, yerr, file_name="", nwalkers=18, nsteps=1000, burnin=100, nbins=10, 

13 verbose=0, plot=False, live_fit=False, truth=None): 

14 FitWorkspace.__init__(self, file_name, nwalkers, nsteps, burnin, nbins, verbose, plot, live_fit, truth=truth) 

15 self.my_logger = set_logger(self.__class__.__name__) 

16 self.x = x 

17 self.data = y 

18 self.err = yerr 

19 self.a = 1 

20 self.b = 1 

21 self.p = np.array([self.a, self.b]) 

22 self.input_labels = ["a", "b"] 

23 self.axis_names = ["$a$", "$b$"] 

24 self.bounds = np.array([(-100, 100), (-100, 100)]) 

25 self.nwalkers = max(2 * self.ndim, nwalkers) 

26 

27 def simulate(self, a, b): 

28 self.model = a * self.x + b 

29 self.model_err = np.zeros_like(self.x) 

30 return self.x, self.model, self.model_err 

31 

32 

33def test_fitworkspace(): 

34 # Create mock data 

35 N = 100 

36 a = 5 

37 b = -1 

38 truth = (a, b) 

39 sigma = 0.1 

40 x = np.linspace(0, 1, N) 

41 y = a * x + b 

42 yerr = sigma * np.ones_like(y) 

43 y += np.random.normal(scale=sigma, size=N) 

44 parameters.VERBOSE = True 

45 

46 # Do the fits 

47 file_name = "./outputs/test_linefitworkspace.txt" 

48 w = LineFitWorkspace(x, y, yerr, file_name, truth=truth, nwalkers=20, nsteps=5000, burnin=1000, nbins=20) 

49 run_minimisation(w, method="minimize") 

50 assert np.all([np.abs(w.p[i] - truth[i]) / sigma < 1 for i in range(w.ndim)]) 

51 w.p = np.array([1, 1]) 

52 run_minimisation(w, method="basinhopping") 

53 assert np.all([np.abs(w.p[i] - truth[i]) / sigma < 1 for i in range(w.ndim)]) 

54 # w.p = np.array([4, -0.5]) 

55 # run_minimisation(w, method="least_squares") 

56 # w.my_logger.warning(f"{w.p} {w.ndim} {sigma} {truth}") 

57 # assert np.all([np.abs(w.p[i] - truth[i]) / sigma < 1 for i in range(w.ndim)]) 

58 w.p = np.array([1, 1]) 

59 run_minimisation(w, method="minuit") 

60 assert np.all([np.abs(w.p[i] - truth[i]) / sigma < 1 for i in range(w.ndim)]) 

61 w.p = np.array([1, 1]) 

62 run_minimisation(w, method="newton") 

63 assert np.all([np.abs(w.p[i] - truth[i]) / sigma < 1 for i in range(w.ndim)]) 

64 

65 def lnprob(p): 

66 lp = w.lnprior(p) 

67 if not np.isfinite(lp): 

68 return -1e20 

69 return lp + w.lnlike(p) 

70 

71 run_emcee(w, ln=lnprob) 

72 w.analyze_chains() 

73 

74 assert w.chains.shape == (5000, 20, 2) 

75 assert np.all(w.gelmans < 0.03) 

76 assert os.path.exists(file_name.replace(".txt", "_emcee.h5")) 

77 assert os.path.exists(file_name.replace(".txt", "_emcee_convergence.pdf")) 

78 assert os.path.exists(file_name.replace(".txt", "_emcee_triangle.pdf")) 

79 assert np.all([np.abs(w.p[i] - truth[i]) / np.sqrt(w.cov[i, i]) < 3 for i in range(w.ndim)]) 

80 

81 

82def test_minimisation_sigma_clipping(): 

83 # Create mock data 

84 N = 100 

85 a = 5 

86 b = -1 

87 truth = (a, b) 

88 sigma = 0.1 

89 x = np.linspace(0, 1, N) 

90 y = a * x + b 

91 yerr = sigma * np.ones_like(y) 

92 y += np.random.normal(scale=sigma, size=N) 

93 parameters.VERBOSE = True 

94 

95 # Add outliers 

96 outliers = [3, 4, 5, 6, 7, 22, 23, 24, 25, 26, 27, 28, 29, 80, 81] 

97 y[outliers[:10]] = 4 

98 y[outliers[10:]] = -3 

99 

100 # Do the fits 

101 sigma = 5 

102 clip_niter = 4 

103 w = LineFitWorkspace(x, y, yerr, file_name="", truth=truth, nwalkers=20, nsteps=5000, burnin=1000, nbins=20) 

104 run_minimisation_sigma_clipping(w, method="minimize", sigma_clip=sigma, niter_clip=clip_niter) 

105 assert np.all([np.abs(w.p[i] - truth[i]) / sigma < 1 for i in range(w.ndim)]) 

106 assert np.all(outliers == w.outliers) 

107 w.p = np.array([1, 1]) 

108 w.outliers = [] 

109 run_minimisation_sigma_clipping(w, method="basinhopping", sigma_clip=sigma, niter_clip=clip_niter) 

110 assert np.all([np.abs(w.p[i] - truth[i]) / sigma < 1 for i in range(w.ndim)]) 

111 assert np.all(outliers == w.outliers) 

112 # w.p = np.array([1, 1]) 

113 # w.outliers = [] 

114 # run_minimisation_sigma_clipping(w, method="least_squares", sigma_clip=sigma, niter_clip=clip_niter) 

115 # assert np.all([np.abs(w.p[i] - truth[i]) / sigma < 1 for i in range(w.ndim)]) 

116 # assert np.all(outliers == w.outliers) 

117 w.p = np.array([1, 1]) 

118 w.outliers = [] 

119 run_minimisation_sigma_clipping(w, method="minuit", sigma_clip=sigma, niter_clip=clip_niter) 

120 assert np.all([np.abs(w.p[i] - truth[i]) / sigma < 1 for i in range(w.ndim)]) 

121 assert np.all(outliers == w.outliers) 

122 w.p = np.array([1, 1]) 

123 w.outliers = [] 

124 run_minimisation_sigma_clipping(w, method="newton", sigma_clip=sigma, niter_clip=clip_niter) 

125 assert np.all([np.abs(w.p[i] - truth[i]) / sigma < 1 for i in range(w.ndim)]) 

126 assert np.all(outliers == w.outliers) 

127 

128 

129if __name__ == "__main__": 129 ↛ 130line 129 didn't jump to line 130, because the condition on line 129 was never true

130 run_module_suite()