114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197 | class CmapOpts(OptsBase):
_type = "cmap"
cmap = param.ClassSelector(default=None, class_=Colormap)
norm = param.ClassSelector(default=None, class_=Normalize)
# the stored norm here is the unscaled one
# scale factor is applied to clim and norm_scaled
clim = param.Tuple((0.0, 1.0), length=2)
sclf = param.Number(1.0, doc="scaling factor to apply") # curent: 1e+3
field = param.String(doc="field on which cmap works")
def __init__(self, rename=True, invert=False, **params):
# todo from_spec constructor method for this kind of logic
cmap = params.pop("cmap", None)
cmap = pplt.Colormap(cmap) if cmap else cmap
params["cmap"] = cmap
super().__init__(**params)
self._excluded_from_opts += [
"norm",
"sclf",
] # perhaps use leading underscore to exclude?
if self.cmap is None and self.norm is None and self.field is not None:
cmap, norm = CMAP_NORM_DEFAULTS[self.field]
elif self.field is None:
cmap = pplt.Colormap("viridis")
norm = pplt.Norm("linear", 0.0, 1.0)
self.norm = norm
self._cmap = cmap # unreversed cmap
if rename:
cmap.name = self.field + "_default"
if invert:
cmap = cmap.reversed()
self.cmap = cmap
# self._norm_updated()
# self.cmap = self.cmap.reversed()
@property
def opts(self):
names = ["cmap", "clim"]
opts = {name: self.param[name] for name in names}
return opts
@property
def norm_scaled(self):
norm = copy(self.norm)
norm.vmin *= self.sclf
norm.vmax *= self.sclf
return norm
@norm_scaled.setter
def norm_scaled(self, norm):
_norm = copy(norm)
_norm.vmin /= self.sclf
_norm.vmax /= self.sclf
self.norm = _norm
@param.depends("norm", watch=True)
def _norm_updated(self):
self.clim = self.norm.vmin * self.sclf, self.norm.vmax * self.sclf
# todo invert bool?
# self.clim = self.norm.vmax*self.sclf, self.norm.vmin*self.sclf,
def apply(self, data):
"""apply cmap / norm to data (pd series or df)"""
# norm = copy(self.norm)
# norm.vmin *= self.sclf
# norm.vmax *= self.sclf
return apply_cmap(data, self.cmap, self.norm)
@param.depends("norm", "cmap", watch=True)
def update(self):
self.updated = True
|