-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
296 lines (245 loc) · 10.2 KB
/
main.c
File metadata and controls
296 lines (245 loc) · 10.2 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <raylib.h>
#define BAR_WIDTH 40
#define BAR_SPACING 20
#define CHART_HEIGHT 500
#define SCREEN_WIDTH 1400
#define SCREEN_HEIGHT 720
#define da_append(v, e) do { \
if ((v).size == (v).capacity) { \
(v).capacity = (v).capacity == 0 ? 1 : (v).capacity * 2; \
(v).reports = realloc((v).reports, sizeof(SmellReport) * (v).capacity); \
if (!(v).reports) { \
perror("Realloc failed"); \
exit(EXIT_FAILURE); \
} \
} \
(v).reports[(v).size++] = e; \
} while (0)
typedef enum {
GODCLASS,
DATACLASS,
FEATUREENVY,
LONGMETHOD,
LONGPARAMETERLIST,
} Smell;
typedef struct {
size_t fp;
size_t fn;
size_t tp;
size_t tn;
float accuracy;
float precision;
float recall;
float f1;
size_t fp0;
size_t fn0;
size_t tp0;
size_t tn0;
float accuracy0;
float precision0;
float recall0;
float f10;
Smell smell;
} SmellReport;
typedef struct {
char name[256];
SmellReport* reports;
size_t size;
size_t capacity;
} ModelReport;
typedef struct {
ModelReport* reports;
size_t size;
size_t capacity;
} Report;
static Report report = {0};
static Smell selected_smell = GODCLASS;
static void eval(SmellReport* report) {
report->accuracy = (float)(report->tp + report->tn) /
(report->tp + report->tn + report->fp + report->fn);
report->precision = report->tp ?
(float)report->tp / (report->tp + report->fp) : 0.0f;
report->recall = report->tp ?
(float)report->tp / (report->tp + report->fn) : 0.0f;
report->f1 = (report->precision + report->recall) > 0 ?
2 * (report->precision * report->recall) /
(report->precision + report->recall) : 0.0f;
report->accuracy0 = (float)(report->tp0 + report->tn0) /
(report->tp0 + report->tn0 + report->fp0 + report->fn0);
report->precision0 = report->tp0 ?
(float)report->tp0 / (report->tp0 + report->fp0) : 0.0f;
report->recall0 = report->tp0 ?
(float)report->tp0 / (report->tp0 + report->fn0) : 0.0f;
report->f10 = (report->precision0 + report->recall0) > 0 ?
2 * (report->precision0 * report->recall0) /
(report->precision0 + report->recall0) : 0.0f;
}
static int is_space(char c) {
return c == ' ' || c == '\t' || c == '\n';
}
static void add_models() {
FILE* file = fopen("inputs.txt", "r");
if (file == NULL) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
for (size_t i = 0; i < 5; i++) {
ModelReport* model_report = (ModelReport*)malloc(sizeof(ModelReport));
if (!model_report) {
perror("Malloc failed");
fclose(file);
exit(EXIT_FAILURE);
}
memset(model_report, 0, sizeof(ModelReport));
char line[256];
char c;
while (is_space(c = fgetc(file)));
ungetc(c, file);
if (fgets(line, sizeof(line), file) != NULL) {
sscanf(line, "%255s", model_report->name);
}
for (size_t j = 0; j < 5; j++) {
SmellReport smell_report = {0};
smell_report.smell = j;
fscanf(file, "%lu %lu %lu %lu", &smell_report.tn, &smell_report.fn, &smell_report.fp, &smell_report.tp);
smell_report.tn0 = smell_report.tp;
smell_report.fn0 = smell_report.fp;
smell_report.fp0 = smell_report.fn;
smell_report.tp0 = smell_report.tn;
eval(&smell_report);
da_append(*model_report, smell_report);
}
if (report.size == report.capacity) {
report.capacity = report.capacity == 0 ? 1 : report.capacity * 2;
report.reports = realloc(report.reports, sizeof(ModelReport) * report.capacity);
if (!report.reports) {
perror("Realloc failed");
fclose(file);
exit(EXIT_FAILURE);
}
}
da_append(report, *model_report);
}
fclose(file);
}
static int screenshot_mode = 0;
static int zero_mode = 0;
static void render_chart() {
const Color metricColors[] = { RED, GREEN, BLUE, ORANGE }; // Colors for metrics
const char* metricLabels[] = { "Precision", "Recall", "F1", "Accuracy" };
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Models Visualization");
Font customFont = LoadFontEx("arial.ttf", 20, NULL, 0); // Custom font
if (customFont.texture.id == 0) {
printf("Error: Failed to load font.\n");
CloseWindow();
return;
}
SetTargetFPS(60);
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(RAYWHITE);
const int chartX = 100, chartY = 50;
const int legendWidth = 150;
const int chartWidth = SCREEN_WIDTH - chartX - legendWidth - 20;
const int chartHeight = CHART_HEIGHT;
DrawLine(chartX, chartY + chartHeight, chartX + chartWidth, chartY + chartHeight, BLACK); // X-axis
DrawLine(chartX, chartY, chartX, chartY + chartHeight, BLACK); // Y-axis
for (int i = 1; i <= 10; i++) {
int y = chartY + chartHeight - (i * chartHeight / 10);
DrawLine(chartX, y, chartX + chartWidth, y, LIGHTGRAY); // Horizontal gridline
DrawTextEx(customFont, TextFormat("%.1f", (float)i / 10), (Vector2){ chartX - 30, y - 10 }, 20, 2, GRAY); // Y-axis labels
}
int numModels = report.size;
int barGroupWidth = (chartWidth - (numModels + 1) * BAR_SPACING) / numModels;
int barWidth = barGroupWidth / 4;
for (int i = 0; i < numModels; i++) {
ModelReport model = report.reports[i];
int groupX = chartX + BAR_SPACING + i * (barGroupWidth + BAR_SPACING);
// Draw bars for each metric for the selected smell
SmellReport* smell_report = NULL;
for (size_t j = 0; j < model.size; j++) {
if (model.reports[j].smell == selected_smell) {
smell_report = &model.reports[j];
break;
}
}
if(IsKeyPressed(KEY_SPACE)){
zero_mode = !zero_mode;
}
if (smell_report) {
int groupX = chartX + BAR_SPACING + i * (barGroupWidth + BAR_SPACING);
// Draw bars for each metric
for (int j = 0; j < 4; j++) {
float value = 0.0f;
if (!zero_mode) {
switch (j) {
case 0: value = smell_report->recall; break;
case 1: value = smell_report->precision; break;
case 2: value = smell_report->f1; break;
case 3: value = smell_report->accuracy; break;
}
} else{
switch (j) {
case 0: value = smell_report->recall0; break;
case 1: value = smell_report->precision0; break;
case 2: value = smell_report->f10; break;
case 3: value = smell_report->accuracy0; break;
}
}
int barHeight = (int)(value * chartHeight);
int barX = groupX + j * barWidth;
int barY = chartY + chartHeight - barHeight;
DrawRectangle(barX, barY, barWidth, barHeight, metricColors[j]);
DrawTextEx(customFont, TextFormat("%.3f", value), (Vector2){ barX, barY - 20 }, 20, 1, DARKGRAY);
}
}
// Model name below the bars
DrawTextEx(customFont, model.name, (Vector2){ groupX, chartY + chartHeight + 10 }, 20, 2, DARKGRAY);
}
// Metric legends
int legendX = chartX + chartWidth + 20;
int legendY = chartY;
for (int i = 0; i < 4; i++) {
DrawRectangle(legendX, legendY + i * 30, 20, 20, metricColors[i]); // Legend color box
DrawTextEx(customFont, metricLabels[i], (Vector2){ legendX + 30, legendY + i * 30 }, 20, 2, BLACK); // Metric name
}
if (IsKeyPressed(KEY_R)) {
screenshot_mode = !screenshot_mode;
}
if(!screenshot_mode) {
// Draw buttons for each smell
int buttonWidth = 240;
int buttonHeight = 40;
int buttonY = chartY + chartHeight + 50;
for (int i = 0; i < 5; i++) {
int buttonX = chartX + i * (buttonWidth + 10);
Rectangle buttonRect = { buttonX, buttonY, buttonWidth, buttonHeight };
Color buttonColor = ((int)selected_smell == i) ? LIGHTGRAY : GRAY;
if (CheckCollisionPointRec(GetMousePosition(), buttonRect) && IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
selected_smell = i;
}
DrawRectangleRec(buttonRect, buttonColor);
DrawTextEx(customFont, TextFormat("%s", (i == 0 ? "GODCLASS" : i == 1 ? "DATACLASS" : i == 2 ? "FEATUREENVY" : i == 3 ? "LONGMETHOD" : "LONGPARAMETERLIST")), (Vector2){ buttonX + 10, buttonY + 10 }, 20, 2, BLACK);
}
}
DrawTextEx(customFont, TextFormat("%s", (selected_smell == 0 ? "GODCLASS" : selected_smell == 1 ? "DATACLASS" : selected_smell == 2 ? "FEATUREENVY" : selected_smell == 3 ? "LONGMETHOD" : "LONGPARAMETERLIST")), (Vector2){ 10, 10 }, 20, 2, BLACK);
DrawTextEx(customFont, TextFormat("Class: %s", zero_mode ? "0" : "1"), (Vector2){ 250, 10 }, 20, 2, BLACK);
EndDrawing();
if (IsKeyPressed(KEY_S)) {
char* smell = (selected_smell == 0 ? "GODCLASS" : selected_smell == 1 ? "DATACLASS" : selected_smell == 2 ? "FEATUREENVY" : selected_smell == 3 ? "LONGMETHOD" : "LONGPARAMETERLIST");
char* zero = zero_mode ? "0" : "1";
TakeScreenshot(TextFormat("chart_%s_%s.png", smell, zero));
}
}
// Clean up
UnloadFont(customFont);
CloseWindow();
}
int main() {
add_models();
render_chart();
return EXIT_SUCCESS;
}