<jupyter_start><jupyter_text>Datashading a 1-billion-point Open Street Map databaseThe [osm.ipynb](https://anaconda.org/jbednar/osm) example shows how to use Dask's out of core support for working with datasets larger than will fit in memory.  Here, we consider the largest dataset that will fit comfortably on a 16GB machine, namely 1 billion x,y points (at 32-bit float resolution). You can download it from our site:http://s3.amazonaws.com/datashader-data/osm-1billion.parq.zipand unzip it into the `../data/` directory, but because of its size it's not downloaded with the other examples by default, and please try to limit the number of times you download it so that we can continue making it available.**NOTE:**  This dataset is also explorable through the Datashader example dashboard. From inside the examples directory, run: `DS_DATASET=osm-1b panel serve --show dashboard.ipynb`The data is taken from Open Street Map's (OSM) [bulk GPS point data](https://blog.openstreetmap.org/2012/04/01/bulk-gps-point-data/). The data was collected by OSM contributors' GPS devices, and was provided as a CSV file of `latitude,longitude` coordinates. The data was downloaded from their website, extracted, converted to use positions in Web Mercator format using `datashader.utils.lnglat_to_meters()`, sorted using [spacial indexing](../user_guide/2_Points.ipynbSpatial-indexing), and then stored in a [parquet](https://github.com/dask/fastparquet) file for [faster disk access](https://github.com/bokeh/datashader/issues/129issuecomment-300515690). As usual, loading it into memory will be the most time-consuming step, by far:.container { width:100% !important; }<jupyter_code>import datashader as ds
import datashader.transfer_functions as tf
from datashader import spatial

from colorcet import fire
%%time
df = spatial.read_parquet('../data/osm-1billion.parq')
df = df.persist()
print(len(df))
df.head()<jupyter_output><empty_output><jupyter_text>AggregationTo visualize this data, we first create a canvas to provide pixel-shaped bins in which points can be aggregated, and then aggregate the data to produce a fixed-size aggregate array. For data already in memory, this aggregation is the only expensive step, because it requires reading every point in the dataset. For interactive use we are most interested in the performance for a *second* aggregation, after all of the components and data are in memory and the user is interacting with a plot dynamically.  So let's first do a dry run, which will be slightly slower, then do the full aggregation to show what performance can be expected during interactive use with a billion datapoints:<jupyter_code>bound = 20026376.39
bounds = dict(x_range = (-bound, bound), y_range = (int(-bound*0.4), int(bound*0.6)))
plot_width = 900
plot_height = int(plot_width*0.5)
%%time
cvs = ds.Canvas(plot_width=plot_width, plot_height=plot_height, **bounds)
agg2=cvs.points(df, 'x', 'y', ds.count())
%%time
cvs = ds.Canvas(plot_width=plot_width, plot_height=plot_height, **bounds)
agg = cvs.points(df, 'x', 'y', ds.count())<jupyter_output><empty_output><jupyter_text>As you can see, once everything has been loaded, aggregating this billion-point dataset takes a couple of seconds on a 16GB Macbook laptop.  The remaining steps for display complete in milliseconds, so those times are not shown below. Transfer FunctionTo actually see the data, we need to render an image of the aggregate array.  Let's map small (but nonzero) counts to light blue, the largest counts to dark blue, and interpolate according to a logarithmic function in between:<jupyter_code>tf.shade(agg, cmap=["lightblue", "darkblue"], how='log')<jupyter_output><empty_output><jupyter_text>There's some odd, low-count, nearly-uniform noise going on in the tropics. It's worth trying to figure out what that could be, but for now we can filter it out quickly from the aggregated data using the `where` method from xarray.  We'll also switch to the parameter-free histogram equalization method for mapping to colors using various colormaps:<jupyter_code>tf.shade(agg.where(agg > 15), cmap=["lightblue", "darkblue"])
tf.set_background(tf.shade(agg.where(agg > 15), cmap=fire),"black")<jupyter_output><empty_output><jupyter_text>The result covers the most-populated areas of the globe, with Europe apparently having particularly many OpenStreetMap contributors. The long great-circle paths visible on the white background are presumably flight or boat trips, from devices that log their GPS coordinates more than 15 times during the space of one pixel in this plot (or else they would have been eliminated by the `where` call).  Interactive plottingAs long as the data fits into RAM on your machine, performance should be high enough to explore the data interactively, with only a brief pause after zooming or panning to a new region of the data space.  Performance should be much higher when you are zoomed in, thanks to the spatially sorted data structure used here; if you use a regular Pandas or Dask dataframe it will need to iterate through the entire dataset on each zoom rather than only considering a subset as used here.<jupyter_code>import holoviews as hv
from holoviews import opts
from holoviews.operation.datashader import datashade, dynspread
hv.extension('bokeh')

rgb_opts = opts.RGB(width=plot_width,height=plot_height, xaxis=None, yaxis=None, bgcolor="black")
points = hv.Points(df, ['x', 'y']).redim.range(x=bounds["x_range"],y=bounds["y_range"])
rgb = dynspread(datashade(points, cmap=["darkblue", "lightcyan"], width=plot_width,height=plot_height))
rgb.opts(rgb_opts)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08343711083437111, 'fraction_numerical': 0.011030065824586372, 'mean_word_length': 5.708830548926015, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '176078', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1652, 'n_tokens_neox': 1564, 'n_words': 686, 'path': 'examples/topics/osm-1billion.ipynb', 'repo_name': 'seanlane/datashader', 'size': '5621'}