|
- #
- # Copyright (C) Stanislaw Adaszewski, 2020
- # License: GPLv3
- #
-
-
- from icosagon.trainprep import TrainValTest, \
- train_val_test_split_edges, \
- get_edges_and_degrees, \
- prepare_adj_mat
- import torch
- import pytest
- import numpy as np
- from itertools import chain
-
-
- def test_train_val_test_split_edges_01():
- edges = torch.randint(0, 10, (10, 2))
- with pytest.raises(ValueError):
- _ = train_val_test_split_edges(edges, TrainValTest(.5, .5, .5))
- with pytest.raises(ValueError):
- _ = train_val_test_split_edges(edges, TrainValTest(.2, .2, .2))
- with pytest.raises(ValueError):
- _ = train_val_test_split_edges(edges, None)
- with pytest.raises(ValueError):
- _ = train_val_test_split_edges(edges, (.8, .1, .1))
- with pytest.raises(ValueError):
- _ = train_val_test_split_edges(np.random.randint(0, 10, (10, 2)), TrainValTest(.8, .1, .1))
- with pytest.raises(ValueError):
- _ = train_val_test_split_edges(torch.randint(0, 10, (10, 3)), TrainValTest(.8, .1, .1))
- with pytest.raises(ValueError):
- _ = train_val_test_split_edges(torch.randint(0, 10, (10, 2, 1)), TrainValTest(.8, .1, .1))
- with pytest.raises(ValueError):
- _ = train_val_test_split_edges(None, TrainValTest(.8, .2, .2))
- res = train_val_test_split_edges(edges, TrainValTest(.8, .1, .1))
- assert res.train.shape == (8, 2) and res.val.shape == (1, 2) and \
- res.test.shape == (1, 2)
- res = train_val_test_split_edges(edges, TrainValTest(.8, .0, .2))
- assert res.train.shape == (8, 2) and res.val.shape == (0, 2) and \
- res.test.shape == (2, 2)
- res = train_val_test_split_edges(edges, TrainValTest(.8, .2, .0))
- assert res.train.shape == (8, 2) and res.val.shape == (2, 2) and \
- res.test.shape == (0, 2)
- res = train_val_test_split_edges(edges, TrainValTest(.0, .5, .5))
- assert res.train.shape == (0, 2) and res.val.shape == (5, 2) and \
- res.test.shape == (5, 2)
- res = train_val_test_split_edges(edges, TrainValTest(.0, .0, 1.))
- assert res.train.shape == (0, 2) and res.val.shape == (0, 2) and \
- res.test.shape == (10, 2)
- res = train_val_test_split_edges(edges, TrainValTest(.0, 1., .0))
- assert res.train.shape == (0, 2) and res.val.shape == (10, 2) and \
- res.test.shape == (0, 2)
-
-
- def test_train_val_test_split_edges_02():
- edges = torch.randint(0, 30, (30, 2))
- ratios = TrainValTest(.8, .1, .1)
- res = train_val_test_split_edges(edges, ratios)
- edges = [ tuple(a) for a in edges ]
- res = [ tuple(a) for a in chain(res.train, res.val, res.test) ]
- assert all([ a in edges for a in res ])
-
-
- def test_get_edges_and_degrees_01():
- adj_mat_dense = (torch.rand((10, 10)) > .5)
- adj_mat_sparse = adj_mat_dense.to_sparse()
- edges_dense, degrees_dense = get_edges_and_degrees(adj_mat_dense)
- edges_sparse, degrees_sparse = get_edges_and_degrees(adj_mat_sparse)
- assert torch.all(degrees_dense == degrees_sparse)
- edges_dense = [ tuple(a) for a in edges_dense ]
- edges_sparse = [ tuple(a) for a in edges_dense ]
- assert len(edges_dense) == len(edges_sparse)
- assert all([ a in edges_dense for a in edges_sparse ])
- assert all([ a in edges_sparse for a in edges_dense ])
- # assert torch.all(edges_dense == edges_sparse)
-
-
- def test_prepare_adj_mat_01():
- adj_mat = (torch.rand((10, 10)) > .5)
- adj_mat = adj_mat.to_sparse()
- ratios = TrainValTest(.8, .1, .1)
- _ = prepare_adj_mat(adj_mat, ratios)
-
-
- def test_prepare_adj_mat_02():
- adj_mat = (torch.rand((10, 10)) > .5)
- adj_mat = adj_mat.to_sparse()
- ratios = TrainValTest(.8, .1, .1)
- (adj_mat_train, edges_pos, edges_neg) = prepare_adj_mat(adj_mat, ratios)
- assert isinstance(adj_mat_train, torch.Tensor)
- assert adj_mat_train.is_sparse
- assert adj_mat_train.shape == adj_mat.shape
- assert adj_mat_train.dtype == adj_mat.dtype
- assert isinstance(edges_pos, TrainValTest)
- assert isinstance(edges_neg, TrainValTest)
- for a in ['train', 'val', 'test']:
- for b in [edges_pos, edges_neg]:
- edges = getattr(b, a)
- assert isinstance(edges, torch.Tensor)
- assert len(edges.shape) == 2
- assert edges.shape[1] == 2
-
- # def prepare_adj_mat(adj_mat: torch.Tensor,
- # ratios: TrainValTest) -> Tuple[TrainValTest, TrainValTest]:
- #
- # degrees = adj_mat.sum(0)
- # edges_pos = torch.nonzero(adj_mat)
- #
- # neg_neighbors = fixed_unigram_candidate_sampler(edges_pos[:, 1],
- # len(edges), degrees, 0.75)
- # edges_neg = torch.cat((edges_pos[:, 0], neg_neighbors.view(-1, 1)), 1)
- #
- # edges_pos = train_val_test_split_edges(edges_pos, ratios)
- # edges_neg = train_val_test_split_edges(edges_neg, ratios)
- #
- # return edges_pos, edges_neg
|