holoviz / datashader

Quickly and accurately render even the largest data.
http://datashader.org
BSD 3-Clause "New" or "Revised" License
3.24k stars 363 forks source link

Support antialiased lines in *_n reductions #1262

Closed ianthomas23 closed 11 months ago

ianthomas23 commented 11 months ago

This adds support for antialiased lines in *_n reductions such as max_n and first_n. They return reasonable results, but these are not necessarily consistent with the non-*_n reductions yet. Support for antialiased where reductions will follow this, and then a full review of whether the results are consistent for all of the different line types supported (axis 0, axis 1, etc).

Example of output using this script

import datashader as ds
import numpy as np
import pandas as pd

df = pd.DataFrame(dict(
    ystart=[0.2, 0, 1, 0.8],
    yend=[0.2, 1, 0, 0.8],
    value=[2, 4, 1, 3],
))
kwargs = dict(source=df, x=np.asarray([0, 1]), y=["ystart", "yend"], axis=1, line_width=15)

reductions = dict(
    first_n=ds.first_n("value", n=2),
    last_n=ds.last_n("value", n=2),
    min_n=ds.min_n("value", n=2),
    max_n=ds.max_n("value", n=2),
    min_n_row_index=ds._min_n_row_index(n=2),
    max_n_row_index=ds._max_n_row_index(n=2),
)

cvs = ds.Canvas(plot_width=200, plot_height=150, x_range=(-0.1, 1.1), y_range=(-0.1, 1.1))
for name, reduction in reductions.items():
    agg = cvs.line(agg=reduction, **kwargs)
    span = (np.nanmin(agg), np.nanmax(agg))
    for k in range(agg.shape[2]):
        im = ds.transfer_functions.shade(agg[:, :, k], how="linear", span=span)
        ds.utils.export_image(im, f"temp_{name}_{k}", background="white")

first_n showing [:, :, 0] then [:, :, 1]: temp_first_n_0 temp_first_n_1

last_n: temp_last_n_0 temp_last_n_1

min_n: temp_min_n_0 temp_min_n_1

max_n: temp_max_n_0 temp_max_n_1

_min_n_row_index: temp_min_n_row_n_0 temp_min_n_row_n_1

_max_n_row_index: temp_max_n_row_n_0 temp_max_n_row_n_1

codecov[bot] commented 11 months ago

Codecov Report

Merging #1262 (1f91111) into main (0b71920) will decrease coverage by 0.19%. The diff coverage is 67.36%.

@@            Coverage Diff             @@
##             main    #1262      +/-   ##
==========================================
- Coverage   83.62%   83.43%   -0.19%     
==========================================
  Files          35       35              
  Lines        8938     9069     +131     
==========================================
+ Hits         7474     7567      +93     
- Misses       1464     1502      +38     
Files Changed Coverage Δ
datashader/core.py 88.25% <ø> (ø)
datashader/utils.py 76.89% <28.84%> (-5.82%) :arrow_down:
datashader/compiler.py 90.03% <83.33%> (-0.21%) :arrow_down:
datashader/reductions.py 80.10% <90.14%> (+0.94%) :arrow_up:
datashader/antialias.py 96.87% <100.00%> (+0.10%) :arrow_up:

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more