File size: 102,070 Bytes
476455e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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.
"""This module configures the SageMaker Clarify bias and model explainability processor jobs.

SageMaker Clarify
==================
"""
from __future__ import absolute_import, print_function

import copy
import json
import logging
import os
import re

import tempfile
from abc import ABC, abstractmethod
from typing import List, Union, Dict, Optional, Any

from schema import Schema, And, Use, Or, Optional as SchemaOptional, Regex

from sagemaker import image_uris, s3, utils
from sagemaker.session import Session
from sagemaker.network import NetworkConfig
from sagemaker.processing import ProcessingInput, ProcessingOutput, Processor

logger = logging.getLogger(__name__)


ENDPOINT_NAME_PREFIX_PATTERN = "^[a-zA-Z0-9](-*[a-zA-Z0-9])"

ANALYSIS_CONFIG_SCHEMA_V1_0 = Schema(
    {
        SchemaOptional("version"): str,
        "dataset_type": And(
            str,
            Use(str.lower),
            lambda s: s
            in (
                "text/csv",
                "application/jsonlines",
                "application/sagemakercapturejson",
                "application/x-parquet",
                "application/x-image",
            ),
        ),
        SchemaOptional("dataset_uri"): str,
        SchemaOptional("headers"): [str],
        SchemaOptional("label"): Or(str, int),
        # this field indicates user provides predicted_label in dataset
        SchemaOptional("predicted_label"): Or(str, int),
        SchemaOptional("features"): str,
        SchemaOptional("label_values_or_threshold"): [Or(int, float, str)],
        SchemaOptional("probability_threshold"): float,
        SchemaOptional("facet"): [
            {
                "name_or_index": Or(str, int),
                SchemaOptional("value_or_threshold"): [Or(int, float, str)],
            }
        ],
        SchemaOptional("facet_dataset_uri"): str,
        SchemaOptional("facet_headers"): [str],
        SchemaOptional("predicted_label_dataset_uri"): str,
        SchemaOptional("predicted_label_headers"): [str],
        SchemaOptional("excluded_columns"): [Or(int, str)],
        SchemaOptional("joinsource_name_or_index"): Or(str, int),
        SchemaOptional("group_variable"): Or(str, int),
        "methods": {
            SchemaOptional("shap"): {
                SchemaOptional("baseline"): Or(
                    # URI of the baseline data file
                    str,
                    # Inplace baseline data (a list of something)
                    [
                        Or(
                            # CSV row
                            [Or(int, float, str, None)],
                            # JSON row (any JSON object). As I write this only
                            # SageMaker JSONLines Dense Format ([1])
                            # is supported and the validation is NOT done
                            # by the schema but by the data loader.
                            # [1] https://docs.aws.amazon.com/sagemaker/latest/dg/cdf-inference.html#cm-jsonlines
                            {object: object},
                        )
                    ],
                ),
                SchemaOptional("num_clusters"): int,
                SchemaOptional("use_logit"): bool,
                SchemaOptional("num_samples"): int,
                SchemaOptional("agg_method"): And(
                    str, Use(str.lower), lambda s: s in ("mean_abs", "median", "mean_sq")
                ),
                SchemaOptional("save_local_shap_values"): bool,
                SchemaOptional("text_config"): {
                    "granularity": And(
                        str, Use(str.lower), lambda s: s in ("token", "sentence", "paragraph")
                    ),
                    "language": And(
                        str,
                        Use(str.lower),
                        lambda s: s
                        in (
                            "chinese",
                            "zh",
                            "danish",
                            "da",
                            "dutch",
                            "nl",
                            "english",
                            "en",
                            "french",
                            "fr",
                            "german",
                            "de",
                            "greek",
                            "el",
                            "italian",
                            "it",
                            "japanese",
                            "ja",
                            "lithuanian",
                            "lt",
                            "multi-language",
                            "xx",
                            "norwegian bokmål",
                            "nb",
                            "polish",
                            "pl",
                            "portuguese",
                            "pt",
                            "romanian",
                            "ro",
                            "russian",
                            "ru",
                            "spanish",
                            "es",
                            "afrikaans",
                            "af",
                            "albanian",
                            "sq",
                            "arabic",
                            "ar",
                            "armenian",
                            "hy",
                            "basque",
                            "eu",
                            "bengali",
                            "bn",
                            "bulgarian",
                            "bg",
                            "catalan",
                            "ca",
                            "croatian",
                            "hr",
                            "czech",
                            "cs",
                            "estonian",
                            "et",
                            "finnish",
                            "fi",
                            "gujarati",
                            "gu",
                            "hebrew",
                            "he",
                            "hindi",
                            "hi",
                            "hungarian",
                            "hu",
                            "icelandic",
                            "is",
                            "indonesian",
                            "id",
                            "irish",
                            "ga",
                            "kannada",
                            "kn",
                            "kyrgyz",
                            "ky",
                            "latvian",
                            "lv",
                            "ligurian",
                            "lij",
                            "luxembourgish",
                            "lb",
                            "macedonian",
                            "mk",
                            "malayalam",
                            "ml",
                            "marathi",
                            "mr",
                            "nepali",
                            "ne",
                            "persian",
                            "fa",
                            "sanskrit",
                            "sa",
                            "serbian",
                            "sr",
                            "setswana",
                            "tn",
                            "sinhala",
                            "si",
                            "slovak",
                            "sk",
                            "slovenian",
                            "sl",
                            "swedish",
                            "sv",
                            "tagalog",
                            "tl",
                            "tamil",
                            "ta",
                            "tatar",
                            "tt",
                            "telugu",
                            "te",
                            "thai",
                            "th",
                            "turkish",
                            "tr",
                            "ukrainian",
                            "uk",
                            "urdu",
                            "ur",
                            "vietnamese",
                            "vi",
                            "yoruba",
                            "yo",
                        ),
                    ),
                    SchemaOptional("max_top_tokens"): int,
                },
                SchemaOptional("image_config"): {
                    SchemaOptional("num_segments"): int,
                    SchemaOptional("segment_compactness"): int,
                    SchemaOptional("feature_extraction_method"): str,
                    SchemaOptional("model_type"): str,
                    SchemaOptional("max_objects"): int,
                    SchemaOptional("iou_threshold"): float,
                    SchemaOptional("context"): float,
                    SchemaOptional("debug"): {
                        SchemaOptional("image_names"): [str],
                        SchemaOptional("class_ids"): [int],
                        SchemaOptional("sample_from"): int,
                        SchemaOptional("sample_to"): int,
                    },
                },
                SchemaOptional("seed"): int,
            },
            SchemaOptional("pre_training_bias"): {"methods": Or(str, [str])},
            SchemaOptional("post_training_bias"): {"methods": Or(str, [str])},
            SchemaOptional("pdp"): {
                "grid_resolution": int,
                SchemaOptional("features"): [Or(str, int)],
                SchemaOptional("top_k_features"): int,
            },
            SchemaOptional("report"): {"name": str, SchemaOptional("title"): str},
        },
        SchemaOptional("predictor"): {
            SchemaOptional("endpoint_name"): str,
            SchemaOptional("endpoint_name_prefix"): And(str, Regex(ENDPOINT_NAME_PREFIX_PATTERN)),
            SchemaOptional("model_name"): str,
            SchemaOptional("target_model"): str,
            SchemaOptional("instance_type"): str,
            SchemaOptional("initial_instance_count"): int,
            SchemaOptional("accelerator_type"): str,
            SchemaOptional("content_type"): And(
                str,
                Use(str.lower),
                lambda s: s
                in (
                    "text/csv",
                    "application/jsonlines",
                    "image/jpeg",
                    "image/jpg",
                    "image/png",
                    "application/x-npy",
                ),
            ),
            SchemaOptional("accept_type"): And(
                str,
                Use(str.lower),
                lambda s: s in ("text/csv", "application/jsonlines", "application/json"),
            ),
            SchemaOptional("label"): Or(str, int),
            SchemaOptional("probability"): Or(str, int),
            SchemaOptional("label_headers"): [Or(str, int)],
            SchemaOptional("content_template"): Or(str, {str: str}),
            SchemaOptional("custom_attributes"): str,
        },
    }
)


class DataConfig:
    """Config object related to configurations of the input and output dataset."""

    def __init__(
        self,
        s3_data_input_path: str,
        s3_output_path: str,
        s3_analysis_config_output_path: Optional[str] = None,
        label: Optional[str] = None,
        headers: Optional[List[str]] = None,
        features: Optional[List[str]] = None,
        dataset_type: str = "text/csv",
        s3_compression_type: str = "None",
        joinsource: Optional[Union[str, int]] = None,
        facet_dataset_uri: Optional[str] = None,
        facet_headers: Optional[List[str]] = None,
        predicted_label_dataset_uri: Optional[str] = None,
        predicted_label_headers: Optional[List[str]] = None,
        predicted_label: Optional[Union[str, int]] = None,
        excluded_columns: Optional[Union[List[int], List[str]]] = None,
    ):
        """Initializes a configuration of both input and output datasets.

        Args:
            s3_data_input_path (str): Dataset S3 prefix/object URI.
            s3_output_path (str): S3 prefix to store the output.
            s3_analysis_config_output_path (str): S3 prefix to store the analysis config output.
                If this field is None, then the ``s3_output_path`` will be used
                to store the ``analysis_config`` output.
            label (str): Target attribute of the model required by bias metrics.
                Specified as column name or index for CSV dataset or as JSONPath for JSONLines.
                *Required parameter* except for when the input dataset does not contain the label.
            features (List[str]): JSONPath for locating the feature columns for bias metrics if the
                dataset format is JSONLines.
            dataset_type (str): Format of the dataset. Valid values are ``"text/csv"`` for CSV,
                ``"application/jsonlines"`` for JSONLines, and
                ``"application/x-parquet"`` for Parquet.
            s3_compression_type (str): Valid options are "None" or ``"Gzip"``.
            joinsource (str or int): The name or index of the column in the dataset that
                acts as an identifier column (for instance, while performing a join).
                This column is only used as an identifier, and not used for any other computations.
                This is an optional field in all cases except:

                * The dataset contains more than one file and `save_local_shap_values`
                  is set to true in :class:`~sagemaker.clarify.ShapConfig`, and/or
                * When the dataset and/or facet dataset and/or predicted label dataset
                  are in separate files.

            facet_dataset_uri (str): Dataset S3 prefix/object URI that contains facet attribute(s),
                used for bias analysis on datasets without facets.

                * If the dataset and the facet dataset are one single file each, then
                  the original dataset and facet dataset must have the same number of rows.
                * If the dataset and facet dataset are in multiple files (either one), then
                  an index column, ``joinsource``, is required to join the two datasets.

                Clarify will not use the ``joinsource`` column and columns present in the facet
                dataset when calling model inference APIs.
            facet_headers (list[str]): List of column names in the facet dataset.
            predicted_label_dataset_uri (str): Dataset S3 prefix/object URI with predicted labels,
                which are used directly for analysis instead of making model inference API calls.

                * If the dataset and the predicted label dataset are one single file each, then the
                  original dataset and predicted label dataset must have the same number of rows.
                * If the dataset and predicted label dataset are in multiple files (either one),
                  then an index column, ``joinsource``, is required to join the two datasets.

            predicted_label_headers (list[str]): List of column names in the predicted label dataset
            predicted_label (str or int): Predicted label of the target attribute of the model
                required for running bias analysis. Specified as column name or index for CSV data.
                Clarify uses the predicted labels directly instead of making model inference API
                calls.
            excluded_columns (list[int] or list[str]): A list of names or indices of the columns
                which are to be excluded from making model inference API calls.

        Raises:
            ValueError: when the ``dataset_type`` is invalid, predicted label dataset parameters
                are used with un-supported ``dataset_type``, or facet dataset parameters
                are used with un-supported ``dataset_type``
        """
        if dataset_type not in [
            "text/csv",
            "application/jsonlines",
            "application/x-parquet",
            "application/x-image",
        ]:
            raise ValueError(
                f"Invalid dataset_type '{dataset_type}'."
                f" Please check the API documentation for the supported dataset types."
            )
        # parameters for analysis on datasets without facets are only supported for CSV datasets
        if dataset_type != "text/csv":
            if predicted_label:
                raise ValueError(
                    f"The parameter 'predicted_label' is not supported"
                    f" for dataset_type '{dataset_type}'."
                    f" Please check the API documentation for the supported dataset types."
                )
            if excluded_columns:
                raise ValueError(
                    f"The parameter 'excluded_columns' is not supported"
                    f" for dataset_type '{dataset_type}'."
                    f" Please check the API documentation for the supported dataset types."
                )
            if facet_dataset_uri or facet_headers:
                raise ValueError(
                    f"The parameters 'facet_dataset_uri' and 'facet_headers'"
                    f" are not supported for dataset_type '{dataset_type}'."
                    f" Please check the API documentation for the supported dataset types."
                )
            if predicted_label_dataset_uri or predicted_label_headers:
                raise ValueError(
                    f"The parameters 'predicted_label_dataset_uri' and 'predicted_label_headers'"
                    f" are not supported for dataset_type '{dataset_type}'."
                    f" Please check the API documentation for the supported dataset types."
                )
        self.s3_data_input_path = s3_data_input_path
        self.s3_output_path = s3_output_path
        self.s3_analysis_config_output_path = s3_analysis_config_output_path
        self.s3_data_distribution_type = "FullyReplicated"
        self.s3_compression_type = s3_compression_type
        self.label = label
        self.headers = headers
        self.features = features
        self.facet_dataset_uri = facet_dataset_uri
        self.facet_headers = facet_headers
        self.predicted_label_dataset_uri = predicted_label_dataset_uri
        self.predicted_label_headers = predicted_label_headers
        self.predicted_label = predicted_label
        self.excluded_columns = excluded_columns
        self.analysis_config = {
            "dataset_type": dataset_type,
        }
        _set(features, "features", self.analysis_config)
        _set(headers, "headers", self.analysis_config)
        _set(label, "label", self.analysis_config)
        _set(joinsource, "joinsource_name_or_index", self.analysis_config)
        _set(facet_dataset_uri, "facet_dataset_uri", self.analysis_config)
        _set(facet_headers, "facet_headers", self.analysis_config)
        _set(
            predicted_label_dataset_uri,
            "predicted_label_dataset_uri",
            self.analysis_config,
        )
        _set(predicted_label_headers, "predicted_label_headers", self.analysis_config)
        _set(predicted_label, "predicted_label", self.analysis_config)
        _set(excluded_columns, "excluded_columns", self.analysis_config)

    def get_config(self):
        """Returns part of an analysis config dictionary."""
        return copy.deepcopy(self.analysis_config)


class BiasConfig:
    """Config object with user-defined bias configurations of the input dataset."""

    def __init__(
        self,
        label_values_or_threshold: Union[int, float, str],
        facet_name: Union[str, int, List[str], List[int]],
        facet_values_or_threshold: Optional[Union[int, float, str]] = None,
        group_name: Optional[str] = None,
    ):
        """Initializes a configuration of the sensitive groups in the dataset.

        Args:
            label_values_or_threshold ([int or float or str]): List of label value(s) or threshold
                to indicate positive outcome used for bias metrics.
                The appropriate threshold depends on the problem type:

                * Binary: The list has one positive value.
                * Categorical:The list has one or more (but not all) categories
                  which are the positive values.
                * Regression: The list should include one threshold that defines the **exclusive**
                  lower bound of positive values.

            facet_name (str or int or list[str] or list[int]): Sensitive attribute column name
                (or index in the input data) to use when computing bias metrics. It can also be a
                list of names (or indexes) for computing metrics for multiple sensitive attributes.
            facet_values_or_threshold ([int or float or str] or [[int or float or str]]):
                The parameter controls the values of the sensitive group.
                If ``facet_name`` is a scalar, then it can be None or a list.
                Depending on the data type of the facet column, the values mean:

                * Binary data: None means computing the bias metrics for each binary value.
                  Or add one binary value to the list, to compute its bias metrics only.
                * Categorical data: None means computing the bias metrics for each category. Or add
                  one or more (but not all) categories to the list, to compute their
                  bias metrics v.s. the other categories.
                * Continuous data: The list should include one and only one threshold which defines
                  the **exclusive** lower bound of a sensitive group.

                If ``facet_name`` is a list, then ``facet_values_or_threshold`` can be None
                if all facets are of binary or categorical type.
                Otherwise, ``facet_values_or_threshold`` should be a list, and each element
                is the value or threshold of the corresponding facet.
            group_name (str): Optional column name or index to indicate a group column to be used
                for the bias metric
                `Conditional Demographic Disparity in Labels `(CDDL) <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-cddl.html>`_
                or
                `Conditional Demographic Disparity in Predicted Labels (CDDPL) <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-cddpl.html>`_.

        Raises:
            ValueError: If the number of ``facet_names`` doesn't equal number of ``facet values``
        """  # noqa E501  # pylint: disable=c0301
        if isinstance(facet_name, list):
            assert len(facet_name) > 0, "Please provide at least one facet"
            if facet_values_or_threshold is None:
                facet_list = [
                    {"name_or_index": single_facet_name} for single_facet_name in facet_name
                ]
            elif len(facet_values_or_threshold) == len(facet_name):
                facet_list = []
                for i, single_facet_name in enumerate(facet_name):
                    facet = {"name_or_index": single_facet_name}
                    if facet_values_or_threshold is not None:
                        _set(facet_values_or_threshold[i], "value_or_threshold", facet)
                    facet_list.append(facet)
            else:
                raise ValueError(
                    "The number of facet names doesn't match the number of facet values"
                )
        else:
            facet = {"name_or_index": facet_name}
            _set(facet_values_or_threshold, "value_or_threshold", facet)
            facet_list = [facet]
        self.analysis_config = {
            "label_values_or_threshold": label_values_or_threshold,
            "facet": facet_list,
        }
        _set(group_name, "group_variable", self.analysis_config)

    def get_config(self):
        """Returns a dictionary of bias detection configurations, part of the analysis config"""
        return copy.deepcopy(self.analysis_config)


class ModelConfig:
    """Config object related to a model and its endpoint to be created."""

    def __init__(
        self,
        model_name: Optional[str] = None,
        instance_count: Optional[int] = None,
        instance_type: Optional[str] = None,
        accept_type: Optional[str] = None,
        content_type: Optional[str] = None,
        content_template: Optional[str] = None,
        custom_attributes: Optional[str] = None,
        accelerator_type: Optional[str] = None,
        endpoint_name_prefix: Optional[str] = None,
        target_model: Optional[str] = None,
        endpoint_name: Optional[str] = None,
    ):
        r"""Initializes a configuration of a model and the endpoint to be created for it.

        Args:
            model_name (str): Model name (as created by
                `CreateModel <https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html>`_.
                Cannot be set when ``endpoint_name`` is set.
                Must be set with ``instance_count``, ``instance_type``
            instance_count (int): The number of instances of a new endpoint for model inference.
                Cannot be set when ``endpoint_name`` is set.
                Must be set with ``model_name``, ``instance_type``
            instance_type (str): The type of
                `EC2 instance <https://aws.amazon.com/ec2/instance-types/>`_
                to use for model inference; for example, ``"ml.c5.xlarge"``.
                Cannot be set when ``endpoint_name`` is set.
                Must be set with ``instance_count``, ``model_name``
            accept_type (str): The model output format to be used for getting inferences with the
                shadow endpoint. Valid values are ``"text/csv"`` for CSV and
                ``"application/jsonlines"``. Default is the same as ``content_type``.
            content_type (str): The model input format to be used for getting inferences with the
                shadow endpoint. Valid values are ``"text/csv"`` for CSV and
                ``"application/jsonlines"``. Default is the same as ``dataset_format``.
            content_template (str): A template string to be used to construct the model input from
                dataset instances. It is only used when ``model_content_type`` is
                ``"application/jsonlines"``. The template should have one and only one placeholder,
                ``"features"``, which will be replaced by a features list to form the model
                inference input.
            custom_attributes (str): Provides additional information about a request for an
                inference submitted to a model hosted at an Amazon SageMaker endpoint. The
                information is an opaque value that is forwarded verbatim. You could use this
                value, for example, to provide an ID that you can use to track a request or to
                provide other metadata that a service endpoint was programmed to process. The value
                must consist of no more than 1024 visible US-ASCII characters as specified in
                Section 3.3.6.
                `Field Value Components <https://tools.ietf.org/html/rfc7230#section-3.2.6>`_
                of the Hypertext Transfer Protocol (HTTP/1.1).
            accelerator_type (str): SageMaker
                `Elastic Inference <https://docs.aws.amazon.com/sagemaker/latest/dg/ei.html>`_
                accelerator type to deploy to the model endpoint instance
                for making inferences to the model.
            endpoint_name_prefix (str): The endpoint name prefix of a new endpoint. Must follow
                pattern ``^[a-zA-Z0-9](-\*[a-zA-Z0-9]``.
            target_model (str): Sets the target model name when using a multi-model endpoint. For
                more information about multi-model endpoints, see
                https://docs.aws.amazon.com/sagemaker/latest/dg/multi-model-endpoints.html
            endpoint_name (str): Sets the endpoint_name when re-uses an existing endpoint.
                Cannot be set when ``model_name``, ``instance_count``,
                and ``instance_type`` set

        Raises:
            ValueError: when the
                - ``endpoint_name_prefix`` is invalid,
                - ``accept_type`` is invalid,
                - ``content_type`` is invalid,
                - ``content_template`` has no placeholder "features"
                - both [``endpoint_name``]
                   AND [``model_name``, ``instance_count``, ``instance_type``] are set
                - both [``endpoint_name``] AND [``endpoint_name_prefix``] are set
        """

        # validation
        _model_endpoint_config_rule = (
            all([model_name, instance_count, instance_type]),
            all([endpoint_name]),
        )
        assert any(_model_endpoint_config_rule) and not all(_model_endpoint_config_rule)
        if endpoint_name:
            assert not endpoint_name_prefix

        # main init logic
        self.predictor_config = (
            {
                "model_name": model_name,
                "instance_type": instance_type,
                "initial_instance_count": instance_count,
            }
            if not endpoint_name
            else {"endpoint_name": endpoint_name}
        )
        if endpoint_name_prefix:
            if re.search("^[a-zA-Z0-9](-*[a-zA-Z0-9])", endpoint_name_prefix) is None:
                raise ValueError(
                    "Invalid endpoint_name_prefix."
                    " Please follow pattern ^[a-zA-Z0-9](-*[a-zA-Z0-9])."
                )
            self.predictor_config["endpoint_name_prefix"] = endpoint_name_prefix
        if accept_type is not None:
            if accept_type not in ["text/csv", "application/jsonlines"]:
                raise ValueError(
                    f"Invalid accept_type {accept_type}."
                    f" Please choose text/csv or application/jsonlines."
                )
            self.predictor_config["accept_type"] = accept_type
        if content_type is not None:
            if content_type not in [
                "text/csv",
                "application/jsonlines",
                "image/jpeg",
                "image/jpg",
                "image/png",
                "application/x-npy",
            ]:
                raise ValueError(
                    f"Invalid content_type {content_type}."
                    f" Please choose text/csv or application/jsonlines."
                )
            self.predictor_config["content_type"] = content_type
        if content_template is not None:
            if "$features" not in content_template:
                raise ValueError(
                    f"Invalid content_template {content_template}."
                    f" Please include a placeholder $features."
                )
            self.predictor_config["content_template"] = content_template
        _set(custom_attributes, "custom_attributes", self.predictor_config)
        _set(accelerator_type, "accelerator_type", self.predictor_config)
        _set(target_model, "target_model", self.predictor_config)

    def get_predictor_config(self):
        """Returns part of the predictor dictionary of the analysis config."""
        return copy.deepcopy(self.predictor_config)


class ModelPredictedLabelConfig:
    """Config object to extract a predicted label from the model output."""

    def __init__(
        self,
        label: Optional[Union[str, int]] = None,
        probability: Optional[Union[str, int]] = None,
        probability_threshold: Optional[float] = None,
        label_headers: Optional[List[str]] = None,
    ):
        """Initializes a model output config to extract the predicted label or predicted score(s).

        The following examples show different parameter configurations depending on the endpoint:

        * **Regression task:**
          The model returns the score, e.g. ``1.2``. We don't need to specify
          anything. For json output, e.g. ``{'score': 1.2}``, we can set ``label='score'``.
        * **Binary classification:**

          * The model returns a single probability score. We want to classify as ``"yes"``
            predictions with a probability score over ``0.2``.
            We can set ``probability_threshold=0.2`` and ``label_headers="yes"``.
          * The model returns ``{"probability": 0.3}``, for which we would like to apply a
            threshold of ``0.5`` to obtain a predicted label in ``{0, 1}``.
            In this case we can set ``label="probability"``.
          * The model returns a tuple of the predicted label and the probability.
            In this case we can set ``label = 0``.
        * **Multiclass classification:**

          * The model returns ``{'labels': ['cat', 'dog', 'fish'],
            'probabilities': [0.35, 0.25, 0.4]}``. In this case we would set
            ``probability='probabilities'``, ``label='labels'``,
            and infer the predicted label to be ``'fish'``.
          * The model returns ``{'predicted_label': 'fish', 'probabilities': [0.35, 0.25, 0.4]}``.
            In this case we would set the ``label='predicted_label'``.
          * The model returns ``[0.35, 0.25, 0.4]``. In this case, we can set
            ``label_headers=['cat','dog','fish']`` and infer the predicted label to be ``'fish'``.

        Args:
            label (str or int): Index or JSONPath location in the model output for the prediction.
                In case, this is a predicted label of the same type as the label in the dataset,
                no further arguments need to be specified.
            probability (str or int): Index or JSONPath location in the model output
                for the predicted score(s).
            probability_threshold (float): An optional value for binary prediction tasks in which
                the model returns a probability, to indicate the threshold to convert the
                prediction to a boolean value. Default is ``0.5``.
            label_headers (list[str]): List of headers, each for a predicted score in model output.
                For bias analysis, it is used to extract the label value with the highest score as
                predicted label. For explainability jobs, it is used to beautify the analysis report
                by replacing placeholders like ``'label0'``.

        Raises:
            TypeError: when the ``probability_threshold`` cannot be cast to a float
        """
        self.label = label
        self.probability = probability
        self.probability_threshold = probability_threshold
        self.label_headers = label_headers
        if probability_threshold is not None:
            try:
                float(probability_threshold)
            except ValueError:
                raise TypeError(
                    f"Invalid probability_threshold {probability_threshold}. "
                    f"Please choose one that can be cast to float."
                )
        self.predictor_config = {}
        _set(label, "label", self.predictor_config)
        _set(probability, "probability", self.predictor_config)
        _set(label_headers, "label_headers", self.predictor_config)

    def get_predictor_config(self):
        """Returns ``probability_threshold`` and predictor config dictionary."""
        return self.probability_threshold, copy.deepcopy(self.predictor_config)


class ExplainabilityConfig(ABC):
    """Abstract config class to configure an explainability method."""

    @abstractmethod
    def get_explainability_config(self):
        """Returns config."""
        return None


class PDPConfig(ExplainabilityConfig):
    """Config class for Partial Dependence Plots (PDP).

    `PDPs <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-partial-dependence-plots.html>`_
    show the marginal effect (the dependence) a subset of features has on the predicted
    outcome of an ML model.

    When PDP is requested (by passing in a :class:`~sagemaker.clarify.PDPConfig` to the
    ``explainability_config`` parameter of :class:`~sagemaker.clarify.SageMakerClarifyProcessor`),
    the Partial Dependence Plots are included in the output
    `report <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-feature-attribute-baselines-reports.html>`__
    and the corresponding values are included in the analysis output.
    """  # noqa E501

    def __init__(
        self, features: Optional[List] = None, grid_resolution: int = 15, top_k_features: int = 10
    ):
        """Initializes PDP config.

        Args:
            features (None or list): List of feature names or indices for which partial dependence
                plots are computed and plotted. When :class:`~sagemaker.clarify.ShapConfig`
                is provided, this parameter is optional, as Clarify will compute the
                partial dependence plots for top features based on
                `SHAP <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-shapley-values.html>`__
                attributions. When :class:`~sagemaker.clarify.ShapConfig` is not provided,
                ``features`` must be provided.
            grid_resolution (int): When using numerical features, this integer represents the
                number of buckets that the range of values must be divided into. This decides the
                granularity of the grid in which the PDP are plotted.
            top_k_features (int): Sets the number of top SHAP attributes used to compute
                partial dependence plots.
        """  # noqa E501
        self.pdp_config = {
            "grid_resolution": grid_resolution,
            "top_k_features": top_k_features,
        }
        if features is not None:
            self.pdp_config["features"] = features

    def get_explainability_config(self):
        """Returns PDP config dictionary."""
        return copy.deepcopy({"pdp": self.pdp_config})


class TextConfig:
    """Config object to handle text features for text explainability

    `SHAP analysis <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-model-explainability.html>`__
    breaks down longer text into chunks (e.g. tokens, sentences, or paragraphs)
    and replaces them with the strings specified in the baseline for that feature.
    The `shap value <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-shapley-values.html>`_
    of a chunk then captures how much replacing it affects the prediction.
    """  # noqa E501  # pylint: disable=c0301

    _SUPPORTED_GRANULARITIES = ["token", "sentence", "paragraph"]
    _SUPPORTED_LANGUAGES = [
        "chinese",
        "zh",
        "danish",
        "da",
        "dutch",
        "nl",
        "english",
        "en",
        "french",
        "fr",
        "german",
        "de",
        "greek",
        "el",
        "italian",
        "it",
        "japanese",
        "ja",
        "lithuanian",
        "lt",
        "multi-language",
        "xx",
        "norwegian bokmål",
        "nb",
        "polish",
        "pl",
        "portuguese",
        "pt",
        "romanian",
        "ro",
        "russian",
        "ru",
        "spanish",
        "es",
        "afrikaans",
        "af",
        "albanian",
        "sq",
        "arabic",
        "ar",
        "armenian",
        "hy",
        "basque",
        "eu",
        "bengali",
        "bn",
        "bulgarian",
        "bg",
        "catalan",
        "ca",
        "croatian",
        "hr",
        "czech",
        "cs",
        "estonian",
        "et",
        "finnish",
        "fi",
        "gujarati",
        "gu",
        "hebrew",
        "he",
        "hindi",
        "hi",
        "hungarian",
        "hu",
        "icelandic",
        "is",
        "indonesian",
        "id",
        "irish",
        "ga",
        "kannada",
        "kn",
        "kyrgyz",
        "ky",
        "latvian",
        "lv",
        "ligurian",
        "lij",
        "luxembourgish",
        "lb",
        "macedonian",
        "mk",
        "malayalam",
        "ml",
        "marathi",
        "mr",
        "nepali",
        "ne",
        "persian",
        "fa",
        "sanskrit",
        "sa",
        "serbian",
        "sr",
        "setswana",
        "tn",
        "sinhala",
        "si",
        "slovak",
        "sk",
        "slovenian",
        "sl",
        "swedish",
        "sv",
        "tagalog",
        "tl",
        "tamil",
        "ta",
        "tatar",
        "tt",
        "telugu",
        "te",
        "thai",
        "th",
        "turkish",
        "tr",
        "ukrainian",
        "uk",
        "urdu",
        "ur",
        "vietnamese",
        "vi",
        "yoruba",
        "yo",
    ]

    def __init__(
        self,
        granularity: str,
        language: str,
    ):
        """Initializes a text configuration.

        Args:
            granularity (str): Determines the granularity in which text features are broken down
                to. Accepted values are ``"token"``, ``"sentence"``, or ``"paragraph"``.
                Computes `shap values <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-shapley-values.html>`_
                for these units.
            language (str): Specifies the language of the text features. Accepted values are
                one of the following:
                ``"chinese"``, ``"danish"``, ``"dutch"``, ``"english"``, ``"french"``, ``"german"``,
                ``"greek"``, ``"italian"``, ``"japanese"``, ``"lithuanian"``, ``"multi-language"``,
                ``"norwegian bokmål"``, ``"polish"``, ``"portuguese"``, ``"romanian"``,
                ``"russian"``, ``"spanish"``, ``"afrikaans"``, ``"albanian"``, ``"arabic"``,
                ``"armenian"``, ``"basque"``, ``"bengali"``, ``"bulgarian"``, ``"catalan"``,
                ``"croatian"``, ``"czech"``, ``"estonian"``, ``"finnish"``, ``"gujarati"``,
                ``"hebrew"``, ``"hindi"``, ``"hungarian"``, ``"icelandic"``, ``"indonesian"``,
                ``"irish"``, ``"kannada"``, ``"kyrgyz"``, ``"latvian"``, ``"ligurian"``,
                ``"luxembourgish"``, ``"macedonian"``, ``"malayalam"``, ``"marathi"``, ``"nepali"``,
                ``"persian"``, ``"sanskrit"``, ``"serbian"``, ``"setswana"``, ``"sinhala"``,
                ``"slovak"``, ``"slovenian"``, ``"swedish"``, ``"tagalog"``, ``"tamil"``,
                ``"tatar"``, ``"telugu"``, ``"thai"``, ``"turkish"``, ``"ukrainian"``, ``"urdu"``,
                ``"vietnamese"``, ``"yoruba"``. Use "multi-language" for a mix of multiple
                languages. The corresponding two-letter ISO codes are also accepted.

        Raises:
            ValueError: when ``granularity`` is not in list of supported values
                or ``language`` is not in list of supported values
        """  # noqa E501  # pylint: disable=c0301
        if granularity not in TextConfig._SUPPORTED_GRANULARITIES:
            raise ValueError(
                f"Invalid granularity {granularity}. Please choose among "
                f"{TextConfig._SUPPORTED_GRANULARITIES}"
            )
        if language not in TextConfig._SUPPORTED_LANGUAGES:
            raise ValueError(
                f"Invalid language {language}. Please choose among "
                f"{TextConfig._SUPPORTED_LANGUAGES}"
            )
        self.text_config = {
            "granularity": granularity,
            "language": language,
        }

    def get_text_config(self):
        """Returns a text config dictionary, part of the analysis config dictionary."""
        return copy.deepcopy(self.text_config)


class ImageConfig:
    """Config object for handling images"""

    def __init__(
        self,
        model_type: str,
        num_segments: Optional[int] = None,
        feature_extraction_method: Optional[str] = None,
        segment_compactness: Optional[float] = None,
        max_objects: Optional[int] = None,
        iou_threshold: Optional[float] = None,
        context: Optional[float] = None,
    ):
        """Initializes a config object for Computer Vision (CV) Image explainability.

        `SHAP for CV explainability <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-model-explainability-computer-vision.html>`__.
        generating heat maps that visualize feature attributions for input images.
        These heat maps highlight the image's features according
        to how much they contribute to the CV model prediction.

        ``"IMAGE_CLASSIFICATION"`` and ``"OBJECT_DETECTION"`` are the two supported CV use cases.

        Args:
            model_type (str): Specifies the type of CV model and use case. Accepted options:
                ``"IMAGE_CLASSIFICATION"`` or ``"OBJECT_DETECTION"``.
            num_segments (None or int): Approximate number of segments to generate when running
                SKLearn's `SLIC method <https://scikit-image.org/docs/dev/api/skimage.segmentation.html?highlight=slic#skimage.segmentation.slic>`_
                for image segmentation to generate features/superpixels.
                The default is None. When set to None, runs SLIC with 20 segments.
            feature_extraction_method (None or str): method used for extracting features from the
                image (ex: "segmentation"). Default is ``"segmentation"``.
            segment_compactness (None or float): Balances color proximity and space proximity.
                Higher values give more weight to space proximity, making superpixel
                shapes more square/cubic. We recommend exploring possible values on a log
                scale, e.g., 0.01, 0.1, 1, 10, 100, before refining around a chosen value.
                The default is None. When set to None, runs with the default value of ``5``.
            max_objects (None or int): Maximum number of objects displayed when running SHAP
                with an ``"OBJECT_DETECTION"`` model. The Object detection algorithm may detect
                more than the ``max_objects`` number of objects in a single image.
                In that case, the algorithm displays the top ``max_objects`` number of objects
                according to confidence score. Default value is None. In the ``"OBJECT_DETECTION"``
                case, passing in None leads to a default value of ``3``.
            iou_threshold (None or float): Minimum intersection over union for the object
                bounding box to consider its confidence score for computing SHAP values,
                in the range ``[0.0, 1.0]``. Used only for the ``"OBJECT_DETECTION"`` case,
                where passing in None sets the default value of ``0.5``.
            context (None or float): The portion of the image outside the bounding box used
                in SHAP analysis, in the range ``[0.0, 1.0]``. If set to ``1.0``, the whole image
                is considered; if set to ``0.0`` only the image inside bounding box is considered.
                Only used for the ``"OBJECT_DETECTION"`` case,
                when passing in None sets the default value of ``1.0``.

        """  # noqa E501  # pylint: disable=c0301
        self.image_config = {}

        if model_type not in ["OBJECT_DETECTION", "IMAGE_CLASSIFICATION"]:
            raise ValueError(
                "Clarify SHAP only supports object detection and image classification methods. "
                "Please set model_type to OBJECT_DETECTION or IMAGE_CLASSIFICATION."
            )
        self.image_config["model_type"] = model_type
        _set(num_segments, "num_segments", self.image_config)
        _set(feature_extraction_method, "feature_extraction_method", self.image_config)
        _set(segment_compactness, "segment_compactness", self.image_config)
        _set(max_objects, "max_objects", self.image_config)
        _set(iou_threshold, "iou_threshold", self.image_config)
        _set(context, "context", self.image_config)

    def get_image_config(self):
        """Returns the image config part of an analysis config dictionary."""
        return copy.deepcopy(self.image_config)


class SHAPConfig(ExplainabilityConfig):
    """Config class for `SHAP <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-model-explainability.html>`__.

    The SHAP algorithm calculates feature attributions by computing
    the contribution of each feature to the prediction outcome, using the concept of
    `Shapley values <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-shapley-values.html>`_.

    These attributions can be provided for specific predictions (locally)
    and at a global level for the model as a whole.
    """  # noqa E501  # pylint: disable=c0301

    def __init__(
        self,
        baseline: Optional[Union[str, List]] = None,
        num_samples: Optional[int] = None,
        agg_method: Optional[str] = None,
        use_logit: bool = False,
        save_local_shap_values: bool = True,
        seed: Optional[int] = None,
        num_clusters: Optional[int] = None,
        text_config: Optional[TextConfig] = None,
        image_config: Optional[ImageConfig] = None,
    ):
        """Initializes config for SHAP analysis.

        Args:
            baseline (None or str or list): `Baseline dataset <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-feature-attribute-shap-baselines.html>`_
                for the Kernel SHAP algorithm, accepted in the form of:
                S3 object URI, a list of rows (with at least one element),
                or None (for no input baseline). The baseline dataset must have the same format
                as the input dataset specified in :class:`~sagemaker.clarify.DataConfig`.
                Each row must have only the feature columns/values and omit the label column/values.
                If None, a baseline will be calculated automatically on the input dataset
                using K-means (for numerical data) or K-prototypes (if there is categorical data).
            num_samples (None or int): Number of samples to be used in the Kernel SHAP algorithm.
                This number determines the size of the generated synthetic dataset to compute the
                SHAP values. If not provided then Clarify job will choose a proper value according
                to the count of features.
            agg_method (None or str): Aggregation method for global SHAP values. Valid values are
                ``"mean_abs"`` (mean of absolute SHAP values for all instances),
                ``"median"`` (median of SHAP values for all instances) and
                ``"mean_sq"`` (mean of squared SHAP values for all instances).
                If None is provided, then Clarify job uses the method ``"mean_abs"``.
            use_logit (bool): Indicates whether to apply the logit function to model predictions.
                Default is False. If ``use_logit`` is true then the SHAP values will
                have log-odds units.
            save_local_shap_values (bool): Indicates whether to save the local SHAP values
                in the output location. Default is True.
            seed (int): Seed value to get deterministic SHAP values. Default is None.
            num_clusters (None or int): If a ``baseline`` is not provided, Clarify automatically
                computes a baseline dataset via a clustering algorithm (K-means/K-prototypes), which
                takes ``num_clusters`` as a parameter. ``num_clusters`` will be the resulting size
                of the baseline dataset. If not provided, Clarify job uses a default value.
            text_config (:class:`~sagemaker.clarify.TextConfig`): Config object for handling
                text features. Default is None.
            image_config (:class:`~sagemaker.clarify.ImageConfig`): Config for handling image
                features. Default is None.
        """  # noqa E501  # pylint: disable=c0301
        if agg_method is not None and agg_method not in [
            "mean_abs",
            "median",
            "mean_sq",
        ]:
            raise ValueError(
                f"Invalid agg_method {agg_method}." f" Please choose mean_abs, median, or mean_sq."
            )
        if num_clusters is not None and baseline is not None:
            raise ValueError(
                "Baseline and num_clusters cannot be provided together. "
                "Please specify one of the two."
            )
        self.shap_config = {
            "use_logit": use_logit,
            "save_local_shap_values": save_local_shap_values,
        }
        _set(baseline, "baseline", self.shap_config)
        _set(num_samples, "num_samples", self.shap_config)
        _set(agg_method, "agg_method", self.shap_config)
        _set(seed, "seed", self.shap_config)
        _set(num_clusters, "num_clusters", self.shap_config)
        if text_config:
            _set(text_config.get_text_config(), "text_config", self.shap_config)
            if not save_local_shap_values:
                logger.warning(
                    "Global aggregation is not yet supported for text features. "
                    "Consider setting save_local_shap_values=True to inspect local text "
                    "explanations."
                )
        if image_config:
            _set(image_config.get_image_config(), "image_config", self.shap_config)

    def get_explainability_config(self):
        """Returns a shap config dictionary."""
        return copy.deepcopy({"shap": self.shap_config})


class SageMakerClarifyProcessor(Processor):
    """Handles SageMaker Processing tasks to compute bias metrics and model explanations."""

    _CLARIFY_DATA_INPUT = "/opt/ml/processing/input/data"
    _CLARIFY_CONFIG_INPUT = "/opt/ml/processing/input/config"
    _CLARIFY_OUTPUT = "/opt/ml/processing/output"

    def __init__(
        self,
        role: str,
        instance_count: int,
        instance_type: str,
        volume_size_in_gb: int = 30,
        volume_kms_key: Optional[str] = None,
        output_kms_key: Optional[str] = None,
        max_runtime_in_seconds: Optional[int] = None,
        sagemaker_session: Optional[Session] = None,
        env: Optional[Dict[str, str]] = None,
        tags: Optional[List[Dict[str, str]]] = None,
        network_config: Optional[NetworkConfig] = None,
        job_name_prefix: Optional[str] = None,
        version: Optional[str] = None,
        skip_early_validation: bool = False,
    ):
        """Initializes a SageMakerClarifyProcessor to compute bias metrics and model explanations.

        Instance of :class:`~sagemaker.processing.Processor`.

        Args:
            role (str): An AWS IAM role name or ARN. Amazon SageMaker Processing
                uses this role to access AWS resources, such as
                data stored in Amazon S3.
            instance_count (int): The number of instances to run
                a processing job with.
            instance_type (str): The type of
                `EC2 instance <https://aws.amazon.com/ec2/instance-types/>`_
                to use for model inference; for example, ``"ml.c5.xlarge"``.
            volume_size_in_gb (int): Size in GB of the
                `EBS volume <https://docs.aws.amazon.com/sagemaker/latest/dg/host-instance-storage.html>`_.
                to use for storing data during processing (default: 30 GB).
            volume_kms_key (str): A
                `KMS key <https://docs.aws.amazon.com/sagemaker/latest/dg/key-management.html>`_
                for the processing volume (default: None).
            output_kms_key (str): The KMS key ID for processing job outputs (default: None).
            max_runtime_in_seconds (int): Timeout in seconds (default: None).
                After this amount of time, Amazon SageMaker terminates the job,
                regardless of its current status. If ``max_runtime_in_seconds`` is not
                specified, the default value is ``86400`` seconds (24 hours).
            sagemaker_session (:class:`~sagemaker.session.Session`):
                :class:`~sagemaker.session.Session` object which manages interactions
                with Amazon SageMaker and any other AWS services needed. If not specified,
                the Processor creates a :class:`~sagemaker.session.Session`
                using the default AWS configuration chain.
            env (dict[str, str]): Environment variables to be passed to
                the processing jobs (default: None).
            tags (list[dict]): List of tags to be passed to the processing job
                (default: None). For more, see
                https://docs.aws.amazon.com/sagemaker/latest/dg/API_Tag.html.
            network_config (:class:`~sagemaker.network.NetworkConfig`):
                A :class:`~sagemaker.network.NetworkConfig`
                object that configures network isolation, encryption of
                inter-container traffic, security group IDs, and subnets.
            job_name_prefix (str): Processing job name prefix.
            version (str): Clarify version to use.
            skip_early_validation (bool): To skip schema validation of the generated analysis_schema.json.
        """  # noqa E501  # pylint: disable=c0301
        container_uri = image_uris.retrieve("clarify", sagemaker_session.boto_region_name, version)
        self._last_analysis_config = None
        self.job_name_prefix = job_name_prefix
        self.skip_early_validation = skip_early_validation
        super(SageMakerClarifyProcessor, self).__init__(
            role,
            container_uri,
            instance_count,
            instance_type,
            None,  # We manage the entrypoint.
            volume_size_in_gb,
            volume_kms_key,
            output_kms_key,
            max_runtime_in_seconds,
            None,  # We set method-specific job names below.
            sagemaker_session,
            env,
            tags,
            network_config,
        )

    def run(self, **_):
        """Overriding the base class method but deferring to specific run_* methods."""
        raise NotImplementedError(
            "Please choose a method of run_pre_training_bias, run_post_training_bias or "
            "run_explainability."
        )

    def _run(
        self,
        data_config: DataConfig,
        analysis_config: Dict[str, Any],
        wait: bool,
        logs: bool,
        job_name: str,
        kms_key: str,
        experiment_config: Dict[str, str],
    ):
        """Runs a :class:`~sagemaker.processing.ProcessingJob` with the SageMaker Clarify container

        and analysis config.

        Args:
            data_config (:class:`~sagemaker.clarify.DataConfig`): Config of the input/output data.
            analysis_config (dict): Config following the analysis_config.json format.
            wait (bool): Whether the call should wait until the job completes (default: True).
            logs (bool): Whether to show the logs produced by the job.
                Only meaningful when ``wait`` is True (default: True).
            job_name (str): Processing job name.
            kms_key (str): The ARN of the KMS key that is used to encrypt the
                user code file (default: None).
            experiment_config (dict[str, str]): Experiment management configuration.
                Optionally, the dict can contain three keys:
                ``'ExperimentName'``, ``'TrialName'``, and ``'TrialComponentDisplayName'``.

                The behavior of setting these keys is as follows:
                * If ``'ExperimentName'`` is supplied but ``'TrialName'`` is not, a Trial will be
                  automatically created and the job's Trial Component associated with the Trial.
                * If ``'TrialName'`` is supplied and the Trial already exists,
                  the job's Trial Component will be associated with the Trial.
                * If both ``'ExperimentName'`` and ``'TrialName'`` are not supplied,
                  the Trial Component will be unassociated.
                * ``'TrialComponentDisplayName'`` is used for display in Amazon SageMaker Studio.
        """
        # for debugging: to access locally, i.e. without a need to look for it in an S3 bucket
        self._last_analysis_config = analysis_config
        logger.info("Analysis Config: %s", analysis_config)
        if not self.skip_early_validation:
            ANALYSIS_CONFIG_SCHEMA_V1_0.validate(analysis_config)

        with tempfile.TemporaryDirectory() as tmpdirname:
            analysis_config_file = os.path.join(tmpdirname, "analysis_config.json")
            with open(analysis_config_file, "w") as f:
                json.dump(analysis_config, f)
            s3_analysis_config_file = _upload_analysis_config(
                analysis_config_file,
                data_config.s3_analysis_config_output_path or data_config.s3_output_path,
                self.sagemaker_session,
                kms_key,
            )
            config_input = ProcessingInput(
                input_name="analysis_config",
                source=s3_analysis_config_file,
                destination=self._CLARIFY_CONFIG_INPUT,
                s3_data_type="S3Prefix",
                s3_input_mode="File",
                s3_compression_type="None",
            )
            data_input = ProcessingInput(
                input_name="dataset",
                source=data_config.s3_data_input_path,
                destination=self._CLARIFY_DATA_INPUT,
                s3_data_type="S3Prefix",
                s3_input_mode="File",
                s3_data_distribution_type=data_config.s3_data_distribution_type,
                s3_compression_type=data_config.s3_compression_type,
            )
            result_output = ProcessingOutput(
                source=self._CLARIFY_OUTPUT,
                destination=data_config.s3_output_path,
                output_name="analysis_result",
                s3_upload_mode="EndOfJob",
            )

            return super().run(
                inputs=[data_input, config_input],
                outputs=[result_output],
                wait=wait,
                logs=logs,
                job_name=job_name,
                kms_key=kms_key,
                experiment_config=experiment_config,
            )

    def run_pre_training_bias(
        self,
        data_config: DataConfig,
        data_bias_config: BiasConfig,
        methods: Union[str, List[str]] = "all",
        wait: bool = True,
        logs: bool = True,
        job_name: Optional[str] = None,
        kms_key: Optional[str] = None,
        experiment_config: Optional[Dict[str, str]] = None,
    ):
        """Runs a :class:`~sagemaker.processing.ProcessingJob` to compute pre-training bias methods

        Computes the requested ``methods`` on the input data. The ``methods`` compare
        metrics (e.g. fraction of examples) for the sensitive group(s) vs. the other examples.

        Args:
            data_config (:class:`~sagemaker.clarify.DataConfig`): Config of the input/output data.
            data_bias_config (:class:`~sagemaker.clarify.BiasConfig`): Config of sensitive groups.
            methods (str or list[str]): Selects a subset of potential metrics:
                ["`CI <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-bias-metric-class-imbalance.html>`_",
                "`DPL <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-true-label-imbalance.html>`_",
                "`KL <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-kl-divergence.html>`_",
                "`JS <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-jensen-shannon-divergence.html>`_",
                "`LP <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-lp-norm.html>`_",
                "`TVD <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-total-variation-distance.html>`_",
                "`KS <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-kolmogorov-smirnov.html>`_",
                "`CDDL <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-cddl.html>`_"].
                Defaults to str "all" to run all metrics if left unspecified.
            wait (bool): Whether the call should wait until the job completes (default: True).
            logs (bool): Whether to show the logs produced by the job.
                Only meaningful when ``wait`` is True (default: True).
            job_name (str): Processing job name. When ``job_name`` is not specified,
                if ``job_name_prefix`` in :class:`~sagemaker.clarify.SageMakerClarifyProcessor` is
                specified, the job name will be the ``job_name_prefix`` and current timestamp;
                otherwise use ``"Clarify-Pretraining-Bias"`` as prefix.
            kms_key (str): The ARN of the KMS key that is used to encrypt the
                user code file (default: None).
            experiment_config (dict[str, str]): Experiment management configuration.
                Optionally, the dict can contain three keys:
                ``'ExperimentName'``, ``'TrialName'``, and ``'TrialComponentDisplayName'``.

                The behavior of setting these keys is as follows:

                * If ``'ExperimentName'`` is supplied but ``'TrialName'`` is not, a Trial will be
                  automatically created and the job's Trial Component associated with the Trial.
                * If ``'TrialName'`` is supplied and the Trial already exists,
                  the job's Trial Component will be associated with the Trial.
                * If both ``'ExperimentName'`` and ``'TrialName'`` are not supplied,
                  the Trial Component will be unassociated.
                * ``'TrialComponentDisplayName'`` is used for display in Amazon SageMaker Studio.
        """  # noqa E501  # pylint: disable=c0301
        analysis_config = _AnalysisConfigGenerator.bias_pre_training(
            data_config, data_bias_config, methods
        )
        # when name is either not provided (is None) or an empty string ("")
        job_name = job_name or utils.name_from_base(
            self.job_name_prefix or "Clarify-Pretraining-Bias"
        )
        return self._run(
            data_config,
            analysis_config,
            wait,
            logs,
            job_name,
            kms_key,
            experiment_config,
        )

    def run_post_training_bias(
        self,
        data_config: DataConfig,
        data_bias_config: BiasConfig,
        model_config: ModelConfig,
        model_predicted_label_config: ModelPredictedLabelConfig,
        methods: Union[str, List[str]] = "all",
        wait: bool = True,
        logs: bool = True,
        job_name: Optional[str] = None,
        kms_key: Optional[str] = None,
        experiment_config: Optional[Dict[str, str]] = None,
    ):
        """Runs a :class:`~sagemaker.processing.ProcessingJob` to compute posttraining bias

        Spins up a model endpoint and runs inference over the input dataset in
        the ``s3_data_input_path`` (from the :class:`~sagemaker.clarify.DataConfig`) to obtain
        predicted labels. Using model predictions, computes the requested posttraining bias
        ``methods`` that compare metrics (e.g. accuracy, precision, recall) for the
        sensitive group(s) versus the other examples.

        Args:
            data_config (:class:`~sagemaker.clarify.DataConfig`): Config of the input/output data.
            data_bias_config (:class:`~sagemaker.clarify.BiasConfig`): Config of sensitive groups.
            model_config (:class:`~sagemaker.clarify.ModelConfig`): Config of the model and its
                endpoint to be created.
            model_predicted_label_config (:class:`~sagemaker.clarify.ModelPredictedLabelConfig`):
                Config of how to extract the predicted label from the model output.
            methods (str or list[str]): Selector of a subset of potential metrics:
                ["`DPPL <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-dppl.html>`_"
                , "`DI <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-di.html>`_",
                "`DCA <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-dca.html>`_",
                "`DCR <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-dcr.html>`_",
                "`RD <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-rd.html>`_",
                "`DAR <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-dar.html>`_",
                "`DRR <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-drr.html>`_",
                "`AD <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-ad.html>`_",
                "`CDDPL <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-cddpl.html>`_
                ", "`TE <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-te.html>`_",
                "`FT <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-ft.html>`_"].
                Defaults to str "all" to run all metrics if left unspecified.
            wait (bool): Whether the call should wait until the job completes (default: True).
            logs (bool): Whether to show the logs produced by the job.
                Only meaningful when ``wait`` is True (default: True).
            job_name (str): Processing job name. When ``job_name`` is not specified,
                if ``job_name_prefix`` in :class:`~sagemaker.clarify.SageMakerClarifyProcessor`
                is specified, the job name will be the ``job_name_prefix`` and current timestamp;
                otherwise use ``"Clarify-Posttraining-Bias"`` as prefix.
            kms_key (str): The ARN of the KMS key that is used to encrypt the
                user code file (default: None).
            experiment_config (dict[str, str]): Experiment management configuration.
                Optionally, the dict can contain three keys:
                ``'ExperimentName'``, ``'TrialName'``, and ``'TrialComponentDisplayName'``.

                The behavior of setting these keys is as follows:

                * If ``'ExperimentName'`` is supplied but ``'TrialName'`` is not, a Trial will be
                  automatically created and the job's Trial Component associated with the Trial.
                * If ``'TrialName'`` is supplied and the Trial already exists,
                  the job's Trial Component will be associated with the Trial.
                * If both ``'ExperimentName'`` and ``'TrialName'`` are not supplied,
                  the Trial Component will be unassociated.
                * ``'TrialComponentDisplayName'`` is used for display in Amazon SageMaker Studio.
        """  # noqa E501  # pylint: disable=c0301
        analysis_config = _AnalysisConfigGenerator.bias_post_training(
            data_config,
            data_bias_config,
            model_predicted_label_config,
            methods,
            model_config,
        )
        # when name is either not provided (is None) or an empty string ("")
        job_name = job_name or utils.name_from_base(
            self.job_name_prefix or "Clarify-Posttraining-Bias"
        )
        return self._run(
            data_config,
            analysis_config,
            wait,
            logs,
            job_name,
            kms_key,
            experiment_config,
        )

    def run_bias(
        self,
        data_config: DataConfig,
        bias_config: BiasConfig,
        model_config: ModelConfig,
        model_predicted_label_config: Optional[ModelPredictedLabelConfig] = None,
        pre_training_methods: Union[str, List[str]] = "all",
        post_training_methods: Union[str, List[str]] = "all",
        wait: bool = True,
        logs: bool = True,
        job_name: Optional[str] = None,
        kms_key: Optional[str] = None,
        experiment_config: Optional[Dict[str, str]] = None,
    ):
        """Runs a :class:`~sagemaker.processing.ProcessingJob` to compute the requested bias methods

        Computes metrics for both the pre-training and the post-training methods.
        To calculate post-training methods, it spins up a model endpoint and runs inference over the
        input examples in 's3_data_input_path' (from the :class:`~sagemaker.clarify.DataConfig`)
        to obtain predicted labels.

        Args:
            data_config (:class:`~sagemaker.clarify.DataConfig`): Config of the input/output data.
            bias_config (:class:`~sagemaker.clarify.BiasConfig`): Config of sensitive groups.
            model_config (:class:`~sagemaker.clarify.ModelConfig`): Config of the model and its
                endpoint to be created.
            model_predicted_label_config (:class:`~sagemaker.clarify.ModelPredictedLabelConfig`):
                Config of how to extract the predicted label from the model output.
            pre_training_methods (str or list[str]): Selector of a subset of potential metrics:
                ["`CI <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-bias-metric-class-imbalance.html>`_",
                "`DPL <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-true-label-imbalance.html>`_",
                "`KL <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-kl-divergence.html>`_",
                "`JS <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-jensen-shannon-divergence.html>`_",
                "`LP <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-lp-norm.html>`_",
                "`TVD <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-total-variation-distance.html>`_",
                "`KS <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-kolmogorov-smirnov.html>`_",
                "`CDDL <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-cddl.html>`_"].
                Defaults to str "all" to run all metrics if left unspecified.
            post_training_methods (str or list[str]): Selector of a subset of potential metrics:
                ["`DPPL <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-dppl.html>`_"
                , "`DI <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-di.html>`_",
                "`DCA <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-dca.html>`_",
                "`DCR <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-dcr.html>`_",
                "`RD <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-rd.html>`_",
                "`DAR <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-dar.html>`_",
                "`DRR <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-drr.html>`_",
                "`AD <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-ad.html>`_",
                "`CDDPL <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-cddpl.html>`_
                ", "`TE <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-te.html>`_",
                "`FT <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-ft.html>`_"].
                Defaults to str "all" to run all metrics if left unspecified.
            wait (bool): Whether the call should wait until the job completes (default: True).
            logs (bool): Whether to show the logs produced by the job.
                Only meaningful when ``wait`` is True (default: True).
            job_name (str): Processing job name. When ``job_name`` is not specified,
                if ``job_name_prefix`` in :class:`~sagemaker.clarify.SageMakerClarifyProcessor` is
                specified, the job name will be ``job_name_prefix`` and the current timestamp;
                otherwise use ``"Clarify-Bias"`` as prefix.
            kms_key (str): The ARN of the KMS key that is used to encrypt the
                user code file (default: None).
            experiment_config (dict[str, str]): Experiment management configuration.
                Optionally, the dict can contain three keys:
                ``'ExperimentName'``, ``'TrialName'``, and ``'TrialComponentDisplayName'``.

                The behavior of setting these keys is as follows:

                * If ``'ExperimentName'`` is supplied but ``'TrialName'`` is not, a Trial will be
                  automatically created and the job's Trial Component associated with the Trial.
                * If ``'TrialName'`` is supplied and the Trial already exists,
                  the job's Trial Component will be associated with the Trial.
                * If both ``'ExperimentName'`` and ``'TrialName'`` are not supplied,
                  the Trial Component will be unassociated.
                * ``'TrialComponentDisplayName'`` is used for display in Amazon SageMaker Studio.
        """  # noqa E501  # pylint: disable=c0301
        analysis_config = _AnalysisConfigGenerator.bias(
            data_config,
            bias_config,
            model_config,
            model_predicted_label_config,
            pre_training_methods,
            post_training_methods,
        )
        # when name is either not provided (is None) or an empty string ("")
        job_name = job_name or utils.name_from_base(self.job_name_prefix or "Clarify-Bias")
        return self._run(
            data_config,
            analysis_config,
            wait,
            logs,
            job_name,
            kms_key,
            experiment_config,
        )

    def run_explainability(
        self,
        data_config: DataConfig,
        model_config: ModelConfig,
        explainability_config: Union[ExplainabilityConfig, List],
        model_scores: Optional[Union[int, str, ModelPredictedLabelConfig]] = None,
        wait: bool = True,
        logs: bool = True,
        job_name: Optional[str] = None,
        kms_key: Optional[str] = None,
        experiment_config: Optional[Dict[str, str]] = None,
    ):
        """Runs a :class:`~sagemaker.processing.ProcessingJob` computing feature attributions.

        Spins up a model endpoint.

        Currently, only SHAP and  Partial Dependence Plots (PDP) are supported
        as explainability methods.
        You can request both methods or one at a time with the ``explainability_config`` parameter.

        When SHAP is requested in the ``explainability_config``,
        the SHAP algorithm calculates the feature importance for each input example
        in the ``s3_data_input_path`` of the :class:`~sagemaker.clarify.DataConfig`,
        by creating ``num_samples`` copies of the example with a subset of features
        replaced with values from the ``baseline``.
        It then runs model inference to see how the model's prediction changes with the replaced
        features. If the model output returns multiple scores importance is computed for each score.
        Across examples, feature importance is aggregated using ``agg_method``.

        When PDP is requested in the ``explainability_config``,
        the PDP algorithm calculates the dependence of the target response
        on the input features and marginalizes over the values of all other input features.
        The Partial Dependence Plots are included in the output
        `report <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-feature-attribute-baselines-reports.html>`__
        and the corresponding values are included in the analysis output.

        Args:
            data_config (:class:`~sagemaker.clarify.DataConfig`): Config of the input/output data.
            model_config (:class:`~sagemaker.clarify.ModelConfig`): Config of the model and its
                endpoint to be created.
            explainability_config (:class:`~sagemaker.clarify.ExplainabilityConfig` or list):
                Config of the specific explainability method or a list of
                :class:`~sagemaker.clarify.ExplainabilityConfig` objects.
                Currently, SHAP and PDP are the two methods supported.
                You can request multiple methods at once by passing in a list of
                `~sagemaker.clarify.ExplainabilityConfig`.
            model_scores (int or str or :class:`~sagemaker.clarify.ModelPredictedLabelConfig`):
                Index or JSONPath to locate the predicted scores in the model output. This is not
                required if the model output is a single score. Alternatively, it can be an instance
                of :class:`~sagemaker.clarify.SageMakerClarifyProcessor`
                to provide more parameters like ``label_headers``.
            wait (bool): Whether the call should wait until the job completes (default: True).
            logs (bool): Whether to show the logs produced by the job.
                Only meaningful when ``wait`` is True (default: True).
            job_name (str): Processing job name. When ``job_name`` is not specified,
                if ``job_name_prefix`` in :class:`~sagemaker.clarify.SageMakerClarifyProcessor`
                is specified, the job name will be composed of ``job_name_prefix`` and current
                timestamp; otherwise use ``"Clarify-Explainability"`` as prefix.
            kms_key (str): The ARN of the KMS key that is used to encrypt the
                user code file (default: None).
            experiment_config (dict[str, str]): Experiment management configuration.
                Optionally, the dict can contain three keys:
                ``'ExperimentName'``, ``'TrialName'``, and ``'TrialComponentDisplayName'``.

                The behavior of setting these keys is as follows:

                * If ``'ExperimentName'`` is supplied but ``'TrialName'`` is not, a Trial will be
                  automatically created and the job's Trial Component associated with the Trial.
                * If ``'TrialName'`` is supplied and the Trial already exists,
                  the job's Trial Component will be associated with the Trial.
                * If both ``'ExperimentName'`` and ``'TrialName'`` are not supplied,
                  the Trial Component will be unassociated.
                * ``'TrialComponentDisplayName'`` is used for display in Amazon SageMaker Studio.
        """  # noqa E501  # pylint: disable=c0301
        analysis_config = _AnalysisConfigGenerator.explainability(
            data_config, model_config, model_scores, explainability_config
        )
        # when name is either not provided (is None) or an empty string ("")
        job_name = job_name or utils.name_from_base(
            self.job_name_prefix or "Clarify-Explainability"
        )
        return self._run(
            data_config,
            analysis_config,
            wait,
            logs,
            job_name,
            kms_key,
            experiment_config,
        )

    def run_bias_and_explainability(
        self,
        data_config: DataConfig,
        model_config: ModelConfig,
        explainability_config: Union[ExplainabilityConfig, List[ExplainabilityConfig]],
        bias_config: BiasConfig,
        pre_training_methods: Union[str, List[str]] = "all",
        post_training_methods: Union[str, List[str]] = "all",
        model_predicted_label_config: ModelPredictedLabelConfig = None,
        wait=True,
        logs=True,
        job_name=None,
        kms_key=None,
        experiment_config=None,
    ):
        """Runs a :class:`~sagemaker.processing.ProcessingJob` computing feature attributions.

        For bias:
        Computes metrics for both the pre-training and the post-training methods.
        To calculate post-training methods, it spins up a model endpoint and runs inference over the
        input examples in 's3_data_input_path' (from the :class:`~sagemaker.clarify.DataConfig`)
        to obtain predicted labels.

        For Explainability:
        Spins up a model endpoint.

        Currently, only SHAP and  Partial Dependence Plots (PDP) are supported
        as explainability methods.
        You can request both methods or one at a time with the ``explainability_config`` parameter.

        When SHAP is requested in the ``explainability_config``,
        the SHAP algorithm calculates the feature importance for each input example
        in the ``s3_data_input_path`` of the :class:`~sagemaker.clarify.DataConfig`,
        by creating ``num_samples`` copies of the example with a subset of features
        replaced with values from the ``baseline``.
        It then runs model inference to see how the model's prediction changes with the replaced
        features. If the model output returns multiple scores importance is computed for each score.
        Across examples, feature importance is aggregated using ``agg_method``.

        When PDP is requested in the ``explainability_config``,
        the PDP algorithm calculates the dependence of the target response
        on the input features and marginalizes over the values of all other input features.
        The Partial Dependence Plots are included in the output
        `report <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-feature-attribute-baselines-reports.html>`__
        and the corresponding values are included in the analysis output.

        Args:
            data_config (:class:`~sagemaker.clarify.DataConfig`): Config of the input/output data.
            model_config (:class:`~sagemaker.clarify.ModelConfig`): Config of the model and its
                endpoint to be created.
            explainability_config (:class:`~sagemaker.clarify.ExplainabilityConfig` or list):
                Config of the specific explainability method or a list of
                :class:`~sagemaker.clarify.ExplainabilityConfig` objects.
                Currently, SHAP and PDP are the two methods supported.
                You can request multiple methods at once by passing in a list of
                `~sagemaker.clarify.ExplainabilityConfig`.
            bias_config (:class:`~sagemaker.clarify.BiasConfig`): Config of sensitive groups.
            pre_training_methods (str or list[str]): Selector of a subset of potential metrics:
                ["`CI <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-bias-metric-class-imbalance.html>`_",
                "`DPL <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-true-label-imbalance.html>`_",
                "`KL <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-kl-divergence.html>`_",
                "`JS <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-jensen-shannon-divergence.html>`_",
                "`LP <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-lp-norm.html>`_",
                "`TVD <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-total-variation-distance.html>`_",
                "`KS <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-kolmogorov-smirnov.html>`_",
                "`CDDL <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-data-bias-metric-cddl.html>`_"].
                Defaults to str "all" to run all metrics if left unspecified.
            post_training_methods (str or list[str]): Selector of a subset of potential metrics:
                ["`DPPL <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-dppl.html>`_"
                , "`DI <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-di.html>`_",
                "`DCA <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-dca.html>`_",
                "`DCR <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-dcr.html>`_",
                "`RD <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-rd.html>`_",
                "`DAR <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-dar.html>`_",
                "`DRR <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-drr.html>`_",
                "`AD <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-ad.html>`_",
                "`CDDPL <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-cddpl.html>`_
                ", "`TE <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-te.html>`_",
                "`FT <https://docs.aws.amazon.com/sagemaker/latest/dg/clarify-post-training-bias-metric-ft.html>`_"].
                Defaults to str "all" to run all metrics if left unspecified.
            model_predicted_label_config (
                int or
                str or
                :class:`~sagemaker.clarify.ModelPredictedLabelConfig`
            ):
                Index or JSONPath to locate the predicted scores in the model output. This is not
                required if the model output is a single score. Alternatively, it can be an instance
                of :class:`~sagemaker.clarify.SageMakerClarifyProcessor`
                to provide more parameters like ``label_headers``.
            wait (bool): Whether the call should wait until the job completes (default: True).
            logs (bool): Whether to show the logs produced by the job.
                Only meaningful when ``wait`` is True (default: True).
            job_name (str): Processing job name. When ``job_name`` is not specified,
                if ``job_name_prefix`` in :class:`~sagemaker.clarify.SageMakerClarifyProcessor`
                is specified, the job name will be composed of ``job_name_prefix`` and current
                timestamp; otherwise use ``"Clarify-Explainability"`` as prefix.
            kms_key (str): The ARN of the KMS key that is used to encrypt the
                user code file (default: None).
            experiment_config (dict[str, str]): Experiment management configuration.
                Optionally, the dict can contain three keys:
                ``'ExperimentName'``, ``'TrialName'``, and ``'TrialComponentDisplayName'``.

                The behavior of setting these keys is as follows:

                * If ``'ExperimentName'`` is supplied but ``'TrialName'`` is not, a Trial will be
                  automatically created and the job's Trial Component associated with the Trial.
                * If ``'TrialName'`` is supplied and the Trial already exists,
                  the job's Trial Component will be associated with the Trial.
                * If both ``'ExperimentName'`` and ``'TrialName'`` are not supplied,
                  the Trial Component will be unassociated.
                * ``'TrialComponentDisplayName'`` is used for display in Amazon SageMaker Studio.
        """  # noqa E501  # pylint: disable=c0301
        analysis_config = _AnalysisConfigGenerator.bias_and_explainability(
            data_config,
            model_config,
            model_predicted_label_config,
            explainability_config,
            bias_config,
            pre_training_methods,
            post_training_methods,
        )
        # when name is either not provided (is None) or an empty string ("")
        job_name = job_name or utils.name_from_base(
            self.job_name_prefix or "Clarify-Bias-And-Explainability"
        )
        return self._run(
            data_config,
            analysis_config,
            wait,
            logs,
            job_name,
            kms_key,
            experiment_config,
        )


class _AnalysisConfigGenerator:
    """Creates analysis_config objects for different type of runs."""

    @classmethod
    def bias_and_explainability(
        cls,
        data_config: DataConfig,
        model_config: ModelConfig,
        model_predicted_label_config: ModelPredictedLabelConfig,
        explainability_config: Union[ExplainabilityConfig, List[ExplainabilityConfig]],
        bias_config: BiasConfig,
        pre_training_methods: Union[str, List[str]] = "all",
        post_training_methods: Union[str, List[str]] = "all",
    ):
        """Generates a config for Bias and Explainability"""
        analysis_config = {**data_config.get_config(), **bias_config.get_config()}
        analysis_config = cls._add_methods(
            analysis_config,
            pre_training_methods=pre_training_methods,
            post_training_methods=post_training_methods,
            explainability_config=explainability_config,
        )
        analysis_config = cls._add_predictor(
            analysis_config, model_config, model_predicted_label_config
        )
        return analysis_config

    @classmethod
    def explainability(
        cls,
        data_config: DataConfig,
        model_config: ModelConfig,
        model_predicted_label_config: ModelPredictedLabelConfig,
        explainability_config: Union[ExplainabilityConfig, List[ExplainabilityConfig]],
    ):
        """Generates a config for Explainability"""
        analysis_config = data_config.analysis_config
        analysis_config = cls._add_predictor(
            analysis_config, model_config, model_predicted_label_config
        )
        analysis_config = cls._add_methods(
            analysis_config, explainability_config=explainability_config
        )
        return analysis_config

    @classmethod
    def bias_pre_training(
        cls,
        data_config: DataConfig,
        bias_config: BiasConfig,
        methods: Union[str, List[str]],
    ):
        """Generates a config for Bias Pre Training"""
        analysis_config = {**data_config.get_config(), **bias_config.get_config()}
        analysis_config = cls._add_methods(analysis_config, pre_training_methods=methods)
        return analysis_config

    @classmethod
    def bias_post_training(
        cls,
        data_config: DataConfig,
        bias_config: BiasConfig,
        model_predicted_label_config: ModelPredictedLabelConfig,
        methods: Union[str, List[str]],
        model_config: ModelConfig,
    ):
        """Generates a config for Bias Post Training"""
        analysis_config = {**data_config.get_config(), **bias_config.get_config()}
        analysis_config = cls._add_methods(analysis_config, post_training_methods=methods)
        analysis_config = cls._add_predictor(
            analysis_config, model_config, model_predicted_label_config
        )
        return analysis_config

    @classmethod
    def bias(
        cls,
        data_config: DataConfig,
        bias_config: BiasConfig,
        model_config: ModelConfig,
        model_predicted_label_config: ModelPredictedLabelConfig,
        pre_training_methods: Union[str, List[str]] = "all",
        post_training_methods: Union[str, List[str]] = "all",
    ):
        """Generates a config for Bias"""
        analysis_config = {**data_config.get_config(), **bias_config.get_config()}
        analysis_config = cls._add_methods(
            analysis_config,
            pre_training_methods=pre_training_methods,
            post_training_methods=post_training_methods,
        )
        analysis_config = cls._add_predictor(
            analysis_config, model_config, model_predicted_label_config
        )
        return analysis_config

    @classmethod
    def _add_predictor(
        cls,
        analysis_config: Dict,
        model_config: ModelConfig,
        model_predicted_label_config: ModelPredictedLabelConfig,
    ):
        """Extends analysis config with predictor."""
        analysis_config = {**analysis_config}
        analysis_config["predictor"] = model_config.get_predictor_config()
        if isinstance(model_predicted_label_config, ModelPredictedLabelConfig):
            (
                probability_threshold,
                predictor_config,
            ) = model_predicted_label_config.get_predictor_config()
            if predictor_config:
                analysis_config["predictor"].update(predictor_config)
            _set(probability_threshold, "probability_threshold", analysis_config)
        else:
            _set(model_predicted_label_config, "label", analysis_config["predictor"])
        return analysis_config

    @classmethod
    def _add_methods(
        cls,
        analysis_config: Dict,
        pre_training_methods: Union[str, List[str]] = None,
        post_training_methods: Union[str, List[str]] = None,
        explainability_config: Union[ExplainabilityConfig, List[ExplainabilityConfig]] = None,
        report=True,
    ):
        """Extends analysis config with methods."""
        # validate
        params = [pre_training_methods, post_training_methods, explainability_config]
        if not any(params):
            raise AttributeError(
                "analysis_config must have at least one working method: "
                "One of the "
                "`pre_training_methods`, `post_training_methods`, `explainability_config`."
            )

        # main logic
        analysis_config = {**analysis_config}
        if "methods" not in analysis_config:
            analysis_config["methods"] = {}

        if report:
            analysis_config["methods"]["report"] = {
                "name": "report",
                "title": "Analysis Report",
            }

        if pre_training_methods:
            analysis_config["methods"]["pre_training_bias"] = {"methods": pre_training_methods}

        if post_training_methods:
            analysis_config["methods"]["post_training_bias"] = {"methods": post_training_methods}

        if explainability_config is not None:
            explainability_methods = cls._merge_explainability_configs(explainability_config)
            analysis_config["methods"] = {
                **analysis_config["methods"],
                **explainability_methods,
            }
        return analysis_config

    @classmethod
    def _merge_explainability_configs(
        cls,
        explainability_config: Union[ExplainabilityConfig, List[ExplainabilityConfig]],
    ):
        """Merges explainability configs, when more than one."""
        if isinstance(explainability_config, list):
            explainability_methods = {}
            if len(explainability_config) == 0:
                raise ValueError("Please provide at least one explainability config.")
            for config in explainability_config:
                explain_config = config.get_explainability_config()
                explainability_methods.update(explain_config)
            if not len(explainability_methods) == len(explainability_config):
                raise ValueError("Duplicate explainability configs are provided")
            if (
                "shap" not in explainability_methods
                and "features" not in explainability_methods["pdp"]
            ):
                raise ValueError("PDP features must be provided when ShapConfig is not provided")
            return explainability_methods
        if (
            isinstance(explainability_config, PDPConfig)
            and "features" not in explainability_config.get_explainability_config()["pdp"]
        ):
            raise ValueError("PDP features must be provided when ShapConfig is not provided")
        return explainability_config.get_explainability_config()


def _upload_analysis_config(analysis_config_file, s3_output_path, sagemaker_session, kms_key):
    """Uploads the local ``analysis_config_file`` to the ``s3_output_path``.

    Args:
        analysis_config_file (str): File path to the local analysis config file.
        s3_output_path (str): S3 prefix to store the analysis config file.
        sagemaker_session (:class:`~sagemaker.session.Session`):
            :class:`~sagemaker.session.Session` object which manages interactions with
            Amazon SageMaker and any other AWS services needed. If not specified,
            the processor creates a :class:`~sagemaker.session.Session`
            using the default AWS configuration chain.
        kms_key (str): The ARN of the KMS key that is used to encrypt the
            user code file (default: None).

    Returns:
        The S3 URI of the uploaded file.
    """
    return s3.S3Uploader.upload(
        local_path=analysis_config_file,
        desired_s3_uri=s3_output_path,
        sagemaker_session=sagemaker_session,
        kms_key=kms_key,
    )


def _set(value, key, dictionary):
    """Sets dictionary[key] = value if value is not None."""
    if value is not None:
        dictionary[key] = value