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
|
#!/usr/bin/python
#
# Strain Analyser
# Copyright (C) 2009-2010 Malcolm Scott <Malcolm.Scott@cl.cam.ac.uk>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program 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 this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import gtk
import os
import csv
from strainanalyser.util import *
from strainanalyser.dataset import *
from strainanalyser.datahandling import *
from strainanalyser.graphing import *
class StrainAnalyserDatasetDiffUI(AbstractUI):
def __init__(self):
super(StrainAnalyserDatasetDiffUI, self).__init__("datasetdiff.xml", "datasetdiffwindow")
self.datasetcollection = StrainAnalyserDatasetCollection()
self.diff = StrainAnalyserDiffDatasetCollection(self.datasetcollection)
self.datagraph = StrainAnalyserDatasetDiffGraph(
container_vbox=self.get_object("graphcontainer_vbox"),
dataset_diff_collection = self.diff
)
# initialise dataset list model-view-controller stuff
widget = self.get_object("datasetlist")
cols = [
("Name", 0, True),
("Resolution (m)", 3, False),
]
for (title, columnId, expand) in cols:
column = gtk.TreeViewColumn(title, gtk.CellRendererText(), text=columnId)
column.set_expand(expand)
widget.append_column(column)
radio_renderer = gtk.CellRendererToggle()
radio_renderer.set_radio(True)
radio_renderer.set_property('activatable', True)
radio_renderer.connect("toggled", self.on_datasetlist_ref_toggled)
column = gtk.TreeViewColumn("Reference", radio_renderer, active=1)
widget.append_column(column)
widget.set_model(self.datasetcollection)
self.get_object("noisefilt").set_active(self.diff.noisefilt_enabled)
self.get_object("noisefilt_order").set_value(self.diff.noisefilt_order)
self.get_object("noisefilt_kernel").set_value(self.diff.noisefilt_kernel)
# Signal handlers
def on_datasetdiffwindow_destroy(self, widget):
gtk.main_quit()
def on_open_dataset_clicked(self, widget):
# TODO
self.datagraph.redraw()
def on_new_dataset_clicked(self, widget):
def cb(cb_arg, dataset):
self.datasetcollection.add(dataset)
self.datagraph.redraw()
StrainAnalyserDatasetUI(save_cb=cb).show()
def on_edit_dataset_clicked(self, widget):
datasetlist = self.get_object("datasetlist")
cursor = datasetlist.get_cursor()[0]
if cursor:
index = cursor[0]
row = self.datasetcollection[index]
dataset = row[2]
def cb(cb_arg, dataset):
row[0] = dataset.name
row[3] = dataset.resolution
self.datagraph.redraw()
StrainAnalyserDatasetUI(compensated_dataset=dataset, save_cb=cb).show()
def on_remove_dataset_clicked(self, widget):
datasetlist = self.get_object("datasetlist")
cursor = datasetlist.get_cursor()[0]
if cursor:
index = cursor[0]
self.datasetcollection.remove(index)
self.datagraph.redraw()
def on_up_dataset_clicked(self, widget):
datasetlist = self.get_object("datasetlist")
cursor = datasetlist.get_cursor()[0]
if cursor:
index = cursor[0]
self.datasetcollection.move_up(index)
self.datagraph.redraw()
def on_down_dataset_clicked(self, widget):
datasetlist = self.get_object("datasetlist")
cursor = datasetlist.get_cursor()[0]
if cursor:
index = cursor[0]
self.datasetcollection.move_down(index)
self.datagraph.redraw()
def on_noisefilt_toggled(self, widget):
self.diff.noisefilt_enabled = widget.get_active()
for i in ["order", "kernel"]:
for j in ["slider", "label"]:
self.get_object("noisefilt_%s_%s" % (i,j)).set_sensitive(widget.get_active())
self.datagraph.redraw()
def on_noisefilt_order_value_changed(self, widget):
order = widget.get_value()
self.diff.noisefilt_order = order
self.get_object("noisefilt_kernel").set_lower(2*order + 1)
self.datagraph.redraw()
def on_noisefilt_kernel_value_changed(self, widget):
kernel = widget.get_value()
if kernel%2 == 0:
# kernel must be odd
widget.set_value(kernel + 1)
elif kernel <= 2*self.diff.noisefilt_order:
widget.set_value(2*self.diff.noisefilt_order + 1)
else:
self.diff.noisefilt_kernel = widget.get_value()
self.datagraph.redraw()
def on_datasetlist_ref_toggled(self, widget, path):
# update dataset collection (have to do the radio button logic ourselves...)
if self.datasetcollection[path][1]:
# clicked the row that's already reference -- we don't want a ref any more (hybrid radio button)
self.datasetcollection[path][1] = False
else:
# clicked a row that should become reference -- turn everything off and this row on
for row in self.datasetcollection:
row[1] = False
self.datasetcollection[path][1] = True
self.datagraph.redraw()
def on_export_clicked(self, widget):
chooser = self.get_object("export_filechooser")
if chooser.run() == 1:
filename = chooser.get_filename()
stem, ext = os.path.splitext(filename)
if ext.lower() != "csv":
filename += ".csv"
with open(filename, "wb") as f:
writer = csv.writer(f)
ref = self.datasetcollection.get_reference()
datasets = []
resolution = None
points = 0
titles = ["Distance (m)"]
for diffds in self.diff.diffed_data:
if diffds.original_ds != ref:
titles += [diffds.name]
datasets += [diffds.straindata]
if resolution is None:
resolution = diffds.resolution
else:
assert(resolution == diffds.resolution)
points = max(points, diffds.len)
writer.writerow(titles)
for i in range(0, points):
row = [resolution*i]
for ds in datasets:
if i > len(ds):
row += [None]
else:
row += [ds[i]]
writer.writerow(row)
chooser.hide()
def run():
ui = StrainAnalyserDatasetDiffUI()
ui.show()
gtk.main()
if __name__ == "__main__":
run()
|