mnncorrect
Batch correction with mutual nearest neighbors
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#ifndef MNNCORRECT_UTILS_HPP
2#define MNNCORRECT_UTILS_HPP
3
4#include <vector>
5#include <algorithm>
6#include <memory>
7#include <cstddef>
8#include <type_traits>
9
10#include "knncolle/knncolle.hpp"
11
12#ifndef MNNCORRECT_CUSTOM_PARALLEL
13#include "subpar/subpar.hpp"
14#endif
15
21namespace mnncorrect {
22
26typedef std::size_t BatchIndex;
27
43enum class MergePolicy : char { INPUT, SIZE, VARIANCE, RSS };
44
57template<typename Task_, class Run_>
58void parallelize(const int num_workers, const Task_ num_tasks, Run_ run_task_range) {
59#ifndef MNNCORRECT_CUSTOM_PARALLEL
60 // Methods could allocate or throw, so nothrow_ = false is safest.
61 subpar::parallelize_range<false>(num_workers, num_tasks, std::move(run_task_range));
62#else
63 MNNCORRECT_CUSTOM_PARALLEL(num_workers, num_tasks, run_task_range);
64#endif
65}
66
71template<typename Index_>
72struct Batch {
76 Index_ start = 0;
77
81 Index_ size = 0;
82};
83
87template<typename Index_, typename Distance_>
88using NeighborSet = std::vector<std::vector<std::pair<Index_, Distance_> > >;
89
90template<typename Index_, typename Float_>
91struct MetaBatch {
92 // Each uncorrected metabatch has an original contiguous set of observations.
93 // Once corrected, the metabatch ceases to exist as it becomes part of the destination metabatch.
94 std::unique_ptr<knncolle::Prebuilt<Index_, Float_, Float_> > original_index;
95 Batch<Index_> original_ids;
96
97 // Corrected observations from other (meta)batches that have been redistributed into this meta batch.
98 struct CorrectedBatch {
99 CorrectedBatch() = default;
100 CorrectedBatch(std::unique_ptr<knncolle::Prebuilt<Index_, Float_, Float_> > index, std::vector<Index_> ids) : index(std::move(index)), ids(std::move(ids)) {}
101 std::unique_ptr<knncolle::Prebuilt<Index_, Float_, Float_> > index;
102 std::vector<Index_> ids;
103 };
104 std::vector<CorrectedBatch> corrected;
105};
106
107template<typename Input_>
108using I = std::remove_cv_t<std::remove_reference_t<Input_> >;
109
110// Putting this here so that we can re-use it in the tests.
111template<typename Index_, typename Float_, class Matrix_>
112std::unique_ptr<knncolle::Prebuilt<Index_, Float_, Float_> > subset_and_index(
113 const std::size_t num_dim,
114 const std::vector<Index_>& subset,
115 const Float_* const data,
117 Float_* const buffer
118) {
119 const auto num_subset = subset.size();
120 for (I<decltype(num_subset)> f = 0; f < num_subset; ++f) {
121 const auto curdata = data + sanisizer::product_unsafe<std::size_t>(subset[f], num_dim);
122 std::copy_n(curdata, num_dim, buffer + sanisizer::product_unsafe<std::size_t>(f, num_dim));
123 }
124 return builder.build_unique(knncolle::SimpleMatrix<Index_, Float_>(num_dim, num_subset, buffer));
125}
130}
131
132#endif
std::unique_ptr< Prebuilt< Index_, Data_, Distance_ > > build_unique(const Matrix_ &data) const
Batch correction with mutual nearest neighbors.
Definition utils.hpp:21
MergePolicy
Definition utils.hpp:43
std::size_t BatchIndex
Definition utils.hpp:26
void parallelize(const int num_workers, const Task_ num_tasks, Run_ run_task_range)
Definition utils.hpp:58
int parallelize_range(int num_workers, const Task_ num_tasks, const Run_ run_task_range)
Start and size of each batch.
Definition utils.hpp:72
Index_ start
Definition utils.hpp:76
Index_ size
Definition utils.hpp:81