def test_serialize(self): """Objects are serialized to JSON-compatible objects""" def epoch(obj): """Convert to JS Epoch time""" return int(time.mktime(obj.timetuple())) * 1000 types = [('test', str, 'test'), (pd.Timestamp('2013-06-08'), int, epoch(pd.Timestamp('2013-06-08'))), (datetime.utcnow(), int, epoch(datetime.utcnow())), (1, int, 1), (1.0, float, 1.0), (np.float32(1), float, 1.0), (np.int32(1), int, 1), (np.float64(1), float, 1.0), (np.int64(1), int, 1)] for puts, pytype, gets in types: nt.assert_equal(Data.serialize(puts), gets) class BadType(object): """Bad object for type warning""" test_obj = BadType() with nt.assert_raises(LoadError) as err: Data.serialize(test_obj) nt.assert_equals(err.exception.args[0], 'cannot serialize index of type BadType')
def test_serialize(self): """Objects are serialized to JSON-compatible objects""" def epoch(obj): """Convert to JS Epoch time""" return int(time.mktime(obj.timetuple())) * 1000 types = [('test', str, 'test'), (pd.Timestamp('2013-06-08'), int, epoch(pd.Timestamp('2013-06-08'))), (datetime.utcnow(), int, epoch(datetime.utcnow())), (1, int, 1), (1.0, float, 1.0), (np.float32(1), float, 1.0), (np.int32(1), int, 1), (np.float64(1), float, 1.0), (np.int64(1), int, 1)] for puts, pytype, gets in types: nt.assert_equal(Data.serialize(puts), gets) class BadType(object): """Bad object for type warning""" test_obj = BadType() with nt.assert_raises(LoadError) as err: Data.serialize(test_obj) nt.assert_equals(err.exception.message, 'cannot serialize index of type BadType')
def test_pandas_series_loading(self): """Pandas Series objects are correctly loaded""" # Test valid series types name = ['_x', ' name'] length = [0, 1, 2] index_key = [None, 'ix', 1] index_types = ['int', 'char', 'datetime', 'Timestamp'] value_key = [None, 'x', 1] value_types = ['int', 'char', 'datetime', 'Timestamp', 'float', 'numpy float', 'numpy int'] series_info = product(name, length, index_key, index_types, value_key, value_types) for n, l, ikey, itype, vkey, vtype in series_info: index = sequences[itype](l) series = pd.Series(sequences[vtype](l), index=index, name=n,) vkey = vkey or series.name expected = [{'idx': Data.serialize(i), 'col': vkey, 'val': Data.serialize(v)} for i, v in zip(index, series)] data = Data.from_pandas(series, name=n, series_key=vkey) nt.assert_list_equal(expected, data.values) nt.assert_equal(n, data.name) data.to_json() # Missing a name series = pd.Series(np.random.randn(10)) data = Data.from_pandas(series) nt.assert_equal(data.name, 'table')
def test_pandas_series_loading(self): """Pandas Series objects are correctly loaded""" # Test valid series types name = ['_x', ' name'] length = [0, 1, 2] index_key = [None, 'ix', 1] index_types = ['int', 'char', 'datetime', 'Timestamp'] value_key = [None, 'x', 1] value_types = ['int', 'char', 'datetime', 'Timestamp', 'float', 'numpy float', 'numpy int'] series_info = product(name, length, index_key, index_types, value_key, value_types) for n, l, ikey, itype, vkey, vtype in series_info: index = sequences[itype](l) series = pd.Series(sequences[vtype](l), index=index, name=n,) vkey = series.name or vkey expected = [{'idx': Data.serialize(i), 'col': vkey, 'val': Data.serialize(v)} for i, v in zip(index, series)] data = Data.from_pandas(series, name=n, series_key=vkey) nt.assert_list_equal(expected, data.values) nt.assert_equal(n, data.name) data.to_json() # Missing a name series = pd.Series(np.random.randn(10)) data = Data.from_pandas(series) nt.assert_equal(data.name, 'table')
def test_from_mult_iters(self): """Test set of iterables""" test1 = Data.from_mult_iters(x=[0, 1, 2], y=[3, 4, 5], z=[7, 8, 9], idx='x') test2 = Data.from_mult_iters(fruit=['apples', 'oranges', 'grapes'], count=[12, 16, 54], idx='fruit') values1 = [{ 'col': 'y', 'idx': 0, 'val': 3 }, { 'col': 'y', 'idx': 1, 'val': 4 }, { 'col': 'y', 'idx': 2, 'val': 5 }, { 'col': 'z', 'idx': 0, 'val': 7 }, { 'col': 'z', 'idx': 1, 'val': 8 }, { 'col': 'z', 'idx': 2, 'val': 9 }] values2 = [{ 'col': 'count', 'idx': 'apples', 'val': 12 }, { 'col': 'count', 'idx': 'oranges', 'val': 16 }, { 'col': 'count', 'idx': 'grapes', 'val': 54 }] nt.assert_list_equal(test1.values, values1) nt.assert_list_equal(test2.values, values2) #Iter errors nt.assert_raises(ValueError, Data.from_mult_iters, x=[0], y=[1, 2])
def test_from_iter(self): """Test data from single iterable""" test_list = Data.from_iter([10, 20, 30]) test_dict = Data.from_iter({'apples': 10, 'bananas': 20, 'oranges': 30}) get1 = [{'col': 'data', 'idx': 0, 'val': 10}, {'col': 'data', 'idx': 1, 'val': 20}, {'col': 'data', 'idx': 2, 'val': 30}] get2 = [{'col': 'data', 'idx': 'apples', 'val': 10}, {'col': 'data', 'idx': 'oranges', 'val': 30}, {'col': 'data', 'idx': 'bananas', 'val': 20}] nt.assert_list_equal(test_list.values, get1) nt.assert_list_equal(test_dict.values, get2)
def test_validation(self): """Test Visualization validation""" test_obj = Visualization() with nt.assert_raises(ValidationError) as err: test_obj.validate() nt.assert_equal(err.exception.args[0], 'data must be defined for valid visualization') test_obj.data = [Data(name='test'), Data(name='test')] with nt.assert_raises(ValidationError) as err: test_obj.validate() nt.assert_equal(err.exception.args[0], 'data has duplicate names')
def test_grammar_typechecking(self): """Data fields are correctly type-checked""" grammar_types = [ ('name', [str]), ('url', [str]), ('values', [list]), ('source', [str]), ('transform', [list])] assert_grammar_typechecking(grammar_types, Data('name'))
def test_from_mult_iters(self): """Test set of iterables""" test1 = Data.from_mult_iters(x=[0, 1, 2], y=[3, 4, 5], z=[7, 8, 9], idx='x') test2 = Data.from_mult_iters(fruit=['apples', 'oranges', 'grapes'], count=[12, 16, 54], idx='fruit') values1 = [{'col': 'y', 'idx': 0, 'val': 3}, {'col': 'y', 'idx': 1, 'val': 4}, {'col': 'y', 'idx': 2, 'val': 5}, {'col': 'z', 'idx': 0, 'val': 7}, {'col': 'z', 'idx': 1, 'val': 8}, {'col': 'z', 'idx': 2, 'val': 9}] values2 = [{'col': 'count', 'idx': 'apples', 'val': 12}, {'col': 'count', 'idx': 'oranges', 'val': 16}, {'col': 'count', 'idx': 'grapes', 'val': 54}] nt.assert_list_equal(test1.values, values1) nt.assert_list_equal(test2.values, values2) #Iter errors nt.assert_raises(ValueError, Data.from_mult_iters, x=[0], y=[1, 2])
def test_numpy_loading(self): """Numpy ndarray objects are correctly loaded""" test_data = np.random.randn(6, 3) index = range(test_data.shape[0]) columns = ['a', 'b', 'c'] data = Data.from_numpy(test_data, name='name', columns=columns) ikey = Data._default_index_key expected_values = [{ ikey: i, 'a': row[0], 'b': row[1], 'c': row[2] } for i, row in zip(index, test_data.tolist())] nt.assert_list_equal(expected_values, data.values) nt.assert_equal('name', data.name) index_key = 'akey' data = Data.from_numpy(test_data, name='name', columns=columns, index_key=index_key) expected_values = [{ index_key: i, 'a': row[0], 'b': row[1], 'c': row[2] } for i, row in zip(index, test_data.tolist())] nt.assert_list_equal(expected_values, data.values) index = ['a', 'b', 'c', 'd', 'e', 'f'] data = Data.from_numpy(test_data, name='name', index=index, columns=columns) expected_values = [{ ikey: i, 'a': row[0], 'b': row[1], 'c': row[2] } for i, row in zip(index, test_data.tolist())] nt.assert_list_equal(expected_values, data.values) #Bad loads with nt.assert_raises(LoadError) as err: Data.from_numpy(test_data, 'test', columns, index=range(4)) nt.assert_equal(err.expected, LoadError) columns = ['a', 'b'] with nt.assert_raises(LoadError) as err: Data.from_numpy(test_data, 'test', columns, index) nt.assert_equal(err.expected, LoadError)
def test_pandas_dataframe_loading(self): # Simple columns/key_on tests df = pd.DataFrame({'one': [1, 2, 3], 'two': [6, 7, 8], 'three': [11, 12, 13], 'four': [17, 18, 19]}) get_all = [{'col': 'four', 'idx': 0, 'val': 17}, {'col': 'one', 'idx': 0, 'val': 1}, {'col': 'three', 'idx': 0, 'val': 11}, {'col': 'two', 'idx': 0, 'val': 6}, {'col': 'four', 'idx': 1, 'val': 18}, {'col': 'one', 'idx': 1, 'val': 2}, {'col': 'three', 'idx': 1, 'val': 12}, {'col': 'two', 'idx': 1, 'val': 7}, {'col': 'four', 'idx': 2, 'val': 19}, {'col': 'one', 'idx': 2, 'val': 3}, {'col': 'three', 'idx': 2, 'val': 13}, {'col': 'two', 'idx': 2, 'val': 8}] get1 = [{'col': 'one', 'idx': 0, 'val': 1}, {'col': 'one', 'idx': 1, 'val': 2}, {'col': 'one', 'idx': 2, 'val': 3}] get2 = [{'col': 'one', 'idx': 0, 'val': 1}, {'col': 'two', 'idx': 0, 'val': 6}, {'col': 'one', 'idx': 1, 'val': 2}, {'col': 'two', 'idx': 1, 'val': 7}, {'col': 'one', 'idx': 2, 'val': 3}, {'col': 'two', 'idx': 2, 'val': 8}] getkey2 = [{'col': 'one', 'idx': 6, 'val': 1}, {'col': 'one', 'idx': 7, 'val': 2}, {'col': 'one', 'idx': 8, 'val': 3}] getkey3 = [{'col': 'one', 'idx': 11, 'val': 1}, {'col': 'two', 'idx': 11, 'val': 6}, {'col': 'one', 'idx': 12, 'val': 2}, {'col': 'two', 'idx': 12, 'val': 7}, {'col': 'one', 'idx': 13, 'val': 3}, {'col': 'two', 'idx': 13, 'val': 8}] val_all = Data.from_pandas(df) val1 = Data.from_pandas(df, columns=['one']) val2 = Data.from_pandas(df, columns=['one', 'two']) key2 = Data.from_pandas(df, columns=['one'], key_on='two') key3 = Data.from_pandas(df, columns=['one', 'two'], key_on='three') nt.assert_list_equal(val_all.values, get_all) nt.assert_list_equal(val1.values, get1) nt.assert_list_equal(val2.values, get2) nt.assert_list_equal(key2.values, getkey2) nt.assert_list_equal(key3.values, getkey3) # Missing a name dataframe = pd.DataFrame(np.random.randn(10, 3)) data = Data.from_pandas(dataframe) nt.assert_equal(data.name, 'table') # Bad obj nt.assert_raises(ValueError, Data.from_pandas, {})
def test_numpy_loading(self): """Numpy ndarray objects are correctly loaded""" test_data = np.random.randn(6, 3) index = xrange(test_data.shape[0]) columns = ['a', 'b', 'c'] data = Data.from_numpy(test_data, name='name', columns=columns) ikey = Data._default_index_key expected_values = [ {ikey: i, 'a': row[0], 'b': row[1], 'c': row[2]} for i, row in zip(index, test_data.tolist())] nt.assert_list_equal(expected_values, data.values) nt.assert_equal('name', data.name) index_key = 'akey' data = Data.from_numpy(test_data, name='name', columns=columns, index_key=index_key) expected_values = [ {index_key: i, 'a': row[0], 'b': row[1], 'c': row[2]} for i, row in zip(index, test_data.tolist())] nt.assert_list_equal(expected_values, data.values) index = ['a', 'b', 'c', 'd', 'e', 'f'] data = Data.from_numpy(test_data, name='name', index=index, columns=columns) expected_values = [ {ikey: i, 'a': row[0], 'b': row[1], 'c': row[2]} for i, row in zip(index, test_data.tolist())] nt.assert_list_equal(expected_values, data.values) #Bad loads with nt.assert_raises(LoadError) as err: Data.from_numpy(test_data, 'test', columns, index=xrange(4)) nt.assert_equal(err.expected, LoadError) columns = ['a', 'b'] with nt.assert_raises(LoadError) as err: Data.from_numpy(test_data, 'test', columns, index) nt.assert_equal(err.expected, LoadError)
def test_pandas_dataframe_loading(self): """Pandas DataFrame objects are correctly loaded""" # name = ['_x'] # length = [0, 1, 2] # index_key = [None, 'ix', 1] # index_types = ['int', 'char', 'datetime', 'Timestamp'] # column_types = ['int', 'char', 'datetime', 'Timestamp'] # # Leaving out some basic types here because we're not worried about # # serialization. # value_types = [ # 'char', 'datetime', 'Timestamp', 'numpy float', 'numpy int'] # dataframe_info = product( # name, length, length, index_key, index_types, column_types, # value_types) # for n, rows, cols, ikey, itype, ctype, vtype in dataframe_info: # index = sequences[itype](rows) # columns = sequences[ctype](cols) # series = { # c: pd.Series(sequences[vtype](rows), index=index, name=n) # for c in columns} # dataframe = pd.DataFrame(series) # ikey = ikey or Data._default_index_key # if cols == 0: # expected = [] # else: # expected = [ # dict([(ikey, Data.serialize(index[i]))] + # [(str(c), Data.serialize(series[c][i])) # for c in columns]) # for i in xrange(rows)] # data = Data.from_pandas(dataframe, name=n, index_key=ikey) # nt.assert_list_equal(expected, data.values) # nt.assert_equal(n, data.name) # data.to_json() #Simple columns/key_on tests df = pd.DataFrame({'one': [1,2,3], 'two': [6,7,8], 'three': [11, 12, 13], 'four': [17, 18, 19]}) get_all = [{'col': 'four', 'idx': 0, 'val': 17}, {'col': 'one', 'idx': 0, 'val': 1}, {'col': 'three', 'idx': 0, 'val': 11}, {'col': 'two', 'idx': 0, 'val': 6}, {'col': 'four', 'idx': 1, 'val': 18}, {'col': 'one', 'idx': 1, 'val': 2}, {'col': 'three', 'idx': 1, 'val': 12}, {'col': 'two', 'idx': 1, 'val': 7}, {'col': 'four', 'idx': 2, 'val': 19}, {'col': 'one', 'idx': 2, 'val': 3}, {'col': 'three', 'idx': 2, 'val': 13}, {'col': 'two', 'idx': 2, 'val': 8}] get1 = [{'col': 'one', 'idx': 0, 'val': 1}, {'col': 'one', 'idx': 1, 'val': 2}, {'col': 'one', 'idx': 2, 'val': 3}] get2 = [{'col': 'one', 'idx': 0, 'val': 1}, {'col': 'two', 'idx': 0, 'val': 6}, {'col': 'one', 'idx': 1, 'val': 2}, {'col': 'two', 'idx': 1, 'val': 7}, {'col': 'one', 'idx': 2, 'val': 3}, {'col': 'two', 'idx': 2, 'val': 8}] getkey2 = [{'col': 'one', 'idx': 6, 'val': 1}, {'col': 'one', 'idx': 7, 'val': 2}, {'col': 'one', 'idx': 8, 'val': 3}] getkey3 = [{'col': 'one', 'idx': 11, 'val': 1}, {'col': 'two', 'idx': 11, 'val': 6}, {'col': 'one', 'idx': 12, 'val': 2}, {'col': 'two', 'idx': 12, 'val': 7}, {'col': 'one', 'idx': 13, 'val': 3}, {'col': 'two', 'idx': 13, 'val': 8}] val_all = Data.from_pandas(df) val1 = Data.from_pandas(df, columns=['one']) val2 = Data.from_pandas(df, columns=['one', 'two']) key2 = Data.from_pandas(df, columns=['one'], key_on='two') key3 = Data.from_pandas(df, columns=['one', 'two'], key_on='three') nt.assert_list_equal(val_all.values, get_all) nt.assert_list_equal(val1.values, get1) nt.assert_list_equal(val2.values, get2) nt.assert_list_equal(key2.values, getkey2) nt.assert_list_equal(key3.values, getkey3) # Missing a name dataframe = pd.DataFrame(np.random.randn(10, 3)) data = Data.from_pandas(dataframe) nt.assert_equal(data.name, 'table') #Bad obj nt.assert_raises(ValueError, Data.from_pandas, {})
def test_pandas_dataframe_loading(self): # Simple columns/key_on tests df = pd.DataFrame({ 'one': [1, 2, 3], 'two': [6, 7, 8], 'three': [11, 12, 13], 'four': [17, 18, 19] }) get_all = [{ 'col': 'four', 'idx': 0, 'val': 17 }, { 'col': 'one', 'idx': 0, 'val': 1 }, { 'col': 'three', 'idx': 0, 'val': 11 }, { 'col': 'two', 'idx': 0, 'val': 6 }, { 'col': 'four', 'idx': 1, 'val': 18 }, { 'col': 'one', 'idx': 1, 'val': 2 }, { 'col': 'three', 'idx': 1, 'val': 12 }, { 'col': 'two', 'idx': 1, 'val': 7 }, { 'col': 'four', 'idx': 2, 'val': 19 }, { 'col': 'one', 'idx': 2, 'val': 3 }, { 'col': 'three', 'idx': 2, 'val': 13 }, { 'col': 'two', 'idx': 2, 'val': 8 }] get1 = [{ 'col': 'one', 'idx': 0, 'val': 1 }, { 'col': 'one', 'idx': 1, 'val': 2 }, { 'col': 'one', 'idx': 2, 'val': 3 }] get2 = [{ 'col': 'one', 'idx': 0, 'val': 1 }, { 'col': 'two', 'idx': 0, 'val': 6 }, { 'col': 'one', 'idx': 1, 'val': 2 }, { 'col': 'two', 'idx': 1, 'val': 7 }, { 'col': 'one', 'idx': 2, 'val': 3 }, { 'col': 'two', 'idx': 2, 'val': 8 }] getkey2 = [{ 'col': 'one', 'idx': 6, 'val': 1 }, { 'col': 'one', 'idx': 7, 'val': 2 }, { 'col': 'one', 'idx': 8, 'val': 3 }] getkey3 = [{ 'col': 'one', 'idx': 11, 'val': 1 }, { 'col': 'two', 'idx': 11, 'val': 6 }, { 'col': 'one', 'idx': 12, 'val': 2 }, { 'col': 'two', 'idx': 12, 'val': 7 }, { 'col': 'one', 'idx': 13, 'val': 3 }, { 'col': 'two', 'idx': 13, 'val': 8 }] val_all = Data.from_pandas(df) val1 = Data.from_pandas(df, columns=['one']) val2 = Data.from_pandas(df, columns=['one', 'two']) key2 = Data.from_pandas(df, columns=['one'], key_on='two') key3 = Data.from_pandas(df, columns=['one', 'two'], key_on='three') nt.assert_list_equal(val_all.values, get_all) nt.assert_list_equal(val1.values, get1) nt.assert_list_equal(val2.values, get2) nt.assert_list_equal(key2.values, getkey2) nt.assert_list_equal(key3.values, getkey3) # Missing a name dataframe = pd.DataFrame(np.random.randn(10, 3)) data = Data.from_pandas(dataframe) nt.assert_equal(data.name, 'table') # Bad obj nt.assert_raises(ValueError, Data.from_pandas, {})
def test_keypairs(self): Data.keypairs([0, 10, 20, 30, 40]) Data.keypairs(((0, 1), (0, 2), (0, 3))) Data.keypairs({'A': 10, 'B': 20, 'C': 30, 'D': 40, 'E': 50})
def test_validate(self): """Test Data name validation""" test_obj = Data() del test_obj.name nt.assert_raises(ValidationError, test_obj.validate)
def test_pandas_dataframe_loading(self): """Pandas DataFrame objects are correctly loaded""" # name = ['_x'] # length = [0, 1, 2] # index_key = [None, 'ix', 1] # index_types = ['int', 'char', 'datetime', 'Timestamp'] # column_types = ['int', 'char', 'datetime', 'Timestamp'] # # Leaving out some basic types here because we're not worried about # # serialization. # value_types = [ # 'char', 'datetime', 'Timestamp', 'numpy float', 'numpy int'] # dataframe_info = product( # name, length, length, index_key, index_types, column_types, # value_types) # for n, rows, cols, ikey, itype, ctype, vtype in dataframe_info: # index = sequences[itype](rows) # columns = sequences[ctype](cols) # series = { # c: pd.Series(sequences[vtype](rows), index=index, name=n) # for c in columns} # dataframe = pd.DataFrame(series) # ikey = ikey or Data._default_index_key # if cols == 0: # expected = [] # else: # expected = [ # dict([(ikey, Data.serialize(index[i]))] + # [(str(c), Data.serialize(series[c][i])) # for c in columns]) # for i in xrange(rows)] # data = Data.from_pandas(dataframe, name=n, index_key=ikey) # nt.assert_list_equal(expected, data.values) # nt.assert_equal(n, data.name) # data.to_json() #Simple columns/key_on tests df = pd.DataFrame({ 'one': [1, 2, 3], 'two': [6, 7, 8], 'three': [11, 12, 13], 'four': [17, 18, 19] }) get_all = [{ 'col': 'four', 'idx': 0, 'val': 17 }, { 'col': 'one', 'idx': 0, 'val': 1 }, { 'col': 'three', 'idx': 0, 'val': 11 }, { 'col': 'two', 'idx': 0, 'val': 6 }, { 'col': 'four', 'idx': 1, 'val': 18 }, { 'col': 'one', 'idx': 1, 'val': 2 }, { 'col': 'three', 'idx': 1, 'val': 12 }, { 'col': 'two', 'idx': 1, 'val': 7 }, { 'col': 'four', 'idx': 2, 'val': 19 }, { 'col': 'one', 'idx': 2, 'val': 3 }, { 'col': 'three', 'idx': 2, 'val': 13 }, { 'col': 'two', 'idx': 2, 'val': 8 }] get1 = [{ 'col': 'one', 'idx': 0, 'val': 1 }, { 'col': 'one', 'idx': 1, 'val': 2 }, { 'col': 'one', 'idx': 2, 'val': 3 }] get2 = [{ 'col': 'one', 'idx': 0, 'val': 1 }, { 'col': 'two', 'idx': 0, 'val': 6 }, { 'col': 'one', 'idx': 1, 'val': 2 }, { 'col': 'two', 'idx': 1, 'val': 7 }, { 'col': 'one', 'idx': 2, 'val': 3 }, { 'col': 'two', 'idx': 2, 'val': 8 }] getkey2 = [{ 'col': 'one', 'idx': 6, 'val': 1 }, { 'col': 'one', 'idx': 7, 'val': 2 }, { 'col': 'one', 'idx': 8, 'val': 3 }] getkey3 = [{ 'col': 'one', 'idx': 11, 'val': 1 }, { 'col': 'two', 'idx': 11, 'val': 6 }, { 'col': 'one', 'idx': 12, 'val': 2 }, { 'col': 'two', 'idx': 12, 'val': 7 }, { 'col': 'one', 'idx': 13, 'val': 3 }, { 'col': 'two', 'idx': 13, 'val': 8 }] val_all = Data.from_pandas(df) val1 = Data.from_pandas(df, columns=['one']) val2 = Data.from_pandas(df, columns=['one', 'two']) key2 = Data.from_pandas(df, columns=['one'], key_on='two') key3 = Data.from_pandas(df, columns=['one', 'two'], key_on='three') nt.assert_list_equal(val_all.values, get_all) nt.assert_list_equal(val1.values, get1) nt.assert_list_equal(val2.values, get2) nt.assert_list_equal(key2.values, getkey2) nt.assert_list_equal(key3.values, getkey3) # Missing a name dataframe = pd.DataFrame(np.random.randn(10, 3)) data = Data.from_pandas(dataframe) nt.assert_equal(data.name, 'table') #Bad obj nt.assert_raises(ValueError, Data.from_pandas, {})