Really hideous merge of Rolf Schmidt's code.
[hooke.git] / hooke / libhooke.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 '''
5 libhooke.py
6
7 General library of internal objects and utilities for Hooke.
8
9 Copyright (C) 2006 Massimo Sandal (University of Bologna, Italy).
10 With algorithms contributed by Francesco Musiani (University of Bologna, Italy)
11
12 This program is released under the GNU General Public License version 2.
13 '''
14
15 import scipy
16 import numpy
17 import xml.dom.minidom
18 import os
19 import os.path
20 import string
21 import csv
22
23
24 HOOKE_VERSION=['0.9.0_devel', 'Kenzo', '2009-09-xx']
25 WX_GOOD=['2.6','2.8']
26 hookeDir=''
27
28
29 def get_file_path(filename, folders = []):
30     if os.path.dirname(filename) == '' or os.path.isabs(filename) == False:
31         path = ''
32         for folder in folders:
33             path = os.path.join(path, folder)
34         filename = os.path.join(hookeDir, path, filename)
35
36     return filename
37
38 class ClickedPoint(object):
39     '''
40     this class defines what a clicked point on the curve plot is
41     '''
42     def __init__(self):
43
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
50
51
52     def find_graph_coords_old(self, xvector, yvector):
53         '''
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
57         '''
58
59         #FIXME: a general algorithm using min() is needed!
60         best_index=0
61         best_dist=10**9 #should be more than enough given the scale
62
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
66             if dist<best_dist:
67                 best_index=index
68                 best_dist=dist
69
70         self.index=best_index
71         self.graph_coords=(xvector[best_index],yvector[best_index])
72         return
73
74     def find_graph_coords(self,xvector,yvector):
75         '''
76         Given a clicked point on the plot, finds the nearest point in the dataset (in X) that
77         corresponds to the clicked point.
78         '''
79         dists=[]
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))
82
83         self.index=dists.index(min(dists))
84         self.graph_coords=(xvector[self.index],yvector[self.index])
85 #-----------------------------------------
86 #CSV-HELPING FUNCTIONS
87
88 def transposed2(lists, defval=0):
89     '''
90     transposes a list of lists, i.e. from [[a,b,c],[x,y,z]] to [[a,x],[b,y],[c,z]] without losing
91     elements
92     (by Zoran Isailovski on the Python Cookbook online)
93     '''
94     if not lists: return []
95     return map(lambda *row: [elem or defval for elem in row], *lists)
96
97 def csv_write_dictionary(f, data, sorting='COLUMNS'):
98     '''
99     Writes a CSV file from a dictionary, with keys as first column or row
100     Keys are in "random" order.
101
102     Keys should be strings
103     Values should be lists or other iterables
104     '''
105     keys=data.keys()
106     values=data.values()
107     t_values=transposed2(values)
108     writer=csv.writer(f)
109
110     if sorting=='COLUMNS':
111         writer.writerow(keys)
112         for item in t_values:
113             writer.writerow(item)
114
115     if sorting=='ROWS':
116         print 'Not implemented!' #FIXME: implement it.
117
118
119 #-----------------------------------------
120
121 def debug():
122     '''
123     debug stuff from latest rewrite of hooke_playlist.py
124     should be removed sooner or later (or substituted with new debug code!)
125     '''
126     confo=HookeConfig()
127     print confo.load_config('hooke.conf')
128
129 if __name__ == '__main__':
130     debug()