-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
StreamingDenseFeatures_unittest.cc
124 lines (96 loc) · 3.35 KB
/
StreamingDenseFeatures_unittest.cc
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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2013 Viktor Gal
*/
#include <shogun/features/streaming/StreamingDenseFeatures.h>
#include <shogun/io/CSVFile.h>
#include <shogun/io/streaming/StreamingAsciiFile.h>
#include <unistd.h>
#include <gtest/gtest.h>
using namespace shogun;
TEST(StreamingDenseFeaturesTest, example_reading_from_file)
{
index_t n=20;
index_t dim=2;
std::string tmp_name = "/tmp/StreamingDenseFeatures_reading.XXXXXX";
char* fname = mktemp(const_cast<char*>(tmp_name.c_str()));
SGMatrix<float64_t> data(dim,n);
for (index_t i=0; i<dim*n; ++i)
data.matrix[i] = sg_rand->std_normal_distrib();
CDenseFeatures<float64_t>* orig_feats=new CDenseFeatures<float64_t>(data);
CCSVFile* saved_features = new CCSVFile(fname, 'w');
orig_feats->save(saved_features);
saved_features->close();
SG_UNREF(saved_features);
CStreamingAsciiFile* input = new CStreamingAsciiFile(fname);
input->set_delimiter(',');
CStreamingDenseFeatures<float64_t>* feats
= new CStreamingDenseFeatures<float64_t>(input, false, 5);
index_t i = 0;
feats->start_parser();
while (feats->get_next_example())
{
SGVector<float64_t> example = feats->get_vector();
SGVector<float64_t> expected = orig_feats->get_feature_vector(i);
ASSERT_EQ(dim, example.vlen);
for (index_t j = 0; j < dim; j++)
EXPECT_NEAR(expected.vector[j], example.vector[j], 1E-5);
feats->release_example();
i++;
}
feats->end_parser();
SG_UNREF(orig_feats);
SG_UNREF(feats);
int delete_success = unlink(fname);
ASSERT_EQ(0, delete_success);
}
TEST(StreamingDenseFeaturesTest, example_reading_from_features)
{
index_t n=20;
index_t dim=2;
SGMatrix<float64_t> data(dim,n);
for (index_t i=0; i<dim*n; ++i)
data.matrix[i] = sg_rand->std_normal_distrib();
CDenseFeatures<float64_t>* orig_feats=new CDenseFeatures<float64_t>(data);
CStreamingDenseFeatures<float64_t>* feats = new CStreamingDenseFeatures<float64_t>(orig_feats);
index_t i = 0;
feats->start_parser();
while (feats->get_next_example())
{
SGVector<float64_t> example = feats->get_vector();
SGVector<float64_t> expected = orig_feats->get_feature_vector(i);
ASSERT_EQ(dim, example.vlen);
for (index_t j = 0; j < dim; j++)
EXPECT_DOUBLE_EQ(expected.vector[j], example.vector[j]);
feats->release_example();
i++;
}
feats->end_parser();
SG_UNREF(feats);
}
TEST(StreamingDenseFeaturesTest, reset_stream)
{
index_t n=20;
index_t dim=2;
SGMatrix<float64_t> data(dim,n);
for (index_t i=0; i<dim*n; ++i)
data.matrix[i]=sg_rand->std_normal_distrib();
CDenseFeatures<float64_t>* orig_feats=new CDenseFeatures<float64_t>(data);
CStreamingDenseFeatures<float64_t>* feats=new CStreamingDenseFeatures<float64_t>(orig_feats);
feats->start_parser();
CDenseFeatures<float64_t>* streamed=dynamic_cast<CDenseFeatures<float64_t>*>(feats->get_streamed_features(n));
ASSERT_TRUE(streamed!=nullptr);
ASSERT_TRUE(orig_feats->equals(streamed));
SG_UNREF(streamed);
feats->reset_stream();
streamed=dynamic_cast<CDenseFeatures<float64_t>*>(feats->get_streamed_features(n));
ASSERT_TRUE(streamed!=nullptr);
ASSERT_TRUE(orig_feats->equals(streamed));
SG_UNREF(streamed);
feats->end_parser();
SG_UNREF(feats);
}