-
Notifications
You must be signed in to change notification settings - Fork 0
/
unittest_model.py
626 lines (458 loc) · 20.5 KB
/
unittest_model.py
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
import unittest
import numpy as np
import model.persistence as persistence
from desktop_app.src.view_model import ViewModel
DEFAULT_RF_PATH = "desktop_app/clf/random_forest_model.sav"
CONFIG_FILE_NAME_DESKTOP_APP = "desktop_app/resources/config.sample.json"
class TestAddFiles(unittest.TestCase, ViewModel):
def setUp(self) -> None:
ViewModel.__init__(self, persistence=persistence.Persistence(CONFIG_FILE_NAME_DESKTOP_APP))
def test_add_new_file(self) -> None:
file_name = "NewFile1"
self.add_files([file_name], lambda x: None)
self.assertEqual(self._opened_files.count(file_name), 1)
self.assertEqual(len(self._opened_files), 1)
def test_add_the_same_file_twice(self):
file_name = "NewFile1"
self.add_files([file_name], lambda x: None)
self.add_files([file_name], lambda x: None)
self.assertEqual(self._opened_files.count(file_name), 1)
self.assertEqual(len(self._opened_files), 1)
def test_add_multiple_files(self):
file_names = ["NewFile1", "NewFile2", "NewFile0", "NewFile1"]
self.add_files(file_names, lambda x: None)
self.assertEqual(self._opened_files.count(file_names[0]), 1)
self.assertEqual(self._opened_files.count(file_names[1]), 1)
self.assertEqual(self._opened_files.count(file_names[2]), 1)
self.assertEqual(len(self._opened_files), 3)
class TestDeleteFiles(unittest.TestCase, ViewModel):
def setUp(self) -> None:
ViewModel.__init__(self, persistence=persistence.Persistence(CONFIG_FILE_NAME_DESKTOP_APP))
def test_delete_file(self):
self._opened_files += ["NewFile1", "NewFile2", "NewFile0", "NewFile4"]
file_name = "NewFile0"
self.delete_files([file_name])
self.assertEqual(self._opened_files.count(file_name), 0)
self.assertEqual(len(self._opened_files), 3)
def test_delete_multiple_files(self):
self._opened_files += ["NewFile1", "NewFile2", "NewFile0", "NewFile4"]
file_names = ["NewFile0", "NewFile1"]
self.delete_files(file_names)
self.assertEqual(self._opened_files.count(file_names[0]), 0)
self.assertEqual(self._opened_files.count(file_names[1]), 0)
self.assertEqual(len(self._opened_files), 2)
class TestSaveTrainingInputFile(unittest.TestCase, ViewModel):
def setUp(self) -> None:
ViewModel.__init__(self, persistence=persistence.Persistence(CONFIG_FILE_NAME_DESKTOP_APP))
def test_save_file(self):
path = "D:/Desktop/test.tif"
self.save_training_input_file(path)
self.assertTrue(path in self._tag_ids.keys())
self.assertEqual(len(self._tag_ids.keys()), 1)
self.assertTrue(isinstance(self._tag_ids[path], dict))
def test_save_same_file_twice(self):
path = "D:/Desktop/test.tif"
self.save_training_input_file(path)
self.save_training_input_file(path)
self.assertTrue(path in self._tag_ids.keys())
self.assertEqual(len(self._tag_ids.keys()), 1)
self.assertTrue(isinstance(self._tag_ids[path], dict))
class TestSavePointOnCanvas(unittest.TestCase, ViewModel):
def setUp(self) -> None:
ViewModel.__init__(self, persistence=persistence.Persistence(CONFIG_FILE_NAME_DESKTOP_APP))
def test_save_point(self):
tag_id = 42
self.save_point_on_canvas(tag_id)
self.assertEqual(self._point_tag_ids.count(tag_id), 1)
self.assertEqual(len(self._point_tag_ids), 1)
class TestSaveNewC(unittest.TestCase, ViewModel):
def setUp(self) -> None:
ViewModel.__init__(self, persistence=persistence.Persistence(CONFIG_FILE_NAME_DESKTOP_APP))
def test_save_class(self):
training_file = "D:/Desktop/test.tif"
c_id = 1
c_name = "Garbage"
color = "#ffffff"
self.save_training_input_file(training_file)
self.save_new_c(training_file, c_id, c_name, color)
self.assertTrue(training_file in self._tag_ids.keys())
self.assertTrue(c_id in self._tag_ids[training_file].keys())
self.assertTrue(self._tag_ids[training_file][c_id] == [c_name, color, []])
self.assertEqual(len(self._tag_ids.keys()), 1)
self.assertEqual(len(self._tag_ids[training_file].keys()), 1)
def test_save_multiple_classes(self):
training_file_1 = "D:/Desktop/test_1.tif"
c_id_1 = 1
c_name_1 = "Garbage"
color_1 = "#ffffff"
training_file_2 = "D:/Desktop/test_2.tif"
c_id_2 = 2
c_name_2 = "Water"
color_2 = "#000000"
training_file_3 = "D:/Desktop/test_3.tif"
c_id_3 = 3
c_name_3 = "Forest"
color_3 = "#cccccc"
self.save_training_input_file(training_file_1)
self.save_training_input_file(training_file_2)
self.save_training_input_file(training_file_3)
self.save_new_c(training_file_1, c_id_1, c_name_1, color_1)
self.save_new_c(training_file_2, c_id_2, c_name_2, color_2)
self.save_new_c(training_file_3, c_id_3, c_name_3, color_3)
self.assertTrue(
all(file in self._tag_ids.keys() for file in [training_file_1, training_file_2, training_file_3])
)
self.assertTrue(c_id_1 in self._tag_ids[training_file_1].keys())
self.assertTrue(c_id_2 in self._tag_ids[training_file_2].keys())
self.assertTrue(c_id_3 in self._tag_ids[training_file_3].keys())
self.assertTrue(self._tag_ids[training_file_1][c_id_1] == [c_name_1, color_1, []])
self.assertTrue(self._tag_ids[training_file_2][c_id_2] == [c_name_2, color_2, []])
self.assertTrue(self._tag_ids[training_file_3][c_id_3] == [c_name_3, color_3, []])
self.assertEqual(len(self._tag_ids.keys()), 3)
self.assertEqual(len(self._tag_ids[training_file_1].keys()), 1)
self.assertEqual(len(self._tag_ids[training_file_2].keys()), 1)
self.assertEqual(len(self._tag_ids[training_file_3].keys()), 1)
self.save_new_c(training_file_1, c_id_3, c_name_3, color_3)
self.save_new_c(training_file_1, c_id_3, c_name_3, color_3)
self.assertTrue(c_id_3 in self._tag_ids[training_file_1].keys())
self.assertEqual(len(self._tag_ids[training_file_1].keys()), 2)
class TestDeleteC(unittest.TestCase, ViewModel):
def setUp(self) -> None:
ViewModel.__init__(self, persistence=persistence.Persistence(CONFIG_FILE_NAME_DESKTOP_APP))
def test_delete_class(self):
training_file_1 = "D:/Desktop/test_1.tif"
c_id_1 = 1
c_name_1 = "Garbage"
color_1 = "#ffffff"
c_id_2 = 2
c_name_2 = "Water"
color_2 = "#000000"
c_id_3 = 3
c_name_3 = "Forest"
color_3 = "#cccccc"
self.save_training_input_file(training_file_1)
self.save_new_c(training_file_1, c_id_1, c_name_1, color_1)
self.save_new_c(training_file_1, c_id_2, c_name_2, color_2)
self.save_new_c(training_file_1, c_id_3, c_name_3, color_3)
for i in range(6):
self.save_tag_id(training_file_1, c_id_1, c_name_1, color_1, i)
self.assertTrue(c_id_1 in self._tag_ids[training_file_1].keys())
self.assertEqual(len(self._tag_ids[training_file_1].keys()), 3)
deleted_tag_ids = self.delete_c(training_file_1, c_id_1)
self.assertEqual(deleted_tag_ids, [0, 1, 2, 3, 4, 5])
self.assertFalse(c_id_1 in self._tag_ids[training_file_1].keys())
self.assertEqual(len(self._tag_ids[training_file_1].keys()), 2)
deleted_tag_ids = self.delete_c(training_file_1, c_id_2)
self.assertEqual(deleted_tag_ids, [])
self.assertFalse(c_id_2 in self._tag_ids[training_file_1].keys())
self.assertEqual(len(self._tag_ids[training_file_1].keys()), 1)
class TestDeleteTagId(unittest.TestCase, ViewModel):
def setUp(self) -> None:
ViewModel.__init__(self, persistence=persistence.Persistence(CONFIG_FILE_NAME_DESKTOP_APP))
def test_delete_tag_ids(self):
training_file_1 = "D:/Desktop/test_1.tif"
c_id_1 = 1
c_name_1 = "Garbage"
color_1 = "#ffffff"
c_id_2 = 2
c_name_2 = "Water"
color_2 = "#000000"
self.save_training_input_file(training_file_1)
self.save_new_c(training_file_1, c_id_1, c_name_1, color_1)
self.save_new_c(training_file_1, c_id_2, c_name_2, color_2)
self.assertEqual(len(self._tag_ids[training_file_1].keys()), 2)
for i in range(6):
self.save_tag_id(training_file_1, c_id_1, c_name_1, color_1, i)
for i in range(1, 4):
self.delete_tag_id(training_file_1, i)
self.assertEqual(len(self._tag_ids[training_file_1][c_id_1][2]), 3)
self.assertEqual(self._tag_ids[training_file_1][c_id_1][2], [0, 4, 5])
self.assertEqual(len(self._tag_ids[training_file_1][c_id_2][2]), 0)
class TestSaveTagId(unittest.TestCase, ViewModel):
def setUp(self) -> None:
ViewModel.__init__(self, persistence=persistence.Persistence(CONFIG_FILE_NAME_DESKTOP_APP))
def test_save_tag_ids(self):
training_file_1 = "D:/Desktop/test_1.tif"
c_id_1 = 1
c_name_1 = "Garbage"
color_1 = "#ffffff"
self.save_training_input_file(training_file_1)
self.save_new_c(training_file_1, c_id_1, c_name_1, color_1)
for i in range(6):
self.save_tag_id(training_file_1, c_id_1, c_name_1, color_1, i)
self.assertEqual(self._tag_ids[training_file_1][c_id_1][2], [0, 1, 2, 3, 4, 5])
class TestSaveTagIdCoords(unittest.TestCase, ViewModel):
def setUp(self) -> None:
ViewModel.__init__(self, persistence=persistence.Persistence(CONFIG_FILE_NAME_DESKTOP_APP))
def test_save_tag_id_coord(self):
training_file_1 = "D:/Desktop/test_1.tif"
c_id_1 = 1
c_name_1 = "Garbage"
coords = [[(1, 2), (1, 3), (4, 2), (6, 9)], [0, 0]]
bbox_coords = [(1, 2, 3, 4), (0, 0, 0, 0)]
self.save_tag_id_coords(training_file_1, c_id_1, c_name_1, coords, bbox_coords)
self.assertTrue(training_file_1 in self._tag_id_coords.keys())
self.assertTrue(c_id_1 in self._tag_id_coords[training_file_1].keys())
self.assertEqual(len(self._tag_id_coords.keys()), 1)
self.assertEqual(len(self._tag_id_coords[training_file_1].keys()), 1)
self.assertEqual(self._tag_id_coords[training_file_1][c_id_1][1], coords)
self.assertEqual(self._tag_id_coords[training_file_1][c_id_1][2], bbox_coords)
class TestDeletePoints(unittest.TestCase, ViewModel):
def setUp(self) -> None:
ViewModel.__init__(self, persistence=persistence.Persistence(CONFIG_FILE_NAME_DESKTOP_APP))
def test_delete_points(self):
for i in range(42):
self.save_point_on_canvas(i)
deleted_points = self.delete_points()
self.assertEqual(deleted_points, list(range(42)))
self.assertEqual(len(self._point_tag_ids), 0)
class TestPlacePolygonOnCanvas(unittest.TestCase, ViewModel):
def setUp(self) -> None:
ViewModel.__init__(self, persistence=persistence.Persistence(CONFIG_FILE_NAME_DESKTOP_APP))
def test_not_enough_points(self):
self.save_point_on_canvas(33)
self.save_point_on_canvas(12)
tag_ids = self.place_polygon_on_canvas()
self.assertEqual(tag_ids, [])
self.assertEqual(len(self._point_tag_ids), 2)
def test_enough_points(self):
for i in range(100):
self.save_point_on_canvas(i)
tag_ids = self.place_polygon_on_canvas()
self.assertEqual(tag_ids, list(range(100)))
self.assertEqual(len(self._point_tag_ids), 0)
class TestCreateUsableTrainingData(unittest.TestCase, ViewModel):
def setUp(self) -> None:
ViewModel.__init__(self, persistence=persistence.Persistence(CONFIG_FILE_NAME_DESKTOP_APP))
def test_no_usable_one_file(self):
training_file_1 = "D:/Desktop/test_1.tif"
c_ids = [1, 2, 3]
test_dict = dict()
test_dict[training_file_1] = dict()
for c_id in c_ids:
test_dict[training_file_1][c_id] = ["name", [], []]
self._tag_id_coords = test_dict
usable_training_data, enough_data = self.create_usable_training_data()
self.assertFalse(enough_data)
self.assertEqual(usable_training_data, {})
def test_no_usable_more_files(self):
training_files = [
"D:/Desktop/test_1.tif",
"D:/Desktop/test_2.tif",
"D:/Desktop/test_3.tif",
]
c_ids = [1, 2, 3]
test_dict = dict()
for training_file in training_files:
test_dict[training_file] = dict()
for i in range(len(training_files)):
for c_id in c_ids[: i + 1]:
test_dict[training_files[i]][c_id] = ["name", [], []]
self.assertEqual(len(test_dict.keys()), 3)
for i in range(len(training_files)):
self.assertEqual(len(test_dict[training_files[i]].keys()), i + 1)
self._tag_id_coords = test_dict
usable_training_data, enough_data = self.create_usable_training_data()
self.assertFalse(enough_data)
self.assertEqual(usable_training_data, {})
def test_one_usable_one_file(self):
training_file_1 = "D:/Desktop/test_1.tif"
c_ids = [1, 2, 3]
test_dict = dict()
test_dict[training_file_1] = dict()
for c_id in c_ids:
if c_id == 2:
test_dict[training_file_1][c_id] = [
"name",
[[1.1, 2.2, 3.3, 4.4]],
[(1, 2, 3, 4)],
]
else:
test_dict[training_file_1][c_id] = ["name", [], []]
self._tag_id_coords = test_dict
usable_training_data, enough_data = self.create_usable_training_data()
self.assertFalse(enough_data)
self.assertEqual(
usable_training_data,
{training_file_1: {2: ["name", [[1.1, 2.2, 3.3, 4.4]], [(1, 2, 3, 4)]]}},
)
def test_one_usable_more_files(self):
training_files = [
"D:/Desktop/test_1.tif",
"D:/Desktop/test_2.tif",
"D:/Desktop/test_3.tif",
]
c_ids = [1, 2, 3]
test_dict = dict()
for training_file in training_files:
test_dict[training_file] = dict()
self.assertEqual(len(test_dict), 3)
for i in range(len(training_files)):
for c_id in c_ids[: i + 1]:
if i == 2 and c_id == 3:
test_dict[training_files[i]][c_id] = [
"name",
[[1.1, 2.2, 3.3, 4.4]],
[(1, 2, 3, 4)],
]
else:
test_dict[training_files[i]][c_id] = ["name", [], []]
self._tag_id_coords = test_dict
usable_training_data, enough_data = self.create_usable_training_data()
self.assertFalse(enough_data)
self.assertEqual(
usable_training_data,
{training_files[2]: {3: ["name", [[1.1, 2.2, 3.3, 4.4]], [(1, 2, 3, 4)]]}},
)
def test_more_usable_one_file(self):
training_file_1 = "D:/Desktop/test_1.tif"
c_ids = [1, 2, 3]
test_dict = dict()
test_dict[training_file_1] = dict()
for c_id in c_ids:
test_dict[training_file_1][c_id] = [
"name",
[[1.1, 2.2, 3.3, 4.4]],
[(1, 2, 3, 4)],
]
self._tag_id_coords = test_dict
usable_training_data, enough_data = self.create_usable_training_data()
self.assertTrue(enough_data)
self.assertEqual(
usable_training_data,
{
training_file_1: {
1: ["name", [[1.1, 2.2, 3.3, 4.4]], [(1, 2, 3, 4)]],
2: ["name", [[1.1, 2.2, 3.3, 4.4]], [(1, 2, 3, 4)]],
3: ["name", [[1.1, 2.2, 3.3, 4.4]], [(1, 2, 3, 4)]],
}
},
)
def test_more_usable_more_files(self):
training_file_1 = "D:/Desktop/test_1.tif"
training_file_2 = "D:/Desktop/test_2.tif"
c_ids = [1, 2, 3]
test_dict = dict()
test_dict[training_file_1] = dict()
test_dict[training_file_2] = dict()
for training_file in [training_file_1, training_file_2]:
for c_id in c_ids:
test_dict[training_file][c_id] = [
"name",
[[1.1, 2.2, 3.3, 4.4]],
[(1, 2, 3, 4)],
]
self._tag_id_coords = test_dict
usable_training_data, enough_data = self.create_usable_training_data()
self.assertTrue(enough_data)
self.assertEqual(usable_training_data, test_dict)
class TestGetCoordsInsidePolygon(unittest.TestCase, ViewModel):
def setUp(self) -> None:
ViewModel.__init__(self, persistence=persistence.Persistence(CONFIG_FILE_NAME_DESKTOP_APP))
def test_convex_polygon(self):
polygon_coords = [1.0, 1.0, 2.0, 5.0, 4.0, 3.0, 3.0, 1.0]
bbox_coords = (1, 1, 4, 5)
result = self.get_coords_inside_polygon(polygon_coords, bbox_coords)
expected = [(2, 2), (3, 2), (2, 3), (3, 3), (2, 4)]
self.assertEqual(len(result), len(expected))
for value in expected:
self.assertTrue(value in result)
def test_concave_polygon(self):
polygon_coords = [2.0, 7.0, 8.0, 1.0, 2.0, 3.0, 10.0, 9.0]
bbox_coords = (2, 1, 10, 9)
result = self.get_coords_inside_polygon(polygon_coords, bbox_coords)
expected = [
(6, 2),
(5, 3),
(4, 3),
(3, 3),
(4, 4),
(4, 6),
(5, 6),
(3, 7),
(4, 7),
(5, 7),
(6, 7),
(7, 7),
(7, 8),
(8, 8),
]
self.assertEqual(len(result), len(expected))
for value in expected:
self.assertTrue(value in result)
class TestCalculateIndex(unittest.TestCase, ViewModel):
def setUp(self) -> None:
ViewModel.__init__(self, persistence=persistence.Persistence(CONFIG_FILE_NAME_DESKTOP_APP))
self.shape = (3, 3)
def test_all_nan(self):
numerator = np.ndarray(
shape=self.shape,
dtype="float64",
)
denominator = np.ndarray(
shape=self.shape,
dtype="float64",
)
for i in range(self.shape[0]):
for j in range(self.shape[1]):
numerator[i, j] = float("NaN")
denominator[i, j] = float("NaN")
result = self.calculate_index(numerator, denominator)
self.assertTrue(np.array_equal(result, numerator, equal_nan=True))
self.assertTrue(np.array_equal(result, denominator, equal_nan=True))
def test_all_not_nan(self):
numerator = np.ndarray(
shape=self.shape,
dtype="float64",
)
denominator = np.ndarray(
shape=self.shape,
dtype="float64",
)
for i in range(self.shape[0]):
for j in range(self.shape[1]):
numerator[i, j] = i + j + 1
denominator[i, j] = i + j + 1
result = self.calculate_index(numerator, denominator)
self.assertTrue(np.all(result == 1))
def test_numerator_negative_denominator_zeros(self):
numerator = np.ndarray(
shape=self.shape,
dtype="float64",
)
denominator = np.zeros(
shape=self.shape,
dtype="float64",
)
for i in range(self.shape[0]):
for j in range(self.shape[1]):
numerator[i, j] = -(i + j + 1)
result = self.calculate_index(numerator, denominator)
self.assertTrue(np.all(result == -5))
def test_numerator_positive_denominator_zeros(self):
numerator = np.ndarray(
shape=self.shape,
dtype="float64",
)
denominator = np.zeros(
shape=self.shape,
dtype="float64",
)
for i in range(self.shape[0]):
for j in range(self.shape[1]):
numerator[i, j] = i + j + 1
result = self.calculate_index(numerator, denominator)
self.assertTrue(np.all(result == 5))
def test_numerator_zeros_denominator_zeros(self):
numerator = np.zeros(
shape=self.shape,
dtype="float64",
)
denominator = np.zeros(
shape=self.shape,
dtype="float64",
)
result = self.calculate_index(numerator, denominator)
self.assertTrue(np.all(np.isnan(result)))
if __name__ == "__main__":
unittest.main()