mumosa
Multi-modal analyses of single-cell data
Loading...
Searching...
No Matches
simple.hpp
Go to the documentation of this file.
1#ifndef MUMOSA_SIMPLE_HPP
2#define MUMOSA_SIMPLE_HPP
3
4#include <vector>
5#include <stdexcept>
6#include <cmath>
7#include <algorithm>
8#include <limits>
9#include <cstddef>
10#include <type_traits>
11
12#include "knncolle/knncolle.hpp"
13#include "quickstats/quickstats.hpp"
14#include "sanisizer/sanisizer.hpp"
15
16#include "utils.hpp"
17
23namespace mumosa {
24
28struct Options {
34 int num_neighbors = 20;
35
40 int num_threads = 1;
41};
42
59template<typename Index_, typename Distance_>
60std::pair<Distance_, Distance_> compute_distance(const Index_ num_cells, Distance_* const distances) {
61 if (num_cells == 0) {
62 return std::pair<Distance_, Distance_>(0, 0);
63 }
64
65 const Distance_ med = quickstats::median(num_cells, distances);
66 Distance_ rmsd = 0;
67 for (Index_ i = 0; i < num_cells; ++i) {
68 const auto d = distances[i];
69 rmsd += d * d;
70 }
71 rmsd = std::sqrt(rmsd / num_cells);
72 return std::make_pair(med, rmsd);
73}
74
93template<typename Index_, typename Input_, typename Distance_>
94std::pair<Distance_, Distance_> compute_distance(
96 Distance_* const buffer,
97 const Options& options
98) {
99 const Index_ nobs = prebuilt.num_observations();
100 const auto capped_k = knncolle::cap_k(options.num_neighbors, nobs);
101
102 knncolle::parallelize(options.num_threads, nobs, [&](const int, const Index_ start, const Index_ length) -> void {
103 const auto searcher = prebuilt.initialize();
104 std::vector<Distance_> cur_distances;
105 for (Index_ i = start, end = start + length; i < end; ++i) {
106 searcher->search(i, capped_k, NULL, &cur_distances);
107 if (cur_distances.size()) {
108 buffer[i] = cur_distances.back();
109 } else {
110 buffer[i] = 0; // i.e., only distance is that to itself.
111 }
112 }
113 });
114
115 return compute_distance(nobs, buffer);
116}
117
141template<typename Index_, typename Input_, typename Distance_, class Matrix_ = knncolle::Matrix<Index_, Input_> >
142std::pair<Distance_, Distance_> compute_distance(
143 const std::size_t num_dim,
144 const Index_ num_cells,
145 const Input_* const data,
147 Distance_* const buffer,
148 const Options& options
149) {
150 const auto prebuilt = builder.build_unique(knncolle::SimpleMatrix(num_dim, num_cells, data));
151 return compute_distance(*prebuilt, buffer, options);
152}
153
154}
155
156#endif
std::unique_ptr< Prebuilt< Index_, Data_, Distance_ > > build_unique(const Matrix_ &data) const
virtual Index_ num_observations() const=0
void parallelize(int num_workers, Task_ num_tasks, Run_ run_task_range)
int cap_k(int k, Index_ num_observations)
Scale multi-modal embeddings to adjust for differences in variance.
Definition blocked.hpp:22
std::pair< Distance_, Distance_ > compute_distance(const Index_ num_cells, Distance_ *const distances)
Definition simple.hpp:60
Options for compute_distance().
Definition simple.hpp:28
int num_threads
Definition simple.hpp:40
int num_neighbors
Definition simple.hpp:34