metavis.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #include "pch.h"
  2. #include "metavis.h"
  3. #include "SettingDialog.h"
  4. #include <QStandardPaths>
  5. #include <QDockwidget>
  6. #include <QLabel>
  7. #include <QLayout>
  8. #include <QDebug>
  9. #include <QStyleFactory>
  10. #include <QFileDialog>
  11. #include <QDir>
  12. #include <map>
  13. #include <boost/multiprecision/cpp_int.hpp>
  14. #include <QDesktopWidget>
  15. #include <QSlider>
  16. #include <QSizePolicy>
  17. #include <QScrollArea>
  18. #include <QMainWindow>
  19. #include "DockableGraphView.h"
  20. #include <QShortcut>
  21. #include "RangeSlider.h"
  22. #include "tsneIteractive.h"
  23. #include "util.h"
  24. metavis::metavis(QWidget* parent)
  25. : QMainWindow(parent)
  26. {
  27. ui.setupUi(this);
  28. /* create settings object*/
  29. //settings = new QSettings(QSettings::IniFormat, QSettings::UserScope, "TK", "metavis", this);
  30. settings = new QSettings("settings.ini", QSettings::IniFormat, this);
  31. //settings = new QSettings(QCoreApplication::applicationDirPath()+ "hatschi.ini", QSettings::IniFormat);
  32. setStyleSheet(styleSheet() + "QMainWindow::separator {background: rgb(200, 200, 200);width: 1px;height: 1px;}");
  33. setStyleSheet(styleSheet() + "QTabBar::tab:selected {color: rgb(0, 122, 204);}");
  34. setStyleSheet(styleSheet() + "QTabWidget::pane {border-top: 0px solid #C2C7CB;margin: -9px -9px -13px -9px;}");
  35. this->setDockNestingEnabled(true);
  36. option = dockOption::right;
  37. createBitInSpector();
  38. QDockWidget* saveForTabPad = lastDocked;
  39. option = dockOption::splitTop;
  40. createProjectManager();
  41. option = dockOption::splitLeft;
  42. createBitFieldV2();
  43. QDockWidget* saveForTabTsne = lastDocked;
  44. initPlotter();
  45. createScratchpad(saveForTabPad);
  46. createTSNE(saveForTabTsne);
  47. readMainWindowSettings();
  48. ui.actionOpen->setShortcut(QKeySequence(Qt::CTRL + Qt::Key::Key_O));
  49. QMenu* viewMenu = createPopupMenu();
  50. viewMenu->setTitle("View");
  51. ui.menuBar->insertMenu(manager->projectMenu->menuAction(), viewMenu);
  52. this->setCentralWidget(nullptr);
  53. }
  54. void metavis::openSetting() {
  55. SettingDialog settingDialog(settings, this);
  56. /* Blocking operation */
  57. settingDialog.exec();
  58. }
  59. metavis::~metavis()
  60. {
  61. writeActualMainWindowSettings();
  62. }
  63. void metavis::selectRunData(RunData* data)
  64. {
  65. selectedBestGraph->removeAll();
  66. selectedBestGraph->setDisplayLabel(QString::fromStdString(data->name));
  67. for (SingleRun& run : data->singleRunList) {
  68. selectedBestGraph->addSeries(&run.bestMaxSolutionFoundPerIteration, data->name, QString::fromStdString(run.name), QColor(12, 116, 137, 200), GraphPlottSeries::SeriesType::Line);
  69. }
  70. selectedBestGraph->addSeries(&data->bestAverageMaxSolutionFoundPerIteration, data->name, "average best from " + QString::fromStdString(data->name), QColor(255, 0, 0), GraphPlottSeries::SeriesType::Line);
  71. selectedBestGraph->getSeriesVector().back().lineWidth = 3;
  72. selectedBestGraph->frameGraphInView();
  73. }
  74. void metavis::selectSingleRun(SingleRun* run)
  75. {
  76. selectedBestAverageGraph->removeAll();
  77. selectedBestAverageGraph->setDisplayLabel(QString::fromStdString(run->name));
  78. selectedBestAverageGraph->addSeries(&run->bestMaxSolutionFoundPerIteration, run->runDataName, "best", QColor(255, 0, 0), GraphPlottSeries::SeriesType::Line);
  79. selectedBestAverageGraph->addSeries(&run->averageSolutionPerItertion, run->runDataName, "average", QColor(0, 0, 255), GraphPlottSeries::SeriesType::Line);
  80. selectedBestAverageGraph->frameGraphInView();
  81. selectedMinMaxGraph->removeAll();
  82. selectedMinMaxGraph->setDisplayLabel(QString::fromStdString(run->name));
  83. selectedMinMaxGraph->addSeries(&run->dotsForDistribution, run->runDataName,"distribution", QColor(255, 165, 0, 100), GraphPlottSeries::SeriesType::Dot);
  84. selectedMinMaxGraph->addSeries(&run->minSolutionPerItertion, run->runDataName,"min", QColor(255, 0, 0), GraphPlottSeries::SeriesType::Line);
  85. selectedMinMaxGraph->addSeries(&run->maxSolutionPerItertion, run->runDataName,"max" , QColor(0, 0, 255), GraphPlottSeries::SeriesType::Line);
  86. selectedMinMaxGraph->frameGraphInView();
  87. selectedMeanHammingDistanceGraph->removeAll();
  88. selectedMeanHammingDistanceGraph->setDisplayLabel(QString::fromStdString(run->name));
  89. selectedMeanHammingDistanceGraph->addSeries(&run->meanHammingDistancePerIteration, run->runDataName, "mean Hamming Distance", QColor(255, 0, 0), GraphPlottSeries::SeriesType::Line);
  90. selectedMeanHammingDistanceGraph->frameGraphInView();
  91. bitfieldPanel->field->setDisplayLabel(QString::fromStdString(run->name));
  92. bitfieldPanel->displaySingleRun(run);
  93. bitfieldPanel->update();
  94. inspectorPanel->inspector->updateData(run->begin, run->end);
  95. inspectorPanel->setRunName(run->name);
  96. tsnePanel->assignData(run->begin, run->end, QString::fromStdString(run->name));
  97. }
  98. void metavis::removeRunDataFromAllViews(RunData* data)
  99. {
  100. selectedBestAverageGraph->removeRunData(data);
  101. selectedBestAverageGraph->update();
  102. selectedMinMaxGraph->removeRunData(data);
  103. selectedMinMaxGraph->update();
  104. selectedMeanHammingDistanceGraph->removeRunData(data);
  105. selectedMeanHammingDistanceGraph->update();
  106. selectedBestGraph->removeRunData(data);
  107. selectedBestGraph->update();
  108. bitfieldPanel->clearRun();
  109. bitfieldPanel->update();
  110. inspectorPanel->removeRun();
  111. tsnePanel->clear();
  112. }
  113. void metavis::dockWidget(QDockWidget* dock)
  114. {
  115. switch (option) {
  116. case left:
  117. addDockWidget(Qt::LeftDockWidgetArea, dock);
  118. break;
  119. case right:
  120. addDockWidget(Qt::RightDockWidgetArea, dock);
  121. break;
  122. case top:
  123. addDockWidget(Qt::TopDockWidgetArea, dock);
  124. break;
  125. case bottom:
  126. addDockWidget(Qt::BottomDockWidgetArea, dock);
  127. break;
  128. case splitLeft:
  129. this->splitDockWidget(lastDocked, dock, Qt::Orientation::Horizontal);
  130. this->splitDockWidget(dock, lastDocked, Qt::Orientation::Horizontal);
  131. break;
  132. case splitRight:
  133. this->splitDockWidget(lastDocked, dock, Qt::Orientation::Horizontal);
  134. break;
  135. case splitBottom:
  136. this->splitDockWidget(lastDocked, dock, Qt::Orientation::Vertical);
  137. break;
  138. case splitTop:
  139. this->splitDockWidget(lastDocked, dock, Qt::Orientation::Vertical);
  140. this->splitDockWidget(dock, lastDocked, Qt::Orientation::Vertical);
  141. break;
  142. case tab:
  143. tabifyDockWidget(lastDocked, dock);
  144. break;
  145. default:
  146. addDockWidget(Qt::LeftDockWidgetArea, dock);
  147. break;
  148. }
  149. lastDocked = dock;
  150. }
  151. GraphView* metavis::createCustomWidget(QString titleString, bool tabToLast)
  152. {
  153. DockableGraphView* dock = new DockableGraphView(this, titleString);
  154. qDebug() << titleString;
  155. dockWidget(dock);
  156. return dock->view;
  157. }
  158. void metavis::createBitFieldV2()
  159. {
  160. QDockWidget* dock = new QDockWidget(this);
  161. bitfieldPanel = new BitfieldControlPanel(this);
  162. bitfieldPanel->field->setScratchpad(pad);
  163. bitfieldPanel->field->setInformation("<h3><u>Bitfield</u></h3>This search space visualization displays all found solution of a single run.<br>Hover over a dot to see its bitstring.<br>The color represents the objective function.<br><b><font color=\"gray\">Gray</font> patterned area:</b> Represents the area where no solution can be generated.<br><b>Y Axis:</b> Represents the amount of set bits of a solution.<br><b>X Axis:</b> The x axis represents the position of the solution which is concluded by collapsing<br>all solutions of a Y Value and spreading these between 0 and 1. The solutions are sorted descendingly based on their value.<br>Example with a four long bitstring and two set bits. For two set bits, there are six different solutions:<br>1100 1010 1001 0110 0101 0011<br>The solution 0101 is the 5th possible solution and gets the position value \'0.8\'.<br><br><b>Tipp:</b> Select an interesting area and add the points to the scratchpad for fast comparison.<h3><u>Options</u></h3>The Transparency slider handles the transparency of the dots.<br>The Size slider handles the radius of the dots.<br><b>Tipp:</b> Make the dots big and the transparency low the see clustering.<br>Under \'More Options\' the color for the objective function and the displayed itertion can be controlled.");
  164. dock->setWindowTitle("Bitfield");
  165. dock->setObjectName("Bitfield");
  166. dock->setWidget(bitfieldPanel);
  167. dockWidget(dock);
  168. }
  169. void metavis::createProjectManager()
  170. {
  171. QDockWidget* dock= new QDockWidget(this);
  172. manager = new ProjectManager(this);
  173. dock->setWindowTitle("Project Manager");
  174. dock->setObjectName("Project Manager");
  175. dock->setWidget(manager);
  176. connect(ui.actionOpen, &QAction::triggered, manager, &ProjectManager::openFileDialog);
  177. dockWidget(dock);
  178. manager->projectMenu->setTitle("Project");
  179. ui.menuBar->addMenu(manager->projectMenu);
  180. }
  181. void metavis::createTSNE(QDockWidget* dockto)
  182. {
  183. QDockWidget* dock = new QDockWidget(this);
  184. tsnePanel = new TsneControlPanel(this);
  185. connect(tsnePanel, &TsneControlPanel::started, this, [this]() {this->showStatusBarMessage("T-SNE calculating...");});
  186. connect(tsnePanel, &TsneControlPanel::finished, this, &metavis::clearStatusBar);
  187. tsnePanel->plott->setScratchpad(pad);
  188. tsnePanel->plott->setInformation("<h3><u>T-SNE</u></h3>This search space evaluation uses a gradient descent method and has to be calculated first via the \"Start\" button.<br>This search space visualization display all found solutions of a single run.<br>Hover over a dot to see its bitstring.<br>The color represents the objective function.<br> The axes have no special meaning besides indicating the distance between solutions.<br><b>Start Button:</b> Starts the tsne methods. This may take a while.<br><b>Pause Button:</b> Pauses the tsne methods.<br><br><br><b>Tipp</b> select a interesting area and add the points to the scratchpad for fast comparison.<h3><u>Options</u></h3>Transparency slider handles the transparency of the dots.<br>Size slider handles the radius of the dots.<br><b>Tipp</b> make the dots big and the transparency low the see clustering.<br>Under \'More Options\' the color for the objective function and the displayed itertion can be controlled.<br> Also to important parameter for T-SNE can be controlled via slider or textfield.<br><b>Perplexity</b> is a parameter that should indicate how many neighbors a solution have.<br><b>Learnrate:</b> is a parameter that determine how fast the gradient descent methods make updates.");
  189. dock->setWindowTitle("T-SNE");
  190. dock->setObjectName("T-SNE");
  191. dock->setWidget(tsnePanel);
  192. this->tabifyDockWidget(dockto, dock);
  193. dockto->raise();
  194. }
  195. GraphPlott* metavis::initGraphPlott(QString title, QString YAxisLegend, QString XAxisLegend)
  196. {
  197. QDockWidget* dock = new QDockWidget(this);
  198. GraphPlott* graphplott = new GraphPlott(this);
  199. graphplott->setDefaultVisibleWindow(0, 100, 30, 110);
  200. dock->setWidget(graphplott);
  201. dock->setWindowTitle(title);
  202. dock->setObjectName(title);
  203. graphplott->setAxisLegend(XAxisLegend, YAxisLegend);
  204. dockWidget(dock);
  205. return graphplott;
  206. }
  207. void metavis::initPlotter()
  208. {
  209. option = splitLeft;
  210. selectedBestGraph = initGraphPlott("Best Overview");
  211. selectedBestGraph->setScratchpad(pad);
  212. selectedBestGraph->setInformation("<h3><u>Best Overview</u></h3>Shows all best graphs in <font color=\"gray\">gray</font> for comparison.<br>The <font color=\"red\">red</font> line shows the average best over all repetition.<br>By hovering over a line the repetition name is shown.<br><br><b>Objective Function:</b> Describes how good the solution is, it depends on the problem if a low or high value is desired.");
  213. option = splitTop;
  214. selectedBestAverageGraph = initGraphPlott("Best Average");
  215. selectedBestAverageGraph->setScratchpad(pad);
  216. selectedBestAverageGraph->setInformation("<h3><u>Best Average</u></h3>The <font color=\"red\">red</font> line shows the best graph that presents the best objective function value found for each iteration of a single repetition.<br>The <font color=\"blue\">blue</font> line shows the average graph that presents the average objective function value in the popullation for each iteration of a single repetition.<br><br><b>Objective Function:</b> Describes how good the solution is. It depends on the problem if a low or high value is desired.");
  217. //GraphPlott* selectedParticleGraph = initGraphPlott("Best Vs Average");
  218. option = tab;
  219. selectedMinMaxGraph = initGraphPlott("Min Max Distribution");
  220. selectedMinMaxGraph->setInformation("<h3><u>Min Max Distribution</u></h3>The <font color=\"blue\">blue</font> line shows the max graph that presents the maximum objective function value in the popullation for each iteration of a single round.<br>The <font color=\"red\">red</font> line shows the minimum objective function value in the popullation for each iteration of a single round.<br>The <font color=\"orange\">orange</font> dots show all objective function values in the population for each iteration of a single repetition.<br><br><b>Objective Function:</b> Describes how good the solution is. It depends on the problem if a low or high value is desired.");
  221. selectedMinMaxGraph->setScratchpad(pad);
  222. option = tab;
  223. selectedMeanHammingDistanceGraph = initGraphPlott("Mean Hamming Distance", "Mean Hamming Distance");
  224. selectedMeanHammingDistanceGraph->setInformation("<h3><u>Mean Hamming Distance</u></h3>The <font color=\"red\">red</font> line shows the mean hamming distance for each solution to each solution from the population for each iteration of a single repetition.<br><br><b>Hamming Distance:</b> Describes the distance between to bitstrings by calculating the amount of different bits, e.g. 00110 and 11100 have a hamming distance of 3.");
  225. multiBestGraph;
  226. multiAvgGraph;
  227. multiMinGraph;
  228. multiMaxGraph;
  229. multiMeanHammingDistanceGraph;
  230. }
  231. void metavis::createBitInSpector()
  232. {
  233. QDockWidget* dock = new QDockWidget(this);
  234. inspectorPanel = new BitInspectorPanel(this);
  235. dock->setWidget(inspectorPanel);
  236. dock->setWindowTitle("Inspector");
  237. dock->setObjectName("Inspector");
  238. dockWidget(dock);
  239. }
  240. void metavis::createScratchpad(QDockWidget* dockto)
  241. {
  242. QDockWidget* dock = new QDockWidget(this);
  243. dock->setWidget(pad);
  244. dock->setWindowTitle("Scratchpad");
  245. dock->setObjectName("Scratchpad");
  246. this->tabifyDockWidget(dockto, dock);
  247. dockto->raise();
  248. }
  249. void metavis::writeActualMainWindowSettings()
  250. {
  251. settings->beginGroup("MainWindow");
  252. settings->setValue("maximized", isMaximized());
  253. if (!isMaximized()) {
  254. /* position and size of the window if not maximized */
  255. settings->setValue("pos", pos());
  256. settings->setValue("size", size());
  257. settings->setValue("screenCount", QApplication::desktop()->screenCount());
  258. }
  259. settings->setValue("geometry", saveGeometry());
  260. settings->setValue("windowState", saveState());
  261. settings->endGroup();
  262. }
  263. void metavis::readMainWindowSettings()
  264. {
  265. settings->beginGroup("MainWindow");
  266. if (QApplication::desktop()->screenCount() == settings->value("screenCount", 1)) {
  267. //Only when same screeenCount move the window;
  268. move(settings->value("pos", QPoint(360, 200)).toPoint());
  269. }
  270. resize(settings->value("size", QSize(1200, 675)).toSize());
  271. if (settings->value("maximized", false).toBool()) {
  272. showMaximized();
  273. }
  274. this->restoreGeometry(settings->value("geometry").toByteArray());
  275. this->restoreState(settings->value("windowState").toByteArray());
  276. settings->endGroup();
  277. }
  278. void metavis::openFile()
  279. {
  280. //int oldIndex = runVec.size();
  281. /*int size = runList.size();
  282. for (int i = 0; i < pathList.size(); i++) {
  283. qDebug() << "file:" << pathList[i];
  284. runList.push_back(RunData(pathList[i].toStdString()));
  285. }*/
  286. /*for (std::list<RunData>::iterator iter = std::next(runList.begin(), size); iter != runList.end(); iter++) {
  287. QColor runColor = multiBestGraph->generateNextColorForGraph();
  288. multiBestGraph->addLine(&iter->bestMinSolutionFoundPerIteration, &*iter, runColor);
  289. multiAvgGraph->addLine(&iter->averageSolutionPerItertion, &*iter, runColor);
  290. multiMaxGraph->addLine(&iter->maxSolutionPerItertion, &*iter, runColor);
  291. multiMinGraph->addLine(&iter->minSolutionPerItertion, &*iter, runColor);
  292. multiMeanHammingDistanceGraph->addLine(&iter->meanHammingDistancePerIteration, &*iter, runColor);
  293. }*/
  294. /*
  295. for (int i = 0; i < runList.size(); i++) {
  296. QColor runColor = multiBestGraph->generateNextColorForGraph();
  297. multiBestGraph->addLine(&runList[i].bestSolutionPerIteration, &runList[i], runColor);
  298. multiAvgGraph->addLine(&runList[i].averageSolutionPerItertion, &runList[i], runColor);
  299. multiMaxGraph->addLine(&runList[i].maxSolutionPerItertion, &runList[i], runColor);
  300. multiMinGraph->addLine(&runList[i].minSolutionPerItertion, &runList[i], runColor);
  301. //multiMeanHammingDistanceGraph->addLine(&runVec[i].meanHammingDistancePerIteration, &runVec[i], runColor);
  302. }*/
  303. /*std::vector<RunData>::iterator end = runVec.end();
  304. for (int i = 0; i < pathList.size(); i++) {
  305. qDebug() << "file:" << pathList[i];
  306. runVec.push_back(RunData(pathList[i].toStdString()));
  307. }
  308. for (std::vector<RunData>::iterator iter = end; iter != runVec.end(); iter++) {
  309. QColor runColor = multiBestGraph->generateNextColorForGraph();
  310. multiBestGraph->addLine(&iter->bestSolutionPerIteration, &*iter, runColor);
  311. multiAvgGraph->addLine(&iter->averageSolutionPerItertion, &*iter, runColor);
  312. multiMaxGraph->addLine(&iter->maxSolutionPerItertion, &*iter, runColor);
  313. multiMinGraph->addLine(&iter->minSolutionPerItertion, &*iter, runColor);
  314. multiMeanHammingDistanceGraph->addLine(&iter->meanHammingDistancePerIteration, &*iter, runColor);
  315. }*/
  316. //actualBestAverageGraph->addLine(runVec[0].bestSolutionPerIteration, QColor(255, 0, 0));
  317. //actualBestAverageGraph->addLine(runVec[0].averageSolutionPerItertion, QColor(0, 0, 255));
  318. //actualMinMaxGraph->addLine(runVec[0].minSolutionPerItertion, QColor(255, 0, 0));
  319. //actualMinMaxGraph->addLine(runVec[0].maxSolutionPerItertion, QColor(0, 0, 255));
  320. //actualMinMaxGraph->addDots(runVec[0].dotsForDistribution, QColor(255, 165, 0, 100));
  321. //for (auto iter = runVec[0].particleMap.begin(); iter != runVec[0].particleMap.end(); iter++) {
  322. // actualParticleGraph->addLine(iter->second);
  323. //}
  324. ////Test
  325. /*RunData* rundata = &runList.back();
  326. bitField->addDots(&rundata->dotsForBitField, rundata, QColor(255, 165, 0, 100));
  327. updateBitFieldColors();
  328. bitField->graphSeriesVec.back().useDataPointColor = true;
  329. tsneWidget->assignRunData(&runList.back());
  330. inspector->addData(&runList.back().solutionVec);
  331. plottTest->addSeries(&rundata->bestMaxSolutionFoundPerIteration, rundata, QColor(255, 165, 0, 100), GraphPlottSeries::SeriesType::Line);*/
  332. //actualMeanHmmingDistanceGraph->addLine(runVec[0].meanHammingDistancePerIteration, QColor(255, 0, 0));
  333. }
  334. void metavis::showStatusBarLoading()
  335. {
  336. ui.statusBar->showMessage("Loading...");
  337. ui.statusBar->setStyleSheet("background-color: rgb(247, 197, 72);");
  338. }
  339. void metavis::showStatusBarMessage(QString message)
  340. {
  341. ui.statusBar->showMessage(message);
  342. ui.statusBar->setStyleSheet("background-color: rgb(247, 197, 72);");
  343. }
  344. void metavis::clearStatusBar()
  345. {
  346. ui.statusBar->clearMessage();
  347. ui.statusBar->setStyleSheet("background-color: rgb(240, 240, 240);");
  348. }