NEUROHACKING IN R

NEUROHACKING IN R
1

R

MEDIUM

last hacked on Sep 21, 2018

Important links:

Plotly Brain Slices

Not so important, buy maybe:


WEEK ONE

Neurohacking

Axial Image

Difference between two images

Why using R?

Why Structural MRI?

DICOM (Digital Imaging and Communications in Medicine)

WEEK TWO

Install Packages

if (!require(devtools)){
    install.packages("devtools")
  }
install.packages("oro.nifti")
install.packages("oro.dicom")
devtools::install_github("muschellij2/fslr")
devtools::install_github("stnava/ITKR")
devtools::install_github("stnava/ANTsR")
devtools::install_github("muschellij2/extrantsr")

Loading Packages

library(oro.nifti)
library(oro.dicom)
library(devtools)
library(muschellij2/fslr)
library(stnava/ITKR)
library(stnava/ANTsR)
library(muschellij2/extrantsr)

Set directory to FLAIR file

setwd("/home/jxr/Neurohacking_data-master/BRAINIX/DICOM/FLAIR")

Ouputs readDICOM

slice=oro.dicom::readDICOM("IM-0001-0011.dcm")
slice
class(slice)
names(slice)
str(slice)
class(slice$hdr)
class((slice$hdr[[1]])
class(slice$img)
class(slice$img[[1]])
dim(slice$img[[1]])

Transposing Data

d <- dim(t(slice$img[[1]]))

DICOM Image

image(1:d[1], 1:d[2], t(slice$img[[1]]), col = gray(0:64/64))

DICOM Image Histogram

hist(slice$img[[1]][,], breaks = 50, xlab = "FLAIR", prob = T, col = rgb(0,0,1,1/4), main = "")

DICOM header

hdr <- slice$hdr[[1]]
names(hdr)

hdr$name

hdr[hdr$name == "PixelSpacing", "value"]

hdr[hdr$name == "FlipAngle", ]

Looking at whole DICOM file

setwd("/home/jxr/Neurohacking_data-master/BRAINIX/DICOM")

all_slices_t1 <- readDICOM("T1/")

dim(all_slices_t1$img[[11]])

hdr <- all_slices_T1$hdr[[11]]

hdr[hdr$name == "PixelSpacing", "value"]

NIfTI (Neuroimaging Informatics Technology Initiative)

From DICOM to NIfTI format

nii_T1 <- dicom2nifti(all_slices_T1)
d <- dim(nii_T1)
class(nii_T1)
image(1:d[1], 1:d[2], nii_T1[,, 13], col = gray(0:64/64), xlab = "", ylab = "")

ReadNIfTI function

library(oro.nifti)
setwd("/home/jxr/Neurohacking_data-master/BRAINIX/NIfTI")
fname <- "Output_3D_File"
writeNIfTI(nim = nii_T1, filename = fname)
list.files(getwd(), pattern = "Output_3D_File")
list.files(getwd(), filename = "T")
nii_T2 <- readNIfTI("T2.nii.gz", reorient = FALSE)
dim(nii_T2)

Basic NIfTI visualizations

print({nii_T1 = readNIfTI(fname = fname)})
image(1:d[1], 1:d[2], nii_T2[,, 13], xlab = "", ylab = "")

Single plot time image visualization

image(nii_T1,z = 11, plot.type = "single")

Orthographic visualization

orthographic(nii_T1, xyz = c(200,220,13))

Histrogram visualization

par(mfrow = c(1,2))
o <- par(mar = c(4,4,0,0))
hist(nii_T1, breaks = 75, prob = T, xlab = "T1 intensities", col = rgb(0,0,1,1/2), main = "")

hist(nii_T1[nii_T1 > 20], breaks = 75, prob = T, xlab = "T1 intensities > 20", col = rgb(0,0,1,1/2), main = ")

Backmapping of one slice

is_btw_300_400 <- ((nii_T1 > 300) & (nii_T1 < 400))
nii_T1_mask <- nii_T1
nii_T1_mask[!is_btw_300_400] = NA
overlay(nii_T1, nii_T1_mask, z = 13, plot.type = "single)

What happens when you do not specify the slice?

*All slices will show, and you won't be able to see them with much detail.

overlay(nii_T1, nii_T1_mask)

Orthographic Backmapping

orthographic(nii_T1, nii_T1_smask), xyz = c(200,200,13), text = "Image overlaid with mask", text.cex = 1.0)

Masking with Kirby21 package


COMMENTS


``` # INSTALLING PACKAGES # install.packages("oro.nifti") # install.packages("oro.dicom") # devtools::install_github("muschellij2/fslr") # devtools::install_github("stnava/ITKR") # devtools::install_github("stnava/ANTsR") # devtools::install_github("muschellij2/extrantsr") # LOADING PACKAGES library(oro.nifti) library(oro.dicom) library(devtools) library(muschellij2/fslr) library(stnava/ITKR) library(stnava/ANTsR) library(muschellij2/extrantsr) # LOADING DATA setwd("/home/dxc/myProjects/repos/Neurohacking_data/BRAINIX/DICOM/FLAIR") # WRANGLING DATA slide=oro.dicom::readDICOM("IM-0001-0011.dcm") slide class(slide) names(slide) str(slide) ```





keep exploring!

back to all projects