Repository navigation
Make copy and to_dict/from_dict work for the model classes - #294
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #294 +/- ##
===========================================
- Coverage 98.21% 98.21% -0.01%
===========================================
Files 65 65
Lines 6399 6398 -1
Branches 1082 1083 +1
===========================================
- Hits 6285 6284 -1
Misses 60 60
Partials 54 54
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Fixes #126. - ComponentCollection.to_dict accepts the skip argument easyscience passes, so SampleModel, ResolutionModel and BackgroundModel can be copied. - InstrumentModel inherits easyscience's ModelBase, whose from_dict handles the energy_offset Parameter. This also makes Analysis copyable. - StretchedExponential and the diffusion models rebuild what depends on their parameters after from_dict has replaced them, so the width, A_1 and the per-Q components of a copy follow the copy's own parameters. - Copying an Experiment without data no longer raises. Co-Authored-By: Claude Fable 5.2 <[email protected]>
An independent review found that only the last commit of this branch belonged to it; the others were stale copies of two PRs that have since changed or merged. They are dropped, and the part that makes the dependents of a copy follow the copy moved to its own PR. ComponentCollection.__copy__ is removed. With to_dict taking skip, the inherited EasyBase.__copy__ does the same and leaves the unique names out, which also lets a collection holding a component with an explicit unique name be copied. Tests: copying such a collection, and copying an Analysis, which the description claimed but nothing tested. Co-Authored-By: Claude Fable 5.2 <[email protected]>
henrikjacobsenfys
force-pushed
the
fix-126-serialization
branch
from
November 12, 2026 22:23
4768c21 to
0d4c85e
Compare
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.
Fixes #126.
Why
copy(sample_model)raised aTypeError, as didcopy()andto_dict()ofResolutionModel,BackgroundModel,InstrumentModelandAnalysis.What changes
ComponentCollection.to_dictacceptsskip. easyscience passes it to every nested object, so any model holding aComponentCollectionfailed to serialize. It is passed on to the components.ComponentCollection.__copy__is removed. Withto_dicttakingskip, the inheritedEasyBase.__copy__does the same and leaves the unique names out. A collection holding a component with an explicitunique_namecan now be copied; before, the copy tried to register the same name again.InstrumentModelinherits easyscience'sModelBaseinstead ofEasyBase.ModelBase.from_dictknows how to hand a serializedParameter(energy_offset) back to a constructor that takes a number. This also makesAnalysiscopyable.copy(Experiment())without data no longer raises.The second comment on the issue (template bounds overwritten by the parameter-creating mixin) no longer reproduces: bounds set on template components are kept in the per-Q copies and in copies of the model.
test_copyintest_model_base.pycovers it.Merge #304 first
Now that these copies succeed, the diffusion models and
StretchedExponentials inside them have to be right. #304 fixes that. The two PRs touch different files and neither needs the other to pass, but without #304 a copiedSampleModelholds diffusion components that ignore the copy's parameters.Things to be aware of
Copies that used to raise now succeed, and some of them lose state silently. All of this is tracked in #293:
Analysiscopies back to its template values, per-Q energy offsets and fixed flags are lost, and a copy of a calibratedResolutionModel.from_sample_model(...)is no longer calibrated.copy(Analysis)shares the experiment's data arrays with the original.copy(Experiment)copies them; the nested path passes them by reference. It only matters if the data are changed in place.nameset after construction is not copied forSampleModel,ResolutionModelandBackgroundModel, whose constructors take noname.to_dictis not JSON-serializable when Q is set.Review
An independent review found that only one of this branch's six commits belonged to it; the other five were stale copies of #289 and #290, which made the PR look like 2,600 lines. They are dropped. The review also suggested the split into two PRs and the removal of
ComponentCollection.__copy__.Tests
In the test files of the classes concerned, under a "Serialization (issue #126)" banner:
copyandto_dictofModelBase,SampleModel,InstrumentModel,ComponentCollection(withskip, and with an explicit unique name),Experimentwithout data, andAnalysis.🤖 Generated with Claude Code