{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":94147,"databundleVersionId":11390004,"sourceType":"competition"}],"dockerImageVersionId":31012,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import h5py\nimport numpy as np\nimport pandas as pd\nfrom sklearn.preprocessing import MinMaxScaler, StandardScaler\nfrom sklearn.linear_model import RANSACRegressor\nfrom sklearn.tree import DecisionTreeRegressor\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.applications import EfficientNetB0\nfrom tensorflow.keras.applications.efficientnet import preprocess_input\n\n# Step 1: Load the .h5 file\nfile_path = '/kaggle/input/el-hackathon-2025/elucidata_ai_challenge_data.h5'\n\nwith h5py.File(file_path, 'r') as f:\n    # Corrected paths based on the actual structure\n    valid_keys = [key for key in ['S_1', 'S_2', 'S_3', 'S_4', 'S_5', 'S_6'] if f'spots/Train/{key}' in f]\n    \n    # Train Spots\n    train_spots = {key: pd.DataFrame(f[f'spots/Train/{key}'][:]) for key in valid_keys}\n    \n    # Train Images\n    train_images = {key: np.array(f[f'images/Train/{key}']) for key in valid_keys}\n    \n    # Test Spots\n    test_spots = pd.DataFrame(f['spots/Test/S_7'][:])\n    \n    # Test Image\n    test_image = np.array(f['images/Test/S_7'])\n\n# Step 2: Normalize Cell Type Abundances\nscaler = MinMaxScaler()\nfor key in train_spots:\n    train_spots[key].iloc[:, 2:] = scaler.fit_transform(train_spots[key].iloc[:, 2:])\n\n# Step 3: Extract Image Features Using Pretrained CNN\nbase_model = EfficientNetB0(weights='imagenet', include_top=False, pooling='avg')\n\ndef extract_image_features(image):\n    image = preprocess_input(image)  # Preprocess for EfficientNet\n    features = base_model.predict(np.expand_dims(image, axis=0))\n    return features.flatten()\n\n# Extract features for all training and test images\ntrain_image_features = {key: extract_image_features(img) for key, img in train_images.items()}\ntest_image_features = extract_image_features(test_image)\n\n# Step 4: Combine Features (Spatial Coordinates + Image Features)\ndef combine_features(spots, image_features):\n    combined_features = []\n    for idx, row in spots.iterrows():\n        x, y = row['x'], row['y']\n        features = np.concatenate(([x, y], image_features))\n        combined_features.append(features)\n    return np.array(combined_features)\n\n# Combine features for training and test data\ntrain_combined_features = {key: combine_features(train_spots[key], train_image_features[key]) for key in train_spots}\ntest_combined_features = combine_features(test_spots, test_image_features)\n\n# Step 5: Prepare Training and Validation Data\nX_train, X_val, y_train, y_val = [], [], [], []\n\n# Shuffle and split data into training and validation sets\nfor key in train_spots:\n    X = train_combined_features[key]\n    y = train_spots[key].iloc[:, 2:].values\n    X_t, X_v, y_t, y_v = train_test_split(X, y, test_size=0.2, random_state=42)\n    X_train.extend(X_t)\n    y_train.extend(y_t)\n    X_val.extend(X_v)\n    y_val.extend(y_v)\n\nX_train, X_val, y_train, y_val = np.array(X_train), np.array(X_val), np.array(y_train), np.array(y_val)\n\n# Step 6: Normalize Features\nscaler_features = StandardScaler()\nX_train = scaler_features.fit_transform(X_train)\nX_val = scaler_features.transform(X_val)\ntest_combined_features = scaler_features.transform(test_combined_features)\n\n# Step 7: Build and Train the RANSAC Regressor Model\nmodel = RANSACRegressor(\n    base_estimator=DecisionTreeRegressor(max_depth=5),\n    min_samples=0.5,\n    residual_threshold=1.0,\n    max_trials=500,\n    random_state=42\n)\n\n# Fit the model\nmodel.fit(X_train, y_train)\n\n# Evaluate the model on the validation set\nval_score = model.score(X_val, y_val)\nprint(f\"Validation R^2 Score: {val_score}\")\n\n# Step 8: Predict on Test Data\npredictions = model.predict(test_combined_features)\n\n# Step 9: Save Predictions to CSV\n# Add ID column and rename columns\nsubmission = pd.DataFrame(predictions, columns=[f'C{i+1}' for i in range(35)])\nsubmission.insert(0, 'ID', test_spots.index)  # Add ID column\nsubmission.to_csv('submission.csv', index=False)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-04-14T01:39:10.295288Z","iopub.execute_input":"2025-04-14T01:39:10.295634Z","iopub.status.idle":"2025-04-14T01:43:52.000506Z","shell.execute_reply.started":"2025-04-14T01:39:10.295610Z","shell.execute_reply":"2025-04-14T01:43:51.998983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}