Added illysam branch
[hooke.git] / lib / libhooke.py
1 #!/usr/bin/env python
2
3 '''
4 libhooke.py
5
6 General library of internal objects and utilities for Hooke.
7
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)
11
12 This program is released under the GNU General Public License version 2.
13 '''
14
15 import csv
16 import os.path
17 import numpy
18 import scipy
19
20 HOOKE_VERSION=['0.9.0_devel', 'Kenzo', '2010-01-31']
21 WX_GOOD=['2.6','2.8']
22 hookeDir=''
23
24 #constants for 'special' curves
25 #this can make it easier to understand what curve we are working on
26 EXTENSION = 0
27 RETRACTION = 1
28
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')
35     aFile=''.join(aFile)
36     return aFile
37
38 def get_file_path(filename, folders = []):
39     if os.path.dirname(filename) == '' or os.path.isabs(filename) == False:
40         path = ''
41         for folder in folders:
42             path = os.path.join(path, folder)
43         filename = os.path.join(hookeDir, path, filename)
44
45     return filename
46
47 def coth(z):
48     '''
49     hyperbolic cotangent
50     '''
51     return (numpy.exp(2 * z) + 1) / (numpy.exp(2 * z) - 1)
52
53 class ClickedPoint(object):
54     '''
55     this class defines what a clicked point on the curve plot is
56     '''
57     def __init__(self):
58
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
65
66     def find_graph_coords(self, xvector, yvector):
67         '''
68         Given a clicked point on the plot, finds the nearest point in the dataset (in X) that
69         corresponds to the clicked point.
70         '''
71         dists = []
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))
74
75         self.index=dists.index(min(dists))
76         self.graph_coords=(xvector[self.index], yvector[self.index])
77 #-----------------------------------------
78 #CSV-HELPING FUNCTIONS
79
80 def transposed2(lists, defval=0):
81     '''
82     transposes a list of lists, i.e. from [[a,b,c],[x,y,z]] to [[a,x],[b,y],[c,z]] without losing
83     elements
84     (by Zoran Isailovski on the Python Cookbook online)
85     '''
86     if not lists: return []
87     return map(lambda *row: [elem or defval for elem in row], *lists)
88
89 def csv_write_dictionary(f, data, sorting='COLUMNS'):
90     '''
91     Writes a CSV file from a dictionary, with keys as first column or row
92     Keys are in "random" order.
93
94     Keys should be strings
95     Values should be lists or other iterables
96     '''
97     keys=data.keys()
98     values=data.values()
99     t_values=transposed2(values)
100     writer=csv.writer(f)
101
102     if sorting=='COLUMNS':
103         writer.writerow(keys)
104         for item in t_values:
105             writer.writerow(item)
106
107     if sorting=='ROWS':
108         print 'Not implemented!' #FIXME: implement it.