-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.cpp
More file actions
executable file
·212 lines (174 loc) · 9.09 KB
/
core.cpp
File metadata and controls
executable file
·212 lines (174 loc) · 9.09 KB
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
#include "core.hpp"
CoreMA::CoreMA(QWidget *parent, GeneralViewMA *gv, LogView *lv){
general = gv;
log = lv;
proView = new ProcessView(parent);
process = new QProcess();
QObject::connect(process, &QProcess::errorOccurred, this, &CoreMA::errorOccurred);
}
void CoreMA::runMatlab() {
log->write("\n-- Start Analysis --\n");
QFileInfo file(general->getVideoPath());
if (!file.exists())
return log->write("Error: video file does not exist and cannot be analyzed\n\n-- Finish Analysis --\n");
QFileInfo appfile(general->getAppPath());
if (!appfile.exists())
return log->write("Error: app does not exist\n\n-- Finish Analysis --\n");
// Get parameters from General Tab
std::string videoPath = file.path().toStdString();
std::string videoName = file.fileName().toStdString();
int maxSize = general->getMaxSize();
int minSize = general->getMinSize();
int areaBool = general->isAreaChecked();
int eccentricityBool = general->isEccentricityChecked();
int orientationBool = general->isOrientationChecked();
// Write parameters to Log
log->write(QString::fromStdString(" File path: " + videoPath));
log->write(QString::fromStdString(" File name: " + videoName));
log->write(QString::fromStdString(" Cell size: max: " + std::to_string(maxSize) + ", min: " + std::to_string(minSize)));
std::string areaString = areaBool? "Yes": "No";
std::string eccentricityString = eccentricityBool? "Yes": "No";
std::string orientationString = orientationBool? "Yes": "No";
log->write(QString::fromStdString(" Area: " + areaString));
log->write(QString::fromStdString(" Eccentricity: " + eccentricityString));
log->write(QString::fromStdString(" Orientation: " + orientationString));
// Pass parameters into exchange file
std::ofstream out((appfile.path() + "/tempPort.txt").toStdString());
out << videoPath << "\n" << videoName << "\n" << maxSize << "\n" << minSize << "\n" << areaBool << "\n" << eccentricityBool << "\n" << orientationBool;
out.close();
// Run matlab code
//input: videoPath, videoName, maxSize, minSize, areaBool, eccentricityBool, orientationBool
log->write("\n Running MATLAB Code...\n");
QObject::connect(process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, &CoreMA::finishedMatlab);
process->start(general->getAppPath());
/* Output: totalcells_GT, totalcells, stoppedcells_GT, stoppedcells, stoppedpercent_GT,
stopped_percent,tracking_accuracy, stoppedcell_accuracy, output_filename
*/
proView->start();
}
void CoreMA::finishedMatlab(int exitCode, QProcess::ExitStatus exitStatus) {
// clear up everthing
QObject::disconnect(process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, &CoreMA::finishedMatlab);
proView->stop();
if (exitStatus == QProcess::CrashExit) {
log->write("Error: the analysis process crashes for unknown reasons.\n\n-- Finish Analysis --\n");
return;
}
QFileInfo info(general->getAppPath());
std::ifstream in((info.path() + "/tempPort.txt").toStdString());
std::string tmp[9];
for (int i = 0; i < 9; i++) {
if (in.fail())
return log->write("Error: fail parsing the returned file.\n\n-- Finish Analysis --\n");
in >> tmp[i];
}
log->write(QString::fromStdString(" Total number of cells present (Ground Truth):\n\t" + tmp[0] +
"\n Total number of cells detected by tracking:\n\t" + tmp[1] +
"\n Number of cells that stopped (Ground Truth):\n\t" + tmp[2] +
"\n Number of cells that stopped:\n\t" + tmp[3] +
"\n Percentage of cells that stopped (Ground Truth):\n\t" + tmp[4] +
"\n Percentage of cells that stopped:\n\t" + tmp[5] +
"\n Accuracy in tracking cells:\n\t" + tmp[6] +
"\n Accuracy in tracking stopped cells:\n\t" + tmp[7] +
"\n Output file name:\n\t" + tmp[8] + "\n"));
log->write("\n Using time: " + proView->timeCost() + "\n");
QFileInfo video(general->getVideoPath());
general->setResultVideo(video.path() + QString::fromStdString("/" + tmp[8]));
log->write("-- Finish Analysis --\n");
}
void CoreMA::errorOccurred(QProcess::ProcessError error) {
QObject::disconnect(process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, &CoreMA::finishedMatlab);
log->write(" Error: the analysis process crashes for unknown reasons.\n\n-- Finish Analysis --\n");
proView->stop();
}
void CoreMA::stopProcess() {
process->kill();
QObject::disconnect(process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, &CoreMA::finishedMatlab);
log->write("Error: process canceled by user.\n\n-- Finish Analysis --\n");
}
CorePy::CorePy(QWidget *parent, GeneralViewPy *gv, HiddenVarView *hv, LogView *lv){
general = gv;
hiddenVar = hv;
log = lv;
proView = new ProcessView(parent);
process = new QProcess();
QObject::connect(process, &QProcess::errorOccurred, this, &CorePy::errorOccurred);
}
void CorePy::runPython() {
log->write("\n-- Start Analysis --\n");
// Check whether video file exists
QFileInfo file(general->getVideoPath());
if (!file.exists())
return log->write("Error: video file does not exist and cannot be analyzed\n\n-- Finish Analysis --\n");
QFileInfo appfile(general->getAppPath());
if (!appfile.exists())
return log->write("Error: python script does not exist\n\n-- Finish Analysis --\n");
// Get parameters from General Tab
QString videoPath = file.path();
QString videoName = file.fileName();
// Write parameters to Log
log->write(" File path: " + videoPath);
log->write(" File name: " + videoName);
auto hvPrintArgument = hiddenVar->getPrintArguments(videoName);
for (int i = 0; i < hvPrintArgument.size(); i++) {
log->write(hvPrintArgument[i]);
}
// Pass parameters by arguments
QStringList arguments;
arguments << general->getAppPath() << videoPath << videoName << hiddenVar->getArguments(videoName);
// Run Python code
//input: videoPath, videoName, vars from template
log->write("\n Running Python Code...\n");
QObject::connect(process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, &CorePy::finishedPython);
#if defined(_WIN32) || defined(_WIN64) //Code for Windows
process->start("python", arguments);
#elif defined(__APPLE__) //Code for macOS
process->start("python3", arguments);
#endif
/* Output:
*/
proView->start();
}
void CorePy::finishedPython(int exitCode, QProcess::ExitStatus exitStatus) {
// clear up everthing
QObject::disconnect(process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, &CorePy::finishedPython);
proView->stop();
if (exitStatus == QProcess::CrashExit) {
log->write("Error: the analysis process crashes for unknown reasons.\n-- Finish Analysis --\n");
return;
}
if (!process->canReadLine()) {
log->write("Error: cannot read result from python script.\n-- Finish Analysis --\n");
return;
}
log->write(" Printing out results from python console...\n");
QByteArray result = process->readAllStandardOutput();
std::string line = "";
std::stringstream stream(result.toStdString());
// Read output result
std::string resVideo = "";
std::getline(stream, resVideo);
log->write(" " + QString::fromStdString(resVideo));
while (std::getline(stream, line)) {
log->write(" " + QString::fromStdString(line));
}
log->write("");
// Send result video to player
QFileInfo video(QString::fromStdString(resVideo));
if (video.exists())
general->setResultVideo(QString::fromStdString(resVideo));
else
log->write("Error: The first return result is not video full path.");
log->write("\n Using time: " + proView->timeCost() + "\n");
log->write("-- Finish Analysis --\n");
}
void CorePy::errorOccurred(QProcess::ProcessError error) {
QObject::disconnect(process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, &CorePy::finishedPython);
log->write(" Error: the analysis process crashes for unknown reasons.\n\n-- Finish Analysis --\n");
proView->stop();
}
void CorePy::stopProcess() {
process->kill();
QObject::disconnect(process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, &CorePy::finishedPython);
log->write("Error: process canceled by user.\n\n-- Finish Analysis --\n");
}