InstrumentResolutionWidget.py 8.37 KB
Newer Older
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
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library 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
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

''' 
Created on Mar 30, 2015

30
:author: Eric C. Pellegrini
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'''

import numpy

from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigCanvas
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar

import wx

from MDANSE import REGISTRY
from MDANSE.Externals.pubsub import pub

from MDANSE.App.GUI import DATA_CONTROLLER
from MDANSE.App.GUI.ComboWidgets.ConfigurationPanel import ConfigurationPanel
46
from MDANSE.App.GUI.Framework.Widgets.IWidget import IWidget
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

class InstrumentResolutionDialog(wx.Dialog):
    
    def __init__(self, parent=None, nSteps=100, timeStep=1.0):
        
        wx.Dialog.__init__(self, parent, wx.ID_ANY, title="Instrument resolution", style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.MINIMIZE_BOX|wx.MAXIMIZE_BOX)

        self._parent = parent
    
        self._nSteps = nSteps
        
        self._timeStep = timeStep
        
        self._frequencies = numpy.fft.fftshift(numpy.fft.fftfreq(self._nSteps, self._timeStep))
                
        self._currentKernel = None
                          
        self._value = None
                                
        self.build_dialog()
        
    def build_dialog(self):

        sizer = wx.GridBagSizer(5,5)
        
        sb = wx.StaticBox(self, wx.ID_ANY, label="kernel")
        sbSizer = wx.StaticBoxSizer(sb, wx.HORIZONTAL)
74
        self._kernelChoice = wx.Choice(self, wx.ID_ANY, choices=REGISTRY["instrument_resolution"].keys())
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
        self._kernelChoice.SetSelection(0)
        sbSizer.Add(self._kernelChoice, 1, wx.ALL|wx.EXPAND, 5)
        sizer.Add(sbSizer, (0,0), flag=wx.EXPAND)

        self._parametersWidgets = ['',{}]
        sb = wx.StaticBox(self, wx.ID_ANY, label="parameters")
        self._parametersSizer = wx.StaticBoxSizer(sb, wx.HORIZONTAL)        
        sizer.Add(self._parametersSizer, (1,0), flag=wx.EXPAND)

        sb = wx.StaticBox(self, wx.ID_ANY)
        sbSizer = wx.StaticBoxSizer(sb, wx.HORIZONTAL)
        self._cancel = wx.Button(self, wx.ID_ANY, label="Cancel")
        self._plot = wx.Button(self, wx.ID_ANY, label="Plot")
        self._ok = wx.Button(self, wx.ID_ANY, label="OK")
        sbSizer.Add(self._cancel, 0, wx.ALL, 5)
        sbSizer.Add((-1,-1), 1, wx.ALL|wx.EXPAND, 5)
        sbSizer.Add(self._plot, 0, wx.ALL, 5)
        sbSizer.Add(self._ok, 0, wx.ALL, 5)
        sizer.Add(sbSizer, (2,0), span=(1,2), flag=wx.EXPAND)

        sb = wx.StaticBox(self, wx.ID_ANY, label="plot")
        sbSizer = wx.StaticBoxSizer(sb, wx.VERTICAL)
        self._dpi = 100
        self._figure = Figure((5.0, 4.0), dpi=self._dpi)
        self._canvas = FigCanvas(self, -1, self._figure)
        self._axis = self._figure.add_subplot(111)
        self._toolbar = NavigationToolbar(self._canvas)
        sbSizer.Add(self._canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        sbSizer.Add(self._toolbar, 0, wx.EXPAND)
        sizer.Add(sbSizer, (0,1),span=(2,1), flag=wx.EXPAND)
        
        sizer.AddGrowableRow(1)
        sizer.AddGrowableCol(1)
        
        self.SetSizer(sizer)
        self.select_kernel(self._kernelChoice.GetStringSelection())
        
        self.Bind(wx.EVT_BUTTON, self.on_cancel, self._cancel)
        self.Bind(wx.EVT_BUTTON, self.on_plot_kernel, self._plot)
        self.Bind(wx.EVT_BUTTON, self.on_ok, self._ok)
        self.Bind(wx.EVT_CHOICE, self.on_select_kernel, self._kernelChoice)
        self.Bind(wx.EVT_CLOSE, self.on_cancel)
                
    @property
    def value(self):
        
        return self._value
                
    def on_cancel(self, event):
        
        self._value = None
                                
        self.Destroy()
        
    def on_ok(self, event):
        
        if not self._parametersPanel.validate():
            return
        
        self._value = (self._currentKernel, self._parametersPanel.get_value())
        
        self.Destroy()
        
    def on_plot_kernel(self, event):
        
        if not self._parametersPanel.validate():
            return
                            
143
        kernelClass = REGISTRY["instrument_resolution"][self._currentKernel]
144
145
146
        
        resolution = kernelClass()
        
147
148
149
        resolution.setup(self._parametersPanel.get_value())
        
        resolution.set_kernel(self._frequencies, self._timeStep)
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
        
        self._axis.clear()
                        
        self._axis.plot(self._frequencies, resolution.frequencyWindow)
        self._axis.set_xlabel("frequency (THz)")
        self._axis.set_ylabel("instrument resolution (a.u)")

        self._canvas.draw()

    def on_select_kernel(self, event):
                        
        self.select_kernel(event.GetString())
        
    def select_kernel(self, kernelName):
                
        if kernelName == self._currentKernel:
            return
        
        self._currentKernel = kernelName
        
        self._parametersSizer.Clear(deleteWindows=True)

172
        resolution = REGISTRY["instrument_resolution"][kernelName]()
173
174
175
        
        self.Freeze()
        
176
        self._parametersPanel = ConfigurationPanel(self, resolution)
177
178
179
180
181
182
183
184
185
        
        self._parametersSizer.Add(self._parametersPanel, 0, wx.ALL|wx.EXPAND, 5)
                
        self.Thaw()
        
        self._parametersSizer.Layout()

        self.Fit()

186
class InstrumentResolutionConfigurator(IWidget):
187
188
189
190
191
192
193
    
    type = "instrument_resolution"
    
    def add_widgets(self):
        
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        
194
        self._resolution = wx.TextCtrl(self._widgetPanel, wx.ID_ANY, value=str(self._configurator.default))
195
196
197
198
199
200
201
202
203

        self._setResolution = wx.Button(self._widgetPanel, wx.ID_ANY, label="Set")
        self._setResolution.Enable(False)
        
        sizer.Add(self._resolution, 1, wx.ALL|wx.EXPAND, 5)
        sizer.Add(self._setResolution, 0, wx.ALL|wx.EXPAND, 5)
        
        self.Bind(wx.EVT_BUTTON, self.on_set_instrument_resolution, self._setResolution)

204
205
        pub.subscribe(self.on_set_trajectory, ("set_trajectory"))
        pub.subscribe(self.on_set_trajectory, ("set_netcdf"))
206
207
208
209
210
211
212
213
214

        return sizer
        
    def get_widget_value(self):
        
        return eval(self._resolution.GetValue())

    def on_set_instrument_resolution(self, event):
                
215
        frameCfgName = self._configurator.dependencies["frames"]
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
        time = self.Parent.widgets[frameCfgName].time
        timeStep = time[1] - time[0]
        nSteps = len(time)
        
        self._instrumentResolutionDialog = InstrumentResolutionDialog(self, nSteps, timeStep)
        
        self._instrumentResolutionDialog.ShowModal()

        value = self._instrumentResolutionDialog.value
        
        if value is not None:
            self._resolution.SetValue(str(value))
        
        self._instrumentResolutionDialog.Destroy()

231
    def on_set_trajectory(self, message):
232
233
234
235
236
237
238
239
240
241
242
243
        
        window, filename = message
                        
        if not window in self.Parent.widgets.values():
            return
        
        if not DATA_CONTROLLER:
            return
        
        self._trajectory = DATA_CONTROLLER[filename]
                        
        self._setResolution.Enable(True)