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
7#include "tatami/tatami.hpp"
8
9#include "build_indices.hpp"
10#include "subset_to_markers.hpp"
11
12#include <vector>
13#include <memory>
14
20namespace singlepp {
21
27template<typename Index_ = DefaultIndex, typename Float_ = DefaultFloat>
36 int top = -1;
37
43 std::shared_ptr<knncolle::Builder<knncolle::SimpleMatrix<Index_, Index_, Float_>, Float_> > trainer;
44
49 int num_threads = 1;
50};
51
55namespace internal {
56
57template<typename Value_, typename Index_, typename Label_, typename Float_>
58std::vector<PerLabelReference<Index_, Float_> > build_references(
59 const tatami::Matrix<Value_, Index_>& ref,
60 const Label_* labels,
61 const std::vector<Index_>& subset,
63{
64 if (options.trainer) {
65 return build_indices(ref, labels, subset, *(options.trainer), options.num_threads);
66 } else {
68 return build_indices(ref, labels, subset, DefaultBuilder(), options.num_threads);
69 }
70}
71
72}
86template<typename Index_, typename Float_>
88public:
95 std::vector<Index_> subset,
96 std::vector<internal::PerLabelReference<Index_, Float_> > references) :
97 my_test_nrow(test_nrow),
98 my_markers(std::move(markers)),
99 my_subset(std::move(subset)),
100 my_references(std::move(references))
101 {}
106private:
107 Index_ my_test_nrow;
108 Markers<Index_> my_markers;
109 std::vector<Index_> my_subset;
110 std::vector<internal::PerLabelReference<Index_, Float_> > my_references;
111
112public:
117 return my_test_nrow;
118 }
119
127 return my_markers;
128 }
129
134 const std::vector<Index_>& get_subset() const {
135 return my_subset;
136 }
137
141 size_t num_labels() const {
142 return my_references.size();
143 }
144
148 size_t num_profiles() const {
149 size_t n = 0;
150 for (const auto& ref : my_references) {
151 n += ref.ranked.size();
152 }
153 return n;
154 }
155
159 const auto& get_references() const {
160 return my_references;
161 }
165};
166
190template<typename Value_, typename Index_, typename Label_, typename Float_>
192 const tatami::Matrix<Value_, Index_>& ref,
193 const Label_* labels,
196{
197 auto subset = internal::subset_to_markers(markers, options.top);
198 auto subref = internal::build_references(ref, labels, subset, options);
199 Index_ test_nrow = ref.nrow(); // remember, test and ref are assumed to have the same features.
200 return TrainedSingle<Index_, Float_>(test_nrow, std::move(markers), std::move(subset), std::move(subref));
201}
202
213template<typename Index_, typename Float_>
215public:
222 std::vector<Index_> test_subset,
223 std::vector<Index_> ref_subset,
224 std::vector<internal::PerLabelReference<Index_, Float_> > references) :
225 my_test_nrow(test_nrow),
226 my_markers(std::move(markers)),
227 my_test_subset(std::move(test_subset)),
228 my_ref_subset(std::move(ref_subset)),
229 my_references(std::move(references))
230 {}
235private:
236 Index_ my_test_nrow;
237 Markers<Index_> my_markers;
238 std::vector<Index_> my_test_subset;
239 std::vector<Index_> my_ref_subset;
240 std::vector<internal::PerLabelReference<Index_, Float_> > my_references;
241
242public:
247 return my_test_nrow;
248 }
249
257 return my_markers;
258 }
259
265 const std::vector<Index_>& get_test_subset() const {
266 return my_test_subset;
267 }
268
274 const std::vector<Index_>& get_ref_subset() const {
275 return my_ref_subset;
276 }
277
281 size_t num_labels() const {
282 return my_references.size();
283 }
284
288 size_t num_profiles() const {
289 size_t n = 0;
290 for (const auto& ref : my_references) {
291 n += ref.ranked.size();
292 }
293 return n;
294 }
295
299 const auto& get_references() const {
300 return my_references;
301 }
305};
306
333template<typename Index_, typename Value_, typename Label_, typename Float_>
337 const tatami::Matrix<Value_, Index_>& ref,
338 const Label_* labels,
341{
342 auto pairs = internal::subset_to_markers(intersection, markers, options.top);
343 auto subref = internal::build_references(ref, labels, pairs.second, options);
344 return TrainedSingleIntersect<Index_, Float_>(test_nrow, std::move(markers), std::move(pairs.first), std::move(pairs.second), std::move(subref));
345}
346
350// For back-compatibility only.
351template<typename Index_, typename Value_, typename Label_, typename Float_>
354 const tatami::Matrix<Value_, Index_>& ref,
355 const Label_* labels,
358{
360}
393template<typename Index_, typename Id_, typename Value_, typename Label_, typename Float_>
406
407}
408
409#endif
Classifier built from an intersection of genes.
Definition train_single.hpp:214
Index_ get_test_nrow() const
Definition train_single.hpp:246
const Markers< Index_ > & get_markers() const
Definition train_single.hpp:256
const std::vector< Index_ > & get_test_subset() const
Definition train_single.hpp:265
const std::vector< Index_ > & get_ref_subset() const
Definition train_single.hpp:274
size_t num_labels() const
Definition train_single.hpp:281
size_t num_profiles() const
Definition train_single.hpp:288
Classifier trained from a single reference.
Definition train_single.hpp:87
const std::vector< Index_ > & get_subset() const
Definition train_single.hpp:134
Index_ get_test_nrow() const
Definition train_single.hpp:116
size_t num_labels() const
Definition train_single.hpp:141
size_t num_profiles() const
Definition train_single.hpp:148
const Markers< Index_ > & get_markers() const
Definition train_single.hpp:126
Common definitions for singlepp.
Cell type classification using the SingleR algorithm in C++.
Definition classify_single.hpp:19
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::vector< std::vector< Index_ > > > Markers
Definition Markers.hpp:40
TrainedSingleIntersect< Index_, Float_ > train_single_intersect(Index_ test_nrow, const Intersection< Index_ > &intersection, const tatami::Matrix< Value_, Index_ > &ref, const Label_ *labels, Markers< Index_ > markers, const TrainSingleOptions< Index_, Float_ > &options)
Definition train_single.hpp:334
TrainedSingle< Index_, Float_ > train_single(const tatami::Matrix< Value_, Index_ > &ref, const Label_ *labels, Markers< Index_ > markers, const TrainSingleOptions< Index_, Float_ > &options)
Definition train_single.hpp:191
Options for train_single() and friends.
Definition train_single.hpp:28
int num_threads
Definition train_single.hpp:49
std::shared_ptr< knncolle::Builder< knncolle::SimpleMatrix< Index_, Index_, Float_ >, Float_ > > trainer
Definition train_single.hpp:43
int top
Definition train_single.hpp:36