def readlabels(pathname): dt_i8 = np.dtype("<u1") with open(pathname, "rb") as bytestream: #Check magic is 2049 magic = _read32(bytestream) num_inputs = _read32(bytestream) buf = bytestream.read(num_inputs*dt_i8.itemsize) labels = np.frombuffer(buf, dtype=dt_i8) labels.shape = (num_inputs) return(labels)
def readlabels(pathname): dt_i8 = np.dtype("<u1") with open(pathname, "rb") as bytestream: #Check magic is 2049 magic = _read32(bytestream) num_inputs = _read32(bytestream) buf = bytestream.read(num_inputs * dt_i8.itemsize) labels = np.frombuffer(buf, dtype=dt_i8) labels.shape = (num_inputs) return (labels)
def readdata(pathname): dt_f32 = np.dtype("<f4") with open(pathname, "rb") as bytestream: #Check magic is 2049 magic = _read32(bytestream) samples = _read32(bytestream) rows = _read32(bytestream) cols = _read32(bytestream) buf = bytestream.read(rows*cols*samples*dt_f32.itemsize) dataset = np.frombuffer(buf, dtype=dt_f32) dataset.shape = (samples, rows, cols) return(dataset)
def readdata(pathname): dt_f32 = np.dtype("<f4") with open(pathname, "rb") as bytestream: #Check magic is 2049 magic = _read32(bytestream) samples = _read32(bytestream) rows = _read32(bytestream) cols = _read32(bytestream) buf = bytestream.read(rows * cols * samples * dt_f32.itemsize) dataset = np.frombuffer(buf, dtype=dt_f32) dataset.shape = (samples, rows, cols) return (dataset)
def readlabels2(pathname): dt_i8 = np.dtype("<u1") labels = [] with open(pathname, "rb") as bytestream: #Check magic is 2049 magic = _read32(bytestream) num_tests = _read32(bytestream) for i in range(0, num_tests): num_inputs = _read32(bytestream) buf = bytestream.read(num_inputs*dt_i8.itemsize) labels_temp = np.frombuffer(buf, dtype=dt_i8) labels_temp.shape = (num_inputs) labels.append(labels_temp) return(labels)
def readlabels2(pathname): dt_i8 = np.dtype("<u1") labels = [] with open(pathname, "rb") as bytestream: #Check magic is 2049 magic = _read32(bytestream) num_tests = _read32(bytestream) for i in range(0, num_tests): num_inputs = _read32(bytestream) buf = bytestream.read(num_inputs * dt_i8.itemsize) labels_temp = np.frombuffer(buf, dtype=dt_i8) labels_temp.shape = (num_inputs) labels.append(labels_temp) return (labels)
def readdata2(pathname): dt_f32 = np.dtype("<f4") dataset = [] with open(pathname, "rb") as bytestream: #Check magic is 2049 magic = _read32(bytestream) num_tests = _read32(bytestream) rows = _read32(bytestream) cols = _read32(bytestream) for i in range(0, num_tests): samples = _read32(bytestream) buf = bytestream.read(rows*cols*samples*dt_f32.itemsize) dataset_temp = np.frombuffer(buf, dtype=dt_f32) dataset_temp.shape = (samples, rows, cols) dataset.append(dataset_temp) return(dataset)
def readdata2(pathname): dt_f32 = np.dtype("<f4") dataset = [] with open(pathname, "rb") as bytestream: #Check magic is 2049 magic = _read32(bytestream) num_tests = _read32(bytestream) rows = _read32(bytestream) cols = _read32(bytestream) for i in range(0, num_tests): samples = _read32(bytestream) buf = bytestream.read(rows * cols * samples * dt_f32.itemsize) dataset_temp = np.frombuffer(buf, dtype=dt_f32) dataset_temp.shape = (samples, rows, cols) dataset.append(dataset_temp) return (dataset)
path = samplespath+"/"+samplefolder # Get Inputs with open(path+"/input", "rb") as bytestream: num_inputs = _read8(bytestream) buf = bytestream.read(num_inputs * dt_i8.itemsize) labels = np.frombuffer(buf, dtype=dt_i8) # Get Samples and Presamples dataset = [] predataset = [] allsamples = sorted(os.listdir(path+"/sample")) for sample in allsamples: with open(path+"/sample/"+sample, "rb") as bytestream: #Check magic is 2049 magic = _read32(bytestream) rows = _read32(bytestream) cols = _read32(bytestream) buf = bytestream.read(max_samples * cols * dt_f32.itemsize) dataset.append(np.frombuffer(buf, dtype=dt_f32)) with open(path+"/presample/"+sample, "rb") as bytestream: num_samples += 1 #Check magic is 2049 magic = _read32(bytestream) rows = _read32(bytestream) cols = _read32(bytestream) buf = bytestream.read(max_presamples * cols * dt_f32.itemsize) predataset.append(np.frombuffer(buf, dtype=dt_f32)) # Reshape dataset = np.array(dataset)
path = samplespath + "/" + samplefolder # Get Inputs with open(path + "/input", "rb") as bytestream: num_inputs = _read8(bytestream) buf = bytestream.read(num_inputs * dt_i8.itemsize) labels = np.frombuffer(buf, dtype=dt_i8) # Get Samples and Presamples dataset = [] predataset = [] allsamples = sorted(os.listdir(path + "/sample")) for sample in allsamples: with open(path + "/sample/" + sample, "rb") as bytestream: #Check magic is 2049 magic = _read32(bytestream) rows = _read32(bytestream) cols = _read32(bytestream) buf = bytestream.read(max_samples * cols * dt_f32.itemsize) dataset.append(np.frombuffer(buf, dtype=dt_f32)) with open(path + "/presample/" + sample, "rb") as bytestream: num_samples += 1 #Check magic is 2049 magic = _read32(bytestream) rows = _read32(bytestream) cols = _read32(bytestream) buf = bytestream.read(max_presamples * cols * dt_f32.itemsize) predataset.append(np.frombuffer(buf, dtype=dt_f32)) # Reshape dataset = np.array(dataset)
dt_f32 = np.dtype("<f4") electrodes = [False, False, False, False, False, False, False, False] masterfolder = "testing" samplefolder = "sample" #Read raw data path = os.path.abspath(os.path.join(__file__,"../")) testingpath = path + "/"+ masterfolder + "/" + samplefolder filename = os.listdir(testingpath)[0] max_rows = 260 with open(testingpath+"/"+filename, "rb") as readstream: magic = _read32(readstream) rows = _read32(readstream) cols = _read32(readstream) buf = readstream.read(max_rows * cols * dt_f32.itemsize) data = np.frombuffer(buf, dtype=dt_f32) data.shape = (max_rows, cols) min_thresh = -60000 max_thresh = -10000 #min_thresh = -38000 #max_thresh = -12000 for i in range(0,data.shape[1]): if max(data[:,i]) < max_thresh and min(data[:,i]) > min_thresh: electrodes[i] = True #DC Filter
import numpy as np from readbytes import _read8, _read32 from datafilters import apply_dc_filter from sklearn.ensemble import RandomForestClassifier num_channels = 8 max_rows = 360 masterfolder = "predicting" dt_f32 = np.dtype("<f4") #Read data from Hardware path = os.path.abspath(os.path.join(__file__, "../")) testingpath = path + "/predicting/sample" with open(testingpath + "/" + sys.argv[1], "rb") as readstream: magic = _read32(readstream) rows = _read32(readstream) cols = _read32(readstream) buf = readstream.read(max_rows * cols * dt_f32.itemsize) data = np.frombuffer(buf, dtype=dt_f32) data.shape = (max_rows, cols) #DC Filter fs = 250 enable_dc = True dc_lowcut = 1.0 #Only get alpha and beta, most related to movement dc_highcut = 13.0 dc_order = 2 dc_type = "bandpass" dc_func_type = "butter"