<reponame>kahilah/hpc-python
from multiprocessing import Pool, Queue, Process
from pdb import PDB
from numpy import array

def average(chunk):
    '''Calculate the average of multiple coordinates. Returns a tuple of
       (average, weight) where
           average -- average coordinate
           weight  -- no. of coordinates averaged over'''
    return (sum(chunk) / len(chunk), len(chunk))

size = 10
pool = Pool(size)
pdb = PDB('5ire.pdb')
n = 100

# split into tasks
tasks = []
for i in range(0, len(pdb), n):
    chunk = pdb.coordinates[i:i+n].copy()
    tasks.append(chunk)

# submit each task to the pool
results = []
for chunk in tasks:
    res = pool.apply_async(average, [chunk])
    results.append(res)

# wait for all tasks to finish
pool.close()
pool.join()

# collect results
averages = []
weights = []
for res in results:
    avg, w = res.get()
    averages.append(avg)
    weights.append(w)
averages = array(averages)
weights = array(weights, ndmin=2)

# calculate the center of coordinates
origo = sum(averages * weights.T) / len(pdb)
print origo


from multiprocessing import Pool, Queue, Process
from pdb import PDB
from numpy import array

def average(chunk):
    '''Calculate the average of multiple coordinates. Returns a tuple of
       (average, weight) where
           average -- average coordinate
           weight  -- no. of coordinates averaged over'''
    return (sum(chunk) / len(chunk), len(chunk))

size = 10
pool = Pool(size)
pdb = PDB('5ire.pdb')
n = 100

# split into tasks
tasks = []
for i in range(0, len(pdb), n):
    chunk = pdb.coordinates[i:i+n].copy()
    tasks.append(chunk)

# submit each task to the pool
results = []
for chunk in tasks:
    res = pool.apply_async(average, [chunk])
    results.append(res)

# wait for all tasks to finish
pool.close()
pool.join()

# collect results
averages = []
weights = []
for res in results:
    avg, w = res.get()
    averages.append(avg)
    weights.append(w)
averages = array(averages)
weights = array(weights, ndmin=2)

# calculate the center of coordinates
origo = sum(averages * weights.T) / len(pdb)
print origo
Calculate the average of multiple coordinates. Returns a tuple of        (average, weight) where            average -- average coordinate            weight  -- no. of coordinates averaged over # split into tasks # submit each task to the pool # wait for all tasks to finish # collect results # calculate the center of coordinates