-
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
Merged
+106
−20
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5693fc9
Freed heavy fit objects and large dataProcess intermediates early
tonywu1999 9edb718
Removing the unnecessary comments
Rudhik1904 d07f4d1
Adding the peak Script
Rudhik1904 4d2d41a
Fixing the PR comments
Rudhik1904 8a85c11
moving the script to MSstats Scalblity
Rudhik1904 2e6cf17
fixing the comment
Rudhik1904 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( | ||
|
tonywu1999 marked this conversation as resolved.
|
||
| 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.
Uh oh!
There was an error while loading. Please reload this page.
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
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.
Will this piece of code be changed in a future PR? It uses data.frame and [[ a lot
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.
@mstaniak, it looks like the current state of the file already addresses that — the version of.updateUnequalVariancesin the branch now usesdata.tablewith:=for all in-place operations rather thandata.frameand[[:The diff in the review was anchored to an earlier commit snapshot — the branch has since been updated to the
data.table/:=style. My earlierfittedrefresh suggestion still applies to this current version, though.