-
Notifications
You must be signed in to change notification settings - Fork 52
Freed heavy fit objects and large dataProcess intermediates early #207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tonywu1999
wants to merge
2
commits into
devel
Choose a base branch
from
MSstats/work/20260514_free-heavy-objects
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+103
−19
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # Tests that .fitSurvival() and .fitLinearModel() return model objects with | ||
| # heavy fields stripped, and that downstream predict/summary/vcov still work. | ||
|
|
||
|
|
||
| # --- .fitSurvival: $y and $linear.predictors are stripped --------------------- | ||
| # | ||
| # Neither field is needed by predict.survreg(). | ||
|
|
||
| surv_input = data.table::data.table( | ||
| newABUNDANCE = c(10.1, 11.2, 9.5, 10.8, 12.0, 9.0, 11.5, 10.3, | ||
| 10.5, 11.0, 9.8, 10.2, 12.2, 9.3, 11.8, 10.6), | ||
| cen = c(1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1), | ||
| RUN = factor(rep(1:4, each = 4)), | ||
| FEATURE = factor(rep(c("feat1", "feat2", "feat3", "feat4"), 4)) | ||
| ) | ||
| # cen = 0 marks left-censored rows; set them to the upper-bound threshold. | ||
| surv_input[cen == 0, newABUNDANCE := 7.0] | ||
|
|
||
| surv_fit = MSstats:::.fitSurvival(surv_input, aft_iterations = 90) | ||
|
|
||
| expect_true(is.null(surv_fit$y), | ||
| info = "survreg $y should be stripped") | ||
| expect_true(is.null(surv_fit$linear.predictors), | ||
| info = "survreg $linear.predictors should be stripped") | ||
|
|
||
| expect_false(is.null(surv_fit$coefficients), | ||
| info = "survreg $coefficients must survive stripping") | ||
| expect_false(is.null(surv_fit$scale), | ||
| info = "survreg $scale must survive stripping") | ||
|
|
||
| predictions = predict(surv_fit, newdata = surv_input) | ||
| expect_equal(length(predictions), nrow(surv_input), | ||
| info = "predict() must work on the stripped survreg object") | ||
| expect_true(all(is.finite(predictions)), | ||
| info = "predict() should return finite values") | ||
|
|
||
| unstripped_fit = survival::survreg( | ||
| survival::Surv(newABUNDANCE, cen, type = "left") ~ FEATURE + RUN, | ||
| data = surv_input, dist = "gaussian") | ||
| expect_true(object.size(surv_fit) < object.size(unstripped_fit), | ||
| info = paste("Stripped survreg should be smaller.", | ||
| "Stripped:", object.size(surv_fit), | ||
| "Unstripped:", object.size(unstripped_fit))) | ||
|
|
||
|
|
||
| # --- .fitLinearModel: $model is stripped -------------------------------------- | ||
| # | ||
| # $model is not needed by summary() or vcov(). | ||
|
|
||
| lm_input = data.table::data.table( | ||
| newABUNDANCE = c(10.1, 11.2, 9.5, 10.8, 12.0, 9.0, 11.5, 10.3, | ||
| 10.5, 11.0, 9.8, 10.2, 12.2, 9.3, 11.8, 10.6), | ||
| RUN = factor(rep(1:4, each = 4)), | ||
| FEATURE = factor(rep(c("feat1", "feat2", "feat3", "feat4"), 4)), | ||
| weights = NA | ||
| ) | ||
|
|
||
| lm_fit = MSstats:::.fitLinearModel(lm_input, is_single_feature = FALSE, | ||
| is_labeled = FALSE, equal_variances = TRUE) | ||
|
|
||
| expect_true(is.null(lm_fit$model), | ||
| info = "lm $model should be stripped") | ||
|
|
||
| expect_false(is.null(lm_fit$coefficients), | ||
| info = "lm $coefficients must survive stripping") | ||
| expect_false(is.null(lm_fit$qr), | ||
| info = "lm $qr must survive stripping (needed by summary and vcov)") | ||
| expect_false(is.null(lm_fit$residuals), | ||
| info = "lm $residuals must survive stripping (needed by summary)") | ||
|
|
||
| lm_summary = summary(lm_fit) | ||
| expect_false(is.null(lm_summary$coefficients), | ||
| info = "summary() must work on the stripped lm object") | ||
| lm_vcov = vcov(lm_fit) | ||
| expect_true(is.matrix(lm_vcov), | ||
| info = "vcov() must work on the stripped lm object") | ||
|
|
||
| unstripped_lm = lm(newABUNDANCE ~ FEATURE + RUN, data = lm_input) | ||
| expect_true(object.size(lm_fit) < object.size(unstripped_lm), | ||
| info = paste("Stripped lm should be smaller.", | ||
| "Stripped:", object.size(lm_fit), | ||
| "Unstripped:", object.size(unstripped_lm))) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refresh
fittedafter each weighted refit.At Line 220 you refresh
abs.residsfromwls.fit, butfittedis never refreshed after Line 211. Fornum_iter > 1, later loess iterations use stale fitted values and inconsistent residual/fitted pairs.Suggested fix
for (i in seq_len(num_iter)) { if (i == 1) { input[["abs.resids"]] = abs(fit$residuals) input[["fitted"]] = fit$fitted.values } fit.loess = loess(abs.resids ~ fitted, data = input) input[["loess.fitted"]] = fitted(fit.loess) ## loess fitted values are predicted sd input[["weight"]] = 1 / (input[["loess.fitted"]] ^ 2) input[["abs.resids"]] = NULL ## re-fit using weight wls.fit = lm(formula(fit), data = input, weights = weight) input[["abs.resids"]] = abs(wls.fit$residuals) + input[["fitted"]] = wls.fit$fitted.values input[["loess.fitted"]] = NULL input[["weight"]] = NULL }🤖 Prompt for AI Agents