Skip to content

plot_actions

Plot specific actions that are used in effects

aggregate_data(plotstate, dff) #

Create aggregated data expressions for heatmap and histogram

Parameters:

Name Type Description Default
plotstate PlotState

plot variables

required
dff DataFrame

filtered dataframe

required

Returns:

Name Type Description
color ndarray

2D aggregated data according to plotstate.bintype

x_edges tuple[ndarray, ndarray, ndarray, list[list[float]]] | tuple[ndarray, ndarray, ndarray]

1D array of x-axis coordinates

y_edges tuple[ndarray, ndarray, ndarray, list[list[float]]] | tuple[ndarray, ndarray, ndarray]

1D array of x-axis coordinates

limits tuple[ndarray, ndarray, ndarray, list[list[float]]] | tuple[ndarray, ndarray, ndarray]

splatted list of x and y axis limits

Raises:

Type Description
ValueError

if no bintype (somehow)

RuntimeError

if binning is too small (stride bug)

AssertionError

if data is invalid (all nan, etc)

Source code in src/sdss_explorer/dashboard/components/views/plot_actions.py
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
def aggregate_data(
    plotstate: PlotState, dff: vx.DataFrame
) -> (tuple[ndarray, ndarray, ndarray, list[list[float]]]
      | tuple[ndarray, ndarray, ndarray]):
    """
    Create aggregated data expressions for heatmap and histogram

    Args:
        plotstate: plot variables
        dff: filtered dataframe

    Returns:
        color (numpy.ndarray): 2D aggregated data according to `plotstate.bintype`
        x_edges: 1D array of x-axis coordinates
        y_edges: 1D array of x-axis coordinates
        limits: splatted list of x and y axis limits

    Raises:
        ValueError: if no bintype (somehow)
        RuntimeError: if binning is too small (stride bug)
        AssertionError: if data is invalid (all nan, etc)

    """
    # check validity
    if plotstate.bintype.value not in plotstate.Lookup["bintypes"]:
        raise ValueError(
            "no assigned bintype for aggregation. bug somewhere in settings.")

    assert len(dff) > 0, "no data in dataframe"

    if plotstate.plottype == "histogram":
        if check_categorical(dff[plotstate.x.value]):
            update_mapping(plotstate, dff, axis="x")
            nbins = len(plotstate.xmapping)
        else:
            nbins = plotstate.nbins.value
        expr = fetch_data(plotstate, dff, axis="x")

        # get bin center and edges; center mainly for us
        if check_categorical(dff[plotstate.x.value]):
            centers = expr.unique(array_type="numpy")
            edges = np.arange(0, expr.nunique() + 1, 1) - 0.5  # offset
        else:
            # try:
            limits = expr.minmax()
            # except Exception:  # stride bug catch
            #    limits = [expr.min()[()], expr.max()[()]]
            edges = dff.bin_edges(expr, limits=limits, shape=nbins)
            centers = dff.bin_centers(expr, limits=limits, shape=nbins)

        # get counts
        if check_categorical(dff[plotstate.x.value]):
            series = expr.value_counts()  # value_counts as in Pandas
            counts = series.values
        else:
            try:
                counts = dff.count(binby=expr,
                                   shape=nbins,
                                   delay=True,
                                   array_type="numpy")
                dff.execute()
                counts = counts.get().flatten()
            except Exception:
                temp = dff.extract()
                expr = temp[plotstate.x.value]
                counts = temp.count(binby=expr,
                                    shape=nbins,
                                    delay=True,
                                    array_type="numpy")
                temp.execute()
                counts = counts.get().flatten()

        return centers, edges, counts

    elif plotstate.plottype == "heatmap":
        # update mappings if needed
        if check_categorical(dff[plotstate.x.value]):
            update_mapping(plotstate, dff, axis="x")
        if check_categorical(dff[plotstate.y.value]):
            update_mapping(plotstate, dff, axis="y")
        assert not check_categorical(dff[plotstate.color.value]), (
            "cannot perform aggregations on categorical data")

        expr = (
            fetch_data(plotstate, dff, axis="x"),
            fetch_data(plotstate, dff, axis="y"),
        )
        expr_c = dff[plotstate.color.value]
        bintype = plotstate.bintype.value

        # get bin widths and props
        # NOTE: ideally this method is delayed; but its not supported to send promises
        edges = [[], []]
        shape = [plotstate.nbins.value, plotstate.nbins.value]
        widths = [1, 1]
        limits = [None, None]
        for i in range(2):
            col = (plotstate.x.value, plotstate.y.value)[i]
            if check_categorical(dff[col]):
                edges[i] = expr[i].unique(array_type="numpy")
                shape[i] = len(edges[i])
                limits[i] = [0, expr[i].nunique()]
                widths[i] = 1
            else:
                # try:
                limit = expr[i].minmax()
                # except Exception:  # stride bug catch
                #    limit = [expr[i].min()[()], expr[i].max()[()]]
                edges[i] = dff.bin_centers(
                    expression=expr[i],
                    limits=limit,
                    shape=plotstate.nbins.value,
                )
                limits[i] = limit
                shape[i] = plotstate.nbins.value
                widths[i] = (limit[1] - limit[0]) / shape[i]

        # pull the aggregation function pointer and call it with our kwargs
        if bintype == "median":
            aggFunc = getattr(dff, "median_approx")  # median under diff name
        else:
            aggFunc = getattr(dff, bintype)
        try:
            color = aggFunc(
                expression=expr_c if bintype != "count" else None,
                binby=expr,
                limits=limits,
                shape=shape,
                delay=True,
            )
            dff.execute()
            color = color.get()
        except Exception:
            # recompile exprs and expr c if chunk failed
            temp = dff.extract()
            expr = (
                fetch_data(plotstate, temp, axis="x"),
                fetch_data(plotstate, temp, axis="y"),
            )
            expr_c = temp[plotstate.color.value]
            color = aggFunc(
                expression=expr_c if bintype != "count" else None,
                binby=expr,
                limits=limits,
                shape=shape,
                delay=True,
            )
            dff.execute()
            color = color.get()

        # convert because it breaks
        if bintype == "count":
            color = color.astype("float")
            color[color == 0] = np.nan
        color[np.abs(color) == np.inf] = np.nan

        # scale if needed
        if plotstate.logcolor.value:
            color = np.log10(color)  # only take log if count
        assert not np.all(np.isnan(color)), "all nan"

        return color, edges[0], edges[1], widths

change_formatter(plotstate, fig_model, dff, axis='x', color=None) #

Updates the formatter on the plot, changing to a categorical type if needed.

Parameters:

Name Type Description Default
plotstate PlotState

plot variables

required
fig_model Plot

figure object

required
axis str

axis to update. any of 'x', 'y', or 'color'

'x'
dff DataFrame

filtered dataframe

required
Source code in src/sdss_explorer/dashboard/components/views/plot_actions.py
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
def change_formatter(
    plotstate: PlotState,
    fig_model: Plot,
    dff: vx.DataFrame,
    axis: str = "x",
    color: Optional[np.ndarray] = None,
) -> None:
    """
    Updates the formatter on the plot, changing to a categorical type if needed.

    Args:
        plotstate: plot variables
        fig_model: figure object
        axis: axis to update. any of 'x', 'y', or 'color'
        dff: filtered dataframe
    """
    assert axis in ("x", "y", "color"), f"expected axis x or y but got {axis}"

    col = getattr(plotstate, axis).value  # column name
    log = getattr(plotstate, f"log{axis}").value  # whether log
    mapping = getattr(plotstate, f"{axis}mapping")  # categorical datamap

    # gangsta one liner
    loc = ("below" if axis == "x" else "left") if axis != "color" else "right"

    # get place and set it
    ax = getattr(fig_model, loc)[0]
    if (plotstate.plottype == "histogram") and (axis == "y"):
        formatter = BasicTickFormatter()
    else:
        if check_categorical(col):
            formatter = generate_categorical_tick_formatter(mapping)
        else:
            formatter = (LogTickFormatter() if
                         (log and (axis != "color")) else BasicTickFormatter())
    if axis == "color":
        if check_categorical(col):
            ax.ticker.ticks = [v for v in mapping.values()]
            ax.major_label_overrides = {v: k for k, v in mapping.items()}
        else:
            low, high = _calculate_color_range(plotstate, dff=dff, color=color)
            ax.ticker.ticks = calculate_colorbar_ticks(low, high)
            ax.major_label_overrides = {0: "0"}
    else:
        ax.formatter = formatter

fetch_data(plotstate, dff, axis='x') #

Helper function to get data and apply mappings if necessary

Parameters:

Name Type Description Default
plotstate PlotState

plot variables

required
dff DataFrame

filtered dataframe

required
axis str

axis to fetch for. either 'x', 'y', or 'color'.

'x'

Returns:

Type Description
Expression

Expression of mapped categorical data or raw expression.

Raises:

Type Description
AssertionError

if unexpected axis

ValueError

if all nan in color

Source code in src/sdss_explorer/dashboard/components/views/plot_actions.py
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
def fetch_data(plotstate: PlotState,
               dff: vx.DataFrame,
               axis: str = "x") -> vx.Expression:
    """Helper function to get data and apply mappings if necessary

    Args:
        plotstate: plot variables
        dff: filtered dataframe
        axis: axis to fetch for. either 'x', 'y', or 'color'.

    Returns:
        Expression of mapped categorical data or raw expression.

    Raises:
        AssertionError: if unexpected axis
        ValueError: if all nan in color

    """
    assert axis in ("x", "y", "color"), f"expected axis x or y but got {axis}"
    col = getattr(plotstate, axis).value  # column name

    if check_categorical(col):
        update_mapping(plotstate, dff, axis=axis)
        mapping = getattr(plotstate, f"{axis}mapping")  # categorical datamap
        colData = dff[col].map(mapping)
    else:
        if (axis == "color") and plotstate.logcolor.value:
            colData = np.log10(dff[col])
            # check if the update will make all nan, and set accordingly
            # try:
            #    test = colData.values
            #    try:  # pyarrow array instance check didnt work; just do this
            #        test = test.to_numpy()
            #    except Exception:
            #        pass
            #    test[np.abs(test) == np.inf] = np.nan
            #    assert not np.all(np.isnan(test))
            # except Exception:
            #    raise ValueError(
            #        "taking log of color gives no data, not updating.")
        else:
            colData = dff[col]
    return colData

reset_range(plotstate, fig_model, dff, axis='x') #

Resets given axis range on a figure model.

Parameters:

Name Type Description Default
plotstate PlotState

plot variables

required
fig_model Plot

figure object

required
dff DataFrame

filtered dataframe

required
axis str

axis to update for. 'x', 'y', or 'color'.

'x'
Source code in src/sdss_explorer/dashboard/components/views/plot_actions.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def reset_range(plotstate: PlotState,
                fig_model: Plot,
                dff: vx.DataFrame,
                axis: str = "x"):
    """Resets given axis range on a figure model.

    Args:
        plotstate: plot variables
        fig_model: figure object
        dff: filtered dataframe
        axis: axis to update for. 'x', 'y', or 'color'.
    """
    assert axis in ("x", "y", "color"), f"expected axis x or y but got {axis}"
    if dff is not None:
        if (plotstate.plottype == "histogram") and (axis == "y"):
            _reset_histogram_yrange(plotstate, fig_model, dff)
        elif axis == "color":
            update_color_mapper(plotstate, fig_model, dff)
        else:
            # NOTE: flip/log is automatically by the calculation function
            datarange = getattr(fig_model, f"{axis}_range")
            newrange = calculate_range(plotstate, dff, axis=axis)
            datarange.update(start=newrange[0], end=newrange[1])

update_axis(plotstate, fig_model, dff, axis='x') #

Direct and complete axis update.

Parameters:

Name Type Description Default
plotstate PlotState

plot variables

required
fig_model Plot

figure object

required
dff DataFrame

filtered dataframe

required
Source code in src/sdss_explorer/dashboard/components/views/plot_actions.py
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
def update_axis(
    plotstate: PlotState,
    fig_model: Plot,
    dff: vx.DataFrame,
    axis: str = "x",
):
    """Direct and complete axis update.

    Args:
        plotstate: plot variables
        fig_model: figure object
        dff: filtered dataframe
    """
    # get all attributes of plotstate + fig_model
    assert axis in ("x", "y", "color"), (
        f"expected axis 'x','y', or 'color' but got {axis}")
    try:
        colData = fetch_data(plotstate, dff, axis=axis)
    except Exception as e:
        logger.debug("fetch update" + str(e))
        Alert.update(f"Color update failed! {e}", color="warning")
        return
    with fig_model.hold(render=True):
        fig_model.renderers[0].data_source.data[
            axis] = colData.values  # set data
        change_formatter(plotstate, fig_model, dff,
                         axis=axis)  # change formatter to cat if needed
        update_label(plotstate, fig_model, axis=axis)
        if axis == "color":
            update_color_mapper(plotstate, fig_model, dff)
        reset_range(plotstate, fig_model, dff, axis=axis)
        update_tooltips(plotstate, fig_model)

update_color_mapper(plotstate, fig_model, dff, color=None) #

Updates low/high property of color mapper

Parameters:

Name Type Description Default
plotstate PlotState

plot variables

required
fig_model Plot

plot object to update

required
dff DataFrame

filtered dataframe

required
color Optional[ndarray]

optional pre-computed aggregation data.

None
Source code in src/sdss_explorer/dashboard/components/views/plot_actions.py
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
def update_color_mapper(
    plotstate: PlotState,
    fig_model: Plot,
    dff: vx.DataFrame,
    color: Optional[ndarray] = None,
) -> None:
    """Updates low/high property of color mapper

    Args:
        plotstate: plot variables
        fig_model: plot object to update
        dff: filtered dataframe
        color: optional pre-computed aggregation data.
    """

    low, high = _calculate_color_range(plotstate, dff, color)
    mapper = fig_model.right[0].color_mapper  # same obj as in glyph
    mapper.update(low=low, high=high)
    mapper = fig_model.renderers[
        0].glyph.fill_color.transform  # same obj as in glyph
    mapper.update(low=low, high=high)

    return

update_label(plotstate, fig_model, axis='x') #

Updates label for axis, regenerating as needed

Parameters:

Name Type Description Default
plotstate PlotState

plot variables

required
fig_model Plot

figure object

required
axis str

axis to update for. 'x', 'y', or 'color'

'x'
Source code in src/sdss_explorer/dashboard/components/views/plot_actions.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
def update_label(plotstate: PlotState,
                 fig_model: Plot,
                 axis: str = "x") -> None:
    """
    Updates label for axis, regenerating as needed

    Args:
        plotstate: plot variables
        fig_model: figure object
        axis: axis to update for. 'x', 'y', or 'color'
    """
    assert axis in ("x", "y", "color"), f"expected axis x or y but got {axis}"

    # gangsta one liner
    loc = ("below" if axis == "x" else "left") if axis != "color" else "right"

    # get place and set it
    ax = getattr(fig_model, loc)[0]
    setattr(
        ax,
        "title" if axis == "color" else "axis_label",
        generate_label(plotstate, axis=axis),
    )

update_mapping(plotstate, dff, axis='x') #

Updates the categorical datamapping for the given axis

Parameters:

Name Type Description Default
plotstate PlotState

plot variables

required
dff DataFrame

filtered dataframe

required
axis str

axis to perform update on. any of 'x', 'y', or 'color'

'x'
Source code in src/sdss_explorer/dashboard/components/views/plot_actions.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
def update_mapping(plotstate: PlotState,
                   dff: vx.DataFrame,
                   axis: str = "x") -> None:
    """Updates the categorical datamapping for the given axis

    Args:
        plotstate: plot variables
        dff: filtered dataframe
        axis: axis to perform update on. any of 'x', 'y', or 'color'

    """

    assert axis in ("x", "y", "color"), f"expected axis x or y but got {axis}"
    col = getattr(plotstate, axis).value  # column name

    assert dff[col].nunique() < 10, (
        "this column has too many unique categories. not supported.")

    mapping = generate_datamap(dff[col])
    setattr(plotstate, f"{axis}mapping", mapping)  # categorical datamap
    return

update_tooltips(plotstate, fig_model) #

Updates tooltips on toolbar's HoverTool

Note

This will fetch mappings, so it assumes it has already been generated.

Parameters:

Name Type Description Default
plotstate PlotState

plot vars

required
fig_model Plot

plot object

required
Source code in src/sdss_explorer/dashboard/components/views/plot_actions.py
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
def update_tooltips(plotstate: PlotState, fig_model: Plot) -> None:
    """Updates tooltips on toolbar's HoverTool

    Note:
        This will fetch mappings, so it assumes it has already been generated.

    Args:
        plotstate: plot vars
        fig_model: plot object
    """
    # find the tool
    for tool in fig_model.toolbar.tools:
        if isinstance(tool, HoverTool):
            tool.tooltips = generate_tooltips(plotstate)
            if plotstate.plottype == "histogram":
                tool.formatters = {
                    "@centers":
                    generate_categorical_hover_formatter(plotstate, axis="x"),
                }
            else:
                tool.formatters = {
                    "$snap_x":
                    generate_categorical_hover_formatter(plotstate, axis="x"),
                    "$snap_y":
                    generate_categorical_hover_formatter(plotstate, axis="y"),
                    "@color":
                    generate_categorical_hover_formatter(plotstate,
                                                         axis="color"),
                }