soldni commited on
Commit
7f17d57
1 Parent(s): a824956
Files changed (1) hide show
  1. prepare_for_release.py +41 -29
prepare_for_release.py CHANGED
@@ -12,7 +12,7 @@ from multiprocessing import Manager, Pool, cpu_count, set_start_method
12
  from queue import Queue
13
  from threading import Thread
14
  from time import sleep
15
- from typing import Optional, Tuple
16
 
17
  from smashed.utils import (
18
  MultiPath,
@@ -26,35 +26,40 @@ from tqdm import tqdm
26
 
27
 
28
  def process_single(
29
- io_paths: Tuple[MultiPath, MultiPath],
30
  version: str,
31
  pbar_queue: Optional[Queue] = None,
32
  ):
33
- src, dst = io_paths
34
  docs_cnt = 0
35
 
36
- with ExitStack() as stack:
37
- in_file = stack.enter_context(stream_file_for_read(src, "rb"))
38
- in_stream = stack.enter_context(decompress_stream(in_file, "rt"))
39
- out_file = stack.enter_context(open_file_for_write(dst, "wb"))
40
- out_stream = stack.enter_context(compress_stream(out_file, "wt"))
 
 
 
 
 
 
 
 
41
 
42
- for line in in_stream:
43
- data = json.loads(line)
44
- data.pop("metadata", None)
45
- data["source"] = "s2ag" if "dataset=s2ag" in src.as_str else "s2orc"
46
- data["split"] = "train" if "split=train" in src.as_str else "valid"
47
- data["version"] = version
48
 
49
- out_stream.write(json.dumps(data) + "\n")
50
- docs_cnt += 1
 
51
 
52
- if pbar_queue is not None and docs_cnt % 1000 == 0:
53
- pbar_queue.put((0, docs_cnt))
54
- docs_cnt = 0
55
 
56
  if pbar_queue is not None:
57
- pbar_queue.put((1, docs_cnt))
58
 
59
 
60
  def threaded_progressbar(q: Queue, timeout: float, total_files: Optional[int] = None):
@@ -81,9 +86,7 @@ def main():
81
  ap = argparse.ArgumentParser()
82
  ap.add_argument("src", type=str, help="Source path")
83
  ap.add_argument("dst", type=str, help="Destination path")
84
- ap.add_argument(
85
- "--debug", default=False, help="Debug mode", action="store_true"
86
- )
87
  ap.add_argument(
88
  "--parallel", type=int, default=cpu_count(), help="Number of parallel processes"
89
  )
@@ -95,11 +98,20 @@ def main():
95
  src = MultiPath.parse(opts.src)
96
  dst = MultiPath.parse(opts.dst)
97
 
98
- src_paths = [MultiPath.parse(p) for p in recursively_list_files(src)]
99
- dst_paths = [
100
- dst / (diff) if len(diff := (single_src - src)) > 0 else dst
101
- for single_src in src_paths
102
- ]
 
 
 
 
 
 
 
 
 
103
 
104
  if opts.debug:
105
  with tqdm(total=len(src_paths)) as pbar:
@@ -114,7 +126,7 @@ def main():
114
  pbar_queue: Queue = (manager := Manager()).Queue()
115
  pbar_thread = Thread(
116
  target=threaded_progressbar,
117
- args=(pbar_queue, 0.1, len(src_paths)),
118
  daemon=True,
119
  )
120
  pbar_thread.start()
 
12
  from queue import Queue
13
  from threading import Thread
14
  from time import sleep
15
+ from typing import List, Optional, Tuple
16
 
17
  from smashed.utils import (
18
  MultiPath,
 
26
 
27
 
28
  def process_single(
29
+ io_paths: Tuple[List[MultiPath], MultiPath],
30
  version: str,
31
  pbar_queue: Optional[Queue] = None,
32
  ):
33
+ all_src, dst = io_paths
34
  docs_cnt = 0
35
 
36
+ with ExitStack() as in_stack, ExitStack() as out_stack:
37
+ out_file = out_stack.enter_context(open_file_for_write(dst, "wb"))
38
+ out_stream = out_stack.enter_context(compress_stream(out_file, "wt"))
39
+ for src in all_src:
40
+ in_file = in_stack.enter_context(stream_file_for_read(src, "rb"))
41
+ in_stream = in_stack.enter_context(decompress_stream(in_file, "rt"))
42
+
43
+ for line in in_stream:
44
+ data = json.loads(line)
45
+ data.pop("metadata", None)
46
+ data["source"] = "s2ag" if "dataset=s2ag" in src.as_str else "s2orc"
47
+ data["split"] = "train" if "split=train" in src.as_str else "valid"
48
+ data["version"] = version
49
 
50
+ out_stream.write(json.dumps(data) + "\n")
51
+ docs_cnt += 1
 
 
 
 
52
 
53
+ if pbar_queue is not None and docs_cnt % 1000 == 0:
54
+ pbar_queue.put((0, docs_cnt))
55
+ docs_cnt = 0
56
 
57
+ in_stack.pop_all().close()
58
+ if pbar_queue is not None:
59
+ pbar_queue.put((1, 0))
60
 
61
  if pbar_queue is not None:
62
+ pbar_queue.put((0, docs_cnt))
63
 
64
 
65
  def threaded_progressbar(q: Queue, timeout: float, total_files: Optional[int] = None):
 
86
  ap = argparse.ArgumentParser()
87
  ap.add_argument("src", type=str, help="Source path")
88
  ap.add_argument("dst", type=str, help="Destination path")
89
+ ap.add_argument("--debug", default=False, help="Debug mode", action="store_true")
 
 
90
  ap.add_argument(
91
  "--parallel", type=int, default=cpu_count(), help="Number of parallel processes"
92
  )
 
98
  src = MultiPath.parse(opts.src)
99
  dst = MultiPath.parse(opts.dst)
100
 
101
+ # catch all in top level directory
102
+ grouped_src_paths = {}
103
+ for single_src in recursively_list_files(src):
104
+ single_src_dir, _ = single_src.rsplit("/", 1)
105
+ grouped_src_paths.setdefault(single_src_dir, []).append(single_src)
106
+
107
+ src_paths: List[List[MultiPath]] = []
108
+ dst_paths: List[MultiPath] = []
109
+ for dir_name, dir_values in grouped_src_paths.items():
110
+ src_paths.append([MultiPath.parse(p) for p in dir_values])
111
+ dir_path = MultiPath.parse(dir_name.replace("part_id=", "") + ".gz")
112
+ dst_path = dst / (diff) if len(diff := (dir_path - src)) > 0 else dst
113
+ dst_path.path = dst_path.path.replace("split=", "").replace("dataset=", "")
114
+ dst_paths.append(dst_path)
115
 
116
  if opts.debug:
117
  with tqdm(total=len(src_paths)) as pbar:
 
126
  pbar_queue: Queue = (manager := Manager()).Queue()
127
  pbar_thread = Thread(
128
  target=threaded_progressbar,
129
+ args=(pbar_queue, 0.1, sum(len(p) for p in src_paths)),
130
  daemon=True,
131
  )
132
  pbar_thread.start()