<jupyter_start><jupyter_text>WNixalo –– 2018/6/1-2<jupyter_code>from pathlib import Path
from shutil import rmtree, copyfile
import os
import sys
import numpy as np
def count_files(path, fullpath=Path(), count=0):
    """Counts all files in a directory recursively."""
    fullpath /= path
    # check root exists
    if not os.path.exists(fullpath):
        print('Directory does not exist.')
        return
    dirs = os.listdir(fullpath)
    for direc in dirs:
        if (fullpath/direc).is_dir():
            count += count_files(direc, fullpath)
        else:
            count += 1
    return count
path = Path('data/cifar/')
Path(str(path) + '_tmp')
def create_cifar_subset(path, fullpath=Path(), copypath='', p=0.1, copydirs=['train','valid','test']):
    if not copypath:
        copypath = Path(str(path) + '_tmp')
        if os.path.exists(copypath): rmtree(copypath)
    else:
        copypath/=path
    fullpath /= path
    copies = []
    dirs = os.listdir(fullpath)
    for f in dirs:
        if (fullpath/f).is_dir() and (copydirs==[] or f in copydirs):
            os.makedirs(copypath/f)
            create_cifar_subset(f, fullpath, copypath, copydirs=[])
        else:
            copies.append(f)
    if copies:
        copies = np.random.choice(copies, max(1, int(len(copies)*p)), replace=False)
        for copy in copies:
            copyfile(fullpath/copy, copypath/copy)
    
    return copypath
PATH = Path('data/cifar10/')
alt_PATH = create_cifar_subset(PATH)
count_files(PATH), count_files(alt_PATH)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12901155327342748, 'fraction_numerical': 0.008985879332477536, 'mean_word_length': 2.936868686868687, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '574414', 'license': 'MIT', 'n_tokens_mistral': 552, 'n_tokens_neox': 534, 'n_words': 125, 'path': 'research/cifar-utils-tests.ipynb', 'repo_name': 'WNoxchi/Kawkasos', 'size': '1558'}