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