# Path: LFPy/inputgenerators.py
# def get_activation_times_from_distribution(n, tstart=0., tstop=1.E6,
#                                            distribution=scipy.stats.expon,
#                                            rvs_args=dict(loc=0, scale=1),
#                                            maxiter=1E6):
#     """
#     Construct a length n list of ndarrays containing continously increasing
#     random numbers on the interval [tstart, tstop], with intervals drawn from
#     a chosen continuous random variable distribution subclassed from
#     scipy.stats.rv_continous, e.g., scipy.stats.expon or scipy.stats.gamma.
# 
#     The most likely initial first entry is
#     ``tstart + method.rvs(size=inf, **rvs_args).mean()``
# 
#     Parameters
#     ----------
#     n: int
#         number of ndarrays in list
#     tstart: float
#         minimum allowed value in ndarrays
#     tstop: float
#         maximum allowed value in ndarrays
#     distribution: object
#         subclass of scipy.stats.rv_continous. Distributions
#         producing negative values should be avoided if continously increasing
#         values should be obtained, i.e., the probability density function
#         ``(distribution.pdf(**rvs_args))`` should be ``0`` for ``x < 0``,
#         which is not explicitly tested for.
#     rvs_args: dict
#         parameters for method.rvs method. If "size" is in dict, then tstop will
#         be ignored, and each ndarray in output list will be
#         ``distribution.rvs(**rvs_args).cumsum() + tstart``. If size is not
#         given in dict, then values up to tstop will be included
#     maxiter: int
#         maximum number of iterations
# 
# 
#     Returns
#     -------
#     list of ndarrays
#         length n list of arrays containing data
# 
#     Raises
#     ------
#     AssertionError
#         if distribution does not have the 'rvs' attribute
#     StopIteration
#         if number of while-loop iterations reaches maxiter
# 
# 
#     Examples
#     --------
#     Create n sets of activation times with intervals drawn from the exponential
#     distribution, with rate expectation lambda 10 s^-1 (thus
#     scale=1000 / lambda). Here we assume output in units of ms
# 
#     >>> from LFPy.inputgenerators import get_activation_times_from_distribution
#     >>> import scipy.stats as st
#     >>> import matplotlib.pyplot as plt
#     >>> times = get_activation_times_from_distribution(n=10, tstart=0.,
#     >>>                                                tstop=1000.,
#     >>>                                                distribution=st.expon,
#     >>>                                                rvs_args=dict(loc=0.,
#     >>>                                                scale=100.))
#     """
#     assert hasattr(distribution, 'rvs'), \
#         'distribution={} must have the attribute "rvs"'.format(distribution)
# 
#     times = []
#     if 'size' in rvs_args.keys():
#         for i in range(n):
#             times += [distribution.rvs(**rvs_args).cumsum() + tstart]
#     else:
#         for i in range(n):
#             values = distribution.rvs(size=1000, **rvs_args).cumsum() + tstart
#             iter = 0
#             while values[-1] < tstop and iter < maxiter:
#                 values = np.r_[values, distribution.rvs(
#                     size=1000, **rvs_args).cumsum() + values[-1]]
#                 iter += 1
# 
#             if iter == maxiter:
#                 raise StopIteration('maximum number of iterations reach. Con')
# 
#             times += [values[values < tstop]]
# 
#     return times


import os
import numpy as np
import scipy.stats as st
import matplotlib.pyplot as plt
import LFPy
import neuron
import sys
import zipfile
import ssl
from os.path import join
from matplotlib.collections import PolyCollection
from LFPy.inputgenerators import get_activation_times_from_distribution
    from urllib2 import urlopen
    from urllib.request import urlopen
from warnings import warn
from mpi4py import MPI
        self.LFP = COMM.reduce(self.LFP)  # LFP is None on all but RANK 0

        # collect all simulation results on RANK 0, including single cell LFP
        if RANK == 0:
            for i in range(1, SIZE):
                result = COMM.recv(
                    source=MPI.ANY_SOURCE)  # receive from ANY rank
                self.results.update(result)  # collect
        else:
            COMM.send(self.results, dest=0)  # send to RANK 0
            self.results = None  # results only exist on RANK 0

        COMM.Barrier()  # sync MPI threads

    def distribute_cellsims(self):
        '''Will distribute and run cell simulations across ranks'''
        # start unique cell simulation on every RANK,
        # and store the electrode and cell objects in dicts indexed by
        # cellindex
        results = {}
        for cellindex in range(self.POPULATION_SIZE):
            if divmod(cellindex, SIZE)[1] == RANK:
                results.update({cellindex: self.cellsim(cellindex)})
        return results

    def drawRandSpikeTimes(self):
        '''draw and distribute some spike times for each cell in population'''
        if RANK == 0:
            RandSpikeTimes = []
            for cellindex in range(self.POPULATION_SIZE):

# Path: LFPy/inputgenerators.py
# def get_activation_times_from_distribution(n, tstart=0., tstop=1.E6,
#                                            distribution=scipy.stats.expon,
#                                            rvs_args=dict(loc=0, scale=1),
#                                            maxiter=1E6):
#     """
#     Construct a length n list of ndarrays containing continously increasing
#     random numbers on the interval [tstart, tstop], with intervals drawn from
#     a chosen continuous random variable distribution subclassed from
#     scipy.stats.rv_continous, e.g., scipy.stats.expon or scipy.stats.gamma.
# 
#     The most likely initial first entry is
#     ``tstart + method.rvs(size=inf, **rvs_args).mean()``
# 
#     Parameters
#     ----------
#     n: int
#         number of ndarrays in list
#     tstart: float
#         minimum allowed value in ndarrays
#     tstop: float
#         maximum allowed value in ndarrays
#     distribution: object
#         subclass of scipy.stats.rv_continous. Distributions
#         producing negative values should be avoided if continously increasing
#         values should be obtained, i.e., the probability density function
#         ``(distribution.pdf(**rvs_args))`` should be ``0`` for ``x < 0``,
#         which is not explicitly tested for.
#     rvs_args: dict
#         parameters for method.rvs method. If "size" is in dict, then tstop will
#         be ignored, and each ndarray in output list will be
#         ``distribution.rvs(**rvs_args).cumsum() + tstart``. If size is not
#         given in dict, then values up to tstop will be included
#     maxiter: int
#         maximum number of iterations
# 
# 
#     Returns
#     -------
#     list of ndarrays
#         length n list of arrays containing data
# 
#     Raises
#     ------
#     AssertionError
#         if distribution does not have the 'rvs' attribute
#     StopIteration
#         if number of while-loop iterations reaches maxiter
# 
# 
#     Examples
#     --------
#     Create n sets of activation times with intervals drawn from the exponential
#     distribution, with rate expectation lambda 10 s^-1 (thus
#     scale=1000 / lambda). Here we assume output in units of ms
# 
#     >>> from LFPy.inputgenerators import get_activation_times_from_distribution
#     >>> import scipy.stats as st
#     >>> import matplotlib.pyplot as plt
#     >>> times = get_activation_times_from_distribution(n=10, tstart=0.,
#     >>>                                                tstop=1000.,
#     >>>                                                distribution=st.expon,
#     >>>                                                rvs_args=dict(loc=0.,
#     >>>                                                scale=100.))
#     """
#     assert hasattr(distribution, 'rvs'), \
#         'distribution={} must have the attribute "rvs"'.format(distribution)
# 
#     times = []
#     if 'size' in rvs_args.keys():
#         for i in range(n):
#             times += [distribution.rvs(**rvs_args).cumsum() + tstart]
#     else:
#         for i in range(n):
#             values = distribution.rvs(size=1000, **rvs_args).cumsum() + tstart
#             iter = 0
#             while values[-1] < tstop and iter < maxiter:
#                 values = np.r_[values, distribution.rvs(
#                     size=1000, **rvs_args).cumsum() + values[-1]]
#                 iter += 1
# 
#             if iter == maxiter:
#                 raise StopIteration('maximum number of iterations reach. Con')
# 
#             times += [values[values < tstop]]
# 
#     return times


# Path: examples/example_mpi.py
import os
import numpy as np
import scipy.stats as st
import matplotlib.pyplot as plt
import LFPy
import neuron
import sys
import zipfile
import ssl
from os.path import join
from matplotlib.collections import PolyCollection
from LFPy.inputgenerators import get_activation_times_from_distribution
    from urllib2 import urlopen
    from urllib.request import urlopen
from warnings import warn
from mpi4py import MPI

        self.LFP = COMM.reduce(self.LFP)  # LFP is None on all but RANK 0

        # collect all simulation results on RANK 0, including single cell LFP
        if RANK == 0:
            for i in range(1, SIZE):
                result = COMM.recv(
                    source=MPI.ANY_SOURCE)  # receive from ANY rank
                self.results.update(result)  # collect
        else:
            COMM.send(self.results, dest=0)  # send to RANK 0
            self.results = None  # results only exist on RANK 0

        COMM.Barrier()  # sync MPI threads

    def distribute_cellsims(self):
        '''Will distribute and run cell simulations across ranks'''
        # start unique cell simulation on every RANK,
        # and store the electrode and cell objects in dicts indexed by
        # cellindex
        results = {}
        for cellindex in range(self.POPULATION_SIZE):
            if divmod(cellindex, SIZE)[1] == RANK:
                results.update({cellindex: self.cellsim(cellindex)})
        return results

    def drawRandSpikeTimes(self):
        '''draw and distribute some spike times for each cell in population'''
        if RANK == 0:
            RandSpikeTimes = []
            for cellindex in range(self.POPULATION_SIZE):

                sptimes = get_activation_times_from_distribution(