# Path: dipde/internals/simulationconfiguration.py
# class SimulationConfiguration(object):
#     
#     def __init__(self, dt, tf, t0=0, checkpoint_file_name='checkpoint.json', checkpoint_period=np.inf):
#         
#         self.dt = dt
#         self.tf = tf
#         self.t0 = t0
#         self.checkpoint_file_name = checkpoint_file_name
#         self.checkpoint_period = checkpoint_period
#         
#         
#     def to_dict(self):
#         
#         return {'dt':self.dt,
#                 'tf':self.tf,
#                 't0':self.t0,
#                 'checkpoint_file_name':self.checkpoint_file_name,
#                 'checkpoint_period':self.checkpoint_period,
#                 'class':self.__class__.__name__,
#                 'module':__name__}
#         
#     def to_json(self, fh=None, **kwargs):
#         '''Save the contents of the InternalPopultion to json'''
#         
#         data_dict = self.to_dict()
# 
#         indent = kwargs.pop('indent',2)
# 
#         if fh is None:
#             return json.dumps(data_dict, indent=indent, **kwargs)
#         else:
#             return json.dump(data_dict, fh, indent=indent, **kwargs)


import json
import importlib
import threading
import types
import numpy as np
import time
import logging
from .simulationconfiguration import SimulationConfiguration
            curr_module, curr_class = network.pop('module'), network.pop('class')
            self.network = getattr(importlib.import_module(curr_module), curr_class)(**network) 
        else: 
            self.network = network
            
        # Create simulation_configuration:
        if isinstance(simulation_configuration, dict):
            curr_module, curr_class = simulation_configuration.pop('module'), simulation_configuration.pop('class')
            self.simulation_configuration = getattr(importlib.import_module(curr_module), curr_class)(**simulation_configuration) 
        else: 
            self.simulation_configuration = simulation_configuration

        self.checkpoint_period = getattr(self.simulation_configuration, 'checkpoint_period', np.inf)
        self.checkpoint_file_name = getattr(self.simulation_configuration, 'checkpoint_file_name', 'checkpoint.json')
        assert self.checkpoint_file_name[-4:] == 'json'

        self.thread = threading.Thread(target=self.run_checkpoint_thread)
        self.thread.daemon = True

    def run(self):
        
        # Monkey patch the trapping function onto the network callback
        old_network_callback = self.network.update_callback
        def new_network_callback(network_self, network):
            old_network_callback(network)
            self._simulation_update_callback(network)
        self.network.update_callback = types.MethodType(new_network_callback, self.network)
        
        self.pause = False
        if not (self.checkpoint_file_name is None or self.checkpoint_period ==np.inf): 

# Path: dipde/internals/simulationconfiguration.py
# class SimulationConfiguration(object):
#     
#     def __init__(self, dt, tf, t0=0, checkpoint_file_name='checkpoint.json', checkpoint_period=np.inf):
#         
#         self.dt = dt
#         self.tf = tf
#         self.t0 = t0
#         self.checkpoint_file_name = checkpoint_file_name
#         self.checkpoint_period = checkpoint_period
#         
#         
#     def to_dict(self):
#         
#         return {'dt':self.dt,
#                 'tf':self.tf,
#                 't0':self.t0,
#                 'checkpoint_file_name':self.checkpoint_file_name,
#                 'checkpoint_period':self.checkpoint_period,
#                 'class':self.__class__.__name__,
#                 'module':__name__}
#         
#     def to_json(self, fh=None, **kwargs):
#         '''Save the contents of the InternalPopultion to json'''
#         
#         data_dict = self.to_dict()
# 
#         indent = kwargs.pop('indent',2)
# 
#         if fh is None:
#             return json.dumps(data_dict, indent=indent, **kwargs)
#         else:
#             return json.dump(data_dict, fh, indent=indent, **kwargs)


# Path: dipde/internals/simulation.py
import json
import importlib
import threading
import types
import numpy as np
import time
import logging
from .simulationconfiguration import SimulationConfiguration

            curr_module, curr_class = network.pop('module'), network.pop('class')
            self.network = getattr(importlib.import_module(curr_module), curr_class)(**network) 
        else: 
            self.network = network
            
        # Create simulation_configuration:
        if isinstance(simulation_configuration, dict):
            curr_module, curr_class = simulation_configuration.pop('module'), simulation_configuration.pop('class')
            self.simulation_configuration = getattr(importlib.import_module(curr_module), curr_class)(**simulation_configuration) 
        else: 
            self.simulation_configuration = simulation_configuration

        self.checkpoint_period = getattr(self.simulation_configuration, 'checkpoint_period', np.inf)
        self.checkpoint_file_name = getattr(self.simulation_configuration, 'checkpoint_file_name', 'checkpoint.json')
        assert self.checkpoint_file_name[-4:] == 'json'

        self.thread = threading.Thread(target=self.run_checkpoint_thread)
        self.thread.daemon = True

    def run(self):
        
        # Monkey patch the trapping function onto the network callback
        old_network_callback = self.network.update_callback
        def new_network_callback(network_self, network):
            old_network_callback(network)
            self._simulation_update_callback(network)
        self.network.update_callback = types.MethodType(new_network_callback, self.network)
        
        self.pause = False
        if not (self.checkpoint_file_name is None or self.checkpoint_period ==np.inf): 

            self.thread.start() 