Ejemplo n.º 1
0
def retrieveLikelihoods(gene_rhos, back_rhos, off_rhos, off_rate, like_fun,
                        nums, gene_span, I_step):

    # Checks if single processor
    if nums == 1:
        single = True
    else:
        single = False

    # Calculates gaussian kernel estimations of the background distributions
    back_dist = st.gaussian_kde(back_rhos)
    off_dist = st.gaussian_kde(off_rhos)

    # Checks and creates parallel environment
    try:
        if not single:
            import pp

    except ImportError:
        print(
            'Parallel Python package pp not found. Defaulting to single core')
        single = True

    if not single:

        # Initiates parallel server
        job_server = pp.Server(nums)
        fn = pp.Template(job_server, trial, (
            like_fun,
            findInterval,
        ), ("numpy", "math", "scipy.misc"))

    # Single core version
    if single:
        geneI, geneL, geneInterval, geneDist = trial(gene_rhos, back_dist,
                                                     off_dist, off_rate,
                                                     like_fun, gene_span,
                                                     I_step)

    # Parallel version
    if not single:

        geneI = {}
        geneL = {}
        geneInterval = {}
        geneDist = {}

        GeneRhosSplit = []
        keys = gene_rhos.keys()

        # Determines the number of genes in each task
        n = int(len(keys) / nums) + 1
        keysSplit = [keys[i:i + n] for i in range(0, len(keys), n)]

        # Splits dictionary
        for keyList in keysSplit:
            dictChunk = {}

            for key in keyList:
                dictChunk[key] = gene_rhos[key]

            GeneRhosSplit.append(dictChunk)

# Submits individual jobs for each gene list
        jobs = []
        for splits in GeneRhosSplit:
            jobs.append(
                fn.submit(splits, back_dist, off_dist, off_rate, like_fun,
                          gene_span, I_step))

# Retrieves results from individual jobs
        for job in jobs:
            val = job()

            try:
                geneIi, geneLi, geneIntervali, geneDisti = val

            # Catches subprocess errors
            except TypeError:
                sys.exit('Subprocess failed')

            # Saves results
            geneI.update(geneIi)
            geneL.update(geneLi)
            geneInterval.update(geneIntervali)
            geneDist.update(geneDisti)

    return geneI, geneL, geneInterval, geneDist
Ejemplo n.º 2
0
def retrievePvals(gene_rhos, back_rhos, nums):

    # Checks if parallel process requested
    if nums == 1:
        single = True
    else:
        single = False

    # Checks and creates parallel environment
    try:
        if not single:
            import pp

    except ImportError:
        print(
            'Parallel Python package pp not found. Defaulting to single core')
        single = True

    # Initiates parallel server
    if not single:
        job_server = pp.Server(nums)
        fn = pp.Template(job_server, calculatePvals, (),
                         ("numpy", "scipy.stats.mstats"))

    # Single core version
    if single:
        rhoMW, rhoKS = calculatePvals(gene_rhos, back_rhos)

    # Parallel version
    if not single:

        rhoMW = {}
        rhoKS = {}

        GeneRhosSplit = []
        keys = gene_rhos.keys()

        # Determines the number of genes in each task
        n = int(len(keys) / nums) + 1
        keysSplit = [keys[i:i + n] for i in range(0, len(keys), n)]

        # Splits dictionary
        for keyList in keysSplit:

            dictChunk = {}
            for key in keyList:
                dictChunk[key] = gene_rhos[key]

            GeneRhosSplit.append(dictChunk)

# Submits individual jobs for each gene list
        jobs = []
        for splits in GeneRhosSplit:
            jobs.append(fn.submit(splits, back_rhos))

# Retrieves results from individual jobs
        for job in jobs:
            val = job()

            try:
                rhoMWi, rhoKSi = val

            # Catches subprocess errors
            except TypeError:
                sys.exit('Subprocess failed')

            # Saves results
            rhoMW.update(rhoMWi)
            rhoKS.update(rhoKSi)

    return rhoMW, rhoKS
Ejemplo n.º 3
0
def parallel_evaluation_pp(candidates, args):
    """Evaluate the candidates in parallel using Parallel Python.

    This function allows parallel evaluation of candidate solutions.
    It uses the `Parallel Python <http://www.parallelpython.com>`_  (pp)
    library to accomplish the parallelization. This library must already 
    be installed in order to use this function. The function assigns the 
    evaluation of each candidate to its own job, all of which are then 
    distributed to the available processing units.
    
    .. note::
    
       All arguments to the evaluation function must be pickleable.
       Those that are not will not be sent through the ``args`` variable
       and will be unavailable to your function.
       
    .. Arguments:
       candidates -- the candidate solutions
       args -- a dictionary of keyword arguments

    Required keyword arguments in args:
    
    - *pp_evaluator* -- actual evaluation function to be used (This function
      should have the same signature as any other inspyred evaluation function.)

    Optional keyword arguments in args:
    
    - *pp_dependencies* -- tuple of functional dependencies of the serial 
      evaluator (default ())
    - *pp_modules* -- tuple of modules that must be imported for the 
      functional dependencies (default ())
    - *pp_servers* -- tuple of servers (on a cluster) that will be used 
      for parallel processing (default ("*",))
    - *pp_secret* -- string representing the secret key needed to authenticate
      on a worker node (default "inspyred")
    - *pp_nprocs* -- integer representing the number of worker processes to
      start on the local machine (default "autodetect", which sets it to the
      number of processors in the system)
      
    For more information about these arguments, please consult the
    documentation for `Parallel Python <http://www.parallelpython.com>`_.
    
    """
    import pp
    logger = args['_ec'].logger

    try:
        evaluator = args['pp_evaluator']
    except KeyError:
        logger.error(
            'parallel_evaluation_pp requires \'pp_evaluator\' be defined in the keyword arguments list'
        )
        raise
    secret_key = args.setdefault('pp_secret', 'inspyred')
    try:
        job_server = args['_pp_job_server']
    except KeyError:
        pp_servers = args.get('pp_servers', ("*", ))
        pp_nprocs = args.get('pp_nprocs', 'autodetect')
        job_server = pp.Server(ncpus=pp_nprocs,
                               ppservers=pp_servers,
                               secret=secret_key)
        args['_pp_job_server'] = job_server
    pp_depends = args.setdefault('pp_dependencies', ())
    pp_modules = args.setdefault('pp_modules', ())

    pickled_args = {}
    for key in args:
        try:
            pickle.dumps(args[key])
            pickled_args[key] = args[key]
        except (TypeError, pickle.PickleError, pickle.PicklingError):
            logger.debug(
                'unable to pickle args parameter {0} in parallel_evaluation_pp'
                .format(key))
            pass

    func_template = pp.Template(job_server, evaluator, pp_depends, pp_modules)
    jobs = [func_template.submit([c], pickled_args) for c in candidates]

    fitness = []
    for i, job in enumerate(jobs):
        r = job()
        try:
            fitness.append(r[0])
        except TypeError:
            logger.warning(
                'parallel_evaluation_pp generated an invalid fitness for candidate {0}'
                .format(candidates[i]))
            fitness.append(None)
    return fitness
Ejemplo n.º 4
0
def sum_primes(n):
	# Calculates sum of all primes until given integer n
	return sum([x for x in xrange(2,n+1) if isprime(x)])



if len(sys.argv) > 1:
	ncpus = int(sys.argv[1])
	# Creates jobserver with ncpus workers
	job_server = pp.Server(ncpus)
else:
	# Creates jobserver with automatically detected number of workers
	job_server = pp.Server()

print "Starting pp with", job_server.get_ncpus(), "workers"

# The following submits 8 jobs and then retrieves the results
inputs = (1000, 1001, 1002, 1003, 1004, 1005, 1006, 100700)

start_time = time.time()

jobn = pp.Template(job_server, sum_primes, (isprime,), ("math",))

jobs = [(input, jobn.submit(input)) for input in inputs]
for input, job in jobs:
	print "Sum of primes until", input, "is", job()

print "Time elapsed: ", time.time() - start_time, "s"

job_server.print_stats()
Ejemplo n.º 5
0
mut = 10**-4  # Forward Mutation Rate
bmut = 10**-5  # Backward Mutation Rate
wfit, hfit, dfit = 1, 0.95, 0.95  # Fitness for wildtype, heterozygous, and dominant organisms
output = 'output.csv'  # Name of output file
nums = 32  # Number of times to repeat simulation

# To run in command line: move to containing directory, enter command
# python balFinPop.py
# Outputs as comma-seperated values (csv), which can be opened in excel.

################################################################
# Code initiates simulation

job_server = pp.Server()
fn = pp.Template(job_server, trial, (), (), None, (), 'default', globals())

for pop in [100, 500, 1000, 3000, 10000]:
    for ctr in [0, 1]:
        for en in [0, 5, 25, 50, 75]:
            if ctr and en == 0:
                continue
            jobs = []
            for t in range(nums):
                jobs.append(
                    fn.submit(pop, wfit, hfit, dfit, en, ctr, mut, bmut))

            qss = []
            avgs = []

            for job in jobs:
Ejemplo n.º 6
0
import pp
import fortran
from numpy import array, zeros

jobserver = pp.Server(3)


def _normcall2(*args):
    return fortran._ffnet.normcall2(*args)


tnormcall2 = pp.Template(jobserver, _normcall2, modules=('fortran', ))


def _func(*args):
    return fortran._ffnet.func(*args)


tfunc = pp.Template(jobserver, _func, modules=('fortran', ))


def _grad(*args):
    return fortran._ffnet.grad(*args)


tgrad = pp.Template(jobserver, _grad, modules=('fortran', ))


#######################################3
def _divide(inp):
    n = jobserver.get_ncpus()
Ejemplo n.º 7
0
# sum_primes - the function
# (100,) - tuple with arguments for sum_primes
# (isprime,) - tuple with functions on which function sum_primes depends
# ("math",) - tuple with module names which must be imported
#             before sum_primes execution
# Execution starts as soon as one of the workers will become available
#job1 = job_server.submit(sum_primes, (10,), (isprime,), ("math",), callback=callback)

# Retrieves the result calculated by job1
# The value of job1() is the same as sum_primes(100)
# If the job has not been finished yet,
# execution will wait here until result is available
#result = job1()

#print "Sum of primes below 10 is", result

inputs = range(10000000, 10100000)

ajo = pp.Template(job_server, isprime, (), ("math", ))

jobs = [(input, ajo.submit(input)) for input in inputs]

for input, job in jobs:
    if (job() == True):
        print "Number %i is a prime number" % input

print "Run done with ", job_server.get_ncpus(), "workers"
job_server.print_stats()

# Parallel Python Software: http://www.parallelpython.com