{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Compare power datasets" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# this script compares dataframes producing using the same antenna pattern but with various galactic radio emission models\n", "# first you need to produce these, first run get_power_dataset.py in loop using all the galactic radio emission models\n", "# e.g.: `for m in LFSS GSM08 GSM16 Haslam LFmap SSM GMOSS ULSA; do python get_power_dataset.py -g $m -s Salla_EW; done`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import plotly.express as px\n", "from pathlib import Path\n", "import os\n", "from scipy.interpolate import CubicSpline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This ensures Plotly output works in multiple places:\n", "# plotly_mimetype: VS Code notebook UI\n", "# notebook: \"Jupyter: Export to HTML\" command in VS Code\n", "# See https://plotly.com/python/renderers/#multiple-renderers\n", "import plotly.io as pio\n", "\n", "pio.renderers.default = \"plotly_mimetype+notebook\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# some global plot settings\n", "plt.rcParams[\"axes.labelweight\"] = \"bold\"\n", "plt.rcParams[\"font.weight\"] = \"bold\"\n", "plt.rcParams[\"font.size\"] = 16\n", "plt.rcParams[\"legend.fontsize\"] = 14\n", "\n", "plt.rcParams[\"xtick.major.width\"] = 2\n", "plt.rcParams[\"ytick.major.width\"] = 2\n", "\n", "plt.rcParams[\"xtick.major.size\"] = 5\n", "plt.rcParams[\"ytick.major.size\"] = 5\n", "\n", "plt.rcParams[\"xtick.labelsize\"] = 14\n", "plt.rcParams[\"ytick.labelsize\"] = 14\n", "\n", "layout_settings = dict(\n", " xaxis=dict(title=\"LST\", tickprefix=\"\", ticksuffix=\"\", dtick=2),\n", " yaxis=dict(\n", " title=\"frequency [MHz]\",\n", " tickprefix=\"\",\n", " ticksuffix=\"\",\n", " range=(30, 80),\n", " tick0=0,\n", " dtick=10,\n", " autorange=False,\n", " ),\n", " font=dict(\n", " size=20,\n", " color=\"black\",\n", " ),\n", " coloraxis=dict(\n", " colorbar=dict(\n", " tickprefix=\"\",\n", " ticksuffix=\"\",\n", " title=dict(text=\"Power [pW]\", side=\"right\")),\n", " cmin=0,\n", " cmax=24,\n", " )\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dir_path = \"./simulated_power_datasets/\"\n", "df_files = [\n", " os.path.join(dir_path, i) for i in os.listdir(dir_path) if (i.endswith(\".csv\") & ('Salla_EW' in i))\n", "]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_list = []\n", "df_names = []\n", "for f in df_files:\n", " df = pd.read_csv(f, index_col=0)\n", " df.columns = df.columns.astype(float)\n", " df_list.append(df)\n", " df_names.append(Path(f).stem)\n", "\n", "concatenated_df = pd.concat(df_list, keys=df_names)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# HINTS\n", "# print df main keys\n", "# print(concatenated_df.index.levels[0])\n", "# get the same cell from each dfs\n", "# one_cell_values = np.asarray([concatenated_df.xs(key).iloc[0,0] for key in concatenated_df.index.levels[0]])\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# mean across the cells of all dfs\n", "mean_df = concatenated_df.groupby(level=1).mean()\n", "# std\n", "std_df = concatenated_df.groupby(level=1).std()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = px.imshow(mean_df.T, width=600, aspect=\"cube\", color_continuous_scale=\"jet\")\n", "fig.update_layout(**layout_settings)\n", "fig.update_layout(\n", " title='Mean',\n", " coloraxis=dict(\n", " colorbar=dict(\n", " title=dict(\n", " text=\" [pW]\"\n", " )\n", " )\n", " )\n", ")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = px.imshow(\n", " (std_df / mean_df.values).T, width=600, aspect=\"cube\", color_continuous_scale=\"jet\", title='Relative standard deviation'\n", ")\n", "fig.update_layout(**layout_settings)\n", "fig.update_layout(\n", " title='Mean',\n", " coloraxis=dict(\n", " colorbar=dict(\n", " title=dict(\n", " text=\"\\u03C1\",\n", " side=\"top\",\n", " )\n", " ),\n", " cmin=0, # Minimum color value\n", " cmax=0.1, # Maximum color value\n", " )\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rho = (std_df / mean_df.values).values.flatten()\n", "print('Min: {:.2f}, Max: {:.2f}, Mean: {:.2f}'.format(np.min(rho), np.max(rho), np.mean(rho)))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots()\n", "\n", "ax.hist(rho)\n", "ax.set_xlabel(r\"$\\rho$\")\n", "ax.set_ylabel(\"entries\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Frequency mean of the mean dataframe\n", "mean_signal = mean_df.mean(axis=1).values\n", "mean_gal_signal_func = CubicSpline(\n", " np.append(mean_df.index.values, mean_df.index.values[-1] + 1),\n", " np.append(mean_signal, mean_signal[0]),\n", " extrapolate=\"periodic\",\n", " bc_type=\"periodic\",\n", ")\n", "new_x = np.linspace(0, 24, 24 * 60)\n", "mean_gal_signal_func(new_x)\n", "\n", "fig = px.line(\n", " x=new_x,\n", " y=mean_gal_signal_func(new_x),\n", " title=\"Frequency mean of the mean dataframe\",\n", " width=600,\n", ")\n", "\n", "fig.add_trace(\n", " px.scatter(\n", " x=[\n", " new_x[np.argmin(mean_gal_signal_func(new_x))],\n", " new_x[np.argmax(mean_gal_signal_func(new_x))],\n", " ],\n", " y=[np.min(mean_gal_signal_func(new_x)), np.max(mean_gal_signal_func(new_x))],\n", " ).data[0]\n", ")\n", "\n", "fig.update_traces(marker=dict(color=\"red\", size=15))\n", "fig.update_layout(**layout_settings)\n", "fig.update_layout(\n", " dict(\n", " yaxis=dict(\n", " title=\"power [pW]\",\n", " tickprefix=\"\",\n", " ticksuffix=\"\",\n", " range=(5, 10),\n", " # tick0=0,\n", " dtick=1,\n", " autorange=False,\n", " ),\n", " )\n", ")\n", "\n", "fig.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "vscode": { "interpreter": { "hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a" } } }, "nbformat": 4, "nbformat_minor": 2 }