def _show_custom_ui(self): width = 250 height = 100 draw_list = imgui.get_window_draw_list() plot_start = imgui.get_cursor_screen_pos() imgui.push_style_color(imgui.COLOR_PLOT_LINES, 0.8, 0.8, 0.8, 1.0) imgui.plot_lines("", self._beat_value_buffer.contents * self.get("gain"), 0.0, 1.0, (width, height)) imgui.pop_style_color() plot_size = imgui.get_item_rect_size() beat_risings = self._beat_rising_buffer.contents count = self._beat_rising_buffer.size for i, beat_rising in enumerate(beat_risings): if not beat_rising: continue x = i / (count - 1) * width line_start = plot_start[0] + x, plot_start[1] line_end = plot_start[0] + x, plot_start[1] + height draw_list.add_line(line_start, line_end, imgui.get_color_u32_rgba(0.0, 0.8, 0.0, 0.8)) threshold = min(1.0, max(0.0, self.get("threshold"))) threshold_start = plot_start[0], plot_start[1] + (1.0 - threshold) * height threshold_end = plot_start[0] + width, threshold_start[1] draw_list.add_line(threshold_start, threshold_end, imgui.get_color_u32_rgba(0.8, 0.0, 0.0, 0.8))
def update(self, data): if data.any(): if self.photo is None: # start new thing self.photo = data.photo self.l_button = data.buttons[:, 0] self.r_button = data.buttons[:, 1] self.l_force = data.forces[:, 0] self.r_force = data.forces[:, 1] elif max(self.photo.shape) < 1000: self.photo = np.hstack((self.photo, data.photo)) self.l_button = np.hstack((self.l_button, data.buttons[:, 0])) self.r_button = np.hstack((self.r_button, data.buttons[:, 1])) self.l_force = np.hstack((self.l_force, data.forces[:, 0])) self.r_force = np.hstack((self.r_force, data.forces[:, 1])) else: shp = -data.photo.shape[0] self.photo = np.roll(self.photo, shp) self.l_button = np.roll(self.l_button, shp) self.r_button = np.roll(self.r_button, shp) self.l_force = np.roll(self.l_force, shp) self.r_force = np.roll(self.r_force, shp) self.photo[shp:] = data.photo self.l_button[shp:] = data.buttons[:, 0] self.r_button[shp:] = data.buttons[:, 1] self.l_force[shp:] = data.forces[:, 0] self.r_force[shp:] = data.forces[:, 1] imgui.set_next_window_size(270, 600) imgui.begin('plot', False, self.flags) imgui.plot_lines('Photo', self.photo.astype('f') / 2**10, scale_min=0, scale_max=1, graph_size=(180, 100)) imgui.plot_lines('l_force', self.l_force.astype('f') / 2**12, scale_min=0, scale_max=1, graph_size=(180, 100)) imgui.plot_lines('r_force', self.r_force.astype('f') / 2**12, scale_min=0, scale_max=1, graph_size=(180, 100)) style = imgui.get_style() imgui.plot_lines('l_button', smooth(self.l_button.astype('f')) * 6, scale_min=-1.1, scale_max=1.1, graph_size=(180, 100)) imgui.plot_lines('r_button', smooth(self.r_button.astype('f')) * 6, scale_min=-1.1, scale_max=1.1, graph_size=(180, 100)) imgui.end()
def draw(self): imgui.begin("Scope") self.begin_input(self.input) imgui.button('input') self.end_input() imgui.same_line(spacing=16) imgui.plot_lines("Sin(t)", np.array(self.values).astype(np.float32), graph_size=imgui.get_content_region_available()) imgui.end()
def draw(self): #imgui.set_next_window_position(self.window.width - 256 - 16, 32, imgui.ONCE) #imgui.set_next_window_size(256, 256, imgui.ONCE) imgui.begin("Meter") self.mark_input(self.input) imgui.text('input') imgui.same_line(spacing=16) imgui.plot_lines("Sin(t)", self.values) imgui.end()
def _show_custom_ui(self): if self.have_inputs_changed("time"): t = self.get("time") count = int(self.get("time") * 60) if count <= 0: count = 1 self.buffer = analyzer.RingBuffer(count) width = int(self.get("width")) height = int(self.get("height")) imgui.plot_lines("", self.buffer.contents, float(self.get("min")), float(self.get("max")), (width, height))
def main(): window = impl_glfw_init() imgui.create_context() impl = GlfwRenderer(window) plot_values = array('f', [sin(x * C) for x in range(L)]) histogram_values = array('f', [random() for _ in range(20)]) while not glfw.window_should_close(window): glfw.poll_events() impl.process_inputs() imgui.new_frame() imgui.begin("Plot example") imgui.plot_lines( "Sin(t)", plot_values, overlay_text="SIN() over time", # offset by one item every milisecond, plot values # buffer its end wraps around values_offset=int(time() * 100) % L, # 0=autoscale => (0, 50) = (autoscale width, 50px height) graph_size=(0, 50), ) imgui.plot_histogram( "histogram(random())", histogram_values, overlay_text="random histogram", # offset by one item every milisecond, plot values # buffer its end wraps around graph_size=(0, 50), ) imgui.end() gl.glClearColor(1., 1., 1., 1) gl.glClear(gl.GL_COLOR_BUFFER_BIT) imgui.render() impl.render(imgui.get_draw_data()) glfw.swap_buffers(window) impl.shutdown() glfw.terminate()
def _show_custom_ui(self): if self._fft is None: imgui.text("No fft data!") return fft = self._fft width = int(self.get("width")) height = int(self.get("height")) min_freq = self.get("min_freq") max_freq = self.get("max_freq") freq_mask = (fft.frequencies >= min_freq) & (fft.frequencies < max_freq) frequencies = fft.frequencies[freq_mask] magnitudes = fft.magnitudes[freq_mask] imgui.plot_lines("", magnitudes * self.get("scale") + self.get("offset"), 0.0, 1.0, (width, height)) imgui.text("%d FFT bins: %0.2fHz - %0.2fHz, resolution: %0.2fHz" % (len(frequencies), frequencies[0], frequencies[-1], fft.bin_resolution))
def displayInterface(self): imgui.begin_child("left_bottom", width=606, height=370) imgui.text("Network Traffic") imgui.separator() imgui.spacing() plot_rx = array('f') for byte in self.rx_bytes: plot_rx.append(byte) plot_tx = array('f') for byte in self.tx_bytes: plot_tx.append(byte) imgui.text("Download Traffic (MB) | Avg: " + str(round(sum(self.rx_bytes) / len(self.rx_bytes), 5)) + " mb | Max: " + str(round(max(self.rx_bytes), 5)) + " mb") imgui.plot_lines("##Rx Traffic (MB)", plot_rx, graph_size=(606, 150)) imgui.spacing() imgui.text("Upload Traffic (MB) | Avg: " + str(round(sum(self.tx_bytes) / len(self.tx_bytes), 5)) + " mb | Max: " + str(round(max(self.tx_bytes), 5)) + " mb") imgui.plot_lines("##Tx Traffic (MB)", plot_tx, graph_size=(606, 150)) imgui.end_child() imgui.same_line() imgui.begin_child("net_traf_alerts") imgui.text("Network Traffic Alerts") imgui.begin_child("net_traf_alerts_logger", border=True) for message in self.alerts: imgui.text_wrapped(message) imgui.end_child() imgui.end_child()
def show_imgui_plot( plot_box_state: PlotState, signal: Signal, width: int = -1, height: int = -1, ui_settings={}, ) -> IMGui[None]: debug_log_time(SIGNAL_PLOT_CALL_START) time_range = plot_box_state.plot_state.time_range if width == -1: width = int(im.get_content_region_available().x) if height == -1: height = int(im.get_content_region_available().y) debug_log_time(DATA_GET_START) data_part = slice_signal( signal, time_range, n_points_needed=width, variant=ui_settings['plot_resample_function']).astype( np.dtype('float32')) debug_log_time(DATA_GET_END) debug_log_time(WAVE_DRAW_START) im.plot_lines( "the_actual_plot##{}".format(plot_box_state.id_), values=data_part, graph_size=(width, height), scale_min=signal.physical_min, scale_max=signal.physical_max, ) debug_log_time(WAVE_DRAW_END) debug_log_time(SIGNAL_PLOT_CALL_END)
def render_controllearn(self): _, self.learn_controls = imgui.begin("control system tuning", True) if not self.learn_controls: imgui.end() return n = max(self.i + 1, 10) # use history from 0..i to learn motor model # dv/dt = k1*DC*V + k2*DC*v + k3*v XTX = np.eye(2) XTY = np.array([5., 1]) v = self.controlstate[1:n, 3].copy() dv = v.copy() dv[1:] = dv[1:] - dv[:-1] u = self.controls[:n - 1, 0] DC = np.abs(u / 127.0) V = (1 + np.sign(u)) / 2 dt = 1.0 / 30 # FIXME X = np.vstack([DC * V, -DC * v]) Y = dv / dt XTX += np.dot(X, X.T) XTY += np.dot(X, Y.T) ks = np.linalg.lstsq(XTX, XTY, rcond=None)[0] imgui.slider_float("motor k1", ks[0], 0, 10) imgui.slider_float("motor k2", ks[1], 0, 2) imgui.plot_lines("dv/dt", dv / dt) imgui.plot_lines("DC*V", DC * V) imgui.plot_lines("v", v) imgui.plot_lines("step response", motor_step_response([ks[0], ks[1], 0], 120)) # let's also solve for steering ratios XTX = np.eye(2) XTY = np.array([1., 0]) w = self.controlstate[1:n, 4].copy() u = self.controls[:n - 1, 1] / 127.0 X = np.vstack([u * v, v]) XTX += np.dot(X, X.T) XTY += np.dot(X, w.T) ks = np.linalg.lstsq(XTX, XTY, rcond=None)[0] imgui.slider_float("servo ratio", ks[0], -2, 2) imgui.slider_float("servo bias", ks[1], -1, 1) imgui.end()
def render_timeline(self): imgui.begin("timeline") tstamp = self.frame['tstamp'] if imgui.button("<"): self.playing = False if self.i > 0: self.loadframe(self.i - 1) imgui.same_line() if self.playing: if (self.i == self.scanner.num_frames() - 1) or imgui.button("stop"): self.playing = False elif time.time() >= self.ts[self.i + 1] - self.t0: self.nextframe() elif imgui.button("play"): self.playing = True self.t0 = tstamp - time.time() imgui.same_line() if imgui.button(">"): self.playing = False self.nextframe() tsfrac = tstamp - int(tstamp) tstring = time.strftime( "%H:%M:%S.", time.localtime(tstamp)) + "%02d" % (tsfrac * 100) imgui.same_line() imgui.text(tstring) w = imgui.get_window_width() if self.show_frontview: imgui.image(self.fronttexid, w, w / 2) # 2:1 aspect for front view else: # 4:3 aspect imgui.image(self.frametexid, w, 3 * w / 4) if self.acts is not None: imgui.plot_lines("activations", self.acts) nCtrlAngles = (len(self.controlstate[self.i]) - 14) // 3 cc = self.controlstate[self.i][-nCtrlAngles:] imgui.plot_lines("control costs", np.clip(cc, 0, 100)) # make a histogram of expected cone locations if self.acts is not None: hist = np.zeros(len(self.acts) * 2, np.float32) np.add.at(hist, self.frame['c0'], 1) np.add.at(hist, self.frame['c1'], -1) hist = np.cumsum(hist) hist = hist[:len(self.acts)] + hist[-len(self.acts):] imgui.plot_lines("expected cone locations", hist) changed, i = imgui.slider_int("frame", self.i, 0, self.scanner.num_frames() - 1) if changed: self.playing = False self.loadframe(i) imgui.end()
def render_graphs(self): imgui.begin("graphs") i = self.i dl = imgui.get_window_draw_list() mi = max(0, i - 30 * 3) temp = self.controlstate[mi:i + 1, 3].copy() imgui.plot_lines("velocity", temp) temp = self.controlstate[mi:i + 1, 8].copy() imgui.plot_lines("target v", temp) temp = self.controls[mi:i + 1, 0].copy() imgui.plot_lines("control v", temp) temp = self.controls[mi:i + 1, 1].copy() imgui.plot_lines("control steer", temp) temp = self.controlstate[mi:i + 1, 9].copy() imgui.plot_lines("target w", temp) temp = self.controlstate[mi:i + 1, 4].copy() imgui.plot_lines("yaw rate", temp) # live variables maxv = int(np.ceil(np.max(self.controlstate[:, 3]) * 1.1)) imgui.slider_float("wheel v", self.controlstate[i, 3], 0, maxv) imgui.slider_float("target v", self.controlstate[i, 8], 0, maxv) lv = 0 if i > 0: dx = self.controlstate[i, 0] - self.controlstate[i - 1, 0] dy = self.controlstate[i, 1] - self.controlstate[i - 1, 1] lv = np.sqrt(dx**2 + dy**2) * 30.0 imgui.slider_float("localized v", lv, 0, maxv) imgui.slider_float("control motor", self.controls[i, 0] / 127., -1, 1) imgui.slider_float("control steer", self.controls[i, 1] / 127., -1, 1) # for yaw rate and curvature, set the limits backwards # so that turning right is to the right maxw = int(np.ceil(np.max(np.abs(self.controlstate[:, 4])) * 1.1)) imgui.slider_float("yaw rate", self.controlstate[i, 4], maxw, -maxw) imgui.slider_float("target w", self.controlstate[i, 9], maxw, -maxw) v = self.controlstate[i, 3] if v > 0.5: k = self.controlstate[i, 4] / v else: k = 0 imgui.slider_float("curvature", k, 2, -2) targetK = self.controlstate[self.i, 7] imgui.slider_float("target k", targetK, 2, -2) # render overview pos = imgui.get_cursor_screen_pos() siz = imgui.get_content_region_available() if siz[1] == 0: siz = [400, 300] imgui.invisible_button("overview", siz[0], siz[0] * 0.7) origin = pos meterwidth = max(np.max(self.track[:, 0]), np.max(self.lm[:, 0])) scale = siz[0] / (meterwidth * 1.1) conecolor = imgui.get_color_u32_rgba(1, 0.7, 0, 1) for lm in self.lm: dl.add_rect_filled(origin[0] + lm[0] * scale - 2, origin[1] - lm[1] * scale - 2, origin[0] + lm[0] * scale + 2, origin[1] - lm[1] * scale + 2, conecolor, 3) trackcolor = imgui.get_color_u32_rgba(1, 1, 0.3, 0.5) for i in range(0, len(self.track), 2): j = (i + 1) % len(self.track) ti = self.track[i] * scale tj = self.track[j] * scale dl.add_line(origin[0] + ti[0], origin[1] - ti[1], origin[0] + tj[0], origin[1] - tj[1], trackcolor, 1.5) particlecolor = imgui.get_color_u32_rgba(1, 1, 1, 0.3) dl.add_rect(pos[0], pos[1], pos[0] + siz[0], pos[1] + siz[1], particlecolor, 1) if 'particles' in self.frame: ps = self.frame['particles'] for p in ps: dl.add_rect_filled(origin[0] + p[0] * scale, origin[1] - p[1] * scale, origin[0] + p[0] * scale + 1, origin[1] - p[1] * scale + 1, particlecolor) # also draw a mean velocity vector mxy = scale * np.mean(ps[:, :2], axis=0) vxy = scale * v * .1 * np.array( [np.mean(np.cos(ps[:, 3])), np.mean(np.sin(ps[:, 3]))]) dl.add_line(origin[0] + mxy[0], origin[1] - mxy[1], origin[0] + mxy[0] + vxy[0], origin[1] - mxy[1] - vxy[1], imgui.get_color_u32_rgba(0, 1, 0, 1), 1) else: x = self.controlstate[self.i, 0] y = self.controlstate[self.i, 1] theta = self.controlstate[self.i, 2] imgui.slider_float("x", x, 0, 20) imgui.slider_float("y", y, -10, 0) imgui.slider_float("theta", theta % (2 * np.pi), -np.pi, np.pi) v = 0.3 * self.controlstate[self.i, 3] S, C = np.sin(theta), np.cos(theta) dl.add_rect_filled(origin[0] + x * scale - 3, origin[1] - y * scale - 3, origin[0] + scale * x + 3, origin[1] - scale * y + 3, imgui.get_color_u32_rgba(0, 1, 0, 1), 1) dl.add_line(origin[0] + x * scale, origin[1] - y * scale, origin[0] + scale * (x + v * C), origin[1] - scale * (y + v * S), imgui.get_color_u32_rgba(0, 1, 0, 1), 1) targetaccel = self.controlstate[self.i][12:14] / 9.8 accel = self.carstate[self.i][2] ox, oy = origin[0] + scale * 3, origin[1] + scale * 9 for i in range(100): t0 = i * 2 * np.pi / 100 t1 = (i + 1) * 2 * np.pi / 100 dl.add_line(ox + 2 * scale * np.cos(t0), oy - 2 * scale * np.sin(t0), ox + 2 * scale * np.cos(t1), oy - 2 * scale * np.sin(t1), imgui.get_color_u32_rgba(0.7, 0.7, 0.7, 1), 1) dl.add_line(ox + scale * np.cos(t0), oy - scale * np.sin(t0), ox + scale * np.cos(t1), oy - scale * np.sin(t1), imgui.get_color_u32_rgba(0.7, 0.7, 0.7, 1), 1) dl.add_line(ox, oy, ox + scale * accel[1], oy - scale * accel[0], imgui.get_color_u32_rgba(0.3, 1, 0.5, 1), 3) dl.add_rect(ox - scale * targetaccel[1] - 2, oy + scale * targetaccel[0] - 2, ox - scale * targetaccel[1] + 2, oy + scale * targetaccel[0] + 2, imgui.get_color_u32_rgba(0.0, 1, 1, 1), 1) imgui.end()
def draw(self): imgui.begin(self.title) imgui.plot_lines("Sin(t)", self.values) imgui.end()
def draw_plots(self, time, size): """Given current time and graph size, draw the imgui plots.""" opts = { "graph_size": size, "scale_min": 0.0, "values_count": np.minimum(time, self.max_time - 1) } imgui.plot_lines("", self.total_counts[0], overlay_text="\nSusceptible", **opts) imgui.plot_lines("", self.total_counts[1], overlay_text="\nExposed", **opts) imgui.plot_lines("", self.total_counts[2], overlay_text="\nPresymptomatic", **opts) imgui.plot_lines("", self.total_counts[3], overlay_text="\nAsymptomatic", **opts) imgui.plot_lines("", self.total_counts[4], overlay_text="\nSymptomatic", **opts) imgui.plot_lines("", self.total_counts[5], overlay_text="\nRecovered", **opts) imgui.plot_lines("", self.total_counts[6], overlay_text="\nDead", **opts)