Skip to content

Make the dependents of a copied model follow the copy - #304

Merged
henrikjacobsenfys merged 1 commit into
developfrom
copy-dependents-follow-copy
Nov 24, 2026
Merged

henrikjacobsenfys merged 1 commit into
developfrom
copy-dependents-follow-copy

Conversation

@henrikjacobsenfys

Copy link
Copy Markdown
Member

Part of #126. Split out of #294, and best merged before it.

Problem

A copy of some objects silently ignored changes to its own parameters.

EasyScience's from_dict, which copy goes through, builds an object from its values and then swaps in the deserialized Parameters. Anything __init__ made dependent keeps following the Parameters it created, which are then discarded:

  • StretchedExponential: the width of a copy did not follow its beta and relaxation_time.
  • Diffusion models with Q set: the per-Q components of a copy ignored the copy's parameters.
  • DeltaLorentz and MittagLefflerDiffusion: A_1 did not follow A_0.
  • DeltaLorentz: the EISF did not follow mean_u_squared.
c = copy(edyn.DeltaLorentz(mean_u_squared=0.3, A_0=0.4, Q=[0.5, 1.0, 1.5]))
c.mean_u_squared = 1.5      # had no effect on the components of the copy

A fit of such a copy would vary a parameter with no effect.

Change

from_dict rebuilds the dependents on the Parameters now in place:

  • StretchedExponential.from_dict drops the width, which the width property rebuilds on first use.
  • DiffusionModelBase.from_dict calls a new _on_parameters_replaced hook, which rebuilds the per-Q components. DeltaLorentz and MittagLefflerDiffusion re-tie their dependent parameters in it first.

Review

An independent review of #294 found the mean_u_squared case, which the original change missed, and reproduced it. It is fixed here and covered by the DeltaLorentz test.

Tests

One test in each of test_stretched_exponential.py, test_brownian_translational_diffusion.py, test_delta_lorentz.py and test_mittag_leffler_diffusion.py: change a parameter of a copy and compare with a model built with that value.

🤖 Generated with Claude Code

EasyScience's from_dict builds an object from its values and then
swaps in the deserialised Parameters. Anything __init__ made dependent
still follows the Parameters it created, which are then discarded. A
copy, which goes through from_dict, therefore ignored changes to its
own parameters:

- StretchedExponential: the width did not follow beta and
  relaxation_time.
- The diffusion models: the per-Q components did not follow the
  model's parameters; A_1 did not follow A_0 in DeltaLorentz and
  MittagLefflerDiffusion; and the EISF of a DeltaLorentz did not follow
  mean_u_squared.

from_dict now rebuilds those dependents on the Parameters in place.

Split out of the PR for issue #126. The mean_u_squared case was found
by an independent review of that PR.

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
@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.23%. Comparing base (4661fda) to head (a2b93ff).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #304      +/-   ##
===========================================
+ Coverage    98.21%   98.23%   +0.02%     
===========================================
  Files           65       65              
  Lines         6399     6419      +20     
  Branches      1082     1082              
===========================================
+ Hits          6285     6306      +21     
  Misses          60       60              
+ Partials        54       53       -1     
Flag Coverage Δ
unittests 98.23% <100.00%> (+0.02%) ⬆️

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

Files with missing lines Coverage Δ
...s/sample_model/components/stretched_exponential.py 100.00% <100.00%> (ø)
...mics/sample_model/diffusion_model/delta_lorentz.py 97.65% <100.00%> (+0.04%) ⬆️
...mple_model/diffusion_model/diffusion_model_base.py 98.56% <100.00%> (+0.04%) ⬆️
..._model/diffusion_model/mittag_leffler_diffusion.py 91.00% <100.00%> (+0.42%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@henrikjacobsenfys
henrikjacobsenfys merged commit b0c1bf3 into develop Nov 24, 2026
49 of 51 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