{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"}],"dockerImageVersionId":30761,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport glob\nfrom pydicom import dcmread\nfrom PIL import Image\nimport pydicom\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Dense,Flatten,MaxPooling2D,Conv2D,Dropout\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.losses import BinaryCrossentropy\nfrom tensorflow.keras.regularizers import l2\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2024-09-18T03:01:49.044639Z","iopub.execute_input":"2024-09-18T03:01:49.045048Z","iopub.status.idle":"2024-09-18T03:01:58.482402Z","shell.execute_reply.started":"2024-09-18T03:01:49.045011Z","shell.execute_reply":"2024-09-18T03:01:58.481257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = '/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification'\ndata1 = os.path.join(files,'train.csv')\ndata2 = os.path.join(files,'train_label_coordinates.csv')\ndata3 = os.path.join(files,'train_series_descriptions.csv')\ntest = os.path.join(files,'test_series_descriptions.csv')\ntest_data = pd.read_csv(test)\ndata3 = pd.read_csv(data3)\ndata2 = pd.read_csv(data2)\ndata1 = pd.read_csv(data1)","metadata":{"execution":{"iopub.status.busy":"2024-09-18T03:01:58.484346Z","iopub.execute_input":"2024-09-18T03:01:58.484975Z","iopub.status.idle":"2024-09-18T03:01:58.663965Z","shell.execute_reply.started":"2024-09-18T03:01:58.484934Z","shell.execute_reply":"2024-09-18T03:01:58.662589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"i will create a for loop that will hold the values of study_id and after that match the id to the coordinates study_id after finding it another for loop will start to find the unique values and give them index (enumerate) after that the reaplacement from l1 to s1 will begin \nsomething like this\nfor x in data['study_id']:\n    for y in data_2['study_id']:\n        if x==y:\n        for indexs,values in enumerate(data_2['study_id']):\n            for index,value in enumerate(data['study_id']):\n                if indexs[1] == index[1]\n                x_answers = x_answers.replace(values,value) ","metadata":{}},{"cell_type":"code","source":"data2['conditions'] = None\nfor index,row in data2.iterrows():\n    study_id = row['study_id']\n    condition=row['condition']\n    levels = row['level']\n    levels = levels.lower()\n    levels = levels.replace('/','_')\n    data1_row = data1[data1['study_id']==study_id]\n    condition_column = condition + '_' + levels\n    condition_column = condition_column.lower()\n    condition_column = condition_column.replace(' ','_')\n    #print (f'theis is the condition column {condition_column}')\n    if not data1_row.empty and condition_column in data1.columns:\n        condition_value = data1_row[condition_column].values[0]\n        data2.loc[index,'conditions'] = condition_value\n    \n    \ndata2 = data2.dropna() #35 total null drop them\ndata2 = data2.reset_index(drop=True)\ndata2 = data2.drop(columns='level')\n            ","metadata":{"execution":{"iopub.status.busy":"2024-09-18T03:01:58.665501Z","iopub.execute_input":"2024-09-18T03:01:58.665899Z","iopub.status.idle":"2024-09-18T03:02:32.705950Z","shell.execute_reply.started":"2024-09-18T03:01:58.665859Z","shell.execute_reply":"2024-09-18T03:02:32.704917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_paths_list = []\ntrain_data = []\ndata2[['study_id','series_id']] = data2[['study_id','series_id']].astype(str)\nfor index,row in data2.iterrows():\n    study_id = row['study_id']\n    series = row['series_id']\n    condition = row['condition']\n    file_path = os.path.join(files,'train_images',study_id,series)\n    file_paths = glob.glob(os.path.join(file_path,'*.dcm'))\n    for filing in file_paths:\n        train_data.append({\n            'study_id': study_id,\n            'series_id': series,\n            'condition': condition,\n            'file_path': filing\n        })\n        \ntrain_data = pd.DataFrame(train_data)\n    ","metadata":{"execution":{"iopub.status.busy":"2024-09-18T03:02:32.708759Z","iopub.execute_input":"2024-09-18T03:02:32.709364Z","iopub.status.idle":"2024-09-18T03:03:27.316719Z","shell.execute_reply.started":"2024-09-18T03:02:32.709305Z","shell.execute_reply":"2024-09-18T03:03:27.315432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"check = train_data.loc[2,'file_path']\ncheck","metadata":{"execution":{"iopub.status.busy":"2024-09-18T03:14:21.561768Z","iopub.execute_input":"2024-09-18T03:14:21.562264Z","iopub.status.idle":"2024-09-18T03:14:21.571278Z","shell.execute_reply.started":"2024-09-18T03:14:21.562217Z","shell.execute_reply":"2024-09-18T03:14:21.570033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data","metadata":{"execution":{"iopub.status.busy":"2024-09-18T03:03:27.318623Z","iopub.execute_input":"2024-09-18T03:03:27.319013Z","iopub.status.idle":"2024-09-18T03:03:27.340042Z","shell.execute_reply.started":"2024-09-18T03:03:27.318973Z","shell.execute_reply":"2024-09-18T03:03:27.338725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport pydicom\nfrom tensorflow import convert_to_tensor\n\n# Assuming training_data is a pandas DataFrame with a column 'file_path'\n# containing paths to the DICOM image files\n\n# Function to convert DICOM image to array\ndef dcm_to_array(file_path):\n    try:\n        # Read the DICOM file\n        dcm = pydicom.dcmread(file_path)\n        img_array = dcm.pixel_array  # Extract the image data as a NumPy array\n        img_array = img_array / np.max(img_array)  # Normalize the image (optional)\n        img_array = np.stack((img_array,) * 3, axis=-1)  # Convert to 3-channel if needed (for color images)\n        return img_array\n    except Exception as e:\n        print(f\"Error processing DICOM image {file_path}: {e}\")\n        return None\n\n# Apply the function to convert DICOM images to arrays\ntrain_data['array'] = train_data['file_path'].apply(dcm_to_array)\n\n# If you want to convert the NumPy arrays to tensors (optional)\ntrain_data['array'] = train_data['array'].apply(lambda x: convert_to_tensor(x, dtype='float32') if x is not None else None)\n\n# Now the training_data DataFrame has a new 'array' column with image data as arrays/tensors\n","metadata":{"execution":{"iopub.status.busy":"2024-09-18T03:10:06.363007Z","iopub.execute_input":"2024-09-18T03:10:06.363453Z","iopub.status.idle":"2024-09-18T03:10:06.708574Z","shell.execute_reply.started":"2024-09-18T03:10:06.363415Z","shell.execute_reply":"2024-09-18T03:10:06.707393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([tf.keras.layers.Input(shape=(640, 640, 1)),\n                    Conv2D(16,(3,3),activation='relu',kernel_regularizer=l2(0.01)),\n                   MaxPooling2D(2,2),\n                   Dropout(0.02),\n                   Conv2D(32,(3,3),activation='relu',kernel_regularizer=l2(0.01)),\n                   MaxPooling2D(2,2),\n                   Dropout(0.02),\n                   Conv2D(64,(3,3),activation='relu',kernel_regularizer=l2(0.01)),\n                   MaxPooling2D(2,2),\n                   Dropout(0.02),\n                   Conv2D(128,(3,3),activation='relu',kernel_regularizer=l2(0.01)),\n                   MaxPooling2D(2,2),\n                   Dropout(0.02),\n                   Conv2D(256,(3,3),activation='relu',kernel_regularizer=l2(0.01)),\n                   MaxPooling2D(2,2),\n                   Dropout(0.02),\n                   Conv2D(512,(3,3),activation='relu',kernel_regularizer=l2(0.01)),\n                   MaxPooling2D(2,2),\n                   Dropout(0.02),\n                   Flatten(),\n                   Dense(1024,activation='relu',kernel_regularizer=l2(0.01)),\n                   Dropout(0.3),\n                   Dense(5,activation='softmax')])","metadata":{"execution":{"iopub.status.busy":"2024-09-18T03:07:24.600121Z","iopub.execute_input":"2024-09-18T03:07:24.601000Z","iopub.status.idle":"2024-09-18T03:07:24.936723Z","shell.execute_reply.started":"2024-09-18T03:07:24.600949Z","shell.execute_reply":"2024-09-18T03:07:24.935423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=Adam(learning_rate=0.001), \n              loss='categorical_crossentropy', \n              metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2024-09-18T03:07:26.318273Z","iopub.execute_input":"2024-09-18T03:07:26.318704Z","iopub.status.idle":"2024-09-18T03:07:26.330974Z","shell.execute_reply.started":"2024-09-18T03:07:26.318664Z","shell.execute_reply":"2024-09-18T03:07:26.329851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-09-18T03:07:26.922217Z","iopub.execute_input":"2024-09-18T03:07:26.923275Z","iopub.status.idle":"2024-09-18T03:07:26.964289Z","shell.execute_reply.started":"2024-09-18T03:07:26.923227Z","shell.execute_reply":"2024-09-18T03:07:26.963294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(training_data,epochs=4)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}