def read_nbm(filename): """Reads NBF metadata, which is usually saved in an 'nbm' file. This file contains basic information about the recording.""" attrs = {} iprint('Reading nbm file {}.'.format(filename)) with open(filename) as f: for line in f: x = line[0:-1].split('=') # No spaces allowed please!! if len(x) == 2: attrs[x[0]] = x[1] iprint('Attribute: {} = {}'.format(x[0], x[1])) else: wprint("Don't know what to do with this line: {}".format(line)) try: date = attrs['date'] except: date = None if attrs['dirname'][0] == '.': new_dirname = os.path.dirname(os.path.abspath(filename)) attrs['dirname'] = new_dirname metadata = nb_metadata(attrs['dirname'], sample_rate=float(attrs['sample_rate']), nchannels=int(attrs['nchannels']), start=int(attrs['start']), end=int(attrs['end']), date=date, attributes=attrs) return metadata
def parse_uri(uri, filename=None): true_uri, rel = resolve_relative_paths(uri, filename) if rel and filename is None: wprint('URI path is relative, but no base filename is specified.') else: uri = true_uri iprint('URI: {}'.format(uri)) desc = urisplit(uri) return desc, rel
def create_table(self, es, name): es.execute('pragma foreign_key=ON;') try: es.execute(self.defs[name]) if self.verbose: iprint('Created table {}.'.format(name)) except Exception as e: if self.verbose: wprint('Could not create table {}. Error: {}'.format(name,e))
def maybe_fix_filenames(self, dirname): old = os.path.dirname(self.filename) iprint('maybe_fix_filenames: dirname={}'.format(dirname)) iprint('maybe_fix_filenames: old dirname={}'.format(old)) if dirname != old: wprint('Directory was moved. Resetting...') name = os.path.basename(dirname) self.filename = os.path.join(dirname, '{}.nbm'.format(name)) self.rawfile = os.path.join(dirname, '{}.raw'.format(name)) self.meanfile = os.path.join(dirname, '{}-mean.pyr'.format(name)) self.minfile = os.path.join(dirname, '{}-min.pyr'.format(name)) self.maxfile = os.path.join(dirname, '{}-max.pyr'.format(name))
def set_functions(self, basis_id, fn_list): n = len(fn_list[0]) for vec in fn_list: if len(vec) != n: wprint( 'db3.set_functions: All elements of fn_list must have equal length. Not adding this basis set!' ) return False for i in range(len(fn_list)): vec = fn_list[i] for k in range(n): self.cur.execute( 'insert or replace into functions values({}, {}, {}, {});'. format(basis_id, i, k, vec[k])) self.commit() return True
def cue(es): """Return a vector of length N (N = number of spike events) that is 0 up until a desired cue event (e.g., injection or behavioral event) and k (the event label) after that point.""" nev = es.event_count() event_info = es.get_events() if nev < 1: wprint( "Error: this file should have exactly 1 event. It contains {}!". format(nev)) else: event_time = event_info[0][0] event_code = event_info[1][0] n = es.get_spike_count() vec = np.zeros(n) state = 0 for i in range(0, n): curr_time = es.get_spike_timestamp(i) if curr_time > event_time: vec[i] = event_code return vec
def add_basis(self, basis_id, sigma, width, nfuncs=5, offset=0, name="gaussian_basis"): """Adds a Gaussian basis with the specified sigma and width in SAMPLES to the database, with the desired ID.""" ret = True self.cur.execute( 'SELECT * from basis where basisID == {};'.format(basis_id)) result = self.cur.fetchall() iprint("{}".format(result)) if result is None or len(result) == 0: self.cur.execute( 'INSERT INTO basis values ({}, {}, {}, {}, {}, "{}");'.format( basis_id, offset, sigma, width, nfuncs, name)) self.conn.commit() else: result = result[0] if math.fabs(result[1] - sigma) > 1.0e-06 or result[ 2] != width or result[3] != nfuncs: wprint( "Basis ID {} is already in use with these parameters: sigma={}, window width={}, n={}" .format(result[0], result[1], result[2], result[3])) iprint("Here are the basis ID's in use in this file:") self.cur.execute('SELECT * from basis;') result = self.cur.fetchall() iprint("BasisID offset sigma window nfuncs name") for tuple in result: iprint("{} {} {} {} {} {}".format( tuple[0], tuple[1], tuple[2], tuple[3], tuple[4], tuple[5])) ret = False else: iprint( "Your basis set is already available in {}.".format(self)) ret = True return ret
def open(uri, filename=None): """Given a URI for a data source, open it and return the appropriate data source object.""" if True: desc, rel = parse_uri(uri, filename) else: new_uri, rel = resolve_relative_paths(uri, filename) if rel and filename is None: wprint('Data store path is relative, but no event store is specified.') else: uri = new_uri desc = urisplit(uri) iprint('Opening data store at {}'.format(uriunsplit(desc))) s = desc.scheme if s is None: return ndk.ds.neo_in.spike2(desc) elif s == 'pre': return ndk.ds.pre.pre_ds(desc) elif s == 'smr': return ndk.ds.neo_in.spike2(desc) # elif s == 'file': # return ndk.ds.neo.neo_in(desc) elif s == 'cass': return ndk.ds.cass.cdb(desc) elif s == 'wav': return ndk.ds.wav.wav_ds(desc.path) elif s == 'ds': return ndk.ds.mmap.mmap_ds(desc.path) elif s == 'edf': return ndk.ds.edf_ds(desc) elif s == 'nbm': iprint('NBF path: {}'.format(desc.path)) if rel and desc.path[0] == '/' and desc.path[1] == '.': return ndk.ds.nbf(desc.path[1:]) # Hack! else: return ndk.ds.nbf(desc.path) else: print("Don't know what do with this URI scheme: {} (in {})".format(s, uri))