44import numpy as np
55import scipy .sparse as sp
66
7- from AnyQt .QtCore import Qt , QRectF , pyqtSignal as Signal , QObject , QThread
7+ from AnyQt .QtCore import Qt , QRectF , pyqtSignal as Signal , QObject , QThread , \
8+ pyqtSlot as Slot
89from AnyQt .QtGui import QTransform , QPen , QBrush , QColor , QPainter , \
910 QPainterPath , QKeyEvent
1011from AnyQt .QtWidgets import \
@@ -498,6 +499,7 @@ def redraw_selection(self, marks=None):
498499 def restart_som_pressed (self ):
499500 if self ._optimizer_thread is not None :
500501 self .stop_optimization = True
502+ self ._optimizer .stop_optimization = True
501503 else :
502504 self .start_som ()
503505
@@ -678,26 +680,29 @@ def _recompute_som(self):
678680 if self .cont_x is None :
679681 return
680682
683+ som = SOM (
684+ self .size_x , self .size_y ,
685+ hexagonal = self .hexagonal ,
686+ pca_init = self .initialization == 0 ,
687+ random_seed = 0 if self .initialization == 2 else None
688+ )
689+
681690 class Optimizer (QObject ):
682691 update = Signal (float , np .ndarray , np .ndarray )
683692 done = Signal (SOM )
684693 stopped = Signal ()
694+ stop_optimization = False
685695
686- def __init__ (self , data , widget ):
696+ def __init__ (self , data , som ):
687697 super ().__init__ ()
688- self .som = SOM (
689- widget .size_x , widget .size_y ,
690- hexagonal = widget .hexagonal ,
691- pca_init = widget .initialization == 0 ,
692- random_seed = 0 if widget .initialization == 2 else None )
698+ self .som = som
693699 self .data = data
694- self .widget = widget
695700
696701 def callback (self , progress ):
697702 self .update .emit (
698703 progress ,
699704 self .som .weights .copy (), self .som .ssum_weights .copy ())
700- return not self .widget . stop_optimization
705+ return not self .stop_optimization
701706
702707 def run (self ):
703708 try :
@@ -708,44 +713,47 @@ def run(self):
708713 self .done .emit (self .som )
709714 self .stopped .emit ()
710715
711- def update (_progress , weights , ssum_weights ):
712- progressbar .advance ()
713- self ._assign_instances (weights , ssum_weights )
714- self ._redraw ()
715-
716- def done (som ):
717- self .enable_controls (True )
718- progressbar .finish ()
719- self ._assign_instances (som .weights , som .ssum_weights )
720- self ._redraw ()
721- # This is the first time we know what was selected (assuming that
722- # initialization is not set to random)
723- if self .__pending_selection is not None :
724- self .on_selection_change (self .__pending_selection )
725- self .__pending_selection = None
726- self .update_output ()
727-
728716 def thread_finished ():
729717 self ._optimizer = None
730718 self ._optimizer_thread = None
731719
732- progressbar = gui . ProgressBar ( self , N_ITERATIONS )
720+ self . progressBarInit ( )
733721
734- self ._optimizer = Optimizer (self .cont_x , self )
722+ self ._optimizer = Optimizer (self .cont_x , som )
735723 self ._optimizer_thread = QThread ()
736724 self ._optimizer_thread .setStackSize (5 * 2 ** 20 )
737- self ._optimizer .update .connect (update )
738- self ._optimizer .done .connect (done )
725+ self ._optimizer .update .connect (self . __update )
726+ self ._optimizer .done .connect (self . __done )
739727 self ._optimizer .stopped .connect (self ._optimizer_thread .quit )
740728 self ._optimizer .moveToThread (self ._optimizer_thread )
741729 self ._optimizer_thread .started .connect (self ._optimizer .run )
742730 self ._optimizer_thread .finished .connect (thread_finished )
743731 self .stop_optimization = False
744732 self ._optimizer_thread .start ()
745733
734+ @Slot (float , object , object )
735+ def __update (self , _progress , weights , ssum_weights ):
736+ self .progressBarSet (_progress )
737+ self ._assign_instances (weights , ssum_weights )
738+ self ._redraw ()
739+
740+ @Slot (object )
741+ def __done (self , som ):
742+ self .enable_controls (True )
743+ self .progressBarFinished ()
744+ self ._assign_instances (som .weights , som .ssum_weights )
745+ self ._redraw ()
746+ # This is the first time we know what was selected (assuming that
747+ # initialization is not set to random)
748+ if self .__pending_selection is not None :
749+ self .on_selection_change (self .__pending_selection )
750+ self .__pending_selection = None
751+ self .update_output ()
752+
746753 def stop_optimization_and_wait (self ):
747754 if self ._optimizer_thread is not None :
748755 self .stop_optimization = True
756+ self ._optimizer .stop_optimization = True
749757 self ._optimizer_thread .quit ()
750758 self ._optimizer_thread .wait ()
751759 self ._optimizer_thread = None
0 commit comments