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
33 return (numpy.exp(2 * z) + 1) / (numpy.exp(2 * z) - 1)
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')
44 def fit_interval_nm(start_index, x_vect, nm, backwards):
46 Calculates the number of points to fit, given a fit interval in nm
47 start_index: index of point
49 backwards: if true, finds a point backwards.
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!
64 def get_file_path(filename, folders = []):
65 if os.path.dirname(filename) == '' or os.path.isabs(filename) == False:
67 for folder in folders:
68 path = os.path.join(path, folder)
69 filename = os.path.join(hookeDir, path, filename)
72 def remove_extension(filename):
74 Removes the extension from a filename.
76 name, extension = os.path.splitext(filename)
79 #CSV-HELPING FUNCTIONS
80 def csv_write_dictionary(f, data, sorting='COLUMNS'):
82 Writes a CSV file from a dictionary, with keys as first column or row
83 Keys are in "random" order.
85 Keys should be strings
86 Values should be lists or other iterables
90 t_values=transposed2(values)
93 if sorting=='COLUMNS':
99 print 'Not implemented!' #FIXME: implement it.
101 def transposed2(lists, defval=0):
103 transposes a list of lists, i.e. from [[a,b,c],[x,y,z]] to [[a,x],[b,y],[c,z]] without losing
105 (by Zoran Isailovski on the Python Cookbook online)
107 if not lists: return []
108 return map(lambda *row: [elem or defval for elem in row], *lists)