Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions news/getoption-by-name.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* ``RefinableObj.GetOption`` accepts an option name, e.g. ``pp.GetOption("Fit Scale Factors at Beginning of Optimization").SetChoice("No")`` to keep fixed scale factors unchanged in ``LSQ.Refine``.

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
3 changes: 3 additions & 0 deletions src/extensions/nb_refinableobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ void wrap_refinableobj(nb::module_& m)
.def("GetOption",
nb::overload_cast<const unsigned int>(&RefinableObj::GetOption),
nb::rv_policy::reference_internal)
.def("GetOption",
nb::overload_cast<const std::string&>(&RefinableObj::GetOption),
nb::rv_policy::reference_internal)
/* State */
.def("IsBeingRefined", &RefinableObj::IsBeingRefined)
.def("BeginGlobalOptRandomMove",&RefinableObj::BeginGlobalOptRandomMove)
Expand Down
37 changes: 37 additions & 0 deletions tests/test_lsq.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@

import unittest

import numpy as np
import pytest

from pyobjcryst import ObjCrystException, refinableobj
from pyobjcryst.atom import Atom
from pyobjcryst.crystal import Crystal
from pyobjcryst.diffractiondatasinglecrystal import (
DiffractionDataSingleCrystal,
)
from pyobjcryst.lsq import LSQ
from pyobjcryst.powderpattern import PowderPattern
from pyobjcryst.scatteringpower import ScatteringPowerAtom


class TestGlobalOptim(unittest.TestCase):
Expand Down Expand Up @@ -144,5 +149,37 @@ def test_lsq_prepare_ref_par_list_verbose(self):
lsq.PrepareRefParList(copy_param=False, verbose=True)


@pytest.mark.parametrize("choice, scale_after", [("Yes", 2.0), ("No", 1.0)])
def test_lsq_refine_fit_scale_begin_optimization(choice, scale_after):
"""Refine fits even fixed scale factors at the beginning, unless the
PowderPattern option is set to "No"."""
cryst = Crystal(4.05, 4.05, 4.05, "Fm-3m")
sp = ScatteringPowerAtom("Al", "Al")
cryst.AddScatteringPower(sp)
cryst.AddScatterer(Atom(0, 0, 0, "Al", sp))

pp = PowderPattern()
pp.SetWavelength(1.0)
pp.SetPowderPatternX(np.radians(np.linspace(10, 60, 2000)))
pp.AddPowderPatternDiffraction(cryst)
# observed pattern = calculated pattern at twice the scale
pp.SetPowderPatternObs(np.ones(2000))
pp.SetPowderPatternObs(2 * pp.GetPowderPatternCalc())
pp.GetOption("Fit Scale Factors at Beginning of Optimization").SetChoice(
choice
)

lsq = LSQ()
lsq.SetRefinedObj(pp, 0, True, True)
lsq.PrepareRefParList(False)
lsq.GetCompiledRefinedObj().FixAllPar()
pp.GetPar("Zero").SetIsFixed(False) # LSQ needs a free parameter
lsq.Refine(nbCycle=1, silent=True)

scale = pp.GetPar("Scale_")
assert scale.IsFixed()
assert scale.GetValue() == pytest.approx(scale_after, rel=1e-6)


if __name__ == "__main__":
unittest.main()
Loading