2 # -*- coding: utf-8 -*-
7 General library of internal objects and utilities for Hooke.
9 Copyright (C) 2006 Massimo Sandal (University of Bologna, Italy).
10 With algorithms contributed by Francesco Musiani (University of Bologna, Italy)
12 This program is released under the GNU General Public License version 2.
17 import xml.dom.minidom
24 HOOKE_VERSION=['0.9.0_devel', 'Kenzo', '2009-09-xx']
29 def get_file_path(filename, folders = []):
30 if os.path.dirname(filename) == '' or os.path.isabs(filename) == False:
32 for folder in folders:
33 path = os.path.join(path, folder)
34 filename = os.path.join(hookeDir, path, filename)
38 class ClickedPoint(object):
40 this class defines what a clicked point on the curve plot is
44 self.is_marker=None #boolean ; decides if it is a marker
45 self.is_line_edge=None #boolean ; decides if it is the edge of a line (unused)
46 self.absolute_coords=(None,None) #(float,float) ; the absolute coordinates of the clicked point on the graph
47 self.graph_coords=(None,None) #(float,float) ; the coordinates of the plot that are nearest in X to the clicked point
48 self.index=None #integer ; the index of the clicked point with respect to the vector selected
49 self.dest=None #0 or 1 ; 0=top plot 1=bottom plot
52 def find_graph_coords_old(self, xvector, yvector):
54 Given a clicked point on the plot, finds the nearest point in the dataset (in X) that
55 corresponds to the clicked point.
56 OLD & DEPRECATED - to be removed
59 #FIXME: a general algorithm using min() is needed!
61 best_dist=10**9 #should be more than enough given the scale
63 for index in scipy.arange(1,len(xvector),1):
64 dist=((self.absolute_coords[0]-xvector[index])**2)+(100*((self.absolute_coords[1]-yvector[index])))**2
65 #TODO, generalize? y coordinate is multiplied by 100 due to scale differences in the plot
71 self.graph_coords=(xvector[best_index],yvector[best_index])
74 def find_graph_coords(self,xvector,yvector):
76 Given a clicked point on the plot, finds the nearest point in the dataset (in X) that
77 corresponds to the clicked point.
80 for index in scipy.arange(1,len(xvector),1):
81 dists.append(((self.absolute_coords[0]-xvector[index])**2)+((self.absolute_coords[1]-yvector[index])**2))
83 self.index=dists.index(min(dists))
84 self.graph_coords=(xvector[self.index],yvector[self.index])
85 #-----------------------------------------
86 #CSV-HELPING FUNCTIONS
88 def transposed2(lists, defval=0):
90 transposes a list of lists, i.e. from [[a,b,c],[x,y,z]] to [[a,x],[b,y],[c,z]] without losing
92 (by Zoran Isailovski on the Python Cookbook online)
94 if not lists: return []
95 return map(lambda *row: [elem or defval for elem in row], *lists)
97 def csv_write_dictionary(f, data, sorting='COLUMNS'):
99 Writes a CSV file from a dictionary, with keys as first column or row
100 Keys are in "random" order.
102 Keys should be strings
103 Values should be lists or other iterables
107 t_values=transposed2(values)
110 if sorting=='COLUMNS':
111 writer.writerow(keys)
112 for item in t_values:
113 writer.writerow(item)
116 print 'Not implemented!' #FIXME: implement it.
119 #-----------------------------------------
123 debug stuff from latest rewrite of hooke_playlist.py
124 should be removed sooner or later (or substituted with new debug code!)
127 print confo.load_config('hooke.conf')
129 if __name__ == '__main__':