Switch to unified view

a b/ipynb/split into folders.ipynb
1
{
2
 "cells": [
3
  {
4
   "cell_type": "code",
5
   "execution_count": 4,
6
   "metadata": {},
7
   "outputs": [],
8
   "source": [
9
    "import os\n",
10
    "import shutil\n",
11
    "src = \"/home/serge/database/data/genomes/bacillus/ncbi-genomes-2019-06-25/train/\"\n",
12
    "t = \"/home/serge/database/data/genomes/bacillus/ncbi-genomes-2019-06-25\"\n",
13
    "\n",
14
    "src_files = os.listdir(src)"
15
   ]
16
  },
17
  {
18
   "cell_type": "code",
19
   "execution_count": 5,
20
   "metadata": {},
21
   "outputs": [
22
    {
23
     "data": {
24
      "text/plain": [
25
       "3966"
26
      ]
27
     },
28
     "execution_count": 5,
29
     "metadata": {},
30
     "output_type": "execute_result"
31
    }
32
   ],
33
   "source": [
34
    "len(src_files)\n"
35
   ]
36
  },
37
  {
38
   "cell_type": "code",
39
   "execution_count": 6,
40
   "metadata": {},
41
   "outputs": [],
42
   "source": [
43
    "num=500\n",
44
    "\n",
45
    "for i, file_name in enumerate(src_files):\n",
46
    "    if i % num == 0:\n",
47
    "        dest = f\"{t}/{i}/\"\n",
48
    "        if not os.path.exists(dest):\n",
49
    "            os.mkdir(dest)\n",
50
    "    full_file_name = os.path.join(src, file_name)\n",
51
    "    if os.path.isfile(full_file_name):\n",
52
    "        shutil.copy(full_file_name, dest)"
53
   ]
54
  },
55
  {
56
   "cell_type": "code",
57
   "execution_count": 14,
58
   "metadata": {},
59
   "outputs": [],
60
   "source": [
61
    "from sklearn.model_selection import train_test_split\n",
62
    "train, test = train_test_split(src_files,test_size=0.2, random_state=42)\n",
63
    "portions = {\"train\":train, \"valid\":test}\n",
64
    "\n",
65
    "for part in portions.keys():\n",
66
    "    for file_name in portions[part]:\n",
67
    "        dest = f\"{t}/{part}\"\n",
68
    "        if not os.path.exists(dest):\n",
69
    "            os.mkdir(dest)\n",
70
    "        full_file_name = os.path.join(src, file_name)\n",
71
    "        if os.path.isfile(full_file_name):\n",
72
    "            shutil.copy(full_file_name, dest)"
73
   ]
74
  },
75
  {
76
   "cell_type": "code",
77
   "execution_count": null,
78
   "metadata": {},
79
   "outputs": [],
80
   "source": []
81
  }
82
 ],
83
 "metadata": {
84
  "kernelspec": {
85
   "display_name": "Python [conda env:bio] *",
86
   "language": "python",
87
   "name": "conda-env-bio-py"
88
  },
89
  "language_info": {
90
   "codemirror_mode": {
91
    "name": "ipython",
92
    "version": 3
93
   },
94
   "file_extension": ".py",
95
   "mimetype": "text/x-python",
96
   "name": "python",
97
   "nbconvert_exporter": "python",
98
   "pygments_lexer": "ipython3",
99
   "version": "3.6.8"
100
  }
101
 },
102
 "nbformat": 4,
103
 "nbformat_minor": 2
104
}