Taxis


In [ ]:
import holoviews as hv
hv.notebook_extension('bokeh', 'matplotlib')


/Users/klay6683/miniconda3/envs/stable/lib/python3.6/site-packages/seaborn/apionly.py:6: UserWarning: As seaborn no longer sets a default style on import, the seaborn.apionly module is deprecated. It will be removed in a future version.
  warnings.warn(msg, UserWarning)

In [ ]:
station_info = pd.read_csv("/Users/klay6683/src/holoviews-examples/assets/station_info.csv")
station_info.head()

In [ ]:
scatter = hv.Scatter(station_info, kdims=['services'], vdims=['ridership'])
scatter

In [ ]:
layout = scatter + hv.Histogram(np.histogram(station_info['opened'], bins=24), kdims=['openened'])
layout

In [ ]:
taxi_dropoffs = {hour:arr for hour, arr in np.load('/Users/klay6683/src/holoviews-examples/assets/hourly_taxi_data.npz').items()}

In [ ]:
taxi_dropoffs['0'].shape

Spike


In [ ]:
spike_train = pd.read_csv("/Users/klay6683/src/holoviews-examples/assets/spike_train.csv.gz")
spike_train.head(3)

In [ ]:
curve = hv.Curve(spike_train, kdims=['milliseconds'], vdims=['Hertz'],
                 group='Firing Rate',)

In [ ]:
spikes = hv.Spikes(spike_train.sample(300), kdims=['milliseconds'], vdims=[], group='Spike Train')

In [ ]:
curve + spikes

In [ ]:
hv.extension('bokeh', 'matplotlib')

In [ ]:
%%output size=150
%%opts Curve  [height=100 width=600 xaxis=None tools=['hover']]
%%opts Curve (color='red' line_width=1.5)
%%opts Spikes [height=100 width=600 yaxis=None] (color='grey' line_width=0.25)
curve = hv.Curve(spike_train, kdims=['milliseconds'], vdims=['Hertz'])
spikes = hv.Spikes(spike_train, kdims=['milliseconds'], vdims=[])
(curve+spikes).cols(1)

In [ ]:
spikes.select(milliseconds=(2000, 4000))

In [ ]:
%output size=150
spikes.select(milliseconds=(2000, 4000))

Diseases


In [ ]:
diseases = pd.read_csv("/Users/klay6683/src/holoviews-examples/assets/diseases.csv.gz")

In [ ]:
vdims = [('measles', 'Measles Incidence'), ('pertussis', 'Pertussis Incidence')]
ds = hv.Dataset(diseases, kdims=['Year', 'State'], vdims=vdims)

In [ ]:
ds = ds.aggregate(function=np.mean)
ds

In [ ]:
%%opts Curve [width=400 height=250] {+framewise}
(ds.to(hv.Curve, 'Year', 'measles') + ds.to(hv.Curve, 'Year', 'pertussis')).cols(1)

In [ ]:
%%opts Bars [width=500 height=250 tools=['hover'] group_index=1 legend_position='top_left']
states = ['New York', 'New Jersey', 'California', 'Texas']
ds.select(State=states, Year=(1980, 1990)).to(hv.Bars, ['Year', 'State'], 'measles')

In [ ]:
%%opts Curve [width=150] (color='indianred')
grouped = ds.select(State=states, Year=(1930, 2005)).to(hv.Curve, 'Year', 'measles')
grouped.grid('State')

In [ ]:
%%opts Curve [width=500] (color=Cycle(values=['indianred', 'slateblue', 'lightseagreen', 'coral']))
grouped.overlay('State')

In [ ]:
%%opts Curve [width=600]
agg = ds.aggregate('Year', function=np.mean, spreadfn=np.std)
(hv.Curve(agg) * hv.ErrorBars(agg,vdims=['measles', 'measles_std'])).redim.range(measles=(-1, None))

In [ ]:
agg

Gridded data


In [ ]:
data = np.load('/Users/klay6683/src/holoviews-examples/assets/twophoton.npz')
calcium_array = data['Calcium']
calcium_array.shape

In [ ]:
ds = hv.Dataset((np.arange(50), np.arange(111), np.arange(62), calcium_array),
                kdims=['Time', 'x', 'y'], vdims=['Fluorescence'])
ds

In [ ]:
ds.clone(datatype=['xarray']).data

In [ ]:
%opts Image [width=111*3, height=3*62] (cmap='viridis')
ds.to(hv.Image, ['x', 'y']).hist()

In [ ]:
ROIs = data['ROIs']
roi_bounds = hv.NdOverlay({i: hv.Bounds(tuple(roi)) for i, roi in enumerate(ROIs)})
print(ROIs.shape)

In [ ]:
%%opts Image [width=400 height=400 xaxis=None yaxis=None] 
%%opts Bounds (color='white') Text (text_color='white' text_font_size='8pt')

opts = dict(halign='left', valign='bottom')
roi_text = hv.NdOverlay({i: hv.Text(roi[0], roi[1], str(i), **opts) for i, roi in enumerate(ROIs)})
(ds[21].to(hv.Image, ['x', 'y']) * roi_bounds * roi_text).relabel('Time: 21')

In [ ]:
ROIs[60]

In [ ]:
x0, y0, x1, y1 = ROIs[60]
roi = ds.select(x=(x0, x1), y=(y0, y1), time=(250, 280)).relabel('ROI #60')
roi.to(hv.Image, ['x', 'y'])

In [ ]:
%%opts GridSpace [shared_xaxis=True shared_yaxis=True]
%%output info=True
roi.to(hv.Image, kdims=['x', 'Time'])

In [ ]:
%%opts Overlay [show_legend=True width=600]
agg = roi.aggregate('Time', np.mean, spreadfn=np.std)
(hv.Curve(agg) * hv.Spread(agg)).relabel("Mean window response")

live data


In [ ]:
%opts Curve [show_grid=False xaxis=None yaxis=None]

In [ ]:
def clifford(a,b,c,d,x0,y0):
    xn,yn = x0,y0
    coords = [(x0,y0)]
    for i in range(10000):
        x_n1 = np.sin(a*yn) + c*np.cos(a*xn)
        y_n1 = np.sin(b*xn) + d*np.cos(b*yn)
        xn,yn = x_n1,y_n1
        coords.append((xn,yn))
    return coords

def clifford_attractor(a,b,c,d):
    return hv.Curve(clifford(a,b,c,d,x0=0,y0=0))

In [ ]:
%%opts Curve (line_width=0.03 color='red')
clifford_attractor(a =-1.5, b=1.5, c=1, d=0.75 )

In [ ]:
dmap = hv.DynamicMap(clifford_attractor, kdims=['a','b','c','d'])
dmap

In [ ]:
%%opts Curve (line_width=0.03 color='green')
# When run live, this cell's output should match the behavior of the GIF below
dmap.redim.range(a=(-1.5,-1),b=(1.5,2),c=(1,1.2),d=(0.75,0.8))

In [ ]:
def interactive_clifford(a,b,c,d,x=0,y=0):
    coords = clifford(a,b,c,d,x0=x,y0=y)
    return (hv.Curve(coords) * hv.Points(coords[0]) * hv.Curve(coords[:2], group='Init')
            * hv.Text(-0.75,1.35, 'x:{x:.2f} y:{y:.2f}'.format(x=coords[0][0],y=coords[0][1])))

In [ ]:
from holoviews.streams import PointerXY

In [ ]:
%%opts Curve (line_width=0.03 color='blue') Points (color='red' size=10) Curve.Init (color='red' line_width=2)
# When run live, this cell's output should match the behavior of the GIF below
dmap = hv.DynamicMap(interactive_clifford, kdims=['a','b','c','d'], streams=[PointerXY(x=0,y=0)])
dmap.redim.range(a=(-1.4,-1),b=(1.6,1.8),c=(1,1.5),d=(0.7,0.8))

In [ ]:
arr = np.random.rand(5,10)
arr.shape

In [ ]:
hv.extension()

In [ ]:
import geoviews as gv

In [ ]:
img = hv.Image(arr)
img

In [ ]:
raster = hv.Raster(arr)
raster

Annotating your data


In [ ]:
xs = range(-10,11)
ys = [100-el**2 for el in xs]
curve = hv.Curve((xs, ys))
curve

In [ ]:
distance = hv.Dimension('distance', label='Horizontal distance', unit='m')
height = hv.Dimension(('height','Height above sea-level'), unit='m')

wo_unit = hv.Curve((xs, ys), 
                   kdims=[('distance','Horizontal distance')], 
                   vdims=[('height','Height above sea-level')])
with_unit = hv.Curve((xs, ys), kdims=[distance], vdims=[height])

wo_unit + with_unit

Composing elements


In [ ]:
xvals = [0.1* i for i in range(100)]
curve =  hv.Curve((xvals, [np.sin(x) for x in xvals]))
scatter =  hv.Scatter((xvals[::5], np.linspace(0,1,20)))

In [ ]:
curve_list   = [hv.Curve((xvals, [np.sin(f*x) for x in xvals])) for f in [0.5, 0.75]]
scatter_list = [hv.Scatter((xvals[::5], f*np.linspace(0,1,20))) for f in [-0.5, 0.5]]
# overlay = hv.Overlay(curve_list + scatter_list)
# overlay
hv.Overlay(curve_list)

Dimensioned containers


In [ ]:
hv.notebook_extension('bokeh')
%opts Curve (line_width=1)

In [ ]:
def fm_modulation(f_carrier=220, f_mod=220, mod_index=1, length=0.1, sampleRate=2000):
    sampleInc = 1.0/sampleRate
    x = np.arange(0,length, sampleInc)
    y = np.sin(2*np.pi*f_carrier*x + mod_index*np.sin(2*np.pi*f_mod*x))
    return hv.Curve((x, y), kdims=['Time'], vdims=['Amplitude'])

In [ ]:
%%opts Curve [width=600]
f_carrier = np.linspace(20, 60, 3)
f_mod = np.linspace(20, 100, 5)

curve_dict = {(fc, fm): fm_modulation(fc, fm) for fc in f_carrier for fm in f_mod}

kdims = [('f_carrier', 'Carrier frequency'), ('f_mod', 'Modulation frequency')]
holomap = hv.HoloMap(curve_dict, kdims=kdims)
holomap

Composite objects


In [ ]:
hv.extension('bokeh')
np.random.seed(10)

def sine_curve(phase, freq, amp, power, samples=102):
    xvals = [0.1* i for i in range(samples)]
    return [(x, amp*np.sin(phase+freq*x)**power) for x in xvals]

phases =      [0, np.pi/2, np.pi, 3*np.pi/2]
powers =      [1,2,3]
amplitudes =  [0.5,0.75, 1.0]
frequencies = [0.5, 0.75, 1.0, 1.25, 1.5, 1.75]


gridspace = hv.GridSpace(kdims=['Amplitude', 'Power'], group='Parameters', label='Sines')

for power in powers:
    for amplitude in amplitudes:
        holomap = hv.HoloMap(kdims=['Frequency'])
        for frequency in frequencies:
            sines = {phase : hv.Curve(sine_curve(phase, frequency, amplitude, power))
                     for phase in phases}
            ndoverlay = hv.NdOverlay(sines , kdims=['Phase']).relabel(group='Phases',
                                                                      label='Sines', depth=1)
            overlay = ndoverlay * hv.Points([(i,0) for i in range(0,10)], group='Markers', label='Dots')
            holomap[frequency] = overlay
        gridspace[amplitude, power] = holomap

penguins = hv.RGB(np.random.randn(400,200,3)).relabel(group="Family", label="Penguin")

layout = gridspace + penguins

In [ ]:
layout

In [ ]:
layout.Parameters.Sines[0.5, 1]

In [ ]:
layout.Parameters.Sines[0.5, 1][1].Phases.Sines[0.0][5.1]

In [ ]: