def test_intproj(old_numpy_printing, override_plot_backend): p = plot.IntervalProjection() r = p._repr_html_() check_empty(r, 'IntervalProjection', nsummary=8) x = np.arange(5, 8, 0.5) y = np.asarray([2, 3, 4, 5, 4, 3]) dy = y / 2 d = Data1D('n n', x, y, staterror=dy) m = Const1D() fit = Fit(d, m, stat=Chi2()) fr = fit.fit() assert fr.succeeded p.prepare(min=1, max=6, nloop=10) p.calc(fit, m.c0) r = p._repr_html_() assert r is not None if plot.backend.name == 'pylab': assert '<summary>IntervalProjection</summary>' in r assert '<svg ' in r return assert '<summary>IntervalProjection (8)</summary>' in r assert '<div class="dataname">x</div><div class="dataval">[ 1. 1.555556 2.111111 2.666667 3.222222 3.777778 4.333333 4.888889\n 5.444444 6. ]</div>' in r assert '<div class="dataname">nloop</div><div class="dataval">10</div>' in r
def test_errresults_limits_interval(): """Missing an error limit""" d = Data1D('dx', [1, 2, 3], [4, 2, 2], [1.2, 0.9, 0.9]) m = Const1D() m.c0 = 3 f = fit.Fit(d, m, stat=Chi2()) er = f.est_errors() # perhaps should just fake this instead? assert len(er.parmaxes) == 1 er.parmaxes = ([0.1, 0.2], ) r = er._repr_html_() assert r is not None print(r) assert '<summary>covariance 1σ (68.2689%) bounds</summary>' in r assert '<summary>Summary (2)' in r assert '<td>const1d.c0</td>' in r assert '<div class="dataname">Fitting Method</div><div class="dataval">levmar</div>' in r assert '<div class="dataname">Statistic</div><div class="dataval">chi2</div>' in r assert '<tr><td>const1d.c0</td><td> 3</td><td> -0.562226</td><td>(1.000000e-01, 2.000000e-01)</td></tr>' in r
def test_stats_calc_stat_chi2_nostat(usesys): """chi-quare statistic with no staterror fails""" data, model = setup_single(False, usesys) statobj = Chi2() with pytest.raises(StatErr) as excinfo: statobj.calc_stat(data, model) emsg = 'If you select chi2 as the statistic, all datasets ' + \ 'must provide a staterror column' assert emsg == str(excinfo.value)
def test_data2d_int_eval_model_to_fit(array_sizes_fixture): from sherpa.fit import Fit from sherpa.optmethods import LevMar from sherpa.stats import Chi2 from sherpa.models import Gauss2D x0, x1, dx, y = array_sizes_fixture data2 = Data2DInt('name', x0.flatten(), x0.flatten() + dx, x1.flatten(), x1.flatten() + dx, y.flatten(), staterror=numpy.sqrt(y).flatten()) model2 = Gauss2D() fitter = Fit(data2, model2, Chi2(), LevMar()) fitter.fit() # Failed in Sherpa 4.11.0
def test_regproj(old_numpy_printing, override_plot_backend): p = plot.RegionProjection() r = p._repr_html_() check_empty(r, 'RegionProjection', nsummary=13) x = np.arange(5, 8, 0.5) y = np.asarray([2, 3, 4, 5, 4, 3]) dy = y / 2 d = Data1D('n n', x, y, staterror=dy) m = Polynom1D() m.c1.thaw() fit = Fit(d, m, stat=Chi2()) fr = fit.fit() assert fr.succeeded p.prepare(min=(-2, -1), max=(2, 2), nloop=(10, 20)) p.calc(fit, m.c0, m.c1) r = p._repr_html_() assert r is not None if plot_backend_is("pylab"): assert "<summary>RegionProjection</summary>" in r assert "<svg " in r return assert "<summary>RegionProjection (13)</summary>" in r # Issue #1372 shows that the numbers here can depend on the platform; as # this test is not about whether the fit converged to the same solution # the tests are very basic. An alternative would be to just place # the values from the fit object into the strings, but then there is # the problem that this test currently requires old_numpy_printing, # so the results would not necessarily match. # assert '<div class="dataname">parval0</div><div class="dataval">-0.5' in r assert '<div class="dataname">parval1</div><div class="dataval">0.5' in r assert '<div class="dataname">sigma</div><div class="dataval">(1, 2, 3)</div>' in r # These values may depend on the platform so only very-limited check. # assert '<div class="dataname">y</div><div class="dataval">[ 30' in r assert '<div class="dataname">levels</div><div class="dataval">[ 3.6' in r assert '<div class="dataname">min</div><div class="dataval">[-2, -1]</div>' in r assert '<div class="dataname">max</div><div class="dataval">[2, 2]</div>' in r assert '<div class="dataname">nloop</div><div class="dataval">(10, 20)</div>' in r
def test_errresults(): d = Data1D('dx', [1, 2, 3], [4, 2, 2], [1.2, 0.9, 0.9]) m = Const1D() m.c0 = 3 f = fit.Fit(d, m, stat=Chi2()) er = f.est_errors() r = er._repr_html_() assert r is not None assert '<summary>covariance 1σ (68.2689%) bounds</summary>' in r assert '<summary>Summary (2)' in r assert '<td>const1d.c0</td>' in r assert '<div class="dataname">Fitting Method</div><div class="dataval">levmar</div>' in r assert '<div class="dataname">Statistic</div><div class="dataval">chi2</div>' in r assert '<tr><td>const1d.c0</td><td> 3</td><td> -0.562226</td><td> 0.562226</td></tr>' in r
def test_regproj(old_numpy_printing, override_plot_backend): p = plot.RegionProjection() r = p._repr_html_() check_empty(r, 'RegionProjection', nsummary=13) x = np.arange(5, 8, 0.5) y = np.asarray([2, 3, 4, 5, 4, 3]) dy = y / 2 d = Data1D('n n', x, y, staterror=dy) m = Polynom1D() m.c1.thaw() fit = Fit(d, m, stat=Chi2()) fr = fit.fit() assert fr.succeeded p.prepare(min=(-2, -1), max=(2, 2), nloop=(10, 20)) p.calc(fit, m.c0, m.c1) r = p._repr_html_() assert r is not None if plot.backend.name == 'pylab': assert '<summary>RegionProjection</summary>' in r assert '<svg ' in r return print(r) assert '<summary>RegionProjection (13)</summary>' in r assert '<div class="dataname">parval0</div><div class="dataval">-0.5315772076542427</div>' in r assert '<div class="dataname">parval1</div><div class="dataval">0.5854611101216837</div>' in r assert '<div class="dataname">sigma</div><div class="dataval">(1, 2, 3)</div>' in r assert '<div class="dataname">y</div><div class="dataval">[ 306.854444 282.795953 259.744431 237.699877 216.662291 196.631674\n' in r assert '<div class="dataname">levels</div><div class="dataval">[ 3.606863 7.491188 13.140272]</div>' in r assert '<div class="dataname">min</div><div class="dataval">[-2, -1]</div>' in r assert '<div class="dataname">max</div><div class="dataval">[2, 2]</div>' in r assert '<div class="dataname">nloop</div><div class="dataval">(10, 20)</div>' in r
def test_fitresults_chisq(): d = Data1D('dx', [1, 2, 3], [4, 2, 2], [1.2, 1.4, 1.4]) m = Const1D() m.c0 = 3 fr = fit.Fit(d, m, method=LevMar(), stat=Chi2()).fit() r = fr._repr_html_() assert r is not None assert '<summary>Fit parameters</summary>' in r assert '<summary>Summary (10)</summary>' in r assert '<td>const1d.c0</td>' in r assert '<div class="dataname">Method</div><div class="dataval">{}</div>'.format(fr.methodname) in r assert '<div class="dataname">Final statistic</div><div class="dataval">1.65289</div>' in r assert '<div class="dataname">Reduced statistic</div><div class="dataval">0.826446</div>' in r assert '<div class="dataname">Probability (Q-value)</div><div class="dataval">0.437602</div>' in r assert '<div class="dataname">Number of data points</div><div class="dataval">3</div>' in r assert '<div class="dataname">Degrees of freedom</div><div class="dataval">2</div>' in r
def test_errresults_multi(): d1 = Data1D('dx', [1, 2, 3], [4, 2, 2], [1.2, 0.9, 0.9]) d2 = Data1D('dx', [10, 11, 12, 13], [4, 4, 2, 4], [0.8, 1.1, 1.1, 0.9]) d = DataSimulFit('combined', (d1, d2)) m1 = Const1D() m1.c0 = 3 m = SimulFitModel('silly', (m1, m1)) f = fit.Fit(d, m, stat=Chi2()) er = f.est_errors() r = er._repr_html_() assert r is not None assert '<summary>covariance 1σ (68.2689%) bounds</summary>' in r assert '<summary>Summary (2)' in r assert '<td>const1d.c0</td>' in r assert '<div class="dataname">Fitting Method</div><div class="dataval">levmar</div>' in r assert '<div class="dataname">Statistic</div><div class="dataval">chi2</div>' in r assert '<tr><td>const1d.c0</td><td> 3</td><td> -0.362415</td><td> 0.362415</td></tr>' in r
report("mdl") from sherpa.plot import ModelPlot mplot = ModelPlot() mplot.prepare(d, mdl) plt.subplot(2, 1, 1) mplot.plot(clearwindow=False) plt.subplot(2, 1, 2) dplot.plot(clearwindow=False) plt.title('') savefig("model_data_before_fit.png") from sherpa.stats import Chi2 from sherpa.fit import Fit f = Fit(d, mdl, stat=Chi2()) report("f") print("Starting statistic: {}".format(f.calc_stat())) fitres = f.fit() report("fitres.format()") print("Reduced chi square = {:.2f}".format(fitres.rstat)) mplot.prepare(d, mdl) dplot.plot() mplot.overplot() savefig("model_data_fit1.png") from sherpa.optmethods import NelderMead