Skip to content

Commit

Permalink
Merge pull request #27 from eth-cscs/RemotecontextRefactoring
Browse files Browse the repository at this point in the history
Remotecontext refactoring
  • Loading branch information
mschoengens authored Oct 20, 2017
2 parents e230b5b + ff3d6cb commit 8fba70d
Show file tree
Hide file tree
Showing 12 changed files with 1,502 additions and 1,150 deletions.
42 changes: 21 additions & 21 deletions abcpy/approx_lhd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@

class Approx_likelihood(metaclass = ABCMeta):
"""This abstract base class defines the approximate likelihood
function. To approximate the likelihood function at a parameter value given observed dataset,
we need to pass a dataset simulated from model set at the parameter value and the observed dataset.
function. To approximate the likelihood function at a parameter value given observed data set,
we need to pass a data set simulated from model set at the parameter value and the observed data set.
"""

@abstractmethod
def __init__(self, statistics_calc):
""" The constructor of a sub-class must accept a non-optional statistics
calculator, which is stored to self.statistics_calc.
"""
The constructor of a sub-class must accept a non-optional statistics
calculator, which is stored to self.statistics_calc.
Parameters
----------
statistics_calc : abcpy.stasistics.Statistics
Statistics extractor object that conforms to the Statistics class.
Parameters
----------
statistics_calc : abcpy.stasistics.Statistics
Statistics extractor object that conforms to the Statistics class.
"""

raise NotImplemented

@abstractmethod
def likelihood(y_obs, y_sim):
"""To be overwritten by any sub-class: should compute the approximate likelihood
value given the observed dataset y_obs and dataset y_sim simulated from
value given the observed data set y_obs and the data set y_sim simulated from
model set at the parameter value.
Parameters
Expand All @@ -46,7 +47,7 @@ def likelihood(y_obs, y_sim):


class SynLiklihood(Approx_likelihood):
"""This class implements the aproximate likelihood function which computes the pproximate
"""This class implements the approximate likelihood function which computes the approximate
likelihood using the synthetic likelihood approach described in Wood [1].
For synthetic likelihood approximation, we compute the robust precision matrix using Ledoit and Wolf's [2]
method.
Expand Down Expand Up @@ -92,8 +93,8 @@ def likelihood(self, y_obs, y_sim):


class PenLogReg(Approx_likelihood):
"""This class implements the aproximate likelihood function which computes the pproximate
likelihood upto a constant using penalized logistic regression described in
"""This class implements the approximate likelihood function which computes the approximate
likelihood up to a constant using penalized logistic regression described in
Dutta et. al. [1]. It takes one additional function handler defining the
true model and two additional parameters n_folds and n_simulate correspondingly defining number
of folds used to estimate prediction error using cross-validation and the number
Expand All @@ -106,10 +107,8 @@ class PenLogReg(Approx_likelihood):
[2] Friedman, J., Hastie, T., and Tibshirani, R. (2010). Regularization
paths for generalized linear models via coordinate descent. Journal of Statistical
Software, 33(1), 1–22.
"""
def __init__(self, statistics_calc, model, n_simulate, n_folds=10, max_iter = 100000, seed = None):
"""
Software, 33(1), 1–22.
Parameters
----------
statistics_calc : abcpy.stasistics.Statistics
Expand All @@ -124,17 +123,18 @@ def __init__(self, statistics_calc, model, n_simulate, n_folds=10, max_iter = 10
Maximum passes over the data. The default is 100000.
seed: int, optional
Seed for the random number generator. The used glmnet solver is not
deterministic, this seed is used for determining the cv folds. The default value is
deterministic, this seed is used for determining the cv folds. The default value is
None.
"""

"""
def __init__(self, statistics_calc, model, n_simulate, n_folds=10, max_iter = 100000, seed = None):

self.model = model
self.statistics_calc = statistics_calc
self.n_folds = n_folds
self.n_simulate = n_simulate
self.seed = seed
self.max_iter = max_iter
# Simulate reference data and extract summary statistics from the reffernce data
# Simulate reference data and extract summary statistics from the reference data
self.ref_data_stat = self._simulate_ref_data()


Expand Down Expand Up @@ -164,7 +164,7 @@ def likelihood(self, y_obs, y_sim):

def _simulate_ref_data(self):
"""
Simulate the reference dataset. This code is run at the initializtion of
Simulate the reference data set. This code is run at the initialization of
Penlogreg
"""

Expand Down
14 changes: 7 additions & 7 deletions abcpy/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def parallelize(self, list):
A reference object that represents the parallelized list
"""

raise NotImplemented
raise NotImplementedError


@abstractmethod
Expand All @@ -49,7 +49,7 @@ def broadcast(self, object):
A reference to the broadcasted object
"""

raise NotImplemented
raise NotImplementedError


@abstractmethod
Expand All @@ -72,7 +72,7 @@ def map(self, func, pds):
a new parallel data set that contains the result of the map
"""

raise NotImplemented
raise NotImplementedError


@abstractmethod
Expand All @@ -91,7 +91,7 @@ def collect(self, pds):
all elements of pds as a list
"""

raise NotImplemented
raise NotImplementedError


class PDS:
Expand All @@ -101,7 +101,7 @@ class PDS:

@abstractmethod
def __init__(self):
raise NotImplemented
raise NotImplementedError


class BDS:
Expand All @@ -111,15 +111,15 @@ class BDS:

@abstractmethod
def __init__(self):
raise NotImplemented
raise NotImplementedError


@abstractmethod
def value(self):
"""
This method should return the actual object that the broadcast data set represents.
"""
raise NotImplemented
raise NotImplementedError


class BackendDummy(Backend):
Expand Down
32 changes: 2 additions & 30 deletions abcpy/backends/mpi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import cloudpickle
import pickle

from mpi4py import MPI
from abcpy.backends import Backend, PDS, BDS
Expand Down Expand Up @@ -54,7 +55,7 @@ def __command_slaves(self, command, data):
elif command == self.OP_MAP:
#In map we receive data as (pds_id,pds_id_new,func)
#Use cloudpickle to dump the function into a string.
function_packed = self.__sanitize_and_pack_func(data[2])
function_packed = cloudpickle.dumps(data[2], pickle.HIGHEST_PROTOCOL)
data_packet = (command, data[0], data[1], function_packed)

elif command == self.OP_BROADCAST:
Expand All @@ -74,35 +75,6 @@ def __command_slaves(self, command, data):
_ = self.comm.bcast(data_packet, root=0)


def __sanitize_and_pack_func(self, func):
"""
Prevents the function from packing the backend by temporarily
setting it to another variable and then uses cloudpickle
to pack it into a string to be sent.
Parameters
----------
func: Python Function
The function we are supposed to pack while sending it along to the slaves
during the map function
Returns
-------
Returns a string of the function packed by cloudpickle
"""

#Set the backend to None to prevent it from being packed
globals()['backend'] = {}

function_packed = cloudpickle.dumps(func)

#Reset the backend to self after it's been packed
globals()['backend'] = self

return function_packed


def __generate_new_pds_id(self):
"""
This method generates a new pds_id to associate a PDS with it's remote counterpart
Expand Down
8 changes: 4 additions & 4 deletions abcpy/distances.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, statistics_calc):
Statistics extractor object that conforms to the Statistics class.
"""

raise NotImplemented
raise NotImplementedError


@abstractmethod
Expand Down Expand Up @@ -58,11 +58,11 @@ def distance(d1, d2):
The distance between the two input data sets.
"""

raise NotImplemented
raise NotImplementedError


@abstractmethod
def dist_max():
def dist_max(self):
"""To be overwritten by sub-class: should return maximum possible value of the
desired distance function.
Expand All @@ -76,7 +76,7 @@ def dist_max():
The maximal possible value of the desired distance function.
"""

raise NotImplemented
raise NotImplementedError


def _calculate_summary_stat(self,d1,d2):
Expand Down
12 changes: 6 additions & 6 deletions abcpy/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ def set_parameters(self, params):
Parameters
----------
theta: list
params: list
Contains all the distributions parameters.
"""

raise NotImplemented
raise NotImplementedError



Expand All @@ -39,7 +39,7 @@ def reseed(self, seed):
"""

raise NotImplemented
raise NotImplementedError



Expand All @@ -58,7 +58,7 @@ def sample(self, k):
"""

raise NotImplemented
raise NotImplementedError


@abstractmethod
Expand All @@ -77,7 +77,7 @@ def pdf(self, x):
"""

raise NotImplemented
raise NotImplementedError


class MultiNormal(Distribution):
Expand Down Expand Up @@ -130,7 +130,7 @@ class Uniform(Distribution):
"""
This class implements a p-dimensional uniform Prior distribution in a closed interval.
"""
def __init__(self, lb : np.ndarray, ub : np.ndarray, seed=None):
def __init__(self, lb, ub, seed=None):
"""
Defines the upper and lower bounds of a p-dimensional uniform Prior distribution in a closed interval.
Expand Down
Loading

0 comments on commit 8fba70d

Please sign in to comment.