Skip to content

Commit 6c2d013

Browse files
akeestessolson
andauthored
Speed up wave.resource module (MHKiT-Software#352)
* fix assignment in type_handling * temporary testing file * initial conversion of energy_period and frequency_moment to DataArray * energy_period working with variety of types and converting to dataArray base * extend xr.dataarray basis to all wave.resource functions * remove testing script * black formatting * fix most test formatting * use dataarrays instead of datasets in wave.performance * revert surface_elevation function back to datasets * Revert "revert surface_elevation function back to datasets" This reverts commit a2d5f61. * allow datasets, 2d dataframes. Update test formatting appropriately * simplify and improve robustness of convert_to_dataarray for 1-var dataframes and 2+ var datasets * update test formatting * clean up frequency_bin and method checks in elevation_surface * update and annotate type_handling * black formatting * minor type fix * update type references in loads * update type references in loads - v2 * black formatting * fix call to fillna() in MLER example * fix references to k in MLER example * add variable names to Hm0, Te, and Tp for DataFrame creation * update pd.Series naming in examples * fix typo in pacwave notebook * add variable names after data conversion * update pacwave example * add type check to all wave.resource variable naming * update wave example with new data types * pull buoy name from metadata in cdip example * tighten up example timing * address minor review comments, add some type checking tests * complicated dataset and dataframe handling in surface_elevation and elevation_spectrum * restore and simplify dataset input to elevation_spectrum, surface_elevation * black formatting * update missed docstring * use np.nan_to_num correctly in test_loads * Speed up wave (#6) * matplotlib >=3.8 * remove debug --------- Co-authored-by: ssolson <[email protected]>
1 parent 6445365 commit 6c2d013

16 files changed

+3041
-3055
lines changed

.github/workflows/generate_notebook_matrix.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
"cdip_example.ipynb": 420,
1515
"Delft3D_example.ipynb": 180,
1616
"directional_waves.ipynb": 180,
17-
"environmental_contours_example.ipynb": 360,
18-
"extreme_response_contour_example.ipynb": 360,
19-
"extreme_response_full_sea_state_example.ipynb": 360,
20-
"extreme_response_MLER_example.ipynb": 360,
17+
"environmental_contours_example.ipynb": 240,
18+
"extreme_response_contour_example.ipynb": 240,
19+
"extreme_response_full_sea_state_example.ipynb": 240,
20+
"extreme_response_MLER_example.ipynb": 240,
2121
"loads_example.ipynb": 180,
2222
"metocean_example.ipynb": 180,
2323
"mooring_example.ipynb": 240,
24-
"PacWave_resource_characterization_example.ipynb": 780,
24+
"PacWave_resource_characterization_example.ipynb": 240,
2525
"power_example.ipynb": 180,
2626
"qc_example.ipynb": 180,
2727
"river_example.ipynb": 180,

examples/PacWave_resource_characterization_example.ipynb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,12 +1119,19 @@
11191119
"\n",
11201120
" Tz_list.append(resource.average_zero_crossing_period(year_data.T))\n",
11211121
"\n",
1122-
"# Concatenate list of Series into a single DataFrame\n",
1122+
"# Concatenate each list of Series into a single Series\n",
11231123
"Te = pd.concat(Te_list, axis=0)\n",
11241124
"Tp = pd.concat(Tp_list, axis=0)\n",
11251125
"Hm0 = pd.concat(Hm0_list, axis=0)\n",
11261126
"J = pd.concat(J_list, axis=0)\n",
11271127
"Tz = pd.concat(Tz_list, axis=0)\n",
1128+
"\n",
1129+
"# Name each Series and concat into a dataFrame\n",
1130+
"Te.name = 'Te'\n",
1131+
"Tp.name = 'Tp'\n",
1132+
"Hm0.name = 'Hm0'\n",
1133+
"J.name = 'J'\n",
1134+
"Tz.name = 'Tz'\n",
11281135
"data = pd.concat([Hm0, Te, Tp, J, Tz], axis=1)\n",
11291136
"\n",
11301137
"# Calculate wave steepness\n",
@@ -1590,7 +1597,7 @@
15901597
" J = []\n",
15911598
" for i in range(len(result)):\n",
15921599
" b = resource.jonswap_spectrum(f, result.Tp[i], result.Hm0[i])\n",
1593-
" J.extend([resource.energy_flux(b, h=399.0).values[0][0]])\n",
1600+
" J.extend([resource.energy_flux(b, h=399.0).item()])\n",
15941601
"\n",
15951602
" result[\"J\"] = J\n",
15961603
" results[N] = result\n",
@@ -1622,7 +1629,7 @@
16221629
"name": "python",
16231630
"nbconvert_exporter": "python",
16241631
"pygments_lexer": "ipython3",
1625-
"version": "3.11.7"
1632+
"version": "3.12.7"
16261633
}
16271634
},
16281635
"nbformat": 4,

examples/cdip_example.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@
576576
"Hs = buoy_data[\"data\"][\"wave\"][\"waveHs\"]\n",
577577
"Tp = buoy_data[\"data\"][\"wave\"][\"waveTp\"]\n",
578578
"Dp = buoy_data[\"data\"][\"wave\"][\"waveDp\"]\n",
579-
"buoy_name = buoy_data[\"data\"][\"wave\"].name\n",
579+
"buoy_name = buoy_data[\"metadata\"][\"name\"]\n",
580580
"ax = graphics.plot_compendium(Hs, Tp, Dp, buoy_name)"
581581
]
582582
},
@@ -590,7 +590,7 @@
590590
],
591591
"metadata": {
592592
"kernelspec": {
593-
"display_name": "Python 3",
593+
"display_name": "Python 3 (ipykernel)",
594594
"language": "python",
595595
"name": "python3"
596596
},
@@ -604,7 +604,7 @@
604604
"name": "python",
605605
"nbconvert_exporter": "python",
606606
"pygments_lexer": "ipython3",
607-
"version": "3.11.7"
607+
"version": "3.12.7"
608608
}
609609
},
610610
"nbformat": 4,

examples/environmental_contours_example.ipynb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -647,9 +647,13 @@
647647
" Hm0_list.append(resource.significant_wave_height(year_data.T))\n",
648648
" Te_list.append(resource.energy_period(year_data.T))\n",
649649
"\n",
650-
"# Concatenate list of Series into a single DataFrame\n",
650+
"# Concatenate each list of Series into a single Series\n",
651651
"Te = pd.concat(Te_list, axis=0)\n",
652652
"Hm0 = pd.concat(Hm0_list, axis=0)\n",
653+
"\n",
654+
"# Name each Series and concat into a dataFrame\n",
655+
"Te.name = 'Te'\n",
656+
"Hm0.name = 'Hm0'\n",
653657
"Hm0_Te = pd.concat([Hm0, Te], axis=1)\n",
654658
"\n",
655659
"# Drop any NaNs created from the calculation of Hm0 or Te\n",
@@ -800,7 +804,7 @@
800804
"source": [
801805
"## Resource Clusters\n",
802806
"\n",
803-
"Often in resource characterization we want to pick a few representative sea state to run an alaysis. To do this with the resource data in python we reccomend using a Gaussian Mixture Model (a more generalized k-means clustering method). Using sckitlearn this is very straigth forward. We combine our Hm0 and Te data into an N x 2 numpy array. We specify our number of components (number of representative sea states) and then call the fit method on the data. Fianlly, using the methods `means_` and `weights` we can organize the results into an easily digestable table."
807+
"Often in resource characterization we want to pick a few representative sea state to run an alaysis. To do this with the resource data in python we reccomend using a Gaussian Mixture Model (a more generalized k-means clustering method). Using sckitlearn this is very straight forward. We combine our Hm0 and Te data into an N x 2 numpy array. We specify our number of components (number of representative sea states) and then call the fit method on the data. Fianlly, using the methods `means_` and `weights` we can organize the results into an easily digestable table."
804808
]
805809
},
806810
{
@@ -933,9 +937,13 @@
933937
" Hm0_list.append(resource.significant_wave_height(year_data.T))\n",
934938
" Tp_list.append(resource.peak_period(year_data.T))\n",
935939
"\n",
936-
"# Concatenate list of Series into a single DataFrame\n",
940+
"# Concatenate each list of Series into a single Series\n",
937941
"Tp = pd.concat(Tp_list, axis=0)\n",
938942
"Hm0 = pd.concat(Hm0_list, axis=0)\n",
943+
"\n",
944+
"# Name each Series and concat into a dataFrame\n",
945+
"Tp.name = 'Tp'\n",
946+
"Hm0.name = 'Hm0'\n",
939947
"Hm0_Tp = pd.concat([Hm0, Tp], axis=1)\n",
940948
"\n",
941949
"# Drop any NaNs created from the calculation of Hm0 or Te\n",
@@ -1116,7 +1124,7 @@
11161124
],
11171125
"metadata": {
11181126
"kernelspec": {
1119-
"display_name": "Python 3.9.13 ('.venv': venv)",
1127+
"display_name": "Python 3 (ipykernel)",
11201128
"language": "python",
11211129
"name": "python3"
11221130
},
@@ -1130,7 +1138,7 @@
11301138
"name": "python",
11311139
"nbconvert_exporter": "python",
11321140
"pygments_lexer": "ipython3",
1133-
"version": "3.11.7"
1141+
"version": "3.12.4"
11341142
},
11351143
"vscode": {
11361144
"interpreter": {

examples/extreme_response_MLER_example.ipynb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,11 @@
197197
"\n",
198198
"# generate wave number k\n",
199199
"k = resource.wave_number(wave_freq, 70)\n",
200-
"k = k.fillna(0)\n",
200+
"np.nan_to_num(k, 0)\n",
201201
"\n",
202202
"peakHeightDesired = Hs / 2 * 1.9\n",
203203
"mler_norm = extreme.mler_wave_amp_normalize(\n",
204-
" peakHeightDesired, mler_data, sim, k.k.values\n",
204+
" peakHeightDesired, mler_data, sim, k\n",
205205
")"
206206
]
207207
},
@@ -239,7 +239,7 @@
239239
}
240240
],
241241
"source": [
242-
"mler_ts = extreme.mler_export_time_series(RAO.values, mler_norm, sim, k.k.values)\n",
242+
"mler_ts = extreme.mler_export_time_series(RAO.values, mler_norm, sim, k)\n",
243243
"mler_ts.plot(xlabel=\"Time (s)\", ylabel=\"[m] / [*]\", xlim=[-100, 100], grid=True)"
244244
]
245245
},
@@ -256,7 +256,7 @@
256256
"hash": "6acc4428af86beefd6565514d05fe9ce8e024621fafadd3627cdac7b7bd68bc4"
257257
},
258258
"kernelspec": {
259-
"display_name": "Python 3.8.10 64-bit ('MHKdev': conda)",
259+
"display_name": "Python 3 (ipykernel)",
260260
"language": "python",
261261
"name": "python3"
262262
},
@@ -270,10 +270,9 @@
270270
"name": "python",
271271
"nbconvert_exporter": "python",
272272
"pygments_lexer": "ipython3",
273-
"version": "3.11.7"
274-
},
275-
"orig_nbformat": 4
273+
"version": "3.12.4"
274+
}
276275
},
277276
"nbformat": 4,
278-
"nbformat_minor": 2
277+
"nbformat_minor": 4
279278
}

examples/extreme_response_contour_example.ipynb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,13 @@
7272
" Hm0_list.append(resource.significant_wave_height(year_data.T))\n",
7373
" Te_list.append(resource.energy_period(year_data.T))\n",
7474
"\n",
75-
"# Concatenate list of Series into a single DataFrame\n",
75+
"# Concatenate each list of Series into a single Series\n",
7676
"Te = pd.concat(Te_list, axis=0)\n",
7777
"Hm0 = pd.concat(Hm0_list, axis=0)\n",
78+
"\n",
79+
"# Name each Series and concat into a dataFrame\n",
80+
"Te.name = 'Te'\n",
81+
"Hm0.name = 'Hm0'\n",
7882
"Hm0_Te = pd.concat([Hm0, Te], axis=1)\n",
7983
"\n",
8084
"# Drop any NaNs created from the calculation of Hm0 or Te\n",
@@ -323,7 +327,7 @@
323327
"name": "python",
324328
"nbconvert_exporter": "python",
325329
"pygments_lexer": "ipython3",
326-
"version": "3.11.7"
330+
"version": "3.12.4"
327331
}
328332
},
329333
"nbformat": 4,

examples/extreme_response_full_sea_state_example.ipynb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,13 @@
7575
" Hm0_list.append(resource.significant_wave_height(year_data.T))\n",
7676
" Te_list.append(resource.energy_period(year_data.T))\n",
7777
"\n",
78-
"# Concatenate list of Series into a single DataFrame\n",
78+
"# Concatenate each list of Series into a single Series\n",
7979
"Te = pd.concat(Te_list, axis=0)\n",
8080
"Hm0 = pd.concat(Hm0_list, axis=0)\n",
81+
"\n",
82+
"# Name each Series and concat into a dataFrame\n",
83+
"Te.name = 'Te'\n",
84+
"Hm0.name = 'Hm0'\n",
8185
"Hm0_Te = pd.concat([Hm0, Te], axis=1)\n",
8286
"\n",
8387
"# Drop any NaNs created from the calculation of Hm0 or Te\n",
@@ -573,7 +577,7 @@
573577
"name": "python",
574578
"nbconvert_exporter": "python",
575579
"pygments_lexer": "ipython3",
576-
"version": "3.11.7"
580+
"version": "3.12.4"
577581
}
578582
},
579583
"nbformat": 4,

0 commit comments

Comments
 (0)