forked from glumpy/glumpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quantitative_scale.py
130 lines (90 loc) · 3.37 KB
/
quantitative_scale.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -----------------------------------------------------------------------------
# Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved.
# Distributed under the (new) BSD License.
# -----------------------------------------------------------------------------
"""
Abstract quantitative scale
Scales are functions that map from an input domain to an output
range. Quantitative scales have a continuous domain, such as the set of real
numbers, or dates. There are also ordinal scales, which have a discrete
domain, such as a set of names or categories.
"""
import numpy as np
from glumpy import library
from . transform import Transform
class QuantitativeScale(Transform):
"""
Abstract quantitative scale.
The transform is connected to the following events:
* ``on_attach``: Transform initialization
"""
aliases = { }
def __init__(self, code, *args, **kwargs):
"""
Initialize the transform.
"""
self._clamp = False
self._discard = True
self._domain = np.array([-1,+1], dtype=np.float32)
self._range = np.array([-1,+1], dtype=np.float32)
self.process_kwargs(**kwargs)
Transform.__init__(self, code, *args, **kwargs)
def process_kwargs(self, **kwargs):
self._domain = Transform._get_kwarg("domain", kwargs, self._domain)
self._range = Transform._get_kwarg("range", kwargs, self._range)
self._clamp = Transform._get_kwarg("clamp", kwargs, self._clamp)
self._discard = Transform._get_kwarg("discard", kwargs, self._discard)
self._domain = np.asarray(self._domain, dtype=np.float32)
self._range = np.asarray(self._range, dtype=np.float32)
Transform.process_kwargs(self, **kwargs)
@property
def domain(self):
""" Input domain """
return self._domain
@domain.setter
def domain(self, value):
""" Input domain """
self._domain = np.asarray(value,dtype=np.float32)
if self.is_attached:
self["domain"] = self._process_domain()
@property
def range(self):
""" Output range """
return self._range
@range.setter
def range(self, value):
""" Output range """
self._range = np.asarray(value, dtype=np.float32)
if self.is_attached:
self["range"] = self._process_range()
@property
def clamp(self):
""" Whether to clamp value when out of range """
return self._clamp
@clamp.setter
def clamp(self, value):
""" Whether to clamp value when out of range """
self._clamp = value
if self.is_attached:
self["clamp"] = self._clamp
@property
def discard(self):
""" Whether to discard value when out of range """
return self._discard
@discard.setter
def discard(self, value):
""" Whether to discard value when out of range """
self._discard = value
if self.is_attached:
self["discard"] = self._discard
def _process_range(self):
# To be overridden
return self._range
def _process_domain(self):
# To be overridden
return self._domain
def on_attach(self, program):
self["discard"] = self._discard
self["clamp"] = self._clamp
self["range"] = self._process_range()
self["domain"] = self._process_domain()