6 Export commands for Hooke.
8 Copyright 2010 by Dr. Rolf Schmidt (Concordia University, Canada)
10 This program is released under the GNU General Public License version 2.
13 import lib.libhooke as lh
15 wxversion.select(lh.WX_GOOD)
22 class exportCommands(object):
24 Export force curves, fits and results in different formats
32 Exports all fitting results (if available) in a columnar ASCII format.
34 ext = self.GetStringFromConfig('export', 'fits', 'ext')
35 folder = self.GetStringFromConfig('export', 'fits', 'folder')
36 prefix = self.GetStringFromConfig('export', 'fits', 'prefix')
37 separator = self.GetStringFromConfig('export', 'fits', 'separator')
38 #TODO: add list for Tab, Space, Comma, Other
41 active_file = self.GetActiveFile()
42 plot = self.GetDisplayedPlot()
44 #add empty columns before adding new results if necessary
46 for results_str, results in plot.results.items():
47 for curve in results.results:
51 header_str += curve.label + '_x (' + curve.units.x + ')' + separator + curve.label + '_y (' + curve.units.y + ')'
52 output.append(header_str)
53 for index, row in enumerate(curve.x):
54 output.append(separator.join([str(curve.x[index]), str(curve.y[index])]))
56 #TODO: add option to replace or add the new file extension
57 #add option to rename file from default.000 to default_000
58 filename = os.path.basename(active_file.filename)
59 filename = ''.join([prefix, filename, '_', curve.label, '.', ext])
60 filename = os.path.join(folder, filename)
61 output_file = open(filename, 'w')
62 output_file.write('\n'.join(output))
65 def do_force_curve(self):
68 Saves the current curve as a text file
69 Columns are, in order:
70 X1 , Y1 , X2 , Y2 , X3 , Y3 ...
73 Syntax: txt [filename] {plot to export}
76 ext = self.GetStringFromConfig('export', 'force_curve', 'ext')
77 folder = self.GetStringFromConfig('export', 'force_curve', 'folder')
78 prefix = self.GetStringFromConfig('export', 'force_curve', 'prefix')
79 separator = self.GetStringFromConfig('export', 'force_curve', 'separator')
80 #TODO: add list for Tab, Space, Comma, Other
83 active_file = self.GetActiveFile()
84 #create the header from the raw plot (i.e. only the force curve)
85 plot = self.GetDisplayedPlotRaw()
89 for index, curve in enumerate(plot.curves):
90 #TODO: only add labels for original curves (i.e. excluding anything added after the fact)
91 header_str += curve.label + '_x (' + curve.units.x + ')' + separator + curve.label + '_y (' + curve.units.y + ')'
92 if index < len(plot.curves) - 1:
93 header_str += separator
94 output.append(header_str)
96 #export the displayed plot
97 plot = self.GetDisplayedPlot()
98 extension = plot.curves[lh.EXTENSION]
99 retraction = plot.curves[lh.RETRACTION]
100 for index, row in enumerate(extension.x):
101 output.append(separator.join([str(extension.x[index]), str(extension.y[index]), str(retraction.x[index]), str(retraction.y[index])]))
104 #TODO: add option to replace or add the new file extension
105 #add option to rename file from default.000 to default_000
106 filename = os.path.basename(active_file.filename)
107 filename = ''.join([prefix, filename, '.', ext])
108 filename = os.path.join(folder, filename)
109 output_file = open(filename, 'w')
110 output_file.write('\n'.join(output))
115 Exports the note for all the files in a playlist.
117 filename = self.GetStringFromConfig('export', 'notes', 'filename')
118 folder = self.GetStringFromConfig('export', 'notes', 'folder')
119 prefix = self.GetStringFromConfig('export', 'notes', 'prefix')
120 use_playlist_filename = self.GetBoolFromConfig('export', 'notes', 'use_playlist_filename')
122 playlist = self.GetActivePlaylist()
124 for current_file in playlist.files:
125 output_str = ''.join([output_str, current_file.filename, ' | ', current_file.note, '\n'])
127 output_str = ''.join(['Notes taken at ', time.asctime(), '\n', playlist.filename, '\n', output_str])
128 if use_playlist_filename:
129 path, filename = os.path.split(playlist.filename)
130 filename = lh.remove_extension(filename)
131 filename = ''.join([prefix, filename, '.txt'])
132 filename = os.path.join(folder, filename)
133 output_file = open(filename, 'w')
134 output_file.write(output_str)
137 dialog = wx.MessageDialog(None, 'No notes found, file not saved.', 'Info', wx.OK)
140 def do_overlay(self):
142 Exports all retraction files in a playlist with the same scale.
143 The files can then be overlaid in a graphing program to see which
144 ones have the same shape.
145 Use this export command only on filtered lists as it takes a long time
146 to complete even with a small number of curves.
148 playlist = self.GetActivePlaylist()
150 filename_prefix = self.GetStringFromConfig('export', 'overlay', 'prefix')
154 number_of_curves = playlist.count
155 message_str = ''.join([str(number_of_curves), ' files to load.\n\n'])
156 progress_dialog = wx.ProgressDialog('Loading', message_str, maximum=number_of_curves, parent=self, style=wx.PD_APP_MODAL|wx.PD_SMOOTH|wx.PD_AUTO_HIDE)
157 for index, current_file in enumerate(playlist.files):
158 current_file.identify(self.drivers)
159 plot = current_file.plot
161 plot.raw_curves = copy.deepcopy(plot.curves)
162 #apply all active plotmanipulators and add the 'manipulated' data
163 for plotmanipulator in self.plotmanipulators:
164 if self.GetBoolFromConfig('core', 'plotmanipulators', plotmanipulator.name):
165 plot = plotmanipulator.method(plot, current_file)
166 #add corrected curves to plot
167 plot.corrected_curves = copy.deepcopy(plot.curves)
169 curve = current_file.plot.corrected_curves[lh.RETRACTION]
170 differences_x.append(curve.x[0] - curve.x[-1])
171 differences_y.append(curve.x[0] - curve.y[-1])
172 progress_dialog.Update(index, ''.join([message_str, 'Loading ', str(index + 1), '/', str(number_of_curves)]))
173 progress_dialog.Destroy()
175 max_x = max(differences_x)
176 max_y = max(differences_y)
177 message_str = ''.join([str(number_of_curves), ' files to export.\n\n'])
178 for index, current_file in enumerate(playlist.files):
179 curve = current_file.plot.corrected_curves[lh.RETRACTION]
182 new_x = [x - first_x for x in curve.x]
183 new_y = [y - first_y for y in curve.y]
187 for row_index, row in enumerate(new_x):
188 output_str += ''.join([str(new_x[row_index]), ', ', str(new_y[row_index]), '\n'])
191 filename = ''.join([filename_prefix, current_file.name])
192 filename = current_file.filename.replace(current_file.name, filename)
193 output_file = open(filename, 'w')
194 output_file.write(output_str)
196 progress_dialog.Destroy()
198 def do_results(self, append=None, filename='', separator=''):
200 Exports all visible fit results in a playlist into a delimited text file
201 append: set append to True if you want to append to an existing results file
202 filename: the filename and path of the results file
203 separator: the separator between columns
206 append = self.GetStringFromConfig('export', 'results', 'append')
208 filename = self.GetStringFromConfig('export', 'results', 'filename')
210 separator = self.GetStringFromConfig('export', 'results', 'separator')
212 playlist = self.GetActivePlaylist()
215 for current_file in playlist.files:
216 if len(current_file.plot.results) > 0:
217 for key in current_file.plot.results.keys():
218 #if there are different types of fit results in the playlist, the header might have to change
219 #here, we generate a temporary header and compare it to the current header
220 #if they are different, the tempeorary header is used
221 #we get the header from the fit and add the 'filename' column
222 temporary_header_str = ''.join([current_file.plot.results[key].get_header_as_str(), separator, 'Filename'])
223 if temporary_header_str != header_str:
224 header_str = ''.join([current_file.plot.results[key].get_header_as_str(), separator, 'Filename'])
225 output_str = ''.join([output_str, header_str, '\n'])
226 for index, result in enumerate(current_file.plot.results[key].results):
228 #similar to above, we get the result from the fit and add the filename
229 line_str = current_file.plot.results[key].get_result_as_string(index)
230 line_str = ''.join([line_str, separator, current_file.filename])
231 output_str = ''.join([output_str, line_str, '\n'])
233 output_str = ''.join(['Analysis started ', time.asctime(), '\n', output_str])
235 if append and os.path.isfile(filename):
236 output_file = open(filename,'a')
238 output_file = open(filename, 'w')
239 output_file.write(output_str)
242 dialog = wx.MessageDialog(None, 'No results found, file not saved.', 'Info', wx.OK)