-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefm.hpp
1798 lines (1355 loc) · 47.9 KB
/
defm.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef DEFM_HPP
#define DEFM_HPP 1
// #include "../barry.hpp"
#include <iterator>
#include <regex>
namespace defm {
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Start of -include/barry/models/defm/defm-types.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
#ifndef DEFM_TYPES_HPP
#define DEFM_TYPES_HPP
class DEFMData;
typedef barry::BArrayDense<int, DEFMData> DEFMArray;
/**
* @brief Data class for DEFM arrays.
*
* This holds information pointing to the data array, including information
* regarding the number of observations, the time slices of the observation,
* and the number of covariates in the data.
*
*/
class DEFMData {
public:
DEFMArray * array; // Pointer to the owner of this data
const double * covariates; ///< Vector of covariates (complete vector)
size_t obs_start; ///< Index of the observation in the data.
size_t X_ncol; ///< Number of columns in the array of covariates.
size_t X_nrow; ///< Number of rows in the array of covariates.
std::vector< size_t > covar_sort; /// Value where the sorting of the covariates is stored.
std::vector< size_t > covar_used; /// Vector indicating which covariates are included in the model
DEFMData() {};
/**
* @brief Constructor
* @param covariates_ Pointer to the attribute data.
* @param obs_start_ Location of the current observation in the covariates
* vector
* @param X_ncol_ Number of columns (covariates.)
*/
DEFMData(
DEFMArray * array_,
const double * covariates_,
size_t obs_start_,
size_t X_ncol_,
size_t X_nrow_
) : array(array_), covariates(covariates_), obs_start(obs_start_),
X_ncol(X_ncol_), X_nrow(X_nrow_) {};
/**
* @brief Access to the row (i) colum (j) data
*
* @param i
* @param j
* @return double
*/
double operator()(size_t i, size_t j) const;
double at(size_t i, size_t j) const;
size_t ncol() const;
size_t nrow() const;
void print() const;
~DEFMData() {};
};
/**
* @brief Data class used to store arbitrary size_t or double vectors */
class DEFMCounterData {
public:
std::vector< size_t > indices;
std::vector< double > numbers;
std::vector< bool > logical;
bool is_motif; ///< If false, then is a logit intercept.
DEFMCounterData() : indices(0u), numbers(0u), logical(0u), is_motif(true) {};
DEFMCounterData(
const std::vector< size_t > indices_,
const std::vector< double > numbers_,
const std::vector< bool > logical_,
bool is_motif_ = true
): indices(indices_), numbers(numbers_),
logical(logical_), is_motif(is_motif_) {};
size_t idx(size_t i) const {return indices[i];};
double num(size_t i) const {return numbers[i];};
bool is_true(size_t i) const {return logical[i];};
~DEFMCounterData() {};
};
class DEFMRuleData {
public:
std::vector< double > numbers;
std::vector< size_t > indices;
std::vector< bool > logical;
bool init = false;
double num(size_t i) const {return numbers[i];};
size_t idx(size_t i) const {return indices[i];};
bool is_true(size_t i) const {return logical[i];};
DEFMRuleData() {};
DEFMRuleData(
std::vector< double > numbers_,
std::vector< size_t > indices_,
std::vector< bool > logical_
) : numbers(numbers_), indices(indices_), logical(logical_) {};
DEFMRuleData(
std::vector< double > numbers_,
std::vector< size_t > indices_
) : numbers(numbers_), indices(indices_), logical(numbers_.size()) {};
};
inline double DEFMData::operator()(size_t i, size_t j) const
{
return *(covariates + (obs_start + j * X_nrow + i));
}
inline size_t DEFMData::ncol() const {
return X_ncol;
}
inline size_t DEFMData::nrow() const {
return X_nrow;
}
inline void DEFMData::print() const {
for (size_t i = 0u; i < array->nrow(); ++i)
{
printf_barry("row %li (%li): ", i, obs_start + i);
for (size_t j = 0u; j < X_ncol; ++j)
printf_barry("% 5.2f, ", operator()(i, j));
printf_barry("\n");
}
}
/**
* @weakgroup rules-phylo Phylo rules
* @brief Rules for phylogenetic modeling
* @param rules A pointer to a `PhyloRules` object (`Rules`<`PhyloArray`, `PhyloRuleData`>).
*/
///@{
class DEFMRuleDynData : public DEFMRuleData {
public:
const std::vector< double > * counts;
DEFMRuleDynData(
const std::vector< double > * counts_,
std::vector< double > numbers_ = {},
std::vector< size_t > indices_ = {},
std::vector< bool > logical_ = {}
) : DEFMRuleData(numbers_, indices_, logical_), counts(counts_) {};
~DEFMRuleDynData() {};
};
/**
* @name Convenient typedefs for network objects.
*/
///@{
typedef barry::Counter<DEFMArray, DEFMCounterData > DEFMCounter;
typedef barry::Counters<DEFMArray, DEFMCounterData> DEFMCounters;
typedef barry::Support<DEFMArray, DEFMCounterData, DEFMRuleData,DEFMRuleDynData> DEFMSupport;
typedef barry::StatsCounter<DEFMArray, DEFMCounterData> DEFMStatsCounter;
typedef barry::Model<DEFMArray, DEFMCounterData,DEFMRuleData,DEFMRuleDynData> DEFMModel;
typedef barry::Rule<DEFMArray, DEFMRuleData> DEFMRule;
typedef barry::Rules<DEFMArray, DEFMRuleData> DEFMRules;
typedef barry::Rule<DEFMArray, DEFMRuleDynData> DEFMRuleDyn;
typedef barry::Rules<DEFMArray, DEFMRuleDynData> DEFMRulesDyn;
///@}
#endif
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
End of -include/barry/models/defm/defm-types.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Start of -include/barry/models/defm/counters.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
#ifndef BARRAY_DEFM_H
#define BARRAY_DEFM_H 1
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Start of -include/barry/models//defm/formula.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
#ifndef BARRY_DEFM_MOTIF_FORMULA_HPP
#define BARRY_DEFM_MOTIF_FORMULA_HPP
/**
* @brief Parses a motif formula
*
* @details This function will take the formula and generate the corresponding
* input for defm::counter_transition(). Formulas can be specified in the
* following ways:
*
* - Intercept effect: {...} No transition, only including the current state.
* - Transition effect: {...} > {...} Includes current and previous states.
*
* The general notation is `[0]y[column id]_[row id]`. A preceeding zero
* means that the value of the cell is considered to be zero. The column
* id goes between 0 and the number of columns in the array - 1 (so it
* is indexed from 0,) and the row id goes from 0 to m_order.
*
* ## Intercept effects
*
* Intercept effects only involve a single set of curly brackets. Using the
* 'greater-than' symbol (i.e., '<') is only for transition effects. When
* specifying intercept effects, users can skip the `row_id`, e.g.,
* `y0_0` is equivalent to `y0`. If the passed `row id` is different from
* the Markov order, i.e., `row_id != m_order`, then the function returns
* with an error.
*
* Examples:
*
* - `"{y0, 0y1}"` is equivalent to set a motif with the first element equal
* to one and the second to zero.
*
* ## Transition effects
*
* Transition effects can be specified using two sets of curly brackets and
* an greater-than symbol, i.e., `{...} > {...}`. The first set of brackets,
* which we call LHS, can only hold `row id` that are less than `m_order`.
*
*
*
* @param formula
* @param locations
* @param signs
* @param m_order
* @param y_ncol
*/
inline void defm_motif_parser(
std::string formula,
std::vector< size_t > & locations,
std::vector< bool > & signs,
size_t m_order,
size_t y_ncol
)
{
// Resetting the results
locations.clear();
signs.clear();
std::regex pattern_intercept(
"\\{\\s*0?y[0-9]+(_[0-9]+)?(\\s*,\\s*0?y[0-9]+(_[0-9]+)?)*\\s*\\}"
);
std::regex pattern_transition(
std::string("\\{\\s*0?y[0-9]+(_[0-9]+)?(\\s*,\\s*0?y[0-9]+(_[0-9]+)?)*\\}\\s*(>)\\s*") +
std::string("\\{\\s*0?y[0-9]+(_[0-9]+)?(\\s*,\\s*0?y[0-9]+(_[0-9]+)?)*\\s*\\}")
);
auto empty = std::sregex_iterator();
// This column-major vector indicates true if the variable has already been
// selected
std::vector< bool > selected((m_order + 1) * y_ncol, false);
std::smatch match;
std::regex_match(formula, match, pattern_transition);
if (!match.empty())
{
if (m_order == 0)
throw std::logic_error("Transition effects are only valid when the data is a markov process.");
// Will indicate where the arrow is located at
size_t arrow_position = match.position(4u);
// This pattern will match
std::regex pattern("(0?)y([0-9]+)(_([0-9]+))?");
auto iter = std::sregex_iterator(formula.begin(), formula.end(), pattern);
for (auto i = iter; i != empty; ++i)
{
// Baseline position
size_t current_location = i->position(0u);
// First value true/false
bool is_positive;
if (i->operator[](1u).str() == "")
is_positive = true;
else if (i->operator[](1u).str() == "0")
is_positive = false;
else
throw std::logic_error("The number preceding y should be either none or zero.");
// Variable position
size_t y_col = std::stoul(i->operator[](2u).str());
if (y_col >= y_ncol)
throw std::logic_error("The proposed column is out of range.");
// Time location
size_t y_row;
std::string tmp_str = i->operator[](4u).str();
if (m_order > 1)
{
// If missing, we replace with the location
if (tmp_str == "")
{
if (current_location > arrow_position)
y_row = m_order;
else
throw std::logic_error("LHS of transition must specify time when m_order > 1");
} else
y_row = std::stoul(tmp_str);
if (y_row > m_order)
throw std::logic_error("The proposed row is out of range.");
} else {
// If missing, we replace with the location
if (tmp_str != "")
y_row = std::stoul(tmp_str);
else
y_row = (current_location < arrow_position ? 0u: 1u);
}
if (selected[y_col * (m_order + 1) + y_row])
throw std::logic_error(
"The term " + i->str() + " shows more than once in the formula.");
// Only the end of the chain can be located at position after the
// arrow
if ((current_location > arrow_position) && (y_row != m_order))
throw std::logic_error(
"Only the row " + std::to_string(m_order) +
" can be specified at the RHS of the motif."
);
selected[y_col * (m_order + 1) + y_row] = true;
locations.push_back(y_col * (m_order + 1) + y_row);
signs.push_back(is_positive);
}
return;
}
std::regex_match(formula, match, pattern_intercept);
if (!match.empty()){
// This pattern will match
std::regex pattern("(0?)y([0-9]+)(_([0-9]+))?");
auto iter = std::sregex_iterator(formula.begin(), formula.end(), pattern);
for (auto i = iter; i != empty; ++i)
{
// First value true/false
bool is_positive;
if (i->operator[](1u).str() == "")
is_positive = true;
else if (i->operator[](1u).str() == "0")
is_positive = false;
else
throw std::logic_error("The number preceding y should be either none or zero.");
// Variable position
size_t y_col = std::stoul(i->operator[](2u).str());
if (y_col >= y_ncol)
throw std::logic_error("The proposed column is out of range.");
// Time location
size_t y_row;
if (i->operator[](4u).str() == "") // Assume is the last
y_row = m_order;
else {
y_row = std::stoul(i->operator[](4u).str());
if (y_row != m_order)
throw std::logic_error(
std::string("Intercept motifs cannot feature past events. ") +
std::string("Only transition motifs can: {...} > {...}.")
);
}
if (selected[y_col * (m_order + 1) + y_row])
throw std::logic_error(
"The term " + i->str() + " shows more than once in the formula.");
selected[y_col * (m_order + 1) + y_row] = true;
locations.push_back(y_col * (m_order + 1) + y_row);
signs.push_back(is_positive);
}
return;
}
throw std::logic_error(
"The motif specified in the formula: " + formula +
" has the wrong syntax."
);
}
#endif
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
End of -include/barry/models//defm/formula.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
/**
* @ingroup counting
* @details Details on the available counters for `DEFMworkData` can be found in
* the \ref counters-network section.
*
*/
///@{
///@}
#define MAKE_DEFM_HASHER(hasher,a,cov) barry::Hasher_fun_type<DEFMArray,DEFMCounterData> \
hasher = [cov](const DEFMArray & array, DEFMCounterData * d) { \
std::vector< double > res; \
/* Adding the column feature */ \
for (size_t i = 0u; i < array.nrow(); ++i) \
res.push_back(array.D()(i, cov)); \
/* Adding the fixed dims */ \
for (size_t i = 0u; i < (array.nrow() - 1); ++i) \
for (size_t j = 0u; j < array.ncol(); ++j) \
res.push_back(array(i, j)); \
return res;\
};
/**@name Macros for defining counters
*/
///@{
/**Function for definition of a network counter function*/
#define DEFM_COUNTER(a) \
inline double (a) (const DEFMArray & Array, size_t i, size_t j, DEFMCounterData & data)
/**Lambda function for definition of a network counter function*/
#define DEFM_COUNTER_LAMBDA(a) \
barry::Counter_fun_type<DEFMArray, DEFMCounterData> a = \
[](const DEFMArray & Array, size_t i, size_t j, DEFMCounterData & data) -> double
///@}
/**@name Macros for defining rules
*/
///@{
/**Function for definition of a network counter function*/
#define DEFM_RULE(a) \
inline bool (a) (const DEFMArray & Array, size_t i, size_t j, bool & data)
/**Lambda function for definition of a network counter function*/
#define DEFM_RULE_LAMBDA(a) \
barry::Rule_fun_type<DEFMArray, DEFMRuleData> a = \
[](const DEFMArray & Array, size_t i, size_t j, DEFMRuleData & data) -> bool
///@}
/**Lambda function for definition of a network counter function*/
#define DEFM_RULEDYN_LAMBDA(a) \
barry::Rule_fun_type<DEFMArray, DEFMRuleDynData> a = \
[](const DEFMArray & Array, size_t i, size_t j, DEFMRuleDynData & data) -> bool
///@}
/**
* @weakgroup counters-network DEFMArray counters
* @brief Counters for network models
* @param counters A pointer to a `DEFMCounters` object (`Counters`<`DEFMArray`, `DEFMCounterData`>).
*/
///@{
// -----------------------------------------------------------------------------
/**
* @brief Prevalence of ones
*
* @param counters Pointer ot a vector of counters
* @param covar_index If >= than 0, then the interaction
*/
inline void counter_ones(
DEFMCounters * counters,
int covar_index = -1,
std::string vname = "",
const std::vector< std::string > * x_names = nullptr
)
{
// Weighted by a feature of the array
if (covar_index >= 0)
{
MAKE_DEFM_HASHER(hasher, array, covar_index)
DEFM_COUNTER_LAMBDA(counter_tmp)
{
// Only count the current
if (i != (Array.nrow() - 1))
return 0.0;
return Array.D()(i, data.idx(0u));
};
if (vname == "")
{
if (x_names != nullptr)
vname = x_names->operator[](covar_index);
else
vname = std::string("attr")+ std::to_string(covar_index);
}
counters->add_counter(
counter_tmp, nullptr, hasher,
DEFMCounterData({static_cast<size_t>(covar_index)}, {}, {}, true),
"Num. of ones x " + vname,
"Overall number of ones"
);
} else {
DEFM_COUNTER_LAMBDA(count_ones)
{
// Only count the current
if (i != (Array.nrow() - 1))
return 0.0;
return 1.0;
};
DEFMCounterData dat;
dat.is_motif = true;
counters->add_counter(
count_ones, nullptr, nullptr,
dat, // DEFMCounterData(),
"Num. of ones",
"Overall number of ones"
);
}
return;
}
inline void counter_logit_intercept(
DEFMCounters * counters,
size_t n_y,
std::vector< size_t > which = {},
int covar_index = -1,
std::string vname = "",
const std::vector< std::string > * x_names = nullptr,
const std::vector< std::string > * y_names = nullptr
) {
if (which.size() == 0u)
{
which.resize(n_y, 0u);
std::iota(which.begin(), which.end(), 0u);
} else {
for (auto w : which)
if (w >= n_y)
throw std::logic_error("Values in `which` are out of range.");
}
// Case when no interaction happens, whatsoever.
if (covar_index < 0)
{
DEFM_COUNTER_LAMBDA(tmp_counter)
{
if (i != (Array.nrow() - 1))
return 0.0;
if (j != data.idx(0u))
return 0.0;
return 1.0;
};
for (auto i : which)
{
if (y_names != nullptr)
vname = y_names->operator[](i);
else
vname = std::to_string(i);
counters->add_counter(
tmp_counter, nullptr, nullptr,
DEFMCounterData({i}, {}, {}, false),
"Logit intercept " + vname,
"Equal to one if the outcome " + vname + " is one. Equivalent to the logistic regression intercept."
);
}
} else {
DEFM_COUNTER_LAMBDA(tmp_counter)
{
if (i != Array.nrow() - 1)
return 0.0;
if (j != data.idx(0u))
return 0.0;
return Array.D()(i, data.idx(1u));
};
MAKE_DEFM_HASHER(hasher, array, covar_index)
bool hasher_added = false;
std::string yname;
for (auto i : which)
{
if (y_names != nullptr)
yname = y_names->operator[](i);
else
yname = std::to_string(i);
if (vname == "")
{
if (x_names != nullptr)
vname = x_names->operator[](covar_index);
else
vname = std::string("attr")+ std::to_string(covar_index);
}
if (hasher_added)
counters->add_counter(
tmp_counter, nullptr, nullptr,
DEFMCounterData({i, static_cast<size_t>(covar_index)}, {}, {}, false),
"Logit intercept " + yname + " x " + vname,
"Equal to one if the outcome " + yname + " is one. Equivalent to the logistic regression intercept."
);
else {
hasher_added = true;
counters->add_counter(
tmp_counter, nullptr, hasher,
DEFMCounterData({i, static_cast<size_t>(covar_index)}, {}, {}, false),
"Logit intercept " + yname + " x " + vname,
"Equal to one if the outcome " + yname + " is one. Equivalent to the logistic regression intercept."
);
}
}
}
}
/**
* @brief Prevalence of ones
*
* @param counters Pointer ot a vector of counters
* @param covar_index If >= than 0, then the interaction
*/
inline void counter_transition(
DEFMCounters * counters,
std::vector< size_t > coords,
std::vector< bool > signs,
size_t m_order,
size_t n_y,
int covar_index = -1,
std::string vname = "",
const std::vector< std::string > * x_names = nullptr,
const std::vector< std::string > * y_names = nullptr
)
{
// A vector to store the type of dat
if (signs.size() == 0u)
signs.resize(coords.size(), true);
else if (signs.size() != coords.size())
throw std::length_error("Size of -coords- and -signs- must match.");
if (covar_index >= 0)
coords.push_back(static_cast<size_t>(covar_index));
else
coords.push_back(1000u);
DEFM_COUNTER_LAMBDA(count_init)
{
auto indices = data.indices;
for (size_t i = 0u; i < (indices.size() - 1u); ++i)
{
if (
std::floor(indices[i] / Array.nrow()) >=
static_cast<int>(Array.ncol())
)
throw std::range_error("The motif includes entries out of range.");
}
return 0.0;
};
DEFM_COUNTER_LAMBDA(count_ones)
{
auto dat = data.indices;
auto sgn = data.logical;
int covaridx = dat[dat.size() - 1u];
// Checking if the observation is in the stat. We
const auto & array = Array.get_data();
size_t loc = i + j * Array.nrow();
size_t n_cells = dat.size() - 1u;
// Only one currently needs to be a zero for it
// to change
size_t n_now = 0;
bool baseline_value = false;
bool i_in_array = false;
for (size_t e = 0u; e < n_cells; ++e)
{
// Is the current cell in the list?
if (dat[e] == loc)
{
i_in_array = true;
baseline_value = sgn[e];
}
if ((sgn[e] && (array[dat[e]] == 1)) || (!sgn[e] && (array[dat[e]] == 0)))
n_now++;
}
// If i in array still false, then no change
if (!i_in_array)
return 0.0;
size_t n_prev = n_now;
if (baseline_value)
n_prev--;
else
n_prev++;
// Computing stats
if (covaridx < 1000)
{
double val = Array.D()(Array.nrow() - 1u, covaridx);
double value_now = n_now == n_cells ? val : 0.0;
double value_prev = n_prev == n_cells ? val : 0.0;
return value_now - value_prev;
}
else
{
double value_now = n_now == n_cells ? 1.0 : 0.0;
double value_prev = n_prev == n_cells ? 1.0 : 0.0;
return value_now - value_prev;
}
};
// Creating name of the structure
std::string name;
if (coords.size() == 1u)
name = "";
else
name = "Motif ";
// Creating an empty motif filled with zeros
barry::BArrayDense<int> motif(m_order + 1u, n_y, 0);
// Filling the matrix in, negative values are 0s and 1s are... 1s.
// Zero are values not used.
size_t n_cells = coords.size() - 1u;
for (size_t d = 0u; d < n_cells; ++d)
{
size_t c = std::floor(coords[d] / (m_order + 1u));
size_t r = coords[d] - c * (m_order + 1u);
motif(r, c) = signs[d] ? 1 : -1;
}
// Checking if any prior to the event
bool any_before_event = false;
for (size_t i = 0u; i < m_order; ++i)
{
for (size_t j = 0u; j < n_y; ++j)
{
if (motif(i,j) != 0)
{
any_before_event = true;
break;
}
}
}
#ifdef BARRY_WITH_LATEX
name += "$";
#endif
if (any_before_event)
#ifdef BARRY_WITH_LATEX
name += "(";
#else
name += "{";
#endif
#ifdef BARRY_WITH_LATEX
#define UNI_SUB(a) \
(\
((a) == 0) ? "_0" : (\
((a) == 1) ? "_1" : (\
((a) == 2) ? "_2" : (\
((a) == 3) ? "_3" : (\
((a) == 4) ? "_4" : (\
((a) == 5) ? "_5" : (\
((a) == 6) ? "_6" : (\
((a) == 7) ? "_7" : (\
((a) == 8) ? "_8" : \
"_9"))))))))\
)
#else
#define UNI_SUB(a) \
(\
((a) == 0) ? u8"\u2080" : (\
((a) == 1) ? u8"\u2081" : (\
((a) == 2) ? u8"\u2082" : (\
((a) == 3) ? u8"\u2083" : (\
((a) == 4) ? u8"\u2084" : (\
((a) == 5) ? u8"\u2085" : (\
((a) == 6) ? u8"\u2086" : (\
((a) == 7) ? u8"\u2087" : (\
((a) == 8) ? u8"\u2088" : \
u8"\u2089"))))))))\
)
#endif
// If order is greater than zero, the starting point of the transtion
for (size_t i = 0u; i < m_order; ++i)
{
bool row_start = true;
for (size_t j = 0u; j < n_y; ++j)
{
// Is it included?
if (motif(i,j) == 0)
continue;
// Is not the first?
if (row_start)
row_start = false;
else
name += ", ";
if (y_names != nullptr)
name += y_names->operator[](j);
else
name += (std::string("y") + std::to_string(j));
#ifdef BARRY_WITH_LATEX
name += (motif(i,j) < 0 ? "^-" : "^+");
#else
name += (motif(i,j) < 0 ? u8"\u207B" : u8"\u207A");
#endif
}
}
// If it has starting point, then need to close.
if (any_before_event & (m_order > 0u))
#ifdef BARRY_WITH_LATEX
name += ") -> (";
#else
name += std::string("}") + u8"\u21E8" + std::string("{");
#endif
else
#ifdef BARRY_WITH_LATEX
name += "(";
#else
name += "{";
#endif
// Looking onto the transtions
bool row_start = true;
for (size_t j = 0u; j < n_y; ++j)
{
if (motif(m_order, j) == 0)
continue;
if (row_start)
row_start = false;
else
name += ", ";
if (y_names != nullptr)
name += y_names->operator[](j);
else
name += (std::string("y") + std::to_string(j));
#ifdef BARRY_WITH_LATEX
name += (motif(m_order, j) < 0 ? "^-" : "^+" );
#else
name += (motif(m_order, j) < 0 ? u8"\u207B" : u8"\u207A" );
#endif
}
#undef UNI_SUB
#ifdef BARRY_WITH_LATEX
name += ")$";
#else
name += "}";