concept Datashader in category dask

appears as: Datashader
Data Science with Python and Dask

This is an excerpt from Manning's book Data Science with Python and Dask.

Imagine for a moment that we wanted to use the NYC Parking Ticket dataset to find the areas of New York City where drivers are most likely to get parking tickets. One way we could do this is by finding the mean location by latitude and longitude of all issued citations. However, this would only tell us the “average” location of a parking ticket, which might not even be located on a city street! There may be many hotspots throughout the city, but we wouldn’t be able to tell that by using only the mean. We could try to use some sort of clustering algorithm, such as k-Means, to identify multiple hotspots, but this would still be troublesome for a few reasons: the center points might again not be on any city streets, and we would have to manually choose the number of clusters to feed into the clustering algorithm. How would we know how many clusters to use if we don’t have a good understanding of the data? The only way to get a true and accurate understanding of the data in this case is to use all of it. But if plotting libraries like Seaborn and Matplotlib don’t work well with datasets stretching into the millions and billions of datapoints, how could we visualize datasets of this size without using any kind of downsampling? This is precisely where Datashader shines. Before we start, take a brief look at figure 8.1, which shows the progress we’ve made through our workflow.

Fortunately, Datashader performs all this mapping and aggregation for you based on the width/height and range options you specified in the Projection step. A small range and a large image will result in a high-resolution image of the space, whereas a large range and a small image will result in a low-resolution image of the space. Given the range and size we specified in listing 8.2, we can expect each pixel of our image to represent about 120,000 square feet, which is about half the area of a standard city block in New York City. So, all we need to do now is tell Datashader which data we want it to use.

Listing 8.3 Defining the aggregation
aggregation  = scene.points(nyc311_geo_data, 'Longitude', 'Latitude')

This call is pretty simple since we don’t need to apply any further transformation to the data. We’re simply telling Datashader to take the data in the nyc311_geo_data DataFrame, plot the Longitude column on the x axis, and plot the Latitude column on the y axis. Since we haven’t specified another method of aggregation, such as sum, mean, and so forth, Datashader will simply count the number of points that fall into each pixel. So, for our example here, Datashader will be counting the number of 311 service calls that happen within each 120,000 square foot chunk of New York City.

The fourth step is colormapping. In this step, Datashader will take the relative values computed in the Aggregation step and map them to a given color palette. In this example, we’re using the default color palette, which ranges from white to dark blue. The higher the value in the underlying array, the darker the shade of blue. This colormapping is what ultimately conveys the information that we want to know. Figure 8.5 demonstrates how Datashader performs the colormapping step.

Figure 8.5 Colormapping translates ranked values into colors.

c08_05.eps

The fifth and final step is embedding. This is where Datashader renders the final image using the information computed in the aggregation step along with the colormap. To trigger the final rendering, we use the shade method on the aggregation object. If we want to use a different colormap (for example, red to blue), you can specify that with the cmap parameter. Embedding is demonstrated in figure 8.6.