ucl-ihi / CodeClub

Repository for all things IHI CodeClub
https://ucl-ihi.github.io/CodeClub/
3 stars 5 forks source link

Histogram Issue #15

Open issylarken28 opened 5 years ago

issylarken28 commented 5 years ago
#set directory
setwd("C:/Users/larke/OneDrive/Desktop/R/R")

#step 1 - import the cilia text file

#import text file #tabs delimited
my_data <- read.csv("pixel intensity data tab.txt", header = TRUE,  sep='\t') 

#testing data class
class(my_data)

#renames columns
names(my_data)[1]<-"Frame"
names(my_data)[2]<-"Mean1"

#step 2 - fourier transform of the values
#will use fft (fast fourier transform)

#will first test fft on one column
Mean1four = fft(my_data$Mean1)

#now do the fft on whole dataframe
fourierd = apply(my_data,2,function(x) fft(as.numeric(x))) #it's now a matrix

#turn it into a data frame
fourierdat = as.data.frame(fourierd)

#fix frame numbers (they were also fft-ed)
fourierdat$Frame = seq(1:256)
#Error in `$<-.data.frame`(`*tmp*`, Frame, value = 1:512) : 
# replacement has 512 rows, data has 256
# Therefore IL canged "(1:512)" to "(1:256)

#step 3 - get the absolute value of the complex numbers resulting from the fft
#in excel this is done using =IMABS(range)

absTest = abs(fourierdat$Mean1) #testing on one column

absTestAll = abs(fourierdat) #converts all the complex numbers to absolute numbers

#step 4 - calculating the CBF

#if frequency resolution is = 0.94
#if is not 0.94, replace with correct value

FRadjusted = absTestAll$Frame*0.94

absTestAll$Frame = FRadjusted #this updates the frame to 0.94

#Pre-defining the matrix
HisDat <- 1

# For each column of data, find the max number & corrisponding row number.
# multiply the two new values together -> HisDat (Histogram Data)
for(i in 1:lengths(absTestAll)){
  MaxNum <- max(absTestAll[[i]]);
  MaxRow <- which(grepl(max(absTestAll[[i]]), absTestAll[[i]]))
  HisDat[i] <- MaxNum*MaxRow
}

# Plot the Histogram
hist(HisDat,
     breaks=256)

#     multiply max ab no. by row number and frequency resolution
#     to result in 1600 data points which = CBF in 1600 areas of interest
|
#Playing with my histogram
histinfo <- hist(HisDat)

#step 5 - CBF Histogram

Data: pixel intensity data tab.txt

issylarken28 commented 5 years ago

Hi @alhenry, do you have the code for this we worked out during code club the other day?

alhenry commented 5 years ago

Hi @issylarken28 here's the code and the output (I removed some parts to make it more concise). Hope this helps

#import text file
my_data <- read.csv("https://github.com/ucl-ihi/CodeClub/files/3579119/pixel.intensity.data.tab.txt", header = TRUE,  sep='\t') 

#now do the fft on whole dataframe
fourierd <- apply(my_data,2,function(x) fft(as.numeric(x))) #it's now a matrix

#turn it into a data frame
fourierdat <- as.data.frame(fourierd)

#step 3 - get the absolute value of the complex numbers resulting from the fft
absTestAll <- abs(fourierdat) 

# histogram
histData <- sapply(absTestAll[2:nrow(absTestAll),], which.max) * 0.94
hist(histData[2:length(histData)], breaks = 1000, xlim = c(0,20), ylim=c(0,150))

Created on 2019-09-10 by the reprex package (v0.3.0)