{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<B>UPDATED TO ALLOW CHECKING LOSS FOR 1 VERTEBRA OR ANY SUBSET OF THE VERTEBRAE</B>. Adapted from pytorch version included in <a href='https://www.kaggle.com/code/vslaykovsky/train-pytorch-effnetv2-baseline-cv-0-49/notebook'>https://www.kaggle.com/code/vslaykovsky/train-pytorch-effnetv2-baseline-cv-0-49/notebook</a>. ","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport sklearn.metrics\nfrom sklearn.metrics import log_loss","metadata":{"execution":{"iopub.status.busy":"2022-09-18T21:39:52.50479Z","iopub.execute_input":"2022-09-18T21:39:52.505356Z","iopub.status.idle":"2022-09-18T21:39:52.512758Z","shell.execute_reply.started":"2022-09-18T21:39:52.505314Z","shell.execute_reply":"2022-09-18T21:39:52.511238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/train.csv\")\n","metadata":{"execution":{"iopub.status.busy":"2022-09-18T21:39:52.525281Z","iopub.execute_input":"2022-09-18T21:39:52.525822Z","iopub.status.idle":"2022-09-18T21:39:52.54026Z","shell.execute_reply.started":"2022-09-18T21:39:52.525782Z","shell.execute_reply":"2022-09-18T21:39:52.538742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def weighted_loss(y_hat, y):\n    \n    predicted_var_len=len(y_hat[0])\n    if predicted_var_len==8:\n        neg_weights = [7, 1, 1, 1, 1, 1, 1, 1] \n        pos_weights = [14, 2, 2, 2, 2, 2, 2, 2] \n    else: \n        neg_weights = [1 for i in range(predicted_var_len)]\n        pos_weights = [2 for i in range(predicted_var_len)]\n    \n    loss_list=[]\n  \n    for i in range(len(y)):\n        ind_loss=[log_loss([y[i][j]], [y_hat[i][j]],labels=[0,1]) \\\n                   for j in range(len(y[i]))]\n        loss_list.append(ind_loss)\n    all_weights=[]\n    \n    for i in range(len(y)):\n    \n        all_weights_list=[]\n        pos_list=[y[i][j] *  pos_weights[j] for \\\n                  j in range(len(y[i]))]\n        neg_list=[(1-y[i][j]) *  neg_weights[j] \\\n                  for j in range(len(y[i]))]\n        all_weights_list=[pos_list[ctr] + neg_list[ctr] \\\n                for ctr in range(len(pos_list))]\n        all_weights.append(all_weights_list)\n        \n    loss=[]\n     \n    for i in range(len(loss_list)):\n\n        loss_item=[loss_list[i][j] *  all_weights[i][j] \\\n            for j in range(len(loss_list[i]))]\n        loss.append(loss_item)\n\n    norm=[sum(all_weights[i]) for i in range(len(all_weights))]\n  \n\n    new_loss=[]\n    for i in range(len(norm)):\n        new_loss_item=[loss[i][j] / norm[i] for j in range(len(loss[i]))]\n        new_loss.append(new_loss_item)\n    loss=new_loss\n\n    \n    loss=[np.sum(loss[i]) for i in range(len(loss))]\n    return(loss)\n                                                           \n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-18T21:39:52.54658Z","iopub.execute_input":"2022-09-18T21:39:52.547048Z","iopub.status.idle":"2022-09-18T21:39:52.564448Z","shell.execute_reply.started":"2022-09-18T21:39:52.547009Z","shell.execute_reply":"2022-09-18T21:39:52.562813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<b>Example: </b>Using baseline solution from <a href='https://www.kaggle.com/code/samuelcortinhas/rsna-fracture-detection-in-depth-eda'>https://www.kaggle.com/code/samuelcortinhas/rsna-fracture-detection-in-depth-eda</a>. ","metadata":{}},{"cell_type":"code","source":"def scale_up(q):\n    return 2*q/(1+q)\n\ntrain_df = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/train.csv\")\npreds = train_df.mean(numeric_only=True).map(scale_up).to_dict()\ny=train_df[['patient_overall','C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7']].values.tolist()\npred_fields=[]\nfor (v, k) in enumerate(preds):\n    train_df[k+'_pred']=preds[k]\n    pred_fields.append(k+'_pred')\ny_hat=train_df[pred_fields].values.tolist()\nloss=weighted_loss(y_hat, y)\ntrain_df['loss']=loss\ntotal_loss=np.mean(train_df['loss'])\nprint(f\"train loss for this solution {total_loss}\")\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-18T21:39:52.566727Z","iopub.execute_input":"2022-09-18T21:39:52.567334Z","iopub.status.idle":"2022-09-18T21:40:01.4244Z","shell.execute_reply.started":"2022-09-18T21:39:52.567294Z","shell.execute_reply":"2022-09-18T21:40:01.423007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<b>Example 2:</b> With three of the 7 vertebrae, to illustrate usage with partial data.","metadata":{}},{"cell_type":"code","source":"evaluate_vertebrae=['C1', 'C2', 'C3']\ny=train_df[evaluate_vertebrae].values.tolist()\npred_fields=[]\nfor (v, k) in enumerate(preds):\n    if k in evaluate_vertebrae:\n        train_df[k+'_pred']=preds[k]\n        pred_fields.append(k+'_pred')\n\ny_hat=train_df[pred_fields].values.tolist()\nloss=weighted_loss(y_hat, y)\ntotal_loss=np.mean(loss)\nprint(f\"train loss for{evaluate_vertebrae} {total_loss}\")","metadata":{"execution":{"iopub.status.busy":"2022-09-18T21:40:01.42678Z","iopub.execute_input":"2022-09-18T21:40:01.427403Z","iopub.status.idle":"2022-09-18T21:40:04.84217Z","shell.execute_reply.started":"2022-09-18T21:40:01.427354Z","shell.execute_reply":"2022-09-18T21:40:04.840811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<b>Example 3:</b> 1 vertebra.","metadata":{}},{"cell_type":"code","source":"evaluate='C1'\ny=train_df[[evaluate]].values.tolist()\ny_hat=train_df[[evaluate+'_pred']].values.tolist()\n\nloss=weighted_loss(y_hat, y)\ntotal_loss=np.mean(loss)\nprint(f\"train loss for {evaluate} {total_loss}\")","metadata":{"execution":{"iopub.status.busy":"2022-09-18T21:40:04.84347Z","iopub.execute_input":"2022-09-18T21:40:04.843847Z","iopub.status.idle":"2022-09-18T21:40:06.023298Z","shell.execute_reply.started":"2022-09-18T21:40:04.843812Z","shell.execute_reply":"2022-09-18T21:40:06.021388Z"},"trusted":true},"execution_count":null,"outputs":[]}]}