6 General library of internal objects and utilities for Hooke.
8 Copyright 2006 by Massimo Sandal (University of Bologna, Italy).
9 With algorithms contributed by Francesco Musiani (University of Bologna, Italy)
10 And additions contributed by Dr. Rolf Schmidt (Concordia University, Canada)
12 This program is released under the GNU General Public License version 2.
20 HOOKE_VERSION=['0.9.0_devel', 'Kenzo', '2010-01-31']
24 #constants for 'special' curves
25 #this can make it easier to understand what curve we are working on
29 def delete_empty_lines_from_xmlfile(filename):
30 #the following 3 lines are needed to strip newlines.
31 #Otherwise, since newlines are XML elements too, the parser would read them
32 #(and re-save them, multiplying newlines...)
33 aFile=file(filename).read()
34 aFile=aFile.split('\n')
38 def get_file_path(filename, folders = []):
39 if os.path.dirname(filename) == '' or os.path.isabs(filename) == False:
41 for folder in folders:
42 path = os.path.join(path, folder)
43 filename = os.path.join(hookeDir, path, filename)
51 return (numpy.exp(2 * z) + 1) / (numpy.exp(2 * z) - 1)
53 class ClickedPoint(object):
55 this class defines what a clicked point on the curve plot is
59 self.is_marker = None #boolean ; decides if it is a marker
60 self.is_line_edge = None #boolean ; decides if it is the edge of a line (unused)
61 self.absolute_coords = (None, None) #(float,float) ; the absolute coordinates of the clicked point on the graph
62 self.graph_coords = (None, None) #(float,float) ; the coordinates of the plot that are nearest in X to the clicked point
63 self.index = None #integer ; the index of the clicked point with respect to the vector selected
64 self.dest = None #0 or 1 ; 0=top plot 1=bottom plot
66 def find_graph_coords(self, xvector, yvector):
68 Given a clicked point on the plot, finds the nearest point in the dataset (in X) that
69 corresponds to the clicked point.
72 for index in scipy.arange(1, len(xvector), 1):
73 dists.append(((self.absolute_coords[0] - xvector[index]) ** 2)+((self.absolute_coords[1] - yvector[index]) ** 2))
75 self.index=dists.index(min(dists))
76 self.graph_coords=(xvector[self.index], yvector[self.index])
77 #-----------------------------------------
78 #CSV-HELPING FUNCTIONS
80 def transposed2(lists, defval=0):
82 transposes a list of lists, i.e. from [[a,b,c],[x,y,z]] to [[a,x],[b,y],[c,z]] without losing
84 (by Zoran Isailovski on the Python Cookbook online)
86 if not lists: return []
87 return map(lambda *row: [elem or defval for elem in row], *lists)
89 def csv_write_dictionary(f, data, sorting='COLUMNS'):
91 Writes a CSV file from a dictionary, with keys as first column or row
92 Keys are in "random" order.
94 Keys should be strings
95 Values should be lists or other iterables
99 t_values=transposed2(values)
102 if sorting=='COLUMNS':
103 writer.writerow(keys)
104 for item in t_values:
105 writer.writerow(item)
108 print 'Not implemented!' #FIXME: implement it.