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
11#include <vector>
12#include <memory>
13#include <cstddef>
14
20namespace singlepp {
21
36 int top = -1;
37
42 int num_threads = 1;
43};
44
48template<typename Index_, typename Float_>
49std::size_t get_num_labels_from_built(const BuiltReference<Index_, Float_>& built) {
50 if (built.sparse.has_value()) {
51 return built.sparse->size();
52 } else {
53 return built.dense->size();
54 }
55}
56
57template<typename Index_, typename Float_>
58std::size_t get_num_profiles_from_built(const BuiltReference<Index_, Float_>& built) {
59 std::size_t n = 0;
60 if (built.sparse.has_value()) {
61 for (const auto& ref : *(built.sparse)) {
62 n += get_num_samples(ref);
63 }
64 } else {
65 for (const auto& ref : *(built->dense)) {
66 n += get_num_samples(ref);
67 }
68 }
69 return n;
70}
84template<typename Index_, typename Float_>
86public:
91 Index_ test_nrow,
93 std::vector<Index_> subset,
94 BuiltReference<Index_, Float_> built
95 ) :
96 my_test_nrow(test_nrow),
97 my_markers(std::move(markers)),
98 my_subset(std::move(subset)),
99 my_built(std::move(built))
100 {}
105private:
106 Index_ my_test_nrow;
107 Markers<Index_> my_markers;
108 std::vector<Index_> my_subset;
109 BuiltReference<Index_, Float_> my_built;
110
111public:
115 Index_ test_nrow() const {
116 return my_test_nrow;
117 }
118
125 const Markers<Index_>& markers() const {
126 return my_markers;
127 }
128
134 const std::vector<Index_>& subset() const {
135 return my_subset;
136 }
137
141 std::size_t num_labels() const {
142 return get_num_labels_from_built(my_built);
143 }
144
148 std::size_t num_profiles() const {
149 return get_num_profiles_from_built(my_built);
150 }
151
155 const auto& built() const {
156 return my_built;
157 }
161};
162
186template<typename Float_ = double, typename Value_, typename Index_, typename Label_>
188 const tatami::Matrix<Value_, Index_>& ref,
189 const Label_* labels,
190 Markers<Index_> markers,
191 const TrainSingleOptions& options
192) {
193 auto subset = internal::subset_to_markers(markers, options.top);
194 auto subref = build_reference<Float_>(ref, labels, subset, options.num_threads);
195 const Index_ test_nrow = ref.nrow(); // remember, test and ref are assumed to have the same features.
196 return TrainedSingle<Index_, Float_>(test_nrow, std::move(markers), std::move(subset), std::move(subref));
197}
198
229template<typename Float_ = double, typename Index_, typename Value_, typename Label_>
231 Index_ test_nrow,
232 const Intersection<Index_>& intersection,
233 const tatami::Matrix<Value_, Index_>& ref,
234 const Label_* labels,
235 Markers<Index_> markers,
236 std::vector<Index_>* ref_subset,
237 const TrainSingleOptions& options
238) {
239 auto pairs = internal::subset_to_markers(intersection, markers, options.top);
240 auto subref = build_reference<Float_>(ref, labels, pairs.second, options.num_threads);
241 if (ref_subset) {
242 *ref_subset = std::move(pairs.second);
243 }
244 return TrainedSingle<Index_, Float_>(test_nrow, std::move(markers), std::move(pairs.first), std::move(subref));
245}
246
279template<typename Float_ = double, typename Index_, typename Id_, typename Value_, typename Label_>
281 Index_ test_nrow,
282 const Id_* test_id,
283 const tatami::Matrix<Value_, Index_>& ref,
284 const Id_* ref_id,
285 const Label_* labels,
286 Markers<Index_> markers,
287 std::vector<Index_>* ref_subset,
288 const TrainSingleOptions& options
289) {
290 auto intersection = intersect_genes(test_nrow, test_id, ref.nrow(), ref_id);
291 return train_single(test_nrow, intersection, ref, labels, std::move(markers), ref_subset, options);
292}
293
294}
295
296#endif
Classifier trained from a single reference.
Definition train_single.hpp:85
const std::vector< Index_ > & subset() const
Definition train_single.hpp:134
Index_ test_nrow() const
Definition train_single.hpp:115
std::size_t num_labels() const
Definition train_single.hpp:141
std::size_t num_profiles() const
Definition train_single.hpp:148
const Markers< Index_ > & markers() const
Definition train_single.hpp:125
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_ > > > Markers
Definition Markers.hpp:40
Intersection< Index_ > intersect_genes(Index_ test_nrow, const Id_ *test_id, Index_ ref_nrow, const Id_ *ref_id)
Definition Intersection.hpp:54
TrainedSingle< Index_, Float_ > train_single(const tatami::Matrix< Value_, Index_ > &ref, const Label_ *labels, Markers< Index_ > markers, const TrainSingleOptions &options)
Definition train_single.hpp:187
std::vector< std::pair< Index_, Index_ > > Intersection
Definition Intersection.hpp:35
Options for train_single() and friends.
Definition train_single.hpp:25
int num_threads
Definition train_single.hpp:42
int top
Definition train_single.hpp:36