forked from secretflow/secretflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_utils.py
685 lines (582 loc) · 21.7 KB
/
data_utils.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
# Copyright 2023 Ant Group Co., Ltd.
#
# 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
#
# https://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 enum
import json
import logging
import os
import uuid
from dataclasses import dataclass
from typing import Any, Dict, List, Tuple
import numpy as np
import pandas as pd
from secretflow.data.core.io import read_file_meta
from secretflow.data.vertical import read_csv
from secretflow.data.vertical.dataframe import VDataFrame
from secretflow.device.device.pyu import PYU, PYUObject
from secretflow.device.device.spu import SPU, SPUObject
from secretflow.device.driver import DeviceObject, reveal, wait
from secretflow.spec.extend.data_pb2 import DeviceObjectCollection
from secretflow.spec.v1.component_pb2 import IoDef
from secretflow.spec.v1.data_pb2 import (
DistData,
IndividualTable,
SystemInfo,
TableSchema,
VerticalTable,
)
class MetaEnum(enum.EnumMeta):
def __contains__(cls, item):
try:
cls(item)
except ValueError:
return False
return True
class BaseEnum(enum.Enum, metaclass=MetaEnum):
def __repr__(self):
return self.value
def __str__(self):
return self.__repr__()
def __eq__(self, other):
return str(self) == str(other)
@enum.unique
class DistDataType(BaseEnum):
# tables
VERTICAL_TABLE = "sf.table.vertical_table"
INDIVIDUAL_TABLE = "sf.table.individual"
# models
SS_SGD_MODEL = "sf.model.ss_sgd"
HESS_SGD_MODEL = "sf.model.hess_sgd"
SS_GLM_MODEL = "sf.model.ss_glm"
SGB_MODEL = "sf.model.sgb"
SS_XGB_MODEL = "sf.model.ss_xgb"
# binning rule
BIN_RUNNING_RULE = "sf.rule.binning"
# others preprocessing rules
PREPROCESSING_RULE = "sf.rule.preprocessing"
# report
REPORT = "sf.report"
# read data
READ_DATA = "sf.read_data"
@enum.unique
class DataSetFormatSupported(BaseEnum):
CSV = "csv"
SUPPORTED_VTABLE_DATA_TYPE = {
"int8": np.int8,
"int16": np.int16,
"int32": np.int32,
"int64": np.int64,
"uint8": np.uint8,
"uint16": np.uint16,
"uint32": np.uint32,
"uint64": np.uint64,
"float16": np.float16,
"float32": np.float32,
"float64": np.float64,
"bool": bool,
"int": int,
"float": float,
"str": object,
}
REVERSE_DATA_TYPE_MAP = dict((v, k) for k, v in SUPPORTED_VTABLE_DATA_TYPE.items())
def check_io_def(io_def: IoDef):
for t in io_def.types:
if t not in DistDataType:
raise ValueError(
f"IoDef {io_def.name}: {t} is not a supported DistData types"
)
def check_dist_data(data: DistData, io_def: IoDef = None):
if io_def is not None:
check_io_def(io_def)
if data.type not in list(io_def.types):
raise ValueError(
f"DistData {data.name}: type {data.type} is not allowed according to io def {io_def.types}."
)
if data.type == DistDataType.INDIVIDUAL_TABLE:
if len(data.data_refs) > 1:
raise f"DistData {data.name}: data_refs is greater than 1 for {data.type}"
@dataclass
class DistdataInfo:
uri: str
format: str
def extract_distdata_info(
db: DistData,
) -> Dict[str, DistdataInfo]:
ret = {}
for dr in db.data_refs:
ret[dr.party] = DistdataInfo(dr.uri, dr.format)
return ret
def merge_individuals_to_vtable(srcs: List[DistData], dest: DistData) -> DistData:
# copy srcs' schema into dist
# use for union individual tables into vtable
vmeta = VerticalTable()
for s in srcs:
assert s.type == DistDataType.INDIVIDUAL_TABLE
imeta = IndividualTable()
assert s.meta.Unpack(imeta)
vmeta.schemas.append(imeta.schema)
vmeta.line_count = imeta.line_count
dest.meta.Pack(vmeta)
return dest
def extract_table_header(
db: DistData,
load_features: bool = False,
load_labels: bool = False,
load_ids: bool = False,
feature_selects: List[str] = None,
col_selects: List[str] = None,
col_excludes: List[str] = None,
return_schema_names: bool = False,
) -> Dict[str, Dict[str, np.dtype]]:
"""
Args:
db (DistData): input DistData.
load_features (bool, optional): Whether to load feature cols. Defaults to False.
load_labels (bool, optional): Whether to load label cols. Defaults to False.
load_ids (bool, optional): Whether to load id cols. Defaults to False.
feature_selects (List[str], optional): Load part of feature cols. Only in effect if load_features is True. Defaults to None.
col_selects (List[str], optional): Load part of cols. Applies to all cols. Defaults to None. Couldn't use with col_excludes.
col_excludes (List[str], optional): Load all cols exclude these. Applies to all cols. Defaults to None. Couldn't use with col_selects.
return_schema_names (bool, optional): if True, also return schema names Dict[str, List[str]]
"""
meta = (
IndividualTable()
if db.type.lower() == DistDataType.INDIVIDUAL_TABLE
else VerticalTable()
)
db.meta.Unpack(meta)
schemas = (
[meta.schema]
if db.type.lower() == DistDataType.INDIVIDUAL_TABLE
else meta.schemas
)
if feature_selects is not None:
feature_selects = set(feature_selects)
if col_selects is not None:
col_selects = set(col_selects)
if col_excludes is not None:
col_excludes = set(col_excludes)
if col_selects is not None and col_excludes is not None:
intersection = set.intersection(col_selects, col_excludes)
assert (
len(intersection) == 0
), f'The following items are in both col_selects and col_excludes : {intersection}, which is not allowed.'
ret = dict()
schema_names = {}
labels = {}
features = {}
ids = {}
for slice, dr in zip(schemas, db.data_refs):
smeta = dict()
party_labels = []
party_features = []
party_ids = []
if load_features:
for t, h in zip(slice.feature_types, slice.features):
if feature_selects is not None:
if h not in feature_selects:
# feature not selected, skip
continue
feature_selects.remove(h)
if col_selects is not None:
if h not in col_selects:
# feature not selected, skip
continue
col_selects.remove(h)
if col_excludes is not None:
if h in col_excludes:
continue
t = t.lower()
assert (
t in SUPPORTED_VTABLE_DATA_TYPE
), f"The feature type {t} is not supported"
if return_schema_names:
party_features.append(h)
smeta[h] = SUPPORTED_VTABLE_DATA_TYPE[t]
if load_labels:
for t, h in zip(slice.label_types, slice.labels):
if col_selects is not None:
if h not in col_selects:
# label not selected, skip
continue
col_selects.remove(h)
if col_excludes is not None:
if h in col_excludes:
continue
if return_schema_names:
party_labels.append(h)
smeta[h] = SUPPORTED_VTABLE_DATA_TYPE[t]
if load_ids:
for t, h in zip(slice.id_types, slice.ids):
if col_selects is not None:
if h not in col_selects:
# id not selected, skip
continue
col_selects.remove(h)
if col_excludes is not None:
if h in col_excludes:
continue
if return_schema_names:
party_ids.append(h)
smeta[h] = SUPPORTED_VTABLE_DATA_TYPE[t]
if len(smeta):
ret[dr.party] = smeta
labels[dr.party] = party_labels
features[dr.party] = party_features
ids[dr.party] = party_ids
schema_names["labels"] = labels
schema_names["features"] = features
schema_names["ids"] = ids
if feature_selects is not None and len(feature_selects) > 0:
raise AttributeError(f"unknown features {feature_selects} in feature_selects")
if col_selects is not None and len(col_selects) > 0:
raise AttributeError(f"unknown cols {col_selects} in col_selects")
if return_schema_names:
return ret, schema_names
return ret
def load_table(
ctx,
db: DistData,
load_features: bool = False,
load_labels: bool = False,
load_ids: bool = False,
feature_selects: List[str] = None, # if None, load all features
col_selects: List[str] = None, # if None, load all cols
col_excludes: List[str] = None,
return_schema_names: bool = False,
nrows: int = None,
) -> VDataFrame:
assert load_features or load_labels or load_ids, "At least one flag should be true"
assert (
db.type.lower() == DistDataType.INDIVIDUAL_TABLE
or db.type.lower() == DistDataType.VERTICAL_TABLE
), f"path format {db.type.lower()} should be sf.table.individual or sf.table.vertical_table"
if return_schema_names:
v_headers, schema_names = extract_table_header(
db,
load_features=load_features,
load_labels=load_labels,
load_ids=load_ids,
feature_selects=feature_selects,
col_selects=col_selects,
col_excludes=col_excludes,
return_schema_names=True,
)
else:
v_headers = extract_table_header(
db,
load_features=load_features,
load_labels=load_labels,
load_ids=load_ids,
feature_selects=feature_selects,
col_selects=col_selects,
col_excludes=col_excludes,
)
parties_path_format = extract_distdata_info(db)
for p in v_headers:
assert (
p in parties_path_format
), f"schema party {p} is not in dataref parties {v_headers.keys()}"
# only support csv for now, skip type distribute
assert (
parties_path_format[p].format.lower() in DataSetFormatSupported
), f"Illegal path format: {parties_path_format[p].format.lower()}, path format of party {p} should be in DataSetFormatSupported"
# TODO: assert system_info
with ctx.tracer.trace_io():
pyus = {p: PYU(p) for p in v_headers}
filepaths = {
pyus[p]: os.path.join(ctx.local_fs_wd, parties_path_format[p].uri)
for p in v_headers
}
dtypes = {pyus[p]: v_headers[p] for p in v_headers}
vdf = read_csv(filepaths, dtypes=dtypes, nrows=nrows)
wait(vdf)
if return_schema_names:
return vdf, schema_names
return vdf
def load_table_select_and_exclude_pair(
ctx,
db: DistData,
load_features: bool = False,
load_labels: bool = False,
load_ids: bool = False,
col_selects: List[str] = None, # if None, load all cols
to_pandas: bool = True,
nrows: int = None,
):
"""
Load two tables, one is the selected, another is the complement.
"""
trans_x = load_table(
ctx,
db,
load_features,
load_labels,
load_ids,
col_selects=col_selects,
nrows=nrows,
)
remain_x = load_table(
ctx,
db,
load_features=True,
load_ids=True,
load_labels=True,
col_excludes=col_selects,
nrows=nrows,
)
if to_pandas:
trans_x = trans_x.to_pandas()
remain_x = remain_x.to_pandas()
return trans_x, remain_x
def move_feature_to_label(schema: TableSchema, label: str) -> TableSchema:
new_schema = TableSchema()
new_schema.CopyFrom(schema)
if label in list(schema.features) and label not in list(schema.labels):
new_schema.ClearField('features')
new_schema.ClearField('feature_types')
for k, v in zip(list(schema.features), list(schema.feature_types)):
if k != label:
new_schema.features.append(k)
new_schema.feature_types.append(v)
else:
label_type = v
new_schema.labels.append(label)
new_schema.label_types.append(label_type)
return new_schema
@dataclass
class VerticalTableWrapper:
line_count: int
schema_map: Dict[str, TableSchema]
def to_vertical_table(self, order: List[str] = None):
if order is None:
schemas = list(self.schema_map.values())
else:
schemas = [self.schema_map[k] for k in order]
return VerticalTable(schemas=schemas, line_count=self.line_count)
@classmethod
def from_dist_data(cls, data: DistData, line_count: int = None):
meta = VerticalTable()
assert data.meta.Unpack(meta)
return cls(
line_count=meta.line_count if line_count is None else line_count,
schema_map={
data_ref.party: schema
for data_ref, schema in zip(list(data.data_refs), list(meta.schemas))
},
)
def dump_vertical_table(
ctx,
v_data: VDataFrame,
uri: str,
meta: VerticalTableWrapper,
system_info: SystemInfo,
) -> DistData:
assert isinstance(v_data, VDataFrame)
assert v_data.aligned
assert len(v_data.partitions) > 0
parties_length = {}
for device, part in v_data.partitions.items():
parties_length[device.party] = len(part)
assert (
len(set(parties_length.values())) == 1
), f"number of samples must be equal across all devices, got {parties_length}"
with ctx.tracer.trace_io():
output_uri = {p: uri for p in v_data.partitions}
output_path = {
p: os.path.join(ctx.local_fs_wd, output_uri[p]) for p in output_uri
}
wait(v_data.to_csv(output_path, index=False))
order = [p.party for p in v_data.partitions]
file_metas = {}
for pyu in output_path:
file_metas[pyu] = reveal(pyu(read_file_meta)(output_path[pyu]))
logging.info(
f"dumped VDataFrame, file uri {output_path}, samples {parties_length}, file meta {file_metas}"
)
ret = DistData(
name=uri,
type=str(DistDataType.VERTICAL_TABLE),
system_info=system_info,
data_refs=[
DistData.DataRef(uri=output_uri[p], party=p.party, format="csv")
for p in output_uri
],
)
ret.meta.Pack(meta.to_vertical_table(order))
return ret
def model_dumps(
ctx,
model_name: str,
model_type: str,
major_version: int,
minor_version: int,
objs: List[DeviceObject],
public_info: Any,
dist_data_uri: str,
system_info: SystemInfo,
) -> DistData:
# TODO: only local fs is supported at this moment.
storage_root = ctx.local_fs_wd
objs_uri = []
objs_party = []
saved_objs = []
for i, obj in enumerate(objs):
if isinstance(obj, PYUObject):
device: PYU = obj.device
uri = f"{dist_data_uri}/{i}"
path = os.path.join(storage_root, uri)
def dumps(path: str, obj: Any):
import pickle
from pathlib import Path
# create parent folders.
file = Path(path)
file.parent.mkdir(parents=True, exist_ok=True)
with open(path, "wb") as f:
f.write(pickle.dumps(obj))
wait(device(dumps)(path, obj))
saved_obj = DeviceObjectCollection.DeviceObject(
type="pyu", data_ref_idxs=[len(objs_uri)]
)
saved_objs.append(saved_obj)
objs_uri.append(uri)
objs_party.append(device.party)
elif isinstance(obj, SPUObject):
device: SPU = obj.device
uris = [f"{dist_data_uri}/{i}" for party in device.actors.keys()]
spu_paths = [os.path.join(storage_root, uri) for uri in uris]
wait(device.dump(obj, spu_paths))
saved_obj = DeviceObjectCollection.DeviceObject(
type="spu", data_ref_idxs=[len(objs_uri) + p for p in range(len(uris))]
)
saved_objs.append(saved_obj)
objs_uri.extend(uris)
objs_party.extend(list(device.actors.keys()))
else:
raise RuntimeError(f"not supported objs type {type(obj)}")
model_info = {
"major_version": major_version,
"minor_version": minor_version,
"public_info": public_info,
}
model_meta = DeviceObjectCollection(
objs=saved_objs,
public_info=json.dumps(model_info),
)
dist_data = DistData(
name=model_name,
type=str(model_type),
system_info=system_info,
data_refs=[
DistData.DataRef(uri=uri, party=p, format="pickle")
for uri, p in zip(objs_uri, objs_party)
],
)
dist_data.meta.Pack(model_meta)
return dist_data
def get_model_public_info(dist_data: DistData):
model_meta = DeviceObjectCollection()
assert dist_data.meta.Unpack(model_meta)
model_info = json.loads(model_meta.public_info)
return json.loads(model_info["public_info"])
def model_loads(
ctx,
dist_data: DistData,
max_major_version: int,
max_minor_version: int,
model_type: str,
pyus: Dict[str, PYU] = None,
spu: SPU = None,
# TODO: assert system_info
# system_info: SystemInfo = None,
) -> Tuple[List[DeviceObject], str]:
# TODO: only local fs is supported at this moment.
storage_root = ctx.local_fs_wd
assert dist_data.type == model_type
model_meta = DeviceObjectCollection()
assert dist_data.meta.Unpack(model_meta)
model_info = json.loads(model_meta.public_info)
assert (
isinstance(model_info, dict)
and "major_version" in model_info
and "minor_version" in model_info
and "public_info" in model_info
)
assert (
max_major_version >= model_info["major_version"]
and max_minor_version >= model_info["minor_version"]
), "not support model version"
objs = []
for save_obj in model_meta.objs:
if save_obj.type == "pyu":
assert len(save_obj.data_ref_idxs) == 1
data_ref = dist_data.data_refs[save_obj.data_ref_idxs[0]]
party = data_ref.party
if pyus is not None:
assert party in pyus
pyu = pyus[party]
else:
pyu = PYU(party)
assert data_ref.format == "pickle"
def loads(path: str) -> Any:
import pickle
with open(path, "rb") as f:
# TODO: not secure, may change to json loads/dumps?
return pickle.loads(f.read())
objs.append(pyu(loads)(os.path.join(storage_root, data_ref.uri)))
elif save_obj.type == "spu":
# TODO: only support one spu for now
assert spu is not None
assert len(save_obj.data_ref_idxs) > 1
full_paths = {}
for data_ref_idx in save_obj.data_ref_idxs:
data_ref = dist_data.data_refs[data_ref_idx]
assert data_ref.format == "pickle"
party = data_ref.party
assert party not in full_paths
full_paths[party] = os.path.join(storage_root, data_ref.uri)
assert set(full_paths.keys()) == set(spu.actors.keys())
spu_paths = [full_paths[party] for party in spu.actors.keys()]
objs.append(spu.load(spu_paths))
else:
raise RuntimeError(f"not supported objs type {save_obj.type}")
return objs, model_info["public_info"]
def save_prediction_csv(
pred_df: pd.DataFrame,
pred_key: str,
path: str,
label_df: pd.DataFrame = None,
label_keys: List[str] = None,
id_df: pd.DataFrame = None,
id_keys: List[str] = None,
try_append: bool = False,
) -> None:
x = pd.DataFrame(pred_df, columns=[pred_key])
if label_df is not None:
label = pd.DataFrame(label_df, columns=label_keys)
x = pd.concat([x, label], axis=1)
if id_df is not None:
id = pd.DataFrame(id_df, columns=id_keys)
x = pd.concat([x, id], axis=1)
import os
if try_append:
if not os.path.isfile(path):
x.to_csv(path, index=False)
else:
x.to_csv(path, mode='a', header=False, index=False)
else:
x.to_csv(path, index=False)
def any_pyu_from_spu_config(config: dict):
return PYU(config["nodes"][0]["party"])
def generate_random_string(pyu: PYU):
return reveal(pyu(lambda: str(uuid.uuid4()))())