Skip to content

Instantly share code, notes, and snippets.

View stemangiola's full-sized avatar

Stefano Mangiola stemangiola

View GitHub Profile
git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
@stemangiola
stemangiola / color_cell_types.R
Last active July 28, 2023 04:27
Color cell types
get_cell_type_color = function(cell_type_vector){
cell_type_vector |>
enframe(value = "harmonised_cell_type") |>
left_join(
tribble(
~harmonised_cell_type, ~palette,
# Theme
friendly_cols <- dittoSeq::dittoColors()
# scale_fill_manual(values = friendly_cols)
# scale_color_manual(values = friendly_cols)
theme_multipanel =
theme_bw() +
theme(
panel.border = element_blank(),
@stemangiola
stemangiola / aggregate_cells.R
Last active November 25, 2020 01:27
code for ABACBS 2020
#' Convert array of quosure (e.g. c(col_a, col_b)) into character vector
#'
#' @keywords internal
#'
#' @importFrom rlang quo_name
#' @importFrom rlang quo_squash
#' @importFrom purrr when
#' @importFrom magrittr equals
#'
#' @param v A array of quosures (e.g. c(col_a, col_b))
@stemangiola
stemangiola / tidy_data_tree.R
Last active June 7, 2019 05:54
tidy_data_tree
ToDataFrameTypeColFull = function(tree, ...){
tree %>%
Clone() %>%
{
t = (.)
foreach(l=1:(t %$% Get("level") %>% max), .combine = bind_rows) %do% {
data.tree::Clone(t) %>%
{ data.tree::Prune(., function(x) x$level <= l + 1); . } %>%
data.tree::ToDataFrameTypeCol(...) %>%
as_tibble
@stemangiola
stemangiola / stan_tidy_tools.R
Created April 16, 2019 23:41
Some useful functions about bayes object parsing
get_bimodality = function(fit) {
fit %>%
tidybayes::gather_draws(exposure_rate[S]) %>% summarise(
bimodal = diptest::dip.test(`.value`) %$% `p.value`
)
}
@stemangiola
stemangiola / rstan_installation.txt
Last active March 21, 2019 04:47
Installation of rstan for big machines
####################################################################
# From within R
####################################################################
####################################################################
# Initialisation of makefiles
####################################################################
dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) dir.create(dotR)
generate_gamma_sum <- function(N, k, theta) {
n_means = length(k)
sums = numeric(N)
for(n in 1:N) sums[n] = sum(rgamma(n_means, shape = k, scale = theta))
list(sums = sums, shape = k, scale = theta)
}
k=c(3,4,5)
theta=c(1, 2, 1)
generate_gamma_sum(10000, c(3,4,5), c(1, 2, 1)) %$%
@stemangiola
stemangiola / approximated_modified_bessel_second_kind_log.R
Created March 11, 2019 05:10
approximated modified bessel second kind log
approximated_modified_bessel_second_kind_log = function(z, v, s = max(0,v-10)){
# s = 0 not approximated
0.5 * ( log(pi) - log(2) ) + (-z - 0.5 * log(z)) +
foreach(j = s:floor(v-0.5), .combine = c) %do% {
lgamma(j + abs(v) - 0.5 + 1) -
@stemangiola
stemangiola / tidy_extensions.R
Last active September 9, 2019 00:31
This is a collection of custom tidy functions
future::plan(future:::multiprocess)
do_and_return = function(tbl, code){
# Execute future
dummy = future::future(code())
# Return
tbl
}