<s> """<STR_LIT>""" <EOL> from __future__ import ( print_function , division ) <EOL> import time <EOL> import json <EOL> from . core import ( <EOL> _assert_is_type , <EOL> ValidationError , <EOL> grammar , <EOL> GrammarClass , <EOL> LoadError <EOL> ) <EOL> from . _compat import str_types <EOL> try : <EOL> import pandas as pd <EOL> except ImportError : <EOL> pd = None <EOL> try : <EOL> import numpy as np <EOL> except ImportError : <EOL> np = None <EOL> class Data ( GrammarClass ) : <EOL> """<STR_LIT>""" <EOL> _default_index_key = '<STR_LIT>' <EOL> def __init__ ( self , name = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( self . __class__ , self ) . __init__ ( ** kwargs ) <EOL> self . name = name if name else '<STR_LIT>' <EOL> @ grammar ( str_types ) <EOL> def name ( value ) : <EOL> """<STR_LIT>""" <EOL> @ grammar ( str_types ) <EOL> def url ( value ) : <EOL> """<STR_LIT>""" <EOL> @ grammar ( list ) <EOL> def values ( value ) : <EOL> """<STR_LIT>""" <EOL> for row in value : <EOL> _assert_is_type ( '<STR_LIT>' , row , ( float , int , dict ) ) <EOL> @ grammar ( str_types ) <EOL> def source ( value ) : <EOL> """<STR_LIT>""" <EOL> @ grammar ( list ) <EOL> def transform ( value ) : <EOL> """<STR_LIT>""" <EOL> @ grammar ( dict ) <EOL> def format ( value ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> super ( self . __class__ , self ) . validate ( * args ) <EOL> if not self . name : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> @ staticmethod <EOL> def serialize ( obj ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( obj , str_types ) : <EOL> return obj <EOL> elif hasattr ( obj , '<STR_LIT>' ) : <EOL> return int ( time . mktime ( obj . timetuple ( ) ) ) * <NUM_LIT:1000> <EOL> elif hasattr ( obj , '<STR_LIT>' ) : <EOL> return obj . item ( ) <EOL> elif hasattr ( obj , '<STR_LIT>' ) : <EOL> if isinstance ( obj , int ) : <EOL> return int ( obj ) <EOL> else : <EOL> return float ( obj ) <EOL> elif hasattr ( obj , '<STR_LIT>' ) : <EOL> return int ( obj ) <EOL> else : <EOL> raise LoadError ( '<STR_LIT>' <EOL> + type ( obj ) . __name__ ) <EOL> @ classmethod <EOL> def from_pandas ( cls , data , columns = None , key_on = '<STR_LIT>' , name = None , <EOL> series_key = '<STR_LIT:data>' , grouped = False , records = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not pd : <EOL> raise LoadError ( '<STR_LIT>' ) <EOL> if not hasattr ( data , '<STR_LIT:index>' ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if name : <EOL> vega_data = cls ( name = name , ** kwargs ) <EOL> else : <EOL> vega_data = cls ( name = '<STR_LIT>' , ** kwargs ) <EOL> pd_obj = data . copy ( ) <EOL> if columns : <EOL> pd_obj = data [ columns ] <EOL> if key_on != '<STR_LIT>' : <EOL> pd_obj . index = data [ key_on ] <EOL> if records : <EOL> vega_data . values = json . loads ( pd_obj . to_json ( orient = '<STR_LIT>' ) ) <EOL> return vega_data <EOL> vega_data . values = [ ] <EOL> if isinstance ( pd_obj , pd . Series ) : <EOL> data_key = data . name or series_key <EOL> for i , v in pd_obj . iteritems ( ) : <EOL> value = { } <EOL> value [ '<STR_LIT>' ] = cls . serialize ( i ) <EOL> value [ '<STR_LIT>' ] = data_key <EOL> value [ '<STR_LIT>' ] = cls . serialize ( v ) <EOL> vega_data . values . append ( value ) <EOL> elif isinstance ( pd_obj , pd . DataFrame ) : <EOL> for i , row in pd_obj . iterrows ( ) : <EOL> for num , ( k , v ) in enumerate ( row . iteritems ( ) ) : <EOL> value = { } <EOL> value [ '<STR_LIT>' ] = cls . serialize ( i ) <EOL> value [ '<STR_LIT>' ] = cls . serialize ( k ) <EOL> value [ '<STR_LIT>' ] = cls . serialize ( v ) <EOL> if grouped : <EOL> value [ '<STR_LIT>' ] = num <EOL> vega_data . values . append ( value ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' <EOL> + type ( pd_obj ) . __name__ ) <EOL> return vega_data <EOL> @ classmethod <EOL> def from_numpy ( cls , np_obj , name , columns , index = None , index_key = None , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not np : <EOL> raise LoadError ( '<STR_LIT>' ) <EOL> _assert_is_type ( '<STR_LIT>' , np_obj , np . ndarray ) <EOL> index = index or range ( np_obj . shape [ <NUM_LIT:0> ] ) <EOL> columns = list ( map ( str , columns ) ) <EOL> index_key = index_key or cls . _default_index_key <EOL> if len ( index ) != np_obj . shape [ <NUM_LIT:0> ] : <EOL> raise LoadError ( <EOL> '<STR_LIT>' ) <EOL> elif len ( columns ) != np_obj . shape [ <NUM_LIT:1> ] : <EOL> raise LoadError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> data = cls ( name = name , ** kwargs ) <EOL> data . values = [ <EOL> dict ( [ ( index_key , cls . serialize ( idx ) ) ] + <EOL> [ ( col , x ) for col , x in zip ( columns , row ) ] ) <EOL> for idx , row in zip ( index , np_obj . tolist ( ) ) ] <EOL> return data <EOL> @ classmethod <EOL> def from_mult_iters ( cls , name = None , idx = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not name : <EOL> name = '<STR_LIT>' <EOL> lengths = [ len ( v ) for v in kwargs . values ( ) ] <EOL> if len ( set ( lengths ) ) != <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not idx : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> index = kwargs . pop ( idx ) <EOL> vega_vals = [ ] <EOL> for k , v in sorted ( kwargs . items ( ) ) : <EOL> for idx , val in zip ( index , v ) : <EOL> value = { } <EOL> value [ '<STR_LIT>' ] = idx <EOL> value [ '<STR_LIT>' ] = k <EOL> value [ '<STR_LIT>' ] = val <EOL> vega_vals . append ( value ) <EOL> return cls ( name , values = vega_vals ) <EOL> @ classmethod <EOL> def from_iter ( cls , data , name = None ) : <EOL> """<STR_LIT>""" <EOL> if not name : <EOL> name = '<STR_LIT>' <EOL> if isinstance ( data , ( list , tuple ) ) : <EOL> data = { x : y for x , y in enumerate ( data ) } <EOL> values = [ { '<STR_LIT>' : k , '<STR_LIT>' : '<STR_LIT:data>' , '<STR_LIT>' : v } <EOL> for k , v in sorted ( data . items ( ) ) ] <EOL> return cls ( name , values = values ) <EOL> @ classmethod <EOL> def keypairs ( cls , data , columns = None , use_index = False , name = None ) : <EOL> """<STR_LIT>""" <EOL> if not name : <EOL> name = '<STR_LIT>' <EOL> cls . raw_data = data <EOL> if isinstance ( data , tuple ) : <EOL> values = [ { "<STR_LIT:x>" : x [ <NUM_LIT:0> ] , "<STR_LIT:y>" : x [ <NUM_LIT:1> ] } for x in data ] <EOL> elif isinstance ( data , list ) : <EOL> values = [ { "<STR_LIT:x>" : x , "<STR_LIT:y>" : y } <EOL> for x , y in zip ( range ( len ( data ) + <NUM_LIT:1> ) , data ) ] <EOL> elif isinstance ( data , dict ) or isinstance ( data , pd . Series ) : <EOL> values = [ { "<STR_LIT:x>" : x , "<STR_LIT:y>" : y } for x , y in sorted ( data . items ( ) ) ] <EOL> elif isinstance ( data , pd . DataFrame ) : <EOL> if len ( columns ) > <NUM_LIT:1> and use_index : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if use_index or len ( columns ) == <NUM_LIT:1> : <EOL> values = [ { "<STR_LIT:x>" : cls . serialize ( x [ <NUM_LIT:0> ] ) , <EOL> "<STR_LIT:y>" : cls . serialize ( x [ <NUM_LIT:1> ] [ columns [ <NUM_LIT:0> ] ] ) } <EOL> for x in data . iterrows ( ) ] <EOL> else : <EOL> values = [ { "<STR_LIT:x>" : cls . serialize ( x [ <NUM_LIT:1> ] [ columns [ <NUM_LIT:0> ] ] ) , <EOL> "<STR_LIT:y>" : cls . serialize ( x [ <NUM_LIT:1> ] [ columns [ <NUM_LIT:1> ] ] ) } <EOL> for x in data . iterrows ( ) ] <EOL> elif isinstance ( data , np . ndarray ) : <EOL> values = cls . _numpy_to_values ( data ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % type ( data ) ) <EOL> return cls ( name , values = values ) <EOL> @ staticmethod <EOL> def _numpy_to_values ( data ) : <EOL> '''<STR_LIT>''' <EOL> def to_list_no_index ( xvals , yvals ) : <EOL> return [ { "<STR_LIT:x>" : x , "<STR_LIT:y>" : np . asscalar ( y ) } <EOL> for x , y in zip ( xvals , yvals ) ] <EOL> if len ( data . shape ) == <NUM_LIT:1> or data . shape [ <NUM_LIT:1> ] == <NUM_LIT:1> : <EOL> xvals = range ( data . shape [ <NUM_LIT:0> ] + <NUM_LIT:1> ) <EOL> values = to_list_no_index ( xvals , data ) <EOL> elif len ( data . shape ) == <NUM_LIT:2> : <EOL> if data . shape [ <NUM_LIT:1> ] == <NUM_LIT:2> : <EOL> if isinstance ( data , np . matrix ) : <EOL> xidx = ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> yidx = ( <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> else : <EOL> xidx = <NUM_LIT:0> <EOL> yidx = <NUM_LIT:1> <EOL> xvals = [ np . asscalar ( row [ xidx ] ) for row in data ] <EOL> yvals = [ np . asscalar ( row [ yidx ] ) for row in data ] <EOL> values = [ { "<STR_LIT:x>" : x , "<STR_LIT:y>" : y } for x , y in zip ( xvals , yvals ) ] <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return values <EOL> def to_json ( self , validate = False , pretty_print = True , data_path = None ) : <EOL> """<STR_LIT>""" <EOL> return super ( self . __class__ , self ) . to_json ( validate = validate , <EOL> pretty_print = pretty_print ) </s>
