aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
pseudoCount_tpl.h
Go to the documentation of this file.
1
/**
2
*
3
* Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4
* info_at_agrum_dot_org
5
*
6
* This library is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with this library. If not, see <http://www.gnu.org/licenses/>.
18
*
19
*/
20
21
22
/** @file
23
* @brief the base class for all the independence tests used for learning
24
25
* @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
26
*/
27
#
include
<
agrum
/
tools
/
stattests
/
pseudoCount
.
h
>
28
#
include
<
agrum
/
tools
/
stattests
/
idCondSet
.
h
>
29
30
#
ifndef
DOXYGEN_SHOULD_SKIP_THIS
31
32
namespace
gum
{
33
34
namespace
learning
{
35
36
/// returns the allocator used by the independence test
37
template
<
template
<
typename
>
class
ALLOC
>
38
INLINE
typename
PseudoCount
<
ALLOC
>::
allocator_type
39
PseudoCount
<
ALLOC
>::
getAllocator
()
const
{
40
return
counter_
.
getAllocator
();
41
}
42
43
/// default constructor
44
template
<
template
<
typename
>
class
ALLOC
>
45
INLINE
PseudoCount
<
ALLOC
>::
PseudoCount
(
46
const
DBRowGeneratorParser
<
ALLOC
>&
parser
,
47
const
Apriori
<
ALLOC
>&
apriori
,
48
const
std
::
vector
<
std
::
pair
<
std
::
size_t
,
std
::
size_t
>,
49
ALLOC
<
std
::
pair
<
std
::
size_t
,
std
::
size_t
> > >&
ranges
,
50
const
Bijection
<
NodeId
,
std
::
size_t
,
ALLOC
<
std
::
size_t
> >&
51
nodeId2columns
,
52
const
typename
PseudoCount
<
ALLOC
>::
allocator_type
&
alloc
) :
53
apriori_
(
apriori
.
clone
(
alloc
)),
54
counter_
(
parser
,
ranges
,
nodeId2columns
,
alloc
) {
55
GUM_CONSTRUCTOR
(
PseudoCount
);
56
}
57
58
59
/// default constructor
60
template
<
template
<
typename
>
class
ALLOC
>
61
INLINE
PseudoCount
<
ALLOC
>::
PseudoCount
(
62
const
DBRowGeneratorParser
<
ALLOC
>&
parser
,
63
const
Apriori
<
ALLOC
>&
apriori
,
64
const
Bijection
<
NodeId
,
std
::
size_t
,
ALLOC
<
std
::
size_t
> >&
65
nodeId2columns
,
66
const
typename
PseudoCount
<
ALLOC
>::
allocator_type
&
alloc
) :
67
apriori_
(
apriori
.
clone
(
alloc
)),
68
counter_
(
parser
,
nodeId2columns
,
alloc
) {
69
GUM_CONSTRUCTOR
(
PseudoCount
);
70
}
71
72
73
/// copy constructor with a given allocator
74
template
<
template
<
typename
>
class
ALLOC
>
75
INLINE
PseudoCount
<
ALLOC
>::
PseudoCount
(
76
const
PseudoCount
<
ALLOC
>&
from
,
77
const
typename
PseudoCount
<
ALLOC
>::
allocator_type
&
alloc
) :
78
apriori_
(
from
.
apriori_
->
clone
(
alloc
)),
79
counter_
(
from
.
counter_
,
alloc
) {
80
GUM_CONS_CPY
(
PseudoCount
);
81
}
82
83
84
/// copy constructor
85
template
<
template
<
typename
>
class
ALLOC
>
86
INLINE
PseudoCount
<
ALLOC
>::
PseudoCount
(
const
PseudoCount
<
ALLOC
>&
from
) :
87
PseudoCount
(
from
,
from
.
getAllocator
()) {}
88
89
90
/// move constructor
91
template
<
template
<
typename
>
class
ALLOC
>
92
INLINE
PseudoCount
<
ALLOC
>::
PseudoCount
(
93
PseudoCount
<
ALLOC
>&&
from
,
94
const
typename
PseudoCount
<
ALLOC
>::
allocator_type
&
alloc
) :
95
apriori_
(
from
.
apriori_
),
96
counter_
(
std
::
move
(
from
.
counter_
),
alloc
) {
97
from
.
apriori_
=
nullptr
;
98
GUM_CONS_MOV
(
PseudoCount
);
99
}
100
101
102
/// move constructor
103
template
<
template
<
typename
>
class
ALLOC
>
104
INLINE
PseudoCount
<
ALLOC
>::
PseudoCount
(
PseudoCount
<
ALLOC
>&&
from
) :
105
PseudoCount
(
std
::
move
(
from
),
from
.
getAllocator
()) {}
106
107
108
/// destructor
109
template
<
template
<
typename
>
class
ALLOC
>
110
INLINE
PseudoCount
<
ALLOC
>::~
PseudoCount
() {
111
if
(
apriori_
!=
nullptr
) {
112
ALLOC
<
Apriori
<
ALLOC
> >
allocator
(
this
->
getAllocator
());
113
allocator
.
destroy
(
apriori_
);
114
allocator
.
deallocate
(
apriori_
, 1);
115
}
116
GUM_DESTRUCTOR
(
PseudoCount
);
117
}
118
119
120
/// copy operator
121
template
<
template
<
typename
>
class
ALLOC
>
122
PseudoCount
<
ALLOC
>&
123
PseudoCount
<
ALLOC
>::
operator
=(
const
PseudoCount
<
ALLOC
>&
from
) {
124
if
(
this
!= &
from
) {
125
Apriori
<
ALLOC
>*
new_apriori
=
from
.
apriori_
->
clone
();
126
RecordCounter
<
ALLOC
>
new_counter
=
from
.
counter_
;
127
128
if
(
apriori_
!=
nullptr
) {
129
ALLOC
<
Apriori
<
ALLOC
> >
allocator
(
this
->
getAllocator
());
130
allocator
.
destroy
(
apriori_
);
131
allocator
.
deallocate
(
apriori_
, 1);
132
}
133
134
apriori_
=
new_apriori
;
135
counter_
=
std
::
move
(
new_counter
);
136
}
137
return
*
this
;
138
}
139
140
141
/// move operator
142
template
<
template
<
typename
>
class
ALLOC
>
143
PseudoCount
<
ALLOC
>&
144
PseudoCount
<
ALLOC
>::
operator
=(
PseudoCount
<
ALLOC
>&&
from
) {
145
if
(
this
!= &
from
) {
146
std
::
swap
(
apriori_
,
from
.
apriori_
);
147
148
counter_
=
std
::
move
(
from
.
counter_
);
149
}
150
return
*
this
;
151
}
152
153
154
/// changes the max number of threads used to parse the database
155
template
<
template
<
typename
>
class
ALLOC
>
156
INLINE
void
PseudoCount
<
ALLOC
>::
setMaxNbThreads
(
std
::
size_t
nb
)
const
{
157
counter_
.
setMaxNbThreads
(
nb
);
158
}
159
160
161
/// returns the number of threads used to parse the database
162
template
<
template
<
typename
>
class
ALLOC
>
163
INLINE
std
::
size_t
PseudoCount
<
ALLOC
>::
nbThreads
()
const
{
164
return
counter_
.
nbThreads
();
165
}
166
167
168
/** @brief changes the number min of rows a thread should process in a
169
* multithreading context */
170
template
<
template
<
typename
>
class
ALLOC
>
171
INLINE
void
172
PseudoCount
<
ALLOC
>::
setMinNbRowsPerThread
(
const
std
::
size_t
nb
)
const
{
173
counter_
.
setMinNbRowsPerThread
(
nb
);
174
}
175
176
177
/// returns the minimum of rows that each thread should process
178
template
<
template
<
typename
>
class
ALLOC
>
179
INLINE
std
::
size_t
PseudoCount
<
ALLOC
>::
minNbRowsPerThread
()
const
{
180
return
counter_
.
minNbRowsPerThread
();
181
}
182
183
184
/// sets new ranges to perform the countings used by the score
185
/** @param ranges a set of pairs {(X1,Y1),...,(Xn,Yn)} of database's rows
186
* indices. The countings are then performed only on the union of the
187
* rows [Xi,Yi), i in {1,...,n}. This is useful, e.g, when performing
188
* cross validation tasks, in which part of the database should be ignored.
189
* An empty set of ranges is equivalent to an interval [X,Y) ranging over
190
* the whole database. */
191
template
<
template
<
typename
>
class
ALLOC
>
192
template
<
template
<
typename
>
class
XALLOC
>
193
void
PseudoCount
<
ALLOC
>::
setRanges
(
194
const
std
::
vector
<
std
::
pair
<
std
::
size_t
,
std
::
size_t
>,
195
XALLOC
<
std
::
pair
<
std
::
size_t
,
std
::
size_t
> > >&
196
new_ranges
) {
197
std
::
vector
<
std
::
pair
<
std
::
size_t
,
std
::
size_t
>,
198
ALLOC
<
std
::
pair
<
std
::
size_t
,
std
::
size_t
> > >
199
old_ranges
=
ranges
();
200
counter_
.
setRanges
(
new_ranges
);
201
if
(
old_ranges
!=
ranges
())
clear
();
202
}
203
204
205
/// reset the ranges to the one range corresponding to the whole database
206
template
<
template
<
typename
>
class
ALLOC
>
207
void
PseudoCount
<
ALLOC
>::
clearRanges
() {
208
std
::
vector
<
std
::
pair
<
std
::
size_t
,
std
::
size_t
>,
209
ALLOC
<
std
::
pair
<
std
::
size_t
,
std
::
size_t
> > >
210
old_ranges
=
ranges
();
211
counter_
.
clearRanges
();
212
}
213
214
215
/// returns the current ranges
216
template
<
template
<
typename
>
class
ALLOC
>
217
INLINE
const
std
::
vector
<
std
::
pair
<
std
::
size_t
,
std
::
size_t
>,
218
ALLOC
<
std
::
pair
<
std
::
size_t
,
std
::
size_t
> > >&
219
PseudoCount
<
ALLOC
>::
ranges
()
const
{
220
return
counter_
.
ranges
();
221
}
222
223
224
/// clears all the data structures from memory
225
template
<
template
<
typename
>
class
ALLOC
>
226
INLINE
void
PseudoCount
<
ALLOC
>::
clear
() {
227
counter_
.
clear
();
228
}
229
230
231
/// return the mapping between the columns of the database and the node ids
232
template
<
template
<
typename
>
class
ALLOC
>
233
INLINE
const
Bijection
<
NodeId
,
std
::
size_t
,
ALLOC
<
std
::
size_t
> >&
234
PseudoCount
<
ALLOC
>::
nodeId2Columns
()
const
{
235
return
counter_
.
nodeId2Columns
();
236
}
237
238
239
/// return the database used by the score
240
template
<
template
<
typename
>
class
ALLOC
>
241
INLINE
const
DatabaseTable
<
ALLOC
>&
PseudoCount
<
ALLOC
>::
database
()
const
{
242
return
counter_
.
database
();
243
}
244
245
246
/// returns a counting vector where variables are marginalized from N_xyz
247
/** @param node_2_marginalize indicates which node(s) shall be marginalized:
248
* - 0 means that X should be marginalized
249
* - 1 means that Y should be marginalized
250
* - 2 means that Z should be marginalized
251
*/
252
template
<
template
<
typename
>
class
ALLOC
>
253
std
::
vector
<
double
,
ALLOC
<
double
> >
PseudoCount
<
ALLOC
>::
get
(
254
const
std
::
vector
<
NodeId
,
ALLOC
<
NodeId
> >&
ids
) {
255
IdCondSet
<
ALLOC
>
idset
(
ids
,
false
,
true
);
256
std
::
vector
<
double
,
ALLOC
<
double
> >
N_xyz
(
257
this
->
counter_
.
counts
(
idset
,
true
));
258
const
bool
informative_external_apriori
=
this
->
apriori_
->
isInformative
();
259
if
(
informative_external_apriori
)
260
this
->
apriori_
->
addAllApriori
(
idset
,
N_xyz
);
261
return
N_xyz
;
262
}
263
264
}
/* namespace learning */
265
266
}
/* namespace gum */
267
268
#
endif
/* DOXYGEN_SHOULD_SKIP_THIS */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669
gum::learning::genericBNLearner::Database::Database
Database(const std::string &filename, const BayesNet< GUM_SCALAR > &bn, const std::vector< std::string > &missing_symbols)
Definition:
genericBNLearner_tpl.h:31