Skip to content

Commit

Permalink
updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffersonfparil committed Jun 7, 2024
1 parent 17e1174 commit bf9b492
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.github/
exec/
exec/
_libs/
shell.nix
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ rsconnect/
# Vscode
.vscode

# Nix stuff
_libs/

# Test files
exec/slurm-*.out
exec/output/
Expand Down
48 changes: 48 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{ pkgs ? import <nixpkgs> { } }:
with pkgs;
let
simquantgen = pkgs.rPackages.buildRPackage {
name = "simquantgen";
src = pkgs.fetchFromGitHub {
owner = "jeffersonfparil";
repo = "simquantgen";
rev = "fed3612a22afa70e3bea758addbe0da421fad7ce";
sha256 = "sha256-ZGGKV7eR6u7ll4ulejyS0hoji20s4tIjqiyncalZ3Og=";
};
propagatedBuildInputs = with rPackages; [
MASS
doParallel
foreach
txtplot
];
}; ### Use lib.fakeSha256 on "sha256" to get the correct code from the error messages
my-r-pkgs = rWrapper.override {
packages = with rPackages; [
remotes
devtools
testthat
vcfR
txtplot
glmnet
BGLR
sommer
MASS
parallel
doParallel
foreach
simquantgen
];
};
in mkShell {
buildInputs = with pkgs; [git glibcLocales openssl which openssh curl wget ];
inputsFrom = [ my-r-pkgs ];
shellHook = ''
mkdir -p "$(pwd)/_libs"
export R_LIBS_USER="$(pwd)/_libs"
'';
GIT_SSL_CAINFO = "${cacert}/etc/ssl/certs/ca-bundle.crt";
# LOCALE_ARCHIVE = stdenv.lib.optionalString stdenv.isLinux
# "${glibcLocales}/lib/locale/locale-archive";
}

### nix-shell --run bash --pure
8 changes: 4 additions & 4 deletions tests/testthat/test-cross_validation.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ test_that("fn_cross_validation_within_population", {
list_merged = fn_merge_genotype_and_phenotype(G=G, list_pheno=list_pheno, COVAR=COVAR, verbose=TRUE)
fname_within_Rds = fn_cross_validation_within_population(list_merged, n_folds=2, n_reps=1, vec_models_to_test=c("ridge","lasso"), verbose=TRUE)
list_within = readRDS(fname_within_Rds)
expect_equal(sum(dim(list_within$METRICS_WITHIN_POP) == c(3*2*1*2, 19)), 2)
expect_equal(sum(dim(list_within$METRICS_WITHIN_POP) == c(3*2*1*2, 20)), 2)
expect_equal(sum(dim(list_within$YPRED_WITHIN_POP) == c(100*2, 8)), 2)
expect_equal(mean(list_within$METRICS_WITHIN_POP$corr) < 0.5, TRUE)
expect_equal(cor(list_within$YPRED_WITHIN_POP$y_true, list_within$YPRED_WITHIN_POP$y_pred) < 0.5, TRUE)
Expand All @@ -93,7 +93,7 @@ test_that("fn_cross_validation_across_populations_bulk", {
list_merged = fn_merge_genotype_and_phenotype(G=G, list_pheno=list_pheno, COVAR=COVAR, verbose=TRUE)
fname_across_bulk_Rds = fn_cross_validation_across_populations_bulk(list_merged, n_folds=2, n_reps=1, vec_models_to_test=c("ridge","lasso"), verbose=TRUE)
list_across_bulk = readRDS(fname_across_bulk_Rds)
expect_equal(sum(dim(list_across_bulk$METRICS_ACROSS_POP_BULK) == c(1*2*1*2, 19)), 2)
expect_equal(sum(dim(list_across_bulk$METRICS_ACROSS_POP_BULK) == c(1*2*1*2, 20)), 2)
expect_equal(sum(dim(list_across_bulk$YPRED_ACROSS_POP_BULK) == c(100*2, 8)), 2)
expect_equal(mean(list_across_bulk$METRICS_ACROSS_POP_BULK$corr) < 0.5, TRUE)
expect_equal(cor(list_across_bulk$YPRED_ACROSS_POP_BULK$y_true, list_across_bulk$YPRED_ACROSS_POP_BULK$y_pred) < 0.5, TRUE)
Expand All @@ -108,7 +108,7 @@ test_that("fn_cross_validation_across_populations_pairwise", {
list_merged = fn_merge_genotype_and_phenotype(G=G, list_pheno=list_pheno, COVAR=COVAR, verbose=TRUE)
fname_across_pairwise_Rds = fn_cross_validation_across_populations_pairwise(list_merged, vec_models_to_test=c("ridge","lasso"), verbose=TRUE)
list_across_pairwise = readRDS(fname_across_pairwise_Rds)
expect_equal(sum(dim(list_across_pairwise$METRICS_ACROSS_POP_PAIRWISE) == c((3*(3-1))*1*2, 19)), 2)
expect_equal(sum(dim(list_across_pairwise$METRICS_ACROSS_POP_PAIRWISE) == c((3*(3-1))*1*2, 20)), 2)
expect_equal(sum(dim(list_across_pairwise$YPRED_ACROSS_POP_PAIRWISE) == c((3*(3-1))*(100/3)*2, 8)), 2)
expect_equal(mean(list_across_pairwise$METRICS_ACROSS_POP_PAIRWISE$corr) < 0.5, TRUE)
expect_equal(cor(list_across_pairwise$YPRED_ACROSS_POP_PAIRWISE$y_true, list_across_pairwise$YPRED_ACROSS_POP_PAIRWISE$y_pred) < 0.5, TRUE)
Expand All @@ -123,7 +123,7 @@ test_that("fn_cross_validation_across_populations_lopo", {
list_merged = fn_merge_genotype_and_phenotype(G=G, list_pheno=list_pheno, COVAR=COVAR, verbose=TRUE)
fname_across_lopo_Rds = fn_cross_validation_across_populations_lopo(list_merged, vec_models_to_test=c("ridge","lasso"), verbose=TRUE)
list_across_lopo = readRDS(fname_across_lopo_Rds)
expect_equal(sum(dim(list_across_lopo$METRICS_ACROSS_POP_LOPO) == c(3*1*2, 19)), 2)
expect_equal(sum(dim(list_across_lopo$METRICS_ACROSS_POP_LOPO) == c(3*1*2, 20)), 2)
expect_equal(sum(dim(list_across_lopo$YPRED_ACROSS_POP_LOPO) == c(3*(100/3)*2, 8)), 2)
expect_equal(mean(list_across_lopo$METRICS_ACROSS_POP_LOPO$corr) < 0.9, TRUE)
expect_equal(cor(list_across_lopo$YPRED_ACROSS_POP_LOPO$y_true, list_across_lopo$YPRED_ACROSS_POP_LOPO$y_pred) < 0.9, TRUE)
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-models.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test_that("fn_ols", {
vec_idx_training = sample(c(1:n), floor(n/2))
vec_idx_validation = c(1:n)[!(c(1:n) %in% vec_idx_training)]
list_ols = fn_ols(list_merged, vec_idx_training, vec_idx_validation, verbose=TRUE)
expect_equal(list_ols$list_perf$corr < 0.5, TRUE)
expect_equal(list_ols$list_perf$corr < 0.9, TRUE)
expect_equal(list_ols$list_perf$corr, cor(list_ols$df_y_validation$y_true, list_ols$df_y_validation$y_pred))
expect_equal(nrow(list_ols$df_y_validation), length(vec_idx_validation))
expect_equal(list_ols$n_non_zero, ncol(G))
Expand All @@ -29,7 +29,7 @@ test_that("fn_ridge", {
vec_idx_training = sample(c(1:n), floor(n/2))
vec_idx_validation = c(1:n)[!(c(1:n) %in% vec_idx_training)]
list_ridge = fn_ridge(list_merged, vec_idx_training, vec_idx_validation, verbose=TRUE)
expect_equal(list_ridge$list_perf$corr < 0.5, TRUE)
expect_equal(list_ridge$list_perf$corr < 0.9, TRUE)
expect_equal(list_ridge$list_perf$corr, cor(list_ridge$df_y_validation$y_true, list_ridge$df_y_validation$y_pred))
expect_equal(nrow(list_ridge$df_y_validation), length(vec_idx_validation))
expect_equal(sum(list_ridge$vec_effects != 0.0), 1+ncol(G))
Expand All @@ -46,7 +46,7 @@ test_that("fn_lasso", {
vec_idx_training = sample(c(1:n), floor(n/2))
vec_idx_validation = c(1:n)[!(c(1:n) %in% vec_idx_training)]
list_lasso = fn_lasso(list_merged, vec_idx_training, vec_idx_validation, verbose=TRUE)
expect_equal(list_lasso$list_perf$corr < 0.5, TRUE)
expect_equal(list_lasso$list_perf$corr < 0.9, TRUE)
expect_equal(list_lasso$list_perf$corr, cor(list_lasso$df_y_validation$y_true, list_lasso$df_y_validation$y_pred))
expect_equal(nrow(list_lasso$df_y_validation), length(vec_idx_validation))
expect_equal(sum(list_lasso$vec_effects != 0.0) < 1+ncol(G), TRUE)
Expand All @@ -63,7 +63,7 @@ test_that("fn_elastic_net", {
vec_idx_training = sample(c(1:n), floor(n/2))
vec_idx_validation = c(1:n)[!(c(1:n) %in% vec_idx_training)]
list_elastic_net = fn_elastic_net(list_merged, vec_idx_training, vec_idx_validation, verbose=TRUE)
expect_equal(list_elastic_net$list_perf$corr < 0.5, TRUE)
expect_equal(list_elastic_net$list_perf$corr < 0.9, TRUE)
expect_equal(list_elastic_net$list_perf$corr, cor(list_elastic_net$df_y_validation$y_true, list_elastic_net$df_y_validation$y_pred))
expect_equal(nrow(list_elastic_net$df_y_validation), length(vec_idx_validation))
expect_equal(sum(list_elastic_net$vec_effects != 0.0) < 1+ncol(G), TRUE)
Expand All @@ -80,7 +80,7 @@ test_that("fn_Bayes_A", {
vec_idx_training = sample(c(1:n), floor(n/2))
vec_idx_validation = c(1:n)[!(c(1:n) %in% vec_idx_training)]
list_Bayes_A = fn_Bayes_A(list_merged, vec_idx_training, vec_idx_validation, verbose=TRUE)
expect_equal(list_Bayes_A$list_perf$corr < 0.5, TRUE)
expect_equal(list_Bayes_A$list_perf$corr < 0.9, TRUE)
expect_equal(list_Bayes_A$list_perf$corr, cor(list_Bayes_A$df_y_validation$y_true, list_Bayes_A$df_y_validation$y_pred))
expect_equal(nrow(list_Bayes_A$df_y_validation), length(vec_idx_validation))
expect_equal(sum(list_Bayes_A$vec_effects != 0.0), ncol(G))
Expand All @@ -97,7 +97,7 @@ test_that("fn_Bayes_B", {
vec_idx_training = sample(c(1:n), floor(n/2))
vec_idx_validation = c(1:n)[!(c(1:n) %in% vec_idx_training)]
list_Bayes_B = fn_Bayes_B(list_merged, vec_idx_training, vec_idx_validation, verbose=TRUE)
expect_equal(list_Bayes_B$list_perf$corr < 0.5, TRUE)
expect_equal(list_Bayes_B$list_perf$corr < 0.9, TRUE)
expect_equal(list_Bayes_B$list_perf$corr, cor(list_Bayes_B$df_y_validation$y_true, list_Bayes_B$df_y_validation$y_pred))
expect_equal(nrow(list_Bayes_B$df_y_validation), length(vec_idx_validation))
expect_equal(sum(list_Bayes_B$vec_effects != 0.0), ncol(G))
Expand All @@ -114,7 +114,7 @@ test_that("fn_Bayes_C", {
vec_idx_training = sample(c(1:n), floor(n/2))
vec_idx_validation = c(1:n)[!(c(1:n) %in% vec_idx_training)]
list_Bayes_C = fn_Bayes_C(list_merged, vec_idx_training, vec_idx_validation, verbose=TRUE)
expect_equal(list_Bayes_C$list_perf$corr < 0.5, TRUE)
expect_equal(list_Bayes_C$list_perf$corr < 0.9, TRUE)
expect_equal(list_Bayes_C$list_perf$corr, cor(list_Bayes_C$df_y_validation$y_true, list_Bayes_C$df_y_validation$y_pred))
expect_equal(nrow(list_Bayes_C$df_y_validation), length(vec_idx_validation))
expect_equal(sum(list_Bayes_C$vec_effects != 0.0), ncol(G))
Expand All @@ -131,7 +131,7 @@ test_that("fn_gBLUP", {
vec_idx_training = sample(c(1:n), floor(n/2))
vec_idx_validation = c(1:n)[!(c(1:n) %in% vec_idx_training)]
list_gBLUP = fn_gBLUP(list_merged, vec_idx_training, vec_idx_validation, verbose=TRUE)
expect_equal(list_gBLUP$list_perf$corr < 0.5, TRUE)
expect_equal(list_gBLUP$list_perf$corr < 0.9, TRUE)
expect_equal(list_gBLUP$list_perf$corr, cor(list_gBLUP$df_y_validation$y_true, list_gBLUP$df_y_validation$y_pred))
expect_equal(nrow(list_gBLUP$df_y_validation), length(vec_idx_validation))
expect_equal(sum(list_gBLUP$vec_effects != 0.0), 1+nrow(G))
Expand Down

0 comments on commit bf9b492

Please sign in to comment.