moved parts of ClusterFile implementation into declaration
All checks were successful
Build on RHEL9 / buildh (push) Successful in 1m55s

This commit is contained in:
Mazzoleni Alice Francesca 2025-04-15 15:15:34 +02:00
parent d4050ec557
commit acd9d5d487

View File

@ -60,80 +60,8 @@ class ClusterFile {
* @throws std::runtime_error if the file could not be opened * @throws std::runtime_error if the file could not be opened
*/ */
ClusterFile(const std::filesystem::path &fname, size_t chunk_size = 1000, ClusterFile(const std::filesystem::path &fname, size_t chunk_size = 1000,
const std::string &mode = "r"); const std::string &mode = "r")
~ClusterFile();
/**
* @brief Read n_clusters clusters from the file discarding frame numbers.
* If EOF is reached the returned vector will have less than n_clusters
* clusters
*/
ClusterVector<ClusterType> read_clusters(size_t n_clusters);
/**
* @brief Read a single frame from the file and return the clusters. The
* cluster vector will have the frame number set.
* @throws std::runtime_error if the file is not opened for reading or the
* file pointer not at the beginning of a frame
*/
ClusterVector<ClusterType> read_frame();
void write_frame(const ClusterVector<ClusterType> &clusters);
/**
* @brief Return the chunk size
*/
size_t chunk_size() const { return m_chunk_size; }
/**
* @brief Set the region of interest to use when reading clusters. If set
* only clusters within the ROI will be read.
*/
void set_roi(ROI roi);
/**
* @brief Set the noise map to use when reading clusters. If set clusters
* below the noise level will be discarded. Selection criteria one of:
* Central pixel above noise, highest 2x2 sum above 2 * noise, total sum
* above 3 * noise.
*/
void set_noise_map(const NDView<int32_t, 2> noise_map);
/**
* @brief Set the gain map to use when reading clusters. If set the gain map
* will be applied to the clusters that pass ROI and noise_map selection.
*/
void set_gain_map(const NDView<double, 2> gain_map);
void set_gain_map(const GainMap &gain_map);
void set_gain_map(const GainMap &&gain_map);
/**
* @brief Close the file. If not closed the file will be closed in the
* destructor
*/
void close();
/** @brief Open the file in specific mode
*
*/
void open(const std::string &mode);
private:
ClusterVector<ClusterType> read_clusters_with_cut(size_t n_clusters);
ClusterVector<ClusterType> read_clusters_without_cut(size_t n_clusters);
ClusterVector<ClusterType> read_frame_with_cut();
ClusterVector<ClusterType> read_frame_without_cut();
bool is_selected(ClusterType &cl);
ClusterType read_one_cluster();
};
template <typename ClusterType, typename Enable>
ClusterFile<ClusterType, Enable>::ClusterFile(
const std::filesystem::path &fname, size_t chunk_size,
const std::string &mode)
: m_filename(fname.string()), m_chunk_size(chunk_size), m_mode(mode) { : m_filename(fname.string()), m_chunk_size(chunk_size), m_mode(mode) {
if (mode == "r") { if (mode == "r") {
@ -157,23 +85,108 @@ ClusterFile<ClusterType, Enable>::ClusterFile(
} else { } else {
throw std::runtime_error("Unsupported mode: " + mode); throw std::runtime_error("Unsupported mode: " + mode);
} }
} }
template <typename ClusterType, typename Enable> ~ClusterFile() { close(); }
ClusterFile<ClusterType, Enable>::~ClusterFile() {
close();
}
template <typename ClusterType, typename Enable> /**
void ClusterFile<ClusterType, Enable>::close() { * @brief Read n_clusters clusters from the file discarding
* frame numbers. If EOF is reached the returned vector will
* have less than n_clusters clusters
*/
ClusterVector<ClusterType> read_clusters(size_t n_clusters) {
if (m_mode != "r") {
throw std::runtime_error("File not opened for reading");
}
if (m_noise_map || m_roi) {
return read_clusters_with_cut(n_clusters);
} else {
return read_clusters_without_cut(n_clusters);
}
}
/**
* @brief Read a single frame from the file and return the
* clusters. The cluster vector will have the frame number
* set.
* @throws std::runtime_error if the file is not opened for
* reading or the file pointer not at the beginning of a
* frame
*/
ClusterVector<ClusterType> read_frame() {
if (m_mode != "r") {
throw std::runtime_error(LOCATION + "File not opened for reading");
}
if (m_noise_map || m_roi) {
return read_frame_with_cut();
} else {
return read_frame_without_cut();
}
}
void write_frame(const ClusterVector<ClusterType> &clusters) {
if (m_mode != "w" && m_mode != "a") {
throw std::runtime_error("File not opened for writing");
}
int32_t frame_number = clusters.frame_number();
fwrite(&frame_number, sizeof(frame_number), 1, fp);
uint32_t n_clusters = clusters.size();
fwrite(&n_clusters, sizeof(n_clusters), 1, fp);
fwrite(clusters.data(), clusters.item_size(), clusters.size(), fp);
}
/**
* @brief Return the chunk size
*/
size_t chunk_size() const { return m_chunk_size; }
/**
* @brief Set the region of interest to use when reading
* clusters. If set only clusters within the ROI will be
* read.
*/
void set_roi(ROI roi) { m_roi = roi; }
/**
* @brief Set the noise map to use when reading clusters. If
* set clusters below the noise level will be discarded.
* Selection criteria one of: Central pixel above noise,
* highest 2x2 sum above 2 * noise, total sum above 3 *
* noise.
*/
void set_noise_map(const NDView<int32_t, 2> noise_map) {
m_noise_map = NDArray<int32_t, 2>(noise_map);
}
/**
* @brief Set the gain map to use when reading clusters. If
* set the gain map will be applied to the clusters that
* pass ROI and noise_map selection.
*/
void set_gain_map(const NDView<double, 2> gain_map) {
m_gain_map = GainMap(gain_map);
}
void set_gain_map(const GainMap &gain_map) { m_gain_map = gain_map; }
void set_gain_map(const GainMap &&gain_map) { m_gain_map = gain_map; }
/**
* @brief Close the file. If not closed the file will be
* closed in the destructor
*/
void close() {
if (fp) { if (fp) {
fclose(fp); fclose(fp);
fp = nullptr; fp = nullptr;
} }
} }
template <typename ClusterType, typename Enable> /** @brief Open the file in specific mode
void ClusterFile<ClusterType, Enable>::open(const std::string &mode) { *
*/
void open(const std::string &mode) {
if (fp) { if (fp) {
close(); close();
} }
@ -202,60 +215,16 @@ void ClusterFile<ClusterType, Enable>::open(const std::string &mode) {
} else { } else {
throw std::runtime_error("Unsupported mode: " + mode); throw std::runtime_error("Unsupported mode: " + mode);
} }
}
template <typename ClusterType, typename Enable>
void ClusterFile<ClusterType, Enable>::set_roi(ROI roi) {
m_roi = roi;
}
template <typename ClusterType, typename Enable>
void ClusterFile<ClusterType, Enable>::set_noise_map(
const NDView<int32_t, 2> noise_map) {
m_noise_map = NDArray<int32_t, 2>(noise_map);
}
template <typename ClusterType, typename Enable>
void ClusterFile<ClusterType, Enable>::set_gain_map(
const NDView<double, 2> gain_map) {
m_gain_map = GainMap(gain_map);
}
template <typename ClusterType, typename Enable>
void ClusterFile<ClusterType, Enable>::set_gain_map(const GainMap &gain_map) {
m_gain_map = gain_map;
}
template <typename ClusterType, typename Enable>
void ClusterFile<ClusterType, Enable>::set_gain_map(const GainMap &&gain_map) {
m_gain_map = gain_map;
}
// TODO generally supported for all clsuter types
template <typename ClusterType, typename Enable>
void ClusterFile<ClusterType, Enable>::write_frame(
const ClusterVector<ClusterType> &clusters) {
if (m_mode != "w" && m_mode != "a") {
throw std::runtime_error("File not opened for writing");
} }
int32_t frame_number = clusters.frame_number(); private:
fwrite(&frame_number, sizeof(frame_number), 1, fp); ClusterVector<ClusterType> read_clusters_with_cut(size_t n_clusters);
uint32_t n_clusters = clusters.size(); ClusterVector<ClusterType> read_clusters_without_cut(size_t n_clusters);
fwrite(&n_clusters, sizeof(n_clusters), 1, fp); ClusterVector<ClusterType> read_frame_with_cut();
fwrite(clusters.data(), clusters.item_size(), clusters.size(), fp); ClusterVector<ClusterType> read_frame_without_cut();
} bool is_selected(ClusterType &cl);
ClusterType read_one_cluster();
template <typename ClusterType, typename Enable> };
ClusterVector<ClusterType>
ClusterFile<ClusterType, Enable>::read_clusters(size_t n_clusters) {
if (m_mode != "r") {
throw std::runtime_error("File not opened for reading");
}
if (m_noise_map || m_roi) {
return read_clusters_with_cut(n_clusters);
} else {
return read_clusters_without_cut(n_clusters);
}
}
template <typename ClusterType, typename Enable> template <typename ClusterType, typename Enable>
ClusterVector<ClusterType> ClusterVector<ClusterType>
@ -276,8 +245,8 @@ ClusterFile<ClusterType, Enable>::read_clusters_without_cut(size_t n_clusters) {
// if there are photons left from previous frame read them first // if there are photons left from previous frame read them first
if (nph) { if (nph) {
if (nph > n_clusters) { if (nph > n_clusters) {
// if we have more photons left in the frame then photons to read we // if we have more photons left in the frame then photons to
// read directly the requested number // read we read directly the requested number
nn = n_clusters; nn = n_clusters;
} else { } else {
nn = nph; nn = nph;
@ -343,8 +312,8 @@ ClusterFile<ClusterType, Enable>::read_clusters_with_cut(size_t n_clusters) {
while (fread(&frame_number, sizeof(frame_number), 1, fp)) { while (fread(&frame_number, sizeof(frame_number), 1, fp)) {
if (fread(&m_num_left, sizeof(m_num_left), 1, fp)) { if (fread(&m_num_left, sizeof(m_num_left), 1, fp)) {
clusters.set_frame_number( clusters.set_frame_number(
frame_number); // cluster vector will hold the last frame frame_number); // cluster vector will hold the last
// number // frame number
while (m_num_left && clusters.size() < n_clusters) { while (m_num_left && clusters.size() < n_clusters) {
ClusterType c = read_one_cluster(); ClusterType c = read_one_cluster();
if (is_selected(c)) { if (is_selected(c)) {
@ -375,18 +344,6 @@ ClusterType ClusterFile<ClusterType, Enable>::read_one_cluster() {
return c; return c;
} }
template <typename ClusterType, typename Enable>
ClusterVector<ClusterType> ClusterFile<ClusterType, Enable>::read_frame() {
if (m_mode != "r") {
throw std::runtime_error(LOCATION + "File not opened for reading");
}
if (m_noise_map || m_roi) {
return read_frame_with_cut();
} else {
return read_frame_without_cut();
}
}
template <typename ClusterType, typename Enable> template <typename ClusterType, typename Enable>
ClusterVector<ClusterType> ClusterVector<ClusterType>
ClusterFile<ClusterType, Enable>::read_frame_without_cut() { ClusterFile<ClusterType, Enable>::read_frame_without_cut() {