Skip to content

Commit 505abc2

Browse files
authored
Merge pull request #188 from MangiolaLaboratory/fix_parallel_chains_for_pathfinder
If estimate and remove_outliers are calculated with different fit met…
2 parents 126e468 + 325949c commit 505abc2

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: sccomp
22
Title: Tests differences in cell-type proportion for single-cell data, robust to outliers
3-
Version: 1.99.14
3+
Version: 1.99.15
44
Authors@R: c(person("Stefano", "Mangiola", email = "[email protected]",
55
role = c("aut", "cre"))
66
)

R/methods.R

+8-4
Original file line numberDiff line numberDiff line change
@@ -733,11 +733,16 @@ sccomp_remove_outliers.sccomp_tbl = function(.estimate,
733733

734734
create_intercept = FALSE
735735
)),
736-
parallel_chains = ifelse(data_for_model$is_vb, 1, attr(.estimate , "fit")$num_chains()),
736+
parallel_chains = ifelse(
737+
inference_method %in% c("variational", "pathfinder") |
738+
attr(.estimate , "fit") |> is("CmdStanPathfinder"),
739+
1,
740+
attr(.estimate , "fit")$num_chains()
741+
),
737742
threads_per_chain = cores
738743

739744
)
740-
745+
741746
# Free memory
742747
rm(.estimate)
743748

@@ -823,7 +828,6 @@ sccomp_remove_outliers.sccomp_tbl = function(.estimate,
823828
...
824829
)
825830

826-
827831
rng2 = mod_rng |> sample_safe(
828832
generate_quantities_fx,
829833
fit2$draws(format = "matrix"),
@@ -852,7 +856,7 @@ sccomp_remove_outliers.sccomp_tbl = function(.estimate,
852856
create_intercept = FALSE
853857

854858
)),
855-
parallel_chains = ifelse(data_for_model$is_vb, 1, fit2$num_chains()),
859+
parallel_chains = ifelse(inference_method %in% c("variational", "pathfinder"), 1, fit2$num_chains()),
856860
threads_per_chain = cores
857861

858862
)

tests/testthat/test-sccomp_.R

+55
Original file line numberDiff line numberDiff line change
@@ -823,4 +823,59 @@ test_that("sample ID malformed", {
823823

824824
})
825825

826+
test_that("LOO", {
827+
skip_cmdstan()
828+
829+
library(loo)
830+
831+
# Fit first model
832+
model_with_factor_association =
833+
seurat_obj |>
834+
sccomp_estimate(
835+
formula_composition = ~ type,
836+
.sample = sample,
837+
.cell_group = cell_group,
838+
inference_method = "hmc",
839+
enable_loo = TRUE
840+
) |>
841+
expect_no_error()
842+
843+
# Fit second model
844+
model_without_association =
845+
seurat_obj |>
846+
sccomp_estimate(
847+
formula_composition = ~ 1,
848+
.sample = sample,
849+
.cell_group = cell_group,
850+
inference_method = "hmc",
851+
enable_loo = TRUE
852+
) |>
853+
expect_no_error()
854+
855+
# Compare models
856+
loo_compare(
857+
attr(model_with_factor_association, "fit")$loo(),
858+
attr(model_without_association, "fit")$loo()
859+
) |>
860+
suppressWarnings() |>
861+
expect_no_error()
862+
863+
})
864+
865+
866+
test_that("use two methods", {
867+
868+
skip_cmdstan()
869+
870+
seurat_obj |>
871+
sccomp_estimate(
872+
formula_composition = ~ 1,
873+
.sample = sample,
874+
.cell_group = cell_group,
875+
inference_method = "hmc"
876+
) |>
877+
sccomp_remove_outliers(inference_method = "pathfinder") |>
878+
expect_no_error()
879+
880+
})
826881

0 commit comments

Comments
 (0)