def test_warning_from_another_thread(self): class AnotherWidget(QObject): # This must be a method, not a staticmethod to run in the thread def issue_warning(self): # pylint: disable=no-self-use time.sleep(0.1) warnings.warn("warning from another thread") warning_thread.quit() def read(): warning_thread.start() time.sleep(0.2) return Table(TITANIC_PATH) warning_thread = QThread() another_widget = AnotherWidget() another_widget.moveToThread(warning_thread) warning_thread.started.connect(another_widget.issue_warning) reader = Mock() reader.read = read self.widget._get_reader = lambda: reader self.widget.last_path = lambda: "foo" self.widget._update_sheet_combo = Mock() # Warning must be caught by unit tests, but not the widget with self.assertWarns(UserWarning): self.widget._try_load() self.assertFalse(self.widget.Warning.load_warning.is_shown())
def __accepted(self): steps = self.addonwidget.item_state() if steps: # Move all uninstall steps to the front steps = sorted(steps, key=lambda step: 0 if step[0] == Uninstall else 1) self.__installer = Installer(steps=steps, user_install=self.user_install) self.__thread = QThread(self) self.__thread.start() self.__installer.moveToThread(self.__thread) self.__installer.finished.connect(self.__on_installer_finished) self.__installer.error.connect(self.__on_installer_error) self.__installer.installStatusChanged.connect( self.__progress.setLabelText) self.__progress.show() self.__progress.setLabelText("Installing") self.__installer.start() else: self.accept()
def test_task(self): results = [] task = Task(function=QThread.currentThread) task.resultReady.connect(results.append) task.start() self.app.processEvents() self.assertSequenceEqual(results, [QThread.currentThread()]) thread = QThread() thread.start() try: task = Task(function=QThread.currentThread) task.moveToThread(thread) self.assertIsNot(task.thread(), QThread.currentThread()) self.assertIs(task.thread(), thread) results = Future() def record(value): # record the result value and the calling thread results.set_result((QThread.currentThread(), value)) task.resultReady.connect(record, Qt.DirectConnection) task.start() f = task.future() emit_thread, thread_ = results.result(3) self.assertIs(f.result(3), thread) self.assertIs(emit_thread, thread) self.assertIs(thread_, thread) finally: thread.quit() thread.wait()
def initialize(self): """ Clear and initialize the dialog. This method must be called by the widget when the data is reset, e.g. from `set_data` handler. """ if self._thread is not None and self._thread.isRunning(): self.keep_running = False self._thread.quit() self._thread.wait() self.keep_running = False self.scheduled_call = None self.saved_state = None self.saved_progress = 0 self.update_timer.stop() self.progressBarFinished() self.scores = [] self._update_model() # empty queue self.rank_model.clear() self.button.setText("Start") self.button.setEnabled(self.check_preconditions()) self._thread = QThread(self) self._worker = Worker(self) self._worker.moveToThread(self._thread) self._worker.stopped.connect(self._thread.quit) self._worker.stopped.connect(self._select_first_if_none) self._worker.stopped.connect(self._stopped) self._worker.done.connect(self._done) self._thread.started.connect(self._worker.do_work)
def test_task(self): results = [] task = Task(function=QThread.currentThread) task.resultReady.connect(results.append) task.start() self.app.processEvents() self.assertSequenceEqual(results, [QThread.currentThread()]) results = [] thread = QThread() thread.start() task = Task(function=QThread.currentThread) task.moveToThread(thread) self.assertIsNot(task.thread(), QThread.currentThread()) self.assertIs(task.thread(), thread) task.resultReady.connect(results.append, Qt.DirectConnection) task.start() f = task.future() self.assertIsNot(f.result(3), QThread.currentThread()) self.assertIs(f.result(3), results[-1])
def _rtd(self): self._daq_reader = QThread() self._zidaq.start_daq((512, 512), self._dwell, num_frames=3) self._zidaq.moveToThread(self._daq_reader) self._zidaq.daqData.connect(self._img.read_daq) self._daq_reader.started.connect(self._zidaq.read_daq) self._daq_reader.start()
def _recompute_som(self): if self.cont_x is None: return som = SOM(self.size_x, self.size_y, hexagonal=self.hexagonal, pca_init=self.initialization == 0, random_seed=0 if self.initialization == 2 else None) class Optimizer(QObject): update = Signal(float, np.ndarray, np.ndarray) done = Signal(SOM) stopped = Signal() stop_optimization = False def __init__(self, data, som): super().__init__() self.som = som self.data = data def callback(self, progress): self.update.emit(progress, self.som.weights.copy(), self.som.ssum_weights.copy()) return not self.stop_optimization def run(self): try: self.som.fit(self.data, N_ITERATIONS, callback=self.callback) # Report an exception, but still remove the thread finally: self.done.emit(self.som) self.stopped.emit() def thread_finished(): self._optimizer = None self._optimizer_thread = None self.progressBarInit() self._optimizer = Optimizer(self.cont_x, som) self._optimizer_thread = QThread() self._optimizer_thread.setStackSize(5 * 2**20) self._optimizer.update.connect(self.__update) self._optimizer.done.connect(self.__done) self._optimizer.stopped.connect(self._optimizer_thread.quit) self._optimizer.moveToThread(self._optimizer_thread) self._optimizer_thread.started.connect(self._optimizer.run) self._optimizer_thread.finished.connect(thread_finished) self.stop_optimization = False self._optimizer_thread.start()
def pull_image(self): self.infoLabel.setText('Pulling \'' + self.image_name + ":" + self.image_version + '\' from Dockerhub...') self.setStatusMessage("Downloading...") self.progressBarInit() self.is_running = True self.btn_run.setEnabled(False) # Pull the image in a new thread self.pull_image_thread = QThread() self.pull_image_worker = PullImageWorker(self.docker, self.image_name, self.image_version) self.pull_image_worker.progress[int].connect(self.pull_image_progress) self.pull_image_worker.finished.connect(self.pull_image_finished) self.pull_image_worker.moveToThread(self.pull_image_thread) self.pull_image_thread.started.connect(self.pull_image_worker.work) self.pull_image_thread.start()
def run_container(self): self.is_running = True self.infoLabel.setText('Running alignment...') self.setStatusMessage('Running...') self.progressBarInit() # Run the container in a new thread self.run_container_thread = QThread() self.run_container_worker = RunAlignmentWorker(self.docker, self.image_name, self.host_ref_dir, self.host_seq_dir, self.host_counts_dir) self.run_container_worker.progress[int].connect( self.run_container_progress) self.run_container_worker.finished.connect(self.run_container_finished) self.run_container_worker.moveToThread(self.run_container_thread) self.run_container_thread.started.connect( self.run_container_worker.work) self.run_container_thread.start()
def relayout(self, restart): if self.edges is None: return if restart or self.positions is None: self.set_random_positions() self.progressbar = gui.ProgressBar(self, 100) self.set_buttons(running=True) self._stop_optimization = False Simplifications = self.graph.Simplifications self.graph.set_simplifications( Simplifications.NoDensity + Simplifications.NoLabels * (len(self.graph.labels) > 20) + Simplifications.NoEdgeLabels * (len(self.graph.edge_labels) > 20) + Simplifications.NoEdges * (self.number_of_edges > 30000)) large_graph = self.number_of_nodes + self.number_of_edges > 30000 class LayoutOptimizer(QObject): update = Signal(np.ndarray, float) done = Signal(np.ndarray) stopped = Signal() def __init__(self, widget): super().__init__() self.widget = widget def send_update(self, positions, progress): if not large_graph: self.update.emit(np.array(positions), progress) return not self.widget._stop_optimization def run(self): widget = self.widget edges = widget.edges nnodes = widget.number_of_nodes init_temp = 0.05 if restart else 0.2 k = widget.layout_density / 10 / np.sqrt(nnodes) sample_ratio = None if nnodes < 1000 else 1000 / nnodes fruchterman_reingold( widget.positions, edges, widget.observe_weights, FR_ALLOWED_TIME, k, init_temp, sample_ratio, callback_step=4, callback=self.send_update) self.done.emit(widget.positions) self.stopped.emit() def update(positions, progress): self.progressbar.advance(progress) self.positions = positions self.graph.update_coordinates() def done(positions): self.positions = positions self.set_buttons(running=False) self.graph.set_simplifications( self.graph.Simplifications.NoSimplifications) self.graph.update_coordinates() self.progressbar.finish() def thread_finished(): self._optimizer = None self._animation_thread = None self._optimizer = LayoutOptimizer(self) self._animation_thread = QThread() self._optimizer.update.connect(update) self._optimizer.done.connect(done) self._optimizer.stopped.connect(self._animation_thread.quit) self._optimizer.moveToThread(self._animation_thread) self._animation_thread.started.connect(self._optimizer.run) self._animation_thread.finished.connect(thread_finished) self._animation_thread.start()
def _recompute_som(self): if self.cont_x is None: return class Optimizer(QObject): update = Signal(float, np.ndarray, np.ndarray) done = Signal(SOM) stopped = Signal() def __init__(self, data, widget): super().__init__() self.som = SOM( widget.size_x, widget.size_y, hexagonal=widget.hexagonal, pca_init=widget.initialization == 0, random_seed=0 if widget.initialization == 2 else None) self.data = data self.widget = widget def callback(self, progress): self.update.emit(progress, self.som.weights.copy(), self.som.ssum_weights.copy()) return not self.widget.stop_optimization def run(self): try: self.som.fit(self.data, N_ITERATIONS, callback=self.callback) # Report an exception, but still remove the thread finally: self.done.emit(self.som) self.stopped.emit() def update(_progress, weights, ssum_weights): progressbar.advance() self._assign_instances(weights, ssum_weights) self._redraw() def done(som): self.enable_controls(True) progressbar.finish() self._assign_instances(som.weights, som.ssum_weights) self._redraw() # This is the first time we know what was selected (assuming that # initialization is not set to random) if self.__pending_selection is not None: self.on_selection_change(self.__pending_selection) self.__pending_selection = None self.update_output() def thread_finished(): self._optimizer = None self._optimizer_thread = None progressbar = gui.ProgressBar(self, N_ITERATIONS) self._optimizer = Optimizer(self.cont_x, self) self._optimizer_thread = QThread() self._optimizer_thread.setStackSize(5 * 2**20) self._optimizer.update.connect(update) self._optimizer.done.connect(done) self._optimizer.stopped.connect(self._optimizer_thread.quit) self._optimizer.moveToThread(self._optimizer_thread) self._optimizer_thread.started.connect(self._optimizer.run) self._optimizer_thread.finished.connect(thread_finished) self.stop_optimization = False self._optimizer_thread.start()
def relayout(self): if self.edges is None: return if self.randomizePositions or self.positions is None: self.set_random_positions() self.progressbar = gui.ProgressBar(self, FR_ITERATIONS) self.set_buttons(running=True) self._stop_optimization = False Simplifications = self.graph.Simplifications self.graph.set_simplifications( Simplifications.NoDensity + Simplifications.NoLabels * (len(self.graph.labels) > 20) + Simplifications.NoEdgeLabels * (len(self.graph.edge_labels) > 20) + Simplifications.NoEdges * (self.number_of_edges > 1000)) large_graph = self.number_of_nodes + self.number_of_edges > 20000 iterations = 5 if large_graph else FR_ITERATIONS class LayoutOptimizer(QObject): update = Signal(np.ndarray, float) done = Signal(np.ndarray) stopped = Signal() def __init__(self, widget): super().__init__() self.widget = widget def send_update(self, positions, progress): if not large_graph: self.update.emit(np.array(positions), progress) return not self.widget._stop_optimization def run(self): widget = self.widget edges = widget.edges positions = np.array(fruchterman_reingold( edges.data, edges.row, edges.col, 1 / np.sqrt(widget.number_of_nodes), # k widget.positions, np.array([], dtype=np.int32), # fixed iterations, 0.1, # sample ratio self.send_update, 0.25)) self.done.emit(positions) self.stopped.emit() def update(positions, progress): self.progressbar.advance(progress) self.positions = positions self.graph.update_coordinates() def done(positions): self.positions = positions self.set_buttons(running=False) self.graph.set_simplifications( self.graph.Simplifications.NoSimplifications) self.graph.update_coordinates() self.progressbar.finish() def thread_finished(): self._optimizer = None self._animation_thread = None self._optimizer = LayoutOptimizer(self) self._animation_thread = QThread() self._optimizer.update.connect(update) self._optimizer.done.connect(done) self._optimizer.stopped.connect(self._animation_thread.quit) self._optimizer.moveToThread(self._animation_thread) self._animation_thread.started.connect(self._optimizer.run) self._animation_thread.finished.connect(thread_finished) self._animation_thread.start()