-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Spectrum as a read-only class obtainable from an imageset (#201)
* Add Spectrum as a read-only class obtainable from an imageset Details: - Add Spectrum C++ object that implements methods get_energies_eV (eV), get_weights (unitless), and get_weighted_wavelenth (angstroms). Spectrum can only be set from constructor. - Add get_spectrum method to Format and FormatMultiImage - Add get_spectrum method to ImageSet. Does not cache it like it does for detector, beam, etc. - Implement get_spectrum in NeXus. Specifically implements nexusformat/definitions#717, using optional variants to specify a calibrated wavelength and a spectrum. - Add test for c++ spectrum object - Add bandwidth calculation. Computes: emin_ev = 1% on CDF and emax_ev = 99% on CDF so 98% or more of the spectrum is within the range. Added a test for this, which has a computed approximation to a real SwissFEL spectrum (so could be useful in other tests) - Add weighted energy variance. Recover the weighted variance around the weighted mean. Add test which makes a clean Gaussian and recovers the same parameters. Co-authored-by: Graeme Winter <graeme.winter@gmail.com>
- Loading branch information
1 parent
f1f15ee
commit bf5c4ae
Showing
14 changed files
with
414 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* spectrum.cc | ||
*/ | ||
#include <boost/python.hpp> | ||
#include <boost/python/def.hpp> | ||
#include <string> | ||
#include <sstream> | ||
#include <dxtbx/model/spectrum.h> | ||
#include <dxtbx/model/boost_python/to_from_dict.h> | ||
#include <scitbx/array_family/boost_python/flex_wrapper.h> | ||
|
||
namespace dxtbx { namespace model { namespace boost_python { | ||
|
||
using namespace boost::python; | ||
using scitbx::deg_as_rad; | ||
using scitbx::rad_as_deg; | ||
|
||
std::string spectrum_to_string(const Spectrum &spectrum) { | ||
std::stringstream ss; | ||
ss << spectrum; | ||
return ss.str(); | ||
} | ||
|
||
struct SpectrumPickleSuite : boost::python::pickle_suite { | ||
static boost::python::tuple getinitargs(const Spectrum &obj) { | ||
return boost::python::make_tuple(obj.get_energies_eV(), obj.get_weights()); | ||
} | ||
|
||
static boost::python::tuple getstate(boost::python::object obj) { | ||
return boost::python::make_tuple(obj.attr("__dict__")); | ||
} | ||
|
||
static void setstate(boost::python::object obj, boost::python::tuple state) { | ||
DXTBX_ASSERT(boost::python::len(state) == 2); | ||
|
||
// restore the object's __dict__ | ||
boost::python::dict d = | ||
boost::python::extract<boost::python::dict>(obj.attr("__dict__"))(); | ||
d.update(state[0]); | ||
} | ||
|
||
static bool getstate_manages_dict() { | ||
return true; | ||
} | ||
}; | ||
|
||
template <> | ||
boost::python::dict to_dict<Spectrum>(const Spectrum &obj) { | ||
boost::python::dict result; | ||
result["energies"] = obj.get_energies_eV(); | ||
result["weights"] = obj.get_weights(); | ||
return result; | ||
} | ||
|
||
template <> | ||
Spectrum *from_dict<Spectrum>(boost::python::dict obj) { | ||
Spectrum *s = new Spectrum(boost::python::extract<vecd>(obj["energies"]), | ||
boost::python::extract<vecd>(obj["weights"])); | ||
return s; | ||
} | ||
|
||
void export_spectrum() { | ||
// Export Spectrum | ||
class_<Spectrum, boost::shared_ptr<Spectrum> >("Spectrum") | ||
.def(init<const Spectrum &>()) | ||
.def(init<vecd, vecd>((arg("energies"), arg("weights")))) | ||
.def("get_energies_eV", &Spectrum::get_energies_eV) | ||
.def("get_weights", &Spectrum::get_weights) | ||
.def("get_weighted_energy_eV", &Spectrum::get_weighted_energy_eV) | ||
.def("get_weighted_energy_variance", &Spectrum::get_weighted_energy_variance) | ||
.def("get_weighted_wavelength", &Spectrum::get_weighted_wavelength) | ||
.def("get_emin_eV", &Spectrum::get_emin_eV) | ||
.def("get_emax_eV", &Spectrum::get_emax_eV) | ||
.def("__str__", &spectrum_to_string) | ||
.def("to_dict", &to_dict<Spectrum>) | ||
.def("from_dict", &from_dict<Spectrum>, return_value_policy<manage_new_object>()) | ||
.staticmethod("from_dict") | ||
.def_pickle(SpectrumPickleSuite()); | ||
|
||
scitbx::af::boost_python::flex_wrapper<Spectrum>::plain("flex_Spectrum"); | ||
} | ||
|
||
}}} // namespace dxtbx::model::boost_python |
Oops, something went wrong.