{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":70367,"databundleVersionId":9188054,"sourceType":"competition"},{"sourceId":3729,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":2656,"modelId":312}],"dockerImageVersionId":30746,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\ntrain = pd.read_csv(\"/kaggle/input/ariel-data-challenge-2024/train_labels.csv\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-03T13:50:43.77235Z","iopub.execute_input":"2024-08-03T13:50:43.772826Z","iopub.status.idle":"2024-08-03T13:50:43.834847Z","shell.execute_reply.started":"2024-08-03T13:50:43.772785Z","shell.execute_reply":"2024-08-03T13:50:43.833741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:43.837365Z","iopub.execute_input":"2024-08-03T13:50:43.8378Z","iopub.status.idle":"2024-08-03T13:50:44.306511Z","shell.execute_reply.started":"2024-08-03T13:50:43.837761Z","shell.execute_reply":"2024-08-03T13:50:44.305333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv(\"/kaggle/input/ariel-data-challenge-2024/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:44.308Z","iopub.execute_input":"2024-08-03T13:50:44.3084Z","iopub.status.idle":"2024-08-03T13:50:44.331882Z","shell.execute_reply.started":"2024-08-03T13:50:44.308362Z","shell.execute_reply":"2024-08-03T13:50:44.33085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:44.333151Z","iopub.execute_input":"2024-08-03T13:50:44.333539Z","iopub.status.idle":"2024-08-03T13:50:44.358594Z","shell.execute_reply.started":"2024-08-03T13:50:44.333505Z","shell.execute_reply":"2024-08-03T13:50:44.357207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_columns = [col for col in train.columns if 'wl_' in col]\nlen(target_columns)","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:44.361698Z","iopub.execute_input":"2024-08-03T13:50:44.362153Z","iopub.status.idle":"2024-08-03T13:50:44.371542Z","shell.execute_reply.started":"2024-08-03T13:50:44.362122Z","shell.execute_reply":"2024-08-03T13:50:44.370377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wl_mean = train[target_columns].mean()\nwl_std = train[target_columns].std()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:44.372917Z","iopub.execute_input":"2024-08-03T13:50:44.373282Z","iopub.status.idle":"2024-08-03T13:50:44.39487Z","shell.execute_reply.started":"2024-08-03T13:50:44.373243Z","shell.execute_reply":"2024-08-03T13:50:44.39333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub[target_columns] = wl_mean.values\nsub[[x.replace('wl_','sigma_') for x in target_columns]] = wl_std.values","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:44.396591Z","iopub.execute_input":"2024-08-03T13:50:44.397176Z","iopub.status.idle":"2024-08-03T13:50:44.469606Z","shell.execute_reply.started":"2024-08-03T13:50:44.397131Z","shell.execute_reply":"2024-08-03T13:50:44.468435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:44.4711Z","iopub.execute_input":"2024-08-03T13:50:44.471503Z","iopub.status.idle":"2024-08-03T13:50:44.500293Z","shell.execute_reply.started":"2024-08-03T13:50:44.471466Z","shell.execute_reply":"2024-08-03T13:50:44.499156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:44.501883Z","iopub.execute_input":"2024-08-03T13:50:44.50267Z","iopub.status.idle":"2024-08-03T13:50:44.525712Z","shell.execute_reply.started":"2024-08-03T13:50:44.50263Z","shell.execute_reply":"2024-08-03T13:50:44.524765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\ntrain = pd.read_csv(\"/kaggle/input/ariel-data-challenge-2024/train_labels.csv\")\ncv_index = random.sample(range(0,len(train)), int(len(train)*0.4))\ndev_index = list(set(range(0,len(train))) - set(cv_index))","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:44.527124Z","iopub.execute_input":"2024-08-03T13:50:44.527511Z","iopub.status.idle":"2024-08-03T13:50:44.587103Z","shell.execute_reply.started":"2024-08-03T13:50:44.527477Z","shell.execute_reply":"2024-08-03T13:50:44.585925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dev = train.iloc[dev_index].copy().reset_index(drop=True)\ncv = train.iloc[cv_index].copy().reset_index(drop=True)\ncv_sub = cv.copy()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:44.588353Z","iopub.execute_input":"2024-08-03T13:50:44.588663Z","iopub.status.idle":"2024-08-03T13:50:44.597072Z","shell.execute_reply.started":"2024-08-03T13:50:44.588636Z","shell.execute_reply":"2024-08-03T13:50:44.596102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dev","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:44.598418Z","iopub.execute_input":"2024-08-03T13:50:44.598738Z","iopub.status.idle":"2024-08-03T13:50:44.629643Z","shell.execute_reply.started":"2024-08-03T13:50:44.598709Z","shell.execute_reply":"2024-08-03T13:50:44.628528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:44.630856Z","iopub.execute_input":"2024-08-03T13:50:44.631237Z","iopub.status.idle":"2024-08-03T13:50:44.661134Z","shell.execute_reply.started":"2024-08-03T13:50:44.631204Z","shell.execute_reply":"2024-08-03T13:50:44.660008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dev_wl_mean = dev[target_columns].mean()\ndev_wl_std = dev[target_columns].std()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:44.664592Z","iopub.execute_input":"2024-08-03T13:50:44.664944Z","iopub.status.idle":"2024-08-03T13:50:44.675989Z","shell.execute_reply.started":"2024-08-03T13:50:44.6649Z","shell.execute_reply":"2024-08-03T13:50:44.674751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv_sub[target_columns] = dev_wl_mean.values","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:44.677303Z","iopub.execute_input":"2024-08-03T13:50:44.677626Z","iopub.status.idle":"2024-08-03T13:50:44.712409Z","shell.execute_reply.started":"2024-08-03T13:50:44.6776Z","shell.execute_reply":"2024-08-03T13:50:44.711346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sigma_target_columns = [x.replace('wl_','sigma_') for x in target_columns]\ncv_sub[sigma_target_columns] = dev_wl_std.std()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:44.713694Z","iopub.execute_input":"2024-08-03T13:50:44.71402Z","iopub.status.idle":"2024-08-03T13:50:44.858352Z","shell.execute_reply.started":"2024-08-03T13:50:44.713993Z","shell.execute_reply":"2024-08-03T13:50:44.857052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport pandas.api.types\nimport scipy.stats\n\n\nclass ParticipantVisibleError(Exception):\n    pass\n\n\ndef score(\n        solution: pd.DataFrame,\n        submission: pd.DataFrame,\n        row_id_column_name: str,\n        naive_mean: float,\n        naive_sigma: float,\n        sigma_true: float\n    ) -> float:\n    '''\n    This is a Gaussian Log Likelihood based metric. For a submission, which contains the predicted mean (x_hat) and variance (x_hat_std),\n    we calculate the Gaussian Log-likelihood (GLL) value to the provided ground truth (x). We treat each pair of x_hat,\n    x_hat_std as a 1D gaussian, meaning there will be 283 1D gaussian distributions, hence 283 values for each test spectrum,\n    the GLL value for one spectrum is the sum of all of them.\n\n    Inputs:\n        - solution: Ground Truth spectra (from test set)\n            - shape: (nsamples, n_wavelengths)\n        - submission: Predicted spectra and errors (from participants)\n            - shape: (nsamples, n_wavelengths*2)\n        naive_mean: (float) mean from the train set.\n        naive_sigma: (float) standard deviation from the train set.\n        sigma_true: (float) essentially sets the scale of the outputs.\n    '''\n\n    if row_id_column_name in solution:\n        del solution[row_id_column_name]\n        del submission[row_id_column_name]\n\n    if submission.min().min() < 0:\n        raise ParticipantVisibleError('Negative values in the submission')\n    for col in submission.columns:\n        if not pandas.api.types.is_numeric_dtype(submission[col]):\n            raise ParticipantVisibleError(f'Submission column {col} must be a number')\n\n    n_wavelengths = len(solution.columns)\n    if len(submission.columns) != n_wavelengths*2:\n        raise ParticipantVisibleError('Wrong number of columns in the submission')\n\n    y_pred = submission.iloc[:, :n_wavelengths].values\n    # Set a non-zero minimum sigma pred to prevent division by zero errors.\n    sigma_pred = np.clip(submission.iloc[:, n_wavelengths:].values, a_min=10**-15, a_max=None)\n    y_true = solution.values\n\n    GLL_pred = np.sum(scipy.stats.norm.logpdf(y_true, loc=y_pred, scale=sigma_pred))\n    GLL_true = np.sum(scipy.stats.norm.logpdf(y_true, loc=y_true, scale=sigma_true * np.ones_like(y_true)))\n    GLL_mean = np.sum(scipy.stats.norm.logpdf(y_true, loc=naive_mean * np.ones_like(y_true), scale=naive_sigma * np.ones_like(y_true)))\n\n    submit_score = (GLL_pred - GLL_mean)/(GLL_true - GLL_mean)\n    return float(np.clip(submit_score, 0.0, 1.0))","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:44.860139Z","iopub.execute_input":"2024-08-03T13:50:44.860472Z","iopub.status.idle":"2024-08-03T13:50:45.331513Z","shell.execute_reply.started":"2024-08-03T13:50:44.860441Z","shell.execute_reply":"2024-08-03T13:50:45.330408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_wavelengths = len(cv.columns)\nn_wavelengths","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:45.33272Z","iopub.execute_input":"2024-08-03T13:50:45.333046Z","iopub.status.idle":"2024-08-03T13:50:45.339499Z","shell.execute_reply.started":"2024-08-03T13:50:45.33302Z","shell.execute_reply":"2024-08-03T13:50:45.338347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dev.shape, cv.shape, cv_sub.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:45.341079Z","iopub.execute_input":"2024-08-03T13:50:45.341535Z","iopub.status.idle":"2024-08-03T13:50:45.351828Z","shell.execute_reply.started":"2024-08-03T13:50:45.341494Z","shell.execute_reply":"2024-08-03T13:50:45.350623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:45.35324Z","iopub.execute_input":"2024-08-03T13:50:45.353636Z","iopub.status.idle":"2024-08-03T13:50:45.386426Z","shell.execute_reply.started":"2024-08-03T13:50:45.353598Z","shell.execute_reply":"2024-08-03T13:50:45.385348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv_sub","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:45.387879Z","iopub.execute_input":"2024-08-03T13:50:45.388227Z","iopub.status.idle":"2024-08-03T13:50:45.422491Z","shell.execute_reply.started":"2024-08-03T13:50:45.388198Z","shell.execute_reply":"2024-08-03T13:50:45.421413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score_cv = cv.copy()\nscore_cv_sub = cv_sub.copy()\nscore(score_cv, score_cv_sub ,'planet_id', dev_wl_mean.mean(), dev_wl_std.std(), dev_wl_std.std())","metadata":{"execution":{"iopub.status.busy":"2024-08-03T13:50:45.423988Z","iopub.execute_input":"2024-08-03T13:50:45.424666Z","iopub.status.idle":"2024-08-03T13:50:45.487014Z","shell.execute_reply.started":"2024-08-03T13:50:45.424627Z","shell.execute_reply":"2024-08-03T13:50:45.485982Z"},"trusted":true},"execution_count":null,"outputs":[]}]}