File: qTrain3DMASCDialog.cpp

package info (click to toggle)
cloudcompare 2.13.2%2Bgit20240821%2Bds-1
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 151,152 kB
  • sloc: cpp: 687,217; ansic: 165,269; python: 31,109; xml: 25,906; sh: 940; makefile: 509; java: 229; asm: 204; fortran: 160; javascript: 73; perl: 18
file content (367 lines) | stat: -rw-r--r-- 10,343 bytes parent folder | download | duplicates (2)
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
//##########################################################################
//#                                                                        #
//#                     CLOUDCOMPARE PLUGIN: qCANUPO                       #
//#                                                                        #
//#  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; version 2 or later of the License.      #
//#                                                                        #
//#  This program is distributed in the hope that it will be useful,       #
//#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
//#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          #
//#  GNU General Public License for more details.                          #
//#                                                                        #
//#            COPYRIGHT: UNIVERSITE EUROPEENNE DE BRETAGNE                #
//#                                                                        #
//##########################################################################

#include "qTrain3DMASCDialog.h"

//Qt
#include <QTableWidgetItem>
#include <QMessageBox>
#include <QFileDialog>
#include <QSettings>
#include <QTextStream>
#include <QStandardPaths>
#include <QDateTime>

#include <ccLog.h>

//System
#include <assert.h>

#include <iostream>

static const int FeatureImportanceColumn = 1;

Train3DMASCDialog::Train3DMASCDialog(QWidget* parent/*=nullptr*/)
	: QDialog(parent)
	, Ui::Train3DMASCDialog()
	, classifierSaved(false)
	, saveRequested(false)
	, traceFileConfigured(false)
	, m_traceFile(nullptr)
	, run(0)
{
	setupUi(this);

	QDateTime dateTime = QDateTime::currentDateTime();
	m_baseName = "3dmasc_" + dateTime.toString("yyyyMMdd_hh")
			+  "h" + dateTime.toString("mm");

	readSettings();

	connect(closePushButton, SIGNAL(clicked()), this, SLOT(onClose()));
	connect(savePushButton, SIGNAL(clicked()), this, SLOT(onSave()));
	connect(exportToolButton, SIGNAL(clicked()), this, SLOT(onExportResults()));
}

Train3DMASCDialog::~Train3DMASCDialog()
{
	writeSettings();
	closeTraceFile();
	for (auto m : toDeleteLater)
	{
		if (m != nullptr)
			delete m;
	}
}

void Train3DMASCDialog::readSettings()
{
	QSettings settings;
	settings.beginGroup("3DMASC");
	bool keepAttributes = settings.value("keepAttributes", false).toBool();
	this->keepAttributesCheckBox->setChecked(keepAttributes);
	bool saveTrace = settings.value("saveTrace", false).toBool();
	setCheckBoxSaveTrace(saveTrace);
}

void Train3DMASCDialog::writeSettings()
{
	QSettings settings;
	settings.beginGroup("3DMASC");
	settings.setValue("keepAttributes", keepAttributesCheckBox->isChecked());
	settings.setValue("saveTrace", checkBox_keepTraces->isChecked());
}

void Train3DMASCDialog::clearResults()
{
	resultLabel->clear();
	tableWidget->clear();
}

int Train3DMASCDialog::addFeature(QString name, float importance, bool isChecked/*=true*/)
{
	int index = tableWidget->rowCount();
	tableWidget->setRowCount(index + 1);

	QTableWidgetItem* nameItem = new QTableWidgetItem(name);
	nameItem->setCheckState(isChecked ? Qt::Checked : Qt::Unchecked);
	tableWidget->setItem(index, 0, nameItem);

	QTableWidgetItem* importanceItem = new QTableWidgetItem(std::isnan(importance) ? QString() : QString::number(importance));
	tableWidget->setItem(index, 1, importanceItem);

	return index;
}

int Train3DMASCDialog::addScale(double scale, bool isChecked/*=true*/)
{
	int index = tableWidgetScales->rowCount();
	tableWidgetScales->setRowCount(index + 1);

	QTableWidgetItem* nameItem = new QTableWidgetItem(QString::number(scale));
	nameItem->setCheckState(isChecked ? Qt::Checked : Qt::Unchecked);
	tableWidgetScales->setItem(index, 0, nameItem);

	return index;
}

void Train3DMASCDialog::scaleStateChanged(QTableWidgetItem* item)
{
	// if the scale is not checked, remove automatically features based on this scale
	QString scale = "_SC" + item->text() + "_";
	for (int row = 0; row < tableWidget->rowCount(); row++)
	{
		QTableWidgetItem* nameItem = tableWidget->item(row, 0);
		QString name = nameItem->text();
		if (name.contains(scale))
			nameItem->setCheckState(item->checkState());
	}
}

void Train3DMASCDialog::connectScaleSelectionToFeatureSelection()
{
	connect(tableWidgetScales, &QTableWidget::itemChanged, this, &Train3DMASCDialog::scaleStateChanged);
}

void Train3DMASCDialog::setResultText(QString text)
{
	resultLabel->setText(text);
}

void Train3DMASCDialog::setFirstRunDone()
{
	runPushButton->setText(tr("Retry"));
	savePushButton->setEnabled(true);
}

bool Train3DMASCDialog::isFeatureSelected(QString featureName) const
{
	for (int index = 0; index < tableWidget->rowCount(); ++index)
	{
		QTableWidgetItem* item = tableWidget->item(index, 0);
		if (item->text() == featureName)
		{
			return (item->checkState() == Qt::Checked);
		}
	}

	assert(false);
	return false;
}

void Train3DMASCDialog::sortByFeatureImportance()
{
	tableWidget->sortByColumn(FeatureImportanceColumn, Qt::DescendingOrder);
}

void Train3DMASCDialog::setFeatureImportance(QString featureName, float importance)
{
	for (int index = 0; index < tableWidget->rowCount(); ++index)
	{
		if (tableWidget->item(index, 0)->text() == featureName)
		{
			QTableWidgetItem* item = tableWidget->item(index, FeatureImportanceColumn);
			item->setText(std::isnan(importance) ? QString() : QString::number(importance, 'f', 6));
			return;
		}
	}

	assert(false);
}

void Train3DMASCDialog::onClose()
{
	if (!classifierSaved && QMessageBox::question(this, "Classifier not saved", "Classifier not saved. Do you confirm you want to close the tool?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
		return;

	reject();
}

void Train3DMASCDialog::onSave()
{
	saveRequested = true;
	accept();
}

void Train3DMASCDialog::onExportResults(QString filePath/*=""*/)
{
	QSettings settings;
	settings.beginGroup("3DMASC");
	QString outputPath = settings.value("FilePath", QCoreApplication::applicationDirPath()).toString();
	QString outputFilename = QFileDialog::getSaveFileName(this, "Export feature importance matrix", outputPath, "*.csv");
	if (outputFilename.isNull())
	{
		//process cancelled by the user
		return;
	}
	settings.setValue("FilePath", QFileInfo(outputFilename).absolutePath());
	settings.endGroup();

	//save the file
	QFile file(outputFilename);
	if (!file.open(QFile::WriteOnly | QFile::Text))
	{
		QMessageBox::critical(this, "Error", "Failed to open file for writing: " + outputFilename);
		return;
	}

	QTextStream stream(&file);
	stream << "Feature;Importance" << Qt::endl;
	for (int index = 0; index < tableWidget->rowCount(); ++index)
	{
		QString featureName = tableWidget->item(index, 0)->text();
		QString importance = tableWidget->item(index, FeatureImportanceColumn)->text();
		stream << featureName << ";" << importance << Qt::endl;
	}
}

void Train3DMASCDialog::addConfusionMatrixAndSaveTraces(ConfusionMatrix* confusionMatrix)
{
	toDeleteLater.push_back(confusionMatrix);
	saveTraces(confusionMatrix);
}

void Train3DMASCDialog::setInputFilePath(QString filePath)
{
	m_parameterFilePath = filePath;
}

void Train3DMASCDialog::setCheckBoxSaveTrace(bool state)
{
	checkBox_keepTraces->setChecked(state);
}

bool Train3DMASCDialog::openTraceFile()
{
	QString traceFileName;
	QString traceFilePath;

	// get currentPath
	QFileInfo info(m_parameterFilePath);
	QDir parameterDir = QDir(info.path());

	QFileDialog dialog(this);
	dialog.setFileMode(QFileDialog::DirectoryOnly);
	dialog.setWindowTitle("Choose a valid directory for the traces");
	dialog.setDirectory(QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).at(0));

	if (!m_traceFile) // create trace file if it does not exists already
	{
		m_tracePath = parameterDir.absolutePath() + "/" + m_baseName;
		traceFileName = m_baseName + ".txt";

		if (parameterDir.exists(m_baseName))
		{
			QDateTime dateTime = QDateTime::currentDateTime();
			m_baseName += "min" + dateTime.toString("ss") + "s";
		}
		if (!parameterDir.mkdir(m_baseName)) // create a specific directory to store the traces
		{
			ccLog::Error("impossible to save in the default directory: " + m_tracePath);
			// impossible to save in the default directory, you have to propose another path
			if(dialog.exec())
				m_tracePath = dialog.selectedFiles().at(0);
			else
				return false;
		}
		else
			ccLog::Print("directory for traces created: " + m_tracePath);

		traceFilePath = m_tracePath + "/" + traceFileName;
		m_traceFile = new QFile(traceFilePath);

		if(!m_traceFile->open(QIODevice::WriteOnly | QIODevice::Text))
		{
			ccLog::Error("impossible to open trace file: " + traceFilePath);
			delete m_traceFile;
			m_tracePath.clear();
			return false;
		}
	}

	if (m_traceFile->isOpen())
	{
		traceFileConfigured = true;
		ccLog::Print("save trace in: " + traceFilePath);
		m_traceStream.setDevice(m_traceFile);
		m_traceStream << "run overallAccuracy\n";
		return true;
	}
	else
	{
		return false;
	}
}

bool Train3DMASCDialog::closeTraceFile()
{
	if (m_traceFile)
		if (m_traceFile->isOpen())
		{
			ccLog::Print("[3DMASC] traces stored in: " + m_traceFile->fileName());
			m_traceFile->close();
		}

	return true;
}

void Train3DMASCDialog::saveTraces(ConfusionMatrix *confusionMatrix)
{
	run++; // increment the run number
	confusionMatrix->setSessionRun(m_baseName, run);
	if (checkBox_keepTraces->isChecked())
	{
		if (!traceFileConfigured) // if the trace file is not configured yet, do it
		{
			if (!openTraceFile())
				return;
		}
		// save the trace

		// save the run number and the overall accuracy
		if (m_traceStream.device())
			m_traceStream << run << " " << confusionMatrix->getOverallAccuracy() << Qt::endl;
		confusionMatrix->save(m_tracePath + "/" + "run_" + QString::number(run) + "_confusion_matrix.txt");

	}
}

bool Train3DMASCDialog::getSaveTrace()
{
	return checkBox_keepTraces->isChecked();
}

QString Train3DMASCDialog::getTracePath()
{
	if (traceFileConfigured)
	{
		QFileInfo fi(*m_traceFile);
		return fi.absoluteDir().absolutePath();
	}
	else if (openTraceFile())
	{
		QFileInfo fi(*m_traceFile);
		return fi.absoluteDir().absolutePath();
	}
	else
		return QString();
}

int Train3DMASCDialog::getRun()
{
	return run;
}