-
Notifications
You must be signed in to change notification settings - Fork 93
/
comparison.py
220 lines (160 loc) · 7.31 KB
/
comparison.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from __future__ import print_function, division, unicode_literals, absolute_import
import wx
import collections
import fnmatch
import abipy.gui.awx as awx
from monty.string import list_strings
from abipy.electrons import ElectronBandsPlotter, ElectronDosPlotter, MdfPlotter, SigresPlotter
from abipy.gui.electronswx import ElectronDosDialog
class FileCheckBoxPanel(awx.Panel):
"""A panel with a list of filepaths and checkboxes."""
def __init__(self, parent, filepaths, **kwargs):
"""
Args:
parent:
Parent window.
filepaths:
String or List of strings with filepaths.
"""
super(FileCheckBoxPanel, self).__init__(parent, -1, **kwargs)
self.all_filepaths = list_strings(filepaths)
self.BuildUi()
def BuildUi(self):
main_sizer = wx.BoxSizer(wx.VERTICAL)
static_sizer = wx.StaticBoxSizer(wx.StaticBox(self, -1, "Files"), wx.VERTICAL)
self.check_boxes = collections.OrderedDict()
self.wildcards = ["*"]
for path in self.all_filepaths:
if not self._SelectFilename(path): continue
assert path not in self.check_boxes
cbox = wx.CheckBox(self, -1, path, wx.DefaultPosition, wx.DefaultSize, 0)
cbox.SetValue(True)
static_sizer.Add(cbox, 0, wx.ALL | wx.EXPAND, 5)
self.check_boxes[path] = cbox
main_sizer.Add(static_sizer, 1, wx.EXPAND, 5)
# Add buttons to (select|deselect) all checkboxes.
hsizer = wx.BoxSizer(wx.HORIZONTAL)
all_button = wx.Button(self, -1, "Select all", wx.DefaultPosition, wx.DefaultSize, 0)
all_button.Bind(wx.EVT_BUTTON, self.OnSelectAll)
hsizer.Add(all_button, 0, wx.ALL, 5)
deselect_button = wx.Button(self, -1, "Deselect all", wx.DefaultPosition, wx.DefaultSize, 0)
deselect_button.Bind(wx.EVT_BUTTON, self.OnDeselectAll)
hsizer.Add(deselect_button, 0, wx.ALL, 5)
filter_label = wx.StaticText(self, -1, "Filter:", wx.DefaultPosition, wx.DefaultSize, 0)
filter_label.Wrap(-1)
hsizer.Add(filter_label, 0, wx.ALIGN_CENTER_VERTICAL | wx.TOP | wx.BOTTOM | wx.LEFT, 5)
wildcard_choices = ["*", "*.nc"]
self.filter_combobox = wx.ComboBox(self, wx.ID_ANY, "*", wx.DefaultPosition, wx.DefaultSize, wildcard_choices,
0)
self.filter_combobox.Bind(wx.EVT_COMBOBOX, self.OnFilterComboBox)
self.filter_combobox.Bind(wx.EVT_TEXT_ENTER, self.OnFilterComboBox)
hsizer.Add(self.filter_combobox, 0, wx.ALL, 5)
main_sizer.Add(hsizer, 0, wx.ALIGN_CENTER_HORIZONTAL, 5)
self.SetSizerAndFit(main_sizer)
def _SelectFilename(self, filename):
for wcard in self.wildcards:
if not fnmatch.fnmatch(filename, wcard):
return False
return True
def OnFilterComboBox(self, event):
self.wildcards = self.filter_combobox.GetValue().split("|")
for (filepath, cbox) in self.check_boxes.items():
if self._SelectFilename(filepath):
cbox.SetValue(True)
else:
cbox.SetValue(False)
def OnSelectAll(self, event):
for cbox in self.check_boxes.values():
cbox.SetValue(True)
def OnDeselectAll(self, event):
for cbox in self.check_boxes.values():
cbox.SetValue(False)
def GetSelectedFilepaths(self):
"""
Return the list of filepaths selected by the user.
Main entry point for client code.
"""
selected = []
for path, cbox in self.check_boxes.items():
if cbox.GetValue():
selected.append(path)
return selected
class ComparisonFrame(awx.Frame):
"""
This frame allows the user to select/deselect a list of files and to produce plots
for all the files selected. Useful for convergence studies.
"""
def __init__(self, parent, dirpaths=None, filepaths=None, wildcard=None, **kwargs):
"""
Args:
parent:
parent window
dirpaths
List of directory names
filepaths
List of filepaths.
wildcard
Regular expression for selecting files.
"""
super(ComparisonFrame, self).__init__(parent, -1, **kwargs)
# TODO
#self.dirpaths = dirpaths if dirpaths is not None else []
#self.filepaths = filepaths if filepaths is not None else []
#
#self.dirpaths = map(os.path.abspath, self.dirpaths)
#self.filepaths = map(os.path.abspath, self.filepaths)
#
#self.wildcard = wildcard if wildcard is not None else ""
main_sizer = wx.BoxSizer(wx.VERTICAL)
hsizer = wx.BoxSizer(wx.HORIZONTAL)
st1 = wx.StaticText(self, -1, "Quantity:", wx.DefaultPosition, wx.DefaultSize, 0)
st1.Wrap(-1)
hsizer.Add(st1, 0, wx.ALIGN_CENTER_VERTICAL | wx.TOP | wx.BOTTOM | wx.LEFT, 5)
plotter_choices = ["ebands", "edos", "mdf", "sigres"]
self.plotter_cbox = wx.ComboBox(self, -1, "ebands", wx.DefaultPosition, wx.DefaultSize, plotter_choices, 0)
hsizer.Add(self.plotter_cbox, 0, wx.ALL, 5)
compare_button = wx.Button(self, -1, "Compare", wx.DefaultPosition, wx.DefaultSize, 0)
compare_button.Bind(wx.EVT_BUTTON, self.OnCompareButton)
hsizer.Add(compare_button, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
main_sizer.Add(hsizer, 0, wx.ALIGN_CENTER_HORIZONTAL, 5)
self.panel = FileCheckBoxPanel(self, filepaths)
main_sizer.Add(self.panel, 1, wx.EXPAND, 5)
self.SetSizerAndFit(main_sizer)
def OnCompareButton(self, event):
selected_files = self.panel.GetSelectedFilepaths()
choice = self.plotter_cbox.GetValue()
try:
if choice == "ebands":
plotter = ElectronBandsPlotter()
for filepath in selected_files:
plotter.add_ebands_from_file(filepath)
plotter.plot()
elif choice == "edos":
# Open dialog to ask the user the DOS parameters.
dos_dialog = ElectronDosDialog(None)
if dos_dialog.ShowModal() == wx.ID_OK:
p = dos_dialog.GetParams()
plotter = ElectronDosPlotter()
for filepath in selected_files:
plotter.add_edos_from_file(filepath, **p)
plotter.plot()
dos_dialog.Destroy()
elif choice == "mdf":
plotter = MdfPlotter()
for filepath in selected_files:
plotter.add_mdf_from_file(filepath, mdf_type="exc")
plotter.plot()
elif choice == "sigres":
plotter = SigresPlotter()
plotter.add_files(selected_files)
plotter.plot_qpgaps()
#plotter.plot_qpenes()
else:
awx.showErrorMessage(self, message="No function registered for choice %s" % choice)
except Exception:
awx.showErrorMessage(self)
def wxapp_comparison(dirpaths=None, filepaths=None, wildcard=None):
app = awx.App()
frame = ComparisonFrame(None, dirpaths=dirpaths, filepaths=filepaths, wildcard=wildcard)
frame.Show()
return app