Skip to content
Draft
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
1 change: 1 addition & 0 deletions dx2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_library(
detector_attenuations.cxx
imagesequence.cxx
experiment.cxx
experiment_list.cxx
beam_ops.cxx
beam_io.cxx
)
Expand Down
15 changes: 15 additions & 0 deletions dx2/beam.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
102 changes: 17 additions & 85 deletions dx2/experiment.cxx
Original file line number Diff line number Diff line change
@@ -1,100 +1,32 @@
#include <dx2/beam_io.hpp>
#include <dx2/experiment.hpp>

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];
Goniometer gonio(gonio_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);
// Save the imageset json to propagate when saving to file.
json imagesequence_data = experiment_data["imageset"][0];
ImageSequence imagesequence(imagesequence_data);
this->_imagesequence = 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;
Experiment::Experiment(Models models, std::string identifier)
: _models(std::move(models)), _identifier(std::move(identifier)) {
if (_identifier.empty()) {
generate_identifier();
}
}

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<json, 1>{_scan.to_json()};
elist_out["goniometer"] = std::array<json, 1>{_goniometer.to_json()};
elist_out["beam"] = std::array<json, 1>{beam_io::to_json(_beam)};
elist_out["detector"] = std::array<json, 1>{_detector.to_json()};
elist_out["imageset"] = std::array<json, 1>{_imagesequence.to_json()};

if (_crystal.get_U_matrix().determinant()) {
expt_out["crystal"] = 0;
elist_out["crystal"] = std::array<json, 1>{_crystal.to_json()};
} else {
elist_out["crystal"] = std::array<json, 0>{};
Crystal &Experiment::crystal() {
if (!_models.crystal) {
throw std::runtime_error("Experiment has no crystal");
}

elist_out["experiment"] = std::array<json, 1>{expt_out};
return elist_out;
return *_models.crystal;
}

Scan &Experiment::scan() { return _scan; }

const Goniometer &Experiment::goniometer() const { return _goniometer; }

Detector &Experiment::detector() { return _detector; }

Crystal &Experiment::crystal() { return _crystal; }

ImageSequence &Experiment::imagesequence() { return _imagesequence; }

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;
const Crystal &Experiment::crystal() const {
if (!_models.crystal) {
throw std::runtime_error("Experiment has no crystal");
}
return *_models.crystal;
}

void Experiment::set_imagesequence(ImageSequence imagesequence) {
_imagesequence = imagesequence;
void Experiment::set_crystal(Crystal crystal) {
_models.crystal = std::make_shared<Crystal>(std::move(crystal));
}

const std::string &Experiment::identifier() const { return _identifier; }

void Experiment::set_identifier(std::string identifier) {
_identifier = identifier;
void Experiment::set_crystal(std::shared_ptr<Crystal> crystal) {
_models.crystal = std::move(crystal);
}

void Experiment::generate_identifier() { _identifier = ersatz_uuid4(); }
218 changes: 218 additions & 0 deletions dx2/experiment_list.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
#include <dx2/beam.hpp>
#include <dx2/beam_io.hpp>
#include <dx2/crystal.hpp>
#include <dx2/detector.hpp>
#include <dx2/experiment_list.hpp>
#include <dx2/goniometer.hpp>
#include <dx2/imagesequence.hpp>
#include <dx2/scan.hpp>
#include <dx2/utils.hpp>
#include <unordered_set>

ExperimentList::ExperimentList(const json &experiment_data) {

// Phase 1: deserialize all models

std::vector<std::shared_ptr<Beam>> beams;
for (const auto &beam_json : experiment_data["beam"]) {
beams.push_back(std::make_shared<Beam>(beam_io::from_json(beam_json)));
}

std::vector<std::shared_ptr<Detector>> detectors;
for (const auto &detector_json : experiment_data["detector"]) {
detectors.push_back(std::make_shared<Detector>(Detector(detector_json)));
}

std::vector<std::shared_ptr<Scan>> scans;
for (const auto &scan_json : experiment_data["scan"]) {
scans.push_back(std::make_shared<Scan>(Scan(scan_json)));
}

std::vector<std::shared_ptr<Goniometer>> goniometers;
for (const auto &gonio_json : experiment_data["goniometer"]) {
goniometers.push_back(std::make_shared<Goniometer>(Goniometer(gonio_json)));
}

std::vector<std::shared_ptr<ImageSequence>> imagesets;
for (const auto &imageset_json : experiment_data["imageset"]) {
imagesets.push_back(
std::make_shared<ImageSequence>(ImageSequence(imageset_json)));
}

std::vector<std::shared_ptr<Crystal>> crystals;
if (experiment_data.contains("crystal")) {
for (const auto &crystal_json : experiment_data["crystal"]) {
crystals.push_back(std::make_shared<Crystal>(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<const Beam *, std::size_t> beam_indices;
std::unordered_map<const Detector *, std::size_t> detector_indices;
std::unordered_map<const Scan *, std::size_t> scan_indices;
std::unordered_map<const Goniometer *, std::size_t> goniometer_indices;
std::unordered_map<const ImageSequence *, std::size_t> imageset_indices;
std::unordered_map<const Crystal *, std::size_t> 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<std::string> &identifiers) const {

// Implement a simple linear scan and lookup for now.
std::unordered_set<std::string> wanted(identifiers.begin(),
identifiers.end());

ExperimentList result;

for (const auto &expt : _experiments) {
if (wanted.contains(expt.identifier())) {
result.append(expt);
}
}

return result;
}

ExperimentList
ExperimentList::select(const std::vector<std::size_t> &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));
}
2 changes: 2 additions & 0 deletions include/dx2/beam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -101,6 +102,7 @@ class PolychromaticBeam : public BeamBase {
json to_json() const; // overload for default
std::array<double, 2> get_wavelength_range() const;
void set_wavelength_range(std::array<double, 2> wavelength_range);
void update(const PolychromaticBeam &beam);

protected:
std::array<double, 2> wavelength_range_{{0.0, 0.0}};
Expand Down
Loading
Loading