From e7fa295d999acf0be7a85757c94d80bb596644d2 Mon Sep 17 00:00:00 2001 From: James Beilsten-Edmands <30625594+jbeilstenedmands@users.noreply.github.com> Date: Fri, 13 Nov 2026 10:10:57 +0000 Subject: [PATCH 1/7] Start updating to shared_ptr --- dx2/experiment.cxx | 56 +++++++++++----------------- include/dx2/experiment.hpp | 75 ++++++++++++++++++++++++++++---------- 2 files changed, 77 insertions(+), 54 deletions(-) diff --git a/dx2/experiment.cxx b/dx2/experiment.cxx index 959c386..8bcec77 100644 --- a/dx2/experiment.cxx +++ b/dx2/experiment.cxx @@ -12,20 +12,20 @@ Experiment::Experiment(const json &experiment_data) { json detector_data = experiment_data["detector"][0]; Detector detector(detector_data); this->_identifier = identifier; - this->_beam = std::move(beam); - this->_scan = std::move(scan); - this->_goniometer = std::move(gonio); - this->_detector = std::move(detector); + _beam = std::make_shared(beam); + _scan = std::make_shared(scan); + _goniometer = std::make_shared(gonio); + >_detector = std::make_shared(detector); // Save the imageset json to propagate when saving to file. json imagesequence_data = experiment_data["imageset"][0]; ImageSequence imagesequence(imagesequence_data); - this->_imagesequence = imagesequence; + _imagesequence = std::make_shared(imagesequence); if (experiment_data.contains("crystal") && experiment_data["crystal"].is_array() && !experiment_data["crystal"].empty()) { const json &crystal_data = experiment_data["crystal"][0]; Crystal crystal(crystal_data); - this->_crystal = crystal; + set_crystal(crystal); } } @@ -44,15 +44,15 @@ json Experiment::to_json() const { expt_out["imageset"] = 0; // add the the actual models - elist_out["scan"] = std::array{_scan.to_json()}; - elist_out["goniometer"] = std::array{_goniometer.to_json()}; - elist_out["beam"] = std::array{beam_io::to_json(_beam)}; - elist_out["detector"] = std::array{_detector.to_json()}; - elist_out["imageset"] = std::array{_imagesequence.to_json()}; + elist_out["scan"] = std::array{_scan->to_json()}; + elist_out["goniometer"] = std::array{_goniometer->to_json()}; + elist_out["beam"] = std::array{beam_io::to_json(*_beam)}; + elist_out["detector"] = std::array{_detector->to_json()}; + elist_out["imageset"] = std::array{_imagesequence->to_json()}; - if (_crystal.get_U_matrix().determinant()) { + if (_crystal) { expt_out["crystal"] = 0; - elist_out["crystal"] = std::array{_crystal.to_json()}; + elist_out["crystal"] = std::array{_crystal->to_json()}; } else { elist_out["crystal"] = std::array{}; } @@ -61,34 +61,22 @@ json Experiment::to_json() const { return elist_out; } -Scan &Experiment::scan() { return _scan; } +void Experiment::set_crystal(Crystal crystal) { _crystal = std::make_shared(std::move(crystal)); } -const Goniometer &Experiment::goniometer() const { return _goniometer; } +void Experiment::set_crystal(std::shared_ptr crystal){_crystal = std::move(crystal);} -Detector &Experiment::detector() { return _detector; } +void Experiment::set_beam(std::shared_ptr beam) { _beam = std::move(beam); } -Crystal &Experiment::crystal() { return _crystal; } +void Experiment::set_scan(std::shared_ptr scan) { _scan = std::move(scan); } -ImageSequence &Experiment::imagesequence() { return _imagesequence; } +void Experiment::set_detector(std::shared_ptr detector) { _detector = std::move(detector); } -void Experiment::set_crystal(Crystal crystal) { _crystal = crystal; } - -void Experiment::set_beam(Beam beam) { _beam = std::move(beam); } - -Beam &Experiment::beam() { return _beam; } - -const Beam &Experiment::beam() const { return _beam; } - -void Experiment::set_scan(Scan scan) { _scan = scan; } - -void Experiment::set_detector(Detector detector) { _detector = detector; } - -void Experiment::set_goniometer(Goniometer goniometer) { - _goniometer = goniometer; +void Experiment::set_goniometer(std::shared_ptr goniometer) { + _goniometer = std::move(goniometer); } -void Experiment::set_imagesequence(ImageSequence imagesequence) { - _imagesequence = imagesequence; +void Experiment::set_imagesequence(std::shared_ptr imagesequence) { + _imagesequence = std::move(imagesequence); } const std::string &Experiment::identifier() const { return _identifier; } diff --git a/include/dx2/experiment.hpp b/include/dx2/experiment.hpp index 5f454ae..af4489c 100644 --- a/include/dx2/experiment.hpp +++ b/include/dx2/experiment.hpp @@ -14,34 +14,69 @@ using json = nlohmann::json; class Experiment { public: - Experiment() = default; Experiment(const json &experiment_data); + Experiment(std::shared_ptr beam, + std::shared_ptr detector + std::shared_ptr _scan, + std::shared_ptr _goniometer, + std::shared_ptr _imagesequence, + std::shared_ptr _crystal); json to_json() const; - Beam &beam(); - const Beam &beam() const; + // These models should always exist as they are defined by the experiment metadata + + Beam& beam() { return *_beam; } + const Beam& beam() const { return *_beam; } + + Detector& detector() { return *_detector; } + const Detector& detector() const { return *_detector; } + + Scan& scan() { return *_scan; } + const Scan& scan() const { return *_scan; } + + Goniometer& goniometer() { return *_goniometer; } + const Goniometer& goniometer() const { return *_goniometer; } + + ImageSequence& imagesequence() { return *_imagesequence; } + const ImageSequence& imagesequence() const { return *_imagesequence; } + + // These models are discovered during processing and so are optional + + Crystal& crystal() { + if (!_crystal) { + throw std::runtime_error("Experiment has no crystal"); + } + return *_crystal; + } + const Crystal& crystal() const { + if (!_crystal) { + throw std::runtime_error("Experiment has no crystal"); + } + return *_crystal; + } + + bool has_crystal() const { + return static_cast(_crystal); + } const std::string &identifier() const; - const Goniometer &goniometer() const; - Scan &scan(); - Detector &detector(); - Crystal &crystal(); - ImageSequence &imagesequence(); + + void set_beam(std::shared_ptr beam); + void set_detector(std::shared_ptr detector); + void set_scan(std::shared_ptr scan); + void set_goniometer(std::shared_ptr goniometer); + void set_imagesequence(std::shared_ptr imagesequence); + void set_crystal(std::shared_ptr crystal); void set_crystal(Crystal crystal); - void set_beam(Beam beam); - void set_scan(Scan scan); - void set_detector(Detector detector); - void set_goniometer(Goniometer goniometer); - void set_imagesequence(ImageSequence imagesequence); void set_identifier(std::string identifier); void generate_identifier(); -protected: - Beam _beam{}; - Scan _scan{}; - Goniometer _goniometer{}; - Detector _detector{}; - Crystal _crystal{}; - ImageSequence _imagesequence{}; +private: + std::shared_ptr _beam; + std::shared_ptr _detector; + std::shared_ptr _scan; + std::shared_ptr _goniometer; + std::shared_ptr _imagesequence; + std::shared_ptr _crystal; std::string _identifier{}; }; From 920b3d2053ab372e50c3642a84bb41ebdf258989 Mon Sep 17 00:00:00 2001 From: James Beilsten-Edmands <30625594+jbeilstenedmands@users.noreply.github.com> Date: Fri, 13 Nov 2026 12:09:02 +0000 Subject: [PATCH 2/7] better experiment model --- dx2/experiment.cxx | 56 ++++++++++++++++-------------- include/dx2/experiment.hpp | 71 +++++++++++++++----------------------- 2 files changed, 58 insertions(+), 69 deletions(-) diff --git a/dx2/experiment.cxx b/dx2/experiment.cxx index 8bcec77..3c8f6f9 100644 --- a/dx2/experiment.cxx +++ b/dx2/experiment.cxx @@ -1,6 +1,13 @@ #include #include +Experiment::Experiment(Models models, std::string identifier) + : _models(std::move(models)), _identifier(std::move(identifier)){ + if (_identifier.empty()) { + generate_identifier(); + } + } + Experiment::Experiment(const json &experiment_data) { std::string identifier = experiment_data["experiment"][0]["identifier"]; json beam_data = experiment_data["beam"][0]; @@ -12,14 +19,14 @@ Experiment::Experiment(const json &experiment_data) { json detector_data = experiment_data["detector"][0]; Detector detector(detector_data); this->_identifier = identifier; - _beam = std::make_shared(beam); - _scan = std::make_shared(scan); - _goniometer = std::make_shared(gonio); - >_detector = std::make_shared(detector); + _models.beam = std::make_shared(beam); + _models.scan = std::make_shared(scan); + _models.goniometer = std::make_shared(gonio); + _models.detector = std::make_shared(detector); // Save the imageset json to propagate when saving to file. json imagesequence_data = experiment_data["imageset"][0]; ImageSequence imagesequence(imagesequence_data); - _imagesequence = std::make_shared(imagesequence); + _models.imagesequence = std::make_shared(imagesequence); if (experiment_data.contains("crystal") && experiment_data["crystal"].is_array() && !experiment_data["crystal"].empty()) { @@ -44,15 +51,15 @@ json Experiment::to_json() const { expt_out["imageset"] = 0; // add the the actual models - elist_out["scan"] = std::array{_scan->to_json()}; - elist_out["goniometer"] = std::array{_goniometer->to_json()}; - elist_out["beam"] = std::array{beam_io::to_json(*_beam)}; - elist_out["detector"] = std::array{_detector->to_json()}; - elist_out["imageset"] = std::array{_imagesequence->to_json()}; + elist_out["scan"] = std::array{_models.scan->to_json()}; + elist_out["goniometer"] = std::array{_models.goniometer->to_json()}; + elist_out["beam"] = std::array{beam_io::to_json(*_models.beam)}; + elist_out["detector"] = std::array{_models.detector->to_json()}; + elist_out["imageset"] = std::array{_models.imagesequence->to_json()}; - if (_crystal) { + if (has_crystal()) { expt_out["crystal"] = 0; - elist_out["crystal"] = std::array{_crystal->to_json()}; + elist_out["crystal"] = std::array{_models.crystal->to_json()}; } else { elist_out["crystal"] = std::array{}; } @@ -61,22 +68,21 @@ json Experiment::to_json() const { return elist_out; } -void Experiment::set_crystal(Crystal crystal) { _crystal = std::make_shared(std::move(crystal)); } - -void Experiment::set_crystal(std::shared_ptr crystal){_crystal = std::move(crystal);} - -void Experiment::set_beam(std::shared_ptr beam) { _beam = std::move(beam); } - -void Experiment::set_scan(std::shared_ptr scan) { _scan = std::move(scan); } +void Experiment::set_crystal(Crystal crystal) { _models.crystal = std::make_shared(std::move(crystal)); } -void Experiment::set_detector(std::shared_ptr detector) { _detector = std::move(detector); } +void Experiment::set_crystal(std::shared_ptr crystal){_models.crystal = std::move(crystal);} -void Experiment::set_goniometer(std::shared_ptr goniometer) { - _goniometer = std::move(goniometer); +Crystal& Experiment::crystal() { + if (!_models.crystal) { + throw std::runtime_error("Experiment has no crystal"); + } + return *_models.crystal; } - -void Experiment::set_imagesequence(std::shared_ptr imagesequence) { - _imagesequence = std::move(imagesequence); +const Crystal& Experiment::crystal() const { + if (!_models.crystal) { + throw std::runtime_error("Experiment has no crystal"); + } + return *_models.crystal; } const std::string &Experiment::identifier() const { return _identifier; } diff --git a/include/dx2/experiment.hpp b/include/dx2/experiment.hpp index af4489c..24be366 100644 --- a/include/dx2/experiment.hpp +++ b/include/dx2/experiment.hpp @@ -14,69 +14,52 @@ using json = nlohmann::json; class Experiment { public: + + struct Models { + std::shared_ptr beam; + std::shared_ptr detector; + std::shared_ptr scan; + std::shared_ptr goniometer; + std::shared_ptr imagesequence; + std::shared_ptr crystal = nullptr; // Optional models default to null + }; + + explicit Experiment(Models models, std::string identifier); + Experiment(const json &experiment_data); - Experiment(std::shared_ptr beam, - std::shared_ptr detector - std::shared_ptr _scan, - std::shared_ptr _goniometer, - std::shared_ptr _imagesequence, - std::shared_ptr _crystal); + json to_json() const; // These models should always exist as they are defined by the experiment metadata - Beam& beam() { return *_beam; } - const Beam& beam() const { return *_beam; } + Beam& beam() { return *_models.beam; } + const Beam& beam() const { return *_models.beam; } - Detector& detector() { return *_detector; } - const Detector& detector() const { return *_detector; } + Detector& detector() { return *_models.detector; } + const Detector& detector() const { return *_models.detector; } - Scan& scan() { return *_scan; } - const Scan& scan() const { return *_scan; } + Scan& scan() { return *_models.scan; } + const Scan& scan() const { return *_models.scan; } - Goniometer& goniometer() { return *_goniometer; } - const Goniometer& goniometer() const { return *_goniometer; } + Goniometer& goniometer() { return *_models.goniometer; } + const Goniometer& goniometer() const { return *_models.goniometer; } - ImageSequence& imagesequence() { return *_imagesequence; } - const ImageSequence& imagesequence() const { return *_imagesequence; } + ImageSequence& imagesequence() { return *_models.imagesequence; } + const ImageSequence& imagesequence() const { return *_models.imagesequence; } // These models are discovered during processing and so are optional - - Crystal& crystal() { - if (!_crystal) { - throw std::runtime_error("Experiment has no crystal"); - } - return *_crystal; - } - const Crystal& crystal() const { - if (!_crystal) { - throw std::runtime_error("Experiment has no crystal"); - } - return *_crystal; - } - - bool has_crystal() const { - return static_cast(_crystal); - } + Crystal& crystal(); + const Crystal& crystal() const; + bool has_crystal() const { return static_cast(_models.crystal);} const std::string &identifier() const; - void set_beam(std::shared_ptr beam); - void set_detector(std::shared_ptr detector); - void set_scan(std::shared_ptr scan); - void set_goniometer(std::shared_ptr goniometer); - void set_imagesequence(std::shared_ptr imagesequence); void set_crystal(std::shared_ptr crystal); void set_crystal(Crystal crystal); void set_identifier(std::string identifier); void generate_identifier(); private: - std::shared_ptr _beam; - std::shared_ptr _detector; - std::shared_ptr _scan; - std::shared_ptr _goniometer; - std::shared_ptr _imagesequence; - std::shared_ptr _crystal; + Models _models; std::string _identifier{}; }; From bd67eea40988a966195e1b61e2c3dba8a2e9bc51 Mon Sep 17 00:00:00 2001 From: James Beilsten-Edmands <30625594+jbeilstenedmands@users.noreply.github.com> Date: Fri, 13 Nov 2026 13:41:04 +0000 Subject: [PATCH 3/7] empty default --- include/dx2/experiment.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/dx2/experiment.hpp b/include/dx2/experiment.hpp index 24be366..d1ca4a7 100644 --- a/include/dx2/experiment.hpp +++ b/include/dx2/experiment.hpp @@ -24,7 +24,7 @@ class Experiment { std::shared_ptr crystal = nullptr; // Optional models default to null }; - explicit Experiment(Models models, std::string identifier); + explicit Experiment(Models models, std::string identifier={}); Experiment(const json &experiment_data); From d474619c23b4da554b556743ed64550da40431f8 Mon Sep 17 00:00:00 2001 From: James Beilsten-Edmands <30625594+jbeilstenedmands@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:27:58 +0000 Subject: [PATCH 4/7] Add ExperimentList class --- dx2/CMakeLists.txt | 1 + dx2/beam.cxx | 15 ++ dx2/experiment.cxx | 33 ++++- dx2/experiment_list.cxx | 240 ++++++++++++++++++++++++++++++++ include/dx2/beam.hpp | 2 + include/dx2/experiment.hpp | 18 ++- include/dx2/experiment_list.hpp | 31 +++++ tests/CMakeLists.txt | 4 + tests/test_experiment.cxx | 57 ++++++++ 9 files changed, 396 insertions(+), 5 deletions(-) create mode 100644 dx2/experiment_list.cxx create mode 100644 include/dx2/experiment_list.hpp create mode 100644 tests/test_experiment.cxx diff --git a/dx2/CMakeLists.txt b/dx2/CMakeLists.txt index e780486..1c5e3c0 100644 --- a/dx2/CMakeLists.txt +++ b/dx2/CMakeLists.txt @@ -12,6 +12,7 @@ add_library( detector_attenuations.cxx imagesequence.cxx experiment.cxx + experiment_list.cxx beam_ops.cxx beam_io.cxx ) diff --git a/dx2/beam.cxx b/dx2/beam.cxx index c56ebbc..041267b 100644 --- a/dx2/beam.cxx +++ b/dx2/beam.cxx @@ -138,6 +138,14 @@ json MonochromaticBeam::to_json() const { return to_json("x-ray"); } +void MonochromaticBeam::update(const MonochromaticBeam& beam){ + Vector3d s0 = beam.get_s0(); + polarization_normal_ = beam.get_polarization_normal(); + polarization_fraction_ = beam.get_polarization_fraction(); + wavelength_ = beam.get_wavelength(); + sample_to_source_direction_ = -1.0 * s0 * wavelength_; +} + double MonochromaticBeam::get_wavelength() const { return wavelength_; } void MonochromaticBeam::set_wavelength(double wavelength) { @@ -221,6 +229,13 @@ void PolychromaticBeam::set_wavelength_range( wavelength_range_ = wavelength_range; } +void PolychromaticBeam::update(const PolychromaticBeam& beam){ + wavelength_range_ = beam.get_wavelength_range(); + polarization_normal_ = beam.get_polarization_normal(); + polarization_fraction_ = beam.get_polarization_fraction(); + // sample_to_source_direction_ = FIXME is this modifiable in Poly? +} + // MonoXrayBeam definitions json MonoXrayBeam::to_json() const { diff --git a/dx2/experiment.cxx b/dx2/experiment.cxx index 3c8f6f9..d500582 100644 --- a/dx2/experiment.cxx +++ b/dx2/experiment.cxx @@ -8,10 +8,35 @@ Experiment::Experiment(Models models, std::string identifier) } } -Experiment::Experiment(const json &experiment_data) { +Experiment::Experiment(const json &experiment_data) + : _models( + std::make_shared( + beam_io::from_json(experiment_data["beam"][0])), + std::make_shared( + Detector(experiment_data["detector"][0])), + std::make_shared( + Scan(experiment_data["scan"][0])), + std::make_shared( + Goniometer(experiment_data["goniometer"][0])), + std::make_shared( + ImageSequence(experiment_data["imageset"][0]))) +{ + _identifier = experiment_data["experiment"][0]["identifier"]; + + if (experiment_data.contains("crystal") && + experiment_data["crystal"].is_array() && + !experiment_data["crystal"].empty()) { + + _models.crystal = + std::make_shared( + experiment_data["crystal"][0]); + } +} + + +/*Experiment::Experiment(const json &experiment_data) { std::string identifier = experiment_data["experiment"][0]["identifier"]; json beam_data = experiment_data["beam"][0]; - Beam beam = beam_io::from_json(beam_data); json scan_data = experiment_data["scan"][0]; Scan scan(scan_data); json gonio_data = experiment_data["goniometer"][0]; @@ -19,7 +44,7 @@ Experiment::Experiment(const json &experiment_data) { json detector_data = experiment_data["detector"][0]; Detector detector(detector_data); this->_identifier = identifier; - _models.beam = std::make_shared(beam); + _models.beam = std::make_shared(beam_io::from_json(beam_data)); _models.scan = std::make_shared(scan); _models.goniometer = std::make_shared(gonio); _models.detector = std::make_shared(detector); @@ -34,7 +59,7 @@ Experiment::Experiment(const json &experiment_data) { Crystal crystal(crystal_data); set_crystal(crystal); } -} +}*/ json Experiment::to_json() const { // save this experiment as an example experiment list diff --git a/dx2/experiment_list.cxx b/dx2/experiment_list.cxx new file mode 100644 index 0000000..fc51ed9 --- /dev/null +++ b/dx2/experiment_list.cxx @@ -0,0 +1,240 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +ExperimentList::ExperimentList(const json& experiment_data) { + + // Phase 1: deserialize all models + + std::vector> beams; + for (const auto& beam_json : experiment_data["beam"]) { + beams.push_back( + std::make_shared( + beam_io::from_json(beam_json))); + } + + std::vector> detectors; + for (const auto& detector_json : experiment_data["detector"]) { + detectors.push_back( + std::make_shared(Detector(detector_json))); + } + + std::vector> scans; + for (const auto& scan_json : experiment_data["scan"]) { + scans.push_back( + std::make_shared(Scan(scan_json))); + } + + std::vector> goniometers; + for (const auto& gonio_json : experiment_data["goniometer"]) { + goniometers.push_back( + std::make_shared(Goniometer(gonio_json))); + } + + std::vector> imagesets; + for (const auto& imageset_json : experiment_data["imageset"]) { + imagesets.push_back( + std::make_shared(ImageSequence(imageset_json))); + } + + std::vector> crystals; + if (experiment_data.contains("crystal")) { + for (const auto& crystal_json : experiment_data["crystal"]) { + crystals.push_back( + std::make_shared(Crystal(crystal_json))); + } + } + + // Phase 2: construct experiments + + for (const auto& expt_json : experiment_data["experiment"]) { + + Experiment::Models models{ + beams.at(expt_json["beam"]), + detectors.at(expt_json["detector"]), + scans.at(expt_json["scan"]), + goniometers.at(expt_json["goniometer"]), + imagesets.at(expt_json["imageset"]) + }; + + if (expt_json.contains("crystal")) { + models.crystal = + crystals.at(expt_json["crystal"]); + } + + _experiments.emplace_back( + std::move(models), + expt_json["identifier"]); + } +} + + +json ExperimentList::to_json() const { + + json result; + result["__id__"] = "ExperimentList"; + + result["experiment"] = json::array(); + result["beam"] = json::array(); + result["detector"] = json::array(); + result["scan"] = json::array(); + result["goniometer"] = json::array(); + result["imageset"] = json::array(); + result["crystal"] = json::array(); + + std::unordered_map beam_indices; + std::unordered_map detector_indices; + std::unordered_map scan_indices; + std::unordered_map goniometer_indices; + std::unordered_map imageset_indices; + std::unordered_map crystal_indices; + + for (const auto &expt : _experiments) { + + json expt_json; + expt_json["__id__"] = "Experiment"; + expt_json["identifier"] = expt.identifier(); + + // + // Beam + // + const Beam *beam_ptr = expt._models.beam.get(); + auto [beam_it, beam_inserted] = + beam_indices.emplace(beam_ptr, beam_indices.size()); + + if (beam_inserted) { + result["beam"].push_back( + beam_io::to_json(*expt._models.beam)); + } + + expt_json["beam"] = beam_it->second; + + // + // Detector + // + const Detector *detector_ptr = expt._models.detector.get(); + auto [detector_it, detector_inserted] = + detector_indices.emplace( + detector_ptr, + detector_indices.size()); + + if (detector_inserted) { + result["detector"].push_back( + expt._models.detector->to_json()); + } + + expt_json["detector"] = detector_it->second; + + // + // Scan + // + const Scan *scan_ptr = expt._models.scan.get(); + auto [scan_it, scan_inserted] = + scan_indices.emplace( + scan_ptr, + scan_indices.size()); + + if (scan_inserted) { + result["scan"].push_back( + expt._models.scan->to_json()); + } + + expt_json["scan"] = scan_it->second; + + // + // Goniometer + // + const Goniometer *gonio_ptr = + expt._models.goniometer.get(); + + auto [gonio_it, gonio_inserted] = + goniometer_indices.emplace( + gonio_ptr, + goniometer_indices.size()); + + if (gonio_inserted) { + result["goniometer"].push_back( + expt._models.goniometer->to_json()); + } + + expt_json["goniometer"] = gonio_it->second; + + // + // Imageset + // + const ImageSequence *imageset_ptr = + expt._models.imagesequence.get(); + + auto [imageset_it, imageset_inserted] = + imageset_indices.emplace( + imageset_ptr, + imageset_indices.size()); + + if (imageset_inserted) { + result["imageset"].push_back( + expt._models.imagesequence->to_json()); + } + + expt_json["imageset"] = imageset_it->second; + + // + // Optional crystal + // + if (expt.has_crystal()) { + + const Crystal *crystal_ptr = + expt._models.crystal.get(); + + auto [crystal_it, crystal_inserted] = + crystal_indices.emplace( + crystal_ptr, + crystal_indices.size()); + + if (crystal_inserted) { + result["crystal"].push_back( + expt._models.crystal->to_json()); + } + + expt_json["crystal"] = crystal_it->second; + } + + result["experiment"].push_back( + std::move(expt_json)); + } + + return result; +} + + +ExperimentList ExperimentList::select( + const std::vector& identifiers) const { + + // Implement a simple linear scan and lookup for now. + std::unordered_set wanted( + identifiers.begin(), + identifiers.end()); + + ExperimentList result; + + for (const auto& expt : _experiments) { + if (wanted.contains(expt.identifier())) { + result.append(expt); + } + } + + return result; +} + +void ExperimentList::append(Experiment experiment) { + _experiments.push_back(std::move(experiment)); +} + + diff --git a/include/dx2/beam.hpp b/include/dx2/beam.hpp index ff7b921..6dd3452 100644 --- a/include/dx2/beam.hpp +++ b/include/dx2/beam.hpp @@ -62,6 +62,7 @@ class MonochromaticBeam : public BeamBase { void set_wavelength(double wavelength); Vector3d get_s0() const; void set_s0(Vector3d const s0); + void update(const MonochromaticBeam& beam); protected: double wavelength_{0.0}; @@ -101,6 +102,7 @@ class PolychromaticBeam : public BeamBase { json to_json() const; // overload for default std::array get_wavelength_range() const; void set_wavelength_range(std::array wavelength_range); + void update(const PolychromaticBeam& beam); protected: std::array wavelength_range_{{0.0, 0.0}}; diff --git a/include/dx2/experiment.hpp b/include/dx2/experiment.hpp index d1ca4a7..059b416 100644 --- a/include/dx2/experiment.hpp +++ b/include/dx2/experiment.hpp @@ -12,16 +12,32 @@ using Eigen::Vector3d; using json = nlohmann::json; +class ExperimentList; // forward declaration + class Experiment { + friend class ExperimentList; public: struct Models { + Models(std::shared_ptr beam, + std::shared_ptr detector, + std::shared_ptr scan, + std::shared_ptr goniometer, + std::shared_ptr imagesequence, + std::shared_ptr crystal = nullptr) // Optional argument + : beam(std::move(beam)), + detector(std::move(detector)), + scan(std::move(scan)), + goniometer(std::move(goniometer)), + imagesequence(std::move(imagesequence)), + crystal(std::move(crystal)) {} + std::shared_ptr beam; std::shared_ptr detector; std::shared_ptr scan; std::shared_ptr goniometer; std::shared_ptr imagesequence; - std::shared_ptr crystal = nullptr; // Optional models default to null + std::shared_ptr crystal; }; explicit Experiment(Models models, std::string identifier={}); diff --git a/include/dx2/experiment_list.hpp b/include/dx2/experiment_list.hpp new file mode 100644 index 0000000..a4cc896 --- /dev/null +++ b/include/dx2/experiment_list.hpp @@ -0,0 +1,31 @@ +#pragma once +#include +#include + +using json = nlohmann::json; + +class ExperimentList { +public: + ExperimentList() = default; + ExperimentList(const json &experiment_data); + json to_json() const; + + std::size_t size() const { return _experiments.size(); } + + Experiment& operator[](std::size_t i) { return _experiments[i]; } + + const Experiment& operator[](std::size_t i) const { return _experiments[i]; } + + auto begin() { return _experiments.begin(); } + auto end() { return _experiments.end(); } + + auto begin() const { return _experiments.begin(); } + auto end() const { return _experiments.end(); } + + ExperimentList select(const std::vector& identifiers) const; // Select on identifiers + + void append(Experiment experiment); + +private: + std::vector _experiments; +}; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index fd33832..985a2ab 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -34,6 +34,9 @@ target_link_libraries(test_read_h5_array GTest::gtest_main dx2 hdf5::hdf5) add_executable(test_reflection_table test_reflection_table.cxx) target_link_libraries(test_reflection_table GTest::gtest_main dx2) +add_executable(test_experiment test_experiment.cxx) +target_link_libraries(test_experiment GTest::gtest_main dx2) + set(DX2_TESTS test_crystal test_beam @@ -45,6 +48,7 @@ set(DX2_TESTS test_write_h5_array test_read_h5_array test_reflection_table + test_experiment ) # Tests that open the same HDF5 files on disk. HDF5 locks a file opened for diff --git a/tests/test_experiment.cxx b/tests/test_experiment.cxx new file mode 100644 index 0000000..fa1bea6 --- /dev/null +++ b/tests/test_experiment.cxx @@ -0,0 +1,57 @@ +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +TEST(ModelTests, ExperimentTest) { + + Experiment::Models models( + std::make_shared(), + std::make_shared(), + std::make_shared(), + std::make_shared(), + std::make_shared()); + + Experiment expt(models); + + // Required models exist + (void)expt.beam(); + (void)expt.detector(); + (void)expt.scan(); + (void)expt.goniometer(); + (void)expt.imagesequence(); + + // Crystal initially absent + assert(!expt.has_crystal()); + + bool threw = false; + try { + (void)expt.crystal(); + } catch (const std::runtime_error&) { + threw = true; + } + assert(threw); + + // Attach crystal + auto crystal = std::make_shared(); + expt.set_crystal(crystal); + + assert(expt.has_crystal()); + + // Shared crystal + Experiment expt2(models); + expt2.set_crystal(crystal); + + assert(&expt.crystal() == &expt2.crystal()); + + // Identifier generated automatically + assert(!expt.identifier().empty()); + +} \ No newline at end of file From a323889953d4344054fbfc4c60c953281ad735f9 Mon Sep 17 00:00:00 2001 From: James Beilsten-Edmands <30625594+jbeilstenedmands@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:36:54 +0000 Subject: [PATCH 5/7] remove experiment serialization to/from json --- dx2/experiment.cxx | 95 +------------------------------------- include/dx2/experiment.hpp | 15 +++--- 2 files changed, 8 insertions(+), 102 deletions(-) diff --git a/dx2/experiment.cxx b/dx2/experiment.cxx index d500582..d5ef127 100644 --- a/dx2/experiment.cxx +++ b/dx2/experiment.cxx @@ -8,95 +8,6 @@ Experiment::Experiment(Models models, std::string identifier) } } -Experiment::Experiment(const json &experiment_data) - : _models( - std::make_shared( - beam_io::from_json(experiment_data["beam"][0])), - std::make_shared( - Detector(experiment_data["detector"][0])), - std::make_shared( - Scan(experiment_data["scan"][0])), - std::make_shared( - Goniometer(experiment_data["goniometer"][0])), - std::make_shared( - ImageSequence(experiment_data["imageset"][0]))) -{ - _identifier = experiment_data["experiment"][0]["identifier"]; - - if (experiment_data.contains("crystal") && - experiment_data["crystal"].is_array() && - !experiment_data["crystal"].empty()) { - - _models.crystal = - std::make_shared( - experiment_data["crystal"][0]); - } -} - - -/*Experiment::Experiment(const json &experiment_data) { - std::string identifier = experiment_data["experiment"][0]["identifier"]; - json beam_data = experiment_data["beam"][0]; - json scan_data = experiment_data["scan"][0]; - Scan scan(scan_data); - json gonio_data = experiment_data["goniometer"][0]; - Goniometer gonio(gonio_data); - json detector_data = experiment_data["detector"][0]; - Detector detector(detector_data); - this->_identifier = identifier; - _models.beam = std::make_shared(beam_io::from_json(beam_data)); - _models.scan = std::make_shared(scan); - _models.goniometer = std::make_shared(gonio); - _models.detector = std::make_shared(detector); - // Save the imageset json to propagate when saving to file. - json imagesequence_data = experiment_data["imageset"][0]; - ImageSequence imagesequence(imagesequence_data); - _models.imagesequence = std::make_shared(imagesequence); - if (experiment_data.contains("crystal") && - experiment_data["crystal"].is_array() && - !experiment_data["crystal"].empty()) { - const json &crystal_data = experiment_data["crystal"][0]; - Crystal crystal(crystal_data); - set_crystal(crystal); - } -}*/ - -json Experiment::to_json() const { - // save this experiment as an example experiment list - json elist_out; // a list of potentially multiple experiments - elist_out["__id__"] = "ExperimentList"; - json expt_out; // our single experiment - expt_out["__id__"] = "Experiment"; - expt_out["identifier"] = _identifier; - expt_out["beam"] = - 0; // the indices of the models that will correspond to our experiment - expt_out["detector"] = 0; - expt_out["goniometer"] = 0; - expt_out["scan"] = 0; - expt_out["imageset"] = 0; - - // add the the actual models - elist_out["scan"] = std::array{_models.scan->to_json()}; - elist_out["goniometer"] = std::array{_models.goniometer->to_json()}; - elist_out["beam"] = std::array{beam_io::to_json(*_models.beam)}; - elist_out["detector"] = std::array{_models.detector->to_json()}; - elist_out["imageset"] = std::array{_models.imagesequence->to_json()}; - - if (has_crystal()) { - expt_out["crystal"] = 0; - elist_out["crystal"] = std::array{_models.crystal->to_json()}; - } else { - elist_out["crystal"] = std::array{}; - } - - elist_out["experiment"] = std::array{expt_out}; - return elist_out; -} - -void Experiment::set_crystal(Crystal crystal) { _models.crystal = std::make_shared(std::move(crystal)); } - -void Experiment::set_crystal(std::shared_ptr crystal){_models.crystal = std::move(crystal);} - Crystal& Experiment::crystal() { if (!_models.crystal) { throw std::runtime_error("Experiment has no crystal"); @@ -110,10 +21,8 @@ const Crystal& Experiment::crystal() const { return *_models.crystal; } -const std::string &Experiment::identifier() const { return _identifier; } +void Experiment::set_crystal(Crystal crystal) { _models.crystal = std::make_shared(std::move(crystal)); } -void Experiment::set_identifier(std::string identifier) { - _identifier = identifier; -} +void Experiment::set_crystal(std::shared_ptr crystal){_models.crystal = std::move(crystal);} void Experiment::generate_identifier() { _identifier = ersatz_uuid4(); } diff --git a/include/dx2/experiment.hpp b/include/dx2/experiment.hpp index 059b416..f838b84 100644 --- a/include/dx2/experiment.hpp +++ b/include/dx2/experiment.hpp @@ -42,10 +42,6 @@ class Experiment { explicit Experiment(Models models, std::string identifier={}); - Experiment(const json &experiment_data); - - json to_json() const; - // These models should always exist as they are defined by the experiment metadata Beam& beam() { return *_models.beam; } @@ -67,12 +63,13 @@ class Experiment { Crystal& crystal(); const Crystal& crystal() const; bool has_crystal() const { return static_cast(_models.crystal);} - - const std::string &identifier() const; - - void set_crystal(std::shared_ptr crystal); + void set_crystal(Crystal crystal); - void set_identifier(std::string identifier); + void set_crystal(std::shared_ptr crystal); + + const std::string& identifier() const { return _identifier; } + void set_identifier(std::string identifier) { _identifier = identifier; } + void generate_identifier(); private: From fd685d95da530998c4a65aa1954505be66a28a92 Mon Sep 17 00:00:00 2001 From: James Beilsten-Edmands <30625594+jbeilstenedmands@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:57:57 +0000 Subject: [PATCH 6/7] add select on index --- dx2/experiment_list.cxx | 17 +++++++++++++++++ include/dx2/experiment_list.hpp | 2 ++ 2 files changed, 19 insertions(+) diff --git a/dx2/experiment_list.cxx b/dx2/experiment_list.cxx index fc51ed9..93472e2 100644 --- a/dx2/experiment_list.cxx +++ b/dx2/experiment_list.cxx @@ -233,6 +233,23 @@ ExperimentList ExperimentList::select( return result; } +ExperimentList ExperimentList::select( + const std::vector& indices) const { + + ExperimentList result; + + for (std::size_t i : indices) { + if (i >= _experiments.size()) { + throw std::out_of_range( + "Experiment index out of range"); + } + + result.append(_experiments[i]); + } + + return result; +} + void ExperimentList::append(Experiment experiment) { _experiments.push_back(std::move(experiment)); } diff --git a/include/dx2/experiment_list.hpp b/include/dx2/experiment_list.hpp index a4cc896..8e8601c 100644 --- a/include/dx2/experiment_list.hpp +++ b/include/dx2/experiment_list.hpp @@ -24,6 +24,8 @@ class ExperimentList { ExperimentList select(const std::vector& identifiers) const; // Select on identifiers + ExperimentList select(const std::vector& indices) const; // Select on indices + void append(Experiment experiment); private: From 6dc61219111ab1f12274747e3a4da3b8235a3f0f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 13 Nov 2026 16:07:07 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- dx2/beam.cxx | 4 +- dx2/experiment.cxx | 22 +++-- dx2/experiment_list.cxx | 153 ++++++++++++-------------------- include/dx2/beam.hpp | 4 +- include/dx2/experiment.hpp | 61 ++++++------- include/dx2/experiment_list.hpp | 10 ++- tests/test_experiment.cxx | 11 +-- 7 files changed, 113 insertions(+), 152 deletions(-) diff --git a/dx2/beam.cxx b/dx2/beam.cxx index 041267b..f94a58b 100644 --- a/dx2/beam.cxx +++ b/dx2/beam.cxx @@ -138,7 +138,7 @@ json MonochromaticBeam::to_json() const { return to_json("x-ray"); } -void MonochromaticBeam::update(const MonochromaticBeam& beam){ +void MonochromaticBeam::update(const MonochromaticBeam &beam) { Vector3d s0 = beam.get_s0(); polarization_normal_ = beam.get_polarization_normal(); polarization_fraction_ = beam.get_polarization_fraction(); @@ -229,7 +229,7 @@ void PolychromaticBeam::set_wavelength_range( wavelength_range_ = wavelength_range; } -void PolychromaticBeam::update(const PolychromaticBeam& beam){ +void PolychromaticBeam::update(const PolychromaticBeam &beam) { wavelength_range_ = beam.get_wavelength_range(); polarization_normal_ = beam.get_polarization_normal(); polarization_fraction_ = beam.get_polarization_fraction(); diff --git a/dx2/experiment.cxx b/dx2/experiment.cxx index d5ef127..7b05295 100644 --- a/dx2/experiment.cxx +++ b/dx2/experiment.cxx @@ -2,27 +2,31 @@ #include Experiment::Experiment(Models models, std::string identifier) - : _models(std::move(models)), _identifier(std::move(identifier)){ - if (_identifier.empty()) { - generate_identifier(); - } - } + : _models(std::move(models)), _identifier(std::move(identifier)) { + if (_identifier.empty()) { + generate_identifier(); + } +} -Crystal& Experiment::crystal() { +Crystal &Experiment::crystal() { if (!_models.crystal) { throw std::runtime_error("Experiment has no crystal"); } return *_models.crystal; } -const Crystal& Experiment::crystal() const { +const Crystal &Experiment::crystal() const { if (!_models.crystal) { throw std::runtime_error("Experiment has no crystal"); } return *_models.crystal; } -void Experiment::set_crystal(Crystal crystal) { _models.crystal = std::make_shared(std::move(crystal)); } +void Experiment::set_crystal(Crystal crystal) { + _models.crystal = std::make_shared(std::move(crystal)); +} -void Experiment::set_crystal(std::shared_ptr crystal){_models.crystal = std::move(crystal);} +void Experiment::set_crystal(std::shared_ptr crystal) { + _models.crystal = std::move(crystal); +} void Experiment::generate_identifier() { _identifier = ersatz_uuid4(); } diff --git a/dx2/experiment_list.cxx b/dx2/experiment_list.cxx index 93472e2..aa9d286 100644 --- a/dx2/experiment_list.cxx +++ b/dx2/experiment_list.cxx @@ -1,82 +1,68 @@ -#include -#include #include +#include #include #include +#include #include #include #include #include #include - -ExperimentList::ExperimentList(const json& experiment_data) { +ExperimentList::ExperimentList(const json &experiment_data) { // Phase 1: deserialize all models std::vector> beams; - for (const auto& beam_json : experiment_data["beam"]) { - beams.push_back( - std::make_shared( - beam_io::from_json(beam_json))); + for (const auto &beam_json : experiment_data["beam"]) { + beams.push_back(std::make_shared(beam_io::from_json(beam_json))); } std::vector> detectors; - for (const auto& detector_json : experiment_data["detector"]) { - detectors.push_back( - std::make_shared(Detector(detector_json))); + for (const auto &detector_json : experiment_data["detector"]) { + detectors.push_back(std::make_shared(Detector(detector_json))); } std::vector> scans; - for (const auto& scan_json : experiment_data["scan"]) { - scans.push_back( - std::make_shared(Scan(scan_json))); + for (const auto &scan_json : experiment_data["scan"]) { + scans.push_back(std::make_shared(Scan(scan_json))); } std::vector> goniometers; - for (const auto& gonio_json : experiment_data["goniometer"]) { - goniometers.push_back( - std::make_shared(Goniometer(gonio_json))); + for (const auto &gonio_json : experiment_data["goniometer"]) { + goniometers.push_back(std::make_shared(Goniometer(gonio_json))); } std::vector> imagesets; - for (const auto& imageset_json : experiment_data["imageset"]) { + for (const auto &imageset_json : experiment_data["imageset"]) { imagesets.push_back( std::make_shared(ImageSequence(imageset_json))); } std::vector> crystals; if (experiment_data.contains("crystal")) { - for (const auto& crystal_json : experiment_data["crystal"]) { - crystals.push_back( - std::make_shared(Crystal(crystal_json))); + for (const auto &crystal_json : experiment_data["crystal"]) { + crystals.push_back(std::make_shared(Crystal(crystal_json))); } } // Phase 2: construct experiments - for (const auto& expt_json : experiment_data["experiment"]) { + for (const auto &expt_json : experiment_data["experiment"]) { Experiment::Models models{ - beams.at(expt_json["beam"]), - detectors.at(expt_json["detector"]), - scans.at(expt_json["scan"]), - goniometers.at(expt_json["goniometer"]), - imagesets.at(expt_json["imageset"]) - }; + beams.at(expt_json["beam"]), detectors.at(expt_json["detector"]), + scans.at(expt_json["scan"]), goniometers.at(expt_json["goniometer"]), + imagesets.at(expt_json["imageset"])}; if (expt_json.contains("crystal")) { - models.crystal = - crystals.at(expt_json["crystal"]); + models.crystal = crystals.at(expt_json["crystal"]); } - _experiments.emplace_back( - std::move(models), - expt_json["identifier"]); + _experiments.emplace_back(std::move(models), expt_json["identifier"]); } } - json ExperimentList::to_json() const { json result; @@ -111,8 +97,7 @@ json ExperimentList::to_json() const { beam_indices.emplace(beam_ptr, beam_indices.size()); if (beam_inserted) { - result["beam"].push_back( - beam_io::to_json(*expt._models.beam)); + result["beam"].push_back(beam_io::to_json(*expt._models.beam)); } expt_json["beam"] = beam_it->second; @@ -122,13 +107,10 @@ json ExperimentList::to_json() const { // const Detector *detector_ptr = expt._models.detector.get(); auto [detector_it, detector_inserted] = - detector_indices.emplace( - detector_ptr, - detector_indices.size()); + detector_indices.emplace(detector_ptr, detector_indices.size()); if (detector_inserted) { - result["detector"].push_back( - expt._models.detector->to_json()); + result["detector"].push_back(expt._models.detector->to_json()); } expt_json["detector"] = detector_it->second; @@ -138,13 +120,10 @@ json ExperimentList::to_json() const { // const Scan *scan_ptr = expt._models.scan.get(); auto [scan_it, scan_inserted] = - scan_indices.emplace( - scan_ptr, - scan_indices.size()); + scan_indices.emplace(scan_ptr, scan_indices.size()); if (scan_inserted) { - result["scan"].push_back( - expt._models.scan->to_json()); + result["scan"].push_back(expt._models.scan->to_json()); } expt_json["scan"] = scan_it->second; @@ -152,17 +131,13 @@ json ExperimentList::to_json() const { // // Goniometer // - const Goniometer *gonio_ptr = - expt._models.goniometer.get(); + const Goniometer *gonio_ptr = expt._models.goniometer.get(); auto [gonio_it, gonio_inserted] = - goniometer_indices.emplace( - gonio_ptr, - goniometer_indices.size()); + goniometer_indices.emplace(gonio_ptr, goniometer_indices.size()); if (gonio_inserted) { - result["goniometer"].push_back( - expt._models.goniometer->to_json()); + result["goniometer"].push_back(expt._models.goniometer->to_json()); } expt_json["goniometer"] = gonio_it->second; @@ -170,17 +145,13 @@ json ExperimentList::to_json() const { // // Imageset // - const ImageSequence *imageset_ptr = - expt._models.imagesequence.get(); + const ImageSequence *imageset_ptr = expt._models.imagesequence.get(); auto [imageset_it, imageset_inserted] = - imageset_indices.emplace( - imageset_ptr, - imageset_indices.size()); + imageset_indices.emplace(imageset_ptr, imageset_indices.size()); if (imageset_inserted) { - result["imageset"].push_back( - expt._models.imagesequence->to_json()); + result["imageset"].push_back(expt._models.imagesequence->to_json()); } expt_json["imageset"] = imageset_it->second; @@ -190,68 +161,58 @@ json ExperimentList::to_json() const { // if (expt.has_crystal()) { - const Crystal *crystal_ptr = - expt._models.crystal.get(); + const Crystal *crystal_ptr = expt._models.crystal.get(); auto [crystal_it, crystal_inserted] = - crystal_indices.emplace( - crystal_ptr, - crystal_indices.size()); + crystal_indices.emplace(crystal_ptr, crystal_indices.size()); if (crystal_inserted) { - result["crystal"].push_back( - expt._models.crystal->to_json()); + result["crystal"].push_back(expt._models.crystal->to_json()); } expt_json["crystal"] = crystal_it->second; } - result["experiment"].push_back( - std::move(expt_json)); + result["experiment"].push_back(std::move(expt_json)); } return result; } +ExperimentList +ExperimentList::select(const std::vector &identifiers) const { -ExperimentList ExperimentList::select( - const std::vector& identifiers) const { + // Implement a simple linear scan and lookup for now. + std::unordered_set wanted(identifiers.begin(), + identifiers.end()); - // Implement a simple linear scan and lookup for now. - std::unordered_set wanted( - identifiers.begin(), - identifiers.end()); + ExperimentList result; - ExperimentList result; - - for (const auto& expt : _experiments) { - if (wanted.contains(expt.identifier())) { - result.append(expt); - } + for (const auto &expt : _experiments) { + if (wanted.contains(expt.identifier())) { + result.append(expt); } + } - return result; + return result; } -ExperimentList ExperimentList::select( - const std::vector& indices) const { +ExperimentList +ExperimentList::select(const std::vector &indices) const { - ExperimentList result; + ExperimentList result; - for (std::size_t i : indices) { - if (i >= _experiments.size()) { - throw std::out_of_range( - "Experiment index out of range"); - } - - result.append(_experiments[i]); + for (std::size_t i : indices) { + if (i >= _experiments.size()) { + throw std::out_of_range("Experiment index out of range"); } - return result; + result.append(_experiments[i]); + } + + return result; } void ExperimentList::append(Experiment experiment) { - _experiments.push_back(std::move(experiment)); + _experiments.push_back(std::move(experiment)); } - - diff --git a/include/dx2/beam.hpp b/include/dx2/beam.hpp index 6dd3452..4446e0b 100644 --- a/include/dx2/beam.hpp +++ b/include/dx2/beam.hpp @@ -62,7 +62,7 @@ class MonochromaticBeam : public BeamBase { void set_wavelength(double wavelength); Vector3d get_s0() const; void set_s0(Vector3d const s0); - void update(const MonochromaticBeam& beam); + void update(const MonochromaticBeam &beam); protected: double wavelength_{0.0}; @@ -102,7 +102,7 @@ class PolychromaticBeam : public BeamBase { json to_json() const; // overload for default std::array get_wavelength_range() const; void set_wavelength_range(std::array wavelength_range); - void update(const PolychromaticBeam& beam); + void update(const PolychromaticBeam &beam); protected: std::array wavelength_range_{{0.0, 0.0}}; diff --git a/include/dx2/experiment.hpp b/include/dx2/experiment.hpp index f838b84..da6bd81 100644 --- a/include/dx2/experiment.hpp +++ b/include/dx2/experiment.hpp @@ -16,21 +16,17 @@ class ExperimentList; // forward declaration class Experiment { friend class ExperimentList; -public: +public: struct Models { - Models(std::shared_ptr beam, - std::shared_ptr detector, - std::shared_ptr scan, - std::shared_ptr goniometer, - std::shared_ptr imagesequence, - std::shared_ptr crystal = nullptr) // Optional argument - : beam(std::move(beam)), - detector(std::move(detector)), - scan(std::move(scan)), - goniometer(std::move(goniometer)), - imagesequence(std::move(imagesequence)), - crystal(std::move(crystal)) {} + Models(std::shared_ptr beam, std::shared_ptr detector, + std::shared_ptr scan, std::shared_ptr goniometer, + std::shared_ptr imagesequence, + std::shared_ptr crystal = nullptr) // Optional argument + : beam(std::move(beam)), detector(std::move(detector)), + scan(std::move(scan)), goniometer(std::move(goniometer)), + imagesequence(std::move(imagesequence)), crystal(std::move(crystal)) { + } std::shared_ptr beam; std::shared_ptr detector; @@ -40,36 +36,37 @@ class Experiment { std::shared_ptr crystal; }; - explicit Experiment(Models models, std::string identifier={}); + explicit Experiment(Models models, std::string identifier = {}); - // These models should always exist as they are defined by the experiment metadata + // These models should always exist as they are defined by the experiment + // metadata - Beam& beam() { return *_models.beam; } - const Beam& beam() const { return *_models.beam; } + Beam &beam() { return *_models.beam; } + const Beam &beam() const { return *_models.beam; } - Detector& detector() { return *_models.detector; } - const Detector& detector() const { return *_models.detector; } + Detector &detector() { return *_models.detector; } + const Detector &detector() const { return *_models.detector; } - Scan& scan() { return *_models.scan; } - const Scan& scan() const { return *_models.scan; } + Scan &scan() { return *_models.scan; } + const Scan &scan() const { return *_models.scan; } - Goniometer& goniometer() { return *_models.goniometer; } - const Goniometer& goniometer() const { return *_models.goniometer; } + Goniometer &goniometer() { return *_models.goniometer; } + const Goniometer &goniometer() const { return *_models.goniometer; } - ImageSequence& imagesequence() { return *_models.imagesequence; } - const ImageSequence& imagesequence() const { return *_models.imagesequence; } + ImageSequence &imagesequence() { return *_models.imagesequence; } + const ImageSequence &imagesequence() const { return *_models.imagesequence; } // These models are discovered during processing and so are optional - Crystal& crystal(); - const Crystal& crystal() const; - bool has_crystal() const { return static_cast(_models.crystal);} - + Crystal &crystal(); + const Crystal &crystal() const; + bool has_crystal() const { return static_cast(_models.crystal); } + void set_crystal(Crystal crystal); void set_crystal(std::shared_ptr crystal); - - const std::string& identifier() const { return _identifier; } + + const std::string &identifier() const { return _identifier; } void set_identifier(std::string identifier) { _identifier = identifier; } - + void generate_identifier(); private: diff --git a/include/dx2/experiment_list.hpp b/include/dx2/experiment_list.hpp index 8e8601c..f24cac5 100644 --- a/include/dx2/experiment_list.hpp +++ b/include/dx2/experiment_list.hpp @@ -12,9 +12,9 @@ class ExperimentList { std::size_t size() const { return _experiments.size(); } - Experiment& operator[](std::size_t i) { return _experiments[i]; } + Experiment &operator[](std::size_t i) { return _experiments[i]; } - const Experiment& operator[](std::size_t i) const { return _experiments[i]; } + const Experiment &operator[](std::size_t i) const { return _experiments[i]; } auto begin() { return _experiments.begin(); } auto end() { return _experiments.end(); } @@ -22,9 +22,11 @@ class ExperimentList { auto begin() const { return _experiments.begin(); } auto end() const { return _experiments.end(); } - ExperimentList select(const std::vector& identifiers) const; // Select on identifiers + ExperimentList select(const std::vector &identifiers) + const; // Select on identifiers - ExperimentList select(const std::vector& indices) const; // Select on indices + ExperimentList + select(const std::vector &indices) const; // Select on indices void append(Experiment experiment); diff --git a/tests/test_experiment.cxx b/tests/test_experiment.cxx index fa1bea6..07869b8 100644 --- a/tests/test_experiment.cxx +++ b/tests/test_experiment.cxx @@ -13,11 +13,9 @@ TEST(ModelTests, ExperimentTest) { Experiment::Models models( - std::make_shared(), - std::make_shared(), - std::make_shared(), - std::make_shared(), - std::make_shared()); + std::make_shared(), std::make_shared(), + std::make_shared(), std::make_shared(), + std::make_shared()); Experiment expt(models); @@ -34,7 +32,7 @@ TEST(ModelTests, ExperimentTest) { bool threw = false; try { (void)expt.crystal(); - } catch (const std::runtime_error&) { + } catch (const std::runtime_error &) { threw = true; } assert(threw); @@ -53,5 +51,4 @@ TEST(ModelTests, ExperimentTest) { // Identifier generated automatically assert(!expt.identifier().empty()); - } \ No newline at end of file