nodes_value_containers.hpp 16.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
/** @file feature_creation/node/value_storage/node_value_containers.hpp
 *  @brief Centeral storage area for all descriptor matrix data
 *
 *  This file contains definitions where all descriptor/feature data is stored permanently or in temporary storage arrays
 *  All Nodes's (except ModelNodes) value_ptr/test_value_ptrs point to data here.
 *
 *  @author Thomas A. R. Purcell (tpurcell)
 *  @bug No known bugs.
 */
10
11
#ifndef NODE_VALUE_ARR
#define NODE_VALUE_ARR
Thomas Purcell's avatar
Thomas Purcell committed
12

Thomas Purcell's avatar
Thomas Purcell committed
13
#include <algorithm>
14
#include <cmath>
Thomas Purcell's avatar
Thomas Purcell committed
15
#include <memory>
Thomas Purcell's avatar
Thomas Purcell committed
16
#include <numeric>
Thomas Purcell's avatar
Thomas Purcell committed
17
#include <vector>
18
#include <iostream>
Thomas Purcell's avatar
Thomas Purcell committed
19

20
21
#include <omp.h>

Thomas Purcell's avatar
Thomas Purcell committed
22
#include"utils/enum.hpp"
Thomas Purcell's avatar
Thomas Purcell committed
23

24
#ifdef PY_BINDINGS
25
#include "python/py_binding_cpp_def/conversion_utils.hpp"
26
27
28
29
namespace np = boost::python::numpy;
namespace py = boost::python;
#endif

Thomas Purcell's avatar
Thomas Purcell committed
30
31
namespace node_value_arrs
{
32
33
34
35
    extern std::vector<double> TEMP_STORAGE_ARR; //!< Array to temporarily store feature training data
    extern std::vector<double> VALUES_ARR; //!< Training data of the stored features
    extern std::vector<double> TEMP_STORAGE_TEST_ARR; //!< Array to temporarily store feature test data
    extern std::vector<double> TEST_VALUES_ARR; //!< Value of the stored features test data
Thomas Purcell's avatar
Thomas Purcell committed
36
    extern std::vector<double> D_MATRIX; //!< The descriptor matrix
37
38
39
    extern std::vector<double> PARAM_STORAGE_ARR; //!< Array to store parameterized feature values
    extern std::vector<double> PARAM_STORAGE_TEST_ARR; //!< Array to store parameterized feature values for the test set

40
41
    extern std::vector<int> TEMP_STORAGE_REG; //!< Register to see which feature is stored in each slot for the training data
    extern std::vector<int> TEMP_STORAGE_TEST_REG; //!< Register to see which feature is stored in each slot for the test data
Thomas Purcell's avatar
Thomas Purcell committed
42
43
44
    extern std::vector<int> TASK_SZ_TRAIN; //!< Number of training samples per task
    extern std::vector<int> TASK_SZ_TEST; //!< Number of test sample per task

45
    extern int N_SELECTED; //!< Number of features selected
46
47
    extern int N_SAMPLES; //!< Number of training samples for each feature
    extern int N_SAMPLES_TEST; //!< Number of test samples for each feature
Thomas Purcell's avatar
Thomas Purcell committed
48
    extern int N_PRIMARY_FEATURES; //!< Number of primary features with stored values
Thomas Purcell's avatar
Thomas Purcell committed
49
50
    extern int N_STORE_FEATURES; //!< Number of features with stored values
    extern int N_RUNGS_STORED; //!< Number of rungs with values stored
51
    extern int MAX_N_THREADS; //!< Get the maximum number of threads possible
52
    extern int N_OP_SLOTS; //!< The number of possible operator slots
53
    extern int N_PARAM_OP_SLOTS; //!< The maximum number of possible operator slots
54
    extern int MAX_RUNG; //!< The maximum rung for all features
Thomas Purcell's avatar
Thomas Purcell committed
55

Thomas Purcell's avatar
Thomas Purcell committed
56
    /**
57
58
     * @brief Initialize the node value arrays
     * @details Using the size of the initial feature space constructor the storage arrays
Thomas Purcell's avatar
Thomas Purcell committed
59
     *
60
61
62
     * @param n_samples Number of training samples for each feature
     * @param n_samples_test Number of test samples for each feature
     * @param n_primary_feat Number of primary features
63
     * @param max_rung Largest rung of a feature
Thomas Purcell's avatar
Thomas Purcell committed
64
     * @param set_test_task_sz If True reset the task_sz vectors
65
     * @param use_params If True set up parameter storage
Thomas Purcell's avatar
Thomas Purcell committed
66
     */
67
68
69
70
71
72
73
74
    void initialize_values_arr(
        const int n_samples,
        const int n_samples_test,
        const int n_primary_feat,
        const int max_rung,
        const bool set_task_sz,
        const bool use_params
    );
Thomas Purcell's avatar
Thomas Purcell committed
75

76
    // DocString: node_vals_init_no_ts
Thomas Purcell's avatar
Thomas Purcell committed
77
78
79
80
81
82
83
    /**
     * @brief Initialize the node value arrays
     * @details Using the size of the initial feature space constructor the storage arrays
     *
     * @param n_samples Number of training samples for each feature
     * @param n_samples_test Number of test samples for each feature
     * @param n_primary_feat Number of primary features
84
     * @param max_rung Largest rung of a feature
Thomas Purcell's avatar
Thomas Purcell committed
85
     */
86
87
88
89
90
91
    inline void initialize_values_arr(
        const int n_samples,
        const int n_samples_test,
        const int n_primary_feat,
        const int max_rung
    )
Thomas Purcell's avatar
Thomas Purcell committed
92
    {
93
        initialize_values_arr(n_samples, n_samples_test, n_primary_feat, max_rung, true, false);
Thomas Purcell's avatar
Thomas Purcell committed
94
95
96
97
98
99
100
101
102
    }

    /**
     * @brief Initialize the node value arrays
     * @details Using the size of the initial feature space constructor the storage arrays
     *
     * @param task_sz_train Number of training samples per task
     * @param task_sz_test Number of test sample per task
     * @param n_primary_feat Number of primary features
103
     * @param max_rung Largest rung of a feature
104
     * @param use_params If True set up parameter storage
Thomas Purcell's avatar
Thomas Purcell committed
105
     */
106
107
108
109
110
111
112
    void initialize_values_arr(
        const std::vector<int> task_sz_train,
        const std::vector<int> task_sz_test,
        const int n_primary_feat,
        const int max_rung,
        const bool use_params
    );
Thomas Purcell's avatar
Thomas Purcell committed
113
114

    /**
115
116
     * @brief Resize the node value arrays
     * @details Using the updated number of features increase the size of the storage arrays
Thomas Purcell's avatar
Thomas Purcell committed
117
118
119
120
121
     *
     * @param n_dims Number of dimensions to store
     * @param n_feat number of features to store
     * @param use_temp If true keep the temporary_storage
     */
Thomas Purcell's avatar
Thomas Purcell committed
122
    void resize_values_arr(const int n_dims, const int n_feat);
Thomas Purcell's avatar
Thomas Purcell committed
123

124
125
126
127
128
    /**
     * @brief Initialize the parameter storage array
     */
    void initialize_param_storage();

129
    // DocString: node_vlas_init_d_mat
130
    /**
131
132
     * @brief Initialize the descriptor matrix
     * @details Initialize an empty descriptor matrix
133
134
135
136
137
     *
     */
    void initialize_d_matrix_arr();

    /**
138
139
     * @brief Resize the descriptor matrix
     * @details Using the number of newly selected features increase the size of the descriptor matrix
140
141
142
     *
     * @param n_select Number of features to select
     */
143
    void resize_d_matrix_arr(const int n_select);
144

Thomas Purcell's avatar
Thomas Purcell committed
145
146
147
148
149
    /**
     * @brief Reset the global TASK_SZ_TRAIN vector
     *
     * @param task_sz_train the new task_sz train
     */
150
    void set_task_sz_train(const std::vector<int> task_sz_train);
Thomas Purcell's avatar
Thomas Purcell committed
151
152
153
154
155
156

    /**
     * @brief Reset the global TASK_SZ_TEST vector
     *
     * @param task_sz_train the new test_sz train
     */
157
    void set_task_sz_test(const std::vector<int> task_sz_test);
Thomas Purcell's avatar
Thomas Purcell committed
158

159
160
161
162
163
    /**
     * @brief Get the operator slot associated with a given rung/offset
     *
     * @param rung Rung of the feature
     * @param offset Offset used to prevent overwrites
164
     * @param for_comp If true get a slot dedicated to comparing features
165
166
167
     *
     * @return The operator slot to use
     */
168
    inline int get_op_slot(const int rung, const int offset, const bool for_comp)
169
170
171
    {
        return std::abs(N_OP_SLOTS / (1 + !for_comp) - static_cast<int>(std::pow(2, MAX_RUNG - rung)) - offset);
    }
172

173
174
175
176
177
178
179
180
181
182
183
184
185
186
    /**
     * @brief Get the parameter operator slot associated with a given rung/offset
     *
     * @param rung Rung of the feature
     * @param offset Offset used to prevent overwrites
     * @param for_comp If true get a slot dedicated to comparing features
     *
     * @return The operator slot to use
     */
    inline int get_param_op_slot(const int rung, const int offset, const bool for_comp)
    {
        return std::abs(N_PARAM_OP_SLOTS / (1 + !for_comp) - static_cast<int>(std::pow(2, MAX_RUNG - rung)) - offset);
    }

Thomas Purcell's avatar
Thomas Purcell committed
187
    /**
188
     * @brief Get a reference slot/feature register of the training data
Thomas Purcell's avatar
Thomas Purcell committed
189
     *
190
     * @param ind The Node's arr_ind
191
192
193
194
     * @param op_slot(int) Offset integer for TEMP_STORE_ARRAY
     *
     * @return The register element for a given feature index and op_slot
     */
195
    inline int& temp_storage_reg(const unsigned long int ind, const int op_slot=0)
196
197
    {
        return TEMP_STORAGE_REG[
Thomas Purcell's avatar
Thomas Purcell committed
198
            (ind % N_PRIMARY_FEATURES) + (op_slot % N_OP_SLOTS) * N_PRIMARY_FEATURES + omp_get_thread_num() * (N_PRIMARY_FEATURES * N_OP_SLOTS + 1)
199
200
        ];
    }
201
202
203
204
205
206
207
208
209

    /**
     * @brief Get a reference slot/feature register of the test data
     *
     * @param ind The Node's arr_ind
     * @param op_slot(int) Offset integer for TEMP_STORE_TEST_ARRAY
     *
     * @return The register element for a given feature index and op_slot
     */
210
    inline int& temp_storage_test_reg(const unsigned long int ind, const int op_slot=0)
211
212
    {
        return TEMP_STORAGE_TEST_REG[
Thomas Purcell's avatar
Thomas Purcell committed
213
            (ind % N_PRIMARY_FEATURES) + (op_slot % N_OP_SLOTS) * N_PRIMARY_FEATURES + omp_get_thread_num() * (N_PRIMARY_FEATURES * N_OP_SLOTS + 1)
214
215
        ];
    }
216

Thomas Purcell's avatar
Thomas Purcell committed
217
    /**
218
     * @brief Get a reference slot/feature register of the training data
Thomas Purcell's avatar
Thomas Purcell committed
219
     *
220
     * @param ind The Node's arr_ind
221
222
     * @param rung Rung of the feature
     * @param offset Offset used to prevent overwrites
223
     * @param for_comp If true get a slot dedicated to comparing features
Thomas Purcell's avatar
Thomas Purcell committed
224
225
226
     *
     * @return The register element for a given feature index and offset
     */
227
    inline int& temp_storage_reg(const unsigned long int ind, const int rung, const int offset, const bool for_comp)
228
229
    {
        return TEMP_STORAGE_REG[
Thomas Purcell's avatar
Thomas Purcell committed
230
231
232
            (ind % N_PRIMARY_FEATURES) +
            (get_op_slot(rung, offset, for_comp) % N_OP_SLOTS) * N_PRIMARY_FEATURES +
            omp_get_thread_num() * (N_PRIMARY_FEATURES * N_OP_SLOTS + 1)
233
234
        ];
    }
Thomas Purcell's avatar
Thomas Purcell committed
235

236
    /**
237
     * @brief Get a reference slot/feature register of the test data
238
     *
239
     * @param ind The Node's arr_ind
240
241
     * @param rung Rung of the feature
     * @param offset Offset used to prevent overwrites
242
     * @param for_comp If true get a slot dedicated to comparing features
243
244
245
     *
     * @return The register element for a given feature index and offset
     */
246
    inline int& temp_storage_test_reg(const unsigned long int ind, const int rung, const int offset, const bool for_comp)
247
248
    {
        return TEMP_STORAGE_TEST_REG[
Thomas Purcell's avatar
Thomas Purcell committed
249
            (ind % N_PRIMARY_FEATURES) +
Thomas Purcell's avatar
Thomas Purcell committed
250
251
            (get_op_slot(rung, offset, for_comp) % N_OP_SLOTS) * N_PRIMARY_FEATURES +
            omp_get_thread_num() * (N_PRIMARY_FEATURES * N_OP_SLOTS + 1)
252
253
        ];
    }
254

Thomas Purcell's avatar
Thomas Purcell committed
255
    /**
256
     * @brief Access element of the permanent training data storage array
Thomas Purcell's avatar
Thomas Purcell committed
257
     *
258
     * @param feature_ind The _arr_ind of Node to get the training data of
Thomas Purcell's avatar
Thomas Purcell committed
259
     *
260
     * @return pointer to the Node's training data
Thomas Purcell's avatar
Thomas Purcell committed
261
     */
262
    inline double* access_value_arr(const unsigned long int feature_ind){return &VALUES_ARR[feature_ind*N_SAMPLES];}
263

264
    /**
265
     * @brief Access element of the permanent test data storage array
266
     *
267
     * @param feature_ind The _arr_ind of Node to get the test data of
268
     *
269
     * @return pointer to the Node's test data
270
     */
271
    inline double* access_test_value_arr(const unsigned long int feature_ind){return &TEST_VALUES_ARR[feature_ind*N_SAMPLES_TEST];}
272

Thomas Purcell's avatar
Thomas Purcell committed
273
    /**
274
     * @brief Access element of temporary storage array for the training data
Thomas Purcell's avatar
Thomas Purcell committed
275
     *
276
     * @param slot The slot of the temporary storage array
Thomas Purcell's avatar
Thomas Purcell committed
277
     *
278
     * @return pointer to the data stored in the specified slot
Thomas Purcell's avatar
Thomas Purcell committed
279
     */
280
    inline double* access_temp_storage(const unsigned long int slot){return &TEMP_STORAGE_ARR[slot*N_SAMPLES];}
281

282
    /**
283
     * @brief Access element of temporary storage array for the test data
284
     *
285
     * @param slot The slot of the temporary storage array
286
     *
287
     * @return pointer to the data stored in the specified slot
288
     */
289
    inline double* access_temp_storage_test(const unsigned long int slot){return &TEMP_STORAGE_TEST_ARR[slot*N_SAMPLES_TEST];}
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
    /**
     * @brief Access the param storage array
     *
     * @param rung Rung of the feature
     * @param offset(int) the offset for temporary storage access
     * @param for_comp If true get a slot dedicated to comparing features
     * @return pointer to the correct element of the PARAM_STORAGE_ARR
     */
    inline double* access_param_storage(const int rung=0, const int offset=0, const bool for_comp=false)
    {
        return &PARAM_STORAGE_ARR[
            N_SAMPLES *
            (
                (get_param_op_slot(rung, offset, for_comp) % N_PARAM_OP_SLOTS) +
                omp_get_thread_num() * (N_PARAM_OP_SLOTS + 1)
            )
        ];
    }

    /**
     * @brief Access the param storage array for the test set
     *
     * @param rung Rung of the feature
     * @param offset(int) the offset for temporary storage access
     * @param for_comp If true get a slot dedicated to comparing features
     * @return pointer to the correct element of the PARAM_STORAGE_ARR
     */
    inline double* access_param_storage_test(const int rung=0, const int offset=0, const bool for_comp=false)
    {
        return &PARAM_STORAGE_TEST_ARR[
            N_SAMPLES_TEST *
            (
                (get_param_op_slot(rung, offset, for_comp) % N_PARAM_OP_SLOTS) +
                omp_get_thread_num() * (N_PARAM_OP_SLOTS + 1)
            )
        ];
    }

Thomas Purcell's avatar
Thomas Purcell committed
329
    /**
330
     * @brief Get a Node's value_ptr
Thomas Purcell's avatar
Thomas Purcell committed
331
     *
332
333
     * @param arr_ind Nodes _arr_ind
     * @param feat_ind Nodes _feat_ind
334
     * @param rung Rung of the feature
335
     * @param offset(int) the offset for temporary storage access
336
     * @param for_comp If true get a slot dedicated to comparing features
Thomas Purcell's avatar
Thomas Purcell committed
337
338
339
     *
     * @return The value pointer
     */
340
341
342
343
344
    double* get_value_ptr(
        const unsigned long int arr_ind,
        const unsigned long int feat_ind,
        const int rung=0,
        const int offset=0,
345
        const bool for_comp=false
346
    );
Thomas Purcell's avatar
Thomas Purcell committed
347

348
    /**
349
     * @brief Get a Node's test_value_ptr
350
     *
351
352
     * @param arr_ind Nodes _arr_ind
     * @param feat_ind Nodes _feat_ind
353
     * @param rung Rung of the feature
354
     * @param offset(int) the offset for temporary storage access
355
     * @param for_comp If true get a slot dedicated to comparing features
356
357
358
     *
     * @return The value pointer
     */
359
360
361
362
363
    double* get_test_value_ptr(
        const unsigned long int arr_ind,
        const unsigned long int feat_ind,
        const int rung=0,
        const int offset=0,
364
        const bool for_comp=false
365
    );
366

367
    /**
368
     * @brief Get the pointer to a particular selected Node from sis
369
     *
370
     * @param ind Index of the data in the descriptor matrix
371
     * @return The pointer to the descriptor matrix's data
372
     */
373
    inline double* get_d_matrix_ptr(const int ind){return &D_MATRIX[ind * N_SAMPLES];}
Thomas Purcell's avatar
Bug Fix    
Thomas Purcell committed
374

375
376
377
378
    /**
     * @brief Flush the temporary storage register (training data)
     * @details Reset all slots in the register to -1
     */
379
    inline void clear_temp_reg(){std::fill_n(TEMP_STORAGE_REG.begin(), TEMP_STORAGE_REG.size(), -1);}
380

381
382
383
384
    /**
     * @brief Flush the temporary storage register (training data)
     * @details Reset all slots in the register to -1
     */
385
386
    inline void clear_temp_reg_thread()
    {
Thomas Purcell's avatar
Thomas Purcell committed
387
        std::fill_n(TEMP_STORAGE_REG.begin() + (N_PRIMARY_FEATURES * N_OP_SLOTS + 1) * omp_get_thread_num(), N_PRIMARY_FEATURES * N_OP_SLOTS + 1, -1);
388
    }
389

390
391
392
393
    /**
     * @brief Flush the temporary storage register (test data)
     * @details Reset all slots in the register to -1
     */
394
    inline void clear_temp_test_reg(){std::fill_n(TEMP_STORAGE_TEST_REG.begin(), TEMP_STORAGE_TEST_REG.size(), -1);}
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
#ifdef PY_BINDINGS

    // DocString: node_vals_ts_list
    /**
     * @brief Initialize the node value arrays
     * @details Using the size of the initial feature space constructor the storage arrays
     *
     * @param task_sz_train Number of training samples per task
     * @param task_sz_test Number of test sample per task
     * @param n_primary_feat Number of primary features
     * @param max_rung Largest rung of a feature
     */
    inline void initialize_values_arr(
        py::list task_sz_train,
        py::list task_sz_test,
        int n_primary_feat,
        int max_rung
    )
    {
        initialize_values_arr(
            python_conv_utils::from_list<int>(task_sz_train),
            python_conv_utils::from_list<int>(task_sz_test),
            n_primary_feat,
            max_rung,
            false
        );
    }

    // DocString: node_vals_ts_arr
    /**
     * @brief Initialize the node value arrays
     * @details Using the size of the initial feature space constructor the storage arrays
     *
     * @param task_sz_train Number of training samples per task
     * @param task_sz_test Number of test sample per task
     * @param n_primary_feat Number of primary features
     * @param max_rung Largest rung of a feature
     */
    inline void initialize_values_arr(
        np::ndarray task_sz_train,
        np::ndarray task_sz_test,
        int n_primary_feat,
        int max_rung
    )
    {
        initialize_values_arr(
            python_conv_utils::from_ndarray<int>(task_sz_train),
            python_conv_utils::from_ndarray<int>(task_sz_test),
            n_primary_feat,
            max_rung,
            false
        );
    }
#endif
Thomas Purcell's avatar
Thomas Purcell committed
450
451
}

452
#endif