Repository navigation
Make models declare their inputs, so that least_squares() and evaluate() can check x. - #9
Merged
Merged
Conversation
…uate()` can check `x`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PZRXiFhGzzrF87vsFg73B9
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #9 +/- ##
=======================================
Coverage 99.65% 99.65%
=======================================
Files 8 8
Lines 861 881 +20
=======================================
+ Hits 858 878 +20
Misses 3 3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…d, and check `x` and the parameters in one `Model` method. - The names of the parameters and inputs must be a sequence of strings, so a set, whose order changes from one process to the next, bytes or `None` raise a `TypeError`. `Model.__post_init__` checks them too, so a model made directly is checked like one made with `model()`. - `inputs` and `cache` are keyword-only, so the inputs cannot be mistaken for the parameters, and 0.1 code which passed `cache` by position gets a clear error. - `Model._check_shapes()` checks the last axes of `x` and of the parameters against the model for `Model.__call__`, `least_squares()` and `evaluate()`, instead of three copies. The error from calling a model names its inputs and parameters. - `test_model_shapes` calls `gaussian_binned` with one column, and `test_model_invalid` makes each invalid model both ways. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PZRXiFhGzzrF87vsFg73B9
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Models now name their inputs, the columns of
x, the way they name their parameters:inputsis required, so this is a breaking change, meant for 0.2.0.Why
A compiled model doesn't check its indices.
least_squares()checks the guess against the model's parameters, but it had nothing to checkxagainst, so a model that reads more columns thanxhas read past the end of each row with no error. Fittinggaussian_binnedto one column of bin centers, for example, took the next point as the upper edge of each bin, and read past the end of the array at the last one.named-arrays needs the count too (sun-data/named-arrays#271). Along an axis where the inputs of a function array are bin edges,
na.ModelFitFunctionArraygives the model the lower and upper edge of each bin. Withmodel.inputs, it can reject a one-input model likegaussianthere, instead of silently fitting it to the lower edges.Changes
ndoptimize.model(parameters, *, inputs, cache=True):inputsis a sequence of unique names, checked likeparameters.Model.inputsholds them, and the repr shows them.inputsandcacheare keyword-only, so the inputs can't be swapped with the parameters, which are also a sequence of names.TypeError, as do bytes and names that aren't strings. AModelmade directly checks its names too, not only one made withmodel().least_squares()andevaluate()raise aValueErrornaming the model's inputs when the last axis ofxdoesn't match them, before any compiled code runs. Calling a model directly checks it too. OneModelmethod holds the checks ofxand of the parameters for all three, and the error from calling a model names its inputs and parameters.gaussian,gaussian_linearandtwo_gaussians_lineartake("x",), andgaussian_binnedtakes("lower", "upper"). Their docstrings give the shape ofxas(num_points, 1)or(num_points, 2), instead of reading "the first component of x".inputsin the model example, and say thatxhas one column per input.Migrating from 0.1
inputs=to every@ndoptimize.model(...), with one name for each column ofxthe model reads. A model that read only the first column of a widerxnow raises an error until the extra columns are dropped.cacheby keyword.Tests
Noneor a repeated name, inparametersor ininputs, raises, both throughmodel()and when aModelis made directly. Passinginputsby position raises.model.inputsand the repr hold the names.xwith the wrong number of columns raises inModel.__call__,least_squares()andevaluate(), includinggaussian_binnedgiven a single column in all three.82 tests pass with and without JIT, and every changed line is covered. ruff, black and pyright 1.1.411 are clean, and the documentation builds without warnings, with the model examples showing their inputs.
The named-arrays side, which checks the number of inputs it gives a model and points a one-input model given bin edges to a binned one, passes its tests against this branch. It will go into #271 once this is released.
🤖 Generated with Claude Code
https://claude.ai/code/session_01PZRXiFhGzzrF87vsFg73B9