-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_models.py
More file actions
89 lines (75 loc) · 2.91 KB
/
diff_models.py
File metadata and controls
89 lines (75 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2026 Apple Inc. All Rights Reserved.
#
import json
import os
import argparse
from safe_dictionary_learning import parse_nnlr_string, parse_dnf_string
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--data-root", type=str, default="data/")
parser.add_argument("--features", type=str, default="beavertails_drug_abuse_weapons_banned_substance_features.txt")
parser.add_argument("--dataset", type=str, default="beavertails_drug_abuse_weapons_banned_substance_annotations_full.csv")
parser.add_argument("--oracle", type=str, default="gpt-4o")
parser.add_argument("--save", action="store_true")
args = parser.parse_args()
results_dir = os.path.join(args.data_root, "results")
datastring = args.dataset[:-4]
print(datastring)
with open(os.path.join(results_dir, f"{datastring}_results.jsonl")) as f:
content = f.read()
models = []
# Split by '}\n{' pattern to separate objects
json_strings = content.strip().split('}\n{')
for i, json_str in enumerate(json_strings):
# Add back the braces that were removed by splitting
if i == 0:
json_str = json_str + '}'
elif i == len(json_strings) - 1:
json_str = '{' + json_str
else:
json_str = '{' + json_str + '}'
# Remove extra whitespace and parse
json_str = json_str.strip()
models.append(json.loads(json_str))
## diff NNLR
modelstrings = {}
for model in models:
if model["model"] != args.oracle:
modelstrings[model["model"]] = model["NNLR"]["Model"]
else:
oracle_model = model["NNLR"]["Model"]
oracle_features = set(parse_nnlr_string(oracle_model))
nnlr_outfiles = []
for annotator in modelstrings.keys():
modelstr = modelstrings[annotator]
features = set(parse_nnlr_string(modelstr))
nnlr_outfiles.append({
"model": annotator,
"diff": list(oracle_features.difference(features))
})
## diff DNF
modelstrings = {}
for model in models:
if model["model"] != args.oracle:
modelstrings[model["model"]] = model["DNF"]["Model"]
else:
oracle_model = model["DNF"]["Model"]
oracle_features = set(parse_dnf_string(oracle_model))
dnf_outfiles = []
for annotator in modelstrings.keys():
modelstr = modelstrings[annotator]
features = set(parse_dnf_string(modelstr))
dnf_outfiles.append({
"model": annotator,
"diff": list(oracle_features.difference(features))
})
resultsdict = {
"oracle": args.oracle,
"NNLR": nnlr_outfiles,
"DNF": dnf_outfiles,
}
if args.save:
with open(os.path.join(results_dir, f"{datastring}_{args.oracle}_diffs.jsonl"), "w") as f:
json.dump(resultsdict, f, indent=2)