def add_ext(path, ext, multiple=False, ask_overwrite=True): """ Adds ext to path; kwargs ----- multiple=False: =False: if path has an extension, nothing will be done ='r', 'replace': existing extension will be replaced. ='a', 'add': extension will be added independent of existing extension """ name, old_ext = os.path.splitext(path) # remove leading dots old_ext = old_ext.lstrip(os.path.extsep) ext = ext.lstrip(os.path.extsep) # modify if old_ext: if multiple in ['r', 'replace']: pass elif (multiple in ['a', 'add']) and (old_ext != ext): ext = os.path.extsep.join([old_ext, ext]) else: ext = old_ext path = os.path.extsep.join([name, ext]) if ask_overwrite: if os.path.exists(path): if not ui.ask(title="Overwrite File?", message="The File '%s' already exists. Overwrite the existing file?" % path): return None return path
def attach(self, dictionary): """ Adds a dictionary to the globals and keeps track of the items so that they can be removed safely with the detach function. """ # self._attached_items = {id(dict_like) -> {key -> value}} present = [] for k in dictionary: if k in self.global_namespace: if id(self.global_namespace[k]) != id(dictionary[k]): present.append(k) if present: title = "Overwrite Items?" message = ("The following items are associated with different " "objects in global namespace. Should we replace them?" + os.linesep) message = os.linesep.join((message, ', '.join(repr(k) for k in present))) answer = ui.ask(title, message, True) if answer is None: return elif answer: attach = dictionary else: attach = dict((k,v) for k,v in dictionary.iteritems() if k not in present) else: attach = dictionary self.global_namespace.update(attach) items = self._attached_items.setdefault(id(dictionary), {}) items.update(attach) msg = "attached: %s" % str(attach.keys()) print msg
def OnRemove(self, event): target = None rm = sorted(self._rm_comp) while not target: dlg = wx.TextEntryDialog(self, "What name should the new ndvar be assigned in the dataset?", "Choose Name for New Variable", "%s" % self._Y.name) if dlg.ShowModal() == wx.ID_OK: newname = str(dlg.GetValue()) if newname in self._dataset: msg = ("The dataset already contains an item named %r. " "Should it be replaced? The item is:\n\n%r" % (newname, self._dataset[newname])) answer = ui.ask("Replace %r?" % newname, msg) if answer is True: target = newname elif answer is None: pass else: return else: target = newname else: return # if we made it down here, remove the component: self._dataset[target] = self.pca.subtract(rm, name=target) self.Close()
def collectstats(self, cells, over, mask=None, mp=False, pool=None, save='ask', **kwargs): """ cells: address; statistics are extracted for each valid combination of values in this address over: variable specifying the cases within the cells (usually subject) - adds statistics segment for all values of cells in address to self.stats - forwards statsForAddress_over_ kwargs kwargs: mp False 'outer' Spawn one process for each StatsSegment True do statsSegments serially but use multiple processes in StatsSegment.initWithSegments_over_ **kwargs: """ t0=time.time() msg = "--> Collecting Statistics for {0} over {1}, mp={2}" print msg.format(cells.name, over.name, str(mp)) if len(self.stats) > 0: msg = "self.stats currently contains the following statistics: %s"%str(self.stats.keys()) if ui.ask(title="Clear existing statistics?", message=msg): self.stats = {} cells = biovars.asaddress(cells) if mask == None: all_segments = self.segments else: all_segments = mask.filter(self.segments) segs_sorted = cells.sort(all_segments) # progress bar # n = len(segs_sorted) # i = 0 # prog_ttl = "Collecting statistics for {n} cells".format(n=n) # prog_msg = "{name} ({i} of {n}) done...".format(name='{name}', n=n, i='{i}') # prog = ui.progress(i_max=n, # title=prog_ttl, # message=prog_msg.format(name='', i=0)) label_dic = cells.short_labels() for address, segs in segs_sorted.iteritems(): label = label_dic[address] # color = cells.color_for_value(key) s = StatsSegment(self.properties, name=label)#, color=color) s.initWithSegments_over_(segs, over, **kwargs) # make sure label is a valid name if label[0].isalpha(): valid_label = label else: valid_label = 'c' + label self.stats[valid_label] = s # close all files self._store.close() # # progress bar # if prog: # i += 1 # prog.advance(prog_msg.format(name=valid_label, i=i)) # if prog: # prog.terminate() # if mp=='outer': # for key, label in cells.dictionary.iteritems(): # self.stats[label] = self.stats[label].recv() print '\n'.join(["---", "Finished after %s"%(time.time()-t0), "Dataset now contains stats: %s"%self.stats.keys()]) # save the experiment if save == 'ask': msg = "Save Experiment with newly added statistics segments?" save = ui.ask(title="Save Experiment?", message=msg) if save: self.experiment.save()
def export_mat(dataset, values=None, destination=None): """ Usage:: >>> export_mat(dataset, ... values={varname -> {cellname -> value}}, ... destination='/path/to/file.mat') Arguments --------- dataset : dataset dataset containing the trial list values : `None` or `{varname -> {cellname -> value}}` additional values that should be stored, where: `varname` is the variable name by which the struct will be available in matlab; `cellname` is the name of the struct's field, and `value` is the value of that field. destination : None or str Path where the mat file should be saved. If `None`, the ui will ask for a location. """ # Make sure we have a valid destination if destination is None: print_path = True destination = ui.ask_saveas("Mat destination", "Where do you want to save the mat file?", ext=[('mat', "Matlab File")]) if not destination: print "aborted" return else: print_path = False if not isinstance(destination, basestring): raise ValueError("destination is not a string") dirname = os.path.dirname(destination) if not os.path.exists(dirname): if ui.ask("Create %r?" % dirname, "No folder named %r exists. Should " "it be created?" % dirname): os.mkdir(dirname) else: print "aborted" return if not destination.endswith('mat'): os.path.extsep.join(destination, 'mat') # assemble the mat file contents mat = {'trials': list(dataset.itercases())} if values: mat.update(values) if print_path: print 'Saving: %r' % destination scipy.io.savemat(destination, mat, do_compression=True, oned_as='row')
def kit2fiff(paths=dict(mrk=None, elp=None, hsp=None, rawtxt=None, rawfif=None), sns='NYU-nellab-2010', sfreq=1000, lowpass=100, highpass=0, stim=xrange(168, 160, -1), stimthresh=2.5, add=None, # (188, 190), #xrange() aligntol=5, overwrite=False): """ Calls the ``mne_kit2fiff`` binary which reads multiple input files and combines them into a fiff file. Implemented after Gwyneth's Manual; for more information see the mne manual (p. 222). **Arguments:** paths : dict Dictionary containing paths to input and output files. Needs the folowing keys: 'mrk', 'elp', 'hsp', 'sns', 'rawtxt', 'outfif' experiment : str The experiment name as it appears in file names. highpass : scalar The highpass filter corner frequency (only for file info, does not filter the data). 0 Hz for DC recording. lowpass : scalar like highpass meg_sdir : str or tuple (see above) Path to the subjects's meg directory. If ``None``, a file dialog is displayed to ask for the directory. sfreq : scalar samplingrate of the data stimthresh : scalar The threshold value used when synthesizing the digital trigger channel add : sequence of int | None channels to include in addition to the 157 default MEG channels and the digital trigger channel. These numbers refer to the scanning order channels as listed in the sns file, starting from one. stim : iterable over ints trigger channels that are used to reconstruct the event cue value from the separate trigger channels. The default ``xrange(168, 160, -1)`` reconstructs the values sent through psychtoolbox. aligntol : scalar Alignment tolerance for coregistration [mm] overwrite : bool Automatically overwrite the target fiff file if it already exists. """ # get all the paths mrk_path = paths.get('mrk') elp_file = paths.get('elp') hsp_file = paths.get('hsp') raw_file = paths.get('rawtxt') out_file = paths.get('rawfif') if sns in _sns_files: sns_file = _sns_files[sns] elif os.path.exists(sns): sns_file = sns else: err = ("sns needs to the be the name of a buit-in sns file (%s) or a path" "to an existing sns file" % ', '.join(map(repr, _sns_files))) raise IOError(err) # convert the marker file mrk_file = MarkerFile(mrk_path) hpi_file = mrk_file.path # make sure target path exists out_dir = os.path.dirname(out_file) if not os.path.exists(out_dir): os.mkdir(out_dir) elif not overwrite and os.path.exists(out_file): if not ui.ask("Overwrite?", "Target File Already Exists at: %r. Should " "it be replaced?" % out_file): return cmd = [get_bin('mne', 'mne_kit2fiff'), '--elp', elp_file, '--hsp', hsp_file, '--sns', sns_file, '--hpi', hpi_file, '--raw', raw_file, '--out', out_file, '--stim', ':'.join(map(str, stim)), # '161:162:163:164:165:166:167:168' '--sfreq', sfreq, '--aligntol', aligntol, '--lowpass', lowpass, '--highpass', highpass, '--stimthresh', stimthresh, ] if add: cmd.extend(('--add', ':'.join(map(str, add)))) _run(cmd) # TODO: rename additional channels # how do I know their names?? ~/Desktop/test/EOG has MISC 28 and MISC 30 # if add: # cmd = [os.path.join(mne_dir, 'bin', 'mne_rename_channels'), # '--fif', out_file, # ] if not os.path.exists(out_file): raise RuntimeError("kit2fiff failed (see above)")