def test_df_to_padded_padded_to_df(): """Tests df_to_padded, padded_to_df """ # Call with names? Call with order? # Continuous tte? # Contiguous t? # np.random.seed(1) n_seqs = 100 max_seq_length = 100 ids = xrange(n_seqs) df = generate_random_df(n_seqs, max_seq_length) df = df.reset_index(drop=True) # Column names to transform to tensor column_names = ['event', 'int_column', 'double_column'] dtypes = df[column_names].dtypes.values padded = df_to_padded(df, column_names) df_new = padded_to_df(padded, column_names, dtypes, ids=ids) column_names = ['id', 't', 'event', 'int_column', 'double_column'] # Pandas is awful. Index changes when slicing df = df[column_names].reset_index(drop=True) pd.util.testing.assert_frame_equal(df[column_names], df_new)
def test_align_padded(): np.random.seed(1) n_seqs = 10 max_seq_length = 10 df = generate_random_df(n_seqs, max_seq_length) column_names = ['event', 'int_column', 'double_column'] padded = df_to_padded(df, column_names) np.testing.assert_array_equal( padded, left_pad_to_right_pad(right_pad_to_left_pad(padded))) padded = np.copy(np.squeeze(padded)) np.testing.assert_array_equal( padded, left_pad_to_right_pad(right_pad_to_left_pad(padded)))
def test_align_padded(): """test the function for switching between left-right padd. """ # For simplicity, create a realistic padded tensor np.random.seed(1) n_seqs = 10 max_seq_length = 10 df = generate_random_df(n_seqs, max_seq_length) cols_to_expand = ['event', 'int_column', 'double_column'] padded = df_to_padded(df, cols_to_expand, 'id', 't_elapsed') np.testing.assert_array_equal( padded, left_pad_to_right_pad(right_pad_to_left_pad(padded))) padded = np.copy(np.squeeze(padded)) np.testing.assert_array_equal( padded, left_pad_to_right_pad(right_pad_to_left_pad(padded)))
def df_to_padded_padded_to_df_runner(t_col): n_seqs = 5 max_seq_length = 10 ids = xrange(n_seqs) cols_to_expand = ['event', 'int_column', 'double_column'] np.random.seed(1) df = generate_random_df(n_seqs, max_seq_length) df = df.reset_index(drop=True) # Column names to transform to tensor dtypes = df[cols_to_expand].dtypes.values padded = df_to_padded(df, cols_to_expand, 'id', t_col) df_new = padded_to_df(padded, cols_to_expand, dtypes, ids, 'id', t_col) # Pandas is awful. Index changes when slicing df = df[['id', t_col] + cols_to_expand].reset_index(drop=True) pd.util.testing.assert_frame_equal(df, df_new)
def test_df_to_padded_padded_to_df(): """Tests df_to_padded, padded_to_df """ # Call with names? Call with order? # Continuous tte? # Contiguous t? # np.random.seed(1) n_seqs = 100 max_seq_length = 100 ids = xrange(n_seqs) df = generate_random_df(n_seqs, max_seq_length) column_names = ['event', 'int_column', 'double_column'] dtypes = ['double', 'int', 'float'] padded = df_to_padded(df, column_names) df_new = padded_to_df(padded, column_names, dtypes, ids=ids) assert False not in ( df[['id', 't', 'event', 'int_column', 'double_column']].values == df_new.values)[0],\ 'test_df_to_padded_padded_to_df failed'