Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Oct 7, 2024
1 parent fe1dabb commit baa20b0
Showing 1 changed file with 113 additions and 96 deletions.
209 changes: 113 additions & 96 deletions benchmarks/Benchmarks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"outputs": [],
"source": [
"import timeit\n",
"import tqdm\n",
"import pprint\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"import tqdm\n",
"from matplotlib import pyplot as plt"
]
},
Expand All @@ -20,10 +20,10 @@
"metadata": {},
"outputs": [],
"source": [
"plt.rcParams['text.usetex'] = True\n",
"plt.rcParams['axes.labelsize'] = 16\n",
"plt.rcParams['font.size'] = 16\n",
"plt.rcParams['legend.fontsize'] = 'large'"
"plt.rcParams[\"text.usetex\"] = True\n",
"plt.rcParams[\"axes.labelsize\"] = 16\n",
"plt.rcParams[\"font.size\"] = 16\n",
"plt.rcParams[\"legend.fontsize\"] = \"large\""
]
},
{
Expand All @@ -48,9 +48,10 @@
}
],
"source": [
"import pyquaternion # pip install pyquaternion --user\n",
"import quaternion # pip install numpy_quaternion --user\n",
"import rowan # pip install rowan --user"
"import pyquaternion # pip install pyquaternion --user\n",
"import quaternion # pip install numpy_quaternion --user\n",
"\n",
"import rowan # pip install rowan --user"
]
},
{
Expand All @@ -61,16 +62,18 @@
"source": [
"def arr_to_pyquat(arr):\n",
" if len(arr.shape) > 1:\n",
" pq_arr = np.empty(arr.shape[:-1], dtype='object')\n",
" pq_arr = np.empty(arr.shape[:-1], dtype=\"object\")\n",
" for i, x in enumerate(arr):\n",
" pq_arr[i] = pyquaternion.Quaternion(x)\n",
" else:\n",
" pq_arr = np.array([pyquaternion.Quaternion(arr)])\n",
" return pq_arr\n",
"\n",
"\n",
"def arr_to_npquat(arr):\n",
" return quaternion.as_quat_array(arr)\n",
"\n",
"\n",
"pyquat_times = {}\n",
"quat_times = {}\n",
"rowan_times = {}\n",
Expand Down Expand Up @@ -107,33 +110,31 @@
}
],
"source": [
"pyquat_times['Multiply'] = []\n",
"quat_times['Multiply'] = []\n",
"rowan_times['Multiply'] = []\n",
"pyquat_times[\"Multiply\"] = []\n",
"quat_times[\"Multiply\"] = []\n",
"rowan_times[\"Multiply\"] = []\n",
"for N in tqdm.tqdm_notebook(Ns):\n",
" x = rowan.random.rand(N)\n",
" y = rowan.random.rand(N)\n",
"\n",
" if N < pqlim:\n",
" pyquat_times['Multiply'].append(\n",
" pyquat_times[\"Multiply\"].append(\n",
" timeit.timeit(\n",
" \"x*y\",\n",
" setup=\"from __main__ import x, y, arr_to_pyquat; x = arr_to_pyquat(x); y = arr_to_pyquat(y)\",\n",
" number = num\n",
" number=num,\n",
" )\n",
" )\n",
" quat_times['Multiply'].append(\n",
" quat_times[\"Multiply\"].append(\n",
" timeit.timeit(\n",
" \"x*y\",\n",
" setup=\"from __main__ import x, y, arr_to_npquat; x = arr_to_npquat(x); y = arr_to_npquat(y)\",\n",
" number = num\n",
" number=num,\n",
" )\n",
" )\n",
" rowan_times['Multiply'].append(\n",
" rowan_times[\"Multiply\"].append(\n",
" timeit.timeit(\n",
" \"rowan.multiply(x, y)\",\n",
" setup=\"from __main__ import x, y, rowan\",\n",
" number = num\n",
" \"rowan.multiply(x, y)\", setup=\"from __main__ import x, y, rowan\", number=num\n",
" )\n",
" )"
]
Expand Down Expand Up @@ -166,33 +167,29 @@
}
],
"source": [
"pyquat_times['Exponential'] = []\n",
"quat_times['Exponential'] = []\n",
"rowan_times['Exponential'] = []\n",
"pyquat_times[\"Exponential\"] = []\n",
"quat_times[\"Exponential\"] = []\n",
"rowan_times[\"Exponential\"] = []\n",
"for N in tqdm.tqdm_notebook(Ns):\n",
" x = rowan.random.rand(N)\n",
"\n",
" if N < pqlim:\n",
" pyquat_times['Exponential'].append(\n",
" pyquat_times[\"Exponential\"].append(\n",
" timeit.timeit(\n",
" \"for i in range(len(x)): pyquaternion.Quaternion.exp(x[i])\",\n",
" setup=\"from __main__ import x, pyquaternion, arr_to_pyquat; x = arr_to_pyquat(x);\",\n",
" number = num\n",
" number=num,\n",
" )\n",
" )\n",
" quat_times['Exponential'].append(\n",
" quat_times[\"Exponential\"].append(\n",
" timeit.timeit(\n",
" \"np.exp(x)\",\n",
" setup=\"from __main__ import x, arr_to_npquat, np; x = arr_to_npquat(x);\",\n",
" number = num\n",
" number=num,\n",
" )\n",
" )\n",
" rowan_times['Exponential'].append(\n",
" timeit.timeit(\n",
" \"rowan.exp(x)\",\n",
" setup=\"from __main__ import x, rowan\",\n",
" number = num\n",
" )\n",
" rowan_times[\"Exponential\"].append(\n",
" timeit.timeit(\"rowan.exp(x)\", setup=\"from __main__ import x, rowan\", number=num)\n",
" )"
]
},
Expand Down Expand Up @@ -224,32 +221,30 @@
}
],
"source": [
"pyquat_times['Conjugate'] = []\n",
"quat_times['Conjugate'] = []\n",
"rowan_times['Conjugate'] = []\n",
"pyquat_times[\"Conjugate\"] = []\n",
"quat_times[\"Conjugate\"] = []\n",
"rowan_times[\"Conjugate\"] = []\n",
"for N in tqdm.tqdm_notebook(Ns):\n",
" x = rowan.random.rand(N)\n",
"\n",
" if N < pqlim:\n",
" pyquat_times['Conjugate'].append(\n",
" pyquat_times[\"Conjugate\"].append(\n",
" timeit.timeit(\n",
" \"for i in range(len(x)): x.conjugate\",\n",
" setup=\"from __main__ import x, arr_to_pyquat; x = arr_to_pyquat(x);\",\n",
" number = num\n",
" number=num,\n",
" )\n",
" )\n",
" quat_times['Conjugate'].append(\n",
" quat_times[\"Conjugate\"].append(\n",
" timeit.timeit(\n",
" \"x.conjugate()\",\n",
" setup=\"from __main__ import x, arr_to_npquat; x = arr_to_npquat(x);\",\n",
" number = num\n",
" number=num,\n",
" )\n",
" )\n",
" rowan_times['Conjugate'].append(\n",
" rowan_times[\"Conjugate\"].append(\n",
" timeit.timeit(\n",
" \"rowan.conjugate(x)\",\n",
" setup=\"from __main__ import x, rowan\",\n",
" number = num\n",
" \"rowan.conjugate(x)\", setup=\"from __main__ import x, rowan\", number=num\n",
" )\n",
" )"
]
Expand Down Expand Up @@ -282,32 +277,30 @@
}
],
"source": [
"pyquat_times['Norm'] = []\n",
"quat_times['Norm'] = []\n",
"rowan_times['Norm'] = []\n",
"pyquat_times[\"Norm\"] = []\n",
"quat_times[\"Norm\"] = []\n",
"rowan_times[\"Norm\"] = []\n",
"for N in tqdm.tqdm_notebook(Ns):\n",
" x = rowan.random.rand(N)\n",
"\n",
" if N < pqlim:\n",
" pyquat_times['Norm'].append(\n",
" pyquat_times[\"Norm\"].append(\n",
" timeit.timeit(\n",
" \"for i in range(len(x)): x[i].norm\",\n",
" setup=\"from __main__ import x, arr_to_pyquat; x = arr_to_pyquat(x);\",\n",
" number = num\n",
" number=num,\n",
" )\n",
" )\n",
" quat_times['Norm'].append(\n",
" quat_times[\"Norm\"].append(\n",
" timeit.timeit(\n",
" \"np.abs(x)\",\n",
" setup=\"from __main__ import x, np, arr_to_npquat; x = arr_to_npquat(x);\",\n",
" number = num\n",
" number=num,\n",
" )\n",
" )\n",
" rowan_times['Norm'].append(\n",
" rowan_times[\"Norm\"].append(\n",
" timeit.timeit(\n",
" \"rowan.norm(x)\",\n",
" setup=\"from __main__ import x, rowan\",\n",
" number = num\n",
" \"rowan.norm(x)\", setup=\"from __main__ import x, rowan\", number=num\n",
" )\n",
" )"
]
Expand Down Expand Up @@ -340,32 +333,30 @@
}
],
"source": [
"pyquat_times['To Matrix'] = []\n",
"quat_times['To Matrix'] = []\n",
"rowan_times['To Matrix'] = []\n",
"pyquat_times[\"To Matrix\"] = []\n",
"quat_times[\"To Matrix\"] = []\n",
"rowan_times[\"To Matrix\"] = []\n",
"for N in tqdm.tqdm_notebook(Ns):\n",
" x = rowan.random.rand(N)\n",
"\n",
" if N < pqlim:\n",
" pyquat_times['To Matrix'].append(\n",
" pyquat_times[\"To Matrix\"].append(\n",
" timeit.timeit(\n",
" \"for i in range(len(x)): x[i].rotation_matrix\",\n",
" setup=\"from __main__ import x, arr_to_pyquat; x = arr_to_pyquat(x);\",\n",
" number = num\n",
" number=num,\n",
" )\n",
" )\n",
" quat_times['To Matrix'].append(\n",
" quat_times[\"To Matrix\"].append(\n",
" timeit.timeit(\n",
" \"quaternion.as_rotation_matrix(x)\",\n",
" setup=\"from __main__ import x, quaternion, arr_to_npquat; x = arr_to_npquat(x);\",\n",
" number = num\n",
" number=num,\n",
" )\n",
" )\n",
" rowan_times['To Matrix'].append(\n",
" rowan_times[\"To Matrix\"].append(\n",
" timeit.timeit(\n",
" \"rowan.to_matrix(x)\",\n",
" setup=\"from __main__ import x, rowan\",\n",
" number = num\n",
" \"rowan.to_matrix(x)\", setup=\"from __main__ import x, rowan\", number=num\n",
" )\n",
" )"
]
Expand All @@ -376,9 +367,9 @@
"metadata": {},
"outputs": [],
"source": [
"pyquat_times['N'] = list(np.array(Ns)[np.array(Ns) < pqlim])\n",
"quat_times['N'] = Ns\n",
"rowan_times['N'] = Ns"
"pyquat_times[\"N\"] = list(np.array(Ns)[np.array(Ns) < pqlim])\n",
"quat_times[\"N\"] = Ns\n",
"rowan_times[\"N\"] = Ns"
]
},
{
Expand All @@ -387,18 +378,24 @@
"metadata": {},
"outputs": [],
"source": [
"df_pq = pd.DataFrame(pyquat_times).melt(id_vars=\"N\", var_name=\"operation\", value_name=\"pyquaternion\")\n",
"df_nq = pd.DataFrame(quat_times).melt(id_vars=\"N\", var_name=\"operation\", value_name=\"npquaternion\")\n",
"df_r = pd.DataFrame(rowan_times).melt(id_vars=\"N\", var_name=\"operation\", value_name=\"rowan\")\n",
"df = df_r.merge(df_nq, on =[\"N\", \"operation\"])\n",
"df = df.merge(df_pq, on =[\"N\", \"operation\"], how = \"left\")\n",
"df_pq = pd.DataFrame(pyquat_times).melt(\n",
" id_vars=\"N\", var_name=\"operation\", value_name=\"pyquaternion\"\n",
")\n",
"df_nq = pd.DataFrame(quat_times).melt(\n",
" id_vars=\"N\", var_name=\"operation\", value_name=\"npquaternion\"\n",
")\n",
"df_r = pd.DataFrame(rowan_times).melt(\n",
" id_vars=\"N\", var_name=\"operation\", value_name=\"rowan\"\n",
")\n",
"df = df_r.merge(df_nq, on=[\"N\", \"operation\"])\n",
"df = df.merge(df_pq, on=[\"N\", \"operation\"], how=\"left\")\n",
"df.fillna(0, inplace=True)\n",
"df['pyquaternion'] /= df['N']\n",
"df['pyquaternion'] *= 1e6\n",
"df['npquaternion'] /= df['N']\n",
"df['npquaternion'] *= 1e6\n",
"df['rowan'] /= df['N']\n",
"df['rowan'] *= 1e6"
"df[\"pyquaternion\"] /= df[\"N\"]\n",
"df[\"pyquaternion\"] *= 1e6\n",
"df[\"npquaternion\"] /= df[\"N\"]\n",
"df[\"npquaternion\"] *= 1e6\n",
"df[\"rowan\"] /= df[\"N\"]\n",
"df[\"rowan\"] *= 1e6"
]
},
{
Expand Down Expand Up @@ -566,8 +563,8 @@
],
"source": [
"view = df.groupby([\"N\", \"operation\"]).mean()\n",
"view['rowan vs. npq'] = view['rowan']/view['npquaternion']\n",
"view['pyq vs. rowan'] = view['pyquaternion']/view['rowan']\n",
"view[\"rowan vs. npq\"] = view[\"rowan\"] / view[\"npquaternion\"]\n",
"view[\"pyq vs. rowan\"] = view[\"pyquaternion\"] / view[\"rowan\"]\n",
"view"
]
},
Expand All @@ -591,23 +588,43 @@
}
],
"source": [
"cols = list(col['color'] for col in plt.rcParams['axes.prop_cycle'])\n",
"cols = list(col[\"color\"] for col in plt.rcParams[\"axes.prop_cycle\"])\n",
"fig, axes = plt.subplots(2, 1, figsize=(8, 15))\n",
"ax = df[df['N'] == Ns[0]].drop(['N'], axis=1).groupby(\n",
" [\"operation\"]).mean().plot.barh(ax=axes[0], logx=True, color = cols[0:3],\n",
" title=\"$\\log_{{\\,10}}(N) = {}$\".format(int(np.log10(Ns[0]))),\n",
" xlim=(10**-2, 10**3), legend=False)\n",
"ax = df[df['N'] == Ns[1]].drop(['N'], axis=1).groupby(\n",
" [\"operation\"]).mean().plot.barh(ax=axes[1], logx=True, color = cols[0:3],\n",
" title=\"$\\log_{{\\,10}}(N) = {}$\".format(int(np.log10(Ns[1]))),\n",
" xlim=(10**-2, 10**3), legend=False)\n",
"ax.set_xlabel(\"$\\log_{\\,10}(sec)$\", fontsize=18)\n",
"ax = (\n",
" df[df[\"N\"] == Ns[0]]\n",
" .drop([\"N\"], axis=1)\n",
" .groupby([\"operation\"])\n",
" .mean()\n",
" .plot.barh(\n",
" ax=axes[0],\n",
" logx=True,\n",
" color=cols[0:3],\n",
" title=rf\"$\\log_{{\\,10}}(N) = {int(np.log10(Ns[0]))}$\",\n",
" xlim=(10**-2, 10**3),\n",
" legend=False,\n",
" )\n",
")\n",
"ax = (\n",
" df[df[\"N\"] == Ns[1]]\n",
" .drop([\"N\"], axis=1)\n",
" .groupby([\"operation\"])\n",
" .mean()\n",
" .plot.barh(\n",
" ax=axes[1],\n",
" logx=True,\n",
" color=cols[0:3],\n",
" title=rf\"$\\log_{{\\,10}}(N) = {int(np.log10(Ns[1]))}$\",\n",
" xlim=(10**-2, 10**3),\n",
" legend=False,\n",
" )\n",
")\n",
"ax.set_xlabel(r\"$\\log_{\\,10}(sec)$\", fontsize=18)\n",
"for ax in axes.flatten():\n",
" ax.set_ylabel(\"\")\n",
" ax.legend(loc='best', fontsize=15)\n",
" ax.legend(loc=\"best\", fontsize=15)\n",
" ax.title.set_fontsize(20)\n",
" #ax.tick_params(axis='both', which='major', labelsize=16)\n",
" \n",
" # ax.tick_params(axis='both', which='major', labelsize=16)\n",
"\n",
"fig.subplots_adjust(left=0.2)\n",
"plt.show()\n",
"fig.savefig(\"Performance.pdf\")"
Expand Down

0 comments on commit baa20b0

Please sign in to comment.