Skip to content

Add get_parameters, fix_parameter and free_parameter to ModelBase - #297

Merged
henrikjacobsenfys merged 4 commits into
developfrom
issue-70-fix-parameter
Nov 24, 2026
Merged

henrikjacobsenfys merged 4 commits into
developfrom
issue-70-fix-parameter

Conversation

@henrikjacobsenfys

@henrikjacobsenfys henrikjacobsenfys commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Part of #70 (first of four PRs).

Problem

To fix one parameter at every Q, users had to loop over the per-Q component collections by hand, as tutorial 2 did:

for Q_index in range(analysis.sample_model.Q.size):
    lorz = analysis.sample_model.get_component_collection(Q_index=Q_index)['Water Lorentzian']
    lorz.width = lorz_width.value
    lorz.width.fixed = True

Change

Three methods on ModelBase, so SampleModel, ResolutionModel and BackgroundModel all get them. A parameter is addressed by component name and attribute name:

sample_model.get_parameters('Water Lorentzian', 'width')              # one Parameter per Q
sample_model.fix_parameter('Water Lorentzian', 'width', value=0.2)    # every Q
sample_model.fix_parameter('Water Lorentzian', 'width', Q_index=1)    # one Q
sample_model.free_parameter('Water Lorentzian', 'width')
  • Rebuilds: fixing or freeing at every Q also updates the template component, so the setting survives a rebuild of the per-Q collections and applies when Q is set later. This matches fix_all_parameters. A single-Q change leaves the template alone.
  • Diffusion-model components: these are not in the template and are only changed per Q.
  • Errors: an unknown component raises KeyError, an attribute that is not a Parameter raises ValueError, and fixing a dependent parameter raises the existing EasyScience AttributeError.

Tutorial

The two fixing loops in tutorial2_nanoparticles.ipynb are replaced by fix_parameter calls. The notebook still runs.

Tests

New # Single-parameter access (issue #70) sections in test_model_base.py and test_sample_model.py.

Review

An independent review found no bugs in this PR. Changes from it: a test for the AttributeError on a dependent parameter, which the description promised, and two tests that no longer pin the text of an EasyScience message.

Limits worth knowing:

  • Polynomial coefficients cannot be addressed. fix_parameter('P', 'c0') raises ValueError: Component 'P' has no Parameter 'c0', since the coefficients are not attributes of the component. That matters for BackgroundModel, where a Polynomial is the usual component.
  • A fix on a diffusion-model component is lost when the diffusion model regenerates its components, e.g. when another SampleModel extends its Q grid.
  • Q_index with Q unset raises a bare IndexError.

🤖 Generated with Claude Code

Address one Parameter of a named component at every Q, or at a single Q,
without looping over the per-Q component collections by hand. Fixing or
freeing at every Q also updates the template, so it survives a rebuild.

Part of #70.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@henrikjacobsenfys henrikjacobsenfys added [scope] enhancement Adds/improves features (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.26%. Comparing base (46cd59b) to head (e33a608).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##           develop     #297   +/-   ##
========================================
  Coverage    98.25%   98.26%           
========================================
  Files           65       65           
  Lines         6477     6504   +27     
  Branches      1088     1094    +6     
========================================
+ Hits          6364     6391   +27     
  Misses          60       60           
  Partials        53       53           
Flag Coverage Δ
unittests 98.26% <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/sample_model/model_base.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 22:05
…eter

# Conflicts:
#	tests/unit/easydynamics/sample_model/test_sample_model.py
The description promises that fixing a dependent parameter raises the
EasyScience AttributeError; that is now tested. Two tests matched the
full text of an EasyScience KeyError and only need the missing name.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…eter

# Conflicts:
#	tests/unit/easydynamics/sample_model/test_model_base.py
#	tests/unit/easydynamics/sample_model/test_sample_model.py
@henrikjacobsenfys
henrikjacobsenfys merged commit 56257cd into develop Nov 24, 2026
31 of 33 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] enhancement Adds/improves features (major.MINOR.patch)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant