def draw(): t_now = util.timestamp(ms=True) ctx.translate(-1., -0.85, -1.5) ctx.rotate(*rotation_x) ctx.rotate(*rotation_y) colors = (1., 1., 1., 1.), (.7, 1., 1., 1.), (1., .7, .7, 1.), for s, sensor in enumerate(list(sensor_data)): samples = sensor_data[sensor] if len(samples): # x = [((t_now - sample[0]) / 10.0, (sample[1][0] - RANGE[0]) / (RANGE[1] - RANGE[0])) for sample in list(samples)] # y = [((t_now - sample[0]) / 10.0, (sample[1][1] - RANGE[0]) / (RANGE[1] - RANGE[0])) for sample in list(samples)] # z = [((t_now - sample[0]) / 10.0, (sample[1][2] - RANGE[0]) / (RANGE[1] - RANGE[0])) for sample in list(samples)] ts = [(t_now - sample[0]) / 10.0 for sample in samples] ys = [(sample[1][2] - RANGE[0]) / (RANGE[1] - RANGE[0]) for sample in samples] zs = [(sample[1][1] - RANGE[0]) / (RANGE[1] - RANGE[0]) - 0.5 for sample in samples] # ys = list(sp.smooth(sp.remove_shots(ys))) # zs = list(sp.smooth(sp.remove_shots(zs))) # ys = list(sp.remove_shots(ys)) # zs = list(sp.remove_shots(zs)) ys = (np.array(ys) * 2.0) - 0.5 zs = (np.array(zs) * 2.0) - 0.5 ys = sp.smooth(ys, 20) zs = sp.smooth(zs, 20) # combo_yz = [((t_now - sample[0]) / 10.0, (sample[1][2] - RANGE[0]) / (RANGE[1] - RANGE[0]), ((sample[1][1] - RANGE[0]) / (RANGE[1] - RANGE[0])) - 0.5) for sample in list(samples)] combo_yz = [(ts[i], ys[i], zs[i]) for i in range(0, len(ys))] ctx.lines3D(combo_yz, color=colors[s], thickness=2.0)
def optimal_gauss_kernel_size(train, optimize_steps, progress=None): """ Return the optimal kernel size for a spike density estimation of a spike train for a gaussian kernel. This function takes a single spike train, which can be a superposition of multiple spike trains (created with :func:`collapsed_spike_trains`) that should be included in a spike density estimation. Implements the algorithm from (Shimazaki, Shinomoto. Journal of Computational Neuroscience. 2010). :param train: The spike train for which the kernel size should be optimized. :type train: :class:`neo.core.SpikeTrain` :param optimize_steps: Array of kernel sizes to try (the best of these sizes will be returned). :type optimize_steps: Quantity 1D :param progress: Set this parameter to report progress. Will be advanced by len(`optimize_steps`) steps. :type progress: :class:`.progress_indicator.ProgressIndicator` :returns: Best of the given kernel sizes :rtype: Quantity scalar """ if not progress: progress = ProgressIndicator() x = train.rescale(optimize_steps.units) N = len(train) C = {} sampling_rate = 1024.0 / (x.t_stop - x.t_start) dt = float(1.0 / sampling_rate) y_hist = tools.bin_spike_trains({0: [x]}, sampling_rate)[0][0][0] y_hist = sp.asfarray(y_hist) / N / dt for step in optimize_steps: s = float(step) yh = sigproc.smooth(y_hist, sigproc.GaussianKernel(2 * step), sampling_rate, num_bins=2048, ensure_unit_area=True) * optimize_steps.units # Equation from Matlab code, 7/2012 c = (sp.sum(yh**2) * dt - 2 * sp.sum(yh * y_hist) * dt + 2 * 1 / sp.sqrt(2 * sp.pi) / s / N) C[s] = c * N * N progress.step() # Return kernel size with smallest cost return min(C, key=C.get) * optimize_steps.units
def optimal_gauss_kernel_size(train, optimize_steps, progress=None): """ Return the optimal kernel size for a spike density estimation of a spike train for a gaussian kernel. This function takes a single spike train, which can be a superposition of multiple spike trains (created with :func:`collapsed_spike_trains`) that should be included in a spike density estimation. Implements the algorithm from (Shimazaki, Shinomoto. Journal of Computational Neuroscience. 2010). :param train: The spike train for which the kernel size should be optimized. :type train: :class:`neo.core.SpikeTrain` :param optimize_steps: Array of kernel sizes to try (the best of these sizes will be returned). :type optimize_steps: Quantity 1D :param progress: Set this parameter to report progress. Will be advanced by len(`optimize_steps`) steps. :type progress: :class:`.progress_indicator.ProgressIndicator` :returns: Best of the given kernel sizes :rtype: Quantity scalar """ if not progress: progress = ProgressIndicator() x = train.rescale(optimize_steps.units) N = len(train) C = {} sampling_rate = 1024.0 / (x.t_stop - x.t_start) dt = float(1.0 / sampling_rate) y_hist = tools.bin_spike_trains({0: [x]}, sampling_rate)[0][0][0] y_hist = sp.asfarray(y_hist) / N / dt for step in optimize_steps: s = float(step) yh = sigproc.smooth( y_hist, sigproc.GaussianKernel(2 * step), sampling_rate, num_bins=2048, ensure_unit_area=True) * optimize_steps.units # Equation from Matlab code, 7/2012 c = (sp.sum(yh ** 2) * dt - 2 * sp.sum(yh * y_hist) * dt + 2 * 1 / sp.sqrt(2 * sp.pi) / s / N) C[s] = c * N * N progress.step() # Return kernel size with smallest cost return min(C, key=C.get) * optimize_steps.units
signals = [] labels = list(streams.keys()) log.info("LABELS %s" % labels) for label in labels: log.info(label) ts = tses[label] ts = [t_min] + ts + [t_max] values = [d[label] if label in d else None for d in streams[label]] values = [values[0]] + values + [values[-1]] values = sp.remove_shots(values, nones=True) # repair missing values signal = sp.resample(ts, values) num_samples = len(signal) sample_rate = num_samples / duration signal = sp.normalize(signal) signal = sp.smooth(signal, 15) signals.append(signal) log.info("Drawing...") ctx = drawing.Context(1200, 500, margin=20, hsv=True) for b in range(12): ctx.line(b / 12, 0, b / 12, 1, stroke=(0.5, 0.5, 0.5, 0.5), thickness=0.5) ctx.line(1, 0, 1, 1, stroke=(0.5, 0.5, 0.5, 0.5), thickness=0.5) for i, signal in enumerate(signals): color = i / (len(signals) + 4) + .1, 1., .8, 1. ctx.plot(signal, stroke=color, thickness=1.5) ctx.line(10 / ctx.width, 1 - ((10 + (i * 10)) / ctx.height), 30 / ctx.width, 1 - ((10 + (i * 10)) / ctx.height), stroke=color, thickness=2) ctx.label(35 / ctx.width, 1 - ((13 + (i * 10)) / ctx.height), labels[i].upper(), size=8) ctx.output("charts/")
def generate(): # load data into t and count arrays per species species = OrderedDict() start_t = util.timestamp(util.parse_date(str(config['start']))) end_t = util.timestamp(util.parse_date(str(config['end']))) max_count = 0 with open("data.csv") as f: data = csv.reader(f) for r, row in enumerate(data): if r == 0: continue plot = row[1] name = row[2] if len(config['species_list'] ) and name not in config['species_list']: continue dt = datetime.datetime(int(row[3]), 1, 1) + datetime.timedelta(int(row[4]) - 1) t = util.timestamp(dt) if t < start_t or t > end_t: continue count = 0 if row[5] == "NA" else int(row[5]) if count > max_count: max_count = count if name not in species: species[name] = {'ts': [start_t, t - 1], 'counts': [0, 0]} species[name]['ts'].append(t) species[name]['counts'].append(count) species = OrderedDict(sorted(species.items())) print("--> loaded") # add a zero count at the start and end of every year yts = [ util.timestamp(datetime.datetime(y, 1, 1)) for y in range(1974, 2017) ] for name in species: ts = species[name]['ts'] for yt in yts: i = 0 while i < len(ts) and ts[i] < yt: i += 1 if i > 0: end_season_t = ts[i - 1] if i < len(ts): start_season_t = ts[i] ts.insert(i, start_season_t - config['tail']) species[name]['counts'].insert(i, 0) ts.insert(i, end_season_t + config['tail']) species[name]['counts'].insert(i, 0) species[name]['ts'].append(end_t) species[name]['counts'].append(0) print("--> onsets added") # create and draw signals signals = [] names = [] i = 0 for name, data in species.items(): print("Processing %s..." % name) # create signal from bloom counts signal = sp.resample(data['ts'], data['counts']) if config['normalize']: signal = sp.normalize(signal) else: signal = sp.normalize(signal, 0, max_count) signal = sp.smooth(signal, size=8) signal = sp.limit( signal, max(signal)) # get rid of noise below 0 for onset detection # add spikes for peaks if config['peak_spikes']: peaks, valleys = sp.detect_peaks(signal, lookahead=50) peak_signal = np.zeros(len(signal)) for peak in peaks: peak_signal[peak[0]] = 1.0 signal += peak_signal # add spikes for onsets if config['onset_spikes']: onsets = sp.detect_onsets(signal) onset_signal = np.zeros(len(signal)) for onset in onsets: onset_signal[onset] = 0.5 onset_signal[onset + 1] = 0.4 onset_signal[onset + 2] = 0.25 signal += onset_signal # limit signal = sp.limit(signal, 1.0) signal *= 0.9 # hack, just controlling gain signals.append(signal) names.append(name) i += 1 return signals, names
def process_walk(walk_id, force=False): if not model.process_check(walk_id): log.error("Walk %s already processed" % walk_id) if force: log.info("--> forcing...") model.remove_sequence(walk_id) else: return log.info("Processing walk %s" % walk_id) # fetch data data = model.fetch_accels(walk_id) data = [(reading['t'], reading['x'], reading['y'], reading['z']) for reading in data] # let's sample every millisecond, so the time of the last reading is how many samples we need data = np.array(data) ts = data[:, 0] total_samples = int(ts[-1]) # need at least 10s of data # add 2000 for trimming at nd if total_samples < 10000 + 2000: log.info("No footsteps detected (too short)") model.hide(walk_id) return # resample the values xs = sp.resample(ts, data[:, 1], total_samples) ys = sp.resample(ts, data[:, 2], total_samples) zs = sp.resample(ts, data[:, 3], total_samples) # skip for accelerometer startup and for phone out of pocket at end skipin, skipout = 0, 2000 xs = xs[skipin:-skipout] ys = ys[skipin:-skipout] zs = zs[skipin:-skipout] total_samples -= (skipin + skipout) log.info("TOTAL SAMPLES %s (%fs)" % (total_samples, (total_samples / 1000.0))) # get 3d magnitude (not RMS) -- orientation shouldnt matter ds = np.sqrt(np.power(xs, 2) + np.power(ys, 2) + np.power(zs, 2)) # prep the raw values for display # normalize the values to a given range (this is Gs) MIN = -10.0 MAX = 10.0 xs = (xs - MIN) / (MAX - MIN) ys = (ys - MIN) / (MAX - MIN) zs = (zs - MIN) / (MAX - MIN) # smooth them xs = sp.smooth(xs, 300) ys = sp.smooth(ys, 300) zs = sp.smooth(zs, 300) # process the magnitude signal ds = sp.smooth(ds, 500) ds = np.clip(ds, -10.0, 10.0) # limit the signal to +-10 Gs ds = sp.normalize(ds) ds = 1 - ds ds = sp.compress(ds, 3.0) ds = sp.normalize(ds) # detect peaks peaks, valleys = sp.detect_peaks(ds, lookahead=50, delta=0.10) peaks = np.array(peaks) valleys = np.array(valleys) log.info("PEAKS %s" % len(peaks)) if not len(peaks): log.info("No footsteps detected") model.hide(walk_id) return # get foot separator line fxs = [int(peak[0]) for peak in peaks] fys = [peak[1] for peak in peaks] avs = np.average([peak[1] for peak in peaks]) fys[0] = avs # it's going to start with a peak, so we need to bring it up or down accordingly fxs.append(total_samples - 1) fys.append(avs) fs = sp.resample(fxs, fys, total_samples) fs = sp.smooth(fs, 3000) # print out log.info("Saving sequence (%s)..." % walk_id) sequence = [] for p, peak in enumerate(peaks): foot = 'right' if peak[1] > fs[int(peak[0])] else 'left' t = peak[0] t += 250 # turns out the peak hits just before the step sequence.append((t, foot)) # fix triples for i in range(len(sequence) - 2): if sequence[i][1] == sequence[i + 1][1] == sequence[i + 2][1]: sequence[i + 1] = (sequence[i + 1][0], 'right') if sequence[i + 1][1] == 'left' else ( sequence[i + 1][0], 'left') model.insert_sequence(walk_id, sequence) plot(walk_id, xs, ys, zs, ds, peaks, total_samples, fs)
def generate(): # load data into t and count arrays per species species = OrderedDict() start_t = util.timestamp(util.parse_date(str(config['start']))) end_t = util.timestamp(util.parse_date(str(config['end']))) max_count = 0 with open("data.csv") as f: data = csv.reader(f) for r, row in enumerate(data): if r == 0: continue plot = row[1] name = row[2] if len(config['species_list']) and name not in config['species_list']: continue dt = datetime.datetime(int(row[3]), 1, 1) + datetime.timedelta(int(row[4]) - 1) t = util.timestamp(dt) if t < start_t or t > end_t: continue count = 0 if row[5] == "NA" else int(row[5]) if count > max_count: max_count = count if name not in species: species[name] = {'ts': [start_t, t - 1], 'counts': [0, 0]} species[name]['ts'].append(t) species[name]['counts'].append(count) species = OrderedDict(sorted(species.items())) print("--> loaded") # add a zero count at the start and end of every year yts = [util.timestamp(datetime.datetime(y, 1, 1)) for y in range(1974, 2017)] for name in species: ts = species[name]['ts'] for yt in yts: i = 0 while i < len(ts) and ts[i] < yt: i += 1 if i > 0: end_season_t = ts[i-1] if i < len(ts): start_season_t = ts[i] ts.insert(i, start_season_t - config['tail']) species[name]['counts'].insert(i, 0) ts.insert(i, end_season_t + config['tail']) species[name]['counts'].insert(i, 0) species[name]['ts'].append(end_t) species[name]['counts'].append(0) print("--> onsets added") # create and draw signals signals = [] names = [] i = 0 for name, data in species.items(): print("Processing %s..." % name) # create signal from bloom counts signal = sp.resample(data['ts'], data['counts']) if config['normalize']: signal = sp.normalize(signal) else: signal = sp.normalize(signal, 0, max_count) signal = sp.smooth(signal, size=8) signal = sp.limit(signal, max(signal)) # get rid of noise below 0 for onset detection # add spikes for peaks if config['peak_spikes']: peaks, valleys = sp.detect_peaks(signal, lookahead=50) peak_signal = np.zeros(len(signal)) for peak in peaks: peak_signal[peak[0]] = 1.0 signal += peak_signal # add spikes for onsets if config['onset_spikes']: onsets = sp.detect_onsets(signal) onset_signal = np.zeros(len(signal)) for onset in onsets: onset_signal[onset] = 0.5 onset_signal[onset+1] = 0.4 onset_signal[onset+2] = 0.25 signal += onset_signal # limit signal = sp.limit(signal, 1.0) signal *= 0.9 # hack, just controlling gain signals.append(signal) names.append(name) i += 1 return signals, names
def get_speed_series(trajectories, slices): return [ np.insert(sigproc.smooth(get_trajectory_speed(trajectories[s, 0:2])), 0, 0) for s in slices ]
def draw(): t_now = util.timestamp(ms=True) # ctx.translate(0, 0, -1) # ctx.translate(-1.5, -0.5, -1) # ctx.translate(-1.3, -0.85, -2) ctx.translate(-1., -0.85, -1.5) ctx.rotate(*rotation_x) ctx.rotate(*rotation_y) # axes # ctx.line3D(-.25, 0, 0, .25, 0, 0, color=(1., 1., 0., 1.)) # ctx.line3D(0, -.25, 0, 0, .25, 0, color=(0., 1., 1., 1.)) # ctx.line3D(0, 0, -.25, 0, 0, .25, color=(1., 0., 1., 1.)) # for (start_t, stop_t) in sessions: # if stop_t is None: # stop_t = t_now # if t_now - stop_t > 10.0: # continue # # ctx.line((t_now - stop_t) / 10.0, .99, (t_now - start_t) / 10.0, .99, color=(1., 0., 0., .2), thickness=10.0) # x1 = (t_now - stop_t) / 10.0 # x2 = (t_now - start_t) / 10.0 # ctx.rect(x1, 0.0, x2 - x1, 1.0, color=(1., 0., 0., 0.25)) # for s, (sensor, (t, rssi)) in enumerate(sensor_rssi.items()): # if t_now - t > 3: # bar = 0.01 # else: # bar = 1.0 - (max(abs(rssi) - 25, 0) / 100) # x = (20 + (s * 20)) / ctx.width # ctx.line(x, .1, x, (bar * 0.9) + .1, color=(0., 0., 0., 0.5), thickness=10) # if sensor not in labels: # print("Adding label for sensor %s" % sensor) # labels.append(sensor) # ctx.label(x, .05, str(sensor), font="Monaco", size=10, width=10, center=True) colors = (1., 1., 1., 1.), (.7, 1., 1., 1.), (1., .7, .7, 1.), for s, sensor in enumerate(list(sensor_data)): samples = sensor_data[sensor] if len(samples): # x = [((t_now - sample[0]) / 10.0, (sample[1][0] - RANGE[0]) / (RANGE[1] - RANGE[0])) for sample in list(samples)] # y = [((t_now - sample[0]) / 10.0, (sample[1][1] - RANGE[0]) / (RANGE[1] - RANGE[0])) for sample in list(samples)] # z = [((t_now - sample[0]) / 10.0, (sample[1][2] - RANGE[0]) / (RANGE[1] - RANGE[0])) for sample in list(samples)] ts = [(t_now - sample[0]) / 10.0 for sample in samples] ys = [(sample[1][2] - RANGE[0]) / (RANGE[1] - RANGE[0]) for sample in samples] zs = [(sample[1][1] - RANGE[0]) / (RANGE[1] - RANGE[0]) - 0.5 for sample in samples] # ys = list(sp.smooth(sp.remove_shots(ys))) # zs = list(sp.smooth(sp.remove_shots(zs))) # ys = list(sp.remove_shots(ys)) # zs = list(sp.remove_shots(zs)) ys = (np.array(ys) * 2.0) - 0.5 zs = (np.array(zs) * 2.0) - 0.5 ys = sp.smooth(ys, 20) zs = sp.smooth(zs, 20) # combo_yz = [((t_now - sample[0]) / 10.0, (sample[1][2] - RANGE[0]) / (RANGE[1] - RANGE[0]), ((sample[1][1] - RANGE[0]) / (RANGE[1] - RANGE[0])) - 0.5) for sample in list(samples)] combo_yz = [(ts[i], ys[i], zs[i]) for i in range(0, len(ys))] ctx.lines3D(combo_yz, color=colors[s], thickness=2.0)
def process_walk(walk_id, force=False): if not model.process_check(walk_id): log.error("Walk %s already processed" % walk_id) if force: log.info("--> forcing...") model.remove_sequence(walk_id) else: return log.info("Processing walk %s" % walk_id) # fetch data data = model.fetch_accels(walk_id) data = [(reading['t'], reading['x'], reading['y'], reading['z']) for reading in data] # let's sample every millisecond, so the time of the last reading is how many samples we need data = np.array(data) ts = data[:,0] total_samples = int(ts[-1]) # need at least 10s of data # add 2000 for trimming at nd if total_samples < 10000 + 2000: log.info("No footsteps detected (too short)") model.hide(walk_id) return # resample the values xs = sp.resample(ts, data[:,1], total_samples) ys = sp.resample(ts, data[:,2], total_samples) zs = sp.resample(ts, data[:,3], total_samples) # skip for accelerometer startup and for phone out of pocket at end skipin, skipout = 0, 2000 xs = xs[skipin:-skipout] ys = ys[skipin:-skipout] zs = zs[skipin:-skipout] total_samples -= (skipin + skipout) log.info("TOTAL SAMPLES %s (%fs)" % (total_samples, (total_samples / 1000.0))) # get 3d magnitude (not RMS) -- orientation shouldnt matter ds = np.sqrt(np.power(xs, 2) + np.power(ys, 2) + np.power(zs, 2)) # prep the raw values for display # normalize the values to a given range (this is Gs) MIN = -10.0 MAX = 10.0 xs = (xs - MIN) / (MAX - MIN) ys = (ys - MIN) / (MAX - MIN) zs = (zs - MIN) / (MAX - MIN) # smooth them xs = sp.smooth(xs, 300) ys = sp.smooth(ys, 300) zs = sp.smooth(zs, 300) # process the magnitude signal ds = sp.smooth(ds, 500) ds = np.clip(ds, -10.0, 10.0) # limit the signal to +-10 Gs ds = sp.normalize(ds) ds = 1 - ds ds = sp.compress(ds, 3.0) ds = sp.normalize(ds) # detect peaks peaks, valleys = sp.detect_peaks(ds, lookahead=50, delta=0.10) peaks = np.array(peaks) valleys = np.array(valleys) log.info("PEAKS %s" % len(peaks)) if not len(peaks): log.info("No footsteps detected") model.hide(walk_id) return # get foot separator line fxs = [int(peak[0]) for peak in peaks] fys = [peak[1] for peak in peaks] avs = np.average([peak[1] for peak in peaks]) fys[0] = avs # it's going to start with a peak, so we need to bring it up or down accordingly fxs.append(total_samples-1) fys.append(avs) fs = sp.resample(fxs, fys, total_samples) fs = sp.smooth(fs, 3000) # print out log.info("Saving sequence (%s)..." % walk_id) sequence = [] for p, peak in enumerate(peaks): foot = 'right' if peak[1] > fs[int(peak[0])] else 'left' t = peak[0] t += 250 # turns out the peak hits just before the step sequence.append((t, foot)) # fix triples for i in range(len(sequence) - 2): if sequence[i][1] == sequence[i+1][1] == sequence[i+2][1]: sequence[i+1] = (sequence[i+1][0], 'right') if sequence[i+1][1] == 'left' else (sequence[i+1][0], 'left') model.insert_sequence(walk_id, sequence) plot(walk_id, xs, ys, zs, ds, peaks, total_samples, fs)
def sample(draw=False): log.info("START SAMPLE") # get the time # dt = timeutil.get_dt(tz=config['tz']) # dt -= datetime.timedelta(days=300) # time adjustment if necessary for testing # t_utc = timeutil.t_utc(dt) t_utc = timeutil.t_utc() dt = timeutil.get_dt(t_utc, tz=config['tz']) log.info("CURRENT TIME %s" % timeutil.get_string(t_utc, tz=config['tz'])) # pull the last 24 hours worth -- we're going to normalize over that to set our dynamic levels log.info(config['sites'][config['sample']]) # # this is the real-time last 24 hours # query = {'site': config['sample'], 't_utc': {'$gt': t_utc - 86400, '$lt': t_utc}} # log.info(query) # results = db.entries.find(query) # this is the last 24 hours we have # assume updating every 15 minutes, last 24 hours is the last 96 results results = db.entries.find({ 'site': config['sample'] }).sort([('t_utc', DESCENDING)]).limit(96) results = list(results) results.reverse() log.info("%s results" % len(results)) # should be 96 log.info(json.dumps(results[-1], indent=4, default=lambda d: str(d))) # show the last one # resample signals for each ts = [d['t_utc'] for d in results] duration = ts[-1] - ts[0] log.info("DURATION %s %s" % (duration, timeutil.format_seconds(duration))) signals = [] rates = [] labels = list(config['labels'].values()) labels.sort() for i, label in enumerate(labels): # log.debug(label) try: values = [d[label] if label in d else None for d in results] values = sp.remove_shots(values, nones=True) # repair missing values signal = sp.resample(ts, values) num_samples = len(signal) sample_rate = num_samples / duration rates.append(sample_rate) signal = sp.normalize(signal) signal = sp.smooth(signal, 15) signals.append(signal) except KeyError as e: log.error(log.exc(e)) log.error(values) # draw if desired if draw: from housepy import drawing ctx = drawing.Context(1200, 500, margin=20, hsv=True) for i, label in enumerate(labels): color = i / len(labels), .8, .8, 1. signal = signals[i] ctx.plot(signal, stroke=color, thickness=2) ctx.output("charts/") # collapse into n-dimensional points points = [] for i in range(len(signals[0])): point = [signal[i] for signal in signals] points.append(point) # PCA to 4D -- this takes whatever data we've got and maximizes variation for our four panels points = np.array(points) # log.debug("INPUT: %s POINTS, %s DIMENSIONS" % points.shape) points = decomposition.PCA(n_components=4).fit_transform(points) # log.debug("OUTPUT: %s POINTS, %s DIMENSIONS" % points.shape) # normalize each dimension independently, again amplifying dynamics points = np.column_stack((sp.normalize(points[:, 0], np.min(points[:, 0]), np.max(points[:, 0])), sp.normalize(points[:, 1], np.min(points[:, 1]), np.max(points[:, 1])), sp.normalize(points[:, 1], np.min(points[:, 1]), np.max(points[:, 2])), sp.normalize(points[:, 2], np.min(points[:, 3]), np.max(points[:, 3])))) # now, for each time this is queried we want to return an interpolation between the last two points # this essentially implements a delay that closes in on the most recent query # ...hopefully to be refreshed with a new USGS reading when it gets there # if that reading doesnt come, it's ok, it just hovers there until we proceed # aaandd actually we want a couple of hours delay, because these come in at bulk every 1-4 hours # we know we have 96 points. four hours back is 16 points # interpolating between points -17 and -16 should give the most recent guaranteed smooth transitions # transduction takes time, pues point_a = points[-17] point_b = points[-16] # log.debug(point_a) # log.debug(point_b) # linear interpolation over 15 minutes position = (((dt.minute % 15) * 60) + dt.second) / (15 * 60) # log.debug(position) point = [(point_a[i] * (1.0 - position)) + (point_b[i] * position) for i in range(len(point_a))] log.info("RESULT: %s" % point) return point
print("DURATION %s %s" % (duration, strings.format_time(duration))) signals = [] rates = [] labels = list(config['labels'].values()) labels.sort() for i, label in enumerate(labels): log.info(label) try: values = [d[label] if label in d else None for d in results] values = sp.remove_shots(values, nones=True) # repair missing values signal = sp.resample(ts, values) num_samples = len(signal) sample_rate = num_samples / duration rates.append(sample_rate) signal = sp.normalize(signal) signal = sp.smooth(signal, 15) signals.append(signal) # color = colors[i] color = i / len(labels), .8, .8, 1. ctx.plot(signal, stroke=color, thickness=2) ctx.line(10 / ctx.width, 1 - ((10 + (i * 10)) / ctx.height), 30 / ctx.width, 1 - ((10 + (i * 10)) / ctx.height), stroke=color, thickness=2) ctx.label(35 / ctx.width, 1 - ((13 + (i * 10)) / ctx.height), label.upper(), size=8) except KeyError as e: log.error(log.exc(e)) log.error(values) ctx.output("charts/") points = [] for i in range(len(signals[0])): point = [signal[i] for signal in signals]
def main(session_id): result = db.branches.find({'session': session_id}).sort([('t', ASCENDING)]) if not result.count(): print("NO DATA!") exit() log.info("Start processing...") result = list(result) ts = [r['t'] for r in result] rms = [r['sample'][3] for r in result] duration = ts[-1] - ts[0] SAMPLING_RATE = 60 # hz log.info("DURATION %fs" % duration) signal = sp.resample(ts, rms, duration * SAMPLING_RATE) signal = sp.remove_shots(signal) signal = sp.normalize(signal) signal = sp.smooth(signal, 15) # this number should match some lower frequency bound. ie, put this in hz. # the smaller the number, the more it will affect small motion # so this should be higher than the slowest motion we care about # ie, dont care about motion over 0.5hz, which is 120 samples trend = sp.smooth(signal, 120) signal -= trend signal += 0.5 atrend = sp.smooth(signal, 500) ## autocorrelation auto = sp.autocorrelate(signal) # this should be small -- if 60hz, fastest gesture would reasonably be half of that, so 30 peaks, valleys = sp.detect_peaks(auto, 10) peaks = [peak for peak in peaks[1:] if peak[1] > 0.5] partials = [] for peak in peaks: frequency = SAMPLING_RATE / peak[0] partial = frequency * 1000 partials.append([partial, float(peak[1])]) log.info("%d samps\t%fhz\t%f magnitude\t%f map" % (peak[0], frequency, peak[1], partial)) log.info(partials) ctx = drawing.Context(2000, 750) ctx.plot(auto, stroke=(0.0, 0.0, 0.0, 1.0), thickness=2.0) for peak in peaks: x = peak[0] / len(auto) ctx.line(x, 0.0, x, peak[1], stroke=(1.0, 0.0, 0.0, 1.0)) ctx.output("graphs") ## audio audio_signal = sp.make_audio(signal) spectrum(audio_signal, SAMPLING_RATE) AUDIO_RATE = 11025 filename = "%s.wav" % util.timestamp() sound.write_audio(audio_signal, filename, AUDIO_RATE) subprocess.call(["open", filename]) log.info("AUDIO DURATION %fs" % (duration / (AUDIO_RATE / SAMPLING_RATE))) ctx = drawing.Context(2000, 750) ctx.plot(signal, stroke=(0.0, 0.0, 0.0, 1.0), thickness=2.0) ctx.plot(trend, stroke=(1.0, 0.0, 0.0, 1.0), thickness=2.0) ctx.plot(atrend, stroke=(0.0, 0.0, 1.0, 1.0), thickness=2.0) ctx.output("graphs") log.info("--> done") # around 300ms
log.info("Processing...") total_time = ts[-1] - ts[0] total_samples = int(total_time / 60) # once per minute log.debug("last_t %s" % ts[-1]) log.debug("total_time %s" % total_time) log.debug("total_time_f %s" % strings.format_time(total_time)) log.debug("total_samples %s" % total_samples) sample_length = total_time / total_samples log.debug("sample_length %s" % sample_length) signal = sp.resample(ts, hrs, total_samples) signal = sp.normalize(signal) signal = signal - sp.smooth(signal, size=100) # flatten it out a bit threshold = np.average(signal) + (2 * np.std(signal)) # threshold is average plus 2 std deviation smoothed_signal = sp.smooth(signal, size=10) peaks, valleys = sp.detect_peaks(smoothed_signal, lookahead=10, delta=.001) max_peak = max(peaks, key=lambda p: p[1]) log.info("max_peak %s" % max_peak) peaks = [peak for peak in peaks if peak[1] > threshold] def draw(): from housepy import drawing log.info("--> done") log.info("Plotting...") ctx = drawing.Context() ctx.plot(signal) ctx.plot(smoothed_signal, stroke=(100, 0, 0)) for peak in peaks:
def process_walk(walk_id, force=False): if not model.process_check(walk_id): log.error("Walk %s already processed" % walk_id) if force: log.info("--> forcing...") model.remove_sequence(walk_id) else: return log.info("Processing walk %s" % walk_id) data = model.fetch_accels(walk_id) data = [(reading['t'], reading['x'], reading['y'], reading['z']) for reading in data] # log.debug(data) # let's sample every millisecond, so the time of the last reading is how many samples we need data = np.array(data) # log.debug(data) ts = data[:,0] total_samples = ts[-1] log.info("TOTAL SAMPLES %s (%fs)" % (total_samples, (total_samples / 1000.0))) # resample the values xs = sp.resample(ts, data[:,1], total_samples) ys = sp.resample(ts, data[:,2], total_samples) zs = sp.resample(ts, data[:,3], total_samples) # skip 0.5s for intro skip = 500 xs = xs[skip:] ys = ys[skip:] zs = zs[skip:] total_samples -= skip # # for testing # log.debug(total_samples) # xs = xs[(30 * 1000):(50 * 1000)] # ys = ys[(30 * 1000):(50 * 1000)] # zs = zs[(30 * 1000):(50 * 1000)] # total_samples = len(xs) # log.debug(total_samples) # get 3d vector ds = np.sqrt(np.power(xs, 2) + np.power(ys, 2) + np.power(zs, 2)) # normalize the values to a given range (this is gs, I believe) MIN = -20.0 MAX = 20.0 xs = (xs - MIN) / (MAX - MIN) ys = (ys - MIN) / (MAX - MIN) zs = (zs - MIN) / (MAX - MIN) ds = (ds - MIN) / (MAX - MIN) # low-pass filter ds = sp.smooth(ds, 300) # ds = sp.normalize(ds) # av = np.average(ds) # detect peaks # lookahead should be the minimum time of a step, maybe .3s, 300ms peaks, valleys = sp.detect_peaks(ds, lookahead=150, delta=0.10) if len(peaks) and peaks[0][0] == 0: peaks = peaks[1:] peaks = np.array(peaks) valleys = np.array(valleys) log.info("PEAKS %s" % len(peaks)) log.info("VALLEYS %s" % len(valleys)) if not (len(peaks) and len(valleys)): log.info("No footsteps detected") return peaks = valleys # start = np.min((np.min(peaks[:,0]), np.min(valleys[:,0]))) start = np.min(peaks[:,0]) log.debug("START %s" % start) xs = xs[start:] ys = ys[start:] zs = zs[start:] ds = ds[start:] peaks = [(peak[0] - start, peak[1]) for peak in peaks] valleys = [(valley[0] - start, valley[1]) for valley in valleys] total_samples -= start # get foot separator line fxs = [peak[0] for peak in peaks] fys = [peak[1] for peak in peaks] avs = np.average([peak[1] for peak in peaks]) fxs.append(total_samples-1) fys.append(avs) fs = sp.resample(fxs, fys, total_samples) fs = sp.smooth(fs, 3000) # print out log.info("Saving sequence (%s)..." % walk_id) sequence = [] for p, peak in enumerate(peaks): foot = 'right' if peak[1] > fs[peak[0]] else 'left' sequence.append((peak[0], foot)) model.insert_sequence(walk_id, sequence) plot(walk_id, xs, ys, zs, ds, peaks, valleys, total_samples, fs)