-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
preprocessing.py
778 lines (711 loc) · 25.2 KB
/
preprocessing.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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
#! /usr/bin/env python
# coding=utf-8
# Copyright (c) 2019 Uber Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import argparse
import logging
import os
import h5py
import numpy as np
import pandas as pd
import yaml
from ludwig.constants import *
from ludwig.constants import TEXT
from ludwig.data.concatenate_datasets import concatenate_csv
from ludwig.data.concatenate_datasets import concatenate_df
from ludwig.data.dataset import Dataset
from ludwig.features.feature_registries import base_type_registry
from ludwig.globals import MODEL_HYPERPARAMETERS_FILE_NAME
from ludwig.utils import data_utils
from ludwig.utils.data_utils import collapse_rare_labels
from ludwig.utils.data_utils import load_json
from ludwig.utils.data_utils import read_csv
from ludwig.utils.data_utils import split_dataset_tvt
from ludwig.utils.data_utils import text_feature_data_field
from ludwig.utils.defaults import default_preprocessing_parameters
from ludwig.utils.defaults import default_random_seed
from ludwig.utils.misc import get_from_registry
from ludwig.utils.misc import merge_dict
from ludwig.utils.misc import set_random_seed
def build_dataset(
dataset_csv,
features,
global_preprocessing_parameters,
train_set_metadata=None,
random_seed=default_random_seed,
**kwargs
):
dataset_df = read_csv(dataset_csv)
dataset_df.csv = dataset_csv
return build_dataset_df(
dataset_df,
features,
global_preprocessing_parameters,
train_set_metadata,
random_seed,
**kwargs
)
def build_dataset_df(
dataset_df,
features,
global_preprocessing_parameters,
train_set_metadata=None,
random_seed=default_random_seed,
**kwargs
):
global_preprocessing_parameters = merge_dict(
default_preprocessing_parameters,
global_preprocessing_parameters
)
if train_set_metadata is None:
train_set_metadata = build_metadata(
dataset_df,
features,
global_preprocessing_parameters
)
data_val = build_data(
dataset_df,
features,
train_set_metadata,
global_preprocessing_parameters
)
data_val['split'] = get_split(
dataset_df,
force_split=global_preprocessing_parameters['force_split'],
split_probabilities=global_preprocessing_parameters[
'split_probabilities'
],
stratify=global_preprocessing_parameters['stratify'],
random_seed=random_seed
)
return data_val, train_set_metadata
def build_metadata(dataset_df, features, global_preprocessing_parameters):
train_set_metadata = {}
for feature in features:
get_feature_meta = get_from_registry(
feature['type'],
base_type_registry
).get_feature_meta
if 'preprocessing' in feature:
preprocessing_parameters = merge_dict(
global_preprocessing_parameters[feature['type']],
feature['preprocessing']
)
else:
preprocessing_parameters = global_preprocessing_parameters[
feature['type']
]
train_set_metadata[feature['name']] = get_feature_meta(
dataset_df[feature['name']].astype(str),
preprocessing_parameters
)
return train_set_metadata
def build_data(
dataset_df,
features,
train_set_metadata,
global_preprocessing_parameters
):
data = {}
for feature in features:
add_feature_data = get_from_registry(
feature['type'],
base_type_registry
).add_feature_data
if 'preprocessing' in feature:
preprocessing_parameters = merge_dict(
global_preprocessing_parameters[feature['type']],
feature['preprocessing']
)
else:
preprocessing_parameters = global_preprocessing_parameters[
feature['type']
]
handle_missing_values(
dataset_df,
feature,
preprocessing_parameters
)
if feature['name'] not in train_set_metadata:
train_set_metadata[feature['name']] = {}
train_set_metadata[
feature['name']
]['preprocessing'] = preprocessing_parameters
add_feature_data(
feature,
dataset_df,
data,
train_set_metadata,
preprocessing_parameters
)
return data
def handle_missing_values(dataset_df, feature, preprocessing_parameters):
missing_value_strategy = preprocessing_parameters['missing_value_strategy']
if missing_value_strategy == FILL_WITH_CONST:
dataset_df[feature['name']] = dataset_df[feature['name']].fillna(
preprocessing_parameters['fill_value'],
)
elif missing_value_strategy == FILL_WITH_MODE:
dataset_df[feature['name']] = dataset_df[feature['name']].fillna(
dataset_df[feature['name']].value_counts().index[0],
)
elif missing_value_strategy == FILL_WITH_MEAN:
if feature['type'] != NUMERICAL:
raise ValueError(
'Filling missing values with mean is supported '
'only for numerical types',
)
dataset_df[feature['name']] = dataset_df[feature['name']].fillna(
dataset_df[feature['name']].mean(),
)
elif missing_value_strategy in ['backfill', 'bfill', 'pad', 'ffill']:
dataset_df[feature['name']] = dataset_df[feature['name']].fillna(
method=missing_value_strategy,
)
else:
raise ValueError('Invalid missing value strategy')
def get_split(
dataset_df,
force_split=False,
split_probabilities=(0.7, 0.1, 0.2),
stratify=None,
random_seed=default_random_seed,
):
if 'split' in dataset_df and not force_split:
split = dataset_df['split']
else:
set_random_seed(random_seed)
if stratify is None:
split = np.random.choice(
3,
len(dataset_df),
p=split_probabilities,
).astype(np.int8)
else:
split = np.zeros(len(dataset_df))
for val in dataset_df[stratify].unique():
idx_list = (
dataset_df.index[dataset_df[stratify] == val].tolist()
)
val_list = np.random.choice(
3,
len(idx_list),
p=split_probabilities,
).astype(np.int8)
split[idx_list] = val_list
return split
def load_data(
hdf5_file_path,
input_features,
output_features,
split_data=True,
shuffle_training=False
):
logging.info('Loading data from: {0}'.format(hdf5_file_path))
# Load data from file
hdf5_data = h5py.File(hdf5_file_path, 'r')
dataset = {}
for input_feature in input_features:
if input_feature['type'] == TEXT:
text_data_field = text_feature_data_field(input_feature)
dataset[text_data_field] = hdf5_data[text_data_field].value
else:
dataset[input_feature['name']] = hdf5_data[
input_feature['name']
].value
for output_feature in output_features:
if output_feature['type'] == TEXT:
dataset[text_feature_data_field(output_feature)] = hdf5_data[
text_feature_data_field(output_feature)
].value
else:
dataset[output_feature['name']] = hdf5_data[
output_feature['name']].value
if 'limit' in output_feature:
dataset[output_feature['name']] = collapse_rare_labels(
dataset[output_feature['name']],
output_feature['limit']
)
if not split_data:
hdf5_data.close()
return dataset
split = hdf5_data['split'].value
hdf5_data.close()
training_set, test_set, validation_set = split_dataset_tvt(dataset, split)
# shuffle up
if shuffle_training:
training_set = data_utils.shuffle_dict_unison_inplace(training_set)
return training_set, test_set, validation_set
def load_metadata(metadata_file_path):
logging.info('Loading metadata from: {0}'.format(metadata_file_path))
return data_utils.load_json(metadata_file_path)
def get_dataset_fun(dataset_type):
return get_from_registry(
dataset_type,
dataset_type_registry
)
dataset_type_registry = {
'generic': (
concatenate_csv,
concatenate_df,
build_dataset,
build_dataset_df
)
}
def preprocess_for_training(
model_definition,
dataset_type='generic',
data_df=None,
data_train_df=None,
data_validation_df=None,
data_test_df=None,
data_csv=None,
data_train_csv=None,
data_validation_csv=None,
data_test_csv=None,
data_hdf5=None,
data_train_hdf5=None,
data_validation_hdf5=None,
data_test_hdf5=None,
train_set_metadata_json=None,
skip_save_processed_input=False,
preprocessing_params=default_preprocessing_parameters,
random_seed=default_random_seed
):
# Check if hdf5 and json already exist
data_hdf5_fp = None
data_train_hdf5_fp = None
data_validation_hdf5_fp = None
data_test_hdf5_fp = None
train_set_metadata_json_fp = 'metadata.json'
if data_csv is not None:
data_hdf5_fp = data_csv.replace('csv', 'hdf5')
train_set_metadata_json_fp = data_csv.replace('csv', 'json')
if (os.path.isfile(data_hdf5_fp) and
os.path.isfile(train_set_metadata_json_fp)):
logging.info(
'Found hdf5 and json with the same filename '
'of the csv, using them instead'
)
data_csv = None
data_hdf5 = data_hdf5_fp
train_set_metadata_json = train_set_metadata_json_fp
if data_train_csv is not None:
data_train_hdf5_fp = data_train_csv.replace('csv', 'hdf5')
train_set_metadata_json_fp = data_train_csv.replace('csv', 'json')
if (os.path.isfile(data_train_hdf5_fp) and
os.path.isfile(train_set_metadata_json_fp)):
logging.info(
'Found hdf5 and json with the same filename of '
'the train csv, using them instead'
)
data_train_csv = None
data_train_hdf5 = data_train_hdf5_fp
train_set_metadata_json = train_set_metadata_json_fp
if data_validation_csv is not None:
data_validation_hdf5_fp = data_validation_csv.replace('csv', 'hdf5')
if os.path.isfile(data_validation_hdf5_fp):
logging.info(
'Found hdf5 with the same filename of '
'the validation csv, using it instead'
)
data_validation_csv = None
data_validation_hdf5 = data_validation_hdf5_fp
if data_test_csv is not None:
data_test_hdf5_fp = data_test_csv.replace('csv', 'hdf5')
if os.path.isfile(data_test_hdf5_fp):
logging.info(
'Found hdf5 with the same filename of '
'the validation csv, using it instead'
)
data_test_csv = None
data_test_hdf5 = data_test_hdf5_fp
model_definition['data_hdf5_fp'] = data_hdf5_fp
# Decide if to preprocess or just load
features = (model_definition['input_features'] +
model_definition['output_features'])
(
concatenate_csv,
concatenate_df,
build_dataset,
build_dataset_df
) = get_dataset_fun(dataset_type)
if data_df is not None:
# needs preprocessing
logging.info('Using full dataframe')
logging.info('Building dataset (it may take a while)')
data, train_set_metadata = build_dataset_df(
data_df,
features,
preprocessing_params,
random_seed=random_seed
)
if not skip_save_processed_input:
logging.info('Writing dataset')
data_utils.save_hdf5(data_hdf5_fp, data, train_set_metadata)
logging.info('Writing train set metadata with vocabulary')
data_utils.save_json(train_set_metadata_json_fp, train_set_metadata)
training_set, test_set, validation_set = split_dataset_tvt(
data,
data['split']
)
elif data_train_df is not None:
# needs preprocessing
logging.info('Using training dataframe')
logging.info('Building dataset (it may take a while)')
concatenated_df = concatenate_df(
data_train_df,
data_validation_df,
data_test_df
)
data, train_set_metadata = build_dataset_df(
concatenated_df,
features,
preprocessing_params,
random_seed=random_seed
)
training_set, test_set, validation_set = split_dataset_tvt(
data,
data['split']
)
if not skip_save_processed_input:
logging.info('Writing dataset')
data_utils.save_hdf5(
data_train_hdf5_fp,
training_set,
train_set_metadata
)
if validation_set is not None:
data_utils.save_hdf5(
data_validation_hdf5_fp,
validation_set,
train_set_metadata
)
if test_set is not None:
data_utils.save_hdf5(
data_test_hdf5_fp,
test_set,
train_set_metadata
)
logging.info('Writing train set metadata with vocabulary')
data_utils.save_json(train_set_metadata_json_fp, train_set_metadata)
elif data_csv is not None:
# Use data and ignore _train, _validation and _test.
# Also ignore data and train set metadata needs preprocessing
logging.info(
'Using full raw csv, no hdf5 and json file '
'with the same name have been found'
)
logging.info('Building dataset (it may take a while)')
data, train_set_metadata = build_dataset(
data_csv,
features,
preprocessing_params,
random_seed=random_seed
)
if not skip_save_processed_input:
logging.info('Writing dataset')
data_utils.save_hdf5(data_hdf5_fp, data, train_set_metadata)
logging.info('Writing train set metadata with vocabulary')
data_utils.save_json(train_set_metadata_json_fp, train_set_metadata)
training_set, test_set, validation_set = split_dataset_tvt(
data,
data['split']
)
elif data_train_csv is not None:
# use data_train (including _validation and _test if they are present)
# and ignore data and train set metadata
# needs preprocessing
logging.info(
'Using training raw csv, no hdf5 and json '
'file with the same name have been found'
)
logging.info('Building dataset (it may take a while)')
concatenated_df = concatenate_csv(
data_train_csv,
data_validation_csv,
data_test_csv
)
data, train_set_metadata = build_dataset_df(
concatenated_df,
features,
preprocessing_params,
random_seed=random_seed
)
training_set, test_set, validation_set = split_dataset_tvt(
data,
data['split']
)
if not skip_save_processed_input:
logging.info('Writing dataset')
data_utils.save_hdf5(
data_train_hdf5_fp,
training_set,
train_set_metadata
)
if validation_set is not None:
data_utils.save_hdf5(
data_validation_hdf5_fp,
validation_set,
train_set_metadata
)
if test_set is not None:
data_utils.save_hdf5(
data_test_hdf5_fp,
test_set,
train_set_metadata
)
logging.info('Writing train set metadata with vocabulary')
data_utils.save_json(train_set_metadata_json_fp, train_set_metadata)
elif data_hdf5 is not None and train_set_metadata_json is not None:
# use data and train set metadata
# doesn't need preprocessing, just load
logging.info('Using full hdf5 and json')
training_set, test_set, validation_set = load_data(
data_hdf5,
model_definition['input_features'],
model_definition['output_features'],
shuffle_training=True
)
train_set_metadata = load_metadata(train_set_metadata_json)
elif data_train_hdf5 is not None and train_set_metadata_json is not None:
# use data and train set metadata
# doesn't need preprocessing, just load
logging.info('Using hdf5 and json')
training_set = load_data(
data_train_hdf5,
model_definition['input_features'],
model_definition['output_features'],
split_data=False
)
train_set_metadata = load_metadata(train_set_metadata_json)
if data_validation_hdf5 is not None:
validation_set = load_data(
data_validation_hdf5,
model_definition['input_features'],
model_definition['output_features'],
split_data=False
)
else:
validation_set = None
if data_test_hdf5 is not None:
test_set = load_data(
data_test_hdf5,
model_definition['input_features'],
model_definition['output_features'],
split_data=False
)
else:
test_set = None
else:
raise RuntimeError('Insufficient input parameters')
replace_text_feature_level(
model_definition,
[training_set, validation_set, test_set]
)
training_dataset = Dataset(
training_set,
model_definition['input_features'],
model_definition['output_features'],
data_hdf5_fp
)
validation_dataset = Dataset(
validation_set,
model_definition['input_features'],
model_definition['output_features'],
data_hdf5_fp
)
test_dataset = Dataset(
test_set,
model_definition['input_features'],
model_definition['output_features'],
data_hdf5_fp
)
return (
training_dataset,
validation_dataset,
test_dataset,
train_set_metadata
)
def preprocess_for_prediction(
model_path,
split,
dataset_type='generic',
data_csv=None,
data_hdf5=None,
train_set_metadata=None,
only_predictions=False
):
"""Preprocesses the dataset to parse it into a format that is usable by the
Ludwig core
:param model_path: The input data that is joined with the model
hyperparameter file to create the model definition file
:type model_path: Str
:param dataset_type: Generic
:type: Str
:param split: Splits the data into the train and test sets
:param data_csv: The CSV input data file
:param data_hdf5: The hdf5 data file if there is no csv data file
:param train_set_metadata: Train set metadata for the input features
:param only_predictions: If False does not load output features
:returns: Dataset, Train set metadata
"""
model_definition = load_json(
os.path.join(model_path, MODEL_HYPERPARAMETERS_FILE_NAME)
)
preprocessing_params = merge_dict(
default_preprocessing_parameters,
model_definition['preprocessing']
)
# Check if hdf5 and json already exist
if data_csv is not None:
data_hdf5_fp = data_csv.replace('csv', 'hdf5')
if os.path.isfile(data_hdf5_fp):
logging.info(
'Found hdf5 with the same filename of the csv, using it instead'
)
data_csv = None
data_hdf5 = data_hdf5_fp
# Load data
_, _, build_dataset, _ = get_dataset_fun(dataset_type)
train_set_metadata = load_metadata(train_set_metadata)
features = (model_definition['input_features'] +
([] if only_predictions
else model_definition['output_features']))
if split == 'full':
if data_hdf5 is not None:
dataset = load_data(
data_hdf5,
model_definition['input_features'],
[] if only_predictions else model_definition['output_features'],
split_data=False, shuffle_training=False
)
else:
dataset, train_set_metadata = build_dataset(
data_csv,
features,
preprocessing_params,
train_set_metadata=train_set_metadata
)
else:
if data_hdf5 is not None:
training, test, validation = load_data(
data_hdf5,
model_definition['input_features'],
[] if only_predictions else model_definition['output_features'],
shuffle_training=False
)
if split == 'training':
dataset = training
elif split == 'validation':
dataset = validation
else: # if split == 'test':
dataset = test
else:
dataset, train_set_metadata = build_dataset(
data_csv,
features,
preprocessing_params,
train_set_metadata=train_set_metadata
)
replace_text_feature_level(model_definition, [dataset])
dataset = Dataset(
dataset,
model_definition['input_features'],
[] if only_predictions else model_definition['output_features'],
data_hdf5,
)
return dataset, train_set_metadata
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='This script takes csv files as input and outputs a HDF5 '
'and JSON file containing a dataset and the train set '
'metadata associated with it'
)
parser.add_argument(
'-id',
'--dataset_csv',
help='CSV containing contacts',
required=True
)
parser.add_argument(
'-ime',
'--train_set_metadata_json',
help='Input JSON containing metadata'
)
parser.add_argument(
'-od',
'--output_dataset_h5',
help='HDF5 containing output data',
required=True
)
parser.add_argument(
'-ome',
'--output_metadata_json',
help='JSON containing metadata',
required=True
)
parser.add_argument(
'-f',
'--features',
type=yaml.load,
help='list of features in the CSV to map to hdf5 and JSON files'
)
parser.add_argument(
'-p',
'--preprocessing_parameters',
type=yaml.load,
default='{}',
help='the parameters for preprocessing the different features'
)
parser.add_argument(
'-rs',
'--random_seed',
type=int,
default=42,
help='a random seed that is going to be used anywhere there is a call '
'to a random number generator: data splitting, parameter '
'initialization and training set shuffling'
)
args = parser.parse_args()
data, train_set_metadata = build_dataset(
args.dataset_csv,
args.train_set_metadata_json,
args.features,
args.preprocessing_parameters,
args.random_seed
)
# write train set metadata, dataset
logging.info('Writing train set metadata with vocabulary')
data_utils.save_json(args.output_metadata_json, train_set_metadata)
logging.info('Writing dataset')
data_utils.save_hdf5(args.output_dataset_h5, data, train_set_metadata)
def replace_text_feature_level(model_definition, datasets):
for feature in (model_definition['input_features'] +
model_definition['output_features']):
if feature['type'] == TEXT:
for dataset in datasets:
dataset[feature['name']] = dataset[
'{}_{}'.format(
feature['name'],
feature['level']
)
]
for level in ('word', 'char'):
del dataset[
'{}_{}'.format(
feature['name'],
level)
]