Skip to content
Merged
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/94.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* <news item>

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* Fixed `PowderPattern.AddPowderPatternDiffraction()` so a no-reflections failure does not leave a failed diffraction component attached to the powder pattern.

**Security:**

* <news item>
27 changes: 23 additions & 4 deletions src/extensions/powderpattern_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <boost/format.hpp>
#undef B0

#include <memory>
#include <string>

#include <ObjCryst/RefinableObj/RefinableObj.h>
Expand All @@ -39,7 +40,6 @@ using namespace ObjCryst;

namespace {


// This creates a C++ PowderPattern object
PowderPattern* _CreatePowderPatternFromCIF(bp::object input)
{
Expand Down Expand Up @@ -117,11 +117,30 @@ PowderPatternBackground& addppbackground(PowderPattern& pp)

PowderPatternDiffraction& addppdiffraction(PowderPattern& pp, Crystal& crst)
{
PowderPatternDiffraction* ppc = new PowderPatternDiffraction();
std::unique_ptr<PowderPatternDiffraction> ppc(new PowderPatternDiffraction());
ppc->SetCrystal(crst);
// Prepare against the target powder-pattern context before final
// registration so a no-reflections failure cannot leave a broken
// partially attached component behind.
ppc->SetParentPowderPattern(pp);
ppc->GenHKLFullSpace();
if(ppc->GetNbReflBelowMaxSinThetaOvLambda() == 0)
{
throw ObjCrystException(
"PowderPatternDiffraction::CalcSinThetaLambda(): there are no reflections!"
);
}
pp.AddPowderPatternComponent(*ppc);
pp.Prepare();
return *ppc;
try
{
pp.Prepare();
}
catch(...)
{
pp.RemovePowderPatternComponent(*ppc);
throw;
}
return *ppc.release();
}


Expand Down
1 change: 1 addition & 0 deletions src/extensions/powderpatterndiffraction_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@ void wrap_powderpatterndiffraction()
.def("GetFhklObsSq", &PowderPatternDiffraction::GetFhklObsSq,
return_value_policy<copy_const_reference>())
;

}
15 changes: 15 additions & 0 deletions tests/test_powderpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import numpy as np
import pytest
from testutils import makeCrystal, makeScatterer

from pyobjcryst import ObjCrystException
from pyobjcryst.indexing import CrystalCentering, CrystalSystem, quick_index
Expand Down Expand Up @@ -74,6 +75,20 @@ def test_GetPowderPatternX(self):
self.assertTrue(np.array_equal([], self.pp.GetPowderPatternX()))
return

def test_AddPowderPatternDiffraction_rollback_on_prepare_error(self):
pp = self.pp
crystal = makeCrystal(*makeScatterer())
pp.SetWavelength(1.54056)
# Keep the 2theta window below the first reflection so setup raises
# and we can verify the failed diffraction component is not retained.
pp.SetPowderPatternPar(np.deg2rad(0.1), np.deg2rad(0.01), 41)

with self.assertRaisesRegex(ObjCrystException, "no reflections"):
pp.AddPowderPatternDiffraction(crystal)

self.assertEqual(0, pp.GetNbPowderPatternComponent())
return

# def test_GetScaleFactor(self): assert False
# def test_ImportPowderPattern2ThetaObs(self): assert False
# def test_ImportPowderPattern2ThetaObsSigma(self): assert False
Expand Down
Loading