aboutsummaryrefslogtreecommitdiff
path: root/main.cpp
blob: 5f7693d798a6664f4c1a6183521fb74f8db7b1fa (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
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
450
451
452
453
454
#include <cmath>
#include <iostream>
#include <numeric>
#include <random>
#include <string>

#include <QApplication>
#include <QtQml>
#include <QQmlApplicationEngine>
#include <QSurfaceFormat>

#include "main.h"
#include "mp.h"
#include "continuouscolorscale.h"
#include "scatterplot.h"
#include "voronoisplat.h"
#include "lineplot.h"
#include "barchart.h"
#include "colormap.h"
#include "transitioncontrol.h"
#include "projectionhistory.h"
#include "manipulationhandler.h"
#include "mapscalehandler.h"
#include "selectionhandler.h"
#include "brushinghandler.h"

static const int RNG_SEED = 123;

static QObject *mainProvider(QQmlEngine *engine, QJSEngine *scriptEngine)
{
    Q_UNUSED(engine)
    Q_UNUSED(scriptEngine)

    return Main::instance();
}

arma::uvec extractCPs(const arma::mat &X)
{
    int numCPs = (int) (3 * sqrt(X.n_rows));
    arma::uvec indices(X.n_rows);
    std::iota(indices.begin(), indices.end(), 0);
    indices = arma::shuffle(indices);
    return indices.subvec(0, numCPs-1);
}

arma::mat standardize(const arma::mat &X)
{
    arma::mat stdX = X;
    for (arma::uword j = 0; j < X.n_cols; j++) {
        double sd = arma::stddev(stdX.col(j));
        double mean = arma::mean(stdX.col(j));
        stdX.col(j) = (stdX.col(j) - mean) / sd;
    }

    return stdX;
}

void overviewBundles(const Main *m)
{
    const arma::mat &unreliability = m->projectionHistory->unreliability();
    arma::uvec indicesLargest = arma::sort_index(unreliability, "descending");
    arma::uword numLargest = m->projectionHistory->Y().n_rows * 0.1f;
    indicesLargest = indicesLargest.subvec(0, numLargest-1);
    m->bundlePlot->setValues(unreliability(indicesLargest));

    const arma::uvec &cpIndices = m->projectionHistory->cpIndices();
    arma::uvec CPs = cpIndices(indicesLargest / unreliability.n_rows);

    const arma::uvec &rpIndices = m->projectionHistory->rpIndices();
    arma::uvec RPs = indicesLargest;
    RPs.transform([&unreliability](arma::uword v) {
        return v % unreliability.n_rows;
    });
    RPs = rpIndices(RPs);

    arma::uvec indices(CPs.n_elem + RPs.n_elem);
    for (arma::uword i = 0; i < CPs.n_elem; i++) {
        indices[2*i + 0] = CPs(i);
        indices[2*i + 1] = RPs(i);
    }
    m->bundlePlot->setLines(indices, m->projectionHistory->Y());
}

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    app.setApplicationName("pm");
    app.setApplicationVersion("1.0");
    // app.setAttribute(Qt::AA_ShareOpenGLContexts);

    // Command line parser
    QCommandLineParser parser;
    parser.setApplicationDescription("Interactive multidimensional projections.");
    parser.addHelpOption();
    parser.addVersionOption();
    parser.addPositionalArgument("dataset", "Dataset filename (.tbl file)");

    QCommandLineOption indicesFileOutputOption(QStringList() << "i" << "indices",
        "Filename to store the control points' indices. Omitting this option disables saving indices.",
        "filename");
    parser.addOption(indicesFileOutputOption);
    QCommandLineOption cpFileOutputOption(QStringList() << "c" << "cpoints",
        "Filename to store the control points' map. Omitting this option disables saving this map.",
        "filename");
    parser.addOption(cpFileOutputOption);

    parser.process(app);
    QStringList args = parser.positionalArguments();
    if (args.size() != 1) {
        parser.showHelp(1);
    }

    // Load dataset
    Main *m = Main::instance();
    if (!m->loadDataset(args[0].toStdString())) {
        std::cerr << "Could not load dataset.\n";
        return 1;
    }

    const arma::mat &X = standardize(m->X());

    arma::arma_rng::set_seed(RNG_SEED);
    arma::uvec cpIndices;
    arma::mat Ys;

    // Load/generate indices
    QString indicesFilename;
    if (parser.isSet(indicesFileOutputOption)) {
        indicesFilename = parser.value(indicesFileOutputOption);
    }
    QFile indicesFile(indicesFilename);
    if (indicesFile.exists()) {
        m->setIndicesSavePath(indicesFilename);
        cpIndices.load(indicesFilename.toStdString(), arma::raw_ascii);
        cpIndices -= 1;
    } else {
        std::cerr << "No indices file, generating indices...\n";
        cpIndices = extractCPs(X);
    }

    // Load/generate CPs
    QString cpFilename;
    if (parser.isSet(cpFileOutputOption)) {
        cpFilename = parser.value(cpFileOutputOption);
    }
    QFile cpFile(cpFilename);
    if (cpFile.exists()) {
        m->setCPSavePath(cpFilename);
        Ys.load(cpFilename.toStdString(), arma::raw_ascii);
    } else {
        std::cerr << "No CP file, generating initial projection...\n";
        Ys.set_size(cpIndices.n_elem, 2);
        Ys.randu();
        mp::forceScheme(mp::dist(X.rows(cpIndices)), Ys);
    }

    if (cpIndices.n_elem != Ys.n_rows) {
        std::cerr << "The number of CP indices and the CP map do not match." << std::endl;
        return 1;
    }

    // TODO: maybe put this inside m->setCP() method; for now, will be outside
    // for more flexibility
    Ys = arma::normalise(Ys);

    // Sort indices (some operations become easier later)
    arma::uvec cpSortedIndices = arma::sort_index(cpIndices);
    cpIndices = cpIndices(cpSortedIndices);
    Ys = Ys.rows(cpSortedIndices);

    m->setCPIndices(cpIndices);
    m->setCP(Ys);

    // Set up multisampling
    QSurfaceFormat fmt;
    fmt.setRenderableType(QSurfaceFormat::OpenGL);
    //fmt.setVersion(4, 5);
    fmt.setRedBufferSize(8);
    fmt.setGreenBufferSize(8);
    fmt.setBlueBufferSize(8);
    fmt.setAlphaBufferSize(8);
    fmt.setSamples(8);
    QSurfaceFormat::setDefaultFormat(fmt);

    // Register our custom QML types & init QML engine
    qmlRegisterType<Scatterplot>("PM", 1, 0, "Scatterplot");
    qmlRegisterType<BarChart>("PM", 1, 0, "BarChart");
    qmlRegisterType<VoronoiSplat>("PM", 1, 0, "VoronoiSplat");
    qmlRegisterType<LinePlot>("PM", 1, 0, "LinePlot");
    qmlRegisterType<Colormap>("PM", 1, 0, "Colormap");
    qmlRegisterType<TransitionControl>("PM", 1, 0, "TransitionControl");
    qmlRegisterSingletonType<Main>("PM", 1, 0, "Main", mainProvider);
    QQmlApplicationEngine engine(QUrl("qrc:///main_view.qml"));

    // Initialize pointers to visual components
    m->cpPlot = engine.rootObjects()[0]->findChild<Scatterplot *>("cpPlot");
    m->rpPlot = engine.rootObjects()[0]->findChild<Scatterplot *>("rpPlot");
    m->splat = engine.rootObjects()[0]->findChild<VoronoiSplat *>("splat");
    m->bundlePlot = engine.rootObjects()[0]->findChild<LinePlot *>("bundlePlot");
    m->cpColormap = engine.rootObjects()[0]->findChild<Colormap *>("cpColormap");
    m->rpColormap = engine.rootObjects()[0]->findChild<Colormap *>("rpColormap");
    m->cpBarChart = engine.rootObjects()[0]->findChild<BarChart *>("cpBarChart");
    m->rpBarChart = engine.rootObjects()[0]->findChild<BarChart *>("rpBarChart");
    TransitionControl *plotTC = engine.rootObjects()[0]->findChild<TransitionControl *>("plotTC");

    // Shared object which stores modifications to projections
    ProjectionHistory history(X, cpIndices);
    m->projectionHistory = &history;

    // Keep track of the current cp (in order to save them later, if requested)
    QObject::connect(m->cpPlot, &Scatterplot::xyChanged,
            m, &Main::setCP);
    QObject::connect(m->cpPlot, &Scatterplot::xyInteractivelyChanged,
            m, &Main::setCP);

    // Keep both scatterplots, the splat and line plot scaled equally and
    // relative to the full plot
    MapScaleHandler mapScaleHandler;
    QObject::connect(&mapScaleHandler, &MapScaleHandler::scaleChanged,
            m->cpPlot, &Scatterplot::setScale);
    QObject::connect(&mapScaleHandler, &MapScaleHandler::scaleChanged,
            m->rpPlot, &Scatterplot::setScale);
    QObject::connect(&mapScaleHandler, &MapScaleHandler::scaleChanged,
            m->splat, &VoronoiSplat::setScale);
    QObject::connect(&mapScaleHandler, &MapScaleHandler::scaleChanged,
            m->bundlePlot, &LinePlot::setScale);
    QObject::connect(m->projectionHistory, &ProjectionHistory::currentMapChanged,
            &mapScaleHandler, &MapScaleHandler::scaleToMap);

    // Update projection as the cp are modified (either directly in the
    // manipulationHandler object or interactively in cpPlot
    ManipulationHandler manipulationHandler(X, cpIndices);
    QObject::connect(m->cpPlot, &Scatterplot::xyInteractivelyChanged,
            &manipulationHandler, &ManipulationHandler::setCP);

    // Update history whenever a new projection is computed...
    QObject::connect(&manipulationHandler, &ManipulationHandler::mapChanged,
            m->projectionHistory, &ProjectionHistory::addMap);

    // ... and update visual components whenever the history changes
    QObject::connect(m->projectionHistory, &ProjectionHistory::currentMapChanged,
            m, &Main::updateMap);
    QObject::connect(m->projectionHistory, &ProjectionHistory::currentMapChanged,
            [m](const arma::mat &Y) {
                // ... and bundling
                overviewBundles(m);
            });

    // Linking between selections
    SelectionHandler cpSelectionHandler(cpIndices.n_elem);
    QObject::connect(m->cpPlot, &Scatterplot::selectionInteractivelyChanged,
            &cpSelectionHandler, &SelectionHandler::setSelection);
    QObject::connect(m->cpBarChart, &BarChart::selectionInteractivelyChanged,
            &cpSelectionHandler, &SelectionHandler::setSelection);
    QObject::connect(&cpSelectionHandler, &SelectionHandler::selectionChanged,
            m->cpPlot, &Scatterplot::setSelection);
    QObject::connect(&cpSelectionHandler, &SelectionHandler::selectionChanged,
            m->cpBarChart, &BarChart::setSelection);
    QObject::connect(&cpSelectionHandler, &SelectionHandler::selectionChanged,
            [m](const std::vector<bool> &cpSelection) {
                // given some CPs, see unexpected RPs *influenced by* them
                std::vector<arma::uword> selectedCPIndices;
                for (int i = 0; i < cpSelection.size(); i++) {
                    if (cpSelection[i]) {
                        selectedCPIndices.push_back(i);
                    }
                }

                if (selectedCPIndices.empty()) {
                    overviewBundles(m);
                    return;
                }

                arma::uvec selectedCPs(selectedCPIndices.size());
                std::copy(selectedCPIndices.begin(), selectedCPIndices.end(),
                        selectedCPs.begin());

                // Only the 10% largest values, filtered by the selected RPs
                const arma::mat &unreliability = m->projectionHistory->unreliability();
                arma::uvec indicesLargest = arma::sort_index(unreliability.cols(selectedCPs), "descending");
                arma::uword numLargest = indicesLargest.n_elem * 0.01f;
                indicesLargest = indicesLargest.subvec(0, numLargest-1);
                m->bundlePlot->setValues(unreliability(indicesLargest));

                const arma::uvec &cpIndices = m->projectionHistory->cpIndices();
                arma::uvec CPs = cpIndices(selectedCPs(indicesLargest / unreliability.n_rows));

                const arma::uvec &rpIndices = m->projectionHistory->rpIndices();
                arma::uvec RPs = indicesLargest;
                RPs.transform([&unreliability](arma::uword v) {
                    return v % unreliability.n_rows;
                });
                RPs = rpIndices(RPs);

                arma::uvec indices(CPs.n_elem + RPs.n_elem);
                for (arma::uword i = 0; i < CPs.n_elem; i++) {
                    indices[2*i + 0] = CPs(i);
                    indices[2*i + 1] = RPs(i);
                }
                m->bundlePlot->setLines(indices, m->projectionHistory->Y());
            });

    SelectionHandler rpSelectionHandler(X.n_rows - cpIndices.n_elem);
    QObject::connect(m->rpPlot, &Scatterplot::selectionInteractivelyChanged,
            &rpSelectionHandler, &SelectionHandler::setSelection);
    QObject::connect(m->rpBarChart, &BarChart::selectionInteractivelyChanged,
            &rpSelectionHandler, &SelectionHandler::setSelection);
    QObject::connect(&rpSelectionHandler, &SelectionHandler::selectionChanged,
            m->rpPlot, &Scatterplot::setSelection);
    QObject::connect(&rpSelectionHandler, &SelectionHandler::selectionChanged,
            m->rpBarChart, &BarChart::setSelection);
    // This still needs more tests
    /*QObject::connect(&rpSelectionHandler, &SelectionHandler::selectionChanged,
            [m](const std::vector<bool> &rpSelection) {
                // given some RPs, see unexpected CPs *influencing* them
                std::vector<arma::uword> selectedRPIndices;
                for (int i = 0; i < rpSelection.size(); i++) {
                    if (rpSelection[i]) {
                        selectedRPIndices.push_back(i);
                    }
                }

                if (selectedRPIndices.empty()) {
                    overviewBundles(m);
                    return;
                }

                arma::uvec selectedRPs(selectedRPIndices.size());
                std::copy(selectedRPIndices.begin(), selectedRPIndices.end(),
                        selectedRPs.begin());

                // Only the 10% largest values, filtered by the selected RPs
                const arma::mat &unreliability = m->projectionHistory->unreliability();
                arma::uvec indicesLargest = arma::sort_index(unreliability.rows(selectedRPs), "descending");
                arma::uword numLargest = indicesLargest.n_elem * 0.01f;
                indicesLargest = indicesLargest.subvec(0, numLargest-1);
                m->bundlePlot->setValues(unreliability(indicesLargest));

                const arma::uvec &cpIndices = m->projectionHistory->cpIndices();
                arma::uvec CPs = cpIndices(indicesLargest / selectedRPs.n_elem);

                const arma::uvec &rpIndices = m->projectionHistory->rpIndices();
                arma::uvec RPs = indicesLargest;
                RPs.transform([&selectedRPs](arma::uword v) {
                    return v % selectedRPs.n_elem;
                });
                RPs = rpIndices(selectedRPs(RPs));

                arma::uvec indices(CPs.n_elem + RPs.n_elem);
                for (arma::uword i = 0; i < CPs.n_elem; i++) {
                    indices[2*i + 0] = CPs(i);
                    indices[2*i + 1] = RPs(i);
                }
                m->bundlePlot->setLines(indices, m->projectionHistory->Y());
            });*/

    // Brushing between each bar chart and respective scatterplot
    BrushingHandler cpBrushHandler;
    QObject::connect(m->cpPlot, &Scatterplot::itemInteractivelyBrushed,
            &cpBrushHandler, &BrushingHandler::brushItem);
    QObject::connect(m->cpBarChart, &BarChart::itemInteractivelyBrushed,
            &cpBrushHandler, &BrushingHandler::brushItem);
    QObject::connect(&cpBrushHandler, &BrushingHandler::itemBrushed,
            m->cpPlot, &Scatterplot::brushItem);
    QObject::connect(&cpBrushHandler, &BrushingHandler::itemBrushed,
            m->cpBarChart, &BarChart::brushItem);

    BrushingHandler rpBrushHandler;
    QObject::connect(m->rpPlot, &Scatterplot::itemInteractivelyBrushed,
            &rpBrushHandler, &BrushingHandler::brushItem);
    QObject::connect(m->rpBarChart, &BarChart::itemInteractivelyBrushed,
            &rpBrushHandler, &BrushingHandler::brushItem);
    QObject::connect(&rpBrushHandler, &BrushingHandler::itemBrushed,
            m->rpPlot, &Scatterplot::brushItem);
    QObject::connect(&rpBrushHandler, &BrushingHandler::itemBrushed,
            m->rpBarChart, &BarChart::brushItem);

    // Update visual components whenever values change
    QObject::connect(m->projectionHistory, &ProjectionHistory::rpValuesChanged,
            [m](const arma::vec &v, bool rescale) {
                ColorScale *ptr = m->colorScaleRPs.get();
                if (!ptr || v.n_elem == 0) {
                    return;
                }

                if (rescale) {
                    ptr->setExtents(v.min(), v.max());
                } else {
                    ptr->setExtents(std::min(ptr->min(), (float) v.min()),
                                    std::max(ptr->max(), (float) v.max()));
                }

                m->splat->setColorScale(ptr);
                m->rpColormap->setColorScale(ptr);
            });
    QObject::connect(m->projectionHistory, &ProjectionHistory::cpValuesChanged,
             [m](const arma::vec &v) {
                 ColorScale *ptr = m->colorScaleCPs.get();
                 if (!ptr || v.n_elem == 0) {
                     return;
                 }

                 ptr->setExtents(v.min(), v.max());
                 m->cpColormap->setColorScale(ptr);
             });
    QObject::connect(m->projectionHistory, &ProjectionHistory::cpValuesChanged,
            m->cpPlot, &Scatterplot::setColorData);
    QObject::connect(m->projectionHistory, &ProjectionHistory::rpValuesChanged,
            m->splat, &VoronoiSplat::setValues);
    QObject::connect(m->projectionHistory, &ProjectionHistory::cpValuesChanged,
            m->cpBarChart, &BarChart::setValues);
    QObject::connect(m->projectionHistory, &ProjectionHistory::rpValuesChanged,
            m->rpBarChart, &BarChart::setValues);

    // ProjectionHistory takes special care of separate CP/RP selections
    QObject::connect(&cpSelectionHandler, &SelectionHandler::selectionChanged,
            m->projectionHistory, &ProjectionHistory::setCPSelection);
    QObject::connect(&rpSelectionHandler, &SelectionHandler::selectionChanged,
            m->projectionHistory, &ProjectionHistory::setRPSelection);
    //QObject::connect(m->projectionHistory, &ProjectionHistory::selectionChanged,
    //        m->bundlePlot, LinePlot::selectionChanged);

    // Connect projection components to rewinding mechanism
    QObject::connect(plotTC, &TransitionControl::tChanged,
            m->projectionHistory, &ProjectionHistory::setRewind);
    QObject::connect(m->projectionHistory, &ProjectionHistory::mapRewound,
            m, &Main::updateMap);
    QObject::connect(m->projectionHistory, &ProjectionHistory::cpValuesRewound,
            m->cpPlot, &Scatterplot::setColorData);
    QObject::connect(m->projectionHistory, &ProjectionHistory::rpValuesRewound,
            m->splat, &VoronoiSplat::setValues);
    QObject::connect(m->projectionHistory, &ProjectionHistory::rpValuesRewound,
            m->rpBarChart, &BarChart::updateValues);

    // General component set up
    plotTC->setAcceptedMouseButtons(Qt::RightButton);
    m->cpPlot->setDragEnabled(true);
    m->cpPlot->setAutoScale(false);
    m->rpPlot->setAutoScale(false);
    m->cpBarChart->setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
    m->setSelectCPs();

    m->cpBarChart->setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
    m->rpBarChart->setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);

    m->setCPColorScale(Main::ColorScaleRainbow);
    m->setRPColorScale(Main::ColorScaleRainbow);

    // This sets the initial CP configuration, triggering all the necessary
    // signals to set up the helper objects and visual components
    manipulationHandler.setCP(Ys);

    return app.exec();
}