{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import os\n", "import shutil\n", "from sklearn.model_selection import train_test_split\n", "import pandas as pd\n", "import json " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create train-test folder" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "base_path = 'kidney-ct-abnormality'\n", "images_path = os.path.join(base_path, 'imagesTr')\n", "all_images = [f for f in os.listdir(images_path) if f.endswith('.mha')]\n", "train_files, test_files = train_test_split(all_images, test_size=0.2, random_state=219)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "train_dir = os.path.join(base_path, 'train')\n", "test_dir = os.path.join(base_path, 'test')\n", "os.makedirs(train_dir, exist_ok=True)\n", "os.makedirs(test_dir, exist_ok=True)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def move_files(files, destination):\n", " for f in files:\n", " shutil.move(os.path.join(images_path, f), os.path.join(destination, f))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "move_files(train_files, train_dir)\n", "move_files(test_files, test_dir)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Modify the json file" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "json_file_path = 'dataset.json' \n", "with open(json_file_path, 'r') as file:\n", " img_label = json.load(file)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "data = img_label['training']\n", "df = pd.DataFrame(data)\n", "df.rename(columns={'Abnormality': 'abnormality'}, inplace=True)\n", "df['image'] = df['image'].apply(lambda x: x[11:-4]+'_0000'+x[-4:])\n", "test_images = [f for f in os.listdir('test') if f.endswith('.mha')]\n", "df['split'] = df['image'].apply(lambda x: 'test' if x in test_images else 'train')\n", "# df" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# df_copy = df.copy()\n", "# df_copy['count']=1\n", "# df_copy.groupby(['split', 'abnormality']).sum()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "list_of_dicts = df.to_dict(orient='records')\n", "# list_of_dicts" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "df.to_json('dataset_m.json', orient='records', lines=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "interpreter": { "hash": "5dd334af8dc9350ae3498c72a940dd1e233d0cdb387a7b49f9cbba56097a7a15" }, "kernelspec": { "display_name": "Python 3.9.7", "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.9.7" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }