|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import os |
|
|
import gc |
|
|
import re |
|
|
import string |
|
|
import json |
|
|
import logging |
|
|
import shutil |
|
|
from datetime import datetime |
|
|
from tqdm import tqdm |
|
|
|
|
|
import fitz |
|
|
from datasets import Dataset, load_dataset |
|
|
|
|
|
|
|
|
output_path = os.path.join(".","data") |
|
|
|
|
|
def remove(path): |
|
|
""" param <path> could either be relative or absolute. """ |
|
|
if os.path.isfile(path) or os.path.islink(path): |
|
|
os.remove(path) |
|
|
elif os.path.isdir(path): |
|
|
shutil.rmtree(path) |
|
|
else: |
|
|
raise ValueError("file {} is not a file or dir.".format(path)) |
|
|
|
|
|
def reorganize_data(output_path: str, shard_size: str = "5"): |
|
|
""" Loads the temporary data folders in the data path and creates 5GB shards for each year, deletes temporary files |
|
|
""" |
|
|
folders = os.listdir(output_path) |
|
|
temp_folders = [i for i in folders if "_t" in i] |
|
|
if len(temp_folders) == 0: |
|
|
print("DATA ORGANIZED") |
|
|
return |
|
|
print("REORGANIZING DATA...") |
|
|
for t_fold in tqdm(temp_folders): |
|
|
|
|
|
data_path = os.path.join(output_path,t_fold) |
|
|
data_set = load_dataset(data_path, split = "train") |
|
|
|
|
|
year_str = t_fold[:-2] |
|
|
new_data_path = os.path.join(output_path,year_str) |
|
|
try: |
|
|
data_set.save_to_disk(new_data_path, max_shard_size="5GB") |
|
|
except BaseException as e: |
|
|
print(f"temporarty_folder: {t_fold} could not be processed") |
|
|
print("Lowering max shard size to 3GB") |
|
|
|
|
|
remove(new_data_path) |
|
|
try: |
|
|
data_set.save_to_disk(new_data_path, max_shard_size="3GB") |
|
|
except BaseException as e: |
|
|
print(f"temporarty_folder: {t_fold} could not be processed") |
|
|
print("Lowering max shard size to 1GB") |
|
|
|
|
|
remove(new_data_path) |
|
|
try: |
|
|
data_set.save_to_disk(new_data_path, max_shard_size="1GB") |
|
|
except BaseException as e: |
|
|
print(f"temporarty_folder: {t_fold} could not be processed") |
|
|
|
|
|
|
|
|
remove(new_data_path) |
|
|
continue |
|
|
|
|
|
try : |
|
|
remove(data_path) |
|
|
except PermissionError as e: |
|
|
print(f"{e}") |
|
|
data_set.cleanup_cache_files() |
|
|
|
|
|
def main(): |
|
|
reorganize_data(output_path) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |