{"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":"none","dataSources":[{"sourceId":70367,"databundleVersionId":9188054,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Mean Submisison","metadata":{}},{"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":{"execution":{"iopub.status.busy":"2024-08-03T11:15:29.136628Z","iopub.execute_input":"2024-08-03T11:15:29.137062Z","iopub.status.idle":"2024-08-03T11:15:29.76431Z","shell.execute_reply.started":"2024-08-03T11:15:29.137029Z","shell.execute_reply":"2024-08-03T11:15:29.762986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2024-08-03T11:15:30.28178Z","iopub.execute_input":"2024-08-03T11:15:30.2822Z","iopub.status.idle":"2024-08-03T11:15:30.335772Z","shell.execute_reply.started":"2024-08-03T11:15:30.282169Z","shell.execute_reply":"2024-08-03T11:15:30.33433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T11:15:31.19351Z","iopub.execute_input":"2024-08-03T11:15:31.193916Z","iopub.status.idle":"2024-08-03T11:15:31.773209Z","shell.execute_reply.started":"2024-08-03T11:15:31.193887Z","shell.execute_reply":"2024-08-03T11:15:31.771871Z"},"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-03T11:15:31.930228Z","iopub.execute_input":"2024-08-03T11:15:31.930641Z","iopub.status.idle":"2024-08-03T11:15:31.964712Z","shell.execute_reply.started":"2024-08-03T11:15:31.930609Z","shell.execute_reply":"2024-08-03T11:15:31.963329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2024-08-03T11:15:42.675786Z","iopub.execute_input":"2024-08-03T11:15:42.677113Z","iopub.status.idle":"2024-08-03T11:15:42.703281Z","shell.execute_reply.started":"2024-08-03T11:15:42.677076Z","shell.execute_reply":"2024-08-03T11:15:42.701686Z"},"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-03T11:15:43.079308Z","iopub.execute_input":"2024-08-03T11:15:43.079724Z","iopub.status.idle":"2024-08-03T11:15:43.088671Z","shell.execute_reply.started":"2024-08-03T11:15:43.079692Z","shell.execute_reply":"2024-08-03T11:15:43.087199Z"},"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-03T11:15:43.661666Z","iopub.execute_input":"2024-08-03T11:15:43.663054Z","iopub.status.idle":"2024-08-03T11:15:43.682547Z","shell.execute_reply.started":"2024-08-03T11:15:43.663009Z","shell.execute_reply":"2024-08-03T11:15:43.681109Z"},"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-03T11:15:44.337105Z","iopub.execute_input":"2024-08-03T11:15:44.337613Z","iopub.status.idle":"2024-08-03T11:15:44.411781Z","shell.execute_reply.started":"2024-08-03T11:15:44.337576Z","shell.execute_reply":"2024-08-03T11:15:44.41027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"execution":{"iopub.status.busy":"2024-08-03T11:15:44.959819Z","iopub.execute_input":"2024-08-03T11:15:44.961442Z","iopub.status.idle":"2024-08-03T11:15:44.993215Z","shell.execute_reply.started":"2024-08-03T11:15:44.961339Z","shell.execute_reply":"2024-08-03T11:15:44.99164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-08-03T11:15:45.535308Z","iopub.execute_input":"2024-08-03T11:15:45.535815Z","iopub.status.idle":"2024-08-03T11:15:45.581413Z","shell.execute_reply.started":"2024-08-03T11:15:45.535749Z","shell.execute_reply":"2024-08-03T11:15:45.579216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Local CV","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfig, ax = plt.subplots(figsize=(20, 1))\nax.barh('Split', 200 , color='blue')\nax.barh('Split', 467, left=200, color='grey')\nax.barh('Split', 333, left=200 + 467, color='red')\nax.text(100, 'Split', f'Train (200 - 20%)', ha='center', va='center', color='white')\nax.text(200+467/2, 'Split', f'Train & Test (467 - 46.7%)', ha='center', va='center', color='white')\nax.text(200+467+333/2, 'Split', f'Test (333 - 33.3%)', ha='center', va='center', color='white')\nax.spines[['top', 'right', 'left', 'bottom']].set_visible(False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T11:20:50.32151Z","iopub.execute_input":"2024-08-03T11:20:50.321975Z","iopub.status.idle":"2024-08-03T11:20:50.525009Z","shell.execute_reply.started":"2024-08-03T11:20:50.321944Z","shell.execute_reply":"2024-08-03T11:20:50.523693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dev_len, comm_len, cv_len = int(657*0.2), int(657*0.467), int(657*0.333)\ncv_len = (657) - dev_len - comm_len\ndev_len, comm_len, cv_len","metadata":{"execution":{"iopub.status.busy":"2024-08-03T11:21:48.544927Z","iopub.execute_input":"2024-08-03T11:21:48.545877Z","iopub.status.idle":"2024-08-03T11:21:48.555574Z","shell.execute_reply.started":"2024-08-03T11:21:48.545839Z","shell.execute_reply":"2024-08-03T11:21:48.55405Z"},"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\")\ndev_index = random.sample(range(0,len(train)), dev_len)\nrem_index = list(set(range(0,len(train))) - set(dev_index))\ncomm_index = random.sample(rem_index, int(len(train)*0.4))\ncv_index = list(set(range(0,len(train))) - set(dev_index)-set(comm_index))\nlen(dev_index), len(rem_index), len(cv_index)","metadata":{"execution":{"iopub.status.busy":"2024-08-03T11:23:48.10718Z","iopub.execute_input":"2024-08-03T11:23:48.107645Z","iopub.status.idle":"2024-08-03T11:23:48.192964Z","shell.execute_reply.started":"2024-08-03T11:23:48.107609Z","shell.execute_reply":"2024-08-03T11:23:48.19162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dev = train.iloc[dev_index+comm_index].copy().reset_index(drop=True)\ncv = train.iloc[cv_index+comm_index].copy().reset_index(drop=True)\ncv_sub = cv.copy()","metadata":{"execution":{"iopub.status.busy":"2024-08-03T11:24:18.046581Z","iopub.execute_input":"2024-08-03T11:24:18.047007Z","iopub.status.idle":"2024-08-03T11:24:18.059188Z","shell.execute_reply.started":"2024-08-03T11:24:18.046978Z","shell.execute_reply":"2024-08-03T11:24:18.057635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dev","metadata":{"execution":{"iopub.status.busy":"2024-08-03T11:24:20.694869Z","iopub.execute_input":"2024-08-03T11:24:20.696119Z","iopub.status.idle":"2024-08-03T11:24:20.733276Z","shell.execute_reply.started":"2024-08-03T11:24:20.696079Z","shell.execute_reply":"2024-08-03T11:24:20.731952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv","metadata":{"execution":{"iopub.status.busy":"2024-08-03T11:24:22.69614Z","iopub.execute_input":"2024-08-03T11:24:22.69666Z","iopub.status.idle":"2024-08-03T11:24:22.733776Z","shell.execute_reply.started":"2024-08-03T11:24:22.696624Z","shell.execute_reply":"2024-08-03T11:24:22.732374Z"},"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-03T11:24:29.971781Z","iopub.execute_input":"2024-08-03T11:24:29.972202Z","iopub.status.idle":"2024-08-03T11:24:29.989049Z","shell.execute_reply.started":"2024-08-03T11:24:29.972167Z","shell.execute_reply":"2024-08-03T11:24:29.987471Z"},"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-03T11:24:32.926696Z","iopub.execute_input":"2024-08-03T11:24:32.92712Z","iopub.status.idle":"2024-08-03T11:24:32.975022Z","shell.execute_reply.started":"2024-08-03T11:24:32.927091Z","shell.execute_reply":"2024-08-03T11:24:32.973848Z"},"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-03T11:24:34.179368Z","iopub.execute_input":"2024-08-03T11:24:34.179811Z","iopub.status.idle":"2024-08-03T11:24:34.353736Z","shell.execute_reply.started":"2024-08-03T11:24:34.179777Z","shell.execute_reply":"2024-08-03T11:24:34.352559Z"},"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-03T11:24:35.335442Z","iopub.execute_input":"2024-08-03T11:24:35.336056Z","iopub.status.idle":"2024-08-03T11:24:35.907024Z","shell.execute_reply.started":"2024-08-03T11:24:35.336011Z","shell.execute_reply":"2024-08-03T11:24:35.905779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_wavelengths = len(cv.columns)\nn_wavelengths","metadata":{"execution":{"iopub.status.busy":"2024-08-03T11:24:36.651257Z","iopub.execute_input":"2024-08-03T11:24:36.651789Z","iopub.status.idle":"2024-08-03T11:24:36.661356Z","shell.execute_reply.started":"2024-08-03T11:24:36.651729Z","shell.execute_reply":"2024-08-03T11:24:36.659831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dev.shape, cv.shape, cv_sub.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-03T11:24:37.646601Z","iopub.execute_input":"2024-08-03T11:24:37.647663Z","iopub.status.idle":"2024-08-03T11:24:37.65655Z","shell.execute_reply.started":"2024-08-03T11:24:37.647621Z","shell.execute_reply":"2024-08-03T11:24:37.654954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv","metadata":{"execution":{"iopub.status.busy":"2024-08-03T11:24:40.1443Z","iopub.execute_input":"2024-08-03T11:24:40.144687Z","iopub.status.idle":"2024-08-03T11:24:40.182358Z","shell.execute_reply.started":"2024-08-03T11:24:40.144659Z","shell.execute_reply":"2024-08-03T11:24:40.180881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv_sub","metadata":{"execution":{"iopub.status.busy":"2024-08-03T11:24:41.291159Z","iopub.execute_input":"2024-08-03T11:24:41.291698Z","iopub.status.idle":"2024-08-03T11:24:41.33794Z","shell.execute_reply.started":"2024-08-03T11:24:41.291659Z","shell.execute_reply":"2024-08-03T11:24:41.336546Z"},"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-03T11:24:42.333914Z","iopub.execute_input":"2024-08-03T11:24:42.33441Z","iopub.status.idle":"2024-08-03T11:24:42.458269Z","shell.execute_reply.started":"2024-08-03T11:24:42.334372Z","shell.execute_reply":"2024-08-03T11:24:42.456714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}