Skip to content

Commit

Permalink
WIP: Add ReflectionTable class to allow efficient data access and fil…
Browse files Browse the repository at this point in the history
…teringwq
  • Loading branch information
dimitrivlachos committed Feb 17, 2025
1 parent b252722 commit f414558
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions include/dx2/reflection.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#ifndef DX2_REFLECTION_HPP
#define DX2_REFLECTION_HPP

#include "dx2/h5/h5read_processed.hpp"
#include "dx2/tensor.hpp"
#include <functional>
#include <vector>

class ReflectionTable {
public:
/**
* @brief Enum representing the columns in the reflection table.
*/
enum class Column { X = 0, Y = 1, Z = 2 };

/**
* @brief Default constructor
*/
ReflectionTable() = default;

/**
* @brief Constructs a ReflectionTable from an HDF5 file.
*
* @param h5_filepath Path to the HDF5 file.
*/
ReflectionTable(const std::string &h5_filepath)
: h5_filepath_(h5_filepath),
table_name_("/dials/processing/group_0/xyzobs.px.value") {}

/**
* @brief Selects reflections based on a predicate applied to a specified
* column.
*
* @param column The column to apply the predicate on (Column::X, Column::Y,
* Column::Z).
* @param predicate A function that returns `true` for rows to be kept.
*/
void select(Column column, const std::function<bool(double)> &predicate) {
size_t column_index = static_cast<size_t>(column);

if (data_.size() == 0) {
load_data();
}
data_.remove_if(column_index, predicate);
}

/**
* @brief Retrieves a reference to a column's data.
*
* @param column The column to retrieve (Column::X, Column::Y, Column::Z).
* @return A vector containing the column's data.
*/
std::vector<double> column(Column column) const {
size_t col_index = static_cast<size_t>(column);
std::vector<double> col_values(data_.shape()[0]);

for (size_t row = 0; row < data_.shape()[0]; ++row) {
col_values[row] = data_(row, col_index);
}
return col_values;
}

private:
Tensor<double> data_; // Internal tensor storing reflection data.
std::string table_name_; // Name of the HDF5 table.
std::string h5_filepath_; // Path to the HDF5 file.

/**
* @brief Loads data from an HDF5 file into the tensor.
*/
void load_data() {
// Read the data from the HDF5 file
H5Data<double> h5_data;
read_array_from_h5_file(h5_filepath_, table_name_, h5_data);

if (h5_data.shape.size() != 2) {
throw std::runtime_error("Error: Expected 2D data for reflection table.");
}
if (h5_data.shape[1] != 3) {
throw std::runtime_error(
"Error: Unexpected number of columns in reflection table.");
}

// Create the tensor and store the entire dataset
data_ = Tensor<double>(h5_data.shape, std::move(h5_data.data),
Layout::RowMajor);
}
};

#endif // DX2_REFLECTION_HPP

0 comments on commit f414558

Please sign in to comment.