Skip to content

Commit

Permalink
Change the package name from "whims" to "MRIreduce"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinyao Tian committed Sep 13, 2024
1 parent b4696b0 commit 376def9
Show file tree
Hide file tree
Showing 26 changed files with 273 additions and 46 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .Rproj.user/6A9A55E1/pcs/files-pane.pper
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
"ascending": true
}
],
"path": "~/Desktop/WHIMs/R"
"path": "~/Desktop/MRIreduce"
}
12 changes: 6 additions & 6 deletions .Rproj.user/6A9A55E1/pcs/windowlayoutstate.pper
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"left": {
"splitterpos": 345,
"splitterpos": 269,
"topwindowstate": "NORMAL",
"panelheight": 682,
"windowheight": 720
"panelheight": 663,
"windowheight": 701
},
"right": {
"splitterpos": 373,
"splitterpos": 79,
"topwindowstate": "NORMAL",
"panelheight": 682,
"windowheight": 720
"panelheight": 663,
"windowheight": 701
}
}
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Package: whims
Package: MRIreduce
Type: Package
Title: NeuroPartitioner: An R Package for ROI-Based Transformation of Neuroimages into High-Dimensional Data Frames
Title: MRIreduce: An R Package for ROI-Based Transformation of Neuroimages into High-Dimensional Data Frames
Version: 0.1.0
Authors@R: c(
person("Joshua", "Milstein", email = "[email protected]", role = c("aut")),
person("Jinyao", "Tian", email = "[email protected]", role = c("aut", "cre"))
)
Maintainer: Jinyao Tian <[email protected]>, Joshua Milstein <[email protected]>
Description: NeuroPartitioner is an advanced R package designed to convert NIfTI format T1/FL neuroimages into structured, high-dimensional 2D data frames, focusing on region of interest (ROI) based processing. This package incorporates a key algorithm called ‘partition’, which offers a fast, flexible framework for agglomerative partitioning based on the Direct-Measure-Reduce approach. This method ensures that each reduced variable maintains a user-specified minimum level of information while being interpretable, as each maps uniquely to one variable in the reduced dataset. The ‘partition’ algorithm, detailed in Millstein et al. (2020), allows for customization in variable selection, measurement of information loss, and data reduction methods. NeuroPartitioner is indispensable for researchers requiring efficient, accurate preparation of neuroimaging data for detailed statistical analysis and machine learning applications, enhancing the interpretability and utility of neuroimaging studies.
Description: MRIreduce is an advanced R package designed to convert NIfTI format T1/FL neuroimages into structured, high-dimensional 2D data frames, focusing on region of interest (ROI) based processing. This package incorporates a key algorithm called ‘partition’, which offers a fast, flexible framework for agglomerative partitioning based on the Direct-Measure-Reduce approach. This method ensures that each reduced variable maintains a user-specified minimum level of information while being interpretable, as each maps uniquely to one variable in the reduced dataset. The ‘partition’ algorithm, detailed in Millstein et al. (2020), allows for customization in variable selection, measurement of information loss, and data reduction methods. NeuroPartitioner is indispensable for researchers requiring efficient, accurate preparation of neuroimaging data for detailed statistical analysis and machine learning applications, enhancing the interpretability and utility of neuroimaging studies.
License: MIT + file LICENSE
Imports:
Rcpp,
Expand Down Expand Up @@ -35,4 +35,4 @@ Suggests:
knitr,
rmarkdown
VignetteBuilder: knitr
URL: https://jtian123.github.io/WHIMs/
URL: https://github.com/jtian123/MRIreduce
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(PartitionPipeline)
export(eve_Fl)
export(eve_T1)
export(map_feature2_loc)
export(run_partition_pipeline)
export(suppar)
import(R6)
Expand Down
2 changes: 1 addition & 1 deletion R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

partagg <- function(grpl1, corrbin, B) {
.Call('_whims_partagg', PACKAGE = 'whims', grpl1, corrbin, B)
.Call(`_MRIreduce_partagg`, grpl1, corrbin, B)
}

4 changes: 3 additions & 1 deletion R/eve_Fl.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
#' Volumes are calculated based on the segmented tissues.
#'
#' @examples
#' ## Not run:
#' eve_Fl("path/to/your/flair/image.nii.gz",
#' "path/to/output/",
#' "/usr/local/fsl",
#' "NIFTI_GZ")
#' ## End(Not run)
#'
#' @importFrom neurobase readnii writenii
#' @importFrom extrantsr bias_correct fslbet_robust
Expand Down Expand Up @@ -114,7 +116,7 @@ eve_Fl <- function(fpath,outpath, fsl_path, fsl_outputtype = "NIFTI_GZ") {

# output array intensities and tissues registered to Eve
outp <- vector("list", 3)
names(outp) <- c("intenisites", "tissues", "brain_volume_cm3")
names(outp) <- c("intensities", "tissues", "brain_volume_cm3")
outp[[1]] <- adat
outp[[2]] <- adat_fast
outp[[3]] <- c(vres, bv, total_icv)
Expand Down
4 changes: 3 additions & 1 deletion R/eve_T1.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
#' Each step logs its progress with timestamps, aiding in debugging and optimization of processing times.
#'
#' @examples
#' ## Not run:
#' eve_T1("path/to/your/image.nii.gz", "path/to/output", "/usr/local/fsl", "NIFTI_GZ")
#' ## End(Not run)
#'
#' @importFrom neurobase readnii writenii
#' @importFrom fslr fslreorient2std fsl_biascorrect fslbet fast fslstats
Expand Down Expand Up @@ -120,7 +122,7 @@ eve_T1 <- function(fpath, outpath, fsl_path = '/Users/jinyaotian/fsl', fsl_outpu

# output array intensities and tissues registered to Eve
outp <- vector("list", 3)
names(outp) <- c("intenisites", "tissues", "brain_volume_cm3")
names(outp) <- c("intensities", "tissues", "brain_volume_cm3")
outp[[1]] <- adat
outp[[2]] <- adat_fast
outp[[3]] <- c(vres,bv, total_icv)
Expand Down
45 changes: 38 additions & 7 deletions R/funcs.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ load_required_data <- function(filename, package_name, envir = .GlobalEnv) {
iproc <- function(tind, nfl, main_dir, outp_volume = TRUE) {
# Load Eve labels
# Load necessary data
load_required_data("eve_label_array.RData", "whims")
load_required_data("eve_label_info_dataframe.RData", "whims")
load_required_data("eve_label_array.RData", "MRIreduce")
load_required_data("eve_label_info_dataframe.RData", "MRIreduce")
# Initial checks and setup
if (!dir.exists(main_dir)) {
stop("main_dir does not exist: ", main_dir)
Expand Down Expand Up @@ -283,7 +283,7 @@ tissue_segment <- function(liste, thresh_vec, tind, tissue_type, main_dir) {
colnames(count_voxel_matrix) <- par_map[, 'variable']

intensity_df <- as.data.frame(intensity_mean_matrix)
intensity_df$fname<- img_name
row.names(intensity_df)<- img_name
volume_df <- as.data.frame(count_voxel_matrix)
volume_df$fname <- img_name

Expand All @@ -300,8 +300,12 @@ tissue_segment <- function(liste, thresh_vec, tind, tissue_type, main_dir) {
merged_df[cols_to_multiply] <- merged_df[cols_to_multiply] * merged_df$unit_voxel
# Resulting data frame has the product, and you might want to remove the extra unit_voxel column if not needed anymore
final_df <- merged_df[, !names(merged_df) %in% c("unit_voxel", "brainVolume")]
row.names(final_df) = final_df$fname
final_df$fname = NULL
saveRDS(final_df, file = volume_file_path)
} else {
row.names(volume_df) = volume_df$fname
volume_df$fname = NULL
saveRDS(volume_df, file = volume_file_path)
}

Expand All @@ -311,15 +315,38 @@ tissue_segment <- function(liste, thresh_vec, tind, tissue_type, main_dir) {
rslts <- lapply(thresh_vec, process_threshold)
}

##Test
# main_dir = '/Users/jinyaotian/Downloads/whims_test'
# roi = 'inferior_frontal_gyrus_left'
# dep_list_path = file.path(main_dir, "dep_list", paste0(roi,'.rds'))
# listes <- readRDS(dep_list_path)
# cat("tissue segmentation for all sublists based on super partitions.\n")
# lapply(listes, function(sub_list) {
# tissue_segment(liste = sub_list,thresh_vec = 0.8,tind = 5, tissue_type =2, main_dir = main_dir )
# })

#Combine by tissue type for each threshold and roi
Cmb_tissue_type <- function(thresh, roi, tissue_type, main_dir){
modify_data <- function(data, roi_name) {
colnames(data) <- paste(roi_name, colnames(data), sep = "_")
return(data)
}
read_rds_file <- function(file_path, intensity = TRUE) {
read_rds_file <- function(file_path) {
# Extract the file name
doc_name <- basename(file_path)
# Extract the number from the file name using regex
number <- gsub("\\D", "", doc_name) # Remove non-numeric characters
# Read the .rds file
df <- readRDS(file_path)
# Update the column names based on whether they contain "reduced_var"
colnames(df) <- sapply(colnames(df), function(col_name) {
if (grepl("reduced_var", col_name)) {
return(paste0("module", number, "_", col_name))
} else {
return(col_name)
}
})
return(df) # Return the modified dataframe
}

if (tissue_type == 3){
Expand All @@ -339,12 +366,10 @@ Cmb_tissue_type <- function(thresh, roi, tissue_type, main_dir){
volume_files_with_prefix <- list.files(path = directory, pattern = paste0("^volume_"), full.names = TRUE)

data_frames_intensity <- lapply(intensity_files_with_prefix, read_rds_file)
data_frames_volume <- lapply(volume_files_with_prefix, function(file) read_rds_file(file, intensity = FALSE))
data_frames_volume <- lapply(volume_files_with_prefix, read_rds_file)

processed_data_frames_intensity <- lapply(data_frames_intensity, function(ls_) {
df <- ls_
row.names(df) <- df$fname
df$fname <- NULL
modify_data(df, roi_name = roi)
})

Expand All @@ -366,6 +391,9 @@ Cmb_tissue_type <- function(thresh, roi, tissue_type, main_dir){
saveRDS(combined_data_frame_volume, file = file.path(save.dir,"volume.rds"))
}

##Test
#Cmb_tissue_type(thresh = 0.8, roi = roi, tissue_type = 2, main_dir = main_dir)

#Process independent variables from Super-Partition
process_indep_variables <- function(indep_list, tissue_type, tind, roi, main_dir) {
if (tissue_type == 3){
Expand Down Expand Up @@ -463,6 +491,9 @@ Cmb_indep_with_dep <- function(tissue_type, roi, thresh, main_dir){
}
}

##Test
#Cmb_indep_with_dep(tissue_type = 2, roi = "inferior_frontal_gyrus_left", thresh = 0.8, main_dir = '/Users/jinyaotian/Downloads/whims_test')

#Combine by ROI
concat_reduced_var <- function(roi, thresh, main_dir){
#Structure of dir: /roi/thresh
Expand Down
68 changes: 68 additions & 0 deletions R/map_feature2_loc.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#' Map Reduced Feature Back to Brain Image Voxel Locations
#'
#' This function maps a given feature name to voxel locations by identifying the pattern in the feature name.
#' If the feature name contains "reduced_var", it follows the format "Roi_module_reduced_var" and this indicates that it is a reduced feature. Otherwise, it follows
#' the format "Roi_Vnumber", which is not a reduced feature by Partition. Based on the pattern, the function extracts the appropriate region of interest (ROI) and
#' retrieves voxel locations from intensity data.
#'
#' @param feature_name String. A feature name.
#' @param threshold a numeric between 0 and 1. The Partition threshold value applied to the data.
#' @param main_dir String. The main directory containing the data files.
#'
#' @return A data frame containing the voxel locations (x,y,z Coordinates) corresponding to the extracted mapping column.
#'
#' @details
#' Each voxel in a brain image corresponds to a specific feature, establishing a one-to-one mapping between a voxel and a feature. This relationship allows us
#' to localize particular brain features to specific brain areas at the voxel level, enabling visualization of these features. However, after applying Super-Partition and Partition techniques,
#' multiple brain features are aggregated into a single reduced feature as part of a data reduction process. The goal of the following function is to relate the reduced feature back to its component features,
#' and subsequently identify the brain image voxels to which those component features are mapped.
#' This function performs the following steps:
#'
#' 1. **Check Feature Name Format:** It first checks if the `feature_name` contains "reduced_var". If it does, the function assumes the format "Roi_module_reduced_var", otherwise it assumes the format "Roi_Vnumber".
#'
#' 2. **Extract ROI and Mapping Information:**
#' - For the "Roi_module_reduced_var" format, the ROI and module number are extracted, and the corresponding partition file is read to retrieve the mapping vector.
#' - For the "Roi_Vnumber" format, the `V` number is extracted from the feature name and used as the mapping vector.
#'
#' 3. **Extract Voxel Locations:** The mapping vector is then used to extract voxel locations from the intensity data.
#'
#' @examples
#' \dontrun{
#' loc_df <- map_feature2_loc(feature_name = "inferior_frontal_gyrus_left_module4_reduced_var_13", threshold = 0.8, main_dir = "/path/to/data")
#' }
#'
#' @export
map_feature2_loc <- function(feature_name, threshold,main_dir) {
if (grepl("reduced_var", feature_name)) {
# Extract the ROI for the format "Roi_module_reduced_var"
roi <- sub("_module.*", "", feature_name) # Remove everything after and including "_module"
module = sub(".*module(\\d+).*", "\\1", feature_name)
variable_name = sub(".*(reduced_var_\\d+).*", "\\1", feature_name)
file_path = file.path(main_dir, "partition", roi, threshold, "map", paste0("Par_intensities_map_", module, ".rds"))
df = readRDS(file_path)
vec <- df[df$variable == variable_name, "mapping"][[1]]
} else {
# Extract the ROI for the format "Roi_Vnumber"
roi <- sub("_V\\d+.*", "", feature_name) # Remove everything after and including "_V<number>"
vec <- sub(".*(V\\d+).*", "\\1", feature_name)
}

# Extract voxel locations
extract_mapping_voxel <- function(tind, roi, Cols) {
file_name <- paste0("intensities_", roi, "_", tind,".rds")
df <- readRDS(file.path(main_dir, "intensities", file_name))
locs <- df[1:3, Cols, drop = FALSE]
return(locs)
}
load_required_data("eve_label_info_dataframe.RData", "whims")
tind = lab_df[lab_df$text_label == roi, "integer_label"]
# Extract the voxel locations
loc_df <- extract_mapping_voxel(tind, roi, vec)
return(loc_df)
}

##Test
# feature_name = "inferior_frontal_gyrus_left_module11_reduced_var_5"
# threshold = 0.8
# main_dir = '/Users/jinyaotian/Downloads/whims_test'
# loc_df = map_feature2_loc(feature_name = feature_name, threshold = threshold, main_dir = main_dir)
4 changes: 2 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "NeuroPartitioner"
title: "MRIreduce"
output: github_document
---

Expand All @@ -13,7 +13,7 @@ knitr::opts_chunk$set(echo = TRUE)

## Introduction

NeuroPartitioner is an advanced R package designed to convert NIfTI format T1/FL neuroimages into structured, high-dimensional 2D data frames, focusing on region of interest (ROI) based processing. This package incorporates a key algorithm called ‘partition’, which offers a fast, flexible framework for agglomerative partitioning based on the Direct-Measure-Reduce approach. This method ensures that each reduced variable maintains a user-specified minimum level of information while being interpretable, as each maps uniquely to one variable in the reduced dataset. The ‘partition’ algorithm, detailed in Millstein et al. (2020), allows for customization in variable selection, measurement of information loss, and data reduction methods. NeuroPartitioner is indispensable for researchers requiring efficient, accurate preparation of neuroimaging data for detailed statistical analysis and machine learning applications, enhancing the interpretability and utility of neuroimaging studies.
MRIreduce is an advanced R package designed to convert NIfTI format T1/FL neuroimages into structured, high-dimensional 2D data frames, focusing on region of interest (ROI) based processing. This package incorporates a key algorithm called ‘partition’, which offers a fast, flexible framework for agglomerative partitioning based on the Direct-Measure-Reduce approach. This method ensures that each reduced variable maintains a user-specified minimum level of information while being interpretable, as each maps uniquely to one variable in the reduced dataset. The ‘partition’ algorithm, detailed in Millstein et al. (2020), allows for customization in variable selection, measurement of information loss, and data reduction methods. MRIreduce is indispensable for researchers requiring efficient, accurate preparation of neuroimaging data for detailed statistical analysis and machine learning applications, enhancing the interpretability and utility of neuroimaging studies.


## Installation Instructions
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
NeuroPartitioner
MRIreduce
================

<!-- badges: start -->
Expand All @@ -9,17 +9,17 @@ IMAGE](https://raw.githubusercontent.com/USCbiostats/badges/master/tommy-image-b

## Introduction

NeuroPartitioner is an advanced R package designed to convert NIfTI
format T1/FL neuroimages into structured, high-dimensional 2D data
frames, focusing on region of interest (ROI) based processing. This
package incorporates a key algorithm called ‘partition’, which offers a
fast, flexible framework for agglomerative partitioning based on the
MRIreduce is an advanced R package designed to convert NIfTI format
T1/FL neuroimages into structured, high-dimensional 2D data frames,
focusing on region of interest (ROI) based processing. This package
incorporates a key algorithm called ‘partition’, which offers a fast,
flexible framework for agglomerative partitioning based on the
Direct-Measure-Reduce approach. This method ensures that each reduced
variable maintains a user-specified minimum level of information while
being interpretable, as each maps uniquely to one variable in the
reduced dataset. The ‘partition’ algorithm, detailed in Millstein et
al. (2020), allows for customization in variable selection, measurement
of information loss, and data reduction methods. NeuroPartitioner is
of information loss, and data reduction methods. MRIreduce is
indispensable for researchers requiring efficient, accurate preparation
of neuroimaging data for detailed statistical analysis and machine
learning applications, enhancing the interpretability and utility of
Expand Down
8 changes: 4 additions & 4 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
url: https://jtian123.github.io/WHIMs/
url: https://jtian123.github.io/MRIreduce/
template:
bootstrap: 5
bootswatch: cosmo # Using the Flatly theme from Bootswatch

navbar:
title: NeuroPartitioner
title: MRIreduce
left:
- text: Home
href: index.html
- text: Get Started
href: articles/NeuroPartitioner.html # Link directly to your vignette
href: articles/MRIreduce.html # Link directly to your vignette
- text: Reference
href: reference/index.html
right:
- text: View on source code
href: https://github.com/jtian123/WHIMs
href: https://github.com/jtian123/MRIreduce
icon: fab fa-github
Binary file modified inst/.DS_Store
Binary file not shown.
Binary file modified inst/extdata/.DS_Store
Binary file not shown.
Binary file added inst/extdata/1514935-20050520_T1.nii.gz.Rdata
Binary file not shown.
Binary file added inst/extdata/intensities_whole.rds
Binary file not shown.
Binary file added inst/extdata/loc_df.rds
Binary file not shown.
Binary file added inst/extdata/volume_whole.rds
Binary file not shown.
2 changes: 2 additions & 0 deletions man/eve_Fl.Rd

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

2 changes: 2 additions & 0 deletions man/eve_T1.Rd

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

Loading

0 comments on commit 376def9

Please sign in to comment.