Skip to content

Commit

Permalink
added average spectra using just pixel IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
prafols committed Feb 21, 2020
1 parent 0165ef5 commit 5bd6e29
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export(PlotClusterImage)
export(PlotTICImage)
export(PlotValues)
export(ROIAverageSpectra)
export(ROIAverageSpectraByIds)
export(ReadBrukerRoiXML)
export(SaveMsiData)
export(SortIDsByAcquisition)
Expand Down
50 changes: 50 additions & 0 deletions R/librMSIdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,56 @@ ROIAverageSpectra <- function( img, roi_list )
return(rois_avg)
}

#' ROIAverageSpectraByIds.
#'
#' Calculates the average spectrum within each roi specified by a vector of pixel ID's
#'
#' @param img an rMSI object.
#' @param Ids Identifiers of spectra to use for average calculation.
#'
#' @return the ROI average spectrum.
#' @export
#'
ROIAverageSpectraByIds <- function( img, Ids )
{
cat("Calculating Average Spectra of slected pixels...\n")

roi_cubes <- getCubeRowFromIds(img, Ids)
roi_avg <- rep(0, length(img$mass))

pb <- txtProgressBar(min = 0, max = length(img$data), initial = 0, style = 3)
LastId <- 0
for( ic in 1:length(img$data) )
{
setTxtProgressBar(pb, ic)
dm <- img$data[[ic]][,] #Load the complete data cube
current_ids <- (LastId+1):(LastId+nrow(dm))

idCube <- which(unlist(lapply(roi_cubes, function(x){ x$cube})) == ic, arr.ind = T)
if( length(idCube) > 0 )
{
idRows <- roi_cubes[[idCube]]$row
if(length(idRows) > 1)
{
roi_avg <- roi_avg + apply(dm[idRows,], 2, sum)
}
else
{
roi_avg <- roi_avg + dm[idRows,]
}
}

LastId <- current_ids[length(current_ids)]
}

#And divide by the pixel count
roi_avg <- roi_avg / length(Ids)

close(pb)

return(roi_avg)
}

#' uuid.
#'
#' Generates a timecode-based 16-bytes UUID.
Expand Down
19 changes: 19 additions & 0 deletions man/ROIAverageSpectraByIds.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5bd6e29

Please sign in to comment.