Edward J. Schwartz
commited on
Commit
•
f57ae1a
1
Parent(s):
221e108
Add first loading script that has configs for splits
Browse files- oo-method-test-split.py +112 -0
oo-method-test-split.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/python
|
2 |
+
|
3 |
+
import datasets
|
4 |
+
|
5 |
+
import pyarrow as pa
|
6 |
+
import pyarrow.parquet as pq
|
7 |
+
|
8 |
+
BASE_DATASET = "ejschwartz/oo-method-test"
|
9 |
+
|
10 |
+
class OOMethodTestDataset(datasets.ArrowBasedBuilder):
|
11 |
+
|
12 |
+
BUILDER_CONFIGS = [
|
13 |
+
datasets.BuilderConfig(
|
14 |
+
name="combined",
|
15 |
+
version=datasets.Version("1.0.0"),
|
16 |
+
description="All data files combined",
|
17 |
+
),
|
18 |
+
datasets.BuilderConfig(
|
19 |
+
name="byrow",
|
20 |
+
version=datasets.Version("1.0.0"),
|
21 |
+
description="Split by example (dumb)",
|
22 |
+
),
|
23 |
+
datasets.BuilderConfig(
|
24 |
+
name="byfuncname",
|
25 |
+
version=datasets.Version("1.0.0"),
|
26 |
+
description="Split by function name",
|
27 |
+
)
|
28 |
+
|
29 |
+
]
|
30 |
+
|
31 |
+
def __init__(self, *args, **kwargs):
|
32 |
+
super().__init__(*args, **kwargs)
|
33 |
+
|
34 |
+
def _info(self):
|
35 |
+
return datasets.DatasetInfo()
|
36 |
+
|
37 |
+
def _split_generators(self, dl_manager):
|
38 |
+
ds = datasets.load_dataset(BASE_DATASET)
|
39 |
+
|
40 |
+
#print(files)
|
41 |
+
#print(downloaded_files)
|
42 |
+
|
43 |
+
if self.config.name == "combined":
|
44 |
+
|
45 |
+
return [
|
46 |
+
datasets.SplitGenerator(
|
47 |
+
name="combined",
|
48 |
+
gen_kwargs={
|
49 |
+
"ds": ds['combined'],
|
50 |
+
},
|
51 |
+
),
|
52 |
+
]
|
53 |
+
|
54 |
+
elif self.config.name == "byrow":
|
55 |
+
|
56 |
+
ds = ds['combined'].train_test_split(test_size=0.1, seed=42)
|
57 |
+
#print(ds)
|
58 |
+
|
59 |
+
return [
|
60 |
+
datasets.SplitGenerator(
|
61 |
+
name="train",
|
62 |
+
gen_kwargs={
|
63 |
+
"ds": ds['train'],
|
64 |
+
},
|
65 |
+
),
|
66 |
+
datasets.SplitGenerator(
|
67 |
+
name="test",
|
68 |
+
gen_kwargs={
|
69 |
+
"ds": ds['test'],
|
70 |
+
},
|
71 |
+
),
|
72 |
+
|
73 |
+
]
|
74 |
+
|
75 |
+
elif self.config.name == "byfuncname":
|
76 |
+
|
77 |
+
ds = ds['combined']
|
78 |
+
|
79 |
+
unique_names = ds.unique('Name')
|
80 |
+
nameds = datasets.Dataset.from_dict({'Name': unique_names})
|
81 |
+
|
82 |
+
name_split = nameds.train_test_split(test_size=0.1, seed=42)
|
83 |
+
#print(name_split)
|
84 |
+
|
85 |
+
train_name = name_split['train']['Name']
|
86 |
+
test_name = name_split['test']['Name']
|
87 |
+
|
88 |
+
return [
|
89 |
+
datasets.SplitGenerator(
|
90 |
+
name="train",
|
91 |
+
gen_kwargs={
|
92 |
+
"ds": ds.filter(lambda r: r['Name'] in train_name),
|
93 |
+
},
|
94 |
+
),
|
95 |
+
datasets.SplitGenerator(
|
96 |
+
name="test",
|
97 |
+
gen_kwargs={
|
98 |
+
"ds": ds.filter(lambda r: r['Name'] in test_name),
|
99 |
+
},
|
100 |
+
),
|
101 |
+
|
102 |
+
]
|
103 |
+
|
104 |
+
else:
|
105 |
+
assert False
|
106 |
+
|
107 |
+
def _generate_tables(self, ds):
|
108 |
+
|
109 |
+
# Converting to pandas is silly, but the old version of datasets doesn't
|
110 |
+
# seem to have a way to convert to Arrow?
|
111 |
+
for i, batch in enumerate(ds.to_pandas(batched=True)):
|
112 |
+
yield i, pa.Table.from_pandas(batch)
|