Skip to content

Commit

Permalink
Bokeh to Plotly (#77)
Browse files Browse the repository at this point in the history
* Change bokeh to plotly for paths notebook

* bokeh to plotly for main notebook

* Remove import statements and markdown mentioning bokeh

* paths notebook tick fixes

* main notebook tick fixes
  • Loading branch information
atharva-2001 authored Apr 18, 2024
1 parent 3ed8369 commit 6edb0f4
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 266 deletions.
216 changes: 76 additions & 140 deletions notebooks/ref_data_compare.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
"$ git fetch upstream\n",
"```\n",
"\n",
"- This notebook uses the `bokeh` plotting backend by default. Install it alongside your `tardis` environment by doing:\n",
"Activate the tardis environment:\n",
"```\n",
"$ conda activate tardis\n",
"$ conda install bokeh -c conda-forge --no-update-deps\n",
"```\n",
"\n",
"## Usage\n",
Expand All @@ -46,7 +45,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -74,7 +73,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-08-02T12:09:13.639421Z",
Expand All @@ -91,12 +90,12 @@
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"from bokeh.plotting import figure, show, ColumnDataSource\n",
"from bokeh.layouts import gridplot\n",
"from bokeh.models.tools import HoverTool\n",
"from bokeh.io import output_notebook\n",
"import plotly\n",
"import plotly.graph_objects as go\n",
"from plotly.subplots import make_subplots\n",
"\n",
"output_notebook()"
"output_notebook()\n",
"plotly.offline.init_notebook_mode()"
]
},
{
Expand All @@ -109,7 +108,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -126,7 +125,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -140,7 +139,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-08-02T09:58:38.546226Z",
Expand Down Expand Up @@ -170,7 +169,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-08-02T12:29:57.536422Z",
Expand Down Expand Up @@ -289,7 +288,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -314,7 +313,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -323,7 +322,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-08-02T12:30:01.760461Z",
Expand All @@ -345,7 +344,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-08-02T12:30:19.494810Z",
Expand All @@ -360,7 +359,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-08-02T12:31:18.320558Z",
Expand All @@ -374,7 +373,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2018-08-02T12:31:22.806199Z",
Expand Down Expand Up @@ -403,147 +402,84 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def compare_output_nu(df1, df2, mpl_backend=False):\n",
" nu_min = np.min([df1.min(), df2.min()])\n",
" nu_max = np.max([df1.max(), df2.max()])\n",
" \n",
" if mpl_backend:\n",
" plt.figure(figsize=(14, 6))\n",
" plt.subplot(121)\n",
" plt.plot(df1, df2, ',')\n",
" plt.xlabel(\"output_nu, ref 1\")\n",
" plt.ylabel(\"output_nu, ref 2\")\n",
" plt.subplot(122)\n",
" plt.hist(df1, bins=np.linspace(nu_min, nu_max, 100), histtype=\"step\", label=\"ref 1\")\n",
" plt.hist(df2, bins=np.linspace(nu_min, nu_max, 100), histtype=\"step\", label=\"ref 2\")\n",
" plt.xlabel(\"output_nu\")\n",
" plt.legend(frameon=False)\n",
" \n",
" return\n",
"\n",
" \n",
" TOOLTIPS = [(\"(x,y)\", \"(@x, @y)\")]\n",
" hover = HoverTool(tooltips=TOOLTIPS)\n",
" \n",
" p = figure()\n",
" output_nu = ColumnDataSource(pd.DataFrame.from_records({'x': df1.values, \n",
" 'y': df2.values}))\n",
" p.circle('x', 'y', size=1, source=output_nu)\n",
" p.xaxis.axis_label = \"output_nu, ref 1\"\n",
" p.yaxis.axis_label = \"output_nu, ref 2\"\n",
" p.xaxis.formatter.precision = 1\n",
" p.yaxis.formatter.precision = 1\n",
" p.add_tools(hover)\n",
"\n",
" # Step lines are hacky way to make histograms with Bokeh\n",
" arr_hist1, edges1 = np.histogram(df1.values, \n",
" bins = 100, \n",
" range = [nu_min, nu_max])\n",
" arr_hist2, edges2 = np.histogram(df1.values, \n",
" bins = 100, \n",
" range = [nu_min, nu_max])\n",
" \n",
" hist1 = ColumnDataSource(pd.DataFrame.from_records({'x': np.linspace(nu_min, nu_max, 100),\n",
" 'y': arr_hist1}))\n",
" hist2 = ColumnDataSource(pd.DataFrame.from_records({'x': np.linspace(nu_min, nu_max, 100),\n",
" 'y': arr_hist2}))\n",
" q = figure()\n",
" q.step('x', 'y', source=hist1, legend_label='ref 1')\n",
" q.step('x', 'y', source=hist2, legend_label='ref 2', color='#ff7f0e')\n",
" q.xaxis.axis_label = \"output_nu\"\n",
" q.xaxis.formatter.precision = 1\n",
" q.legend.click_policy=\"hide\"\n",
" fig = make_subplots(rows=1, cols=2)\n",
" \n",
" # Currently HoverTool does not work for step line glyph. See: https://github.com/bokeh/bokeh/issues/7419\n",
" q.add_tools(hover)\n",
" fig.add_trace(\n",
" go.Scatter(x=df1, y=df2, mode=\"markers\", marker=dict(size=3)), row=1, col=1\n",
" )\n",
" fig.update_xaxes(title_text=\"output_nu, ref 1\", row=1, col=1)\n",
" fig.update_yaxes(title_text=\"output_nu, ref 2\", row=1, col=1)\n",
" \n",
" plot = gridplot([p, q], ncols=2, plot_width=420, plot_height=360)\n",
" fig.add_trace(\n",
" go.Histogram(\n",
" x=df1,\n",
" nbinsx=100,\n",
" histfunc=\"count\",\n",
" name=\"ref 1\",\n",
" marker=dict(color='rgba(0,0,0,0)', line=dict(width=1, color='orange')),\n",
" opacity=0.5,\n",
" xbins=dict(start=nu_min, end=nu_max, size=(nu_max - nu_min) / 100),\n",
" ),\n",
" row=1,\n",
" col=2,\n",
" )\n",
" fig.add_trace(\n",
" go.Histogram(\n",
" x=df2,\n",
" nbinsx=100,\n",
" histfunc=\"count\",\n",
" name=\"ref 2\",\n",
" marker=dict(color='rgba(0,0,0,0)', line=dict(width=1, color='blue')),\n",
" xbins=dict(start=nu_min, end=nu_max, size=(nu_max - nu_min) / 100),\n",
" opacity=0.5,\n",
" ),\n",
" row=1,\n",
" col=2,\n",
" )\n",
" fig.update_layout(barmode=\"overlay\", height=550) # Overlay the histograms\n",
" fig.update_xaxes(title_text=\"output_nu\", row=1, col=2)\n",
" \n",
" show(plot)"
" fig.show()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def compare_spectrum(ref1_nu, ref1_lam, ref1_L, ref2_nu, ref2_lam, ref2_L, mpl_backend=False):\n",
" fig = make_subplots(rows=2, cols=2,)\n",
" \n",
" if mpl_backend:\n",
" plt.figure(figsize=(14, 6))\n",
" plt.subplot(121)\n",
" plt.plot(ref1_nu, ref1_L, label=\"ref 1\")\n",
" plt.plot(ref2_nu, ref2_L, label=\"ref 2\")\n",
" plt.xlabel(\"nu\")\n",
" plt.ylabel(\"L\")\n",
" plt.legend(frameon=False)\n",
" plt.subplot(122)\n",
" plt.plot(ref1_nu, ref1_L / ref2_L)\n",
" plt.xlabel(\"nu\")\n",
" plt.ylabel(\"L ref 1 / L ref 2\")\n",
" \n",
" return\n",
" \n",
" \n",
" TOOLTIPS = [(\"(x,y)\", \"(@x, @y)\")]\n",
" hover = HoverTool(tooltips=TOOLTIPS)\n",
" fig.add_trace(go.Scatter(x=ref1_nu, y=ref1_L, mode='lines', name='ref 1', marker=dict(size=2)), row=1, col=1)\n",
" fig.add_trace(go.Scatter(x=ref2_nu, y=ref2_L, mode='lines', name='ref 2', marker=dict(size=2)), row=1, col=1)\n",
" \n",
" p = figure()\n",
" spectrum1 = ColumnDataSource(pd.DataFrame.from_records({'x': ref1_nu.values, \n",
" 'y': ref1_L}))\n",
" spectrum2 = ColumnDataSource(pd.DataFrame.from_records({'x': ref2_nu.values, \n",
" 'y': ref2_L}))\n",
" p.line('x', 'y', source=spectrum1, legend_label='ref 1')\n",
" p.line('x', 'y', source=spectrum2, legend_label='ref 2', color='#ff7f0e')\n",
" p.xaxis.axis_label = \"nu\"\n",
" p.yaxis.axis_label = \"L\"\n",
" p.xaxis.formatter.precision = 1\n",
" p.yaxis.formatter.precision = 1\n",
" p.legend.click_policy=\"hide\"\n",
" p.add_tools(hover)\n",
" fig.add_trace(go.Scatter(x=ref1_nu, y=ref1_L / ref2_L, mode='markers', name='L ref 1 / L ref 2', marker=dict(size=2)), row=1, col=2)\n",
" \n",
" q = figure()\n",
" lum_ratio = ColumnDataSource(pd.DataFrame.from_records({'x': ref1_nu.values, \n",
" 'y': ref1_L.values/ref2_L.values}))\n",
" q.circle('x', 'y', size=1, source=lum_ratio)\n",
" q.xaxis.axis_label = \"nu\"\n",
" q.yaxis.axis_label = \"L ref 1 / L ref 2\"\n",
" q.xaxis.formatter.precision = 1\n",
" q.yaxis.formatter.precision = 1\n",
" q.add_tools(hover)\n",
" fig.add_trace(go.Scatter(x=ref1_lam, y=ref1_L, mode='lines', name='ref 1', marker=dict(size=2)), row=2, col=1)\n",
" fig.add_trace(go.Scatter(x=ref2_lam, y=ref2_L, mode='lines', name='ref 2', marker=dict(size=2)), row=2, col=1)\n",
" \n",
" r = figure()\n",
" spectrum_wav1 = ColumnDataSource(pd.DataFrame.from_records({'x': ref1_lam.values, \n",
" 'y': ref1_L}))\n",
" spectrum_wav2 = ColumnDataSource(pd.DataFrame.from_records({'x': ref2_lam.values, \n",
" 'y': ref2_L}))\n",
" r.line('x', 'y', source=spectrum_wav1, legend_label='ref 1')\n",
" r.line('x', 'y', source=spectrum_wav2, legend_label='ref 2', color='#ff7f0e')\n",
" r.xaxis.axis_label = \"lambda\"\n",
" r.yaxis.axis_label = \"L\"\n",
" r.xaxis.formatter.precision = 1\n",
" r.yaxis.formatter.precision = 1\n",
" r.legend.click_policy=\"hide\"\n",
" r.add_tools(hover)\n",
" fig.add_trace(go.Scatter(x=ref1_lam, y=ref1_L / ref2_L, mode='markers', name='L ref 1 / L ref 2', marker=dict(size=2)), row=2, col=2)\n",
" \n",
" s = figure()\n",
" lum_ratio = ColumnDataSource(pd.DataFrame.from_records({'x': ref1_lam.values, \n",
" 'y': ref1_L.values/ref2_L.values}))\n",
" s.circle('x', 'y', size=1, source=lum_ratio)\n",
" s.xaxis.axis_label = \"lambda\"\n",
" s.yaxis.axis_label = \"L ref 1 / L ref 2\"\n",
" s.xaxis.formatter.precision = 1\n",
" s.yaxis.formatter.precision = 1\n",
" s.add_tools(hover)\n",
" fig.update_xaxes(title_text=\"nu\", row=1, col=1)\n",
" fig.update_yaxes(title_text=\"L\", row=1, col=1)\n",
" fig.update_xaxes(title_text=\"nu\", row=1, col=2)\n",
" fig.update_yaxes(title_text=\"L ref 1 / L ref 2\", row=1, col=2)\n",
" fig.update_xaxes(title_text=\"lambda\", row=2, col=1, tickformat='.2e')\n",
" fig.update_yaxes(title_text=\"L\", row=2, col=1)\n",
" fig.update_xaxes(title_text=\"lambda\", row=2, col=2, tickformat='.2e')\n",
" fig.update_yaxes(title_text=\"L ref 1 / L ref 2\", row=2, col=2)\n",
" fig.update_layout(height=1000)\n",
" \n",
" plot = gridplot([p, q, r, s], ncols=2, plot_width=420, plot_height=360)\n",
" \n",
" show(plot)"
" fig.show()"
]
},
{
Expand All @@ -556,7 +492,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -576,7 +512,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -684,7 +620,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.16"
"version": "3.11.5"
},
"toc": {
"nav_menu": {},
Expand All @@ -698,5 +634,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
Loading

0 comments on commit 6edb0f4

Please sign in to comment.