lalababa commited on
Commit
6abb2b2
·
verified ·
1 Parent(s): 063ee10

Update time_series_library.py

Browse files
Files changed (1) hide show
  1. time_series_library.py +42 -21
time_series_library.py CHANGED
@@ -22,37 +22,58 @@ class TimeSeriesLibrary(GeneratorBasedBuilder):
22
  return self.dataset_info_from_configs(features=Features(cols))
23
 
24
 
 
 
25
  def _split_generators(self, dl_manager):
26
  dfiles = self.config.data_files
27
  files = list(dfiles) if isinstance(dfiles, (list, tuple)) else [dfiles]
28
 
29
- def find_and_pop(keys):
30
- keys = [k.lower() for k in keys]
31
- for i, f in enumerate(files):
32
- name = os.path.basename(str(f)).lower()
33
- if any(k in name for k in keys):
34
- return files.pop(i)
35
- return None
36
 
37
- train_f = find_and_pop(["train"])
38
- val_f = find_and_pop(["val", "valid", "validation"])
39
- test_f = find_and_pop(["test"])
40
- label_f = find_and_pop(["test_label", "label", "labels"])
 
 
 
 
 
 
 
 
 
 
41
 
42
- gens = []
43
- if train_f:
44
- gens.append(SplitGenerator(name=Split.TRAIN, gen_kwargs={"filepath": train_f}))
45
- if val_f:
46
- gens.append(SplitGenerator(name=Split.VALIDATION, gen_kwargs={"filepath": val_f}))
47
- if test_f:
48
- gens.append(SplitGenerator(name=Split.TEST, gen_kwargs={"filepath": test_f}))
49
- if label_f:
50
- gens.append(SplitGenerator(name="test_label", gen_kwargs={"filepath": label_f}))
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  if not gens:
53
- gens = [SplitGenerator(name=Split.TRAIN, gen_kwargs={"filepath": files})]
 
54
 
55
  return gens
 
56
 
57
 
58
  def _generate_examples(self, filepath, row_slice=None):
 
22
  return self.dataset_info_from_configs(features=Features(cols))
23
 
24
 
25
+ from datasets import Split, SplitGenerator
26
+
27
  def _split_generators(self, dl_manager):
28
  dfiles = self.config.data_files
29
  files = list(dfiles) if isinstance(dfiles, (list, tuple)) else [dfiles]
30
 
31
+ gens = []
 
 
 
 
 
 
32
 
33
+ def split_name_for(path, used):
34
+ """按文件名自动判别 split;若重复则加后缀避免合并"""
35
+ name = os.path.basename(str(path)).lower()
36
+ if "train" in name:
37
+ base = "train"
38
+ elif any(k in name for k in ["val", "valid", "validation"]):
39
+ base = "validation"
40
+ elif "test_label" in name or "label" in name:
41
+ base = "test_label"
42
+ elif "test" in name:
43
+ base = "test"
44
+ else:
45
+ # 没有关键字就用文件名(去掉拓展名)
46
+ base = os.path.splitext(os.path.basename(str(path)))[0]
47
 
48
+ # 避免同名冲突:train/train_1/train_2...
49
+ s = base
50
+ idx = 1
51
+ while s in used:
52
+ s = f"{base}_{idx}"
53
+ idx += 1
54
+ used.add(s)
55
+ return s
 
56
 
57
+ used_names = set()
58
+ for f in files:
59
+ sname = split_name_for(f, used_names)
60
+ # 标准 split 用内置常量;自定义用 Split(sname)
61
+ if sname == "train":
62
+ gens.append(SplitGenerator(name=Split.TRAIN, gen_kwargs={"filepath": f}))
63
+ elif sname == "validation":
64
+ gens.append(SplitGenerator(name=Split.VALIDATION, gen_kwargs={"filepath": f}))
65
+ elif sname == "test":
66
+ gens.append(SplitGenerator(name=Split.TEST, gen_kwargs={"filepath": f}))
67
+ else:
68
+ gens.append(SplitGenerator(name=Split(sname), gen_kwargs={"filepath": f}))
69
+
70
+ # 如果列表为空(极端情况),兜底把所有文件各自做一个 split
71
  if not gens:
72
+ for i, f in enumerate(files):
73
+ gens.append(SplitGenerator(name=Split(f"file_{i}"), gen_kwargs={"filepath": f}))
74
 
75
  return gens
76
+
77
 
78
 
79
  def _generate_examples(self, filepath, row_slice=None):