Hooke(GUI)
[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 coth(z):
30     '''
31     Hyperbolic cotangent.
32     '''
33     return (numpy.exp(2 * z) + 1) / (numpy.exp(2 * z) - 1)
34
35 def delete_empty_lines_from_xmlfile(filename):
36     #the following 3 lines are needed to strip newlines.
37     #Otherwise, since newlines are XML elements too, the parser would read them
38     #(and re-save them, multiplying newlines...)
39     aFile=file(filename).read()
40     aFile=aFile.split('\n')
41     aFile=''.join(aFile)
42     return aFile
43
44 def fit_interval_nm(start_index, x_vect, nm, backwards):
45     '''
46     Calculates the number of points to fit, given a fit interval in nm
47     start_index: index of point
48     plot: plot to use
49     backwards: if true, finds a point backwards.
50     '''
51     c = 0
52     i = start_index
53     maxlen=len(x_vect)
54     while abs(x_vect[i] - x_vect[start_index]) * (10**9) < nm:
55         if i == 0 or i == maxlen-1: #we reached boundaries of vector!
56             return c
57         if backwards:
58             i -= 1
59         else:
60             i += 1
61         c += 1
62     return c
63
64 def get_file_path(filename, folders = []):
65     if os.path.dirname(filename) == '' or os.path.isabs(filename) == False:
66         path = ''
67         for folder in folders:
68             path = os.path.join(path, folder)
69         filename = os.path.join(hookeDir, path, filename)
70     return filename
71
72 def remove_extension(filename):
73     '''
74     Removes the extension from a filename.
75     '''
76     name, extension = os.path.splitext(filename)
77     return name
78
79 #CSV-HELPING FUNCTIONS
80 def csv_write_dictionary(f, data, sorting='COLUMNS'):
81     '''
82     Writes a CSV file from a dictionary, with keys as first column or row
83     Keys are in "random" order.
84
85     Keys should be strings
86     Values should be lists or other iterables
87     '''
88     keys=data.keys()
89     values=data.values()
90     t_values=transposed2(values)
91     writer=csv.writer(f)
92
93     if sorting=='COLUMNS':
94         writer.writerow(keys)
95         for item in t_values:
96             writer.writerow(item)
97
98     if sorting=='ROWS':
99         print 'Not implemented!' #FIXME: implement it.
100
101 def transposed2(lists, defval=0):
102     '''
103     transposes a list of lists, i.e. from [[a,b,c],[x,y,z]] to [[a,x],[b,y],[c,z]] without losing
104     elements
105     (by Zoran Isailovski on the Python Cookbook online)
106     '''
107     if not lists: return []
108     return map(lambda *row: [elem or defval for elem in row], *lists)
109