def OnSave(self, event): dialog = wx.FileDialog(self, "Save Figure (If no extension is provided, pdf is used).", style=wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT) # wildcard="Current Page|*.pdf|All Pages (*_n.*)|*.gif") if dialog.ShowModal() == wx.ID_OK: path = dialog.GetPath() # wc = dialog.GetFilterIndex() wc = 0 path, filename = os.path.split(path) if wc == 0: # save only current page if not '.' in filename: filename += '.pdf' self.figure.savefig(os.path.join(path, filename)) elif wc == 1: # save all pages fn_temp = os.path.join('{dir}', '{root}_{n}.{ext}') if '.' in filename: filename, ext = filename.split('.') else: ext = 'pdf' i = self.current_page_i self.figure.savefig(fn_temp.format(dir=path, root=filename, n=i, ext=ext)) pages = range(self.n) pages.remove(i) prog = ui.progress(self.n-1, "Saving Figures", "Saving Figures") for i in pages: self.show_page(i) self.figure.savefig(fn_temp.format(dir=path, root=filename, n=i, ext=ext)) prog.advance() prog.terminate() else: logging.error(" invalid wildcard: %s"%wc)
def _segments(self, datasets): """ useage:: >>> for (new_seg, filename) in self._segments(self.children) :arg datasets: the datasets that have been created based on the call to self._datasets() :returns: new_seg: segment object; filename: filename, used to construct filename variabels """ logging.debug("preparing to IMPORT") # prepare properties and varlist if there are event channels i_properties = self.get_input_properties() ### prepare -- epoching ### ### ### epoch_length = self.p.epoch_length.get() epoch_var = self.p.epoch_var.get() # start import loop files = self.p.source.get_as_filelist() # progress bar n = len(files) prog_i = 0 prog_ttl = "Importing {n} Files".format(n=n) prog_msg = "{i} of {n} files done...".format(n=n, i='{i}') prog = ui.progress(i_max=n, title=prog_ttl, message=prog_msg.format(i=0)) for path in files: filename = os.path.basename(path) logging.debug("%i of %i: %s" % (prog_i, n, filename)) data, properties = self.get_file(path) # id id, ext = os.path.splitext(filename) while id in self._imported_files: id += '_' # FIXME: self._imported_files[id] = os.path.getmtime(path) # assert that input conforms to dataset properties; if not, skip. errors = [] superfluous = [] t0 = i_properties['t0'] for k, v in properties.iteritems(): if k in i_properties: if i_properties[k] != v: txt = " Property [{p}] mismatch: {vf} (file) != {vd} dataset" errors.append(txt.format(p=k, vf=v, vd=i_properties[k])) else: superfluous.append(k) for k in superfluous: properties.pop(k) if errors: txt = '\n'.join(["! Skipped: {f}".format(f=filename)] + errors) logging.warning(txt) else: # Add to datasets n_samples, n_sensors = data.shape if epoch_length: # check shape if n_samples % epoch_length != 0: raise ValueError("n_samples not divisible by epoch_" "length for '%s'"%filename) epoch_indexes = np.arange(0, n_samples, epoch_length) i_length = epoch_length else: epoch_indexes = [0] i_length = n_samples for i, i_start in enumerate(epoch_indexes): for dataset in datasets: index = dataset['source_index'] i_stop = i_start + i_length c_data = data[i_start:i_stop, index] # segment name if epoch_length: seg_name = "%s, %s-%s" % (filename, i_start, i_stop) id = '%s_%s_%s' % (filename, i_start, i_stop) else: seg_name = filename if dataset.properties['data_type'] == 'event': samplingrate = dataset['samplingrate'] seg_duration = c_data.shape[0] / samplingrate e_list = _evts_from_data(c_data, samplingrate, t0=t0, threshold=dataset.properties['evt_threshold'], targets=dataset.properties['evt_targets']) properties.update(duration = seg_duration, shape = e_list.shape) new_seg = _seg.Event_Segment(properties=properties, data=e_list, dataset=dataset, name=seg_name, id=id) else: # UTS properties.update(shape = c_data.shape) new_seg = _seg.UTS_Segment(properties=properties, dataset=dataset, data=c_data, name=seg_name, id=id) # assign epoch id if epoch_length: if epoch_var is not None: new_seg[epoch_var] = i yield new_seg, filename # progress monitoring prog_i += 1 if prog: prog.advance(prog_msg.format(i=prog_i)) # terminate progress bar if prog: prog.terminate()