File: sammon.cpp

package info (click to toggle)
mldemos 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,224 kB
  • ctags: 46,525
  • sloc: cpp: 306,887; ansic: 167,718; ml: 126; sh: 109; makefile: 2
file content (211 lines) | stat: -rw-r--r-- 4,917 bytes parent folder | download | duplicates (5)
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
// Copyright (C) 2012  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.


#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <dlib/statistics.h>

#include "tester.h"

namespace  
{

    using namespace test;
    using namespace dlib;
    using namespace std;

    logger dlog("test.sammon");


    std::vector<matrix<double,4,1> > make_test_data4(
    )
    {
        std::vector<matrix<double,4,1> > data;

        matrix<double,4,1> m;

        m = 0,0,0, 0; data.push_back(m);
        m = 1,0,0, 0; data.push_back(m);
        m = 0,1,0, 0; data.push_back(m);
        m = 0,0,1, 0; data.push_back(m);

        return data;
    }

    std::vector<matrix<double,3,1> > make_test_data3(
    )
    {
        std::vector<matrix<double,3,1> > data;

        matrix<double,3,1> m;

        m = 0,0,0; data.push_back(m);
        m = 1,0,0; data.push_back(m);
        m = 0,1,0; data.push_back(m);
        m = 0,0,1; data.push_back(m);

        return data;
    }

    std::vector<matrix<double> > make_test_data3d(
    )
    {
        std::vector<matrix<double> > data;

        matrix<double,3,1> m;

        m = 0,0,0; data.push_back(m);
        m = 1,0,0; data.push_back(m);
        m = 0,1,0; data.push_back(m);
        m = 0,0,1; data.push_back(m);

        return data;
    }


    void runtest()
    {
        sammon_projection s;
        std::vector<matrix<double, 0, 1> >  projs = s(make_test_data3(),2);
        running_stats<double> rs1, rs2;

        rs1.add(length(projs[0] - projs[1]));
        rs1.add(length(projs[0] - projs[2]));
        rs1.add(length(projs[0] - projs[3]));

        rs2.add(length(projs[1] - projs[2]));
        rs2.add(length(projs[2] - projs[3]));
        rs2.add(length(projs[3] - projs[1]));

        DLIB_TEST(rs1.stddev()/rs1.mean() < 1e-4);
        DLIB_TEST(rs2.stddev()/rs2.mean() < 1e-4);



        projs = s(make_test_data4(),2);
        rs1.clear();
        rs2.clear();

        rs1.add(length(projs[0] - projs[1]));
        rs1.add(length(projs[0] - projs[2]));
        rs1.add(length(projs[0] - projs[3]));

        rs2.add(length(projs[1] - projs[2]));
        rs2.add(length(projs[2] - projs[3]));
        rs2.add(length(projs[3] - projs[1]));

        DLIB_TEST(rs1.stddev()/rs1.mean() < 1e-4);
        DLIB_TEST(rs2.stddev()/rs2.mean() < 1e-4);

        projs = s(make_test_data3d(),2);
        rs1.clear();
        rs2.clear();

        rs1.add(length(projs[0] - projs[1]));
        rs1.add(length(projs[0] - projs[2]));
        rs1.add(length(projs[0] - projs[3]));

        rs2.add(length(projs[1] - projs[2]));
        rs2.add(length(projs[2] - projs[3]));
        rs2.add(length(projs[3] - projs[1]));

        DLIB_TEST(rs1.stddev()/rs1.mean() < 1e-4);
        DLIB_TEST(rs2.stddev()/rs2.mean() < 1e-4);
    }

    void runtest2()
    {
        sammon_projection s;
        std::vector<matrix<double, 0, 1> >  projs, temp;

        DLIB_TEST(s(projs,3).size() == 0);

        matrix<double,2,1> m;
        m = 1,2;
        projs.push_back(m);
        temp = s(projs,2);
        DLIB_TEST(temp.size() == 1);
        DLIB_TEST(temp[0].size() == 2);

        projs.push_back(m);
        temp = s(projs,1);
        DLIB_TEST(temp.size() == 2);
        DLIB_TEST(temp[0].size() == 1);
        DLIB_TEST(temp[1].size() == 1);
    }

    void runtest3(int num_dims)
    {
        sammon_projection s;
        std::vector<matrix<double, 0, 1> >  projs;
        matrix<double,3,1> m;
        m = 1, 1, 1;
        projs.push_back(m);

        m = 1, 2, 1;
        projs.push_back(m);

        m = 1, 3, 1;
        projs.push_back(m);

        projs = s(projs,num_dims);

        const double d1a = length(projs[0] - projs[1]);
        const double d1b = length(projs[1] - projs[2]);
        const double d2  = length(projs[0] - projs[2]);

        DLIB_TEST(std::abs(d1a-d1b)/d1a < 1e-8);
        DLIB_TEST(std::abs(d2/d1a-2) < 1e-8);
    }

    void runtest4(int num_dims)
    {
        sammon_projection s;
        std::vector<matrix<double, 0, 1> >  projs;
        matrix<double,3,1> m;
        m = 1, 1, 1;
        projs.push_back(m);

        m = 1, 2, 1;
        projs.push_back(m);


        projs = s(projs,num_dims);

        DLIB_TEST(length(projs[0] - projs[1]) > 1e-5); 
    }

    class sammon_tester : public tester
    {
    public:
        sammon_tester (
        ) :
            tester ("test_sammon",
                    "Runs tests on the sammon_projection component.")
        {}

        void perform_test (
        )
        {
            print_spinner();
            runtest();
            print_spinner();
            runtest2();
            print_spinner();
            runtest3(2);
            print_spinner();
            runtest4(2);
            runtest3(1);
            print_spinner();
            runtest4(1);
        }
    } a;

}