Update simulation.py
Browse files- simulation.py +53 -26
simulation.py
CHANGED
|
@@ -146,6 +146,11 @@ class SequenceGenerator():
|
|
| 146 |
return seq, new_mask, idx
|
| 147 |
|
| 148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
class SimulationConfig(datasets.BuilderConfig):
|
| 151 |
def __init__(self, n_seq, num_motifs=1, motif_length=10, error_rate=0.0, seed=0, add_primer=False, **kwargs):
|
|
@@ -166,10 +171,12 @@ class Simulation(datasets.GeneratorBasedBuilder):
|
|
| 166 |
|
| 167 |
BUILDER_CONFIGS = [
|
| 168 |
SimulationConfig(name="multiple", num_motifs=10, error_rate=0.1, n_seq=10000, seed=0),
|
| 169 |
-
SimulationConfig(name="paired", n_seq=5000, seed=0)
|
|
|
|
|
|
|
| 170 |
]
|
| 171 |
|
| 172 |
-
DEFAULT_CONFIG_NAME = "multiple"
|
| 173 |
|
| 174 |
def _info(self):
|
| 175 |
return datasets.DatasetInfo(
|
|
@@ -188,21 +195,33 @@ class Simulation(datasets.GeneratorBasedBuilder):
|
|
| 188 |
)
|
| 189 |
|
| 190 |
def _split_generators(self, dl_manager):
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
simulator = SequenceGenerator(num_motifs=num_motifs, motif_length=motif_length,
|
| 207 |
error_rate=error_rate, seed=seed,
|
| 208 |
add_primer=add_primer)
|
|
@@ -216,15 +235,23 @@ class Simulation(datasets.GeneratorBasedBuilder):
|
|
| 216 |
"motif_mask": mask,
|
| 217 |
}
|
| 218 |
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
dataset = load_dataset("simulation.py", name="paired", split="all")
|
| 223 |
-
print(dataset)
|
| 224 |
-
dataset = load_dataset("simulation.py", name="multiple", split="all")
|
| 225 |
-
print(dataset)
|
| 226 |
-
|
| 227 |
-
# simulator = SequenceGenerator(num_motifs=10, error_rate=0.1, seed=0)
|
| 228 |
-
# data = simulator.sample(10000)
|
| 229 |
|
| 230 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
return seq, new_mask, idx
|
| 147 |
|
| 148 |
|
| 149 |
+
DATA_FILES = {"multiple-666": {"train": "https://huggingface.co/datasets/thewall/Simulation/resolve/main/data/multiple-666-train.parquet",
|
| 150 |
+
"test": "https://huggingface.co/datasets/thewall/Simulation/resolve/main/data/multiple-666-test.parquet"},
|
| 151 |
+
"paired-666": {"train": "https://huggingface.co/datasets/thewall/Simulation/resolve/main/data/paired-666-train.parquet",
|
| 152 |
+
"test": "https://huggingface.co/datasets/thewall/Simulation/resolve/main/data/paired-666-test.parquet"},
|
| 153 |
+
}
|
| 154 |
|
| 155 |
class SimulationConfig(datasets.BuilderConfig):
|
| 156 |
def __init__(self, n_seq, num_motifs=1, motif_length=10, error_rate=0.0, seed=0, add_primer=False, **kwargs):
|
|
|
|
| 171 |
|
| 172 |
BUILDER_CONFIGS = [
|
| 173 |
SimulationConfig(name="multiple", num_motifs=10, error_rate=0.1, n_seq=10000, seed=0),
|
| 174 |
+
SimulationConfig(name="paired", n_seq=5000, seed=0),
|
| 175 |
+
SimulationConfig(name="multiple-666", num_motifs=10, error_rate=0.1, n_seq=10000, seed=0),
|
| 176 |
+
SimulationConfig(name="paired-666", n_seq=5000, seed=0),
|
| 177 |
]
|
| 178 |
|
| 179 |
+
DEFAULT_CONFIG_NAME = "multiple-666"
|
| 180 |
|
| 181 |
def _info(self):
|
| 182 |
return datasets.DatasetInfo(
|
|
|
|
| 195 |
)
|
| 196 |
|
| 197 |
def _split_generators(self, dl_manager):
|
| 198 |
+
if self.config.name in DATA_FILES:
|
| 199 |
+
train_data_file = dl_manager.download(DATA_FILES[self.config.name]['train'])
|
| 200 |
+
test_data_file = dl_manager.download(DATA_FILES[self.config.name]['train'])
|
| 201 |
+
dataset = datasets.load_dataset("parquet", data_files={"train": train_data_file,
|
| 202 |
+
"test": test_data_file})
|
| 203 |
+
|
| 204 |
+
train_iterator = self._iterator(dataset['train'])
|
| 205 |
+
test_iterator = self._iterator(dataset['test'])
|
| 206 |
+
return [
|
| 207 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"iterator_fn": train_iterator}),
|
| 208 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"iterator_fn": test_iterator}),
|
| 209 |
+
]
|
| 210 |
+
else:
|
| 211 |
+
kwargs = {"num_motifs": self.config.num_motifs,
|
| 212 |
+
"motif_length": self.config.motif_length,
|
| 213 |
+
"error_rate": self.config.error_rate,
|
| 214 |
+
"seed": self.config.seed,
|
| 215 |
+
"add_primer": self.config.add_primer,
|
| 216 |
+
"sample_num": self.config.n_seq
|
| 217 |
+
}
|
| 218 |
+
iterator = self._sample(**kwargs)
|
| 219 |
+
|
| 220 |
+
return [
|
| 221 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"iterator_fn": iterator}),
|
| 222 |
+
]
|
| 223 |
+
|
| 224 |
+
def _sample(self, num_motifs, motif_length, error_rate, seed, add_primer, sample_num):
|
| 225 |
simulator = SequenceGenerator(num_motifs=num_motifs, motif_length=motif_length,
|
| 226 |
error_rate=error_rate, seed=seed,
|
| 227 |
add_primer=add_primer)
|
|
|
|
| 235 |
"motif_mask": mask,
|
| 236 |
}
|
| 237 |
|
| 238 |
+
def _iterator(self, dataset):
|
| 239 |
+
for row in dataset:
|
| 240 |
+
yield row['id'], row
|
| 241 |
|
| 242 |
+
def _generate_examples(self, iterator_fn):
|
| 243 |
+
yield from iterator_fn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
|
| 246 |
+
if __name__=="__main__":
|
| 247 |
+
from datasets import load_dataset
|
| 248 |
+
# splited_data = dataset.train_test_split(train_size=0.9, seed=666)
|
| 249 |
+
# splited_data['train'].to_parquet("paired-666-train.parquet")
|
| 250 |
+
# splited_data['test'].to_parquet("paired-666-test.parquet")
|
| 251 |
+
|
| 252 |
+
# dataset = load_dataset(path = "thewall/simulation", name="multiple", split="all")
|
| 253 |
+
# splited_data = dataset.train_test_split(train_size=0.9, seed=666)
|
| 254 |
+
# splited_data['train'].to_parquet("multiple-666-train.parquet")
|
| 255 |
+
# splited_data['test'].to_parquet("multiple-666-test.parquet")
|
| 256 |
+
|
| 257 |
+
dataset = load_dataset("simulation.py", name="paired-666", split="test")
|