forked from nzjakemartin/PyTrA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobal.py
95 lines (78 loc) · 2.26 KB
/
Global.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
from traits.api import HasTraits, File, Button, Array, Enum, Instance, Str, List, HasPrivateTraits, Float, Int, Bool
from traitsui.api import Group, Item, View, Label, HSplit, Tabbed, ListEditor
from Data import Data
import matplotlib.pyplot as plt
import pymodelfit as fit
from scipy.optimize import fmin
import numpy as np
def Global_chi2(start,linked,fix_vals,fixed):
chi2 = 0.0
for i in range(len(start)-1):
Data.tracefitmodel.pardict[linked[i]] = start[i]
for i in range(len(fix_vals)-1):
Data.tracefitmodel.pardict[fixed[i]] = fix_vals[i]
fix_ = linked
start = np.array(start)
print(Data.Traces.shape)
for i in range(len(Data.Traces[:,1])):
Data.tracefitmodel.fitData(Data.time,Data.Traces[i,:],fixedpars=fix_)
chi2 = (Data.tracefitmodel.chi2Data()[0]+chi2)/2
return(chi2)
class Global(HasTraits):
parameters = List
max_iter = Int(400)
fit = Button("Fit")
status = Str
linked = []
fixed = []
sample = Enum('fmin', 'mcmc')
view = View(
Item( 'parameters',
style = 'custom',
editor = ListEditor( use_notebook = True,
deletable = True,
dock_style = 'tab',
page_name = '.name' )
),
Item('max_iter'),
Item('fit'),
Item('sample'),
Item('status'),
title = 'Global', resizable=True,
buttons = [ 'OK', 'Cancel' ]
)
def _fit_fired(self):
start = []
fix_vals = []
#Construct list of fixed and linked parameters
for i in range(len(self.parameters)):
if self.parameters[i].linked==True:
self.linked.append(self.parameters[i].name)
start.append(self.parameters[i].val)
if self.parameters[i].fixed==True:
self.fixed.append(self.parameters[i].name)
fix_vals.append(self.parameters[i].val)
linked=self.linked
fixed=self.fixed
#Use minimisation over linked parameters
if self.sample == 'fmin':
xopt = fmin(Global_chi2, start, args=(linked,fix_vals,fixed))
self.status = xopt
#Use MCMC over linked parameters
if self.sample == 'mcmc':
loop.MCMC
class Params(HasPrivateTraits):
#Name of string
i = Int
name = Str
fixed = Bool
linked = Bool
val = Float
view = View(
Item('name',label='Parameter',style='readonly'),
Item('fixed',label='fix'),
Item('linked',label='link'),
Item('val',label='Value'),
)
def _val_default(self):
return Data.tracefitmodel.pardict[self.name]