Skip to content

Commit 2fcc5da

Browse files
committed
numerous and sundry
1 parent 61120a1 commit 2fcc5da

35 files changed

+7069
-1053
lines changed

NAMESPACE

+11-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ S3method(get_searchlight,mvpa_surface_dataset)
2323
S3method(has_crossval,default)
2424
S3method(has_crossval,model_spec)
2525
S3method(has_crossval,mvpa_model)
26+
S3method(has_test_set,model_spec)
2627
S3method(has_test_set,mvpa_dataset)
2728
S3method(has_test_set,mvpa_design)
2829
S3method(has_test_set,mvpa_model)
@@ -33,6 +34,7 @@ S3method(merge_results,feature_rsa_model)
3334
S3method(merge_results,multiway_classification_result)
3435
S3method(merge_results,regional_mvpa_result)
3536
S3method(merge_results,regression_result)
37+
S3method(merge_results,vector_rsa_model)
3638
S3method(nobs,mvpa_design)
3739
S3method(nresponses,mvpa_design)
3840
S3method(pairwise_dist,cordist)
@@ -74,15 +76,15 @@ S3method(prob_observed,binary_classification_result)
7476
S3method(prob_observed,multiway_classification_result)
7577
S3method(process_roi,custom_internal_model_spec)
7678
S3method(run_regional,default)
77-
S3method(run_regional,feature_rsa_model)
7879
S3method(run_regional,mvpa_model)
7980
S3method(run_regional,rsa_model)
81+
S3method(run_regional,vector_rsa_model)
8082
S3method(run_searchlight,default)
81-
S3method(run_searchlight,feature_rsa_model)
8283
S3method(run_searchlight,vector_rsa)
8384
S3method(select_features,FTest)
8485
S3method(select_features,catscore)
8586
S3method(select_features,mvpa_model)
87+
S3method(strip_dataset,default)
8688
S3method(sub_result,binary_classification_result)
8789
S3method(sub_result,multiway_classification_result)
8890
S3method(summary,feature_rsa_model)
@@ -97,7 +99,12 @@ S3method(y_train,feature_rsa_design)
9799
S3method(y_train,feature_rsa_model)
98100
S3method(y_train,mvpa_design)
99101
S3method(y_train,mvpa_model)
102+
export("for")
103+
export(MVPA)
100104
export(MVPAModels)
105+
export(Objects)
106+
export(Regional)
107+
export(`vector_rsa_model`)
101108
export(balance_partitions)
102109
export(binary_classification_result)
103110
export(blocked_cross_validation)
@@ -116,6 +123,7 @@ export(custom_performance)
116123
export(data_sample)
117124
export(eucdist)
118125
export(evaluate_model.feature_rsa_model)
126+
export(evaluate_model.vector_rsa_model)
119127
export(feature_rsa_design)
120128
export(feature_rsa_model)
121129
export(feature_selector)
@@ -162,6 +170,7 @@ export(run_searchlight_base)
162170
export(second_order_similarity)
163171
export(select_features)
164172
export(sequential_blocked_cross_validation)
173+
export(strip_dataset)
165174
export(sub_result)
166175
export(test_design)
167176
export(train_model)

R/allgeneric.R

+27-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ get_unique_regions <- function(region_mask, ...) {
1111
UseMethod("get_unique_regions")
1212
}
1313

14+
#' Strip Dataset from Model Specification
15+
#'
16+
#' Removes the potentially large dataset component from a model specification object
17+
#' to avoid copying it during parallel processing.
18+
#'
19+
#' @param obj The model specification object.
20+
#' @param ... Additional arguments.
21+
#' @return The model specification object with the `dataset` element removed or set to NULL.
22+
#' @export
23+
strip_dataset <- function(obj, ...) {
24+
UseMethod("strip_dataset")
25+
}
26+
1427
#' Select Features
1528
#'
1629
#' Given a \code{feature_selection} specification object and a dataset, returns the set of selected features as a binary vector.
@@ -107,7 +120,7 @@ process_roi <- function(mod_spec, roi, rnum, ...) {
107120
process_roi.default <- function(mod_spec, roi, rnum, ...) {
108121
if (!is.null(mod_spec$process_roi)) {
109122
mod_spec$process_roi(mod_spec, roi, rnum, ...)
110-
} else if (has_test_set(mod_spec$dataset)) {
123+
} else if (has_test_set(mod_spec)) {
111124
external_crossval(mod_spec, roi, rnum, ...)
112125
} else if (has_crossval(mod_spec)) {
113126
#print("internal crossval")
@@ -462,13 +475,18 @@ run_searchlight <- function(model_spec, radius, method = c("standard", "randomiz
462475
#'
463476
#' @param model_spec A \code{mvpa_model} instance containing the model specifications
464477
#' @param region_mask A \code{NeuroVol} or \code{NeuroSurface} object where each region is identified by a unique integer
465-
#' @param ... Extra arguments passed to specific regional analysis methods
466-
#'
467-
#' @return A named list containing:
468-
#' \item{performance}{Performance metrics for each region}
469-
#' \item{prediction_table}{Predictions for each region}
470-
#' \item{fits}{Model fits if return_fits=TRUE}
471-
#'
478+
#' @param coalesce_design_vars If \code{TRUE}, merges design variables into the prediction table (if present and generated). Default is \code{FALSE}.
479+
#' @param processor An optional custom processor function for each region (ROI). If NULL (default), behavior depends on the \code{model_spec} class.
480+
#' @param verbose If \code{TRUE}, print progress messages during iteration (default is \code{FALSE}).
481+
#' @param ... Extra arguments passed to specific regional analysis methods (e.g., `return_fits`, `compute_performance`).
482+
#'
483+
#' @return A \code{regional_mvpa_result} object (list) containing:
484+
#' \item{performance_table}{A tibble of performance metrics for each region (if computed).}
485+
#' \item{prediction_table}{A tibble with detailed predictions for each observation/region (if generated).}
486+
#' \item{vol_results}{A list of volumetric maps representing performance metrics across space (if computed).}
487+
#' \item{fits}{A list of fitted model objects for each region (if requested via `return_fits=TRUE`).}
488+
#' \item{model_spec}{The original model specification object provided.} # Note: Original documentation said 'performance', clarified here.
489+
#'
472490
#' @examples
473491
#' \donttest{
474492
#' # Generate sample dataset (3D volume with categorical response)
@@ -512,6 +530,7 @@ run_searchlight <- function(model_spec, radius, method = c("standard", "randomiz
512530
#' first_roi_fit <- results$fits[[1]] # First ROI's fitted model
513531
#' }
514532
#'
533+
#' @rdname run_regional-methods
515534
#' @export
516535
run_regional <- function(model_spec, region_mask, ...) {
517536
UseMethod("run_regional")

R/dataset.R

+25-9
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,22 @@ gen_sample_dataset <- function(D, nobs, response_type=c("categorical", "continuo
129129

130130
block_var <- as.integer(as.character(cut(1:nobs, blocks, labels=1:blocks)))
131131

132-
des <- if (external_test) {
132+
if (external_test) {
133133
message("external test")
134-
mvpa_design(data.frame(Y=Y, block_var=block_var), test_design=data.frame(Ytest = Ytest),
134+
mvdes <- mvpa_design(data.frame(Y=Y, block_var=block_var), test_design=data.frame(Ytest = Ytest),
135135
block_var= "block_var", y_train= ~ Y, y_test = ~ Ytest, split_by=split_by)
136136
} else {
137-
mvpa_design(data.frame(Y=Y, block_var=block_var), block_var="block_var", y_train= ~ Y, split_by=split_by)
137+
mvdes <- mvpa_design(data.frame(Y=Y, block_var=block_var), block_var="block_var", y_train= ~ Y, split_by=split_by)
138138
}
139139

140-
list(dataset=dset, design=des)
140+
# Make sure the dataset also has the _has_test_set flag set consistently
141+
if (is.list(dset) && "dataset" %in% names(dset)) {
142+
dset$dataset$has_test_set <- external_test
143+
} else {
144+
dset$has_test_set <- external_test
145+
}
146+
147+
list(dataset=dset, design=mvdes)
141148
}
142149

143150

@@ -155,6 +162,7 @@ gen_sample_dataset <- function(D, nobs, response_type=c("categorical", "continuo
155162
#' \item{train_data}{The training data as a \code{NeuroVec} instance}
156163
#' \item{test_data}{The test data as a \code{NeuroVec} instance (if provided, otherwise NULL)}
157164
#' \item{mask}{The binary mask defining valid voxels as a \code{NeuroVol} instance}
165+
#' \item{has_test_set}{Logical flag indicating whether this dataset has a test set}
158166
#' }
159167
#'
160168
#' @examples
@@ -196,11 +204,15 @@ mvpa_dataset <- function(train_data, test_data=NULL, mask) {
196204
stop("Invalid dataset: Only ", active_voxels, " active voxel(s) in mask. Feature RSA analysis requires multiple active voxels.")
197205
}
198206

207+
# Store a flag indicating whether this dataset has a test set
208+
has_test <- !is.null(test_data)
209+
199210
ret <- structure(
200211
list(
201212
train_data=train_data,
202213
test_data=test_data,
203-
mask=mask
214+
mask=mask,
215+
has_test_set=has_test # Add flag for test set presence
204216
),
205217
class=c("mvpa_image_dataset", "mvpa_dataset", "list")
206218
)
@@ -224,6 +236,7 @@ mvpa_dataset <- function(train_data, test_data=NULL, mask) {
224236
#' \item{test_data}{The test data as a \code{NeuroSurfaceVector} instance (if provided)}
225237
#' \item{mask}{A numeric vector indicating valid vertices (1) and excluded vertices (0)}
226238
#' \item{name}{Character string identifier for the dataset}
239+
#' \item{has_test_set}{Logical flag indicating whether this dataset has a test set}
227240
#' }
228241
#'
229242
#' @details
@@ -263,17 +276,19 @@ mvpa_surface_dataset <- function(train_data, test_data=NULL, mask=NULL, name="")
263276
mask[indices(train_data)] <- 1
264277
}
265278

279+
# Store a flag indicating whether this dataset has a test set
280+
has_test <- !is.null(test_data)
281+
266282
structure(
267283
list(
268284
train_data=train_data,
269285
test_data=test_data,
270286
mask=mask,
271-
name=name
287+
name=name,
288+
has_test_set=has_test # Add flag for test set presence
272289
),
273290
class=c("mvpa_surface_dataset", "mvpa_dataset", "list")
274291
)
275-
276-
277292
}
278293

279294
#' @export
@@ -440,7 +455,8 @@ wrap_output.mvpa_surface_dataset <- function(obj, vals, indices) {
440455

441456
#' @export
442457
has_test_set.mvpa_dataset <- function(obj) {
443-
!is.null(obj$test_data)
458+
# Use the stored flag rather than checking for existence of test_data
459+
isTRUE(obj$has_test_set)
444460
}
445461

446462

0 commit comments

Comments
 (0)