Analysis#
Mesh-native analysis kernels: point extraction, random sampling,
contouring, and histogram plotting. These functions operate on
Mesh2d + numpy arrays and are called by the _UgridFacades
mixin methods on UgridDataset.
pyramids.netcdf.ugrid.analysis
#
Mesh-native analysis: point extraction, random sampling, contouring.
Module-level functions taking :class:~pyramids.netcdf.ugrid.mesh.Mesh2d
plus plain arrays — no dataset import, keeping the import graph acyclic.
Same contract as fill.py, graph.py, vector_ops.py.
extract_points(mesh, values, x, y, location='face')
#
Sample variable values at arbitrary coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh2d
|
The mesh topology. |
required |
values
|
ndarray
|
Variable data ( |
required |
x
|
ndarray
|
Query x-coordinates. |
required |
y
|
ndarray
|
Query y-coordinates. |
required |
location
|
str
|
|
'face'
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Extracted values — shape |
ndarray
|
Points outside the mesh get |
Raises:
| Type | Description |
|---|---|
ValueError
|
If location is not |
Examples:
- Sample face values at a query point inside the first triangle:
>>> import numpy as np >>> from pyramids.netcdf.ugrid.connectivity import Connectivity >>> from pyramids.netcdf.ugrid.mesh import Mesh2d >>> from pyramids.netcdf.ugrid.analysis import extract_points >>> fnc = Connectivity( ... np.array([[0, 1, 2], [1, 3, 2]]), ... -1, "face_node_connectivity", 0, ... ) >>> mesh = Mesh2d( ... node_x=np.array([0.0, 1.0, 0.5, 1.5]), ... node_y=np.array([0.0, 0.0, 1.0, 1.0]), ... face_node_connectivity=fnc, ... ) >>> values = np.array([10.0, 20.0]) >>> extract_points(mesh, values, np.array([0.3]), np.array([0.3])) array([10.]) - Points outside the mesh return NaN:
>>> import numpy as np >>> from pyramids.netcdf.ugrid.connectivity import Connectivity >>> from pyramids.netcdf.ugrid.mesh import Mesh2d >>> from pyramids.netcdf.ugrid.analysis import extract_points >>> fnc = Connectivity( ... np.array([[0, 1, 2], [1, 3, 2]]), ... -1, "face_node_connectivity", 0, ... ) >>> mesh = Mesh2d( ... node_x=np.array([0.0, 1.0, 0.5, 1.5]), ... node_y=np.array([0.0, 0.0, 1.0, 1.0]), ... face_node_connectivity=fnc, ... ) >>> extract_points( ... mesh, np.array([10.0, 20.0]), ... np.array([5.0]), np.array([5.0]), ... ) array([nan])
See Also
sample_random: Draw spatially random samples instead of querying specific coordinates.
Source code in src/pyramids/netcdf/ugrid/analysis.py
sample_random(mesh, values, n, seed=None)
#
Draw n spatially random samples from the mesh domain.
Uses rejection sampling within the mesh bounding box, keeping only points that fall inside a mesh face.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh2d
|
The mesh topology. |
required |
values
|
ndarray
|
Variable data ( |
required |
n
|
int
|
Number of samples to draw. |
required |
seed
|
int | None
|
RNG seed for reproducibility. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray, ndarray]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If n < 1. |
RuntimeError
|
If rejection sampling cannot collect n points after 100 batches (the mesh domain may be degenerate or extremely sparse relative to its bounding box). |
Examples:
- Draw three random samples with a fixed seed:
>>> import numpy as np >>> from pyramids.netcdf.ugrid.connectivity import Connectivity >>> from pyramids.netcdf.ugrid.mesh import Mesh2d >>> from pyramids.netcdf.ugrid.analysis import sample_random >>> fnc = Connectivity( ... np.array([[0, 1, 2], [1, 3, 2]]), ... -1, "face_node_connectivity", 0, ... ) >>> mesh = Mesh2d( ... node_x=np.array([0.0, 1.0, 0.5, 1.5]), ... node_y=np.array([0.0, 0.0, 1.0, 1.0]), ... face_node_connectivity=fnc, ... ) >>> values = np.array([10.0, 20.0]) >>> x, y, sampled = sample_random(mesh, values, 3, seed=42) >>> x.shape (3,) >>> sampled.shape (3,)
See Also
extract_points: Sample at specific coordinates rather than random ones.
Source code in src/pyramids/netcdf/ugrid/analysis.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
contour_lines(mesh, values, levels=None, location='face')
#
Generate contour lines from mesh data via tricontour.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mesh
|
Mesh2d
|
The mesh topology. |
required |
values
|
ndarray
|
1-D variable data (one value per element). |
required |
levels
|
Any
|
Contour levels (int for count, array for explicit). |
None
|
location
|
str
|
|
'face'
|
Returns:
| Name | Type | Description |
|---|---|---|
GeoDataFrame |
Any
|
Contour lines with a |
Source code in src/pyramids/netcdf/ugrid/analysis.py
plot_histogram(values, nodata=None, bins=15, ax=None, **kwargs)
#
Plot a variable's value distribution as a histogram.
Delegates to cleopatra's HistogramGlyph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ndarray
|
The flat data array. |
required |
nodata
|
Any
|
No-data sentinel to exclude. |
None
|
bins
|
int
|
Number of histogram bins. |
15
|
ax
|
Any
|
Existing matplotlib axes to draw into. |
None
|
**kwargs
|
Any
|
Forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If all values are NaN / nodata and nothing remains to histogram. |
See Also
contour_lines: Spatial contour extraction from the same mesh variable.
Source code in src/pyramids/netcdf/ugrid/analysis.py
valid_samples(values, nodata=None)
#
The flat array of values that are neither NaN nor the declared no-data sentinel.
Factored out of :func:plot_histogram so the numbers :func:histogram reports and the
bars :func:plot_histogram draws are derived from the same samples and cannot
disagree (issue #1375).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ndarray
|
The data array, of any shape. |
required |
nodata
|
Any
|
No-data sentinel to exclude, or |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The 1-D array of valid samples. |
Raises:
| Type | Description |
|---|---|
ValueError
|
Nothing survives the masking, so there is no distribution to describe. |
Source code in src/pyramids/netcdf/ugrid/analysis.py
histogram(values, nodata=None, bins=256, value_range=None)
#
The histogram of a variable's values as data, rather than as a picture.
The mesh counterpart of the raster get_histogram. :func:plot_histogram could already
draw the distribution but gave no way to obtain it; both now mask through
:func:valid_samples, so the counts here describe exactly the samples the bars show.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ndarray
|
The data array, of any shape. |
required |
nodata
|
Any
|
No-data sentinel to exclude, or |
None
|
bins
|
int
|
Number of equal-width bins. Defaults to 256, as the raster version does. |
256
|
value_range
|
tuple[float, float] | None
|
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
ndarray
|
|
ndarray
|
variable's own units. |
Raises:
| Type | Description |
|---|---|
ValueError
|
Nothing survives the no-data masking. |