{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":111512,"databundleVersionId":13502500,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nfrom skimage.metrics import peak_signal_noise_ratio, structural_similarity\n\ndef SAM(gt, pr, eps=1e-8):\n    dot = np.sum(gt * pr, axis=-1)\n    norm_gt = np.linalg.norm(gt, axis=-1)\n    norm_pr = np.linalg.norm(pr, axis=-1)\n    cos_theta = np.clip(dot / (norm_gt * norm_pr + eps), -1, 1)\n    return np.mean(np.degrees(np.arccos(cos_theta)))\n\ndef SID(gt, pr, eps=1e-8):\n    gt_p = gt / (np.sum(gt, axis=-1, keepdims=True) + eps)\n    pr_p = pr / (np.sum(pr, axis=-1, keepdims=True) + eps)\n    return np.mean(np.sum(gt_p * np.log((gt_p + eps) / (pr_p + eps)) +\n                          pr_p * np.log((pr_p + eps) / (gt_p + eps)), axis=-1))\n\ndef ERGAS(gt, pr, ratio=1.0, eps=1e-8):\n    bands = gt.shape[-1]\n    mean_gt = np.mean(gt, axis=(0,1))\n    rmse = np.sqrt(np.mean((gt - pr)**2, axis=(0,1)))\n    return 100/ratio * np.sqrt(np.mean((rmse / (mean_gt+eps))**2))\n\ndef evaluate_pair_ssc(gt_cube, pr_cube, wl_nm=None):\n    \"\"\"\n    gt_cube, pr_cube: (H, W, C) numpy arrays\n    wl_nm: wavelength array (optional, not used in these metrics)\n    \"\"\"\n    scores = {}\n\n    # Spectral metrics\n    scores[\"SAM_deg\"] = SAM(gt_cube, pr_cube)\n    scores[\"SID\"]     = SID(gt_cube, pr_cube)\n    scores[\"ERGAS\"]   = ERGAS(gt_cube, pr_cube)\n\n    gt_rgb = np.mean(gt_cube, axis=-1) \n    pr_rgb = np.mean(pr_cube, axis=-1)\n\n    scores[\"PSNR_dB\"] = peak_signal_noise_ratio(gt_rgb, pr_rgb, data_range=gt_rgb.max() - gt_rgb.min())\n    scores[\"SSIM\"]    = structural_similarity(gt_rgb, pr_rgb, data_range=gt_rgb.max() - gt_rgb.min())\n\n    scores[\"S_SAM\"]   = scores[\"SAM_deg\"]\n    scores[\"S_SID\"]   = scores[\"SID\"]\n    scores[\"S_ERGAS\"] = scores[\"ERGAS\"]\n    scores[\"S_PSNR\"]  = scores[\"PSNR_dB\"]\n    scores[\"S_SSIM\"]  = scores[\"SSIM\"] if \"SSIM\" in scores else 0.0\n\n    # Dummy placeholders (replace with proper definitions if available)\n    scores[\"S_SPEC\"]  = scores[\"SAM_deg\"]\n    scores[\"S_SPAT\"]  = scores[\"SSIM\"]\n    scores[\"S_COLOR\"] = scores[\"PSNR_dB\"]\n\n    # Final SSC = average of sub-scores (toy version)\n    scores[\"SSC\"] = np.mean([\n        scores[\"S_SAM\"],\n        scores[\"S_SID\"],\n        scores[\"S_ERGAS\"],\n        scores[\"S_PSNR\"],\n        scores[\"S_SSIM\"],\n    ])\n\n    return scores","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"save this in utils folder","metadata":{}}]}