{"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":"<div style=\"color:white;\n           display:fill;\n           border-radius:2px;\n           background-color:Black;\n           font-size:250%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n<p style=\"padding: 10px;\n          text-align: center;\n          font-size:100%;\n          color:brown;\">\n           🎨Code is taken from Organizer📊\n</p>\n<style>\n        h1{text-align: center;}\n </style>  \n    \n</div> ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-20T10:31:13.543908Z","iopub.execute_input":"2023-01-20T10:31:13.544351Z","iopub.status.idle":"2023-01-20T10:31:13.589270Z","shell.execute_reply.started":"2023-01-20T10:31:13.544262Z","shell.execute_reply":"2023-01-20T10:31:13.587819Z"}}},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:#00838F;\n           font-size:110%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<p style=\"padding: 10px;\n              color:white;\">\nthe code defines a function angular_dist_score() which calculates the mean angular distance (MAE) between two directions represented by their azimuth and zenith angles. The function takes four parameters: az_true, zen_true, az_pred, and zen_pred, which are the true and predicted azimuth and zenith angles in radians.\n\n\n</p>\n</div>","metadata":{}},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:green;\n           font-size:250%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<p style=\"padding:3px;\n          text-align: center;\n          font-size:50%;\n          color:yellow;\">\n          The function first checks that all the input values are finite, and raises a ValueError if any of them are not. Next, it precomputes all the sine and cosine values of the true and predicted azimuth and zenith angles.\n</p>\n<style>\n        h1{text-align: center;}\n </style>  ","metadata":{}},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:black;\n           font-size:110%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n<p style=\"padding: 10px;\n              color:white;\">\nThen, the code converts the true and predicted angles into their corresponding cartesian unit vectors, and computes their scalar product, which is equal to the cosine of the angle between the two vectors.\n\n</p>\n</div>","metadata":{}},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:brown;\n           font-size:250%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n<p style=\"padding:3px;\n          text-align: center;\n          font-size:50%;\n          color:black;\">\n           It then clips the scalar product between -1 and 1 to prevent any numerical instability that might occur due to the finite precision of the sine and cosine functions.\nFinally, it converts the scalar product back to an angle in radians and calculates the average of the absolute value of this angle.\n</p>\n<style>\n        h1{text-align: center;}\n </style>  \n","metadata":{}},{"cell_type":"code","source":"import numpy as np\n\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 (np.all(np.isfinite(az_true)) and\n            np.all(np.isfinite(zen_true)) and\n            np.all(np.isfinite(az_pred)) and\n            np.all(np.isfinite(zen_pred))):\n        raise ValueError(\"All arguments must be finite\")\n    \n    # pre-compute all sine and cosine values\n    sa1 = np.sin(az_true)\n    ca1 = np.cos(az_true)\n    sz1 = np.sin(zen_true)\n    cz1 = np.cos(zen_true)\n    \n    sa2 = np.sin(az_pred)\n    ca2 = np.cos(az_pred)\n    sz2 = np.sin(zen_pred)\n    cz2 = np.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 =  np.clip(scalar_prod, -1, 1)\n    \n    # convert back to an angle (in radian)\n    return np.average(np.abs(np.arccos(scalar_prod)))\n","metadata":{"execution":{"iopub.status.busy":"2023-01-20T11:14:51.280651Z","iopub.execute_input":"2023-01-20T11:14:51.281141Z","iopub.status.idle":"2023-01-20T11:14:51.294248Z","shell.execute_reply.started":"2023-01-20T11:14:51.281103Z","shell.execute_reply":"2023-01-20T11:14:51.292416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:green;\n           font-size:150%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<p style=\"padding:3px;\n          text-align: center;\n          font-size:150%;\n          color:yellow;\">\n           📚📚Understanding the function with example🧾📑\n</p>\n<style>\n        h1{text-align: center;}\n </style>  ","metadata":{}},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:yellow;\n           font-size:150%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<p style=\"padding:3px;\n          text-align: center;\n          font-size:100%;\n          color:black;\">\n          if the true azimuth and zenith values are az_true = 10 degrees, zen_true = 20 degrees and the predicted azimuth and zenith values are az_pred = 350 degrees, zen_pred = 20 degrees, the function would calculate the average angular distance between these two directions.\n</p>\n<style>\n        h1{text-align: center;}\n </style>  ","metadata":{}},{"cell_type":"code","source":"angular_dist_score(10, 20, 350, 20)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T11:14:51.297286Z","iopub.execute_input":"2023-01-20T11:14:51.297969Z","iopub.status.idle":"2023-01-20T11:14:51.316056Z","shell.execute_reply.started":"2023-01-20T11:14:51.297914Z","shell.execute_reply":"2023-01-20T11:14:51.314997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"angular_dist_score(10, 20, 0, 20)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T11:14:51.317941Z","iopub.execute_input":"2023-01-20T11:14:51.318308Z","iopub.status.idle":"2023-01-20T11:14:51.330770Z","shell.execute_reply.started":"2023-01-20T11:14:51.318275Z","shell.execute_reply":"2023-01-20T11:14:51.328847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:green;\n           font-size:150%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<p style=\"padding:3px;\n          text-align: center;\n          font-size:150%;\n          color:yellow;\">\n           📚📚Using np.mean instead of np.average in above code for faster run🧾📑\n</p>\n<style>\n        h1{text-align: center;}\n </style>  ","metadata":{}},{"cell_type":"code","source":"import numpy as np\n\ndef angular_dist_score1(az_true, zen_true, az_pred, zen_pred):\n    scalar_prod = (np.sin(zen_true) * np.sin(zen_pred) * (np.cos(az_true) * np.cos(az_pred) + np.sin(az_true) * np.sin(az_pred)) + (np.cos(zen_true) * np.cos(zen_pred)))\n    scalar_prod =  np.clip(scalar_prod, -1, 1)\n    return np.mean(np.abs(np.arccos(scalar_prod)))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"angular_dist_score1(10, 20, 350, 20)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T11:20:56.976517Z","iopub.execute_input":"2023-01-20T11:20:56.977097Z","iopub.status.idle":"2023-01-20T11:20:56.986808Z","shell.execute_reply.started":"2023-01-20T11:20:56.977060Z","shell.execute_reply":"2023-01-20T11:20:56.985422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:black;\n           font-size:110%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n<p style=\"padding: 10px;\n              color:white;\">\nHere we are calculating Mean angular error\n\n</p>\n</div>","metadata":{}},{"cell_type":"code","source":"import numpy as np\n\ndef mean_angular_error(true_angles, predicted_angles):\n    \"\"\"\n    Calculates the Mean Angular Error (MAE) for a given set of true angles and predicted angles.\n    :param true_angles: array-like, true angles\n    :param predicted_angles: array-like, predicted angles\n    :return: mean angular error\n    \"\"\"\n    # Convert angles to radians\n    true_angles = np.radians(true_angles)\n    predicted_angles = np.radians(predicted_angles)\n    \n    # Calculate deviation between true angles and predicted angles\n    deviation = np.abs(np.mod(true_angles - predicted_angles + np.pi, 2*np.pi) - np.pi)\n    \n    # Calculate mean angular error\n    mae = np.mean(deviation)\n    \n    return np.degrees(mae)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T11:14:51.334266Z","iopub.execute_input":"2023-01-20T11:14:51.335418Z","iopub.status.idle":"2023-01-20T11:14:51.344074Z","shell.execute_reply.started":"2023-01-20T11:14:51.335355Z","shell.execute_reply":"2023-01-20T11:14:51.342710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_angular_error(0, 360)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T11:14:51.346076Z","iopub.execute_input":"2023-01-20T11:14:51.346739Z","iopub.status.idle":"2023-01-20T11:14:51.365456Z","shell.execute_reply.started":"2023-01-20T11:14:51.346694Z","shell.execute_reply":"2023-01-20T11:14:51.364465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_angular_error(10, 350)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T11:14:51.366812Z","iopub.execute_input":"2023-01-20T11:14:51.367584Z","iopub.status.idle":"2023-01-20T11:14:51.380153Z","shell.execute_reply.started":"2023-01-20T11:14:51.367546Z","shell.execute_reply":"2023-01-20T11:14:51.378667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:green;\n           font-size:150%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<p style=\"padding:3px;\n          text-align: center;\n          font-size:150%;\n          color:yellow;\">\n           📚📚Looking for suggestion if somethng is nt correct my apology...🧾📑\n</p>\n<style>\n        h1{text-align: center;}\n </style> ","metadata":{}},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-01-20T11:16:53.047458Z","iopub.execute_input":"2023-01-20T11:16:53.047956Z","iopub.status.idle":"2023-01-20T11:16:53.056765Z","shell.execute_reply.started":"2023-01-20T11:16:53.047919Z","shell.execute_reply":"2023-01-20T11:16:53.055118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#angular_dist_score1(az_true, zen_true, az_pred, zen_pred)\nangular_dist_score1(10, 20, 350, 20)","metadata":{"execution":{"iopub.status.busy":"2023-01-20T11:16:55.505546Z","iopub.execute_input":"2023-01-20T11:16:55.506016Z","iopub.status.idle":"2023-01-20T11:16:55.514373Z","shell.execute_reply.started":"2023-01-20T11:16:55.505981Z","shell.execute_reply":"2023-01-20T11:16:55.512957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}