Datasets:
File size: 1,023 Bytes
fc10d73 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
from npy_append_array import NpyAppendArray
import sys
import os
import numpy as np
from tqdm import tqdm
import random
in_dir = sys.argv[1]
out_file = sys.argv[2]
out_len_file = sys.argv[3]
percentage = float(sys.argv[4])
out_len_file = open(out_len_file, 'a')
#with NpyAppendArray(out_file, delete_if_exists=True) as npaa:
cnt = 0
with NpyAppendArray(out_file, delete_if_exists=True) as npaa:
for file in tqdm(os.listdir(in_dir)):
try:
#if percentage > 0 and random.random() > percentage:
# continue
arr = np.load(os.path.join(in_dir, file))
if percentage > 0:
indices = np.random.choice(arr.shape[0], int(arr.shape[0] * percentage), replace=False)
arr = arr[indices]
npaa.append(arr)
out_len_file.write(f"{str(arr.shape[0])}\n")
cnt += 1
except:
continue
data = np.load(out_file, mmap_mode="r")
print(data.shape)
print(len(os.listdir(in_dir)), cnt)
|