transformation_factory.py 2.06 KB
Newer Older
Jait Dixit's avatar
Jait Dixit committed
1
2
3
from nifty.rg import RGSpace
from nifty.lm import GLSpace, HPSpace, LMSpace

Jait Dixit's avatar
Jait Dixit committed
4
5
6
7
8
from rgrgtransformation import RGRGTransformation
from gllmtransformation import GLLMTransformation
from hplmtransformation import HPLMTransformation
from lmgltransformation import LMGLTransformation
from lmhptransformation import LMHPTransformation
Jait Dixit's avatar
Jait Dixit committed
9
10


11
class _TransformationFactory(object):
Jait Dixit's avatar
Jait Dixit committed
12
13
14
15
16
17
18
19
20
21
    """
        Transform factory which generates transform objects
    """

    def __init__(self):
        # cache for storing the transform objects
        self.cache = {}

    def _get_transform(self, domain, codomain, module):
        if isinstance(domain, RGSpace):
22
23
24
25
26
            if isinstance(codomain, RGSpace):
                return RGRGTransformation(domain, codomain, module)
            else:
                raise ValueError('ERROR: incompatible codomain')

27
        elif isinstance(domain, GLSpace):
28
29
30
31
32
            if isinstance(codomain, GLSpace):
                return GLLMTransformation(domain, codomain, module)
            else:
                raise ValueError('ERROR: incompatible codomain')

33
        elif isinstance(domain, HPSpace):
34
35
36
37
38
            if isinstance(codomain, GLSpace):
                return HPLMTransformation(domain, codomain, module)
            else:
                raise ValueError('ERROR: incompatible codomain')

39
40
        elif isinstance(domain, LMSpace):
            if isinstance(codomain, GLSpace):
41
                return LMGLTransformation(domain, codomain, module)
42
            elif isinstance(codomain, HPSpace):
43
                return LMHPTransformation(domain, codomain, module)
44
45
            else:
                raise ValueError('ERROR: incompatible codomain')
46
47
        else:
            raise ValueError('ERROR: unknown domain')
Jait Dixit's avatar
Jait Dixit committed
48
49

    def create(self, domain, codomain, module=None):
theos's avatar
theos committed
50
51
        key = domain.__hash__() ^ ((codomain.__hash__()/111) ^
                                   (module.__hash__())/179)
Jait Dixit's avatar
Jait Dixit committed
52
53
54
55

        if key not in self.cache:
            self.cache[key] = self._get_transform(domain, codomain, module)

56
        return self.cache[key]
57
58
59


TransformationFactory = _TransformationFactory()