Skip to content

Commit

Permalink
Update demo.ipynb
Browse files Browse the repository at this point in the history
  • Loading branch information
MOchiara authored Nov 20, 2024
1 parent 55f79ff commit 722614b
Showing 1 changed file with 1 addition and 235 deletions.
236 changes: 1 addition & 235 deletions notebooks/demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,241 +30,7 @@
"- Vertical velocity (from a flight model)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f37f8af1-62b5-4139-9a96-d55142a54e2a",
"metadata": {},
"outputs": [],
"source": [
"import pytest\n",
"from glidertest import fetchers, tools, plots\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import matplotlib\n",
"matplotlib.use('agg') # use agg backend to prevent creating plot windows during tests\n",
"\n",
"def test_plots(start_prof=0, end_prof=100):\n",
" ds = fetchers.load_sample_dataset()\n",
" ds = ds.drop_vars(['DENSITY'])\n",
" fig, ax = plots.plot_basic_vars(ds,start_prof=start_prof, end_prof=end_prof)\n",
" assert ax[0].get_ylabel() == 'Depth (m)'\n",
" assert ax[0].get_xlabel() == f'Average Temperature [C] \\nbetween profile {start_prof} and {end_prof}'\n",
"\n",
"\n",
"def test_up_down_bias(v_res=1, xlabel='Salinity'):\n",
" ds = fetchers.load_sample_dataset()\n",
" fig, ax = plt.subplots()\n",
" df = tools.quant_updown_bias(ds, var='PSAL', v_res=v_res)\n",
" plots.plot_updown_bias(df, ax, xlabel=xlabel)\n",
" lims = np.abs(df.dc)\n",
" assert ax.get_xlim() == (-np.nanpercentile(lims, 99.5), np.nanpercentile(lims, 99.5))\n",
" assert ax.get_ylim() == (df.depth.max() + 1, -df.depth.max() / 30)\n",
" assert ax.get_xlabel() == xlabel\n",
" # check without passing axis\n",
" new_fig, new_ax = plots.plot_updown_bias(df, xlabel=xlabel)\n",
" assert new_ax.get_xlim() == (-np.nanpercentile(lims, 99.5), np.nanpercentile(lims, 99.5))\n",
" assert new_ax.get_ylim() == (df.depth.max() + 1, -df.depth.max() / 30)\n",
" assert new_ax.get_xlabel() == xlabel\n",
"\n",
"\n",
"def test_chl(var1='CHLA', var2='BBP700'):\n",
" ds = fetchers.load_sample_dataset()\n",
" ax = plots.process_optics_assess(ds, var=var1)\n",
" assert ax.get_ylabel() == var1\n",
" ax = plots.process_optics_assess(ds, var=var2)\n",
" assert ax.get_ylabel() == var2\n",
" with pytest.raises(KeyError) as e:\n",
" plots.process_optics_assess(ds, var='nonexistent_variable')\n",
"\n",
"\n",
"def test_quench_sequence(xlabel='Temperature [C]',ylim=45):\n",
" ds = fetchers.load_sample_dataset()\n",
" if not \"TIME\" in ds.indexes.keys():\n",
" ds = ds.set_xindex('TIME')\n",
" fig, ax = plt.subplots()\n",
" plots.plot_quench_assess(ds, 'CHLA', ax,ylim=ylim)\n",
" assert ax.get_ylabel() == 'Depth [m]'\n",
" assert ax.get_ylim() == (ylim, -ylim / 30)\n",
" \n",
" dayT, nightT = tools.compute_daynight_avg(ds, sel_var='TEMP')\n",
" fig, ax = plots.plot_daynight_avg(dayT, nightT,xlabel=xlabel) \n",
" assert ax.get_ylabel() == 'Depth [m]'\n",
" assert ax.get_xlabel() == xlabel\n",
"\n",
"\n",
"def test_temporal_drift(var='DOXY'):\n",
" ds = fetchers.load_sample_dataset()\n",
" fig, ax = plt.subplots(1, 2)\n",
" plots.check_temporal_drift(ds,var, ax)\n",
" assert ax[1].get_ylabel() == 'Depth (m)'\n",
" assert ax[0].get_ylabel() == var\n",
" assert ax[1].get_xlim() == (np.nanpercentile(ds[var], 0.01), np.nanpercentile(ds[var], 99.99))\n",
" plots.check_temporal_drift(ds,'CHLA')\n",
"\n",
"\n",
"def test_profile_check():\n",
" ds = fetchers.load_sample_dataset()\n",
" tools.check_monotony(ds.PROFILE_NUMBER)\n",
" fig, ax = plots.plot_prof_monotony(ds)\n",
" assert ax[0].get_ylabel() == 'Profile number'\n",
" assert ax[1].get_ylabel() == 'Depth (m)'\n",
" duration = tools.compute_prof_duration(ds)\n",
" rolling_mean, overtime = tools.find_outlier_duration(duration, rolling=20, std=2)\n",
" fig, ax = plots.plot_outlier_duration(ds, rolling_mean, overtime, std = 2)\n",
" assert ax[0].get_ylabel() == 'Profile duration (min)'\n",
" assert ax[0].get_xlabel() == 'Profile number'\n",
" assert ax[1].get_ylabel() == 'Depth (m)'\n",
"\n",
"def test_basic_statistics():\n",
" ds = fetchers.load_sample_dataset()\n",
" plots.plot_glider_track(ds)\n",
" plots.plot_grid_spacing(ds)\n",
" plots.plot_ts(ds)\n",
"\n",
"\n",
"def test_vert_vel():\n",
" ds_sg014 = fetchers.load_sample_dataset(dataset_name=\"sg014_20040924T182454_delayed_subset.nc\")\n",
" ds_sg014 = tools.calc_w_meas(ds_sg014)\n",
" ds_sg014 = tools.calc_w_sw(ds_sg014)\n",
" plots.plot_vertical_speeds_with_histograms(ds_sg014)\n",
" ds_dives = ds_sg014.sel(N_MEASUREMENTS=ds_sg014.PHASE == 2)\n",
" ds_climbs = ds_sg014.sel(N_MEASUREMENTS=ds_sg014.PHASE == 1)\n",
" ds_out_dives = tools.quant_binavg(ds_dives, var = 'VERT_CURR_MODEL', dz=10)\n",
" ds_out_climbs = tools.quant_binavg(ds_climbs, var = 'VERT_CURR_MODEL', dz=10)\n",
" plots.plot_combined_velocity_profiles(ds_out_dives, ds_out_climbs)\n",
" # extra tests for ramsey calculations of DEPTH_Z\n",
" ds_climbs = ds_climbs.drop_vars(['DEPTH_Z'])\n",
" tools.quant_binavg(ds_climbs, var='VERT_CURR_MODEL', dz=10)\n",
" ds_climbs = ds_climbs.drop_vars(['LATITUDE'])\n",
" with pytest.raises(KeyError) as e:\n",
" tools.quant_binavg(ds_climbs, var='VERT_CURR_MODEL', dz=10)\n",
"def test_hyst_plot(var='DOXY'):\n",
" ds = fetchers.load_sample_dataset()\n",
" fig, ax = plots.plot_hysteresis(ds, var=var, v_res=1, perct_err=2, ax=None)\n",
" assert ax[3].get_ylabel() == 'Depth (m)'\n",
" assert ax[0].get_ylabel() == 'Depth (m)'\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e48a9acf-2bce-44ca-88cc-6c36fdcbe50f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The thermocline, halocline and pycnocline are located at respectively [60.1], [60.1] and [60.1]m as shown in the plots as well\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\u241346\\uni_hamburg\\glidertest\\glidertest\\plots.py:157: UserWarning: FigureCanvasAgg is non-interactive, and thus cannot be shown\n",
" plt.show()\n",
"C:\\Users\\u241346\\uni_hamburg\\glidertest\\glidertest\\plots.py:59: UserWarning: FigureCanvasAgg is non-interactive, and thus cannot be shown\n",
" plt.show()\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.2% of scaled CHLA data is negative, consider recalibrating data\n"
]
}
],
"source": [
"test_plots(start_prof=0, end_prof=100)\n",
"test_up_down_bias(v_res=1, xlabel='Salinity')\n",
"test_chl(var1='CHLA', var2='BBP700')\n",
"test_quench_sequence(xlabel='Temperature [C]',ylim=45)\n",
"test_temporal_drift(var='DOXY')\n",
"test_profile_check()\n",
"test_basic_statistics()\n",
"test_vert_vel()\n",
"test_hyst_plot(var='DOXY')\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b1a65246-6a20-4f93-8db7-bcaf53e7c3a2",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"sys.path.append('C:\\\\Users\\\\u241346\\\\uni_hamburg\\\\glidertest')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "132a5005-d805-4ab7-8869-ea5579fcdd1f",
"metadata": {},
"outputs": [],
"source": [
"import pytest\n",
"from glidertest import fetchers, tools\n",
"import math\n",
"import numpy as np\n",
"import matplotlib\n",
"matplotlib.use('agg') # use agg backend to prevent creating plot windows during tests\n",
"\n",
"\n",
"def test_updown_bias(v_res=1):\n",
" ds = fetchers.load_sample_dataset()\n",
" df = tools.quant_updown_bias(ds, var='PSAL', v_res=v_res)\n",
" bins = np.unique(np.round(ds.DEPTH,0))\n",
" ncell = math.ceil(len(bins)/v_res)\n",
" assert len(df) == ncell\n",
"\n",
"def test_daynight():\n",
" ds = fetchers.load_sample_dataset()\n",
" if not \"TIME\" in ds.indexes.keys():\n",
" ds = ds.set_xindex('TIME')\n",
"\n",
" dayT, nightT = tools.compute_daynight_avg(ds, sel_var='TEMP')\n",
" assert len(nightT.dat.dropna()) > 0\n",
" assert len(dayT.dat.dropna()) > 0\n",
"\n",
"def test_check_monotony():\n",
" ds = fetchers.load_sample_dataset()\n",
" profile_number_monotony = tools.check_monotony(ds.PROFILE_NUMBER)\n",
" temperature_monotony = tools.check_monotony(ds.TEMP)\n",
" assert profile_number_monotony\n",
" assert not temperature_monotony\n",
" duration = tools.compute_prof_duration(ds)\n",
" rolling_mean, overtime = tools.find_outlier_duration(duration, rolling=20, std=2)\n",
"\n",
"\n",
"def test_vert_vel():\n",
" ds_sg014 = fetchers.load_sample_dataset(dataset_name=\"sg014_20040924T182454_delayed_subset.nc\")\n",
" ds_sg014 = ds_sg014.drop_vars(\"DEPTH_Z\")\n",
" ds_sg014 = tools.calc_w_meas(ds_sg014)\n",
" ds_sg014 = tools.calc_w_sw(ds_sg014)\n",
"\n",
" ds_dives = ds_sg014.sel(N_MEASUREMENTS=ds_sg014.PHASE == 2)\n",
" ds_climbs = ds_sg014.sel(N_MEASUREMENTS=ds_sg014.PHASE == 1)\n",
" tools.quant_binavg(ds_dives, var = 'VERT_CURR_MODEL', dz=10)\n",
" \n",
" # extra tests for ramsey calculations of DEPTH_Z\n",
" ds_climbs = ds_climbs.drop_vars(['DEPTH_Z'])\n",
" tools.quant_binavg(ds_climbs, var='VERT_CURR_MODEL', dz=10)\n",
" ds_climbs = ds_climbs.drop_vars(['LATITUDE'])\n",
" with pytest.raises(KeyError) as e:\n",
" tools.quant_binavg(ds_climbs, var='VERT_CURR_MODEL', dz=10)\n",
"def test_hyst():\n",
" ds = fetchers.load_sample_dataset()\n",
" df_h = tools.quant_hysteresis(ds, var = 'DOXY', v_res = 1)\n",
" df, diff, err, rms = tools.compute_hyst_stat(ds, var='DOXY', v_res=1)\n",
" assert np.array_equal(df_h.dropna(), df.dropna())\n",
" assert len(diff) == len(err)"
]
},

{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit 722614b

Please sign in to comment.