Skip to content

Instantly share code, notes, and snippets.

@Miserlou
Created February 15, 2019 19:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Miserlou/e572660a9aa264d893b6be0c23a262a8 to your computer and use it in GitHub Desktop.
Save Miserlou/e572660a9aa264d893b6be0c23a262a8 to your computer and use it in GitHub Desktop.
Heatmaps with Dask, Holoviews and Datashader
import time
import datashader as ds
import pandas as pd
import numpy as np
import holoviews as hv
from holoviews import opts
from colorcet import fire
from datashader import transfer_functions as tf
from dask import dataframe as dd
import multiprocessing
from holoviews.operation.datashader import datashade, shade, dynspread, rasterize
from holoviews.operation import decimate
from holoviews import opts
hv.extension('bokeh')
print("Loading data..")
df = pd.read_csv('./data/DANIO_RERIO.tsv', sep='\t', header=0, index_col=0, error_bad_lines=False)
print("Data loaded..")
print("Converted to Dask..")
dask_df = dd.from_pandas(df, npartitions=multiprocessing.cpu_count()).persist()
print("To Dask Array..")
da = dask_df.to_dask_array(True).persist()
# Output resolution. Bokeh doesn't like very much more than this, try matplotlib
x_size = 1000
y_size = 1000
num_genes, num_samples = df.shape
print("To Image..")
img = hv.Image((np.arange(num_samples), np.arange(num_genes), da))
rasterized_img = rasterize(img, width=x_size, height=y_size)
rasterized_img.opts(width=x_size, height=y_size, cmap='viridis', logz=True)
# You have two options, bokeh requires selenium and phantomjs for png export, if you have those you can do
# hv.save(hv_obj, 'test.png')
# or you could use the matplotlib backend using
# hv.save(hv_obj, 'test.png', backend='matplotlib')
hv.save(rasterized_img, "rasterized" + str(time.time()) + ".png")
@KingGame0
Copy link

<style type="text/css"></style> KingGame -- Gold99 PGasia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment