Exemplo n.º 1
0
def scales(scale=None):
	"""
	Displays a color scale (HTML)

	Parameters:
	-----------
		scale : str
			Color scale name
			If no scale name is provided then all scales are returned
				(max number for each scale)
			If scale='all' then all scale combinations available 
				will be returned

	Example:
		scales('accent')
		scales('all')
		scales()
	"""
	if scale:
		if scale=='all':
			display(HTML(cl.to_html(_scales)))
		else:	
			display(HTML(cl.to_html(get_scales(scale))))	
	else:
		s=''
		keys=list(_scales_names.keys())
		keys.sort()
		for k in keys:
			scale=get_scales(k)
			s+= '<div style="display:inline-block;padding:10px;"><div>{0}</div>{1}</div>'.format(k,cl.to_html(scale))
		display(HTML(s))
Exemplo n.º 2
0
def scales(scale=None):
    """
    Displays a color scale (HTML)

    Parameters:
    -----------
            scale : str
                    Color scale name
                    If no scale name is provided then all scales are returned
                            (max number for each scale)
                    If scale='all' then all scale combinations available
                            will be returned

    Example:
            scales('accent')
            scales('all')
            scales()
    """
    if scale:
        if scale == 'all':
            display(HTML(cl.to_html(_scales)))
        else:
            display(HTML(cl.to_html(get_scales(scale))))
    else:
        s = ''
        keys = list(_scales_names.keys())
        keys.sort()
        for k in keys:
            scale = get_scales(k)
            s += '<div style="display:inline-block;padding:10px;"><div>{0}</div>{1}</div>'.format(
                k, cl.to_html(scale))
        display(HTML(s))
Exemplo n.º 3
0
 def test_to_html(self):
     html = cl.to_html(cl.scales['3']['div']['RdYlBu'])
     self.assertEqual(
         html, ('<div style="background-color:rgb(252,141,89);'
                'height:20px;width:20px;display:inline-block;"></div>'
                '<div style="background-color:rgb(255,255,191);'
                'height:20px;width:20px;display:inline-block;"></div>'
                '<div style="background-color:rgb(145,191,219);'
                'height:20px;width:20px;display:inline-block;"></div>'))
Exemplo n.º 4
0
def rgb_to_html_display(name, rgb):
    """
    colorlover cl.to_html returns a div based on the input color
    If we have a name associated, hack the div and give it a title so it has a 
    Hover value in jupyter
    :param name: string name to fill div
    :param rgb: tuple of rgb values
    """
    rgb_as_html = cl.to_html([rgb])
    return '<div title="{}" {}'.format(name, rgb_as_html.split('<div ')[-1])
Exemplo n.º 5
0
 def test_to_html(self):
     html = cl.to_html(cl.scales['3']['div']['RdYlBu'])
     self.assertEqual(
         html,
         ('<div style="background-color:rgb(252,141,89);'
          'height:20px;width:20px;display:inline-block;"></div>'
          '<div style="background-color:rgb(255,255,191);'
          'height:20px;width:20px;display:inline-block;"></div>'
          '<div style="background-color:rgb(145,191,219);'
          'height:20px;width:20px;display:inline-block;"></div>')
     )
Exemplo n.º 6
0
def display_custom_scales():
    """
    Displays custom colorscales (HTML)
    """
    
    custom_colorscales = yaml.load(open(THEMES_PATH / 'custom_colorscales.yaml', 'r'))
    rgb_custom = dict()
    
    for scale in custom_colorscales.keys():
        rgb_custom[scale] = [cf.colors.hex_to_rgb(i) for i in custom_colorscales[scale]]
    
    display(HTML(cl.to_html(rgb_custom)))
Exemplo n.º 7
0
    def __call__(self, data, kind='rgb', as_string=False,
                 vmin=None, vmax=None):
        """Convert a subset of colors to a given type.

        Parameters
        ----------
        data : array, shape (n_colors,)
            Must be an array of floats between 0 and 1. These
            will be used to index into self.cmap.
        kind : 'rgb' | 'husl' | 'html'
            Whether to return output colors in rgb or husl space.
            If 'html', the color output of the call will be displayed.
        as_string : bool
            If True, return colors as plotly-style strings.

        Returns
        -------
        arr : np.array | list of strings
            The colors associated with values in `frac`.
        """
        data = np.atleast_1d(data)
        if data.ndim > 1:
            raise ValueError('frac must be 1-d')
        if vmin is not None or vmax is not None:
            # If we need to scale out data
            frac = np.clip((data - vmin) / float(vmax - vmin), 0, 1)
        else:
            frac = data
        if not ((frac >= 0.) * (frac <= 1.)).all(0):
            raise ValueError('input must be between 0 and 1, you'
                             ' provided {}'.format(frac))
        arr = self.cmap(frac)
        if kind == 'rgb':
            if as_string is False:
                pass
            else:
                arr = [tuple(i) for i in arr[:, :-1] * 255]
                arr = cl.to_rgb(arr)
        elif kind == 'husl':
            if as_string is False:
                arr = np.array([pl.husl.rgb_to_husl(r, g, b)
                                for r, g, b, _ in arr])
            else:
                arr = [tuple(i) for i in arr[:, :-1] * 255]
                arr = cl.to_hsl(arr)
        elif kind == 'html':
            arr = [tuple(i) for i in arr[:, :-1] * 255]
            arr = cl.to_hsl(arr)
            arr = HTML(cl.to_html(arr))
        else:
            raise ValueError("Kind {} not supported".format(kind))
        return arr
Exemplo n.º 8
0
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 23 23:49:44 2017

@author: nmishra
"""

import numpy as np
import pandas as pd
from tabulate import tabulate
import matplotlib.pyplot as plt
from collections import defaultdict
import matplotlib
import colorlover as cl
from IPython.display import HTML
HTML(cl.to_html( cl.flipper()['seq']['3'] ))
matplotlib.style.use('ggplot')

Colors   = ['blue','green','red','cyan', 'magenta', 'beige', 
            'bisque', 'black', 'dimgray','darkturquoise','blanchedalmond','darkviolet']

deadline = ['0.60','0.65','0.70','0.75','0.80','0.85','0.90']
table    = []
All_data = defaultdict(dict)
for x in ['eff','lat']:
    for y in deadline:
        filename = '../results/single-app/dyn-' + x +'-' +y  +'-v3.txt'
        f        = open(filename, 'r')  
        table    = [row.strip().split('\t')[0].split() for row in f if 'AVERAGE']
        All_data[x][y]= np.array([table[i][1:] for i in range(1,len(table)-1)]).astype(float)
        f.close()
Exemplo n.º 9
0
def plot_results(sample, ordering, clusters, noise, name, epsilon_cutoff):
    # Find valleys boundaries
    cluster_ordering = np.array(ordering.cluster_ordering)
    if epsilon_cutoff > np.max(cluster_ordering):
        epsilon_cutoff = np.max(cluster_ordering)
    peaks = np.where(cluster_ordering > epsilon_cutoff)[0]
    val_boundaries = [0] + list(peaks) + [len(ordering.cluster_ordering)]
    print 'valleys boundaries: %s' % val_boundaries
    num_valleys = len(val_boundaries) - 1
    print 'num_valleys: %s' % num_valleys

    # Color palette (max 9 colors)
    num_colors = min(len(cl.scales), 9)
    color_palette = cl.scales[str(num_colors)]
    palette_file = 'palette.html'
    # Save and look at the color palette.
    with open(palette_file, 'w+') as f:
        f.write(cl.to_html(color_palette))
        print 'color palette saved to: %s' % palette_file
    colors = color_palette['qual']['Set1']
    # Make the set bigger if need be
    while len(colors) < num_valleys:
        colors += colors

    # Data traces to plot
    traces1 = [
        go.Scatter(name='Cluster %s data' % i,
                   x=[
                       sample[sample_id][0] for sample_id in clusters[0]
                       [val_boundaries[i]:val_boundaries[i + 1]]
                   ],
                   y=[
                       sample[sample_id][1] for sample_id in clusters[0]
                       [val_boundaries[i]:val_boundaries[i + 1]]
                   ],
                   mode='markers',
                   marker=dict(color=colors[i])) for i in range(num_valleys)
    ]

    traces2 = [
        go.Scatter(
            name='Cluster %s reachability distances' % i,
            x=range(val_boundaries[i], val_boundaries[i + 1] + 1),
            y=ordering.cluster_ordering[val_boundaries[i]:val_boundaries[i +
                                                                         1] +
                                        1],
            mode='lines',
            line=dict(color=colors[i])) for i in range(num_valleys)
    ]

    traces2.append(
        go.Scatter(name='Reachability threshold',
                   y=[epsilon_cutoff for _ in ordering.cluster_ordering],
                   mode='lines',
                   line=dict(color='grey', dash='dash')))

    # Traces layouts
    layout1 = go.Layout(title='Input data',
                        xaxis=go.XAxis(title='x'),
                        yaxis=go.YAxis(title='y'))
    layout2 = go.Layout(title='Clustering structure',
                        xaxis=go.XAxis(title='Index (cluster order of the '
                                       'objects)'),
                        yaxis=go.YAxis(title='Reachability distance'),
                        annotations=[
                            dict(
                                x=val_boundaries[i],
                                y=ordering.cluster_ordering[val_boundaries[i]],
                                xref='x',
                                yref='y',
                                text='Reachability distance over threshold',
                                showarrow=True,
                                arrowhead=7,
                                ax=0,
                                ay=-40) for i in range(1, num_valleys)
                        ])

    fig1 = {'data': traces1, 'layout': layout1}
    url1 = py.plot(fig1, auto_open=False, filename='%s_data.html' % name)
    print url1

    fig2 = go.Figure(data=traces2, layout=layout2)
    url2 = py.plot(fig2,
                   auto_open=False,
                   filename='%s_reachability.html' % name)
    print url2

    # With subplots:
    fig3 = make_subplots(rows=1,
                         cols=2,
                         subplot_titles=['Input Data', 'Clustering Structure'])
    for trace1 in traces1:
        fig3.append_trace(trace1, row=1, col=1)
    for trace2 in traces2:
        fig3.append_trace(trace2, row=1, col=2)

    # Subplot layout example:
    fig3['layout'].update(xaxis1=go.XAxis(title='x'),
                          yaxis1=go.YAxis(title='y'))
    # Or:
    fig3['layout']['xaxis1'].update(title='X values')  # , range=[0, 6])
    fig3['layout']['yaxis1'].update(title='Y values')  # , range=[0, 6])
    fig3['layout']['xaxis2'].update(
        title='Index (cluster order of the objects)')
    fig3['layout']['yaxis2'].update(
        title='Reachability distance')  # , range=[0,1])

    description = (
        '<b><a href="https://en.wikipedia.org/wiki/OPTICS_algorithm">'
        'OPTICS</a> %s Example</b>' % name)

    fig3['layout'].update(width=1200, height=600, title=description)

    url3 = py.plot(fig3,
                   filename='%s_clustering-structure.html' % name,
                   auto_open=False)
    print url3
Exemplo n.º 10
0
def plot_results(sample, ordering, clusters, noise, name, epsilon_cutoff,
                 output_dir):
  # Find valleys boundaries
  cluster_ordering = np.array(ordering.cluster_ordering)
  if epsilon_cutoff > np.max(cluster_ordering):
    epsilon_cutoff = np.max(cluster_ordering)
  peaks = np.where(cluster_ordering > epsilon_cutoff)[0]
  val_boundaries = [0] + list(peaks) + [len(ordering.cluster_ordering)]
  print 'valleys boundaries: %s' % val_boundaries
  num_valleys = len(val_boundaries) - 1
  print 'num_valleys: %s' % num_valleys

  # Color palette (max 9 colors)
  num_colors = min(len(cl.scales), 9)
  color_palette = cl.scales[str(num_colors)]
  palette_file = os.path.join(output_dir, 'palette.html')
  # Save and look at the color palette.
  with open(palette_file, 'w+') as f:
    f.write(cl.to_html(color_palette))
    print 'color palette saved to: %s' % palette_file
  colors = color_palette['qual']['Set1']
  # Make the set bigger if need be
  while len(colors) < num_valleys:
    colors += colors

  # Data traces to plot
  traces1 = [go.Scatter(
    name='Cluster %s data' % i,
    x=[sample[sample_id][0] for sample_id in
       clusters[0][val_boundaries[i]:val_boundaries[i + 1]]],
    y=[sample[sample_id][1] for sample_id in
       clusters[0][val_boundaries[i]:val_boundaries[i + 1]]],
    mode='markers',
    marker=dict(color=colors[i])
  ) for i in range(num_valleys)]

  traces2 = [go.Scatter(
    name='Cluster %s reachability distances' % i,
    x=range(val_boundaries[i], val_boundaries[i + 1] + 1),
    y=ordering.cluster_ordering[val_boundaries[i]:val_boundaries[i + 1] + 1],
    mode='lines',
    line=dict(color=colors[i])
  ) for i in range(num_valleys)]

  traces2.append(go.Scatter(
    name='Reachability threshold',
    y=[epsilon_cutoff for _ in ordering.cluster_ordering],
    mode='lines',
    line=dict(color='grey', dash='dash')
  ))

  # Traces layouts
  layout1 = go.Layout(title='Input data',
                      xaxis=go.XAxis(title='x'),
                      yaxis=go.YAxis(title='y'))
  layout2 = go.Layout(title='Clustering structure',
                      xaxis=go.XAxis(title='Index (cluster order of the '
                                           'objects)'),
                      yaxis=go.YAxis(title='Reachability distance'),
                      annotations=[
                        dict(
                          x=val_boundaries[i],
                          y=ordering.cluster_ordering[val_boundaries[i]],
                          xref='x',
                          yref='y',
                          text='Reachability distance over threshold',
                          showarrow=True,
                          arrowhead=7,
                          ax=0,
                          ay=-40
                        ) for i in range(1, num_valleys)]
                      )

  fig1 = {'data': traces1, 'layout': layout1}
  filename = os.path.join(output_dir, '%s_data.html' % name)
  url1 = py.plot(fig1, auto_open=False, filename=filename)
  print url1

  fig2 = go.Figure(data=traces2, layout=layout2)
  filename = os.path.join(output_dir, '%s_reachability.html' % name)
  url2 = py.plot(fig2, auto_open=False, filename=filename)
  print url2

  # With subplots:
  fig3 = make_subplots(rows=1, cols=2, subplot_titles=['Input Data',
                                                       'Clustering Structure'])
  for trace1 in traces1:
    fig3.append_trace(trace1, row=1, col=1)
  for trace2 in traces2:
    fig3.append_trace(trace2, row=1, col=2)

  # Subplot layout example:
  fig3['layout'].update(xaxis1=go.XAxis(title='x'),
                        yaxis1=go.YAxis(title='y'))
  # Or:
  fig3['layout']['xaxis1'].update(title='X values')  # , range=[0, 6])
  fig3['layout']['yaxis1'].update(title='Y values')  # , range=[0, 6])
  fig3['layout']['xaxis2'].update(title='Index (cluster order of the objects)')
  fig3['layout']['yaxis2'].update(
    title='Reachability distance')  # , range=[0,1])

  description = ('<b><a href="https://en.wikipedia.org/wiki/OPTICS_algorithm">'
                 'OPTICS</a> %s Example</b>' % name)

  fig3['layout'].update(width=1200, height=600, title=description)
  filename = os.path.join(output_dir, '%s_clustering-structure.html' % name)
  url3 = py.plot(fig3, filename=filename, auto_open=False)
  print url3
Exemplo n.º 11
0
# Change COMPLNT_FR_TM to datetime.time type
data.CMPLNT_FR_TM = pd.to_datetime(data.CMPLNT_FR_TM, format='%H:%M:%S').dt.time
type(data.CMPLNT_FR_TM[0])

data = pd.read_csv('/content/gdrive/My Drive/Colab Notebooks/Criminal_Prediction/NYC_Crime_sampling.csv')

data = data.drop(columns=['Unnamed: 0'])

print('We have total {} observations and {} variables.'.format(data.shape[0], data.shape[1]))
print("\nHere are first 5 row of our sample data.\n")
data.head()

import colorlover as cl
from IPython.display import HTML
HTML(cl.to_html( cl.flipper()['seq']['3'] ))

colors = cl.scales['9']['seq']['GnBu']
print('Color we chose in this notebook:\n')
HTML(cl.to_html(colors))

# Visulize number of crimes in NYC Boroughs
data.BORO_NM.value_counts().sort_index()

import plotly.graph_objs as go
import plotly.offline as py

trace1 = go.Bar(x = data.BORO_NM.value_counts().sort_index().index, 
                y = data.BORO_NM[data.LAW_CAT_CD == 'MISDEMEANOR'].value_counts().sort_index(),
                name = 'MISDEMEANOR', 
                text = (data.BORO_NM[data.LAW_CAT_CD == 'MISDEMEANOR'].value_counts()/data.BORO_NM.value_counts()).apply(lambda x: format(x, '.2%')).sort_index(),
Exemplo n.º 12
0
import numpy as np
import plotly.graph_objects as go
import matplotlib.pyplot as plt

import colorlover as cl
from IPython.display import HTML
colors = cl.scales['11']['div']['RdYlBu']
colors = colors[:5] + ['rgb(183,188,143)','rgb(143,188,143)'] + colors[8:]
HTML(cl.to_html(colors))




def y_smooth(y, box_pts):
    box = np.ones(box_pts)/box_pts
    y_smooth = np.convolve(y, box, mode='same')
    return y_smooth



def go_chart(gvalues,title=None, y_smooth=False, y_savgol_filter=False):
    
    ##### chart
    if y_smooth:
        
        fig = go.Figure(data=[
            go.Scatter(
                y=y_smooth(gvalues.loc[gvalues[c].index>=min(gvalues.loc[gvalues[c].notnull()].index),c].dropna(),2),
                name=c,
                mode='lines+markers',
                line=dict(color=colors[i],width=4)