I am at the moment trying make a setup script, capable of setting up a workspace up for me, such that I don't need to do it manually. I started doing this in bash, but quickly realized that would not work that well.
My next idea was to do it using python, but can't seem to do it a proper way.. My idea was to make a list (a list being a .txt files with the paths for all the datafiles), shuffle this list, and then move each file to either my train dir or test dir, given the ratio....
But this is python, isn't there a more simpler way to do it, it seems like I am doing an unessesary workaround just to split the files.
Bash Code:
# Partition data randomly into train and test.
cd ${PATH_TO_DATASET}
SPLIT=0.5 #train/test split
NUMBER_OF_FILES=$(ls ${PATH_TO_DATASET} | wc -l) ## number of directories in the dataset
even=1
echo ${NUMBER_OF_FILES}
if [ `echo "${NUMBER_OF_FILES} % 2" | bc` -eq 0 ]
then
even=1
echo "Even is true"
else
even=0
echo "Even is false"
fi
echo -e "${BLUE}Seperating files in to train and test set!${NC}"
for ((i=1; i<=${NUMBER_OF_FILES}; i++))
do
ran=$(python -c "import random;print(random.uniform(0.0, 1.0))")
if [[ ${ran} < ${SPLIT} ]]
then
##echo "test ${ran}"
cp -R $(ls -d */|sed "${i}q;d") ${WORKSPACE_SETUP_ROOT}/../${WORKSPACE}/data/test/
else
##echo "train ${ran}"
cp -R $(ls -d */|sed "${i}q;d") ${WORKSPACE_SETUP_ROOT}/../${WORKSPACE}/data/train/
fi
##echo $(ls -d */|sed "${i}q;d")
done
cd ${WORKSPACE_SETUP_ROOT}/../${WORKSPACE}/data
NUMBER_TRAIN_FILES=$(ls train/ | wc -l)
NUMBER_TEST_FILES=$(ls test/ | wc -l)
echo "${NUMBER_TRAIN_FILES} and ${NUMBER_TEST_FILES}..."
echo $(calc ${NUMBER_TRAIN_FILES}/${NUMBER_OF_FILES})
if [[ ${even} = 1 ]] && [[ ${NUMBER_TRAIN_FILES}/${NUMBER_OF_FILES} != ${SPLIT} ]]
then
echo "Something need to be fixed!"
if [[ $(calc ${NUMBER_TRAIN_FILES}/${NUMBER_OF_FILES}) > ${SPLIT} ]]
then
echo "Too many files in the TRAIN set move some to TEST"
cd train
echo $(pwd)
while [[ ${NUMBER_TRAIN_FILES}/${NUMBER_TEST_FILES} != ${SPLIT} ]]
do
mv $(ls -d */|sed "1q;d") ../test/
echo $(calc ${NUMBER_TRAIN_FILES}/${NUMBER_OF_FILES})
done
else
echo "Too many files in the TEST set move some to TRAIN"
cd test
while [[ ${NUMBER_TRAIN_FILES}/${NUMBER_TEST_FILES} != ${SPLIT} ]]
do
mv $(ls -d */|sed "1q;d") ../train/
echo $(calc ${NUMBER_TRAIN_FILES}/${NUMBER_OF_FILES})
done
fi
fi
My problem were the last part. Since i picking the numbers by random, I would not be sure that the data would be partitioned as hoped, which my last if statement were to check whether the partition was done right, and if not then fix it.. This was not possible since i am checking floating points, and the solution in general became more like a quick fix.