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:
94 std::vector<Index_> subset,
95 std::vector<internal::PerLabelReference<Index_, Float_> > references) :
96 my_markers(std::move(markers)),
97 my_subset(std::move(subset)),
98 my_references(std::move(references))
99 {}
104private:
105 Markers<Index_> my_markers;
106 std::vector<Index_> my_subset;
107 std::vector<internal::PerLabelReference<Index_, Float_> > my_references;
108
109public:
117 return my_markers;
118 }
119
124 const std::vector<Index_>& get_subset() const {
125 return my_subset;
126 }
127
131 size_t num_labels() const {
132 return my_references.size();
133 }
134
138 size_t num_profiles() const {
139 size_t n = 0;
140 for (const auto& ref : my_references) {
141 n += ref.ranked.size();
142 }
143 return n;
144 }
145
149 const auto& get_references() const {
150 return my_references;
151 }
155};
156
180template<typename Value_, typename Index_, typename Label_, typename Float_>
182 const tatami::Matrix<Value_, Index_>& ref,
183 const Label_* labels,
186{
187 auto subset = internal::subset_to_markers(markers, options.top);
188 auto subref = internal::build_references(ref, labels, subset, options);
189 return TrainedSingle<Index_, Float_>(std::move(markers), std::move(subset), std::move(subref));
190}
191
202template<typename Index_, typename Float_>
204public:
210 std::vector<Index_> test_subset,
211 std::vector<Index_> ref_subset,
212 std::vector<internal::PerLabelReference<Index_, Float_> > references) :
213 my_markers(std::move(markers)),
214 my_test_subset(std::move(test_subset)),
215 my_ref_subset(std::move(ref_subset)),
216 my_references(std::move(references))
217 {}
222private:
223 Markers<Index_> my_markers;
224 std::vector<Index_> my_test_subset;
225 std::vector<Index_> my_ref_subset;
226 std::vector<internal::PerLabelReference<Index_, Float_> > my_references;
227
228public:
236 return my_markers;
237 }
238
244 const std::vector<Index_>& get_test_subset() const {
245 return my_test_subset;
246 }
247
253 const std::vector<Index_>& get_ref_subset() const {
254 return my_ref_subset;
255 }
256
260 size_t num_labels() const {
261 return my_references.size();
262 }
263
267 size_t num_profiles() const {
268 size_t n = 0;
269 for (const auto& ref : my_references) {
270 n += ref.ranked.size();
271 }
272 return n;
273 }
274
278 const auto& get_references() const {
279 return my_references;
280 }
284};
285
310template<typename Index_, typename Value_, typename Label_, typename Float_>
313 const tatami::Matrix<Value_, Index_>& ref,
314 const Label_* labels,
317{
318 auto pairs = internal::subset_to_markers(intersection, markers, options.top);
319 auto subref = internal::build_references(ref, labels, pairs.second, options);
320 return TrainedSingleIntersect<Index_, Float_>(std::move(markers), std::move(pairs.first), std::move(pairs.second), std::move(subref));
321}
322
351template<typename Index_, typename Id_, typename Value_, typename Label_, typename Float_>
354 const Id_* test_id,
355 const tatami::Matrix<Value_, Index_>& ref,
356 const Id_* ref_id,
357 const Label_* labels,
360{
363}
364
365}
366
367#endif
Classifier built from an intersection of genes.
Definition train_single.hpp:203
const Markers< Index_ > & get_markers() const
Definition train_single.hpp:235
const std::vector< Index_ > & get_test_subset() const
Definition train_single.hpp:244
const std::vector< Index_ > & get_ref_subset() const
Definition train_single.hpp:253
size_t num_labels() const
Definition train_single.hpp:260
size_t num_profiles() const
Definition train_single.hpp:267
Classifier trained from a single reference.
Definition train_single.hpp:87
const std::vector< Index_ > & get_subset() const
Definition train_single.hpp:124
size_t num_labels() const
Definition train_single.hpp:131
size_t num_profiles() const
Definition train_single.hpp:138
const Markers< Index_ > & get_markers() const
Definition train_single.hpp:116
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
TrainedSingleIntersect< Index_, Float_ > train_single_intersect(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:311
std::vector< std::vector< std::vector< Index_ > > > Markers
Definition Markers.hpp:40
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:181
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