Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved antialiased mean reduction #1300

Merged
merged 1 commit into from
Oct 27, 2023
Merged

Improved antialiased mean reduction #1300

merged 1 commit into from
Oct 27, 2023

Conversation

ianthomas23
Copy link
Member

This improves the output of Canvas.line using an antialiased mean reduction.

Example code:

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

x = np.asarray([0, 0.5, 1])
df = pd.DataFrame(dict(
    y0    = [0.0, 0.5, 1.0],
    y1    = [1.0, 0.0, 0.5],
    y2    = [0.0, 1.0, 0.2],
    value = [1.1, 2.2, 3.3],
))

canvas = ds.Canvas(plot_height=100, plot_width=150, x_range=(-0.1, 1.1), y_range=(-0.1, 1.1))
agg = canvas.line(source=df, x=x, y=["y0", "y1", "y2"], line_width=10, agg=ds.mean("value"), axis=1)
ds.transfer_functions.shade(agg, how="linear")

Image produced, before and after this PR:
temp_old temp_new

and zoomed in by a factor of 3 (before and after):
Screenshot 2023-10-27 at 15 52 42 Screenshot 2023-10-27 at 15 52 57

Before this PR a mean reduction was implemented by dividing a sum reduction by a count reduction. If you imagine sum("value") where all the value are 1 then the sum and count produce identical results with larger values at the middle of lines and smaller antialiased values at the edges. Dividing one by the other therefore loses the antialiasing.

This PR changes the mean to be a sum divided by a new reduction _count_ignore_antialiasing and the latter, as the name suggests, ignores the antialiasing factor. Hence the result produced is antialiased.

It isn't actually quite that simple as at the edges of joins artifacts were produced where the line goes over itself. Pixels where this occurred are visited twice, the second time with a larger antialiasing factor. Now when this occurs both of the antialiasing factors are passed to Reduction.append functions so that the reduction can better deal with it. There is no change to the low-level antialiasing code which has always calculated both values, but now both values are used rather than just one.

@codecov
Copy link

codecov bot commented Oct 27, 2023

Codecov Report

Merging #1300 (fd57632) into main (5eb4b22) will increase coverage by 0.01%.
Report is 4 commits behind head on main.
The diff coverage is 92.06%.

@@            Coverage Diff             @@
##             main    #1300      +/-   ##
==========================================
+ Coverage   85.64%   85.65%   +0.01%     
==========================================
  Files          52       52              
  Lines       11264    11288      +24     
==========================================
+ Hits         9647     9669      +22     
- Misses       1617     1619       +2     

Copy link
Member

@jbednar jbednar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great; thanks for following up on this!

@jbednar jbednar merged commit 23dfe67 into main Oct 27, 2023
14 checks passed
@jbednar jbednar deleted the prev_aa_factor branch October 27, 2023 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants