Fix ValueError when refitting after load(fitted_as_initial=True)#333
Open
gaoflow wants to merge 1 commit into
Open
Fix ValueError when refitting after load(fitted_as_initial=True)#333gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
CustomCircuit.load(..., fitted_as_initial=True) stores the fitted
parameters as a numpy array in self.initial_guess. A subsequent call to
fit() then evaluated self.initial_guess != [], which NumPy
broadcasts element-wise, raising:
ValueError: operands could not be broadcast together with
shapes (n,) (0,)
Check emptiness with len() instead so the guard works for both lists
and numpy arrays. Fixes ECSHackWeek#310.
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.
Summary
Loading a previously-fitted model with
fitted_as_initial=Trueand then refitting it on a new spectrum raises aValueError, as reported in #310:Root cause
BaseCircuit.load(..., fitted_as_initial=True)stores the fitted parameters as a numpy array:The subsequent
fit()then checks emptiness withif self.initial_guess != []:. For a numpy array this is an element-wise comparison, which NumPy tries to broadcast against the empty list[](shape(0,)) and raises the broadcasting error. (This matches the workaround noted in the issue thread — manually castinginitial_guessback to alist.)Fix
Test for emptiness with
len(self.initial_guess) != 0instead. This is robust whetherinitial_guessis a list (the construction-time type) or a numpy array (the post-load type), and preserves the existing behaviour for the empty case.Reproduction
Verification
Added a regression test
test_refit_after_load_fitted_as_initialinimpedance/tests/test_model_io.pythat fits a model, saves it, reloads it withfitted_as_initial=True, and refits. It fails on the current code with the reported(n,) (0,)broadcasting error and passes with this fix. The existingtest_model_iosuite continues to pass.Fixes #310.