def test_fail_connectors_order(self): g = DeepGraph(v) with pytest.raises(KeyError): g.create_edges(connectors=[dsi, velo, dsf]) g.create_edges_ft(('si', v.si.max()), connectors=[dsi, velo, dsf])
def test_fail_common_output_rs(self): g = DeepGraph(v) with pytest.raises(AssertionError): g.create_edges(connectors=[dsi, dsf], selectors=[dsf_velo_t]) g.create_edges_ft(('si', v.si.max()), connectors=[dsi, dsf], selectors=[dsf_velo_t])
def test_fail_selector_ind_shape(self): g = DeepGraph(v) with pytest.raises(AssertionError): g.create_edges(connectors=dsi, selectors=fail_ind_shape) g.create_edges_ft(('si', v.si.max()), connectors=dsi, selectors=fail_ind_shape)
def test_fail_selectors_order(self): g = DeepGraph(v) with pytest.raises(KeyError): g.create_edges(selectors=[fail_selector_order, dsi_dsf_t]) g.create_edges_ft(('si', v.si.max()), selectors=[fail_selector_order, dsi_dsf_t])
def test_ft_dt_min_chunk_size_hdf(self, tmpdir, min_chunk_size): pytest.importorskip('tables') # tmp hdf store folder = str(tmpdir) vo = v.drop('o', axis=1) vs = pd.HDFStore(folder + 'vs.h5', mode='w') vs.put('v', vo, format='t', data_columns=True, index=False) e_true = e_full_true.copy() e_true['dmcs'] = [ 3, 6, 7, 7, 8, 12, 12, 3, 4, 4, 5, 9, 9, 1, 1, 2, 6, 6, 0, 1, 5, 5, 1, 5, 5, 4, 4, 0 ] e_true = e_true[e_true.dmcs <= 2][['dmcs']] g = DeepGraph(vs) g.create_edges_ft(ft_feature=('dtmcs', 2, 'h'), min_chunk_size=min_chunk_size) e_test = g.e.rename(columns={'ft_r': 'dmcs'}) e_test['dmcs'] = e_test.dmcs.astype(int) assert_frame_equal(e_test.sort_index(axis=1), e_true.sort_index(axis=1))
def test_hdf_key(self, tmpdir): pytest.importorskip('tables') # tmp hdf store folder = str(tmpdir) vo = v.drop('o', axis=1) vs = pd.HDFStore(folder + 'vs.h5', mode='w') vs.put('v', vo, format='t', data_columns=True, index=False) e_true = e_full_true[(e_full_true.dsi <= 3) & (e_full_true.dsf <= 1)] g = DeepGraph(vs) g.create_edges(selectors=[dsi_dsf_t], hdf_key='v') e_test = g.e pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1)) g.create_edges_ft(('si', v.si.max()), selectors=[dsi_dsf_t], hdf_key='v') e_test = g.e.drop('ft_r', axis=1) vs.close() pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1))
def test_step_size(self, step_size): e_true = e_full_true.iloc[:, 0:0] g = DeepGraph(v) g.create_edges(step_size=step_size) e_test = g.e pdt.assert_frame_equal(e_test, e_true)
def test_ft_sf(self): e_true = e_full_true[e_full_true.dsf <= 2.][['dsf']] g = DeepGraph(v) g.create_edges_ft(ft_feature=('sf', 2.)) e_test = g.e.rename(columns={'ft_r': 'dsf'}) pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1))
def test_from_pos(self, from_pos): e_true = e_full_true.iloc[from_pos:, 0:0] g = DeepGraph(v) g.create_edges(from_pos=from_pos) e_test = g.e pdt.assert_frame_equal(e_test, e_true)
def test_to_pos(self, to_pos): e_true = e_full_true.iloc[:to_pos, 0:0] g = DeepGraph(v) g.create_edges(to_pos=to_pos) e_test = g.e pdt.assert_frame_equal(e_test, e_true)
def test_ft_max_pairs(self, max_pairs): e_true = e_full_true[['dsi']] g = DeepGraph(v) g.create_edges_ft(ft_feature=('si', v.si.max()), max_pairs=max_pairs) e_test = g.e.rename(columns={'ft_r': 'dsi'}) pdt.assert_frame_equal(e_test, e_true)
def test_ft_ftt_order(self): e_true = e_full_true[e_full_true.dsf <= 1] g = DeepGraph(v) g.create_edges_ft(('si', v.si.max()), connectors=[dsi], selectors=[dsf_velo_t, 'ft_selector']) e_test = g.e.drop('ft_r', axis=1) pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1))
def test_ft_si(self): e_true = e_full_true[e_full_true.dsi <= 3][['dsi']] g = DeepGraph(v) g.create_edges_ft(ft_feature=('si', 3)) e_test = g.e.rename(columns={'ft_r': 'dsi'}) pdt.assert_frame_equal(e_test.sort_index(axis=1), e_true.sort_index(axis=1))
def test_ft_sdt(self): e_true = e_full_true[e_full_true.dsi <= 3][['dsi']] g = DeepGraph(v) g.create_edges_ft(ft_feature=('sdt', 3, 'h')) e_test = g.e.rename(columns={'ft_r': 'dsi'}) e_test['dsi'] = e_test.dsi.astype(int) pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1))
def test_ft_r_dep_connector(self): e_true = e_full_true[e_full_true.dsi <= 5] g = DeepGraph(v) g.create_edges_ft(ft_feature=('sdt', 5, 'h'), connectors=ft_r_dep) e_test = g.e.rename(columns={'ft_r': 'dsi'}) e_test['dsi'] = e_test.dsi.astype(int) pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1))
def test_ft_ftt_order(self): e_true = e_full_true[e_full_true.dsf <= 1] g = DeepGraph(v) g.create_edges_ft(('si', v.si.max()), connectors=[dsi], selectors=[dsf_velo_t, 'ft_selector']) e_test = g.e.drop('ft_r', axis=1) pdt.assert_frame_equal(e_test.sort_index(axis=1), e_true.sort_index(axis=1))
def test_ft_to_pos(self, ft_to_pos): e_true = e_full_true[['dsi']] s = e_true.index.get_level_values(0) e_true = e_true[s < ft_to_pos] g = DeepGraph(v) g.create_edges_ft(ft_feature=('si', v.si.max()), to_pos=ft_to_pos) e_test = g.e.rename(columns={'ft_r': 'dsi'}) pdt.assert_frame_equal(e_test, e_true)
def test_no_arguments(self): e_true = e_full_true.iloc[:, 0:0] g = DeepGraph(v) g.create_edges() e_test = g.e pdt.assert_frame_equal(e_test, e_true) g.create_edges_ft(('si', v.si.max())) e_test = g.e.drop('ft_r', axis=1) pdt.assert_frame_equal(e_test, e_true)
def test_connector(self): e_true = e_full_true[['dsi']] g = DeepGraph(v) g.create_edges(connectors=dsi) e_test = g.e pdt.assert_frame_equal(e_test, e_true) g.create_edges_ft(('si', v.si.max()), connectors=dsi) e_test = g.e.drop('ft_r', axis=1) pdt.assert_frame_equal(e_test, e_true)
def test_connectors(self): e_true = e_full_true[['dsf', 'dsi']] g = DeepGraph(v) g.create_edges(connectors=[dsi, dsf]) e_test = g.e pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1)) g.create_edges_ft(('si', v.si.max()), connectors=[dsi, dsf]) e_test = g.e.drop('ft_r', axis=1) pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1))
class TestAppendCP(object): e = g.e.iloc[[0, 3]] g = DeepGraph(v, e) def test_defaults(self): pytest.importorskip('scipy') cp_true = np.array([0, 0, 0, 2, 1]) self.g.append_cp() cp_test = g.v.cp.values assert_allclose(cp_true, cp_test) def test_consolidate_singles(self): pytest.importorskip('scipy') cp_true = np.array([1, 1, 1, 0, 0]) self.g.append_cp(consolidate_singles=True) cp_test = g.v.cp.values assert_allclose(cp_true, cp_test)
def test_selector(self): e_true = e_full_true[(e_full_true.dsi <= 3) & (e_full_true.dsf <= 1)] g = DeepGraph(v) g.create_edges(selectors=[dsi_dsf_t]) e_test = g.e pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1)) g.create_edges_ft(('si', v.si.max()), selectors=[dsi_dsf_t]) e_test = g.e.drop('ft_r', axis=1) pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1))
def test_logging(self, tmpdir): e_true = e_full_true.iloc[:, 0:0] g = DeepGraph(v) folder = str(tmpdir) g.create_edges(verbose=True, logfile=folder+'lf.txt') e_test = g.e pdt.assert_frame_equal(e_test, e_true) g.create_edges_ft(('si', v.si.max()), verbose=True, logfile=folder+'lf.txt') e_test = g.e.drop('ft_r', axis=1) pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1))
def test_no_transfer_r(self): e_true = e_full_true.drop('dsf', axis=1) g = DeepGraph(v) g.create_edges(connectors=[dsi, dsf, velo], no_transfer_rs='dsf') e_test = g.e pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1)) g.create_edges_ft(('si', v.si.max()), connectors=[dsi, dsf, velo], no_transfer_rs='dsf') e_test = g.e.drop('ft_r', axis=1) pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1))
def test_connector_selector(self): e_true = e_full_true[e_full_true.dsi <= 3] g = DeepGraph(v) g.create_edges(connectors=[dsi, dsf, velo], selectors=dsi_t) e_test = g.e pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1)) g.create_edges_ft(('si', v.si.max()), connectors=[dsi, dsf, velo], selectors=dsi_t) e_test = g.e.drop('ft_r', axis=1) pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1))
def test_hdf_step_size(self, tmpdir, step_size): pytest.importorskip('tables') # tmp hdf store folder = str(tmpdir) vo = v.drop('o', axis=1) vs = pd.HDFStore(folder + 'vs.h5', mode='w') vs.put('v', vo, format='t', index=False) e_true = e_full_true[(e_full_true.dsi <= 3) & (e_full_true.dsf <= 1)] g = DeepGraph(vs) g.create_edges(selectors=[dsi_dsf_t], step_size=step_size, hdf_key='v') e_test = g.e vs.close() pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1))
def test_r_dtype_dic(self): e_true = e_full_true[['dsf', 'dsi']] e_true['dsf'] = e_true.dsf.astype(np.uint8) e_true['dsi'] = e_true.dsi.astype(np.float32) r_dtype_dic = {'dsf': np.uint8, 'dsi': np.float32} g = DeepGraph(v) g.create_edges(connectors=[dsi, dsf], r_dtype_dic=r_dtype_dic) e_test = g.e pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1)) g.create_edges_ft(('si', v.si.max()), connectors=[dsi, dsf], r_dtype_dic=r_dtype_dic) e_test = g.e.drop('ft_r', axis=1) pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1))
def test_transfer_feature(self): e_true = e_full_true.iloc[:, 0:0] s = e_true.index.get_level_values(0) t = e_true.index.get_level_values(1) e_true['f_s'] = v.loc[s, 'f'].values e_true['f_t'] = v.loc[t, 'f'].values g = DeepGraph(v) g.create_edges(transfer_features='f') e_test = g.e pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1)) g.create_edges_ft(('si', v.si.max()), transfer_features='f') e_test = g.e.drop('ft_r', axis=1) pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1))
def test_ft_sdt_hdf(self, tmpdir): pytest.importorskip('tables') # tmp hdf store folder = str(tmpdir) vo = v.drop('o', axis=1) vs = pd.HDFStore(folder + 'vs.h5', mode='w') vs.put('v', vo, format='t', data_columns=True, index=False) e_true = e_full_true[(e_full_true.dsi <= 3) & (e_full_true.dsf <= 1)] g = DeepGraph(vs) g.create_edges_ft(ft_feature=('sdt', 3, 'h'), selectors=dsi_dsf_t, hdf_key='v') e_test = g.e.drop('ft_r', axis=1) vs.close() pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1))
def test_ft_min_chunk_size(self, min_chunk_size): e_true = e_full_true.copy() e_true['dmcs'] = [3,6,7,7,8,12,12,3,4,4,5,9,9,1,1,2,6,6,0,1,5,5,1, 5,5,4,4,0] e_true = e_true[e_true.dmcs <= 2][['dmcs']] g = DeepGraph(v) g.create_edges_ft(ft_feature=('mcs', 2), min_chunk_size=min_chunk_size) e_test = g.e.rename(columns={'ft_r': 'dmcs'}) e_test['dmcs'] = e_test.dmcs.astype(int) pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1)) g.create_edges_ft(ft_feature=('dtmcs', 2, 'h'), min_chunk_size=min_chunk_size) e_test = g.e.rename(columns={'ft_r': 'dmcs'}) e_test['dmcs'] = e_test.dmcs.astype(int) pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1))
def test_transfer_features(self): e_true = e_full_true.iloc[:, 0:0] s = e_true.index.get_level_values(0) t = e_true.index.get_level_values(1) e_true['f_s'] = v.loc[s, 'f'].values e_true['f_t'] = v.loc[t, 'f'].values e_true['s_s'] = v.loc[s, 's'].values e_true['s_t'] = v.loc[t, 's'].values e_true['o_s'] = v.loc[s, 'o'].values e_true['o_t'] = v.loc[t, 'o'].values g = DeepGraph(v) g.create_edges(transfer_features=['f', 's', 'o']) e_test = g.e pdt.assert_frame_equal(e_test.sort_index(axis=1), e_true.sort_index(axis=1)) g.create_edges_ft(('si', v.si.max()), transfer_features=['f', 's', 'o']) e_test = g.e.drop('ft_r', axis=1) pdt.assert_frame_equal(e_test.sort_index(axis=1), e_true.sort_index(axis=1))
def test_ft_dt_min_chunk_size_hdf(self, tmpdir, min_chunk_size): pytest.importorskip('tables') # tmp hdf store folder = str(tmpdir) vo = v.drop('o', axis=1) vs = pd.HDFStore(folder + 'vs.h5', mode='w') vs.put('v', vo, format='t', data_columns=True, index=False) e_true = e_full_true.copy() e_true['dmcs'] = [3,6,7,7,8,12,12,3,4,4,5,9,9,1,1,2,6,6,0,1,5,5,1, 5,5,4,4,0] e_true = e_true[e_true.dmcs <= 2][['dmcs']] g = DeepGraph(vs) g.create_edges_ft(ft_feature=('dtmcs', 2, 'h'), min_chunk_size=min_chunk_size) e_test = g.e.rename(columns={'ft_r': 'dmcs'}) e_test['dmcs'] = e_test.dmcs.astype(int) pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1))
def test_logging(self, tmpdir): e_true = e_full_true.iloc[:, 0:0] g = DeepGraph(v) folder = str(tmpdir) g.create_edges(verbose=True, logfile=folder+'lf.txt') e_test = g.e pdt.assert_frame_equal(e_test, e_true) g.create_edges_ft(('si', v.si.max()), verbose=True, logfile=folder+'lf.txt') e_test = g.e.drop('ft_r', axis=1) pdt.assert_frame_equal(e_test.sort_index(axis=1), e_true.sort_index(axis=1))
def test_r_dep_selector(self): e_true = e_full_true[(e_full_true.dsi <= 3) & (e_full_true.dsf <= 1)] g = DeepGraph(v) g.create_edges(connectors=[dsi], selectors=[dsi_t, dsf_velo_t]) e_test = g.e assert_frame_equal(e_test.sort_index(axis=1), e_true.sort_index(axis=1)) g.create_edges_ft(('si', v.si.max()), connectors=[dsi], selectors=[dsi_t, dsf_velo_t]) e_test = g.e.drop('ft_r', axis=1) assert_frame_equal(e_test.sort_index(axis=1), e_true.sort_index(axis=1))
def test_ft_min_chunk_size(self, min_chunk_size): e_true = e_full_true.copy() e_true['dmcs'] = [ 3, 6, 7, 7, 8, 12, 12, 3, 4, 4, 5, 9, 9, 1, 1, 2, 6, 6, 0, 1, 5, 5, 1, 5, 5, 4, 4, 0 ] e_true = e_true[e_true.dmcs <= 2][['dmcs']] g = DeepGraph(v) g.create_edges_ft(ft_feature=('mcs', 2), min_chunk_size=min_chunk_size) e_test = g.e.rename(columns={'ft_r': 'dmcs'}) e_test['dmcs'] = e_test.dmcs.astype(int) pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1)) g.create_edges_ft(ft_feature=('dtmcs', 2, 'h'), min_chunk_size=min_chunk_size) e_test = g.e.rename(columns={'ft_r': 'dmcs'}) e_test['dmcs'] = e_test.dmcs.astype(int) pdt.assert_frame_equal(e_test.sort(axis=1), e_true.sort(axis=1))
import pytest import numpy as np import pandas as pd import pandas.util.testing as pdt from numpy.testing import assert_allclose from deepgraph import DeepGraph # test node table v = pd.DataFrame({'x': [-3.4,2.1,-1.1,0.9,2.3], 'time': [0,1,2,5,9], 'color': ['g','g','b','g','r'], 'size': [1,3,2,3,1]}) g = DeepGraph(v) # create test edge table def some_relations(ft_r,x_s,x_t,color_s,color_t,size_s,size_t): dx = x_t - x_s v = dx / ft_r same_color = color_s == color_t larger_than = size_s > size_t return dx, v, same_color, larger_than g.create_edges_ft(('time', 5), connectors=some_relations) g.e.rename(columns={'ft_r': 'dt'}, inplace=True) class TestPartitionNodes(object):
def test_ft_fail_sorted(self): g = DeepGraph(v) with pytest.raises(AssertionError): g.create_edges_ft(ft_feature=('f', 1))
class TestInterfaces(object): e = g.e[['dx', 'dt', 'larger_than', 'same_color', 'v']] e.iloc[0:5, 0] = np.nan e.iloc[1, 1] = np.nan e.iloc[2, 2] = np.nan e.iloc[3, 3] = np.nan e.iloc[4, 4] = np.nan g = DeepGraph(v, e) v_shift_ind = g.v.reset_index() v_shift_ind['index'] += 2 v_shift_ind.set_index('index', inplace=True) e_shift_ind = e.reset_index() e_shift_ind['s'] += 2 e_shift_ind['t'] += 2 e_shift_ind.set_index(['s', 't'], inplace=True) g_si = DeepGraph(v_shift_ind, e_shift_ind) def test_return_cs_graph(self): pytest.importorskip('scipy') # relations = False csgraph_true = np.array([[False, True, True, True, False], [False, False, True, True, False], [False, False, False, True, False], [False, False, False, False, True], [False, False, False, False, False]]) csgraph_test = self.g.return_cs_graph(relations=False).toarray() csgraph_test_si = self.g_si.return_cs_graph(relations=False).toarray() assert_allclose(csgraph_true, csgraph_test) assert_allclose(csgraph_true, csgraph_test_si) # relations = 'dt', dropna = True csgraph_true = np.array([[0., 1., 0., 5., 0.], [0., 0., 1., 4., 0.], [0., 0., 0., 3., 0.], [0., 0., 0., 0., 4.], [0., 0., 0., 0., 0.]]) csgraph_test = self.g.return_cs_graph('dt', dropna=True).toarray() csgraph_test_si = self.g_si.return_cs_graph('dt', dropna=True).toarray() assert_allclose(csgraph_true, csgraph_test) assert_allclose(csgraph_true, csgraph_test_si) # relations = 'dt', dropna = False csgraph_true = np.array([[0., 1., np.nan, 5., 0.], [0., 0., 1., 4., 0.], [0., 0., 0., 3., 0.], [0., 0., 0., 0., 4.], [0., 0., 0., 0., 0.]]) csgraph_test = self.g.return_cs_graph('dt', dropna=False).toarray() csgraph_test_si = self.g_si.return_cs_graph('dt', dropna=False).toarray() assert_allclose(csgraph_true, csgraph_test) assert_allclose(csgraph_true, csgraph_test_si) # relations = ['dx', 'dt] csgraph_test = self.g.return_cs_graph(['dx', 'dt'], [True, False]) assert csgraph_test['dx'].nnz == 2 assert csgraph_test['dt'].nnz == 7 def test_return_nx_graph(self): # only testing that return_nx_graph produces no errors # work in progress! pytest.importorskip('networkx') pytest.importorskip('pandas', minversion="0.17.0") self.g.return_nx_graph(False, False) self.g.return_nx_graph(False, True) self.g.return_nx_graph(True, False) self.g.return_nx_graph('color', 'dx') self.g.return_nx_graph(['color'], ['dx', 'dt']) self.g_si.return_nx_graph(['color'], ['dx', 'dt']) # test dropna nxg = self.g.return_nx_graph(['color'], ['dx', 'dt'], dropna='none') assert nxg.number_of_edges() == 7 nxg = self.g.return_nx_graph(['color'], ['dx', 'dt'], dropna='all') assert nxg.number_of_edges() == 6 nxg = self.g.return_nx_graph(['color'], ['dx', 'dt'], dropna='any') assert nxg.number_of_edges() == 2 def test_return_gt_graph(self): # only testing that return_gt_graph produces no errors # work in progress! pytest.importorskip('graph_tool') self.g.return_gt_graph(False, False) self.g.return_gt_graph(False, True) self.g.return_gt_graph(True, False) self.g.return_gt_graph('color', 'dx') self.g.return_gt_graph(['color'], ['dx', 'dt']) self.g_si.return_gt_graph(['color'], ['dx', 'dt'], node_indices=True, edge_indices=True) # test dropna gtg = self.g.return_gt_graph(['color'], ['dx', 'dt'], dropna='none') assert gtg.num_edges() == 7 gtg = self.g.return_gt_graph(['color'], ['dx', 'dt'], dropna='all') assert gtg.num_edges() == 6 gtg = self.g.return_gt_graph(['color'], ['dx', 'dt'], dropna='any') assert gtg.num_edges() == 2