1#ifndef SINGLEPP_TRAIN_SINGLE_HPP
2#define SINGLEPP_TRAIN_SINGLE_HPP
6#include "tatami/tatami.hpp"
8#include "build_reference.hpp"
9#include "subset_to_markers.hpp"
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();
43 return built.dense->size();
47template<
typename Index_,
typename Float_>
48std::size_t get_num_profiles_from_built(
const BuiltReference<Index_, Float_>& built) {
50 if (built.sparse.has_value()) {
51 for (
const auto& ref : *(built.sparse)) {
52 n += get_num_samples(ref);
55 for (
const auto& ref : *(built->dense)) {
56 n += get_num_samples(ref);
74template<
typename Index_,
typename Float_>
83 std::vector<Index_>
subset,
84 BuiltReference<Index_, Float_> built
88 my_subset(std::move(
subset)),
89 my_built(std::move(built))
93 const auto nlabels = my_built.dense.has_value() ? my_built.dense->size() : my_built.sparse->size();
94 if (!sanisizer::is_equal(my_markers.size(), nlabels)) {
95 throw std::runtime_error(
"'markers' length should be equal to the number of unique labels");
97 for (
const auto& mm : my_markers) {
98 if (!sanisizer::is_equal(mm.size(), nlabels)) {
99 throw std::runtime_error(
"length of each entry of 'markers' should be equal to the number of unique labels");
110 std::vector<Index_> my_subset;
111 BuiltReference<Index_, Float_> my_built;
136 const std::vector<Index_>&
subset()
const {
144 return get_num_labels_from_built(my_built);
151 return get_num_profiles_from_built(my_built);
157 const auto& built()
const {
191template<
typename Float_ =
double,
typename Value_,
typename Index_,
typename Label_>
193 const tatami::Matrix<Value_, Index_>& ref,
194 const Label_* labels,
198 auto subset = subset_to_markers(ref.nrow(), markers);
199 auto subref = build_reference<Float_>(ref, labels, subset, options.
num_threads);
200 const Index_ test_nrow = ref.nrow();
237template<
typename Float_ =
double,
typename Index_,
typename Value_,
typename Label_>
241 const tatami::Matrix<Value_, Index_>& ref,
242 const Label_* labels,
244 std::vector<Index_>* ref_subset,
247 auto pairs = subset_to_markers(test_nrow, intersection, ref.nrow(), markers);
248 auto subref = build_reference<Float_>(ref, labels, pairs.second, options.
num_threads);
250 *ref_subset = std::move(pairs.second);
290template<
typename Float_ =
double,
typename Index_,
typename Id_,
typename Value_,
typename Label_>
294 const tatami::Matrix<Value_, Index_>& ref,
296 const Label_* labels,
298 std::vector<Index_>* ref_subset,
301 auto intersection =
intersect_genes(test_nrow, test_id, ref.nrow(), ref_id);
302 return train_single(test_nrow, intersection, ref, labels, std::move(markers), ref_subset, options);
Classifier trained from a single reference.
Definition train_single.hpp:75
const std::vector< Index_ > & subset() const
Definition train_single.hpp:136
Index_ test_nrow() const
Definition train_single.hpp:117
const PairwiseMarkers< Index_ > & markers() const
Definition train_single.hpp:127
std::size_t num_labels() const
Definition train_single.hpp:143
std::size_t num_profiles() const
Definition train_single.hpp:150
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:192
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