This repository has been archived by the owner on Aug 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fit_model.py
executable file
·89 lines (67 loc) · 2.48 KB
/
fit_model.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
#! /usr/bin/env python
"""
Fit model to measurements using the given error parameters.
Usage:
./fit_model.py [-m MODEL] [-b] [-o OUTPUT] [--stderr VAL]
[-absolute | --hybrid] [-i ERRORS]
<ERRORS> [<MEASURED>...]
Options:
-b, --backtrack Use backtracking
-m MODEL, --model MODEL Model path
-o OUTPUT, --output OUTPUT Output file
--absolute Fit absolute orbits
--hybrid Fit absolute orbit + orbit response
--stderr VALUE Set BPM position stderr [default: 0.0003]
-i ERRORS Set initial/global errors
Arguments:
<ERRORS> YAML file containing initial error values
<MEASURED> YAML files with madgui measurements
"""
import os
import sys
from scipy.optimize import Bounds
from docopt import docopt
import madgui.util.yaml as yaml
from orm_util import Analysis, parse_errors
def main(args=None):
opts = docopt(__doc__, args)
input_file = opts['<ERRORS>']
output_file = opts['--output']
if not output_file:
output_file = os.path.splitext(input_file)[0] + '-fit.yml'
BPM_ERR = float(opts['--stderr'])
record_files = (
opts['<MEASURED>'] or
'../data/orm/2018-10-20-orm_measurements/M8-E108-F1-I9-G1/*.yml')
model_path = opts['--model'] or '../hit_models/hht3'
ana = Analysis.app(model_path, record_files)
ana.model.update_twiss_args(dict(x=0, y=0, px=0, py=0))
if opts['--absolute']:
ana.mode = 'abs'
elif opts['--hybrid']:
ana.mode = 'hybrid'
else:
ana.mode = 'orm'
ana.measured.stderr = (ana.measured.stderr**2 + BPM_ERR**2)**0.5
if opts['-i']:
init_errors = parse_errors(yaml.load_file(opts['-i']))
ana.apply_errors(init_errors.keys(), init_errors.values())
if opts['--backtrack']:
from_monitors = ['t3dg2g', 't3dg1g', 't3df1']
ana.ensure_monitors_available(from_monitors)
ana.setup_backtracking(ana.extrapolate(from_monitors, to='#e'))
ana.init()
errors = parse_errors(yaml.load_file(input_file))
options = dict(
algorithm='lstsq',
mode='xy',
iterations=8,
delta=1e-3,
bounds=Bounds(-0.001, 0.001),
fourier=False,
rcond=1e-6,
)
result = ana.fit(errors, save_to=output_file, **options)
print(result)
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))