Capitol 4

Lab Docs

Find out how to create and organize your content quickly and intuitively.

Subsecțiune a Lab Docs

Reguli în laborator

  1. Siguranță – Investigatorul principal al studiului și utilizatorii desemnați de acesta sunt responsabili pentru a se asigura că laboratorul este un mediu sigur. Vă rugăm să vă revizuiți protocolul experimental și să vă asigurați că este aprobat de către cercetătorul principal. Nu lăsați niciodată un participant singur în laborator. Este responsabilitatea investigatorului principal să se asigure că sunt respectate procedurile de siguranță și alte proceduri.

  2. Amabilitate – Nu scoateți din laborator niciun echipament sau cablu care nu vă aparține. Readuceți la starea inițială orice modificare de echipament (de exemplu, mutarea monitoarelor sau a scaunelor etc.). Nu lăsați echipamente folosite în încăperi (de exemplu, cabluri sau monitoare), deoarece acestea pot cauza artefacte (de exemplu, cu EEG) și accidente.

  3. Curățenie – Păstrați curățenia în laborator (ex: aruncați gunoiul). Ridicați cablurile sau echipamentele care s-au desprins (readuceți-le la locul unde le-ați găsit) atunci când ați terminat de utilizat. Păstrați rafturile organizate și îngrijite. Când ați terminat experimentul, apartamentul de laborator trebuie să arate ca și cum nu ați fi fost niciodată acolo.

  4. Nu depășiți timpul alocat – Sălile trebuie predate următorului grup de utilizatori la începutul sesiunii lor. Acest lucru înseamnă că trebuie să lăsați timp la sfârșitul intervalului de timp pentru a vă îndepărta participantul și a face ordine în zonele pe care le-ați folosit pentru următorul utilizator. Chiar dacă ați început cu întârziere, sesiunea dvs. trebuie să se încheie la timp.

  5. Mâncare și băutură –Nu este permisă prezența alimentelor în laborator, cu excepția cazului în care acestea sunt gustări uscate furnizate participanților la studiu sau dacă acest lucru va face parte din protocolul dvs. experimental. Mâncarea caldă nu este niciodată permisă, deoarece laboratorul este un spațiu închis! Băuturile sunt în regulă doar dacă sunt recipiente acoperite!

  6. Responsabilități – Pe lângă cele menționate mai sus, responsabilitățile includ și completarea condicii (log book), realizarea unei copii de rezervă a datelor dumneavoastră astfel încât acestea să nu fie stocate pe computerele din laborator. Vă rugăm să raportați imediat orice problemă sau echipament stricat investigatorului principal.

Reproducibility: recipes for science

Reproducibility

Replicability in is vital for science, but we may get it every time.

Reproducibility ensures that using the obtained data anyone can achieve the same results.This is not as easy as it seems because it necessitates (1) methodological procedures and analytic pipeline to be explicitly documented in their entirety (like a recipe), and (2) the use of a fully reproducible environment.

To give you an example, here we will reproduce the hexagonal logo (sometimes refereed to as hex-stickers) of our course. Keep in mind that this example is only partial computational reproducibility, as we employ only package management. R version, system packages, operating system can vary and bias our results.

Full computational reproducibility, achievable with Docker containers or NixOS derivations, is beyond the scope of this demo.

Managing packages

For simplicity, we just store package versions within the .Rmd file and use renv for package management and taking care of all other dependencies.

Take note that renv is more powerful then this and should typically be used in a project-centric (i.e. .prj) workflow.

if (!require("renv")) install.packages("renv", dependencies = TRUE)

renv::use(
  "renv@1.0.3",
  "here@1.0.1",
  "dplyr@1.1.4",
  "tidyr@1.3.0",
  "ggseg3d@1.6.3",
  "ggseg@1.6.5",
  "plotly@4.10.3",
  "RColorBrewer@1.1-3",
  "reticulate@1.34.0",
  "magick@2.8.2",
  "cropcircles@0.2.4",
  "hexSticker@0.4.9"
)

Loading packages and setting up

# Packages
library(renv)
library(here)
library(dplyr)
library(tidyr)
library(ggseg)
library(ggseg3d)
library(plotly)
library(RColorBrewer)
library(reticulate)
library(magick)
library(cropcircles)
library(hexSticker)

As we are doing everything within a single .Rmd file. Here we use an unusual way of setting up an working directory for the .Rmd file that can be reproduced on any other system. Nevertheless, the usual way would be to use knitr options to set a root directory.

# Settings 
# If working in .prj directory and not using RStudio
here::set_here()
## File .here already exists in C:\Users\User\c\Github\ns101-website\content\lab_docs\2024-01-20-reproducibility
unloadNamespace("here")   # need new R session or unload namespace for .here file to take precedence over .Rproj
script_path <- file.path(here::here())

# If using RStudio
# script_path <- dirname(rstudioapi::getActiveDocumentContext()$path) 

# Create output folder if it doesn't exist
setwd(script_path)
folder_name <- "output"
if(!exists(folder_name)) {dir.create(folder_name)} 
## Warning in dir.create(folder_name): 'output' already exists
setwd(file.path(script_path, folder_name))

Making an interactive plot

The package aseg_3d helps us build a plotly interactive plot using a brain atlas.

# Make the Brain plot -------------------------------------------------------------------------
scene <- list(eye = list(x = -1.5, y = 0.3, z = 0))

my_aseg <- aseg_3d %>% 
  unnest(cols = ggseg_3d) %>% 
  select(label, colour) %>% 
  mutate(coln = as.integer(as.factor(colour))) %>% 
  mutate(coln = if_else(grepl("Cerebellum|Ventricle|Vent", label), NA_integer_, coln)) %>% 
  mutate(coln = if_else(grepl("CC_", label), 1L, coln)) %>% 
  mutate(col = RColorBrewer::brewer.pal(11, "Paired")[12 - as.integer(as.factor(coln))]) %>% 
  mutate(col = if_else(grepl("Cerebellum", label), "#012169", col))  # UB color

brain_plotly <- 
  ggseg3d(.data = my_aseg, atlas = aseg_3d, 
    colour = "col", 
    na.colour = "#A6CEE3",
    show.legend = TRUE
  ) %>% 
  add_glassbrain(
    hemisphere = "left",  
    colour = "#A6CEE3", # "#cecece"
    opacity = 0.3
  ) %>% 
  remove_axes() %>% 
  pan_camera(camera = scene) %>% 
  hide_colorbar()

You can interact with this visualization of a glass brain showing its subcortical structures.

brain_plotly
# Export to html
# htmlwidgets::saveWidget(brain_plotly, "brain_plotly.html")

Exporting the plot

Exporting plotly graphics is a bit of a hassle, as we need to spawn a python environment in order to use the kaleido library.

## Error : Miniconda is already installed at path "C:/Users/User/AppData/Local/r-miniconda".
## - Use `reticulate::install_miniconda(force = TRUE)` to overwrite the previous installation.

Now we have a .png of our glass brain!

# file.show(here::here("output", "brain_img.png"))
knitr::include_graphics(here::here("output", "brain_img.png"))

Let’s crop around it so we can then fit it inside a hexagonal shape.

# Make the hex sticker -------------------------------------------------------------------------
brain_img <- magick::image_read(here::here("output", "brain_img.png"))

cropcircles::circle_crop(
  images = brain_img,
  border_colour = "#ffffff",
  border_size = 10,
  to = here::here("output", "brain_img_cropped.png")
)
## [1] "C:/Users/User/c/Github/ns101-website/content/lab_docs/2024-01-20-reproducibility/output/brain_img_cropped.png"
brain_img_cropped <- magick::image_read(here::here("output", "brain_img_cropped.png"))
# brain_img_cropped
knitr::include_graphics(here::here("output", "brain_img_cropped.png"))

Now we hexSticker package to make out image into a hex sticker!

hexSticker::sticker(
  brain_img_cropped, 
  package = "UniBuc NS101", p_color="#012169", p_family = "sans",     # don't use: p_fontface = "bold"
  p_size = 14, p_y = 1.5,  
  s_x = 1, s_y = 0.9, s_width = 2, s_height = 1.5,
  h_fill="#ffffff", h_color="#012169", 
  url = "ns101.psychlab.eu", u_color = "#012169", u_size	= 8,
  filename = here::here("output", "brain_hex_bg.png")
)

brain_hex_bg <- magick::image_read(here::here("output", "brain_hex_bg.png"))
knitr::include_graphics(here::here("output", "brain_hex_bg.png"))

A little bit more styling and we are done - we really want the background to be transparent, not white.

fuzz <- 50

brain_hex <- brain_hex_bg %>%             # make background transparent
  magick::image_fill(color = "transparent", refcolor = "white", fuzz = fuzz, point = "+1+1") %>%
  magick::image_fill(color = "transparent", refcolor = "white", fuzz = fuzz, 
             point = paste0("+", image_info(brain_hex_bg)$width-1, "+1")) %>%
  magick::image_fill(color = "transparent", refcolor = "white", fuzz = fuzz, 
             point = paste0("+1", "+", image_info(brain_hex_bg)$height-1)) %>%
  magick::image_fill(color = "transparent", refcolor = "white", fuzz = fuzz, 
             point = paste0("+", image_info(brain_hex_bg)$width-1, "+", image_info(brain_hex_bg)$height-1))
magick::image_write(image = brain_hex, path = here::here("output", "brain_hex.png"))
# file.show(here::here("output", "brain_hex.png"))
knitr::include_graphics(here::here("output", "brain_hex.png"))

If we documented everything well enough and if you have a similar system, there should be a very high probability that by running this .Rmd file, you will re-construct our brain hex logo.