1
|
|
|
""" Unit testing module for pre-processing functions """ |
2
|
|
|
|
3
|
|
|
import unittest |
4
|
|
|
import string |
5
|
|
|
|
6
|
|
|
import crowdtruth |
7
|
|
|
from crowdtruth.configuration import DefaultConfig |
8
|
|
|
|
9
|
|
|
TEST_FILE_PREF = "test/test_data/load/" |
10
|
|
|
|
11
|
|
|
class TestConfig(DefaultConfig): |
12
|
|
|
inputColumns = ["input"] |
13
|
|
|
outputColumns = ["Answer.output"] |
14
|
|
|
open_ended_task = False |
15
|
|
|
annotation_separator = " " |
16
|
|
|
annotation_vector = list(string.ascii_uppercase) |
17
|
|
|
def processJudgments(self, judgments): |
18
|
|
|
return judgments |
19
|
|
|
|
20
|
|
|
class ConfigKeepEmptyRows(TestConfig): |
21
|
|
|
remove_empty_rows = False |
22
|
|
|
|
23
|
|
|
class ConfigProcessJudg(TestConfig): |
24
|
|
|
def processJudgments(self, judgments): |
25
|
|
|
for col in self.outputColumns: |
26
|
|
|
judgments[col] = judgments[col].apply(lambda x: str(x).lower()) |
27
|
|
|
return judgments |
28
|
|
|
|
29
|
|
|
class TestLoad(unittest.TestCase): |
30
|
|
|
test_conf_const = TestConfig() |
31
|
|
|
test_keep_empty_rows = ConfigKeepEmptyRows() |
32
|
|
|
test_process_judg = ConfigProcessJudg() |
33
|
|
|
|
34
|
|
|
def test_platform(self): |
35
|
|
|
for w in range(1, 6): |
36
|
|
|
test_config_amt = self.test_conf_const.__class__ |
37
|
|
|
data_amt, _ = crowdtruth.load( |
38
|
|
|
file=TEST_FILE_PREF + "platform_amt" + str(w) + ".csv", |
39
|
|
|
config=test_config_amt()) |
40
|
|
|
test_config_cf = self.test_conf_const.__class__ |
41
|
|
|
data_cf, _ = crowdtruth.load( |
42
|
|
|
file=TEST_FILE_PREF + "platform_cf" + str(w) + ".csv", |
43
|
|
|
config=test_config_cf()) |
44
|
|
|
self.assertEqual( |
45
|
|
|
(set(data_cf["units"]["duration"].keys()) - |
46
|
|
|
set(data_amt["units"]["duration"].keys())), |
47
|
|
|
set([])) |
48
|
|
|
self.assertEqual( |
49
|
|
|
(set(data_cf["workers"]["judgment"].keys()) - |
50
|
|
|
set(data_amt["workers"]["judgment"].keys())), |
51
|
|
|
set([])) |
52
|
|
|
self.assertEqual( |
53
|
|
|
set(data_cf["workers"]["judgment"] - data_amt["workers"]["judgment"]), |
54
|
|
|
set([0])) |
55
|
|
|
|
56
|
|
|
def test_folder(self): |
57
|
|
|
test_config = self.test_conf_const.__class__ |
58
|
|
|
data, _ = crowdtruth.load( |
59
|
|
|
directory=TEST_FILE_PREF + "dir/", |
60
|
|
|
config=test_config()) |
61
|
|
|
self.assertEqual(data["workers"].shape[0], 7) |
62
|
|
|
self.assertEqual(data["units"].shape[0], 2) |
63
|
|
|
self.assertEqual(data["judgments"].shape[0], 12) |
64
|
|
|
|
65
|
|
|
def test_empty_rows(self): |
66
|
|
|
test_without = self.test_conf_const.__class__ |
67
|
|
|
data_without, _ = crowdtruth.load( |
68
|
|
|
file=TEST_FILE_PREF + "empty_rows.csv", |
69
|
|
|
config=test_without()) |
70
|
|
|
self.assertEqual(data_without["judgments"].shape[0], 24) |
71
|
|
|
|
72
|
|
|
test_proc_judg = self.test_process_judg.__class__ |
73
|
|
|
data_proc_judg, _ = crowdtruth.load( |
74
|
|
|
file=TEST_FILE_PREF + "empty_rows.csv", |
75
|
|
|
config=test_proc_judg()) |
76
|
|
|
self.assertEqual(data_proc_judg["judgments"].shape[0], 24) |
77
|
|
|
|
78
|
|
|
test_with = self.test_keep_empty_rows.__class__ |
79
|
|
|
data_with, _ = crowdtruth.load( |
80
|
|
|
file=TEST_FILE_PREF + "empty_rows.csv", |
81
|
|
|
config=test_with()) |
82
|
|
|
self.assertEqual(data_with["judgments"].shape[0], 27) |
83
|
|
|
|