Skip to content

rebuilt trim_grid.py. is now more flexible, faster, and memory-efficient - #873

Merged
karllark merged 5 commits into
BEAST-Fitting:masterfrom
benjamin-gibson:trim_grid_completeness
Nov 3, 2026
Merged

karllark merged 5 commits into
BEAST-Fitting:masterfrom
benjamin-gibson:trim_grid_completeness

Conversation

@benjamin-gibson

@benjamin-gibson benjamin-gibson commented Oct 16, 2026 •

Copy link
Copy Markdown

I spent two weeks thinking this entire code was broken and needed to be rethought. Turns out it wasn't. Worked pretty well, other than being slow and needing a lot of memory.

The only functionality I ended up adding was some code that calculates an optimal value of sigma_fac (the factor by which you multiply the errors on the noisemodel to determine the rigidity with which the models are trimmed). The user now sets a value of sigma_fac (default is still 3.0), and this is now the minimum sigma_fac that gets used. Ensures that the brightest/faintest stars in a file aren't accidentally unable to be fit because their models get trimmed. I also removed the n_detected parameter from the function call, as that was not used anywhere.

I did pretty extensive testing of this and, though yes it does increase the number of models that make it through the trimming, it is still pretty efficient while being robust to outliers.

And all the worries I had about completeness were unwarranted. How the BEAST generates noisemodels is sound, as far as I can tell).

@benjamin-gibson

Copy link
Copy Markdown
Author

I reformatted with black, so we should pass the codestyle test now, but IDK what's going on with the cov_test. standby.

@benjamin-gibson

Copy link
Copy Markdown
Author

Okay so with that new functionality where a different value of sigma_fac could be calculated and used, we're naturally not getting back the number of trimmed SEDs that the test is expecting.
I've run the code locally and can confirm everything works, it's now just a factor of if we want to accept this new functionality and update the test, or remove the functionality. Whatcha think, Karl?

@karllark

Copy link
Copy Markdown
Member

The main description starts with "...ent". What does this mean?

@karllark

karllark commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Codestyle test is still failing. Please fix. You said you used black, but that should have fixed things. Did you push the updated code?

@karllark

Copy link
Copy Markdown
Member

Why are the changes to docs in a different PR?

@karllark karllark left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single comment, not a full review yet.

Comment thread beast/fitting/trim_grid.py Resolved
@benjamin-gibson benjamin-gibson changed the title rebuilt trim_grid.py. is now more flexible, faster, and memory-effici… rebuilt trim_grid.py. is now more flexible, faster, and memory-efficient Oct 20, 2026
@benjamin-gibson

Copy link
Copy Markdown
Author

After discussion between Karl and myself, we decided to retain the calculation of sigma_fac, as a straight cutting at sigma_fac=3 could easily cut out models that represent a very small subset of good data. Along those same lines, I've removed the 0.1st and 99.9th percentiles and returned to a straight minimum and maximum for calculation of sigma_fac -- the thinking there was that those 0.2% of data points were photometric errors that won't be able to be properly fit. However, in the case where no bad data is in the catalog, this would systematically bias your results. The desire here is to ensure that all of the data can be represented by models retained post-trimming, so that even the weirdest, rarest stars have a chance of being adequately fit. Hence going back to the minimum and maximum of the data range. I estimate this will somewhat noticeably increase the number of models that are retained post trimming, but this should ensure we get more high quality fits.

Additionally, this new PR will include updates to the docs -- explaining the new definition of sigma_fac, and clarifying how the cut_flag for ASTs should be defined -- and the reformatting with black so the codestyle test should not fail.

I also changed the documentation for running the coverage tests, as that has been wrong for a while. The proper function call should be tox -e test -- --remote-data --cov, not tox -e test -- --remote-data --coverage.

We will need to update the regression test to the new number of models that survive, but it is showing that with the new calculation of sigma_fac, we are now trimming fewer models than before, which is the expected behavior.

@karllark karllark left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Thanks for this PR.

A couple of small comments requesting answers and then this PR is ready for merging.

Comment thread beast/fitting/trim_grid.py Resolved
Comment thread beast/fitting/trim_grid.py Resolved
Comment thread beast/fitting/trim_grid.py Resolved
Comment thread beast/fitting/trim_grid.py Resolved
Comment thread docs/beast_development.rst Resolved
@benjamin-gibson

Copy link
Copy Markdown
Author

All the delete statements were included for memory concerns. Basically I was testing this on my mac (which is not short for RAM) and was constantly getting kernel death from trying to hold 150 million SEDs in memory and writing a bunch of new variables and such. The delete statements helped mitigate that by ensuring that old variables were removed before writing new ones.

I'm not married to this behavior and am willing to remove it, but when the model grids are large (>50 GB), this should help reduce the necessary memory to run this step.

@benjamin-gibson

Copy link
Copy Markdown
Author

Updated the comment so it no longer mentions the 0.1 and 99.9 percentile cut (as we don't use it). cov_test still has the same issue where it expects a lower number of retained models than it's actually getting, which isn't surprising. Now there's a new doc_test issue though.

@karllark

karllark commented Nov 3, 2026

Copy link
Copy Markdown
Member

The doc_test issues are due to other issues - like websites being down.

@karllark karllark left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@karllark
karllark merged commit db64099 into BEAST-Fitting:master Nov 3, 2026
7 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants