ʻOhana
Population structure, admixture history, and selection using learning methods.
Public Types | Public Member Functions
jade::basic_forced_grouping< TValue > Class Template Reference

A template for a class that implements the forced grouping feature. More...

#include <jade.forced_grouping.hpp>

+ Collaboration diagram for jade::basic_forced_grouping< TValue >:

Public Types

typedef TValue value_type
 The value type. More...
 
typedef basic_matrix< value_typematrix_type
 The matrix type. More...
 
typedef basic_randomizer< value_typerandomizer_type
 The randomizer type. More...
 
typedef TValue value_type
 The value type. More...
 
typedef basic_matrix< value_typematrix_type
 The matrix type. More...
 
typedef basic_randomizer< value_typerandomizer_type
 The randomizer type. More...
 

Public Member Functions

 basic_forced_grouping ()
 Initializes a new instance of the class. More...
 
 basic_forced_grouping (char const *const path)
 Initializes a new instance of the class based on values from the specified file. More...
 
 basic_forced_grouping (const std::string &path)
 Initializes a new instance of the class based on values from the specified file. More...
 
size_t get_i () const
 
size_t get_k () const
 
value_type get_max (const size_t i, const size_t k) const
 
value_type get_min (const size_t i, const size_t k) const
 
matrix_type randomize_q (randomizer_type &rnd) const
 
bool validate_q (const matrix_type &q) const
 Validates the Q matrix and throws an exception if validation fails. More...
 
 basic_forced_grouping ()
 Initializes a new instance of the class. More...
 
 basic_forced_grouping (char const *const path)
 Initializes a new instance of the class based on values from the specified file. More...
 
 basic_forced_grouping (const std::string &path)
 Initializes a new instance of the class based on values from the specified file. More...
 
size_t get_i () const
 
size_t get_k () const
 
value_type get_max (const size_t i, const size_t k) const
 
value_type get_min (const size_t i, const size_t k) const
 
matrix_type randomize_q (randomizer_type &rnd) const
 
bool validate_q (const matrix_type &q) const
 Validates the Q matrix and throws an exception if validation fails. More...
 

Detailed Description

template<typename TValue>
class jade::basic_forced_grouping< TValue >

A template for a class that implements the forced grouping feature.

Definition at line 18 of file cpax/jade.forced_grouping.hpp.

Member Typedef Documentation

◆ matrix_type [1/2]

template<typename TValue >
typedef basic_matrix<value_type> jade::basic_forced_grouping< TValue >::matrix_type

The matrix type.

Definition at line 25 of file cpax/jade.forced_grouping.hpp.

◆ matrix_type [2/2]

template<typename TValue >
typedef basic_matrix<value_type> jade::basic_forced_grouping< TValue >::matrix_type

The matrix type.

Definition at line 25 of file qpas/jade.forced_grouping.hpp.

◆ randomizer_type [1/2]

template<typename TValue >
typedef basic_randomizer<value_type> jade::basic_forced_grouping< TValue >::randomizer_type

The randomizer type.

Definition at line 28 of file cpax/jade.forced_grouping.hpp.

◆ randomizer_type [2/2]

template<typename TValue >
typedef basic_randomizer<value_type> jade::basic_forced_grouping< TValue >::randomizer_type

The randomizer type.

Definition at line 28 of file qpas/jade.forced_grouping.hpp.

◆ value_type [1/2]

template<typename TValue >
typedef TValue jade::basic_forced_grouping< TValue >::value_type

The value type.

Definition at line 22 of file cpax/jade.forced_grouping.hpp.

◆ value_type [2/2]

template<typename TValue >
typedef TValue jade::basic_forced_grouping< TValue >::value_type

The value type.

Definition at line 22 of file qpas/jade.forced_grouping.hpp.

Constructor & Destructor Documentation

◆ basic_forced_grouping() [1/6]

template<typename TValue >
jade::basic_forced_grouping< TValue >::basic_forced_grouping ( )
inline

Initializes a new instance of the class.

Definition at line 33 of file cpax/jade.forced_grouping.hpp.

34  : _a ()
35  , _b ()
36  , _i (0)
37  , _k (0)
38  {
39  }

◆ basic_forced_grouping() [2/6]

template<typename TValue >
jade::basic_forced_grouping< TValue >::basic_forced_grouping ( char const *const  path)
inlineexplicit

Initializes a new instance of the class based on values from the specified file.

Parameters
pathThe path to the file.

Definition at line 45 of file cpax/jade.forced_grouping.hpp.

47  : _a ()
48  , _b ()
49  , _i ()
50  , _k ()
51  {
52  assert(path != nullptr);
53  try
54  {
55  std::istringstream in (_strip_comments(path));
56 
57  if (!(in >> _i) || _i == 0)
58  throw error()
59  << "error parsing number of individuals";
60 
61  if (!(in >> _k) || _k == 0)
62  throw error()
63  << "error parsing number of components";
64 
65  _a.reserve(_i);
66 
67  for (size_t i = 0; i < _i; i++)
68  {
69  size_t value;
70 
71  if (!(in >> value))
72  throw error()
73  << "error parsing component assignment "
74  << "for individual " << i+1;
75 
76  _a.push_back(value);
77  }
78 
79  const auto P = 1 + *std::max_element(_a.begin(), _a.end());
80 
81  _b.reserve(P);
82 
83  for (size_t p = 0; p < P; p++)
84  {
85  try
86  {
87  _b.emplace_back(in);
88  }
89  catch (const std::exception & e)
90  {
91  throw error()
92  << "error reading B vector for population index "
93  << p << ": " << e.what();
94  }
95  }
96 
97  _validate(in);
98  }
99  catch (const std::exception & e)
100  {
101  throw error() << "failed to read forced-grouping file '"
102  << path << "': " << e.what();
103  }
104  }
+ Here is the call graph for this function:

◆ basic_forced_grouping() [3/6]

template<typename TValue >
jade::basic_forced_grouping< TValue >::basic_forced_grouping ( const std::string &  path)
inlineexplicit

Initializes a new instance of the class based on values from the specified file.

Parameters
pathThe path to the file.

Definition at line 110 of file cpax/jade.forced_grouping.hpp.

112  : basic_forced_grouping(path.c_str())
113  {
114  }

◆ basic_forced_grouping() [4/6]

template<typename TValue >
jade::basic_forced_grouping< TValue >::basic_forced_grouping ( )
inline

Initializes a new instance of the class.

Definition at line 33 of file qpas/jade.forced_grouping.hpp.

34  : _a ()
35  , _b ()
36  , _i (0)
37  , _k (0)
38  {
39  }

◆ basic_forced_grouping() [5/6]

template<typename TValue >
jade::basic_forced_grouping< TValue >::basic_forced_grouping ( char const *const  path)
inlineexplicit

Initializes a new instance of the class based on values from the specified file.

Parameters
pathThe path to the file.

Definition at line 45 of file qpas/jade.forced_grouping.hpp.

47  : _a ()
48  , _b ()
49  , _i ()
50  , _k ()
51  {
52  assert(path != nullptr);
53  try
54  {
55  std::istringstream in (_strip_comments(path));
56 
57  if (!(in >> _i) || _i == 0)
58  throw error()
59  << "error parsing number of individuals";
60 
61  if (!(in >> _k) || _k == 0)
62  throw error()
63  << "error parsing number of components";
64 
65  _a.reserve(_i);
66 
67  for (size_t i = 0; i < _i; i++)
68  {
69  size_t value;
70 
71  if (!(in >> value))
72  throw error()
73  << "error parsing component assignment "
74  << "for individual " << i+1;
75 
76  _a.push_back(value);
77  }
78 
79  const auto P = 1 + *std::max_element(_a.begin(), _a.end());
80 
81  _b.reserve(P);
82 
83  for (size_t p = 0; p < P; p++)
84  {
85  try
86  {
87  _b.emplace_back(in);
88  }
89  catch (const std::exception & e)
90  {
91  throw error()
92  << "error reading B vector for population index "
93  << p << ": " << e.what();
94  }
95  }
96 
97  _validate(in);
98  }
99  catch (const std::exception & e)
100  {
101  throw error() << "failed to read forced-grouping file '"
102  << path << "': " << e.what();
103  }
104  }
+ Here is the call graph for this function:

◆ basic_forced_grouping() [6/6]

template<typename TValue >
jade::basic_forced_grouping< TValue >::basic_forced_grouping ( const std::string &  path)
inlineexplicit

Initializes a new instance of the class based on values from the specified file.

Parameters
pathThe path to the file.

Definition at line 110 of file qpas/jade.forced_grouping.hpp.

112  : basic_forced_grouping(path.c_str())
113  {
114  }

Member Function Documentation

◆ get_i() [1/2]

template<typename TValue >
size_t jade::basic_forced_grouping< TValue >::get_i ( ) const
inline
Returns
The number of individuals.

Definition at line 119 of file cpax/jade.forced_grouping.hpp.

120  {
121  return _i;
122  }

◆ get_i() [2/2]

template<typename TValue >
size_t jade::basic_forced_grouping< TValue >::get_i ( ) const
inline
Returns
The number of individuals.

Definition at line 119 of file qpas/jade.forced_grouping.hpp.

120  {
121  return _i;
122  }

◆ get_k() [1/2]

template<typename TValue >
size_t jade::basic_forced_grouping< TValue >::get_k ( ) const
inline
Returns
The number of components.

Definition at line 127 of file cpax/jade.forced_grouping.hpp.

128  {
129  return _k;
130  }

◆ get_k() [2/2]

template<typename TValue >
size_t jade::basic_forced_grouping< TValue >::get_k ( ) const
inline
Returns
The number of components.

Definition at line 127 of file qpas/jade.forced_grouping.hpp.

128  {
129  return _k;
130  }

◆ get_max() [1/2]

template<typename TValue >
value_type jade::basic_forced_grouping< TValue >::get_max ( const size_t  i,
const size_t  k 
) const
inline
Returns
The maximum value for a specified individual and component.
Parameters
iThe individual index.
kThe component index.

Definition at line 135 of file cpax/jade.forced_grouping.hpp.

139  {
140  assert(i < _i);
141  assert(k < _k);
142  return _b[_a[i]].get_value(k + _k);
143  }
+ Here is the caller graph for this function:

◆ get_max() [2/2]

template<typename TValue >
value_type jade::basic_forced_grouping< TValue >::get_max ( const size_t  i,
const size_t  k 
) const
inline
Returns
The maximum value for a specified individual and component.
Parameters
iThe individual index.
kThe component index.

Definition at line 135 of file qpas/jade.forced_grouping.hpp.

139  {
140  assert(i < _i);
141  assert(k < _k);
142  return _b[_a[i]].get_value(k + _k);
143  }

◆ get_min() [1/2]

template<typename TValue >
value_type jade::basic_forced_grouping< TValue >::get_min ( const size_t  i,
const size_t  k 
) const
inline
Returns
The minimim value for a specified individual and component.
Parameters
iThe individual index.
kThe component index.

Definition at line 148 of file cpax/jade.forced_grouping.hpp.

152  {
153  assert(i < _i);
154  assert(k < _k);
155  return _b[_a[i]].get_value(k);
156  }
+ Here is the caller graph for this function:

◆ get_min() [2/2]

template<typename TValue >
value_type jade::basic_forced_grouping< TValue >::get_min ( const size_t  i,
const size_t  k 
) const
inline
Returns
The minimim value for a specified individual and component.
Parameters
iThe individual index.
kThe component index.

Definition at line 148 of file qpas/jade.forced_grouping.hpp.

152  {
153  assert(i < _i);
154  assert(k < _k);
155  return _b[_a[i]].get_value(k);
156  }

◆ randomize_q() [1/2]

template<typename TValue >
matrix_type jade::basic_forced_grouping< TValue >::randomize_q ( randomizer_type rnd) const
inline
Returns
A new Q matrix from random values.
Parameters
rndThe randomizer.

Definition at line 161 of file cpax/jade.forced_grouping.hpp.

164  {
165  static const auto epsilon = value_type(0.000001);
166 
167  matrix_type q (_i, _k);
168 
169  for (size_t k = 0; k < _k; k++)
170  for (size_t i = 0; i < _i; i++)
171  q(i, k) = _lerp(get_min(i, k), get_max(i, k), q(i, k));
172 
173  typedef std::uniform_int_distribution<size_t> k_dist_type;
174  typedef std::uniform_real_distribution<value_type> q_dist_type;
175 
176  k_dist_type k_dist (0, _k - 1);
177 
178  auto & engine = rnd.get_engine();
179 
180  for (size_t i = 0; i < _i; i++)
181  {
182  for (;;)
183  {
184  const auto row_sum = q.get_row_sum(i);
185  if (std::fabs(value_type(1) - row_sum) < epsilon)
186  break;
187 
188  const auto k = k_dist(engine);
189  const auto q_ik = q(i, k);
190 
191  if (row_sum > value_type(1))
192  {
193  const auto min_ik = get_min(i, k);
194  const auto distance = row_sum - value_type(1);
195  const auto boundary = std::max(min_ik, q_ik - distance);
196 
197  q_dist_type q_dist (boundary, q_ik);
198  q(i, k) = q_dist(engine);
199  }
200  else
201  {
202  const auto max_ik = get_max(i, k);
203  const auto distance = value_type(1) - row_sum;
204  const auto boundary = std::min(q_ik + distance, max_ik);
205 
206  q_dist_type q_dist (q_ik, boundary);
207  q(i, k) = q_dist(engine);
208  }
209  }
210  }
211 
212  return q;
213  }
+ Here is the call graph for this function:

◆ randomize_q() [2/2]

template<typename TValue >
matrix_type jade::basic_forced_grouping< TValue >::randomize_q ( randomizer_type rnd) const
inline
Returns
A new Q matrix from random values.
Parameters
rndThe randomizer.

Definition at line 161 of file qpas/jade.forced_grouping.hpp.

164  {
165  static const auto epsilon = value_type(0.000001);
166 
167  matrix_type q (_i, _k);
168 
169  for (size_t k = 0; k < _k; k++)
170  for (size_t i = 0; i < _i; i++)
171  q(i, k) = _lerp(get_min(i, k), get_max(i, k), q(i, k));
172 
173  typedef std::uniform_int_distribution<size_t> k_dist_type;
174  typedef std::uniform_real_distribution<value_type> q_dist_type;
175 
176  k_dist_type k_dist (0, _k - 1);
177 
178  auto & engine = rnd.get_engine();
179 
180  for (size_t i = 0; i < _i; i++)
181  {
182  for (;;)
183  {
184  const auto row_sum = q.get_row_sum(i);
185  if (std::fabs(value_type(1) - row_sum) < epsilon)
186  break;
187 
188  const auto k = k_dist(engine);
189  const auto q_ik = q(i, k);
190 
191  if (row_sum > value_type(1))
192  {
193  const auto min_ik = get_min(i, k);
194  const auto distance = row_sum - value_type(1);
195  const auto boundary = std::max(min_ik, q_ik - distance);
196 
197  q_dist_type q_dist (boundary, q_ik);
198  q(i, k) = q_dist(engine);
199  }
200  else
201  {
202  const auto max_ik = get_max(i, k);
203  const auto distance = value_type(1) - row_sum;
204  const auto boundary = std::min(q_ik + distance, max_ik);
205 
206  q_dist_type q_dist (q_ik, boundary);
207  q(i, k) = q_dist(engine);
208  }
209  }
210  }
211 
212  return q;
213  }
+ Here is the call graph for this function:

◆ validate_q() [1/2]

template<typename TValue >
bool jade::basic_forced_grouping< TValue >::validate_q ( const matrix_type q) const
inline

Validates the Q matrix and throws an exception if validation fails.

Returns
True.
Parameters
qThe Q matrix.

Definition at line 219 of file cpax/jade.forced_grouping.hpp.

222  {
223  if (_k != q.get_width())
224  throw error()
225  << "inconsistent number of components specified in "
226  << "forced-grouping file (" << _k << ") and "
227  << q.get_size_str() << " Q matrix";
228 
229  if (_i != q.get_height())
230  throw error()
231  << "inconsistent number of individuals specified in "
232  << "forced-grouping file (" << _i << ") and "
233  << q.get_size_str() << " Q matrix";
234 
235  for (size_t i = 0; i < _i; i++)
236  {
237  for (size_t k = 0; k < _k; k++)
238  {
239  const auto q_ik = q(i, k);
240  const auto min = get_min(i, k);
241  const auto max = get_max(i, k);
242  if (q_ik >= min && q_ik <= max)
243  continue;
244 
245  throw error()
246  << "inconsistent Q matrix cell [" << i+1 << "," << k+1
247  << "] (" << q_ik << ") is outside the range specified "
248  << "in the forced-grouping file " << min << " to "
249  << max;
250  }
251  }
252 
253  return true;
254  }
+ Here is the call graph for this function:

◆ validate_q() [2/2]

template<typename TValue >
bool jade::basic_forced_grouping< TValue >::validate_q ( const matrix_type q) const
inline

Validates the Q matrix and throws an exception if validation fails.

Returns
True.
Parameters
qThe Q matrix.

Definition at line 219 of file qpas/jade.forced_grouping.hpp.

222  {
223  if (_k != q.get_width())
224  throw error()
225  << "inconsistent number of components specified in "
226  << "forced-grouping file (" << _k << ") and "
227  << q.get_size_str() << " Q matrix";
228 
229  if (_i != q.get_height())
230  throw error()
231  << "inconsistent number of individuals specified in "
232  << "forced-grouping file (" << _i << ") and "
233  << q.get_size_str() << " Q matrix";
234 
235  for (size_t i = 0; i < _i; i++)
236  {
237  for (size_t k = 0; k < _k; k++)
238  {
239  const auto q_ik = q(i, k);
240  const auto min = get_min(i, k);
241  const auto max = get_max(i, k);
242  if (q_ik >= min && q_ik <= max)
243  continue;
244 
245  throw error()
246  << "inconsistent Q matrix cell [" << i+1 << "," << k+1
247  << "] (" << q_ik << ") is outside the range specified "
248  << "in the forced-grouping file " << min << " to "
249  << max;
250  }
251  }
252 
253  return true;
254  }
+ Here is the call graph for this function:

The documentation for this class was generated from the following file:
jade::basic_forced_grouping::get_max
value_type get_max(const size_t i, const size_t k) const
Definition: cpax/jade.forced_grouping.hpp:135
jade::basic_forced_grouping::matrix_type
basic_matrix< value_type > matrix_type
The matrix type.
Definition: cpax/jade.forced_grouping.hpp:25
jade::basic_forced_grouping::basic_forced_grouping
basic_forced_grouping()
Initializes a new instance of the class.
Definition: cpax/jade.forced_grouping.hpp:33
jade::basic_forced_grouping::get_min
value_type get_min(const size_t i, const size_t k) const
Definition: cpax/jade.forced_grouping.hpp:148
jade::basic_forced_grouping::value_type
TValue value_type
The value type.
Definition: cpax/jade.forced_grouping.hpp:22