-
Notifications
You must be signed in to change notification settings - Fork 9
/
test_fitInterface.py
178 lines (159 loc) · 5.5 KB
/
test_fitInterface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# -*- coding: utf-8 -*-
# HDTV - A ROOT-based spectrum analysis software
# Copyright (C) 2006-2009 The HDTV development team (see file AUTHORS)
#
# This file is part of HDTV.
#
# HDTV is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# HDTV is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with HDTV; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
import re
import os
import sys
import pytest
from tests.helpers.utils import redirect_stdout, hdtvcmd
from hdtv.util import monkey_patch_ui
monkey_patch_ui()
import hdtv.cmdline
import hdtv.options
import hdtv.session
import __main__
try:
__main__.spectra = hdtv.session.Session()
except RuntimeError:
pass
from hdtv.plugins.specInterface import spec_interface
from hdtv.plugins.fitInterface import fit_interface
import hdtv.plugins.peakfinder
spectra = __main__.spectra
testspectrum = os.path.join(
os.path.curdir, "tests", "share", "osiris_bg.spc")
@pytest.fixture(autouse=True)
def prepare():
fit_interface.ResetFitterParameters()
hdtv.options.Set("table", "classic")
hdtv.options.Set("uncertainties", "short")
spectra.Clear()
yield
spectra.Clear()
fit_interface.ResetFitterParameters()
def test_cmd_fit_various():
hdtvcmd("fit function peak activate theuerkauf")
spec_interface.LoadSpectra(testspectrum)
assert len(spec_interface.spectra.dict) == 1
f, ferr = setup_fit()
assert f == ""
assert ferr == ""
f, ferr = hdtvcmd("fit execute")
assert "WorkFit on spectrum: 0" in f
assert ".0 |" in f
assert ".1 |" in f
assert "2 peaks in WorkFit" in f
f, ferr = hdtvcmd("fit store")
assert f == "Storing workFit with ID 0"
assert ferr == ""
f, ferr = hdtvcmd("fit clear", "fit list")
assert ferr == ""
assert "Fits in Spectrum 0" in f
f, ferr = hdtvcmd(
"fit hide 0",
"fit show 0",
"fit show decomposition 0",
"fit hide decomposition 0")
assert ferr == ""
assert f == ""
f, ferr = hdtvcmd("fit activate 0")
assert "Activating fit 0" in f
assert ferr == ""
f, ferr = hdtvcmd("fit delete 0",
"fit function peak activate ee")
assert ferr == ""
assert f == ""
def test_cmd_fit_peakfind():
spec_interface.LoadSpectra(testspectrum)
assert len(spec_interface.spectra.dict) == 1
f, ferr = hdtvcmd("fit peakfind -a -t 0.002")
assert "Search Peaks in region" in f
assert "Found 68 peaks" in f
# This was not needed before commits around ~b41833c9c66f9ba5dbdcfc6fc4468b242360641f
# However, some small change has happened that prevents a correct fit of a single
# double peak at ~1540 keV. When I fit it manually, using a similar fit region
# and internal background only, it works.
assert "WARNING: Adding invalid fit" in ferr
@pytest.mark.filterwarnings("ignore::RuntimeWarning")
@pytest.mark.parametrize("peak", [
"theuerkauf", "ee" ])
@pytest.mark.parametrize("bg", [
"polynomial", "exponential", "interpolation" ])
@pytest.mark.parametrize("integrate", [
"True", "False" ])
@pytest.mark.parametrize("likelihood", [
"normal", "poisson" ])
def test_cmd_fit_parameter(peak, bg, integrate, likelihood):
spec_interface.LoadSpectra(testspectrum)
f, ferr = setup_fit()
hdtvcmd(
"fit marker background set 415",
"fit marker background set 450")
f, ferr = hdtvcmd(
f"fit function peak activate {peak}",
f"fit function background activate {bg}",
f"fit parameter integrate {integrate}",
f"fit parameter likelihood {likelihood}")
assert f == ""
assert ferr == ""
f, ferr = hdtvcmd("fit execute")
assert "WorkFit on spectrum: 0" in f
assert ".0 |" in f
assert ".1 |" in f
assert "2 peaks in WorkFit" in f
f, ferr = hdtvcmd("fit store")
assert f == "Storing workFit with ID 0"
assert ferr == ""
f, ferr = hdtvcmd("fit clear", "fit list")
assert ferr == ""
assert "Fits in Spectrum 0" in f
def test_interpolation_incomplete():
spec_interface.LoadSpectra(testspectrum)
assert len(spec_interface.spectra.dict) == 1
f, ferr = setup_interpolation_incomplete()
assert f == ""
assert "Background fit failed" in ferr
def setup_interpolation_incomplete():
return hdtvcmd(
"fit function background activate interpolation",
"fit marker background set 520",
"fit marker background set 550",
"fit marker background set 620",
"fit marker background set 650",
"fit execute")
def setup_fit():
return hdtvcmd(
"fit function background activate polynomial",
"fit parameter background 2",
"fit marker peak set 580",
"fit marker peak set 610",
"fit marker background set 520",
"fit marker background set 550",
"fit marker background set 620",
"fit marker background set 650",
"fit marker region set 570",
"fit marker region set 615")
# More tests are still needed for:
#
# fit focus
# fit getlists
# fit read
# fit savelists
# fit tex
# fit write