File size: 3,028 Bytes
1a84bee |
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Starting copy with 48 processes...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Copying files: 100%|ββββββββββ| 7680/7680 [00:12<00:00, 626.66it/s]\n"
]
}
],
"source": [
"import os\n",
"import shutil\n",
"from multiprocessing import Pool\n",
"from pathlib import Path\n",
"from tqdm import tqdm\n",
"\n",
"def copy_file(args):\n",
" src, dst = args\n",
" os.makedirs(os.path.dirname(dst), exist_ok=True)\n",
" shutil.copy2(src, dst)\n",
" return src\n",
"\n",
"def parallel_copy():\n",
" # Source and destination paths\n",
" src_dir = \"/l/users/chaimaa.abi/all_code_thesis/finetune-DM/dataset_upscaled/upscaled_VLCS\"\n",
" dst_dir = \"/l/users/sarim.hashmi/Thesis/ICCV/chimaa_data/upscaled_VLCS\"\n",
" \n",
" # Get all files from source directory\n",
" src_files = []\n",
" dst_files = []\n",
" \n",
" for root, _, files in os.walk(src_dir):\n",
" for file in files:\n",
" src_path = os.path.join(root, file)\n",
" # Create corresponding destination path\n",
" rel_path = os.path.relpath(src_path, src_dir)\n",
" dst_path = os.path.join(dst_dir, rel_path)\n",
" \n",
" src_files.append(src_path)\n",
" dst_files.append(dst_path)\n",
" \n",
" # Create file pairs for mapping\n",
" file_pairs = list(zip(src_files, dst_files))\n",
" \n",
" # Create destination directory\n",
" os.makedirs(dst_dir, exist_ok=True)\n",
" \n",
" # Use Process Pool for parallel copying\n",
" num_processes = os.cpu_count() # Use all available CPU cores\n",
" print(f\"Starting copy with {num_processes} processes...\")\n",
" \n",
" with Pool(num_processes) as pool:\n",
" # Use tqdm to show progress\n",
" for _ in tqdm(pool.imap_unordered(copy_file, file_pairs), \n",
" total=len(file_pairs),\n",
" desc=\"Copying files\"):\n",
" pass\n",
"\n",
"if __name__ == \"__main__\":\n",
" parallel_copy()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "AI702",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|