wavespectra 4.5.0__tar.gz → 4.6.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. {wavespectra-4.5.0/wavespectra.egg-info → wavespectra-4.6.0}/PKG-INFO +12 -9
  2. {wavespectra-4.5.0 → wavespectra-4.6.0}/README.rst +9 -7
  3. {wavespectra-4.5.0 → wavespectra-4.6.0}/pyproject.toml +43 -26
  4. wavespectra-4.6.0/setup.cfg +4 -0
  5. wavespectra-4.6.0/tests/test_combine_hp01.py +217 -0
  6. {wavespectra-4.5.0 → wavespectra-4.6.0}/tests/test_cookiecutter_template.py +0 -6
  7. wavespectra-4.6.0/tests/test_partition.py +596 -0
  8. wavespectra-4.6.0/tests/test_reader_registry.py +86 -0
  9. {wavespectra-4.5.0 → wavespectra-4.6.0}/tests/test_specarray.py +4 -7
  10. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/__init__.py +15 -10
  11. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/cli.py +0 -2
  12. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/awac.py +20 -0
  13. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/dataset.py +2 -2
  14. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/datawell.py +4 -2
  15. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/era5.py +1 -1
  16. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/obscape.py +20 -0
  17. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/swan.py +4 -0
  18. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/output/funwave.py +6 -2
  19. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/output/netcdf.py +8 -1
  20. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/output/octopus.py +0 -1
  21. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/output/ww3.py +9 -6
  22. wavespectra-4.6.0/wavespectra/partition/hanson_and_phillips_2001.py +369 -0
  23. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/partition/partition.py +397 -389
  24. wavespectra-4.6.0/wavespectra/partition/specpart/specpart.c +305 -0
  25. wavespectra-4.6.0/wavespectra/partition/specpart/specpart.h +25 -0
  26. wavespectra-4.6.0/wavespectra/partition/specpart/specpart_wrap.c +98 -0
  27. wavespectra-4.6.0/wavespectra/partition/tracking.py +484 -0
  28. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/specarray.py +2 -4
  29. {wavespectra-4.5.0 → wavespectra-4.6.0/wavespectra.egg-info}/PKG-INFO +12 -9
  30. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra.egg-info/SOURCES.txt +2 -1
  31. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra.egg-info/entry_points.txt +3 -0
  32. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra.egg-info/requires.txt +2 -1
  33. wavespectra-4.5.0/setup.cfg +0 -44
  34. wavespectra-4.5.0/tests/test_partition.py +0 -397
  35. wavespectra-4.5.0/wavespectra/partition/hanson_and_phillips_2001.py +0 -295
  36. wavespectra-4.5.0/wavespectra/partition/specpart/specpart.c +0 -489
  37. wavespectra-4.5.0/wavespectra/partition/specpart/specpart.h +0 -5
  38. wavespectra-4.5.0/wavespectra/partition/specpart/specpart_wrap.c +0 -61
  39. wavespectra-4.5.0/wavespectra/partition/tracking.py +0 -356
  40. {wavespectra-4.5.0 → wavespectra-4.6.0}/AUTHORS.rst +0 -0
  41. {wavespectra-4.5.0 → wavespectra-4.6.0}/LICENSE.txt +0 -0
  42. {wavespectra-4.5.0 → wavespectra-4.6.0}/setup.py +0 -0
  43. {wavespectra-4.5.0 → wavespectra-4.6.0}/tests/test_cli.py +0 -0
  44. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/construct/__init__.py +0 -0
  45. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/construct/direction.py +0 -0
  46. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/construct/frequency.py +0 -0
  47. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/core/__init__.py +0 -0
  48. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/core/attributes.py +0 -0
  49. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/core/attributes.yml +0 -0
  50. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/core/fitting.py +0 -0
  51. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/core/npstats.py +0 -0
  52. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/core/select.py +0 -0
  53. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/core/swan.py +0 -0
  54. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/core/utils.py +0 -0
  55. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/core/xrstats.py +0 -0
  56. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/__init__.py +0 -0
  57. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/funwave.py +0 -0
  58. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/funwave_new.py +0 -0
  59. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/json.py +0 -0
  60. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/ncswan.py +0 -0
  61. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/ndbc.py +0 -0
  62. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/ndbc_ascii.py +0 -0
  63. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/netcdf.py +0 -0
  64. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/octopus.py +0 -0
  65. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/spotter.py +0 -0
  66. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/triaxys.py +0 -0
  67. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/wavespectra.py +0 -0
  68. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/ww3.py +0 -0
  69. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/ww3_station.py +0 -0
  70. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/wwm.py +0 -0
  71. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/input/xwaves.py +0 -0
  72. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/output/__init__.py +0 -0
  73. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/output/json.py +0 -0
  74. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/output/orcaflex.py +0 -0
  75. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/output/swan.py +0 -0
  76. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/output/ww3.yml +0 -0
  77. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/partition/__init__.py +0 -0
  78. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/plot.py +0 -0
  79. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra/specdataset.py +0 -0
  80. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra.egg-info/dependency_links.txt +0 -0
  81. {wavespectra-4.5.0 → wavespectra-4.6.0}/wavespectra.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: wavespectra
3
- Version: 4.5.0
3
+ Version: 4.6.0
4
4
  Summary: Library for ocean wave spectra
5
5
  Author-email: Wavespectra Developers <r.guedes@oceanum.science>
6
6
  Maintainer-email: Rafael Guedes <r.guedes@oceanum.science>, Tom Durrant <t.durrant@oceanum.science>
@@ -56,7 +56,8 @@ Requires-Dist: scipy
56
56
  Requires-Dist: xarray
57
57
  Provides-Extra: test
58
58
  Requires-Dist: pytest; extra == "test"
59
- Requires-Dist: tox; extra == "test"
59
+ Requires-Dist: pytest-cov; extra == "test"
60
+ Requires-Dist: pytest-timeout; extra == "test"
60
61
  Requires-Dist: ruff; extra == "test"
61
62
  Provides-Extra: extra
62
63
  Requires-Dist: fsspec; extra == "extra"
@@ -90,12 +91,12 @@ wavespectra
90
91
  :target: https://zenodo.org/badge/latestdoi/205463939
91
92
  :alt: DOI
92
93
 
93
- .. image:: https://img.shields.io/github/actions/workflow/status/wavespectra/wavespectra/python-publish.yml
94
+ .. image:: https://img.shields.io/github/actions/workflow/status/wavespectra/wavespectra/testing.yml?branch=main
94
95
  :target: https://github.com/wavespectra/wavespectra/actions
95
96
  :alt: Build Status
96
97
 
97
- .. image:: https://coveralls.io/repos/github/wavespectra/wavespectra/badge.svg?branch=master
98
- :target: https://coveralls.io/github/wavespectra/wavespectra?branch=master
98
+ .. image:: https://coveralls.io/repos/github/wavespectra/wavespectra/badge.svg?branch=main
99
+ :target: https://coveralls.io/github/wavespectra/wavespectra?branch=main
99
100
  :alt: Coverage
100
101
 
101
102
  .. image:: https://readthedocs.org/projects/wavespectra/badge/?version=latest
@@ -130,7 +131,7 @@ Key Features
130
131
  - **Unified Data Model**: Built on xarray with standardised conventions for wave spectral data
131
132
  - **Extensive I/O Support**: Read/write 15+ formats including WW3, SWAN, ERA5, NDBC, and more
132
133
  - **Rich Analysis Tools**: 60+ methods for wave parameter calculation and spectral transformations
133
- - **Spectral Partitioning**: Separate wind sea and swell using multiple algorithms (PTM1-5, watershed, wave age)
134
+ - **Spectral Partitioning**: Separate wind sea and swell using multiple algorithms (PTM1-PTM5, HP01, wave age)
134
135
  - **Spectral Construction**: Create synthetic spectra using parametric forms (JONSWAP, TMA, Gaussian, Pierson-Moskowitz)
135
136
  - **Flexible Visualisation**: Polar spectral plots with matplotlib integration
136
137
  - **High Performance**: Leverages dask for efficient processing of large datasets
@@ -167,7 +168,7 @@ Basic Usage
167
168
  from wavespectra import read_swan
168
169
 
169
170
  # Read wave spectra from various formats
170
- dset = read_swan("spectra.swn") # SWAN format
171
+ dset = read_swan("spectra.spec") # SWAN format
171
172
  # dset = xr.open_dataset("era5.nc", engine="era5") # ERA5 reanalysis
172
173
  # dset = xr.open_dataset("ww3.nc", engine="ww3") # WAVEWATCH III
173
174
 
@@ -244,6 +245,8 @@ Create synthetic spectra from parametric forms:
244
245
 
245
246
  .. code-block:: python
246
247
 
248
+ import numpy as np
249
+
247
250
  from wavespectra.construct.frequency import jonswap, tma, gaussian
248
251
  from wavespectra.construct.direction import cartwright
249
252
  from wavespectra.construct import construct_partition
@@ -314,8 +317,8 @@ Input and Output Formats
314
317
 
315
318
  - **Wave Models**: WAVEWATCH III, SWAN, WWM, FUNWAVE, OrcaFlex
316
319
  - **Reanalysis**: ERA5, ERA-Interim, ECMWF
317
- - **Observations**: NDBC, TRIAXYS, Spotter, Octopus, AWAC
318
- - **Generic**: NetCDF, JSON, CSV
320
+ - **Observations**: NDBC, TRIAXYS, Spotter, Datawell, Obscape, AWAC, Octopus
321
+ - **Generic**: NetCDF, Zarr, JSON
319
322
 
320
323
  Documentation
321
324
  =============
@@ -10,12 +10,12 @@ wavespectra
10
10
  :target: https://zenodo.org/badge/latestdoi/205463939
11
11
  :alt: DOI
12
12
 
13
- .. image:: https://img.shields.io/github/actions/workflow/status/wavespectra/wavespectra/python-publish.yml
13
+ .. image:: https://img.shields.io/github/actions/workflow/status/wavespectra/wavespectra/testing.yml?branch=main
14
14
  :target: https://github.com/wavespectra/wavespectra/actions
15
15
  :alt: Build Status
16
16
 
17
- .. image:: https://coveralls.io/repos/github/wavespectra/wavespectra/badge.svg?branch=master
18
- :target: https://coveralls.io/github/wavespectra/wavespectra?branch=master
17
+ .. image:: https://coveralls.io/repos/github/wavespectra/wavespectra/badge.svg?branch=main
18
+ :target: https://coveralls.io/github/wavespectra/wavespectra?branch=main
19
19
  :alt: Coverage
20
20
 
21
21
  .. image:: https://readthedocs.org/projects/wavespectra/badge/?version=latest
@@ -50,7 +50,7 @@ Key Features
50
50
  - **Unified Data Model**: Built on xarray with standardised conventions for wave spectral data
51
51
  - **Extensive I/O Support**: Read/write 15+ formats including WW3, SWAN, ERA5, NDBC, and more
52
52
  - **Rich Analysis Tools**: 60+ methods for wave parameter calculation and spectral transformations
53
- - **Spectral Partitioning**: Separate wind sea and swell using multiple algorithms (PTM1-5, watershed, wave age)
53
+ - **Spectral Partitioning**: Separate wind sea and swell using multiple algorithms (PTM1-PTM5, HP01, wave age)
54
54
  - **Spectral Construction**: Create synthetic spectra using parametric forms (JONSWAP, TMA, Gaussian, Pierson-Moskowitz)
55
55
  - **Flexible Visualisation**: Polar spectral plots with matplotlib integration
56
56
  - **High Performance**: Leverages dask for efficient processing of large datasets
@@ -87,7 +87,7 @@ Basic Usage
87
87
  from wavespectra import read_swan
88
88
 
89
89
  # Read wave spectra from various formats
90
- dset = read_swan("spectra.swn") # SWAN format
90
+ dset = read_swan("spectra.spec") # SWAN format
91
91
  # dset = xr.open_dataset("era5.nc", engine="era5") # ERA5 reanalysis
92
92
  # dset = xr.open_dataset("ww3.nc", engine="ww3") # WAVEWATCH III
93
93
 
@@ -164,6 +164,8 @@ Create synthetic spectra from parametric forms:
164
164
 
165
165
  .. code-block:: python
166
166
 
167
+ import numpy as np
168
+
167
169
  from wavespectra.construct.frequency import jonswap, tma, gaussian
168
170
  from wavespectra.construct.direction import cartwright
169
171
  from wavespectra.construct import construct_partition
@@ -234,8 +236,8 @@ Input and Output Formats
234
236
 
235
237
  - **Wave Models**: WAVEWATCH III, SWAN, WWM, FUNWAVE, OrcaFlex
236
238
  - **Reanalysis**: ERA5, ERA-Interim, ECMWF
237
- - **Observations**: NDBC, TRIAXYS, Spotter, Octopus, AWAC
238
- - **Generic**: NetCDF, JSON, CSV
239
+ - **Observations**: NDBC, TRIAXYS, Spotter, Datawell, Obscape, AWAC, Octopus
240
+ - **Generic**: NetCDF, Zarr, JSON
239
241
 
240
242
  Documentation
241
243
  =============
@@ -54,6 +54,8 @@ dynamic = ["version"]
54
54
  wavespectra = "wavespectra.cli:main"
55
55
 
56
56
  [project.entry-points."xarray.backends"]
57
+ awac = "wavespectra.input.awac:AWACBackendEntrypoint"
58
+ datawell = "wavespectra.input.datawell:DatawellBackendEntrypoint"
57
59
  era5 = "wavespectra.input.era5:ERA5BackendEntrypoint"
58
60
  funwave = "wavespectra.input.funwave:FunwaveBackendEntrypoint"
59
61
  funwave_new = "wavespectra.input.funwave_new:FunwaveNewBackendEntrypoint"
@@ -62,6 +64,7 @@ ncswan = "wavespectra.input.ncswan:NCSwanBackendEntrypoint"
62
64
  ndbc = "wavespectra.input.ndbc:NDBCBackendEntrypoint"
63
65
  ndbc_ascii = "wavespectra.input.ndbc_ascii:NDBCASCIIBackendEntrypoint"
64
66
  netcdf = "wavespectra.input.netcdf:NetCDFBackendEntrypoint"
67
+ obscape = "wavespectra.input.obscape:ObscapeBackendEntrypoint"
65
68
  octopus = "wavespectra.input.octopus:OctopusBackendEntrypoint"
66
69
  spotter = "wavespectra.input.spotter:SpotterBackendEntrypoint"
67
70
  swan = "wavespectra.input.swan:SWANBackendEntrypoint"
@@ -73,7 +76,7 @@ ww3_station = "wavespectra.input.ww3_station:WW3StationBackendEntrypoint"
73
76
  xwaves = "wavespectra.input.xwaves:XWavesBackendEntrypoint"
74
77
 
75
78
  [project.optional-dependencies]
76
- test = ["pytest", "tox", "ruff"]
79
+ test = ["pytest", "pytest-cov", "pytest-timeout", "ruff"]
77
80
  extra = [
78
81
  "fsspec",
79
82
  "gcsfs",
@@ -95,6 +98,11 @@ docs = [
95
98
  "sphinx-gallery",
96
99
  ]
97
100
 
101
+ [tool.setuptools.packages.find]
102
+ # Explicit include so stray top-level directories created at build time
103
+ # (e.g. cibuildwheel's wheelhouse/) don't break flat-layout auto-discovery.
104
+ include = ["wavespectra*"]
105
+
98
106
  [tool.setuptools.package-data]
99
107
  "wavespectra.core" = ["*.yml"]
100
108
  "wavespectra.output" = ["*.yml"]
@@ -103,6 +111,29 @@ docs = [
103
111
  [tool.setuptools.dynamic]
104
112
  version = {attr = "wavespectra.__version__"}
105
113
 
114
+ [tool.cibuildwheel]
115
+ # CPython versions matching the supported range in the classifiers.
116
+ build = ["cp39-*", "cp310-*", "cp311-*", "cp312-*", "cp313-*"]
117
+ # 64-bit only: 32-bit Windows wheels would be built by default, but core
118
+ # dependencies (e.g. matplotlib) no longer ship win32 wheels to test against.
119
+ archs = ["auto64"]
120
+ # Skip musl-based linux; can be added later if there is demand.
121
+ skip = ["*-musllinux*"]
122
+ # Test every built wheel by running the partition test module, which
123
+ # exercises the compiled specpart extension end to end.
124
+ test-requires = ["pytest", "netCDF4"]
125
+ test-command = "pytest {project}/tests/test_partition.py -q"
126
+ build-verbosity = 1
127
+
128
+ [tool.pytest.ini_options]
129
+ norecursedirs = [".*", "build", "dist", "CVS", "_darcs", "{arch}", "*.egg", "venv", "broken"]
130
+ filterwarnings = [
131
+ # netCDF4-python sets the .shape attribute on numpy arrays, which numpy
132
+ # 2.5 deprecated; it surfaces through xarray's netCDF4 backend whenever a
133
+ # netCDF file is written. Third-party issue, nothing to fix in wavespectra.
134
+ "ignore:Setting the shape on a NumPy array has been deprecated:DeprecationWarning",
135
+ ]
136
+
106
137
  [tool.ruff]
107
138
  line-length = 88
108
139
 
@@ -111,6 +142,11 @@ quote-style = "double"
111
142
  indent-style = "space"
112
143
  line-ending = "auto"
113
144
 
145
+ [tool.ruff.lint]
146
+ # B015/B018: flag comparisons/expressions whose result is discarded, which in
147
+ # tests almost always means a forgotten `assert`.
148
+ extend-select = ["B015", "B018"]
149
+
114
150
  [tool.ruff.lint.extend-per-file-ignores]
115
151
  "wavespectra/__init__.py" = ["F401"]
116
152
  "wavespectra/specdataset.py" = ["F401"]
@@ -122,11 +158,11 @@ line-ending = "auto"
122
158
  "wavespectra/input/spotter.py" = ["F401"]
123
159
  "wavespectra/input/dataset.py" = ["F401"]
124
160
  "wavespectra/input/swan.py" = ["F401"]
125
- "wavespectra/construct.py" = ["F401"]
126
- "wavespectra/directional.py" = ["F401"]
127
- "wavespectra/fit/pierson_moskowitz.py" = ["F401"]
128
- "wavespectra/fit/tma.py" = ["F401"]
161
+ "wavespectra/construct/__init__.py" = ["F401"]
162
+ "wavespectra/construct/direction.py" = ["F401"]
163
+ "wavespectra/construct/frequency.py" = ["F401"]
129
164
  "wavespectra/input/funwave.py" = ["F401"]
165
+ "wavespectra/input/funwave_new.py" = ["F401"]
130
166
  "wavespectra/input/ndbc.py" = ["F401"]
131
167
  "wavespectra/input/ndbc_ascii.py" = ["F401"]
132
168
  "wavespectra/input/octopus.py" = ["F401"]
@@ -137,7 +173,7 @@ line-ending = "auto"
137
173
  github_url = "https://github.com/wavespectra/wavespectra/"
138
174
 
139
175
  [tool.tbump.version]
140
- current = "4.4.3"
176
+ current = "4.6.0"
141
177
  regex = '''
142
178
  (?P<major>\d+)
143
179
  \.(?P<minor>\d+)
@@ -149,23 +185,4 @@ message_template = "Bump to {new_version}"
149
185
  tag_template = "v{new_version}"
150
186
 
151
187
  [[tool.tbump.file]]
152
- src = "wavespectra/__init__.py"
153
-
154
- [tool.tox]
155
- legacy_tox_ini = """
156
- [tox]
157
- min_version = 4.0
158
- env_list =
159
- py38
160
- py39
161
- py310
162
-
163
- [testenv]
164
- deps = wavespectra[extra, test]
165
- commands = pytest tests
166
-
167
- [testenv:flake8]
168
- basepython = python
169
- deps = flake8
170
- commands = flake8 wavespectra
171
- """
188
+ src = "wavespectra/__init__.py"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,217 @@
1
+ """Tests for the Hanson and Phillips (2001) partition combining algorithm."""
2
+
3
+ from pathlib import Path
4
+
5
+ import numpy as np
6
+ import pytest
7
+
8
+ from wavespectra import read_datawell
9
+ from wavespectra.core import npstats
10
+ from wavespectra.partition import specpart
11
+ from wavespectra.partition.hanson_and_phillips_2001 import (
12
+ _adjacency_saddles,
13
+ _label_map,
14
+ combine_partitions_hp01,
15
+ )
16
+
17
+
18
+ HERE = Path(__file__).parent
19
+
20
+ FREQ = np.arange(0.04, 0.4, 0.01)
21
+ DIR = np.arange(0, 360, 10.0)
22
+
23
+
24
+ def _gaussian_system(fp, dp, hs, fspread=0.02, dspread=20.0):
25
+ """Synthetic Gaussian wave system in frequency-direction space."""
26
+ f = FREQ[:, None]
27
+ d = DIR[None, :]
28
+ ddiff = np.minimum(np.abs(d - dp), 360 - np.abs(d - dp))
29
+ spec = np.exp(-0.5 * ((f - fp) / fspread) ** 2 - 0.5 * (ddiff / dspread) ** 2)
30
+ e = (spec * np.gradient(FREQ)[:, None] * (DIR[1] - DIR[0])).sum()
31
+ return spec * (hs / 4) ** 2 / e
32
+
33
+
34
+ def _split(spectrum, ihmax=100):
35
+ """Split a spectrum into its watershed partitions."""
36
+ wmap = specpart.partition(spectrum.astype("float32"), ihmax)
37
+ return [np.where(wmap == ip, spectrum, 0.0) for ip in range(1, wmap.max() + 1)]
38
+
39
+
40
+ def _hs(spectrum):
41
+ return npstats.hs(spectrum, FREQ, DIR)
42
+
43
+
44
+ def _keys(partitions):
45
+ """Hashable identifiers of combined partitions, independent of order."""
46
+ return {tuple(np.flatnonzero(p.ravel())) for p in partitions}
47
+
48
+
49
+ @pytest.fixture
50
+ def noisy_two_systems():
51
+ """Two distinct systems plus noise that over-segments the watershed."""
52
+ rng = np.random.default_rng(42)
53
+ spec = _gaussian_system(0.08, 270, 2.0) + _gaussian_system(0.20, 90, 1.5)
54
+ spec *= rng.uniform(0.5, 1.5, spec.shape)
55
+ return spec
56
+
57
+
58
+ class TestAdjacencySaddles:
59
+ def test_adjacent_partitions_have_saddle(self):
60
+ spec = _gaussian_system(0.08, 180, 2.0) + _gaussian_system(0.14, 180, 1.5)
61
+ parts = _split(spec)
62
+ assert len(parts) == 2
63
+ lmap = _label_map(np.array(parts))
64
+ saddles = _adjacency_saddles(lmap, spec, len(parts))
65
+ assert saddles[1, 2] > 0
66
+ assert saddles[1, 2] == saddles[2, 1]
67
+ # The saddle between the two mutual peaks is a large fraction of the
68
+ # smaller peak density
69
+ assert saddles[1, 2] / min(p.max() for p in parts) > 0.5
70
+
71
+ def test_distant_partitions_low_saddle(self):
72
+ spec = _gaussian_system(0.06, 270, 2.0, 0.01, 10) + _gaussian_system(
73
+ 0.3, 90, 1.5, 0.01, 10
74
+ )
75
+ parts = _split(spec)
76
+ lmap = _label_map(np.array(parts))
77
+ saddles = _adjacency_saddles(lmap, spec, len(parts))
78
+ # Basins touch only through near-zero tails so the saddle is
79
+ # negligible relative to the peaks
80
+ assert saddles[1, 2] / min(p.max() for p in parts) < 1e-3
81
+
82
+ def test_wrap_across_direction_axis(self):
83
+ lmap = np.zeros((3, 4), dtype=int)
84
+ lmap[:, 0] = 1
85
+ lmap[:, -1] = 2
86
+ spec = np.ones((3, 4))
87
+ saddles = _adjacency_saddles(lmap, spec, 2)
88
+ assert saddles[1, 2] == 1
89
+
90
+
91
+ class TestCombine:
92
+ def test_mutual_partitions_combined(self, noisy_two_systems):
93
+ parts = _split(noisy_two_systems)
94
+ assert len(parts) > 2 # noise over-segments
95
+ combined = combine_partitions_hp01(parts, FREQ, DIR)
96
+ assert len(combined) == 2
97
+
98
+ def test_distinct_systems_not_combined(self, noisy_two_systems):
99
+ parts = _split(noisy_two_systems)
100
+ combined = combine_partitions_hp01(parts, FREQ, DIR)
101
+ # Each combined partition contains exactly one of the two peaks
102
+ fp = sorted(FREQ[np.argmax(p.sum(axis=1))] for p in combined)
103
+ assert fp[0] == pytest.approx(0.08, abs=0.02)
104
+ assert fp[1] == pytest.approx(0.20, abs=0.02)
105
+
106
+ def test_energy_conserved(self, noisy_two_systems):
107
+ parts = _split(noisy_two_systems)
108
+ combined = combine_partitions_hp01(parts, FREQ, DIR)
109
+ hs_combined = np.sqrt(sum(_hs(p) ** 2 for p in combined))
110
+ assert hs_combined == pytest.approx(_hs(noisy_two_systems), rel=1e-6)
111
+
112
+ def test_input_order_invariance(self, noisy_two_systems):
113
+ """The main instability of the old code: results depended on ordering."""
114
+ parts = _split(noisy_two_systems)
115
+ expected = _keys(combine_partitions_hp01(parts, FREQ, DIR))
116
+ rng = np.random.default_rng(7)
117
+ for _ in range(5):
118
+ shuffled = [parts[i] for i in rng.permutation(len(parts))]
119
+ assert _keys(combine_partitions_hp01(shuffled, FREQ, DIR)) == expected
120
+
121
+ def test_output_sorted_by_energy(self, noisy_two_systems):
122
+ parts = _split(noisy_two_systems)
123
+ combined = combine_partitions_hp01(parts, FREQ, DIR)
124
+ hs = [_hs(p) for p in combined]
125
+ assert hs == sorted(hs, reverse=True)
126
+
127
+ def test_exact_number_of_swells_combined(self, noisy_two_systems):
128
+ parts = _split(noisy_two_systems)
129
+ combined = combine_partitions_hp01(parts, FREQ, DIR, swells=1)
130
+ assert len(combined) == 1
131
+ assert _hs(combined[0]) == pytest.approx(_hs(noisy_two_systems), rel=1e-6)
132
+
133
+ def test_exact_number_of_swells_dropped(self, noisy_two_systems):
134
+ parts = _split(noisy_two_systems)
135
+ combined = combine_partitions_hp01(
136
+ parts, FREQ, DIR, swells=1, combine_extra_swells=False
137
+ )
138
+ assert len(combined) == 1
139
+ assert _hs(combined[0]) < _hs(noisy_two_systems)
140
+
141
+ def test_fewer_partitions_than_swells(self, noisy_two_systems):
142
+ parts = _split(noisy_two_systems)
143
+ combined = combine_partitions_hp01(parts, FREQ, DIR, swells=5)
144
+ # Combining does not pad, callers do
145
+ assert len(combined) == 2
146
+
147
+ def test_hs_min_forces_merging(self):
148
+ spec = _gaussian_system(0.08, 270, 2.0) + _gaussian_system(0.25, 90, 0.3)
149
+ parts = _split(spec)
150
+ assert len(parts) == 2
151
+ combined = combine_partitions_hp01(parts, FREQ, DIR, hs_min=0.5)
152
+ assert len(combined) == 1
153
+ assert _hs(combined[0]) == pytest.approx(_hs(spec), rel=1e-6)
154
+
155
+ def test_noise_threshold(self):
156
+ spec = _gaussian_system(0.08, 270, 2.0) + _gaussian_system(0.25, 90, 0.3)
157
+ parts = _split(spec)
158
+ assert len(parts) == 2
159
+ e_small = min((_hs(p) / 4) ** 2 for p in parts)
160
+ noise_a = 2 * e_small * 0.25**4
161
+ combined = combine_partitions_hp01(parts, FREQ, DIR, noise_a=noise_a)
162
+ assert len(combined) == 1
163
+ assert _hs(combined[0]) == pytest.approx(_hs(spec), rel=1e-6)
164
+
165
+ def test_angle_max_gate(self):
166
+ # Two close systems at right angles are combined by the distance
167
+ # criterion unless the angle gate is enabled
168
+ spec = _gaussian_system(0.1, 180, 2.0, 0.01, 10) + _gaussian_system(
169
+ 0.12, 90, 1.5, 0.01, 10
170
+ )
171
+ parts = _split(spec)
172
+ if len(parts) < 2:
173
+ pytest.skip("watershed did not separate the test systems")
174
+ with_gate = combine_partitions_hp01(parts, FREQ, DIR, angle_max=30)
175
+ without_gate = combine_partitions_hp01(parts, FREQ, DIR, angle_max=None)
176
+ assert len(with_gate) >= len(without_gate)
177
+
178
+ def test_null_partitions_dropped(self, noisy_two_systems):
179
+ parts = _split(noisy_two_systems) + [np.zeros_like(noisy_two_systems)]
180
+ combined = combine_partitions_hp01(parts, FREQ, DIR)
181
+ assert len(combined) == 2
182
+
183
+ def test_all_null_partitions(self):
184
+ parts = [np.zeros((FREQ.size, DIR.size)) for _ in range(3)]
185
+ assert combine_partitions_hp01(parts, FREQ, DIR) == []
186
+
187
+ def test_single_partition(self):
188
+ spec = _gaussian_system(0.1, 180, 2.0)
189
+ combined = combine_partitions_hp01([spec], FREQ, DIR)
190
+ assert len(combined) == 1
191
+ assert _hs(combined[0]) == pytest.approx(_hs(spec), rel=1e-6)
192
+
193
+ def test_invalid_shape_raises(self):
194
+ with pytest.raises(ValueError):
195
+ combine_partitions_hp01(np.zeros((5, 5)), FREQ, DIR)
196
+
197
+
198
+ class TestMeasuredSpectra:
199
+ """Sanity checks on real, noisy buoy spectra."""
200
+
201
+ @classmethod
202
+ def setup_class(cls):
203
+ files = sorted((HERE / "sample_files/datawell").glob("*.spt"))
204
+ cls.dset = read_datawell(files)
205
+
206
+ def test_combining_reduces_and_conserves(self):
207
+ for time in range(self.dset.time.size):
208
+ d = self.dset.isel(time=time)
209
+ spec = d.efth.values.astype("float64")
210
+ freq = d.freq.values
211
+ dirs = d.dir.values
212
+ wmap = specpart.partition(spec.astype("float32"), 100)
213
+ parts = [np.where(wmap == ip, spec, 0.0) for ip in range(1, wmap.max() + 1)]
214
+ combined = combine_partitions_hp01(parts, freq, dirs, hs_min=0.2)
215
+ assert 0 < len(combined) < len(parts)
216
+ hs_combined = np.sqrt(sum(npstats.hs(p, freq, dirs) ** 2 for p in combined))
217
+ assert hs_combined == pytest.approx(npstats.hs(spec, freq, dirs), rel=1e-6)
@@ -3,12 +3,6 @@
3
3
 
4
4
  """Tests for `wavespectra` package."""
5
5
 
6
- import pytest
7
-
8
- from click.testing import CliRunner
9
-
10
- from wavespectra import cli
11
-
12
6
 
13
7
  # @pytest.fixture
14
8
  # def response():