Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a2b296c
Initial Commit
aaronzedwick Nov 13, 2024
182e7bb
Updated test cases
aaronzedwick Nov 13, 2024
82477f9
Merge branch 'main' into zedwick/edge_to_face
aaronzedwick Nov 19, 2024
7221bc2
Merge branch 'zedwick/edge_to_face' of https://github.com/UXARRAY/uxa…
aaronzedwick Nov 19, 2024
4938c4f
Merge branch 'main' into zedwick/edge_to_face
aaronzedwick Nov 26, 2024
495934d
Merge branch 'main' into zedwick/edge_to_face
philipc2 Dec 2, 2024
5472f63
Merge branch 'main' into zedwick/edge_to_face
aaronzedwick Dec 12, 2024
7d99d38
Merge branch 'main' into zedwick/edge_to_face
aaronzedwick Jan 7, 2025
a0e7ad7
Merge branch 'zedwick/edge_to_face' of https://github.com/UXARRAY/uxa…
aaronzedwick Jan 7, 2025
edaead7
initial work on face to node
aaronzedwick Jan 8, 2025
4696070
Fixed face to node agg bug
aaronzedwick Jan 9, 2025
54fec18
Updated notebook
aaronzedwick Jan 9, 2025
816bc16
Added face to edge
aaronzedwick Jan 9, 2025
04abf2f
Added edge_to_node and connectivity constructions
aaronzedwick Jan 10, 2025
f7a5589
Update grid.py
aaronzedwick Jan 10, 2025
dd41f93
Merge branch 'main' into zedwick/edge_to_face
aaronzedwick Jan 10, 2025
441f8a3
Initial node_edge_connectivity work
aaronzedwick Jan 15, 2025
fee78f0
Merge branch 'main' into zedwick/edge_to_face
philipc2 Jan 23, 2025
afc8ea8
Merge branch 'main' into zedwick/edge_to_face
philipc2 Feb 6, 2025
80383f6
Merge branch 'main' into zedwick/edge_to_face
philipc2 Feb 24, 2025
de85b35
Merge branch 'main' into zedwick/edge_to_face
philipc2 Mar 4, 2025
51af602
Merge branch 'main' into zedwick/edge_to_face
aaronzedwick Mar 10, 2025
4a9d98b
Merge branch 'main' into zedwick/edge_to_face
aaronzedwick Jun 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,999 changes: 2,999 additions & 0 deletions docs/user-guide/.ipynb_checkpoints/topological-aggregations-checkpoint.ipynb

Large diffs are not rendered by default.

2,542 changes: 2,516 additions & 26 deletions docs/user-guide/topological-aggregations.ipynb

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions test/test_topological_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

current_path = Path(os.path.dirname(os.path.realpath(__file__)))


ds_path = current_path / 'meshfiles' / "mpas" / "QU" / 'oQU480.231010.nc'

AGGS = ["topological_mean",
Expand All @@ -32,11 +31,22 @@ def test_node_to_face_aggs():
assert 'n_face' in grid_reduction.dims



def test_node_to_edge_aggs():
uxds = ux.open_dataset(ds_path, ds_path)

for agg_func in AGGS:
grid_reduction = getattr(uxds['areaTriangle'], agg_func)(destination='edge')

assert 'n_edge' in grid_reduction.dims


def test_edge_to_face_aggs():
grid_path = '/Users/aaronzedwick/uxarray/test/meshfiles/mpas/QU/mesh.QU.1920km.151026.nc'

uxds = ux.open_dataset(grid_path, grid_path)

uxds = uxds['cellsOnEdge'].subset.nearest_neighbor(k=3, center_coord=[0, 0])

uxda_edge_face_agg = uxds.topological_mean(destination="node")

print(uxda_edge_face_agg)
27 changes: 27 additions & 0 deletions uxarray/conventions/ugrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,33 @@

N_NODES_PER_FACE_DIMS = ["n_face"]

N_FACES_PER_NODE_ATTRS = {
"cf_role": "n_faces_per_node",
"long name": "Number of faces per node",
}

N_FACES_PER_NODE_DIMS = ["n_node"]

N_FACES_PER_EDGE_ATTRS = {
"cf_role": "n_faces_per_edge",
"long name": "Number of faces per edge",
}

N_FACES_PER_EDGE_DIMS = ["n_edge"]

N_EDGES_PER_FACE_ATTRS = {
"cf_role": "n_edges_per_face",
"long name": "Number of edges per face",
}

N_EDGES_PER_FACE_DIMS = ["n_face"]

N_EDGES_PER_NODE_ATTRS = {
"cf_role": "n_edges_per_node",
"long name": "Number of edges per node",
}

N_EDGES_PER_NODE_DIMS = ["n_node"]

CONNECTIVITY_NAMES = [
"face_node_connectivity",
Expand Down
282 changes: 263 additions & 19 deletions uxarray/core/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,32 @@ def _uxda_grid_aggregate(uxda, destination, aggregation, **kwargs):
else:
raise ValueError(
f"Invalid destination for a node-centered data variable. Expected"
f"one of ['face', 'edge' but received {destination}"
f"one of ['face', 'edge'] but received {destination}"
)

elif uxda._edge_centered():
# aggregation of an edge-centered data variable
raise NotImplementedError(
"Aggregation of edge-centered data variables is not yet supported."
)
# if destination == "node":
# pass
# elif destination == "face":
# pass
# else:
# raise ValueError("TODO: )
if destination == "face":
return _edge_to_face_aggregation(uxda, aggregation, kwargs)
elif destination == "node":
return _edge_to_node_aggregation(uxda, aggregation, kwargs)
else:
raise ValueError(
f"Invalid destination for an edge-centered data variable. Expected"
f"one of ['face', 'node'] but received {destination}"
)

elif uxda._face_centered():
# aggregation of a face-centered data variable
raise NotImplementedError(
"Aggregation of face-centered data variables is not yet supported."
)
# if destination == "node":
# pass
# elif destination == "edge":
# pass
# else:
# raise ValueError("TODO: ")
if destination == "node":
return _face_to_node_aggregation(uxda, aggregation, kwargs)
elif destination == "edge":
return _face_to_edge_aggregation(uxda, aggregation, kwargs)
else:
raise ValueError(
f"Invalid destination for a face-centered data variable. Expected"
f"one of ['node', 'edge'] but received {destination}"
)

else:
raise ValueError(
Expand Down Expand Up @@ -182,3 +182,247 @@ def _apply_node_to_edge_aggregation_numpy(
def _apply_node_to_edge_aggregation_dask(*args, **kwargs):
"""Applies a Node to Edge topological aggregation on a dask array."""
pass


def _apply_edge_to_face_aggregation_numpy(
uxda, aggregation_func, aggregation_func_kwargs
):
"""Applies an Edge to Face Topological aggregation on a Numpy array."""
data = uxda.values
face_edge_conn = uxda.uxgrid.face_edge_connectivity.values
n_nodes_per_face = uxda.uxgrid.n_nodes_per_face.values

(
change_ind,
n_nodes_per_face_sorted_ind,
element_sizes,
size_counts,
) = get_face_node_partitions(n_nodes_per_face)

result = np.empty(shape=(data.shape[:-1]) + (uxda.uxgrid.n_face,))

for e, start, end in zip(element_sizes, change_ind[:-1], change_ind[1:]):
face_inds = n_nodes_per_face_sorted_ind[start:end]
face_nodes_par = face_edge_conn[face_inds, 0:e]

# apply aggregation function to current face edge partition
aggregation_par = aggregation_func(
data[..., face_nodes_par], axis=-1, **aggregation_func_kwargs
)

# store current aggregation
result[..., face_inds] = aggregation_par

return result


def _edge_to_face_aggregation(uxda, aggregation, aggregation_func_kwargs):
"""Applies an Edge to Face Topological aggregation."""
if not uxda._edge_centered():
raise ValueError(
f"Data Variable must be mapped to the edge centers of each face, with dimension "
f"{uxda.uxgrid.n_edge}."
)

if isinstance(uxda.data, np.ndarray):
# apply aggregation using numpy
aggregated_var = _apply_edge_to_face_aggregation_numpy(
uxda, NUMPY_AGGREGATIONS[aggregation], aggregation_func_kwargs
)
elif isinstance(uxda.data, da.Array):
# apply aggregation on dask array, TODO:
aggregated_var = _apply_edge_to_face_aggregation_numpy(
uxda, NUMPY_AGGREGATIONS[aggregation], aggregation_func_kwargs
)
else:
raise ValueError

return uxarray.core.dataarray.UxDataArray(
uxgrid=uxda.uxgrid,
data=aggregated_var,
dims=uxda.dims,
name=uxda.name,
).rename({"n_edge": "n_face"})


def _apply_edge_to_node_aggregation_numpy(
uxda, aggregation_func, aggregation_func_kwargs
):
"""Applies an Edge to Node Topological aggregation on a Numpy array."""
data = uxda.values
node_edge_conn = uxda.uxgrid.node_edge_connectivity.values
n_edges_per_node = uxda.uxgrid.n_edges_per_node.values

(
change_ind,
n_edges_per_node_sorted_ind,
element_sizes,
size_counts,
) = get_face_node_partitions(n_edges_per_node)

result = np.empty(shape=(data.shape[:-1]) + (uxda.uxgrid.n_node,))

for e, start, end in zip(element_sizes, change_ind[:-1], change_ind[1:]):
node_inds = n_edges_per_node_sorted_ind[start:end]
node_edges_par = node_edge_conn[node_inds, 0:e]

# apply aggregation function to current node edge partition
aggregation_par = aggregation_func(
data[..., node_edges_par], axis=-1, **aggregation_func_kwargs
)

# store current aggregation
result[..., node_inds] = aggregation_par

return result


def _edge_to_node_aggregation(uxda, aggregation, aggregation_func_kwargs):
"""Applies an Edge to Node Topological aggregation."""
if not uxda._edge_centered():
raise ValueError(
f"Data Variable must be mapped to the edge centers of each face, with dimension "
f"{uxda.uxgrid.n_edge}."
)

if isinstance(uxda.data, np.ndarray):
# apply aggregation using numpy
aggregated_var = _apply_edge_to_node_aggregation_numpy(
uxda, NUMPY_AGGREGATIONS[aggregation], aggregation_func_kwargs
)
elif isinstance(uxda.data, da.Array):
# apply aggregation on dask array, TODO:
aggregated_var = _apply_edge_to_node_aggregation_numpy(
uxda, NUMPY_AGGREGATIONS[aggregation], aggregation_func_kwargs
)
else:
raise ValueError

return uxarray.core.dataarray.UxDataArray(
uxgrid=uxda.uxgrid,
data=aggregated_var,
dims=uxda.dims,
name=uxda.name,
).rename({"n_edge": "n_node"})


def _apply_face_to_node_aggregation_numpy(
uxda, aggregation_func, aggregation_func_kwargs
):
"""Applies a Face to Node Topological aggregation on a Numpy array."""
data = uxda.values
node_face_conn = uxda.uxgrid.node_face_connectivity.values
n_faces_per_node = uxda.uxgrid.n_faces_per_node.values

(
change_ind,
n_faces_per_node_sorted_ind,
element_sizes,
size_counts,
) = get_face_node_partitions(n_faces_per_node)

result = np.empty(shape=(data.shape[:-1]) + (uxda.uxgrid.n_node,))

for e, start, end in zip(element_sizes, change_ind[:-1], change_ind[1:]):
face_inds = n_faces_per_node_sorted_ind[start:end]
face_nodes_par = node_face_conn[face_inds, 0:e]

# apply aggregation function to current node face partition
aggregation_par = aggregation_func(
data[..., face_nodes_par], axis=-1, **aggregation_func_kwargs
)

# store current aggregation
result[..., face_inds] = aggregation_par

return result


def _face_to_node_aggregation(uxda, aggregation, aggregation_func_kwargs):
"""Applies a Face to Node Topological aggregation."""
if not uxda._face_centered():
raise ValueError(
f"Data Variable must be mapped to the face centers of each face, with dimension "
f"{uxda.uxgrid.n_face}."
)

if isinstance(uxda.data, np.ndarray):
# apply aggregation using numpy
aggregated_var = _apply_face_to_node_aggregation_numpy(
uxda, NUMPY_AGGREGATIONS[aggregation], aggregation_func_kwargs
)
elif isinstance(uxda.data, da.Array):
# apply aggregation on dask array, TODO:
aggregated_var = _apply_face_to_node_aggregation_numpy(
uxda, NUMPY_AGGREGATIONS[aggregation], aggregation_func_kwargs
)
else:
raise ValueError

return uxarray.core.dataarray.UxDataArray(
uxgrid=uxda.uxgrid,
data=aggregated_var,
dims=uxda.dims,
name=uxda.name,
).rename({"n_face": "n_node"})


def _apply_face_to_edge_aggregation_numpy(
uxda, aggregation_func, aggregation_func_kwargs
):
"""Applies a Face to Edge Topological aggregation on a Numpy array."""
data = uxda.values
edge_face_conn = uxda.uxgrid.edge_face_connectivity.values
n_faces_per_edge = uxda.uxgrid.n_faces_per_edge.values

(
change_ind,
n_faces_per_edge_sorted_ind,
element_sizes,
size_counts,
) = get_face_node_partitions(n_faces_per_edge)

result = np.empty(shape=(data.shape[:-1]) + (uxda.uxgrid.n_edge,))

for e, start, end in zip(element_sizes, change_ind[:-1], change_ind[1:]):
face_inds = n_faces_per_edge_sorted_ind[start:end]
face_edge_par = edge_face_conn[face_inds, 0:e]

# apply aggregation function to current node face partition
aggregation_par = aggregation_func(
data[..., face_edge_par], axis=-1, **aggregation_func_kwargs
)

# store current aggregation
result[..., face_inds] = aggregation_par

return result


def _face_to_edge_aggregation(uxda, aggregation, aggregation_func_kwargs):
"""Applies a Face to Edge Topological aggregation."""
if not uxda._face_centered():
raise ValueError(
f"Data Variable must be mapped to the face centers of each face, with dimension "
f"{uxda.uxgrid.n_face}."
)

if isinstance(uxda.data, np.ndarray):
# apply aggregation using numpy
aggregated_var = _apply_face_to_edge_aggregation_numpy(
uxda, NUMPY_AGGREGATIONS[aggregation], aggregation_func_kwargs
)
elif isinstance(uxda.data, da.Array):
# apply aggregation on dask array, TODO:
aggregated_var = _apply_face_to_edge_aggregation_numpy(
uxda, NUMPY_AGGREGATIONS[aggregation], aggregation_func_kwargs
)
else:
raise ValueError

return uxarray.core.dataarray.UxDataArray(
uxgrid=uxda.uxgrid,
data=aggregated_var,
dims=uxda.dims,
name=uxda.name,
).rename({"n_face": "n_edge"})
Loading
Loading