Skip to content

Make copy and to_dict/from_dict work for the model classes - #294

Merged
henrikjacobsenfys merged 2 commits into
developfrom
fix-126-serialization
Nov 24, 2026
Merged

henrikjacobsenfys merged 2 commits into
developfrom
fix-126-serialization

Conversation

@henrikjacobsenfys

@henrikjacobsenfys henrikjacobsenfys commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Fixes #126.

Why

copy(sample_model) raised a TypeError, as did copy() and to_dict() of ResolutionModel, BackgroundModel, InstrumentModel and Analysis.

What changes

  • ComponentCollection.to_dict accepts skip. easyscience passes it to every nested object, so any model holding a ComponentCollection failed to serialize. It is passed on to the components.
  • ComponentCollection.__copy__ is removed. With to_dict taking skip, the inherited EasyBase.__copy__ does the same and leaves the unique names out. A collection holding a component with an explicit unique_name can now be copied; before, the copy tried to register the same name again.
  • InstrumentModel inherits easyscience's ModelBase instead of EasyBase. ModelBase.from_dict knows how to hand a serialized Parameter (energy_offset) back to a constructor that takes a number. This also makes Analysis copyable.
  • 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_copy in test_model_base.py covers 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 copied SampleModel holds 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:

  • Per-Q values are not carried over. A copy is rebuilt from the constructor arguments, so a fitted Analysis copies back to its template values, per-Q energy offsets and fixed flags are lost, and a copy of a calibrated ResolutionModel.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.
  • A name set after construction is not copied for SampleModel, ResolutionModel and BackgroundModel, whose constructors take no name.
  • to_dict is 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: copy and to_dict of ModelBase, SampleModel, InstrumentModel, ComponentCollection (with skip, and with an explicit unique name), Experiment without data, and Analysis.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.21%. Comparing base (4661fda) to head (0d4c85e).

Additional details and impacted files

Impacted file tree graph

@@             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              
Flag Coverage Δ
unittests 98.21% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/easydynamics/experiment/experiment.py 98.94% <100.00%> (+<0.01%) ⬆️
.../easydynamics/sample_model/component_collection.py 100.00% <100.00%> (ø)
src/easydynamics/sample_model/instrument_model.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

henrikjacobsenfys and others added 2 commits November 12, 2026 23:18
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 henrikjacobsenfys added [scope] bug Bug report or fix (major.minor.PATCH) [priority] medium Normal/default priority labels Nov 12, 2026
@henrikjacobsenfys
henrikjacobsenfys merged commit 46cd59b into develop Nov 24, 2026
51 of 56 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[priority] medium Normal/default priority [scope] bug Bug report or fix (major.minor.PATCH)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant