Skip to content

Commit

Permalink
version bump
Browse files Browse the repository at this point in the history
Merge branch 'devel'

# Conflicts:
#	docs/404.html
#	docs/CONTRIBUTING.html
#	docs/LICENSE-text.html
#	docs/articles/abfit-baseline-opts.html
#	docs/articles/abfit-baseline-opts_files/figure-html/abfit_aic-1.png
#	docs/articles/abfit-baseline-opts_files/figure-html/abfit_bspline-1.png
#	docs/articles/abfit-baseline-opts_files/figure-html/abfit_bspline_more-1.png
#	docs/articles/abfit-baseline-opts_files/figure-html/abfit_default-1.png
#	docs/articles/abfit-baseline-opts_files/figure-html/abfit_flex-1.png
#	docs/articles/abfit-baseline-opts_files/figure-html/abfit_stiff-1.png
#	docs/articles/index.html
#	docs/articles/spant-basis-simulation.html
#	docs/articles/spant-intro.html
#	docs/articles/spant-intro_files/figure-html/unnamed-chunk-10-1.png
#	docs/articles/spant-metabolite-simulation.html
#	docs/articles/spant-preprocessing.html
#	docs/authors.html
#	docs/index.html
#	docs/news/index.html
#	docs/pkgdown.yml
#	docs/reference/Arg.mrs_data.html
#	docs/reference/Conj.mrs_data.html
#	docs/reference/Im.mrs_data.html
#	docs/reference/Imzap.html
#	docs/reference/Mod.mrs_data.html
#	docs/reference/Ncoils.html
#	docs/reference/Ndyns.html
#	docs/reference/Npts.html
#	docs/reference/Nspec.html
#	docs/reference/Ntrans.html
#	docs/reference/Nx.html
#	docs/reference/Ny.html
#	docs/reference/Nz.html
#	docs/reference/Re.mrs_data.html
#	docs/reference/abfit_opts.html
#	docs/reference/abfit_opts_v1_9_0.html
#	docs/reference/acquire.html
#	docs/reference/add_noise.html
#	docs/reference/add_noise_spec_snr.html
#	docs/reference/align.html
#	docs/reference/apodise_xy.html
#	docs/reference/append_basis.html
#	docs/reference/append_coils.html
#	docs/reference/append_dyns.html
#	docs/reference/append_regs.html
#	docs/reference/apply_axes.html
#	docs/reference/apply_mrs.html
#	docs/reference/apply_pulse.html
#	docs/reference/array2mrs_data.html
#	docs/reference/auto_phase.html
#	docs/reference/back_extrap_ar.html
#	docs/reference/basis2dyn_mrs_data.html
#	docs/reference/basis2mrs_data.html
#	docs/reference/bbase.html
#	docs/reference/bc_als.html
#	docs/reference/bc_constant.html
#	docs/reference/bc_gauss.html
#	docs/reference/bc_poly.html
#	docs/reference/bc_spline.html
#	docs/reference/beta2lw.html
#	docs/reference/bin_spec.html
#	docs/reference/calc_coil_noise_cor.html
#	docs/reference/calc_coil_noise_sd.html
#	docs/reference/calc_design_efficiency.html
#	docs/reference/calc_ed_from_lambda.html
#	docs/reference/calc_peak_info_vec.html
#	docs/reference/calc_sd_poly.html
#	docs/reference/calc_spec_diff.html
#	docs/reference/calc_spec_snr.html
#	docs/reference/check_lcm.html
#	docs/reference/check_tqn.html
#	docs/reference/circ_mask.html
#	docs/reference/coherence_filter.html
#	docs/reference/collapse_to_dyns.html
#	docs/reference/comb_coils.html
#	docs/reference/comb_coils_mrsi_gls.html
#	docs/reference/comb_coils_svs_gls.html
#	docs/reference/comb_fit_list_fit_tables.html
#	docs/reference/comb_fit_list_result_tables.html
#	docs/reference/comb_fit_tables.html
#	docs/reference/comb_metab_ref.html
#	docs/reference/conv_mrs.html
#	docs/reference/crop_basis.html
#	docs/reference/crop_spec.html
#	docs/reference/crop_td_pts.html
#	docs/reference/crop_td_pts_end.html
#	docs/reference/crop_td_pts_pot.html
#	docs/reference/crop_xy.html
#	docs/reference/crossprod_3d.html
#	docs/reference/decimate_mrs_fd.html
#	docs/reference/decimate_mrs_td.html
#	docs/reference/deconv_mrs.html
#	docs/reference/def_N.html
#	docs/reference/def_acq_paras.html
#	docs/reference/def_fs.html
#	docs/reference/def_ft.html
#	docs/reference/def_nuc.html
#	docs/reference/def_ref.html
#	docs/reference/dicom_reader.html
#	docs/reference/diff_mrs.html
#	docs/reference/downsample_mrs_fd.html
#	docs/reference/downsample_mrs_td.html
#	docs/reference/dyn_acq_times.html
#	docs/reference/ecc.html
#	docs/reference/elliptical_mask.html
#	docs/reference/est_noise_sd.html
#	docs/reference/fd2td.html
#	docs/reference/fd_conv_filt.html
#	docs/reference/fd_gauss_smo.html
#	docs/reference/find_bids_mrs.html
#	docs/reference/find_mrs_files.html
#	docs/reference/fit_amps.html
#	docs/reference/fit_diags.html
#	docs/reference/fit_mrs.html
#	docs/reference/fit_res2csv.html
#	docs/reference/fit_t1_ti_array.html
#	docs/reference/fit_t1_tr_array.html
#	docs/reference/fit_t2_te_array.html
#	docs/reference/fp_phase.html
#	docs/reference/fp_phase_correct.html
#	docs/reference/fp_scale.html
#	docs/reference/fs.html
#	docs/reference/ft_dyns.html
#	docs/reference/ft_shift.html
#	docs/reference/ft_shift_mat.html
#	docs/reference/gausswin_2d.html
#	docs/reference/gen_F.html
#	docs/reference/gen_F_xy.html
#	docs/reference/gen_I.html
#	docs/reference/gen_baseline_reg.html
#	docs/reference/gen_bold_reg.html
#	docs/reference/gen_conv_reg.html
#	docs/reference/gen_group_reg.html
#	docs/reference/gen_impulse_reg.html
#	docs/reference/gen_poly_reg.html
#	docs/reference/gen_trap_reg.html
#	docs/reference/get_1h_brain_basis_names.html
#	docs/reference/get_1h_brain_basis_paras.html
#	docs/reference/get_1h_brain_basis_paras_v1.html
#	docs/reference/get_1h_brain_basis_paras_v2.html
#	docs/reference/get_1h_brain_basis_paras_v3.html
#	docs/reference/get_1h_braino_basis_names.html
#	docs/reference/get_1h_spectre_basis_names.html
#	docs/reference/get_2d_psf.html
#	docs/reference/get_acq_paras.html
#	docs/reference/get_basis_subset.html
#	docs/reference/get_dyns.html
#	docs/reference/get_even_dyns.html
#	docs/reference/get_fh_dyns.html
#	docs/reference/get_fit_map.html
#	docs/reference/get_fp.html
#	docs/reference/get_guassian_pulse.html
#	docs/reference/get_head_dyns.html
#	docs/reference/get_lcm_cmd.html
#	docs/reference/get_metab.html
#	docs/reference/get_mol_names.html
#	docs/reference/get_mol_paras.html
#	docs/reference/get_mrs_affine.html
#	docs/reference/get_mrsi2d_seg.html
#	docs/reference/get_mrsi_voi.html
#	docs/reference/get_mrsi_voxel.html
#	docs/reference/get_mrsi_voxel_xy_psf.html
#	docs/reference/get_odd_dyns.html
#	docs/reference/get_ref.html
#	docs/reference/get_seg_ind.html
#	docs/reference/get_sh_dyns.html
#	docs/reference/get_slice.html
#	docs/reference/get_spin_num.html
#	docs/reference/get_subset.html
#	docs/reference/get_svs_voi.html
#	docs/reference/get_tail_dyns.html
#	docs/reference/get_td_amp.html
#	docs/reference/get_tqn_cmd.html
#	docs/reference/get_uncoupled_mol.html
#	docs/reference/get_voi_cog.html
#	docs/reference/get_voi_seg.html
#	docs/reference/get_voi_seg_psf.html
#	docs/reference/get_voxel.html
#	docs/reference/glm_spec.html
#	docs/reference/glm_spec_fmrs_fl.html
#	docs/reference/glm_spec_fmrs_group.html
#	docs/reference/glm_spec_group_linhyp.html
#	docs/reference/grid_shift_xy.html
#	docs/reference/gridplot.html
#	docs/reference/gridplot.mrs_data.html
#	docs/reference/hsvd.html
#	docs/reference/hsvd_filt.html
#	docs/reference/hsvd_vec.html
#	docs/reference/hz.html
#	docs/reference/ift_shift.html
#	docs/reference/ift_shift_mat.html
#	docs/reference/image.mrs_data.html
#	docs/reference/img2kspace_xy.html
#	docs/reference/index.html
#	docs/reference/int_spec.html
#	docs/reference/interleave_dyns.html
#	docs/reference/inv_even_dyns.html
#	docs/reference/inv_odd_dyns.html
#	docs/reference/is.def.html
#	docs/reference/is_fd.html
#	docs/reference/kspace2img_xy.html
#	docs/reference/l2_reg.html
#	docs/reference/lb.html
#	docs/reference/lofdc.html
#	docs/reference/lw2alpha.html
#	docs/reference/lw2beta.html
#	docs/reference/make_basis_from_raw.html
#	docs/reference/mask_dyns.html
#	docs/reference/mask_fit_res.html
#	docs/reference/mask_xy.html
#	docs/reference/mask_xy_corners.html
#	docs/reference/mask_xy_ellipse.html
#	docs/reference/mask_xy_mat.html
#	docs/reference/mat2mrs_data.html
#	docs/reference/matexp.html
#	docs/reference/max_mrs.html
#	docs/reference/max_mrs_interp.html
#	docs/reference/mean.list.html
#	docs/reference/mean.mrs_data.html
#	docs/reference/mean_dyn_blocks.html
#	docs/reference/mean_dyn_pairs.html
#	docs/reference/mean_dyns.html
#	docs/reference/mean_mrs_list.html
#	docs/reference/mean_vec_blocks.html
#	docs/reference/median_dyns.html
#	docs/reference/mod_td.html
#	docs/reference/mrs_data2basis.html
#	docs/reference/mrs_data2bids.html
#	docs/reference/mrs_data2mat.html
#	docs/reference/mrs_data2spec_mat.html
#	docs/reference/mrs_data2vec.html
#	docs/reference/mvfftshift.html
#	docs/reference/mvifftshift.html
#	docs/reference/n2coord.html
#	docs/reference/nifti_flip_lr.html
#	docs/reference/one_page_pdf.html
#	docs/reference/ortho3.html
#	docs/reference/ortho3_inter.html
#	docs/reference/peak_info.html
#	docs/reference/pg_extrap_xy.html
#	docs/reference/phase.html
#	docs/reference/phase_ref_1h_brain.html
#	docs/reference/plot.fit_result.html
#	docs/reference/plot.mrs_data.html
#	docs/reference/plot_bc.html
#	docs/reference/plot_reg.html
#	docs/reference/plot_slice_fit.html
#	docs/reference/plot_slice_fit_inter.html
#	docs/reference/plot_slice_map.html
#	docs/reference/plot_slice_map_inter.html
#	docs/reference/plot_spec_sd.html
#	docs/reference/plot_voi_overlay.html
#	docs/reference/plot_voi_overlay_seg.html
#	docs/reference/ppm.html
#	docs/reference/precomp.html
#	docs/reference/preproc_svs.html
#	docs/reference/preproc_svs_dataset.html
#	docs/reference/print.fit_result.html
#	docs/reference/print.mrs_data.html
#	docs/reference/qn_states.html
#	docs/reference/rats.html
#	docs/reference/re_weighting.html
#	docs/reference/read_basis.html
#	docs/reference/read_ima_coil_dir.html
#	docs/reference/read_ima_dyn_dir.html
#	docs/reference/read_lcm_coord.html
#	docs/reference/read_mrs.html
#	docs/reference/read_mrs_tqn.html
#	docs/reference/read_pulse_ascii.html
#	docs/reference/read_pulse_bruker.html
#	docs/reference/read_pulse_pta.html
#	docs/reference/read_siemens_txt_hdr.html
#	docs/reference/read_tqn_fit.html
#	docs/reference/read_tqn_result.html
#	docs/reference/recon_imag.html
#	docs/reference/recon_imag_vec.html
#	docs/reference/recon_twix_2d_mrsi.html
#	docs/reference/rectangular_mask.html
#	docs/reference/reexports.html
#	docs/reference/rep_array_dim.html
#	docs/reference/rep_dyn.html
#	docs/reference/rep_mrs.html
#	docs/reference/resample_basis.html
#	docs/reference/resample_img.html
#	docs/reference/resample_voi.html
#	docs/reference/reslice_to_mrs.html
#	docs/reference/reson_table2mrs_data.html
#	docs/reference/rm_dyns.html
#	docs/reference/scale_amp_molal.html
#	docs/reference/scale_amp_molal_pvc.html
#	docs/reference/scale_amp_molar.html
#	docs/reference/scale_amp_molar2molal_pvc.html
#	docs/reference/scale_amp_ratio.html
#	docs/reference/scale_amp_ratio_value.html
#	docs/reference/scale_amp_water_ratio.html
#	docs/reference/scale_basis_amp.html
#	docs/reference/scale_basis_from_singlet.html
#	docs/reference/scale_mrs_amp.html
#	docs/reference/scale_spec.html
#	docs/reference/sd.html
#	docs/reference/sd.mrs_data.html
#	docs/reference/seconds.html
#	docs/reference/seq_cpmg_ideal.html
#	docs/reference/seq_mega_press_ideal.html
#	docs/reference/seq_press_2d_shaped.html
#	docs/reference/seq_press_ideal.html
#	docs/reference/seq_pulse_acquire.html
#	docs/reference/seq_slaser_ideal.html
#	docs/reference/seq_spin_echo_ideal.html
#	docs/reference/seq_steam_ideal.html
#	docs/reference/seq_steam_ideal_cof.html
#	docs/reference/seq_steam_ideal_young.html
#	docs/reference/set_Ntrans.html
#	docs/reference/set_def_acq_paras.html
#	docs/reference/set_lcm_cmd.html
#	docs/reference/set_lw.html
#	docs/reference/set_mask_xy_mat.html
#	docs/reference/set_precomp_mode.html
#	docs/reference/set_precomp_verbose.html
#	docs/reference/set_ref.html
#	docs/reference/set_td_pts.html
#	docs/reference/set_tqn_cmd.html
#	docs/reference/set_tr.html
#	docs/reference/shift.html
#	docs/reference/shift_basis.html
#	docs/reference/sim_basis.html
#	docs/reference/sim_basis_1h_brain.html
#	docs/reference/sim_basis_1h_brain_press.html
#	docs/reference/sim_basis_mm_lip_lcm.html
#	docs/reference/sim_basis_tqn.html
#	docs/reference/sim_brain_1h.html
#	docs/reference/sim_mol.html
#	docs/reference/sim_noise.html
#	docs/reference/sim_resonances.html
#	docs/reference/sim_th_excit_profile.html
#	docs/reference/sim_zero.html
#	docs/reference/smooth_dyns.html
#	docs/reference/sort_basis.html
#	docs/reference/spant-package.html
#	docs/reference/spant_abfit_benchmark.html
#	docs/reference/spant_mpress_drift.html
#	docs/reference/spant_sim_fmrs_dataset.html
#	docs/reference/spant_simulation_benchmark.html
#	docs/reference/spec_decomp.html
#	docs/reference/spec_op.html
#	docs/reference/spin_sys.html
#	docs/reference/spm_pve2categorical.html
#	docs/reference/ssp.html
#	docs/reference/stackplot.fit_result.html
#	docs/reference/stackplot.html
#	docs/reference/stackplot.mrs_data.html
#	docs/reference/sub_first_dyn.html
#	docs/reference/sub_mean_dyns.html
#	docs/reference/sub_median_dyns.html
#	docs/reference/sum_coils.html
#	docs/reference/sum_dyns.html
#	docs/reference/sum_mrs.html
#	docs/reference/sum_mrs_list.html
#	docs/reference/svs_1h_brain_analysis.html
#	docs/reference/svs_1h_brain_analysis_dev.html
#	docs/reference/svs_1h_brain_batch_analysis.html
#	docs/reference/t_test_spec.html
#	docs/reference/td2fd.html
#	docs/reference/td_conv_filt.html
#	docs/reference/tdsr.html
#	docs/reference/te.html
#	docs/reference/tr.html
#	docs/reference/varpro_3_para_opts.html
#	docs/reference/varpro_basic_opts.html
#	docs/reference/varpro_opts.html
#	docs/reference/vec2mrs_data.html
#	docs/reference/write_basis.html
#	docs/reference/write_basis_tqn.html
#	docs/reference/write_mrs.html
#	docs/reference/write_mrs_nifti.html
#	docs/reference/write_pulse_ascii.html
#	docs/reference/zero_fade_spec.html
#	docs/reference/zero_higher_orders.html
#	docs/reference/zero_td_pts_end.html
#	docs/reference/zf.html
#	docs/reference/zf_xy.html
#	docs/sitemap.xml
  • Loading branch information
martin3141 committed Aug 27, 2024
2 parents 2158ea3 + f49480b commit 33f5ccb
Show file tree
Hide file tree
Showing 400 changed files with 9,532 additions and 4,426 deletions.
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: spant
Type: Package
Title: MR Spectroscopy Analysis Tools
Version: 2.22.0
Date: 2024-07-11
Version: 2.23.0
Date: 2024-08-27
Authors@R: c(
person("Martin", "Wilson", email = "martin@pipegrep.co.uk",
role = c("cre", "aut"), comment = c(ORCID = "0000-0002-2089-3956")),
Expand All @@ -14,7 +14,7 @@ Description: Tools for reading, visualising and processing Magnetic Resonance
<DOI:10.1002/mrm.27605>.
BugReports: https://github.com/martin3141/spant/issues/
License: GPL-3
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE, roclets = c("rd", "namespace", "collate"))
NeedsCompilation: yes
LazyData: yes
Expand Down Expand Up @@ -50,6 +50,7 @@ Suggests:
testthat,
ragg,
doParallel,
parallel,
car
VignetteBuilder:
knitr
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export(bc_poly)
export(bc_spline)
export(beta2lw)
export(bin_spec)
export(calc_basis_corr_mat)
export(calc_basis_crlbs)
export(calc_coil_noise_cor)
export(calc_coil_noise_sd)
export(calc_design_efficiency)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# spant 2.23.0
* abfit options for lipid and MM maximum broadening and maximum frequency shift
now default to be the same as the metabolite values unless explicitly changed.
* Added function to calculate the correlation matrix from a basis_set.
* Fix for RDA files where a comma is sometimes used as a decimal point.

# spant 2.22.0
* Added find_bids_mrs and mrs_data2bids helper functions.
* Improved handling of CMRR semi-LASER SVS water reference scans.
Expand Down
275 changes: 207 additions & 68 deletions R/abfit.R

Large diffs are not rendered by default.

97 changes: 95 additions & 2 deletions R/basis_set.R
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,13 @@ write_basis <- function(basis, basis_file, fwhmba = 0.1) {
#' @param amps a vector of scaling factors to apply to each basis element.
#' @param shifts a vector of frequency shifts (in ppm) to apply to each basis
#' element.
#' @param lbs a vector of Lorentzian line broadening terms (in Hz) to apply to
#' each basis element.
#' @return an mrs_data object with basis signals spread across the dynamic
#' dimension or summed.
#' @export
basis2mrs_data <- function(basis, sum_elements = FALSE, amps = NULL,
shifts = NULL) {
shifts = NULL, lbs = NULL) {

# TODO nuc is just the default for now
res <- mat2mrs_data(t(basis$data), fs = basis$fs, ft = basis$ft,
Expand All @@ -277,9 +279,17 @@ basis2mrs_data <- function(basis, sum_elements = FALSE, amps = NULL,
res <- shift(res, shifts)
}

# Lorentzian line broaden basis elements
if (!is.null(lbs)) {
if (n_sigs != length(lbs)) {
stop(paste("Error, length of amps does not match the number of basis elements :", dim(basis$data)[2]))
}
res <- lb(res, lbs, 0)
}

if (sum_elements) res <- sum_dyns(res)

res
return(res)
}

#' Convert a basis object to a dynamic mrs_data object.
Expand Down Expand Up @@ -478,3 +488,86 @@ make_basis_from_raw <- function(dir_path, ft, fs, ref) {
basis <- mrs_data2basis(mrs_data_dynamic, names = names)
return(basis)
}

#' Estimate the CRLB for each element in a basis set.
#' @param basis basis_set object.
#' @param xlim spectral range to use in ppm.
#' @param zf zero-fill the basis set.
#' @param sd standard deviation of the noise.
#' @param bl_comp_pppm number spline baseline components to append per-ppm.
#' @return a vector of predicted errors.
#' @export
calc_basis_crlbs <- function(basis, xlim = c(4, 0.2), zf = TRUE, sd = 1,
bl_comp_pppm = NULL) {

basis_mrs <- basis2mrs_data(basis)

if (zf) basis_mrs <- zf(basis_mrs)

if (is.null(xlim)) {
ppm_sc <- ppm(basis_mrs)
xlim <- c(ppm_sc[1], ppm_sc[length(ppm_sc)])
basis_mrs <- td2fd(basis_mrs)
} else {
basis_mrs <- crop_spec(basis_mrs, xlim = xlim)
}

if (is.null(bl_comp_pppm)) {
D <- t(Re(mrs_data2mat(basis_mrs)))
} else {
D <- t(Re(mrs_data2mat(basis_mrs)))
comps <- diff(sort(xlim)) * bl_comp_pppm
sp_basis <- bbase(dim(D)[1], round(comps))
D <- cbind(D, sp_basis)
}

F <- (t(D) %*% D) / (sd ^ 2)

F_inv <- pracma::pinv(F)

errors <- diag(F_inv) ^ 0.5

if (is.null(bl_comp_pppm)) {
names(errors) <- basis$names
} else {
spline_names <- paste("SPLINE", as.character(1:dim(sp_basis)[2]),
sep = "_")

names(errors) <- c(basis$names, spline_names)
}

return(errors)
}

#' Estimate the correlation matrix for a basis set.
#' @param basis basis_set object.
#' @param xlim spectral range to use in ppm.
#' @param zf zero-fill the basis set.
#' @return correlation matrix.
#' @export
calc_basis_corr_mat <- function(basis, xlim = c(4, 0.2), zf = TRUE) {

basis_mrs <- basis2mrs_data(basis)

if (zf) basis_mrs <- zf(basis_mrs)

basis_mrs <- crop_spec(basis_mrs, xlim = xlim)

D <- t(Re(mrs_data2mat(basis_mrs)))

F <- (t(D) %*% D)
F_inv <- pracma::pinv(F)

corr_mat <- matrix(nrow = nrow(F_inv), ncol = ncol(F_inv))

for (x in 1:nrow(corr_mat)) {
for (y in 1:ncol(corr_mat)) {
corr_mat[x, y] <- F_inv[x, y] / ((F_inv[x, x] * F_inv[y, y]) ^ 0.5)
}
}

rownames(corr_mat) <- basis$names
colnames(corr_mat) <- basis$names

return(corr_mat)
}
51 changes: 39 additions & 12 deletions R/fitting.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#' @param w_ref water reference data for concentration scaling (optional).
#' @param opts options to pass to the analysis method.
#' @param parallel perform analyses in parallel (TRUE or FALSE).
#' @param cl a parallel socket cluster required to run analyses in parallel.
#' Eg, cl <- parallel::makeCluster(4).
#' @param time measure the time taken for the analysis to complete
#' (TRUE or FALSE).
#' @param progress option is passed to plyr::alply function to display a
Expand All @@ -49,13 +51,12 @@
#' }
#' @export
fit_mrs <- function(metab, basis = NULL, method = 'ABFIT', w_ref = NULL,
opts = NULL, parallel = FALSE, time = TRUE,
opts = NULL, parallel = FALSE, cl = NULL, time = TRUE,
progress = "text", extra = NULL) {

if (inherits(metab, "list")) {

if (!is.null(w_ref)) {
# if (class(w_ref) != "list") stop("w_ref is not a list but metab is")
if (!inherits(w_ref, "list")) stop("w_ref is not a list but metab is")

if (length(metab) != length(w_ref)) {
Expand Down Expand Up @@ -137,23 +138,31 @@ fit_mrs <- function(metab, basis = NULL, method = 'ABFIT', w_ref = NULL,

acq_paras <- get_acq_paras(metab)

plyr <- TRUE
if (plyr) {
if (!parallel) {
result_list <- plyr::alply(metab$data, c(2, 3, 4, 5, 6), abfit,
acq_paras, basis, opts,
.parallel = parallel,
.paropts = list(.inorder = TRUE,
.packages = "spant"),
.progress = progress, .inform = FALSE)
} else {
result_list <- apply(metab$data, c(2, 3, 4, 5, 6), abfit, acq_paras,
basis, opts)
if (is.null(cl)) stop("pass cl argument to fit_mrs for parallel analyses")
result_list <- parallel::parApply(cl, metab$data, c(2, 3, 4, 5, 6), abfit,
acq_paras, basis, opts)
labs <- which(array(TRUE, dim(result_list)), arr.ind = TRUE)
result_list <- result_list[,,,,]
attr(result_list, "split_labels") <- labs
names(result_list) <- seq_len(nrow(labs))
}

# method using base apply
# result_list <- apply(metab$data, c(2, 3, 4, 5, 6), abfit, acq_paras,
# basis, opts)
# labs <- which(array(TRUE, dim(result_list)), arr.ind = TRUE)
# result_list <- result_list[,,,,]
# attr(result_list, "split_labels") <- labs
# names(result_list) <- seq_len(nrow(labs))

} else if (METHOD == "VARPRO") {
# read basis into memory if a file
if (is.character(basis)) {
Expand Down Expand Up @@ -266,12 +275,30 @@ fit_mrs <- function(metab, basis = NULL, method = 'ABFIT', w_ref = NULL,
temp_mrs$data = temp_mrs$data[1, 1, 1, 1, 1, 1,]
dim(temp_mrs$data) <- c(1, 1, 1, 1, 1, 1, length(temp_mrs$data))

result_list <- plyr::alply(metab$data, c(2,3,4,5,6), lcmodel_fit,
temp_mrs, basis_file, opts,
.parallel = parallel,
.paropts = list(.inorder = TRUE,
.packages = "spant"),
.progress = progress, .inform = FALSE)
if (!parallel) {
result_list <- plyr::alply(metab$data, c(2, 3, 4, 5, 6), lcmodel_fit,
temp_mrs, basis_file, opts,
.parallel = parallel,
.paropts = list(.inorder = TRUE,
.packages = "spant"),
.progress = progress, .inform = FALSE)
} else {
if (is.null(cl)) stop("pass cl argument to fit_mrs for parallel analyses")
result_list <- parallel::parApply(cl, metab$data, c(2, 3, 4, 5, 6),
lcmodel_fit, temp_mrs, basis_file, opts)
labs <- which(array(TRUE, dim(result_list)), arr.ind = TRUE)
result_list <- result_list[,,,,]
attr(result_list, "split_labels") <- labs
names(result_list) <- seq_len(nrow(labs))
}

# result_list <- plyr::alply(metab$data, c(2,3,4,5,6), lcmodel_fit,
# temp_mrs, basis_file, opts,
# .parallel = parallel,
# .paropts = list(.inorder = TRUE,
# .packages = "spant"),
# .progress = progress, .inform = FALSE)

} else if (exists(method)) {
message(paste("Using external fit method :", method))

Expand Down
3 changes: 3 additions & 0 deletions R/mrs_read_rda.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ read_rda <- function(fname, extra) {
col_ori <- rep(NA, 3)
pos_vec <- rep(NA, 3)

# a fix for when commas are used as decimal points
txt$V2 <- gsub(",", ".", txt$V2)

N <- as.integer(txt$V2[which(txt$V1 == "VectorSize")])
fs <- 1e6 / as.numeric(txt$V2[which(txt$V1 == "DwellTime")])
ft <- 1e6 * as.numeric(txt$V2[which(txt$V1 == "MRFrequency")])
Expand Down
20 changes: 14 additions & 6 deletions docs/404.html

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

Loading

0 comments on commit 33f5ccb

Please sign in to comment.