# Path: cameo/config.py

# 
# Path: cameo/strain_design/heuristic/evolutionary/multiprocess/observers.py
# class AbstractParallelObserver(object):
#     def __init__(self, number_of_islands=None, *args, **kwargs):
#         assert isinstance(number_of_islands, int)
#         super(AbstractParallelObserver, self).__init__()
#         self.queue = RedisQueue(name=str(uuid4()), namespace=self.__name__)
#         self.clients = {}
#         self.run = True
#         self.t = None
#         for i in range(number_of_islands):
#             self._create_client(i)
# 
#     def _create_client(self, i):
#         raise NotImplementedError
# 
#     def _listen(self):
#         print("Start %s" % self.__name__)
#         while self.run:
#             try:
#                 message = self.queue.get_nowait()
#                 self._process_message(message)
#             except Empty:
#                 pass
#             except Exception as e:
#                 print(e)
# 
#         print("Exit %s" % self.__name__)
# 
#     def _process_message(self, message):
#         raise NotImplementedError
# 
#     def start(self):
#         """
#         Starts the observer. It is called internally before the optimization starts.
#         The observer will not report anything until start has been called.
# 
#         """
#         self.run = True
#         self.t = Thread(target=self._listen)
#         self.t.start()
# 
#     def finish(self):
#         """
#         Stops the observer. The observer will not report anything else from the optimization.
#         """
#         self.run = False
# 
# class AbstractParallelObserverClient(object):
#     def __init__(self, index=None, queue=None, *args, **kwargs):
#         assert isinstance(index, int)
#         super(AbstractParallelObserverClient, self).__init__(*args, **kwargs)
#         self.index = index
#         self._queue = queue
# 
#     def __call__(self, population, num_generations, num_evaluations, args):
#         raise NotImplementedError
# 
#     def reset(self):
#         pass


from multiprocessing.queues import Full
from pandas import DataFrame
from cameo import config
from cameo.strain_design.heuristic.evolutionary.multiprocess.observers import AbstractParallelObserver, \
    AbstractParallelObserverClient
    from bokeh.plotting import figure, show
    from bokeh.models import ColumnDataSource
    from bokeh.io import push_notebook
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import, print_function

__all__ = ['IPythonNotebookBokehMultiprocessPlotObserver']


if config.use_bokeh:


class IPythonNotebookBokehMultiprocessPlotObserver(AbstractParallelObserver):
    __name__ = "IPython Notebook Bokeh Multiprocess Plot Observer"

    def __init__(self, color_map={}, *args, **kwargs):
        super(IPythonNotebookBokehMultiprocessPlotObserver, self).__init__(*args, **kwargs)
        self.connections = {}
        self.color_map = color_map
        self.data_frame = DataFrame(columns=['iteration', 'island', 'color', 'fitness'])
        self.plotted = False
        self.handle = None

    def _create_client(self, i):
        self.clients[i] = IPythonNotebookBokehMultiprocessPlotObserverClient(queue=self.queue, index=i)

    def start(self):
        self._plot()

# Path: cameo/config.py

# 
# Path: cameo/strain_design/heuristic/evolutionary/multiprocess/observers.py
# class AbstractParallelObserver(object):
#     def __init__(self, number_of_islands=None, *args, **kwargs):
#         assert isinstance(number_of_islands, int)
#         super(AbstractParallelObserver, self).__init__()
#         self.queue = RedisQueue(name=str(uuid4()), namespace=self.__name__)
#         self.clients = {}
#         self.run = True
#         self.t = None
#         for i in range(number_of_islands):
#             self._create_client(i)
# 
#     def _create_client(self, i):
#         raise NotImplementedError
# 
#     def _listen(self):
#         print("Start %s" % self.__name__)
#         while self.run:
#             try:
#                 message = self.queue.get_nowait()
#                 self._process_message(message)
#             except Empty:
#                 pass
#             except Exception as e:
#                 print(e)
# 
#         print("Exit %s" % self.__name__)
# 
#     def _process_message(self, message):
#         raise NotImplementedError
# 
#     def start(self):
#         """
#         Starts the observer. It is called internally before the optimization starts.
#         The observer will not report anything until start has been called.
# 
#         """
#         self.run = True
#         self.t = Thread(target=self._listen)
#         self.t.start()
# 
#     def finish(self):
#         """
#         Stops the observer. The observer will not report anything else from the optimization.
#         """
#         self.run = False
# 
# class AbstractParallelObserverClient(object):
#     def __init__(self, index=None, queue=None, *args, **kwargs):
#         assert isinstance(index, int)
#         super(AbstractParallelObserverClient, self).__init__(*args, **kwargs)
#         self.index = index
#         self._queue = queue
# 
#     def __call__(self, population, num_generations, num_evaluations, args):
#         raise NotImplementedError
# 
#     def reset(self):
#         pass


# Path: cameo/strain_design/heuristic/evolutionary/multiprocess/plotters.py
from multiprocessing.queues import Full
from pandas import DataFrame
from cameo import config
from cameo.strain_design.heuristic.evolutionary.multiprocess.observers import AbstractParallelObserver, \
    AbstractParallelObserverClient
    from bokeh.plotting import figure, show
    from bokeh.models import ColumnDataSource
    from bokeh.io import push_notebook

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import, print_function

__all__ = ['IPythonNotebookBokehMultiprocessPlotObserver']


if config.use_bokeh:


class IPythonNotebookBokehMultiprocessPlotObserver(AbstractParallelObserver):
    __name__ = "IPython Notebook Bokeh Multiprocess Plot Observer"

    def __init__(self, color_map={}, *args, **kwargs):
        super(IPythonNotebookBokehMultiprocessPlotObserver, self).__init__(*args, **kwargs)
        self.connections = {}
        self.color_map = color_map
        self.data_frame = DataFrame(columns=['iteration', 'island', 'color', 'fitness'])
        self.plotted = False
        self.handle = None

    def _create_client(self, i):
        self.clients[i] = IPythonNotebookBokehMultiprocessPlotObserverClient(queue=self.queue, index=i)

    def start(self):
        self._plot()

        AbstractParallelObserver.start(self)