# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda root]
#     language: python
#     name: conda-root-py
# ---

import os
import numpy as np
from shutil import copyfile
KG_PATH = '/home/ubuntu/kgcatsdogsr/'
DST_PATH = '/home/ubuntu/myfastai/nbs/data/dogscats/'


def check_and_make_dir(path):
    if not os.path.exists(path):
        os.makedirs(path)


def setup_train(classes, spath=KG_PATH+'train/', dpath=DST_PATH+'train/'):
    for c in classes:
        fdir = dpath+c+'s/'
        check_and_make_dir(fdir)
        clen = len(c)
        
        for f in os.listdir(spath):
            if f.startswith(c):
                copyfile(spath+f,fdir+f)
    


def setup_test(spath=KG_PATH+'test/', dpath=DST_PATH+'test/'):
    fdir = dpath+'unknown/'
    check_and_make_dir(fdir)
    
    for f in os.listdir(spath):
        copyfile(spath+f,dpath+f)


def sample_files(spath, num):
    return np.random.choice(os.listdir(spath), num, replace=False).tolist()


def setup_valid(classes, path=DST_PATH, num=1000):
    for c in classes:
        spath = path+'train/'+c+'s/'
        dpath = path+'valid/'+c+'s/'
        check_and_make_dir(dpath)
        
        for f in sample_files(spath, num):
            os.rename(spath+f,dpath+f)    


def setup_sample(classes, path=DST_PATH, numt=80, numv=20):
    for c in classes:
        for d, n in [('train/', numt), ('valid/', numv)]:
            spath = path+d+c+'s/'
            dpath = path+'sample/'+d+c+'s/'
            check_and_make_dir(dpath)
            
            for f in sample_files(spath, n):
                copyfile(spath+f,dpath+f)


def setup_data(classes):
    setup_train(classes)
    setup_test()
    setup_valid(classes)
    setup_sample(classes)



classes = ['cat', 'dog']
setup_data(classes)

StarcoderdataJupyterScriptsDedupFiltered