Commit 4940efce authored by theos's avatar theos
Browse files

Updated new (operator-)prober classes.

parent 0e03e1f2
......@@ -55,6 +55,8 @@ from spaces import *
from operators import *
from probing import *
from demos import get_demo_dir
#import pyximport; pyximport.install(pyimport = True)
......@@ -2,8 +2,6 @@
import abc
from nifty import about
from prober import Prober
......@@ -11,25 +9,26 @@ class OperatorProber(Prober):
# ---Overwritten properties and methods---
def __init__(self, operator, random='pm1', distribution_strategy=None,
compute_variance=False):
def __init__(self, operator, probe_count=8, random_type='pm1',
distribution_strategy=None, compute_variance=False):
super(OperatorProber, self).__init__(
random=random,
probe_count=probe_count,
random_type=random_type,
distribution_strategy=distribution_strategy,
compute_variance=compute_variance)
self.operator = operator
self._operator = operator
# ---Mandatory properties and methods---
@abc.abstractproperty
@property
def domain(self):
if self.is_inverse:
return self.operator.target
else:
return self.operator.domain
@abc.abstractproperty
@property
def field_type(self):
if self.is_inverse:
return self.operator.field_type_target
......@@ -39,21 +38,13 @@ class OperatorProber(Prober):
# ---Added properties and methods---
@abc.abstractproperty
def valid_operator_class(self):
def is_inverse(self):
raise NotImplementedError
@property
def is_inverse(self):
@abc.abstractproperty
def valid_operator_class(self):
raise NotImplementedError
@property
def operator(self):
return self._operator
@operator.setter
def operator(self, operator):
if not isinstance(operator, self.valid_operator_class):
raise ValueError(about._errors.cstring(
"ERROR: The given operator is not an instance of the "
"LinearOperator class."))
self._operator = operator
......@@ -2,6 +2,8 @@
import abc
import numpy as np
from nifty.config import about,\
nifty_configuration as gc
......@@ -13,21 +15,21 @@ from d2o import STRATEGIES as DISTRIBUTION_STRATEGIES
class Prober(object):
__metaclass__ = abc.ABCMeta
def __init__(self, random='pm1', distribution_strategy=None,
compute_variance=False):
def __init__(self, probe_count=8, random_type='pm1',
distribution_strategy=None, compute_variance=False):
self.probe_count = probe_count
self.random = random
self.random_type = random_type
if distribution_strategy is None:
distribution_strategy = gc['default_distribution_strategy']
elif distribution_strategy not in DISTRIBUTION_STRATEGIES['global']:
raise ValueError(about._errors.cstring(
"ERROR: distribution_strategy must be a global-type "
"strategy."))
self._distribution_strategy = distribution_strategy
self.distribution_strategy = distribution_strategy
self.compute_variance = bool(compute_variance)
# ---Properties---
@abc.abstractproperty
def domain(self):
raise NotImplemented
......@@ -36,50 +38,89 @@ class Prober(object):
def field_type(self):
raise NotImplemented
@abc.abstractmethod
def probe(self):
""" controls the generation, evaluation and finalization of probes """
raise NotImplementedError
@abc.abstractmethod
def probing_function(self, probe):
""" processes probes """
raise NotImplementedError
@property
def distribution_strategy(self):
return self._distribution_strategy
@distribution_strategy.setter
def distribution_strategy(self, distribution_strategy):
distribution_strategy = str(distribution_strategy)
if distribution_strategy not in DISTRIBUTION_STRATEGIES['global']:
raise ValueError(about._errors.cstring(
"ERROR: distribution_strategy must be a global-type "
"strategy."))
self._distribution_strategy = distribution_strategy
@property
def random(self):
return self._random
def probe_count(self):
return self._probe_count
@random.setter
def random(self, random):
if random not in ["pm1", "normal"]:
@probe_count.setter
def probe_count(self, probe_count):
self._probe_count = int(probe_count)
@property
def random_type(self):
return self._random_type
@random_type.setter
def random_type(self, random_type):
if random_type not in ["pm1", "normal"]:
raise ValueError(about._errors.cstring(
"ERROR: unsupported random key '" + str(random) + "'."))
"ERROR: unsupported random type: '" + str(random) + "'."))
else:
self._random = random
self._random_type = random_type
# ---Probing methods---
def probing_run(self):
""" controls the generation, evaluation and finalization of probes """
sum_of_probes = 0
sum_of_squares = 0
for index in xrange(self.probe_count):
current_probe = self.get_probe(index)
pre_result = self.process_probe(current_probe, index)
result = self.finish_probe(pre_result)
sum_of_probes += result
if self.compute_variance:
sum_of_squares += result.conjugate() * result
mean_and_variance = self.finalize(sum_of_probes, sum_of_squares)
return mean_and_variance
def get_probe(self, index):
""" layer of abstraction for potential probe-caching """
return self.generate_probe()
def generate_probe(self):
""" a random-probe generator """
f = Field.from_random(random=self.random,
f = Field.from_random(random_type=self.random_type,
domain=self.domain,
field_type=self.field_type,
distribution_strategy=self.distribution_strategy)
return f
uid = np.random.randint(1e18)
return (uid, f)
def get_probe(self, index=None):
""" layer of abstraction for potential probe-caching """
return self.generate_probe()
def process_probe(self, probe, index):
return self.evaluate_probe(probe)
@abc.abstractmethod
def evaluate_probe(self, probe):
""" processes a probe """
raise NotImplementedError
def finish_probe(self, probe):
return probe
def finalize(self, probes_count, sum_of_probes, sum_of_squares):
mean_of_probes = sum_of_probes/probes_count
def finalize(self, sum_of_probes, sum_of_squares):
probe_count = self.probe_count
mean_of_probes = sum_of_probes/probe_count
if self.compute_variance:
# variance = 1/(n-1) (sum(x^2) - 1/n*sum(x)^2)
variance = ((sum_of_squares - sum_of_probes*mean_of_probes) /
(probes_count-1))
(probe_count-1))
else:
variance = None
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment