Skip to content

Commit 57e356a

Browse files
committed
Noise strength map
Smooth the noisy current to find the noise strength map; some things are a bit broken, like plotting and the axis directions, but its ok for now...
1 parent 4f06b35 commit 57e356a

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Utilities for applying noise to data
3+
4+
"""
5+
6+
import numpy as np
7+
from scipy.ndimage import gaussian_filter
8+
9+
10+
def noise_strength_map(current_grid: np.ndarray, *, filter_size: int) -> np.ndarray:
11+
"""
12+
Get the expected noise strength map from a gridded field of current
13+
velocities.
14+
15+
:param current_grid: gridded field showing current strength; land should be indicated
16+
with np.nan.
17+
:param filter_size: size of Gaussian filter to apply, in grid points
18+
:returns: a grid of the same shape as current_grid, showing the expected noise strength
19+
at that grid point. Land values are set to np.nan.
20+
"""
21+
land_mask = np.isnan(current_grid)
22+
23+
current_grid = np.where(land_mask, 0.0, current_grid)
24+
25+
filtered = gaussian_filter(current_grid, sigma=filter_size)
26+
filtered[land_mask] = np.nan
27+
28+
return filtered

src/notebooks/plot_tile_rms.ipynb

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
"Plot the RMS of the $8\\times8\\degree$ MDT current tiles, to visualise how the energy in the tiles varies - we want to exclude the regions near coastlines when picking our training data."
1111
]
1212
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"id": "002e5fcb",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"%load_ext autoreload\n",
21+
"%autoreload 2"
22+
]
23+
},
1324
{
1425
"cell_type": "code",
1526
"execution_count": null,
@@ -141,6 +152,92 @@
141152
"\n",
142153
"fig.colorbar(im, ax=axes[\"A\"])"
143154
]
155+
},
156+
{
157+
"cell_type": "markdown",
158+
"id": "3a5a91c1",
159+
"metadata": {},
160+
"source": [
161+
"# Noise application\n",
162+
"\n",
163+
"We estimate noise strength by creating a noise strength map - visualise this here"
164+
]
165+
},
166+
{
167+
"cell_type": "code",
168+
"execution_count": null,
169+
"id": "2a99491a",
170+
"metadata": {},
171+
"outputs": [],
172+
"source": [
173+
"from current_denoising.generation import applying_noise\n",
174+
"\n",
175+
"filter_size = 5\n",
176+
"\n",
177+
"filtered = applying_noise.noise_strength_map(data, filter_size=filter_size)"
178+
]
179+
},
180+
{
181+
"cell_type": "code",
182+
"execution_count": null,
183+
"id": "40fc172c",
184+
"metadata": {},
185+
"outputs": [],
186+
"source": [
187+
"fig, axis = plt.subplots(1, 1, figsize=(12, 6))\n",
188+
"\n",
189+
"im = axis.imshow(filtered, **imshow_kw)\n",
190+
"\n",
191+
"earth_radius_km = 6370\n",
192+
"axis.set_title(\n",
193+
" f\"{filter_size=} grid point\\n\"\n",
194+
" r\"$\\approx$\"\n",
195+
" f\"{earth_radius_km / 12 * filter_size:.0f}km at equator\"\n",
196+
")\n",
197+
"fig.colorbar(im)\n",
198+
"\n",
199+
"lat, long = maps.lat_long_grid(data.shape)\n",
200+
"extent = [long[0], long[-1], lat[0], lat[-1]]\n",
201+
"im.set_extent(extent)\n",
202+
"\n",
203+
"fig.tight_layout()"
204+
]
205+
},
206+
{
207+
"cell_type": "code",
208+
"execution_count": null,
209+
"id": "0b754829",
210+
"metadata": {},
211+
"outputs": [],
212+
"source": [
213+
"\"\"\"Plot a tile near Australia to reproduce Fig 9 in Laura's paper\"\"\"\n",
214+
"\n",
215+
"import numpy as np\n",
216+
"from current_denoising.generation import ioutils\n",
217+
"\n",
218+
"# NB the latitude is backwards here, so it is positive instead of negative\n",
219+
"lat, long = -38, -70\n",
220+
"size = 128\n",
221+
"\n",
222+
"# Get the index of the nearest grid point to each of these\n",
223+
"indices = []\n",
224+
"for a, b in zip([lat, long], maps.lat_long_grid(data.shape)):\n",
225+
" indices.append(int(np.argmin(np.abs(b - a))))\n",
226+
"\n",
227+
"tile = ioutils._tile(filtered, indices, size)\n",
228+
"\n",
229+
"fig, axis = plt.subplots()\n",
230+
"axis.imshow(tile, **imshow_kw)\n",
231+
"axis.set_axis_off()"
232+
]
233+
},
234+
{
235+
"cell_type": "code",
236+
"execution_count": null,
237+
"id": "63bbc617",
238+
"metadata": {},
239+
"outputs": [],
240+
"source": []
144241
}
145242
],
146243
"metadata": {

src/tests/ut/test_applying_noise.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Tests for the noise application utils
3+
"""
4+
5+
import numpy as np
6+
7+
from current_denoising.generation import applying_noise
8+
9+
10+
def test_noise_strength_land_mask():
11+
"""
12+
Check that the land is in the right place in our noise strength map
13+
"""
14+
map = np.arange(100, dtype=np.float32).reshape(10, 10)
15+
# Set some values to indicate that they're land
16+
map[(map % 3).astype(bool)] = np.nan
17+
18+
assert (
19+
np.isnan(map) == np.isnan(applying_noise.noise_strength_map(map, filter_size=5))
20+
).all()
21+
22+
assert (
23+
np.isnan(map)
24+
== np.isnan(applying_noise.noise_strength_map(map, filter_size=50))
25+
).all()

0 commit comments

Comments
 (0)