-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_selectURLs.py
More file actions
226 lines (183 loc) · 7.92 KB
/
Copy pathrun_selectURLs.py
File metadata and controls
226 lines (183 loc) · 7.92 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" run_labeller is the main script for the labelling application.
It must be invoked using
python run_labeler.py [project_folder]
Created on July, 2015
Last update: Jan, 2017
@autor: Jesus Cid.
"""
import ast
import sys
import os
import shutil
# Local imports
from labelfactory.ConfigCfg import ConfigCfg as Cfg
from labelfactory.Log import Log
from labelfactory.labeling.datamanager import DataManager
CF_FNAME = "config.cf"
CF_DEFAULT_PATH = "./config.cf.default"
def main():
# To complete the migration to python 3, I should replace all "raw_input"
# by "input". Transitorily, to preserve compatibility with python 2, I
# simply rename inut to raw_input
if sys.version_info.major == 3:
raw_input2 = input
else:
raw_input2 = raw_input
#######
# Start
#######
# Check if project folder exists. Otherwise create a default one.
if len(sys.argv) > 1:
project_path = sys.argv[1]
else:
project_path = raw_input2("Select the (absolute or relative) path to" +
" the labeling project folder: ")
if not project_path.endswith('/'):
project_path = project_path + '/'
# Check if project folder exists. This is necessary to follow
if not os.path.isdir(project_path):
createfolder = raw_input2("Folder " + project_path +
"does not exist. Create? (y/n)")
if createfolder == "y":
os.makedirs(project_path) # Create folder
else:
sys.exit("A route to the project folder must be provided.\n")
#########################
# Read configuration data
#########################
# Check if configuration file existe
config_path = project_path + CF_FNAME
if not os.path.isfile(config_path):
shutil.copy(CF_DEFAULT_PATH, config_path)
print("The project folder must contain a configuration file.")
print("A default configuration file has been copied.")
print("Please check and modify it if necessary before continuing.")
# Read data from the configuation file
cf = Cfg(config_path)
# Data source and destination (options: file, mongodb)
source_type = cf.get('DataPaths', 'source_type')
dest_type = cf.get('DataPaths', 'dest_type')
# This is for backward compatibility
if source_type is None:
source_type = 'file'
if dest_type is None:
dest_type = 'file'
# Mongo DB settings
if source_type == 'mongodb' or dest_type == 'mongodb':
db_info = {'name': cf.get('DataPaths', 'db_name'),
'hostname': cf.get('DataPaths', 'db_hostname'),
'user': cf.get('DataPaths', 'db_user'),
'pwd': cf.get('DataPaths', 'db_pwd'),
'label_coll_name': cf.get(
'DataPaths', 'db_label_coll_name'),
'history_coll_name': cf.get(
'DataPaths', 'db_history_coll_name'),
'port': cf.get('DataPaths', 'db_port'),
'mode': cf.get('DataPaths', 'db_mode')}
else:
db_info = None
# Read file and folder names related to the dataset files.
if source_type == 'file' or dest_type == 'file':
file_info = {'project_path': project_path,
'input_folder': cf.get('DataPaths', 'input_folder'),
'output_folder': cf.get('DataPaths', 'output_folder'),
'used_folder': cf.get('DataPaths', 'used_folder'),
'dataset_fname': cf.get('DataPaths', 'dataset_fname'),
'labelhistory_fname': cf.get('DataPaths',
'labelhistory_fname'),
'labels_endname': cf.get('DataPaths', 'labels_endname'),
'preds_endname': cf.get('DataPaths', 'preds_endname'),
'urls_fname': cf.get('DataPaths', 'urls_fname')}
# 'recycle_endname': cf.get('DataPaths', 'recycle_endname')
else:
# The prediction file is stored in files, so this is still required
file_info = {'project_path': project_path,
'input_folder': cf.get('DataPaths', 'input_folder'),
'dataset_fname': cf.get('DataPaths', 'dataset_fname'),
'preds_endname': cf.get('DataPaths', 'preds_endname')}
# Type of wid: if 'yes', the wid is computed as a transformed url.
# if 'no', the wid is taken equal to the url.
compute_wid = cf.get('Labeler', 'compute_wid')
# List of categories to label.
categories = ast.literal_eval(cf.get('Labeler', 'categories'))
parentcat = ast.literal_eval(cf.get('Labeler', 'parentcat'))
fill_with_Other = cf.get('Labeler', 'fill_with_Other')
# Possible labels for each category
yes_label = cf.get('Labeler', 'yes_label')
no_label = cf.get('Labeler', 'no_label')
unknown_label = cf.get('Labeler', 'unknown_label')
error_label = cf.get('Labeler', 'error_label')
alphabet = {'yes': yes_label, 'no': no_label, 'unknown': unknown_label,
'error': error_label}
unknown_pred = int(cf.get('Labeler', 'unknown_pred'))
# In multiclass cases, the reference class is the class used by the active
# learning algorithm to compute the sample scores.
ref_class = cf.get('ActiveLearning', 'ref_class')
##########
# Log file
##########
# Create the log object
log = Log(project_path + 'log')
# Starting message for the log output.
log.info('*****************************')
log.info('****** WEB LABELER **********')
############################
# Pre-processing config data
############################
# Verify that ref_class is one of the given categories
if ref_class not in categories:
log.error("The reference class is not in the set of categories. " +
"Revise the assignment to ref_class in the config.cf file")
sys.exit()
# Include the negative class in the category tree.
if fill_with_Other.lower() == 'yes':
extra_class = 'Other'
while extra_class.lower() in [c.lower() for c in categories]:
extra_class += 'X'
categories.append(extra_class)
# Include categories without parents in the parent dictionary.
for cat in categories:
if cat not in parentcat:
parentcat[cat] = None
#####################
# Create main objects
#####################
# Data manager object
data_mgr = DataManager(source_type, dest_type, file_info, db_info,
categories, parentcat, ref_class, alphabet,
compute_wid, unknown_pred)
###################
# Read all datasets
###################
log.info('Carga de datos')
# Load data from the standard datasets.
df_labels, df_preds, labelhistory = data_mgr.loadData(source_type)
# Read dataset
log.info("-- Cargando datos del repositorio")
preds, labels, urls, markers, relabels, weights, userIds = \
data_mgr.getDataset(df_labels, df_preds)
if len(preds) == 0 and len(labels) == 0:
log.error(u"El repositorio de datos está vacío")
sys.exit()
if len(urls) == 0:
log.error(u"El repositorio de datos no tiene urls. Añada un fichero " +
file_info['urls_fname'] + ".csv a la subcarpeta " +
file_info['input_folder'] + " del proyecto")
sys.exit()
#############
# Select URLs
#############
for w in df_labels.index:
# Write your selection condition here:
if df_labels.loc[w]['label', 'Holding-'] == yes_label:
date = df_labels.loc[w]['info', 'date']
try:
if date.year == 2017:
print('www.' + w)
except:
print("Error in {0}".format(w))
print(date)
if __name__ == "__main__":
main()