Skip to content

Instantly share code, notes, and snippets.

@bendichter
Created December 3, 2020 20:55
Show Gist options
  • Save bendichter/2b624263077ea798d3c40d7eca3afb7c to your computer and use it in GitHub Desktop.
Save bendichter/2b624263077ea798d3c40d7eca3afb7c to your computer and use it in GitHub Desktop.
from datetime import datetime, timedelta
import plotly.graph_objs as go
from plotly.subplots import make_subplots
import numpy as np
import dash_core_components as dcc
import dash_html_components as html
import dash
def discrete_colorscale(bvals, colors):
"""
bvals - list of values bounding intervals/ranges of interest
colors - list of rgb or hex colorcodes for values in [bvals[k], bvals[k+1]],0<=k < len(bvals)-1
returns the plotly discrete colorscale
"""
if len(bvals) != len(colors)+1:
raise ValueError('len(boundary values) should be equal to len(colors)+1')
bvals = sorted(bvals)
nvals = [(v-bvals[0])/(bvals[-1]-bvals[0]) for v in bvals] #normalized values
dcolorscale = [] #discrete colorscale
for k in range(len(colors)):
dcolorscale.extend([[nvals[k], colors[k]], [nvals[k+1], colors[k]]])
return dcolorscale
def show_electrode_status(data, state_labels):
nlabels = len(state_labels)
tickvals = np.linspace(0, nlabels - 1, 2 * nlabels + 1)[1::2]
bvals = np.arange(nlabels+1) - .5
colors = np.array(['#b81d13', '#efb700', '#008450', '#009ece'])[:nlabels]
dcolorsc = discrete_colorscale(bvals, colors.tolist())
data = [
go.Heatmap(
z=elec_data,
x=days,
colorscale=dcolorsc,
colorbar = dict(
thickness=10,
tickvals=tickvals,
ticktext=state_labels
),
xgap=2,
ygap=2
),
]
layout = go.Layout(
plot_bgcolor=('#fff'),
margin = dict(t=40),
yaxis={'title': 'electrode #'}
)
fig = go.Figure(data=data, layout=layout)
return fig
# generate fake data
start = datetime(1900, 1, 1)
nelecs = 50
state_labels = ['no units', 'one unit', '2 units', '2+ units']
ndays = 100
days = start + np.array([timedelta(days=int(x)) for x in np.arange(ndays)])
elec_data = np.random.randint(len(state_labels), size=(nelecs, ndays))
# visualize
show_electrode_status(elec_data, state_labels)
@bendichter
Copy link
Author

image

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