-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibhooke.py
executable file
·406 lines (332 loc) · 14.9 KB
/
libhooke.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
libhooke.py
General library of internal objects and utilities for Hooke.
Copyright (C) 2006 Massimo Sandal (University of Bologna, Italy).
With algorithms contributed by Francesco Musiani (University of Bologna, Italy)
This program is released under the GNU General Public License version 2.
'''
import libhookecurve as lhc
import scipy
import scipy.signal
import scipy.optimize
import scipy.stats
import numpy
import xml.dom.minidom
import os
import string
import csv
from matplotlib.ticker import ScalarFormatter
HOOKE_VERSION=['0.8.3_devel', 'Seinei', '2008-04-16']
WX_GOOD=['2.6','2.8']
class PlaylistXML:
'''
This module allows for import/export of an XML playlist into/out of a list of HookeCurve objects
'''
def __init__(self):
self.playlist=None #the DOM object representing the playlist data structure
self.playpath=None #the path of the playlist XML file
self.plaything=None
self.hidden_attributes=['curve'] #This list contains hidden attributes that we don't want to go into the playlist.
def export(self, list_of_hooke_curves, generics):
'''
Creates an initial playlist from a list of files.
A playlist is an XML document with the following syntaxis:
<playlist>
<element path="/my/file/path/"/ attribute="attribute">
<element path="...">
</playlist>
'''
#create the output playlist, a simple XML document
impl=xml.dom.minidom.getDOMImplementation()
#create the document DOM object and the root element
newdoc=impl.createDocument(None, "playlist",None)
top_element=newdoc.documentElement
#save generics variables
playlist_generics=newdoc.createElement("generics")
top_element.appendChild(playlist_generics)
for key in generics.keys():
newdoc.createAttribute(key)
playlist_generics.setAttribute(key,str(generics[key]))
#save curves and their attributes
for item in list_of_hooke_curves:
#playlist_element=newdoc.createElement("curve")
playlist_element=newdoc.createElement("element")
top_element.appendChild(playlist_element)
for key in item.__dict__:
if not (key in self.hidden_attributes):
newdoc.createAttribute(key)
playlist_element.setAttribute(key,str(item.__dict__[key]))
self.playlist=newdoc
def load(self,filename):
'''
loads a playlist file
'''
myplay=file(filename)
self.playpath=filename
#the following 3 lines are needed to strip newlines. otherwise, since newlines
#are XML elements too (why?), the parser would read them (and re-save them, multiplying
#newlines...)
#yes, I'm an XML n00b
the_file=myplay.read()
the_file_lines=the_file.split('\n')
the_file=''.join(the_file_lines)
self.playlist=xml.dom.minidom.parseString(the_file)
#inner parsing functions
def handlePlaylist(playlist):
list_of_files=playlist.getElementsByTagName("element")
generics=playlist.getElementsByTagName("generics")
return handleFiles(list_of_files), handleGenerics(generics)
def handleGenerics(generics):
generics_dict={}
if len(generics)==0:
return generics_dict
for attribute in generics[0].attributes.keys():
generics_dict[attribute]=generics[0].getAttribute(attribute)
return generics_dict
def handleFiles(list_of_files):
new_playlist=[]
for myfile in list_of_files:
#rebuild a data structure from the xml attributes
the_curve=lhc.HookeCurve(myfile.getAttribute('path'))
for attribute in myfile.attributes.keys(): #extract attributes for the single curve
the_curve.__dict__[attribute]=myfile.getAttribute(attribute)
new_playlist.append(the_curve)
return new_playlist #this is the true thing returned at the end of this function...(FIXME: clarity)
return handlePlaylist(self.playlist)
def save(self,output_filename):
'''
saves the playlist in a XML file.
'''
try:
outfile=file(output_filename,'w')
except IOError:
print 'libhooke.py : Cannot save playlist. Wrong path or filename'
return
self.playlist.writexml(outfile,indent='\n')
outfile.close()
class HookeConfig:
'''
Handling of Hooke configuration file
Mostly based on the simple-yet-useful examples of the Python Library Reference
about xml.dom.minidom
FIXME: starting to look a mess, should require refactoring
'''
def __init__(self):
self.config={}
self.config['plugins']=[]
self.config['drivers']=[]
self.config['plotmanips']=[]
def load_config(self, filename):
myconfig=file(filename)
#the following 3 lines are needed to strip newlines. otherwise, since newlines
#are XML elements too, the parser would read them (and re-save them, multiplying
#newlines...)
#yes, I'm an XML n00b
the_file=myconfig.read()
the_file_lines=the_file.split('\n')
the_file=''.join(the_file_lines)
self.config_tree=xml.dom.minidom.parseString(the_file)
def getText(nodelist):
#take the text from a nodelist
#from Python Library Reference 13.7.2
rc = ''
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc += node.data
return rc
def handleConfig(config):
display_elements=config.getElementsByTagName("display")
plugins_elements=config.getElementsByTagName("plugins")
drivers_elements=config.getElementsByTagName("drivers")
workdir_elements=config.getElementsByTagName("workdir")
defaultlist_elements=config.getElementsByTagName("defaultlist")
plotmanip_elements=config.getElementsByTagName("plotmanips")
handleDisplay(display_elements)
handlePlugins(plugins_elements)
handleDrivers(drivers_elements)
handleWorkdir(workdir_elements)
handleDefaultlist(defaultlist_elements)
handlePlotmanip(plotmanip_elements)
def handleDisplay(display_elements):
for element in display_elements:
for attribute in element.attributes.keys():
self.config[attribute]=element.getAttribute(attribute)
def handlePlugins(plugins):
for plugin in plugins[0].childNodes:
try:
self.config['plugins'].append(str(plugin.tagName))
except: #if we allow fancy formatting of xml, there is a text node, so tagName fails for it...
pass
#FIXME: code duplication
def handleDrivers(drivers):
for driver in drivers[0].childNodes:
try:
self.config['drivers'].append(str(driver.tagName))
except: #if we allow fancy formatting of xml, there is a text node, so tagName fails for it...
pass
def handlePlotmanip(plotmanips):
for plotmanip in plotmanips[0].childNodes:
try:
self.config['plotmanips'].append(str(plotmanip.tagName))
except: #if we allow fancy formatting of xml, there is a text node, so tagName fails for it...
pass
def handleWorkdir(workdir):
'''
default working directory
'''
wdir=getText(workdir[0].childNodes)
self.config['workdir']=wdir.strip()
def handleDefaultlist(defaultlist):
'''
default playlist
'''
dflist=getText(defaultlist[0].childNodes)
self.config['defaultlist']=dflist.strip()
handleConfig(self.config_tree)
#making items in the dictionary more machine-readable
for item in self.config.keys():
try:
self.config[item]=float(self.config[item])
except TypeError: #we are dealing with a list, probably. keep it this way.
try:
self.config[item]=eval(self.config[item])
except: #not a list, not a tuple, probably a string?
pass
except ValueError: #if we can't get it to a number, it must be None or a string
if string.lower(self.config[item])=='none':
self.config[item]=None
else:
pass
return self.config
def save_config(self, config_filename):
print 'Not Implemented.'
pass
class EngrFormatter(ScalarFormatter):
"""A variation of the standard ScalarFormatter, using only multiples of
three
in the mantissa. A fixed number of decimals can be displayed with the optional
parameter `ndec` . If `ndec` is None (default), the number of decimals is
defined
from the current ticks.
"""
def __init__(self, ndec=None, useOffset=True, useMathText=False):
ScalarFormatter.__init__(self, useOffset, useMathText)
if ndec is None or ndec < 0:
self.format = None
elif ndec == 0:
self.format = "%d"
else:
self.format = "%%1.%if" % ndec
#........................
def _set_orderOfMagnitude(self, mrange):
"""Sets the order of magnitude."""
locs = numpy.absolute(self.locs)
if self.offset:
oom = numpy.floor(numpy.log10(mrange))
else:
if locs[0] > locs[-1]:
val = locs[0]
else:
val = locs[-1]
if val == 0:
oom = 0
else:
oom = numpy.floor(numpy.log10(val))
if oom <= -3:
self.orderOfMagnitude = 3*(oom//3)
elif oom <= -1:
self.orderOfMagnitude = -3
elif oom >= 4:
self.orderOfMagnitude = 3*(oom//3)
else:
self.orderOfMagnitude = 0
#........................
def _set_format(self):
"""Sets the format string to format all ticklabels."""
# set the format string to format all the ticklabels
locs = (numpy.array(self.locs)-self.offset) / 10**self.orderOfMagnitude+1e-15
sigfigs = [len(str('%1.3f'% loc).split('.')[1].rstrip('0')) \
for loc in locs]
sigfigs.sort()
if self.format is None:
self.format = '%1.' + str(sigfigs[-1]) + 'f'
if self._usetex or self._useMathText: self.format = '$%s$'%self.format
class ClickedPoint:
'''
this class defines what a clicked point on the curve plot is
'''
def __init__(self):
self.is_marker=None #boolean ; decides if it is a marker
self.is_line_edge=None #boolean ; decides if it is the edge of a line (unused)
self.absolute_coords=(None,None) #(float,float) ; the absolute coordinates of the clicked point on the graph
self.graph_coords=(None,None) #(float,float) ; the coordinates of the plot that are nearest in X to the clicked point
self.index=None #integer ; the index of the clicked point with respect to the vector selected
self.dest=None #0 or 1 ; 0=top plot 1=bottom plot
def find_graph_coords_old(self, xvector, yvector):
'''
Given a clicked point on the plot, finds the nearest point in the dataset (in X) that
corresponds to the clicked point.
OLD & DEPRECATED - to be removed
'''
#FIXME: a general algorithm using min() is needed!
#print '---DEPRECATED FIND_GRAPH_COORDS_OLD---'
best_index=0
best_dist=10**9 #should be more than enough given the scale
for index in scipy.arange(1,len(xvector),1):
dist=((self.absolute_coords[0]-xvector[index])**2)+(100*((self.absolute_coords[1]-yvector[index])))**2
#TODO, generalize? y coordinate is multiplied by 100 due to scale differences in the plot
if dist<best_dist:
best_index=index
best_dist=dist
self.index=best_index
self.graph_coords=(xvector[best_index],yvector[best_index])
return
def find_graph_coords(self,xvector,yvector):
'''
Given a clicked point on the plot, finds the nearest point in the dataset that
corresponds to the clicked point.
'''
dists=[]
for index in scipy.arange(1,len(xvector),1):
dists.append(((self.absolute_coords[0]-xvector[index])**2)+((self.absolute_coords[1]-yvector[index])**2))
self.index=dists.index(min(dists))
self.graph_coords=(xvector[self.index],yvector[self.index])
#-----------------------------------------
#CSV-HELPING FUNCTIONS
def transposed2(lists, defval=0):
'''
transposes a list of lists, i.e. from [[a,b,c],[x,y,z]] to [[a,x],[b,y],[c,z]] without losing
elements
(by Zoran Isailovski on the Python Cookbook online)
'''
if not lists: return []
return map(lambda *row: [elem or defval for elem in row], *lists)
def csv_write_dictionary(f, data, sorting='COLUMNS'):
'''
Writes a CSV file from a dictionary, with keys as first column or row
Keys are in "random" order.
Keys should be strings
Values should be lists or other iterables
'''
keys=data.keys()
values=data.values()
t_values=transposed2(values)
writer=csv.writer(f)
if sorting=='COLUMNS':
writer.writerow(keys)
for item in t_values:
writer.writerow(item)
if sorting=='ROWS':
print 'Not implemented!' #FIXME: implement it.
#-----------------------------------------
def debug():
'''
debug stuff from latest rewrite of hooke_playlist.py
should be removed sooner or later (or substituted with new debug code!)
'''
confo=HookeConfig()
print confo.load_config('hooke.conf')
if __name__ == '__main__':
debug()