def rb_filter(): p = argparse.ArgumentParser(description=""" filter a sampled dataset """) p.add_argument("dat", help="dat file") p.add_argument("-o", "--out", help="name of output dat file") p.add_argument("--order", help="filter order", default=3, type=int) p.add_argument("--highpass", help="highpass frequency", type=float) p.add_argument("--lowpass", help="lowpass frequency", type=float) p.add_argument("-f", "--filter", help="filter type: butter or bessel", default="bessel") opt = p.parse_args() dtype = bark.read_metadata(opt.dat)['dtype'] stream.read(opt.dat)._analog_filter(opt.filter, highpass=opt.highpass, lowpass=opt.lowpass, order=opt.order).write(opt.out, dtype) attrs = bark.read_metadata(opt.out) attrs['highpass'] = opt.highpass attrs['lowpass'] = opt.lowpass attrs['filter'] = opt.filter attrs['filter_order'] = opt.order bark.write_metadata(opt.out, **attrs)
def test_read_metadata(tmpdir): # entry/dir with good metadata file entry_path = os.path.join(tmpdir.strpath, "myentry") dtime = arrow.get("2020-01-02T03:04:05+06:00").datetime entry = bark.create_entry(entry_path, dtime, food="pizza") entry_metadata = bark.read_metadata(entry_path) assert 'timestamp' in entry_metadata # try to read entry/dir metadata file directly with pytest.raises(ValueError): entry_metadata = bark.read_metadata(os.path.join(entry_path, 'meta.yaml')) # entry/dir without metadata file with pytest.raises(FileNotFoundError): entry_metadata = bark.read_metadata(tmpdir.strpath) # dataset with good metadata file data = np.zeros((10,3), dtype='int16') params = dict(sampling_rate=30000, units="mV", unit_scale=0.025, extra="barley") dset_path = os.path.join(tmpdir.strpath, "test_sampled") dset = bark.write_sampled(datfile=dset_path, data=data, **params) dset_metadata = bark.read_metadata(dset_path) assert 'sampling_rate' in dset_metadata # try to read dataset metadata file directly with pytest.raises(ValueError): dset_metadata = bark.read_metadata(dset_path + '.meta.yaml') # dataset without metadata file os.remove(dset_path + '.meta.yaml') with pytest.raises(FileNotFoundError): dset_metadata = bark.read_metadata(dset_path) # dataset that doesn't exist with pytest.raises(FileNotFoundError): dset_metadata = bark.read_metadata(os.path.join(tmpdir.strpath, 'fake_dset.dat'))
def datchunk(dat, stride, use_seconds): attrs = bark.read_metadata(dat) sr = attrs['sampling_rate'] if use_seconds: stride = stride * sr stride = int(stride) basename = os.path.splitext(dat)[0] for i, chunk in enumerate(stream.read(dat, chunksize=stride)): filename = "{}-chunk-{}.dat".format(basename, i) attrs['offset'] = stride * i bark.write_sampled(filename, chunk, **attrs)
def meta_attr(): p = argparse.ArgumentParser( description="Create/Modify a metadata attribute") p.add_argument("name", help="name of bark object (Entry or Dataset)") p.add_argument("attribute", help="name of bark attribute to create or modify") p.add_argument("value", help="value of attribute") args = p.parse_args() name, attr, val = (args.name, args.attribute, args.value) attrs = bark.read_metadata(name) try: attrs[attr] = eval(val) # try to parse except Exception: attrs[attr] = val # assign as string bark.write_metadata(name, **attrs)
def datchunk(dat, stride, use_seconds, one_cut): def write_chunk(chunk, attrs, i): filename = "{}-chunk-{}.dat".format(basename, i) attrs['offset'] = stride * i bark.write_sampled(filename, chunk, **attrs) attrs = bark.read_metadata(dat) if use_seconds: stride = stride * attrs['sampling_rate'] stride = int(stride) basename = os.path.splitext(dat)[0] if one_cut: sds = bark.read_sampled(dat) write_chunk(sds.data[:stride,:], attrs, 0) write_chunk(sds.data[stride:,:], attrs, 1) else: for i, chunk in enumerate(stream.read(dat, chunksize=stride)): write_chunk(chunk, attrs, i)
def meta_column_attr(): p = argparse.ArgumentParser( description="Create/Modify a metadata attribute for a column of data") p.add_argument("name", help="name of bark object (Entry or Dataset)") p.add_argument("column", help="name of the column of a Dataset") p.add_argument("attribute", help="name of bark attribute to create or modify") p.add_argument("value", help="value of attribute") args = p.parse_args() name, column, attr, val = (args.name, args.column, args.attribute, args.value) attrs = bark.read_metadata(name) columns = attrs['columns'] if 'dtype' in attrs: column = int(column) try: columns[column][attr] = eval(val) # try to parse except Exception: columns[column][attr] = val # assign as string bark.write_metadata(name, **attrs)
def main(): import argparse p = argparse.ArgumentParser(description=""" Open raw binary file in neuroscope. """) p.add_argument("dat", help="Raw binary file to open.") args = p.parse_args() fname = os.path.splitext(args.dat)[0] + ".xml" if not os.path.isfile(fname): # load metadata meta = read_metadata(args.dat) # convert to neuroscope xml_tree = meta_to_neuroscope_xml(meta) # save to file xml_tree.write(fname, xml_declaration=True, short_empty_elements=False) # open neuroscope try: subprocess.run(['neuroscope', args.dat]) except AttributeError: subprocess.call(['neuroscope', args.dat])
def rb_to_audio(): p = argparse.ArgumentParser() p.add_argument("dat", help="""dat file to convert to audio, can be any number of channels but you probably want 1 or 2""") p.add_argument("out", help="name of output file, with filetype extension") opt = p.parse_args() attrs = bark.read_metadata(opt.dat) sr = str(attrs['sampling_rate']) ch = str(len(attrs['columns'])) dt = numpy.dtype(attrs['dtype']) bd = str(dt.itemsize * 8) if dt.name[:5] == 'float': enc = 'floating-point' elif dt.name[:3] == 'int': enc = 'signed-integer' elif dt.name[:4] == 'uint': enc = 'unsigned-integer' else: raise TypeError('cannot handle dtype of ' + dtname) if dt.byteorder == '<': order = 'little' elif dt.byteorder == '>': order = 'big' elif dt.byteorder == '=': # native order = sys.byteorder else: raise ValueError('unrecognized endianness: ' + dt.byteorder) sox_cmd = [ 'sox', '-r', sr, '-c', ch, '-b', bd, '-e', enc, '--endian', order, '-t', 'raw', opt.dat, opt.out ] try: subprocess.run(sox_cmd) except FileNotFoundError as e: if "'sox'" in str(e): raise FileNotFoundError(str(e) + '. dat-to-audio requires SOX') else: raise
def extract_sc(entry_fn, dataset, sc_suffix, out_fn): sr = bark.read_metadata(os.path.join(entry_fn, dataset))['sampling_rate'] # determine file names results_path = get_sc_path(entry_fn, dataset, sc_suffix, 'result') templates_path = get_sc_path(entry_fn, dataset, sc_suffix, 'templates') # extract times and amplitudes with h5py.File(results_path, 'r') as rf: cluster_times = { unique_temp_name(name): np.array(indices).astype(float) / sr for name, indices in rf['spiketimes'].items() } cluster_amplitudes = { unique_temp_name(name): np.array(amplitudes) for name, amplitudes in rf['amplitudes'].items() } cluster_names = sorted(cluster_times.keys(), key=int) event_list = [] for n in cluster_names: event_list.extend([ SpikeEvent(n, time[0], amp[0]) for time, amp in zip(cluster_times[n], cluster_amplitudes[n]) ]) event_list.sort(key=lambda se: se.time) # extract grades and center pad with h5py.File(templates_path, 'r') as tf: cluster_grades = [SC_GRADES_DICT[tag[0]] for tag in tf['tagged']] cluster_grades = { n: cluster_grades[idx] for idx, n in enumerate(cluster_names) } NUM_TEMPLATES = int(tf['temp_shape'][2][0] / 2) NUM_CHANNELS = int(tf['temp_shape'][0][0]) NUM_SAMPLES = int(tf['temp_shape'][1][0]) CHAN_BY_SAMPLE = NUM_CHANNELS * NUM_SAMPLES full_templates = {} for t in range(NUM_TEMPLATES): y_vals = tf['temp_y'][0] == t x_vals = tf['temp_x'][:, y_vals][0].astype(int) reconst = np.zeros(CHAN_BY_SAMPLE) for loc in x_vals: reconst[loc] = tf['temp_data'][:, loc][0] reshaped = reconst.reshape((NUM_CHANNELS, -1)) full_templates[t] = np.copy(reshaped) center_channel = {} for t in full_templates: # note that this assumes negative-going spikes min_across_channels = list(np.amin(full_templates[t], axis=1)) total_min = min(min_across_channels) center_channel[str(t)] = min_across_channels.index(total_min) # write times and amplitudes to event dataset attrs = { 'columns': { 'start': { 'units': 's' }, 'name': { 'units': None }, 'amplitude': { 'units': None } }, 'datatype': 1001, 'sampling_rate': sr, 'templates': { name: { 'score': cluster_grades[name], 'sc_name': long_temp_name(name), 'center_channel': center_channel[name] } for name in cluster_names } } return bark.write_events( os.path.join(entry_fn, out_fn), pandas.DataFrame({ 'start': [event.time for event in event_list], 'name': [event.name for event in event_list], 'amplitude': [event.amplitude for event in event_list] }), **attrs)