File size: 5,187 Bytes
f57ae1a 65df539 f57ae1a 65df539 f57ae1a 65df539 f57ae1a 755bba7 2ba158b 9199788 01df533 9199788 f57ae1a 65df539 f57ae1a 65df539 f57ae1a 65df539 f57ae1a 65df539 f57ae1a 65df539 f57ae1a |
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 |
#!/usr/bin/python
import datasets
import itertools
import os
import pyarrow as pa
import pyarrow.parquet as pq
BASE_DATASET = "ejschwartz/oo-method-test"
def setexe(r):
r['Dirname'], r['Exename'] = os.path.split(r['Binary'])
return r
class OOMethodTestDataset(datasets.ArrowBasedBuilder):
BUILDER_CONFIGS = [
datasets.BuilderConfig(
name="combined",
version=datasets.Version("1.0.0"),
description="All data files combined",
),
datasets.BuilderConfig(
name="byrow",
version=datasets.Version("1.0.0"),
description="Split by example (dumb)",
),
datasets.BuilderConfig(
name="byfuncname",
version=datasets.Version("1.0.0"),
description="Split by function name",
),
datasets.BuilderConfig(
name="bylibrary",
version=datasets.Version("1.0.0"),
description="Split so that library functions (those appearing in >1 exe) are used for training, and non-library functions are used for testing",
)
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def _info(self):
return datasets.DatasetInfo(
features = datasets.Features({'Binary': datasets.Value(dtype='string', id=None),
'Addr': datasets.Value(dtype='string'),
'Name': datasets.Value(dtype='string'),
'Type': datasets.ClassLabel(num_classes=2, names=['func', 'method']),
'Disassembly': datasets.Value(dtype='string'),
'Dirname': datasets.Value(dtype='string'),
'Exename': datasets.Value(dtype='string')}))
def _split_generators(self, dl_manager):
ds = datasets.load_dataset(BASE_DATASET)['combined']
#print(files)
#print(downloaded_files)
ds = ds.map(setexe, batched=False)
if self.config.name == "combined":
return [
datasets.SplitGenerator(
name="combined",
gen_kwargs={
"ds": ds,
},
),
]
elif self.config.name == "byrow":
ds = ds.train_test_split(test_size=0.1, seed=42)
#print(ds)
return [
datasets.SplitGenerator(
name="train",
gen_kwargs={
"ds": ds['train'],
},
),
datasets.SplitGenerator(
name="test",
gen_kwargs={
"ds": ds['test'],
},
),
]
elif self.config.name == "byfuncname":
unique_names = ds.unique('Name')
nameds = datasets.Dataset.from_dict({'Name': unique_names})
name_split = nameds.train_test_split(test_size=0.1, seed=42)
#print(name_split)
train_name = name_split['train']['Name']
test_name = name_split['test']['Name']
return [
datasets.SplitGenerator(
name="train",
gen_kwargs={
"ds": ds.filter(lambda r: r['Name'] in train_name),
},
),
datasets.SplitGenerator(
name="test",
gen_kwargs={
"ds": ds.filter(lambda r: r['Name'] in test_name),
},
),
]
elif self.config.name == "bylibrary":
# A function (name) is a library function if it appears in more than one Exename
# this is (('func', 'oo.exe'): 123)
testcount = set(zip(ds['Name'], ds['Exename']))
# sorted pairs by function name
testcount = sorted(testcount, key=lambda x: x[0])
# group by function name
grouped = itertools.groupby(testcount, lambda t: t[0])
grouped = {k: [b for _,b in g] for k, g in grouped}
library_func_names = {f for f, exes in grouped.items() if len(exes) > 1}
nonlibrary_func_names = {f for f, exes in grouped.items() if len(exes) == 1}
return [
datasets.SplitGenerator(
name="train",
gen_kwargs={
"ds": ds.filter(lambda r: r['Name'] in library_func_names),
},
),
datasets.SplitGenerator(
name="test",
gen_kwargs={
"ds": ds.filter(lambda r: r['Name'] in nonlibrary_func_names),
},
),
]
else:
assert False
def _generate_tables(self, ds):
# Converting to pandas is silly, but the old version of datasets doesn't
# seem to have a way to convert to Arrow?
for i, batch in enumerate(ds.to_pandas(batched=True)):
yield i, pa.Table.from_pandas(batch)
|