{"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":"raw","source":"Implementation of Mean Angular Error (the metric used in this competition) in PyTorch.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import torch\n\ndef angular_dist_score(az_true, zen_true, az_pred, zen_pred):\n    '''\n    calculate the MAE of the angular distance between two directions.\n    The two vectors are first converted to cartesian unit vectors,\n    and then their scalar product is computed, which is equal to\n    the cosine of the angle between the two vectors. The inverse \n    cosine (arccos) thereof is then the angle between the two input vectors\n    \n    Parameters:\n    -----------\n    \n    az_true : float (or array thereof)\n        true azimuth value(s) in radian\n    zen_true : float (or array thereof)\n        true zenith value(s) in radian\n    az_pred : float (or array thereof)\n        predicted azimuth value(s) in radian\n    zen_pred : float (or array thereof)\n        predicted zenith value(s) in radian\n    \n    Returns:\n    --------\n    \n    dist : float\n        mean over the angular distance(s) in radian\n    '''\n    \n    if not (torch.all(torch.isfinite(az_true)) and\n            torch.all(torch.isfinite(zen_true)) and\n            torch.all(torch.isfinite(az_pred)) and\n            torch.all(torch.isfinite(zen_pred))):\n        raise ValueError(\"All arguments must be finite\")\n    \n    # pre-compute all sine and cosine values\n    sa1 = torch.sin(az_true)\n    ca1 = torch.cos(az_true)\n    sz1 = torch.sin(zen_true)\n    cz1 = torch.cos(zen_true)\n    \n    sa2 = torch.sin(az_pred)\n    ca2 = torch.cos(az_pred)\n    sz2 = torch.sin(zen_pred)\n    cz2 = torch.cos(zen_pred)\n    \n    # scalar product of the two cartesian vectors (x = sz*ca, y = sz*sa, z = cz)\n    scalar_prod = sz1*sz2*(ca1*ca2 + sa1*sa2) + (cz1*cz2)\n    \n    # scalar product of two unit vectors is always between -1 and 1, this is against nummerical instability\n    # that might otherwise occure from the finite precision of the sine and cosine functions\n    scalar_prod =  torch.clamp(scalar_prod, -1, 1)\n    \n    # convert back to an angle (in radian)\n    return torch.mean(torch.abs(torch.acos(scalar_prod)))\n","metadata":{"execution":{"iopub.status.busy":"2023-02-12T10:25:35.290940Z","iopub.execute_input":"2023-02-12T10:25:35.291469Z","iopub.status.idle":"2023-02-12T10:25:37.217755Z","shell.execute_reply.started":"2023-02-12T10:25:35.291358Z","shell.execute_reply":"2023-02-12T10:25:37.216091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"az_true, zen_true, az_pred, zen_pred = torch.tensor([0.1]), torch.tensor([0.2]), torch.tensor([0.15]), torch.tensor([0.25])\nangular_dist_score(az_true, zen_true, az_pred, zen_pred)","metadata":{"execution":{"iopub.status.busy":"2023-02-12T10:26:52.950513Z","iopub.execute_input":"2023-02-12T10:26:52.950939Z","iopub.status.idle":"2023-02-12T10:26:52.980738Z","shell.execute_reply.started":"2023-02-12T10:26:52.950904Z","shell.execute_reply":"2023-02-12T10:26:52.979798Z"},"trusted":true},"execution_count":null,"outputs":[]}]}