-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
FeaturesUtil.cpp
149 lines (136 loc) · 5.33 KB
/
FeaturesUtil.cpp
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
/*
* Copyright (c) The Shogun Machine Learning Toolbox
* Written (w) 2016 Soumyajit De
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the Shogun Development Team.
*/
#include <stack>
#include <algorithm>
#include <shogun/io/SGIO.h>
#include <shogun/lib/SGMatrix.h>
#include <shogun/lib/SGVector.h>
#include <shogun/features/Features.h>
#include <shogun/features/FeatureTypes.h>
#include <shogun/features/Subset.h>
#include <shogun/features/SubsetStack.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/statistical_testing/internals/FeaturesUtil.h>
using namespace shogun;
using namespace internal;
CFeatures* FeaturesUtil::create_shallow_copy(CFeatures* other)
{
SG_SDEBUG("Entering!\n");
CFeatures* shallow_copy=nullptr;
if (other->get_feature_type()==F_DREAL && other->get_feature_class()==C_DENSE)
{
auto casted=static_cast<CDenseFeatures<float64_t>*>(other);
// use the same underlying feature matrix, no ref-count
int32_t num_feats=0, num_vecs=0;
float64_t* data=casted->get_feature_matrix(num_feats, num_vecs);
SG_SDEBUG("Using underlying feature matrix with %d dimensions and %d feature vectors!\n", num_feats, num_vecs);
SGMatrix<float64_t> feats_matrix(data, num_feats, num_vecs, false);
shallow_copy=new CDenseFeatures<float64_t>(feats_matrix);
clone_subset_stack(other, shallow_copy);
}
else
SG_SNOTIMPLEMENTED;
SG_SDEBUG("Leaving!\n");
return shallow_copy;
}
CFeatures* FeaturesUtil::create_merged_copy(CFeatures* feats_a, CFeatures* feats_b)
{
SG_SDEBUG("Entering!\n");
REQUIRE(feats_a->get_feature_type()==feats_b->get_feature_type(),
"The feature types of the underlying feature objects should be same!\n");
REQUIRE(feats_a->get_feature_class()==feats_b->get_feature_class(),
"The feature classes of the underlying feature objects should be same!\n");
CFeatures* merged_copy=nullptr;
if (feats_a->get_feature_type()==F_DREAL && feats_a->get_feature_class()==C_DENSE)
{
auto casted_a=static_cast<CDenseFeatures<float64_t>*>(feats_a);
auto casted_b=static_cast<CDenseFeatures<float64_t>*>(feats_b);
REQUIRE(casted_a->get_num_features()==casted_b->get_num_features(),
"The number of features from a (%d) has to be equal with that of b (%d)!\n",
casted_a->get_num_features(), casted_b->get_num_features());
SGMatrix<float64_t> data_a=casted_a->get_feature_matrix();
SGMatrix<float64_t> data_b=casted_b->get_feature_matrix();
ASSERT(data_a.num_rows==data_b.num_rows);
SGMatrix<float64_t> merged(data_a.num_rows, data_a.num_cols+data_b.num_cols);
std::copy(data_a.data(), data_a.data()+data_a.size(), merged.data());
std::copy(data_b.data(), data_b.data()+data_b.size(), merged.data()+data_a.size());
merged_copy=new CDenseFeatures<float64_t>(merged);
}
else
SG_SNOTIMPLEMENTED;
SG_SDEBUG("Leaving!\n");
return merged_copy;
}
void FeaturesUtil::clone_subset_stack(CFeatures* src, CFeatures* dst)
{
SG_SDEBUG("Entering!\n");
CSubsetStack* src_subset_stack=src->get_subset_stack();
if (src_subset_stack->has_subsets())
{
SG_SDEBUG("Subset present, cloning the subsets!\n");
CSubsetStack* subset_stack=static_cast<CSubsetStack*>(src_subset_stack->clone());
std::stack<SGVector<index_t>> stack;
while (subset_stack->has_subsets())
{
stack.push(subset_stack->get_last_subset()->get_subset_idx());
subset_stack->remove_subset();
}
SG_UNREF(subset_stack);
SG_SDEBUG("Number of subsets to be added is %d!\n", stack.size());
if (stack.size()>1)
{
SGVector<index_t> ref=stack.top();
dst->add_subset(ref);
stack.pop();
do
{
SGVector<index_t> inds=stack.top();
for (auto i=0, j=0; i<ref.size() && j<inds.size(); ++i)
{
if (ref[i]==inds[j])
inds[j++]=i;
}
dst->add_subset(inds);
inds=ref;
stack.pop();
} while (!stack.empty());
}
else
{
while (!stack.empty())
{
dst->add_subset(stack.top());
stack.pop();
}
}
}
SG_UNREF(src_subset_stack);
SG_SDEBUG("Leaving!\n");
}