# Path: ndlib/utils.py
# def multi_runs(model, execution_number=1, iteration_number=50, infection_sets=None,
#                nprocesses=multiprocessing.cpu_count()):
#     """
#     Multiple executions of a given model varying the initial set of infected nodes
# 
#     :param model: a configured diffusion model
#     :param execution_number: number of instantiations
#     :param iteration_number: number of iterations per execution
#     :param infection_sets: predefined set of infected nodes sets
#     :param nprocesses: number of processes. Default values cpu number.
#     :return: resulting trends for all the executions
#     """
# 
#     if nprocesses > multiprocessing.cpu_count():
#         nprocesses = multiprocessing.cpu_count()
# 
#     executions = []
#     seeds = np.around(np.random.rand(execution_number)*2**32).astype(int)
# 
#     if infection_sets is not None:
#         if len(infection_sets) != execution_number:
#             raise InitializationException(
#                 {"message": "Number of infection sets provided does not match the number of executions required"})
# 
#         for x in past.builtins.xrange(0, execution_number, nprocesses):
# 
#             with closing(multiprocessing.Pool(processes=nprocesses, maxtasksperchild=10)) as pool:
#                 tasks = [(seeds[i], copy.deepcopy(model).reset(infection_sets[i])) for i in
#                          past.builtins.xrange(x, min(x + nprocesses, execution_number))]
#                 results = [pool.apply_async(__execute, (*t, iteration_number)) for t in tasks]
# 
#             for result in results:
#                 executions.append(result.get())
#     else:
#         for x in past.builtins.xrange(0, execution_number, nprocesses):
#             with closing(multiprocessing.Pool(processes=nprocesses, maxtasksperchild=10)) as pool:
# 
#                 tasks = [(seeds[i], copy.deepcopy(model).reset()) for i in
#                          past.builtins.xrange(x, min(x + nprocesses, execution_number))]
#                 results = [pool.apply_async(__execute, (*t, iteration_number)) for t in tasks]
# 
#             for result in results:
#                 executions.append(result.get())
# 
#     return executions


import unittest
import networkx as nx
import ndlib.models.ModelConfig as mc
import ndlib.models.epidemics as epd
from ndlib.utils import multi_runs

        # Model selection
        model1 = epd.SIRModel(g)

        # Model Configuration
        config = mc.Configuration()
        config.add_model_parameter('beta', 0.001)
        config.add_model_parameter('gamma', 0.01)
        config.add_model_parameter("fraction_infected", 0.05)
        model1.set_initial_status(config)

        # Simulation multiple execution
        trends = multi_runs(model1, execution_number=10, iteration_number=100, infection_sets=None, nprocesses=4)
        self.assertIsNotNone(trends)

    def test_multi_initial_set(self):
        # Network topology
        g = nx.erdos_renyi_graph(1000, 0.1)

        # Model selection
        model1 = epd.SIRModel(g)

        # Model Configuration
        config = mc.Configuration()
        config.add_model_parameter('beta', 0.001)
        config.add_model_parameter('gamma', 0.01)
        model1.set_initial_status(config)

        # Simulation multiple execution
        infection_sets = [(1, 2, 3, 4, 5), (3, 23, 22, 54, 2), (98, 2, 12, 26, 3), (4, 6, 9)]

# Path: ndlib/utils.py
# def multi_runs(model, execution_number=1, iteration_number=50, infection_sets=None,
#                nprocesses=multiprocessing.cpu_count()):
#     """
#     Multiple executions of a given model varying the initial set of infected nodes
# 
#     :param model: a configured diffusion model
#     :param execution_number: number of instantiations
#     :param iteration_number: number of iterations per execution
#     :param infection_sets: predefined set of infected nodes sets
#     :param nprocesses: number of processes. Default values cpu number.
#     :return: resulting trends for all the executions
#     """
# 
#     if nprocesses > multiprocessing.cpu_count():
#         nprocesses = multiprocessing.cpu_count()
# 
#     executions = []
#     seeds = np.around(np.random.rand(execution_number)*2**32).astype(int)
# 
#     if infection_sets is not None:
#         if len(infection_sets) != execution_number:
#             raise InitializationException(
#                 {"message": "Number of infection sets provided does not match the number of executions required"})
# 
#         for x in past.builtins.xrange(0, execution_number, nprocesses):
# 
#             with closing(multiprocessing.Pool(processes=nprocesses, maxtasksperchild=10)) as pool:
#                 tasks = [(seeds[i], copy.deepcopy(model).reset(infection_sets[i])) for i in
#                          past.builtins.xrange(x, min(x + nprocesses, execution_number))]
#                 results = [pool.apply_async(__execute, (*t, iteration_number)) for t in tasks]
# 
#             for result in results:
#                 executions.append(result.get())
#     else:
#         for x in past.builtins.xrange(0, execution_number, nprocesses):
#             with closing(multiprocessing.Pool(processes=nprocesses, maxtasksperchild=10)) as pool:
# 
#                 tasks = [(seeds[i], copy.deepcopy(model).reset()) for i in
#                          past.builtins.xrange(x, min(x + nprocesses, execution_number))]
#                 results = [pool.apply_async(__execute, (*t, iteration_number)) for t in tasks]
# 
#             for result in results:
#                 executions.append(result.get())
# 
#     return executions


# Path: ndlib/test/test_parallel.py
import unittest
import networkx as nx
import ndlib.models.ModelConfig as mc
import ndlib.models.epidemics as epd
from ndlib.utils import multi_runs


        # Model selection
        model1 = epd.SIRModel(g)

        # Model Configuration
        config = mc.Configuration()
        config.add_model_parameter('beta', 0.001)
        config.add_model_parameter('gamma', 0.01)
        config.add_model_parameter("fraction_infected", 0.05)
        model1.set_initial_status(config)

        # Simulation multiple execution
        trends = multi_runs(model1, execution_number=10, iteration_number=100, infection_sets=None, nprocesses=4)
        self.assertIsNotNone(trends)

    def test_multi_initial_set(self):
        # Network topology
        g = nx.erdos_renyi_graph(1000, 0.1)

        # Model selection
        model1 = epd.SIRModel(g)

        # Model Configuration
        config = mc.Configuration()
        config.add_model_parameter('beta', 0.001)
        config.add_model_parameter('gamma', 0.01)
        model1.set_initial_status(config)

        # Simulation multiple execution
        infection_sets = [(1, 2, 3, 4, 5), (3, 23, 22, 54, 2), (98, 2, 12, 26, 3), (4, 6, 9)]

        trends = multi_runs(model1, execution_number=4, iteration_number=100, infection_sets=infection_sets,