singlepp
A C++ library for cell type classification
Loading...
Searching...
No Matches
train_single.hpp
Go to the documentation of this file.
1#ifndef SINGLEPP_TRAIN_SINGLE_HPP
2#define SINGLEPP_TRAIN_SINGLE_HPP
3
4#include "defs.hpp"
5
6#include "tatami/tatami.hpp"
7
8#include "build_reference.hpp"
9#include "subset_to_markers.hpp"
10#include "utils.hpp"
11
12#include <vector>
13#include <memory>
14#include <cstddef>
15#include <cassert>
16
22namespace singlepp {
23
32 int num_threads = 1;
33};
34
38template<typename Index_, typename Float_>
39std::size_t get_num_labels_from_built(const BuiltReference<Index_, Float_>& built) {
40 if (built.sparse.has_value()) {
41 return built.sparse->size();
42 } else {
43 return built.dense->size();
44 }
45}
46
47template<typename Index_, typename Float_>
48std::size_t get_num_profiles_from_built(const BuiltReference<Index_, Float_>& built) {
49 std::size_t n = 0;
50 if (built.sparse.has_value()) {
51 for (const auto& ref : *(built.sparse)) {
52 n += get_num_samples(ref);
53 }
54 } else {
55 for (const auto& ref : *(built->dense)) {
56 n += get_num_samples(ref);
57 }
58 }
59 return n;
60}
74template<typename Index_, typename Float_>
76public:
81 Index_ test_nrow,
83 std::vector<Index_> subset,
84 BuiltReference<Index_, Float_> built
85 ) :
86 my_test_nrow(test_nrow),
87 my_markers(std::move(markers)),
88 my_subset(std::move(subset)),
89 my_built(std::move(built))
90 {
91 assert(is_sorted_unique(subset.size(), subset.data()));
92
93 const auto nlabels = my_built.dense.has_value() ? my_built.dense->size() : my_built.sparse->size();
94 assert(sanisizer::is_equal(my_markers.size(), nlabels));
95 for ([[maybe_unused]] const auto& mm : my_markers) {
96 assert(sanisizer::is_equal(mm.size(), nlabels));
97 }
98 }
103private:
104 Index_ my_test_nrow;
105 PairwiseMarkers<Index_> my_markers;
106 std::vector<Index_> my_subset;
107 BuiltReference<Index_, Float_> my_built;
108
109public:
113 Index_ test_nrow() const {
114 return my_test_nrow;
115 }
116
124 return my_markers;
125 }
126
132 const std::vector<Index_>& subset() const {
133 return my_subset;
134 }
135
139 std::size_t num_labels() const {
140 return get_num_labels_from_built(my_built);
141 }
142
146 std::size_t num_profiles() const {
147 return get_num_profiles_from_built(my_built);
148 }
149
153 const auto& built() const {
154 return my_built;
155 }
159};
160
188template<typename Float_ = double, typename Value_, typename Index_, typename Label_>
191 const Label_* labels,
193 const TrainSingleOptions& options
194) {
195 auto subset = subset_to_markers(ref.nrow(), markers);
196 const auto num_labels = markers.size();
197 auto subref = build_reference<Float_>(ref, labels, num_labels, subset, options.num_threads);
198 const Index_ test_nrow = ref.nrow(); // remember, test and ref are assumed to have the same features.
199 return TrainedSingle<Index_, Float_>(test_nrow, std::move(markers), std::move(subset), std::move(subref));
200}
201
236template<typename Float_ = double, typename Index_, typename Value_, typename Label_>
238 Index_ test_nrow,
239 const Intersection<Index_>& intersection,
241 const Label_* labels,
243 std::vector<Index_>* ref_subset,
244 const TrainSingleOptions& options
245) {
246 auto pairs = subset_to_markers(test_nrow, intersection, ref.nrow(), markers);
247 const auto num_labels = markers.size();
248 auto subref = build_reference<Float_>(ref, labels, num_labels, pairs.second, options.num_threads);
249 if (ref_subset) {
250 *ref_subset = std::move(pairs.second);
251 }
252 return TrainedSingle<Index_, Float_>(test_nrow, std::move(markers), std::move(pairs.first), std::move(subref));
253}
254
291template<typename Float_ = double, typename Index_, typename Id_, typename Value_, typename Label_>
293 Index_ test_nrow,
294 const Id_* test_id,
296 const Id_* ref_id,
297 const Label_* labels,
299 std::vector<Index_>* ref_subset,
300 const TrainSingleOptions& options
301) {
302 auto intersection = intersect_genes(test_nrow, test_id, ref.nrow(), ref_id);
303 return train_single(test_nrow, intersection, ref, labels, std::move(markers), ref_subset, options);
304}
305
306}
307
308#endif
Classifier trained from a single reference.
Definition train_single.hpp:75
const std::vector< Index_ > & subset() const
Definition train_single.hpp:132
Index_ test_nrow() const
Definition train_single.hpp:113
const PairwiseMarkers< Index_ > & markers() const
Definition train_single.hpp:123
std::size_t num_labels() const
Definition train_single.hpp:139
std::size_t num_profiles() const
Definition train_single.hpp:146
virtual Index_ nrow() const=0
Common definitions for singlepp.
Cell type classification using the SingleR algorithm in C++.
Definition classify_single.hpp:20
std::vector< std::vector< std::vector< Index_ > > > PairwiseMarkers
Definition Markers.hpp:39
TrainedSingle< Index_, Float_ > train_single(const tatami::Matrix< Value_, Index_ > &ref, const Label_ *labels, PairwiseMarkers< Index_ > markers, const TrainSingleOptions &options)
Definition train_single.hpp:189
Intersection< Index_ > intersect_genes(Index_ test_nrow, const Id_ *test_id, Index_ ref_nrow, const Id_ *ref_id)
Definition Intersection.hpp:54
std::vector< std::pair< Index_, Index_ > > Intersection
Definition Intersection.hpp:35
Options for train_single() and friends.
Definition train_single.hpp:27
int num_threads
Definition train_single.hpp:32