def __init__(self, name: str, parent=None): super().__init__(name=name, parent=parent) self.dialog = RegionPlotWindow() # Signals self.signal_ok = self.dialog.ok_button.clicked self.signal_add = self.dialog.add_button.clicked self.signal_value_changed = self.dialog.signal_value_changed self.signal_close = self.dialog.signal_close
def test_region_plot_window_can_be_initialized_from_document_data( database_fixture, ): # generate data and associate with document n = 100 document, *_ = pytest.helpers.add_document_and_foreign_keys( database_fixture) x_arr = np.linspace(left_edge, right_edge, n) y_arr = np.random.rand(n) document.insert_time_series(database_fixture, x_arr, y_arr) region_plot_window = RegionPlotWindow() region_plot_window.plot( *document.get_related_time_series(database_fixture)) np.testing.assert_array_almost_equal(region_plot_window.x_arr, x_arr) np.testing.assert_array_almost_equal(region_plot_window.y_arr, y_arr)
def run(): ''' Graphical annotation ''' df = join_data(*list(read_data())) sessions = df['session_id'].unique() session_id = sessions[9] df_session = df[df['session_id'] == session_id] p = RegionPlotWindow() w = RegionWidget() p.add_plot(w) # plot data w.plot(df_session['time_s'], df_session['torque_Nm']) w.x_label = 'time (s)' w.y_label = 'torque (Nm)' # add regions for existing events for key, df_event in df_session.groupby('event'): t = df_event['time_s'] edit_widget = w.add_region((t.min(), t.max())) edit_widget.name = key # run graphical annotation ret = p.exec_() # extract only Data table columns df_data = df[[c.name for c in Data.columns]] # remove old events from session data if len(w.region_edit_map) > 0: df_data.loc[df_data['session_id'] == session_id, 'event'] = None for edit_widget in w.region_edit_map.values(): x_range = sorted(edit_widget.region()) df_data.loc[(df_data['session_id'] == session_id) & (df_data['time_s'] >= x_range[0]) & (df_data['time_s'] <= x_range[1]), 'event', ] = edit_widget.name df_data.to_sql(str(Data), engine, if_exists='replace', index=False) return ret
def test_annotated_events_inserted_to_database_after_ok_on_region_plot_window_is_clicked( database_fixture, ): # generate data and associate with document n = 100 document, *_ = pytest.helpers.add_document_and_foreign_keys( database_fixture) x_arr = np.linspace(left_edge, right_edge, n) y_arr = np.random.rand(n) document.insert_time_series(database_fixture, x_arr, y_arr) region_plot_window = RegionPlotWindow() region_plot_window.plot( *document.get_related_time_series(database_fixture)) # add regions using add button region_plot_window.set_add_count(region_count) region_plot_window.add_button.clicked.emit(True) # verify that annotated events are correct events = region_plot_window.get_annotated_events() assert len(events) == region_count
def test_region_plot_window_has_maximize_button(): region_plot_window = RegionPlotWindow() assert int(region_plot_window.windowFlags() & Qt.WindowMaximizeButtonHint)
def test_region_plot_window_ok_button_closes_the_window(): d = RegionPlotWindow() n = 100 d.plot(x_arr=list(range(n)), y_arr=list(range(n))) assert len(d.region_edit_map) == 0 d.add_button_clicked() assert len(d.region_edit_map) == 1 def click_button(standard_button): w = QApplication.activeWindow() b = w.button(standard_button) b.clicked.emit() timer = QTimer() timer.setSingleShot(True) timer.setInterval(100) timer.timeout.connect(partial(click_button, QMessageBox.Yes)) timer.start() d.show() assert d.isVisible() d.ok_button_clicked() assert not d.isVisible()
class EventDetectionState(MyState): def __init__(self, name: str, parent=None): super().__init__(name=name, parent=parent) self.dialog = RegionPlotWindow() # Signals self.signal_ok = self.dialog.ok_button.clicked self.signal_add = self.dialog.add_button.clicked self.signal_value_changed = self.dialog.signal_value_changed self.signal_close = self.dialog.signal_close def onEntry(self, event: QEvent): """ Open a RegionPlotWindow and plot context document data. :param event: :return: """ super().onEntry(event) self.dialog.plot(*self.document.get_related_time_series(self.database)) # Clear existing regions self.dialog.clear_regions() # Add as many regions as there are turns in one full turn sensor_info = self.document.get_related_sensor_info(self.database) self.dialog.set_add_count(int(sensor_info.turns_in_full_turn)) self.dialog.add_button.clicked.emit(True) self.dialog.show() def onExit(self, event: QEvent): super().onExit(event) self.dialog.close() def region_count(self) -> int: """ Return number of regions. """ return self.dialog.region_count() def get_annotated_events(self) -> List[AnnotatedEvent]: """ Return list of annotated events. """ return self.dialog.get_annotated_events()