-
Notifications
You must be signed in to change notification settings - Fork 0
/
qual-city
executable file
·267 lines (223 loc) · 7.02 KB
/
qual-city
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
#! /usr/bin/env python3
# -*- coding: <utf-8> -*-
"""qualcity.
Usage:
qual-city (-h | --help)
qual-city pipeline <pipline_conf> [logger <log_conf>] [(-v | --verbose)]
Options:
-h --help Show this screen.
-v --verbose Verbose mode
"""
import time
import os
import docopt
import operator
import yaml
import logging
import logging.config
import matplotlib.pyplot as plt
import qualcity.features
import qualcity.labels
import qualcity.learning
import qualcity.utils
import qualcity.config
logger = logging.getLogger('qualcity')
def config_logger(arguments):
if not arguments['logger']:
logging.config.dictConfig(qualcity.config.logger_default)
logger.info('Default logger chosen.')
else:
with open(arguments['<log_conf>'], mode='r') as conf:
configuration = yaml.load(conf)
configuration['handlers']['file']['filename'] = (
'qualcity-' + time.ctime() + '.log'
)
configuration['loggers']['qualcity']['level'] = (
'DEBUG' if arguments['--verbose'] else 'INFO'
)
logging.config.dictConfig(
configuration
)
logger.info('Loaded logger from: ' + arguments['<log_conf>'])
logger.debug(yaml.dump(configuration))
def prepare_cache(cache_file):
if not os.path.isdir(cache_file):
os.mkdir(cache_file)
if not os.path.isdir(os.path.join(cache_file, 'predictions')):
os.mkdir(os.path.join(cache_file, 'predictions'))
if not os.path.isdir(os.path.join(cache_file, 'classifiers')):
os.mkdir(os.path.join(cache_file, 'classifiers'))
if not os.path.isdir(os.path.join(cache_file, 'features')):
os.mkdir(os.path.join(cache_file, 'features'))
if not os.path.isdir(os.path.join(cache_file, 'reports')):
os.mkdir(os.path.join(cache_file, 'reports'))
def get_labels(hierarchical, depth, LoD, threshold, labels_path, filetype):
logger.info('Getting Labels ...')
return [
(building, label)
for building, label in sorted(
qualcity.labels.labels_map(
labels_path,
hierarchical,
depth,
LoD,
threshold,
filetype
).items(),
key=operator.itemgetter(0)
)
]
def format_labels(hierarchical, depth, LoD, threshold, labels_path, filetype):
logger.info('Formatting labels')
labels = get_labels(
hierarchical,
depth,
LoD,
threshold,
labels_path,
filetype
)
if depth > 0:
labels = [
(building, label)
for building, label in labels
if label != 'Unqualifiable'
]
return zip(*labels)
def build_reductor(algorithm, **parameters):
logger.info('Building a dimension reductor...')
if parameters['n_components'] > 3:
logger.error('Cannot visualize more than three dimensions!')
raise ValueError
elif parameters['n_components'] < 2:
logger.error('Cannot visualize less than two dimensions!')
raise ValueError
return qualcity.utils.resolve(algorithm)(
**parameters
)
def visualize(features, labels, label_names, **visualization_args):
logger.info('Feature space visualization.')
qualcity.features.visualize_features(
features,
labels,
label_names,
build_reductor(
visualization_args['dimension_reduction']['algorithm'],
**visualization_args['dimension_reduction']['parameters']
),
visualization_args['dimension_reduction']['parameters']['n_components'],
**visualization_args['style']
)
logger.info('Visualization process ended.')
def process(form, features, labels, buildings, label_names, cache_file, cache_config, **process_args):
logger.info('Processing features...')
if 'visualization' in process_args.keys():
if form == 'vector':
visualize(
features,
labels,
label_names,
**process_args['visualization']
)
else:
raise RuntimeError('Impossible to visualize features in {} form'.format(form))
qualcity.learning.classify(
form,
features,
qualcity.learning.fuse(form, **process_args['fusion']),
labels,
buildings,
label_names,
cache_file,
cache_config,
**process_args['classification']
)
logger.info('Succesfully classified features.')
plt.show()
def load_pipeline_config(pip_conf):
logger.info('Loading pipeline configuration file...')
with open(pip_conf, mode='r') as conf:
return yaml.load(conf)
def label_names(config, labels):
if config['depth'] < 2:
return tuple(set(labels))
elif config['depth'] == 2:
return (
tuple(set(labels)) if config['hierarchical']
else ['Building', 'Facet']
)
elif config['depth'] == 3:
return (
{
'Valid': None,
'Building': qualcity.labels.LABELS(
config['LoD'],
['Building'],
drop=True
),
'Facet': qualcity.labels.LABELS(
config['LoD'],
['Facet']
)
} if config['hierarchical']
else qualcity.labels.LABELS(
config['LoD'],
['Building', 'Facet'],
drop=True
)
)
else:
raise LookupError('depth cannot be > 3')
def main():
arguments = docopt.docopt(
__doc__,
help=True,
version=qualcity.config.__version__,
options_first=False
)
config_logger(arguments)
configuration = load_pipeline_config(arguments['<pipline_conf>'])
logger.info('Pipeline loaded.')
prepare_cache(configuration['cache'])
logger.info('Cache prepared.')
buildings, labels = format_labels(
**configuration['labels']
)
logger.debug(
'There are %s buildings. Buildings are: %s',
len(buildings),
buildings
)
logger.debug('Labels are: %s', labels)
logger.info('Labels safely loaded.')
(form, features) = qualcity.features.get_features(
buildings,
configuration['cache'],
**configuration['features']
)
if form == 'kernel':
features_config, features = zip(*features)
print(features_config)
logger.info('Feature configuration:')
logger.info(features_config)
logger.debug(features)
logger.info('Features safely loaded.')
process(
form,
features,
labels,
buildings,
label_names(
configuration['labels'],
labels
),
configuration['cache'],
{
key: conf
for (key, conf) in configuration.items()
if key in ['labels', 'features']
},
**configuration['processing']
)
if __name__ == '__main__':
main()