Skip to content

opts

CmapOpts

Bases: OptsBase

Source code in pyhdx/web/opts.py
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

apply(data)

apply cmap / norm to data (pd series or df)

Source code in pyhdx/web/opts.py
188
189
190
191
192
193
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)

OptsBase

Bases: Parameterized

Source code in pyhdx/web/opts.py
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
class OptsBase(param.Parameterized):
    _type = None

    updated = param.Event()

    def __init__(self, **params):
        super().__init__(**params)
        self._excluded_from_opts = ["name"]  # todo remove this and opts property

        self.widgets = self.generate_widgets()

    @property
    def panel(self):
        return pn.Column(*self.widgets.values())

    @property
    def opts(self):
        opts = {
            name: self.param[name] for name in self.param if name not in self._excluded_from_opts
        }
        return opts

    def generate_widgets(self, **kwargs):
        """returns a dict with keys parameter names and values default mapped widgets"""
        # todo base class?

        names = [
            p
            for p in self.param
            if self.param[p].precedence is None or self.param[p].precedence > 1
        ]
        widgets = pn.Param(self.param, show_name=False, show_labels=True, widgets=kwargs)

        return {k: v for k, v in zip(names[1:], widgets)}

generate_widgets(**kwargs)

returns a dict with keys parameter names and values default mapped widgets

Source code in pyhdx/web/opts.py
36
37
38
39
40
41
42
43
44
45
46
47
def generate_widgets(self, **kwargs):
    """returns a dict with keys parameter names and values default mapped widgets"""
    # todo base class?

    names = [
        p
        for p in self.param
        if self.param[p].precedence is None or self.param[p].precedence > 1
    ]
    widgets = pn.Param(self.param, show_name=False, show_labels=True, widgets=kwargs)

    return {k: v for k, v in zip(names[1:], widgets)}