Skip to content

Commit

Permalink
Correct Python notebooks (#160 | GRIDEDIT-964_correct_python_notebooks)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacarniato authored Apr 3, 2024
1 parent cd21054 commit ceba150
Show file tree
Hide file tree
Showing 14 changed files with 360 additions and 346 deletions.
97 changes: 36 additions & 61 deletions docs/examples/01_mesh2d_basics.ipynb

Large diffs are not rendered by default.

44 changes: 32 additions & 12 deletions docs/examples/02_mesh1d_basics.ipynb

Large diffs are not rendered by default.

34 changes: 19 additions & 15 deletions docs/examples/03_tri_mesh2d_pol.ipynb

Large diffs are not rendered by default.

202 changes: 126 additions & 76 deletions docs/examples/04_curvilineargrid_basics.ipynb

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions docs/examples/05_mesh2d_refinement_gridded_samples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,6 @@
"fig, ax = plt.subplots()\n",
"mesh2d_output.plot_edges(ax, color=\"black\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
58 changes: 12 additions & 46 deletions docs/examples/06_mesh2d_refinement_gridded_samples_gebco.ipynb

Large diffs are not rendered by default.

14 changes: 0 additions & 14 deletions docs/examples/07_curvilineargrid_with_defined_extension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,6 @@
"fig, ax = plt.subplots()\n",
"mesh2d.plot_edges(ax, color=\"black\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
15 changes: 4 additions & 11 deletions docs/examples/09_mesh2d_deletion.ipynb

Large diffs are not rendered by default.

9 changes: 1 addition & 8 deletions docs/examples/10_mesh2d_global_grid.ipynb

Large diffs are not rendered by default.

51 changes: 17 additions & 34 deletions docs/examples/11_mesh2d_refine_ridges_gridded_samples.ipynb

Large diffs are not rendered by default.

23 changes: 7 additions & 16 deletions docs/examples/12_mesh2d_refine_gridded_samples_coastlines.ipynb

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions meshkernel/c_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,7 @@ def allocate_memory(self) -> Mesh1d:
self.node_x = as_ctypes(node_x)
self.node_y = as_ctypes(node_y)

return Mesh1d(
node_x,
node_y,
edge_nodes,
)
return Mesh1d(node_x=node_x, node_y=node_y, edge_nodes=edge_nodes)


class CContacts(Structure):
Expand Down
10 changes: 5 additions & 5 deletions meshkernel/meshkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ def __init__(self, projection: ProjectionType = ProjectionType.CARTESIAN):

self._allocate_state(projection)

invalid_value = self.mkernel_get_separator()
self._int_invalid_value = int(-1)

self._int_invalid_value = int(invalid_value)
invalid_value = self.mkernel_get_separator()

self._float_invalid_value = float(invalid_value)

Expand Down Expand Up @@ -221,7 +221,7 @@ def mesh2d_get(self) -> Mesh2d:
self.lib.mkernel_mesh2d_get_data, self._meshkernelid, byref(c_mesh2d)
)

mesh2d.remove_invalid_values(self._float_invalid_value, self._int_invalid_value)
mesh2d.remove_invalid_values(float_invalid_value=self._float_invalid_value)

return mesh2d

Expand Down Expand Up @@ -1244,7 +1244,7 @@ def mesh1d_get(self) -> Mesh1d:
self.lib.mkernel_mesh1d_get_data, self._meshkernelid, byref(c_mesh1d)
)

mesh1d.remove_invalid_values(self._float_invalid_value, self._int_invalid_value)
mesh1d.remove_invalid_values(float_invalid_value=self._float_invalid_value)

return mesh1d

Expand Down Expand Up @@ -1315,7 +1315,7 @@ def contacts_get(self) -> Contacts:
self.lib.mkernel_contacts_get_data, self._meshkernelid, byref(c_contacts)
)

contacts.remove_invalid_values(self._int_invalid_value)
contacts.remove_invalid_values(int_invalid_value=self._int_invalid_value)

return contacts

Expand Down
136 changes: 100 additions & 36 deletions meshkernel/py_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,33 +142,85 @@ def __init__(
self.edge_faces: ndarray = np.asarray(edge_faces, dtype=np.int32)
self.face_edges: ndarray = np.asarray(face_edges, dtype=np.int32)

def remove_invalid_values(self, float_invalid_value: float, int_invalid_value: int):
def remove_invalid_values(self, float_invalid_value: float):
"""Removes invalid values that might be present in the arrays.
Remove the corresponding entries in the others
Args:
float_invalid_value: (float): The float invalid value.
int_invalid_value: (int): The int invalid value.
"""

self.node_x = self.node_x[self.node_x != float_invalid_value]
self.node_y = self.node_y[self.node_y != float_invalid_value]
self.edge_x = self.edge_x[self.edge_x != float_invalid_value]
self.edge_y = self.edge_y[self.edge_y != float_invalid_value]
self.face_x = self.face_x[self.face_x != float_invalid_value]
self.face_y = self.face_y[self.face_y != float_invalid_value]
valid_node_indices = [
i
for i, (x, y) in enumerate(zip(self.node_x, self.node_y))
if x != float_invalid_value and y != float_invalid_value
]
valid_nodes_map = {
old_index: new_index
for new_index, old_index in enumerate(valid_node_indices)
}

indices_to_remove = np.where(self.edge_nodes == int_invalid_value)[0]
self.edge_nodes = np.delete(self.edge_nodes, indices_to_remove)
self.node_x = np.array(
[self.node_x[i] for i in valid_node_indices], dtype=np.double
)
self.node_y = np.array(
[self.node_y[i] for i in valid_node_indices], dtype=np.double
)
self.edge_nodes = np.array(
[
valid_nodes_map[edge_node]
for edge_node in self.edge_nodes
if edge_node in valid_nodes_map
],
dtype=np.int32,
)
self.face_nodes = np.array(
[
valid_nodes_map[face_node]
for face_node in self.face_nodes
if face_node in valid_nodes_map
],
dtype=np.int32,
)

indices_to_remove = np.where(self.face_nodes == int_invalid_value)[0]
self.face_nodes = np.delete(self.face_nodes, indices_to_remove)
valid_face_indices = [
i
for i, (x, y) in enumerate(zip(self.face_x, self.face_y))
if x != float_invalid_value and y != float_invalid_value
]
valid_face_map = {
old_index: new_index
for new_index, old_index in enumerate(valid_face_indices)
}
self.face_x = np.array(
[self.face_x[i] for i in valid_face_map], dtype=np.double
)
self.face_y = np.array(
[self.face_y[i] for i in valid_face_map], dtype=np.double
)

indices_to_remove = np.where(self.edge_faces == int_invalid_value)[0]
self.edge_faces = np.delete(self.edge_faces, indices_to_remove)
valid_edge_indices = [
i
for i, (x, y) in enumerate(zip(self.edge_x, self.edge_y))
if x != float_invalid_value and y != float_invalid_value
]
valid_edge_map = {
old_index: new_index
for new_index, old_index in enumerate(valid_edge_indices)
}
self.edge_x = np.array(
[self.edge_x[i] for i in valid_edge_map], dtype=np.double
)
self.edge_y = np.array(
[self.edge_y[i] for i in valid_edge_map], dtype=np.double
)

indices_to_remove = np.where(self.face_edges == int_invalid_value)[0]
self.face_edges = np.delete(self.face_edges, indices_to_remove)
self.edge_faces = np.array(
[self.edge_faces[i] for i in valid_edge_indices], dtype=np.int32
)
self.face_edges = np.array(
[self.face_edges[i] for i in valid_face_indices], dtype=np.int32
)

def __eq__(self, other: Mesh2d):
"""Checks if the mesh is exactly equal to another.
Expand Down Expand Up @@ -366,34 +418,35 @@ def plot_edges(self, ax, *args, **kwargs):
node_indices = np.fromiter(
(int(x) for x in range(self.num_m * self.num_n)), int
)
node_indices = node_indices.reshape((self.num_m, self.num_n))
node_indices = node_indices.reshape((self.num_n, self.num_m))

invalid_value = -999.0
edge_nodes = np.zeros(
(self.num_m * (self.num_n - 1) + (self.num_m - 1) * self.num_n) * 2,
dtype=np.int_,
)
index = 0
for m in range(self.num_m - 1):
for n in range(self.num_n):

for n in range(self.num_n - 1):
for m in range(self.num_m):
if (
self.node_x[node_indices[m][n]] != invalid_value
and self.node_x[node_indices[m + 1][n]] != invalid_value
self.node_x[node_indices[n][m]] != invalid_value
and self.node_x[node_indices[n + 1][m]] != invalid_value
):
edge_nodes[index] = node_indices[m][n]
edge_nodes[index] = node_indices[n][m]
index += 1
edge_nodes[index] = node_indices[m + 1][n]
edge_nodes[index] = node_indices[n + 1][m]
index += 1

for m in range(self.num_m):
for n in range(self.num_n - 1):
for n in range(self.num_n):
for m in range(self.num_m - 1):
if (
self.node_x[node_indices[m][n]] != invalid_value
and self.node_x[node_indices[m][n + 1]] != invalid_value
self.node_x[node_indices[n][m]] != invalid_value
and self.node_x[node_indices[n][m + 1]] != invalid_value
):
edge_nodes[index] = node_indices[m][n]
edge_nodes[index] = node_indices[n][m]
index += 1
edge_nodes[index] = node_indices[m][n + 1]
edge_nodes[index] = node_indices[n][m + 1]
index += 1

edge_nodes = np.resize(edge_nodes, index)
Expand Down Expand Up @@ -576,19 +629,30 @@ def __init__(self, node_x, node_y, edge_nodes):
self.node_y: ndarray = np.asarray(node_y, dtype=np.double)
self.edge_nodes: ndarray = np.asarray(edge_nodes, dtype=np.int32)

def remove_invalid_values(self, float_invalid_value: float, int_invalid_value: int):
def remove_invalid_values(self, float_invalid_value: float):
"""Removes invalid values that might be present in the arrays.
Args:
float_invalid_value: (float): The float invalid value.
int_invalid_value: (int): The int invalid value.
"""

self.node_x = self.node_x[self.node_x != float_invalid_value]
self.node_y = self.node_y[self.node_y != float_invalid_value]

indices_to_remove = np.where(self.edge_nodes == int_invalid_value)[0]
self.edge_nodes = np.delete(self.edge_nodes, indices_to_remove)
valid_node_indices = [
i
for i, (x, y) in enumerate(zip(self.node_x, self.node_y))
if x != float_invalid_value and y != float_invalid_value
]
valid_nodes_map = {
old_index: new_index
for new_index, old_index in enumerate(valid_node_indices)
}
self.edge_nodes = np.array(
[
valid_nodes_map[edge_node]
for edge_node in self.edge_nodes
if edge_node in valid_nodes_map
],
dtype=np.int32,
)

def plot_edges(self, ax, *args, **kwargs):
"""Plots the edges at a given axes.
Expand Down

0 comments on commit ceba150

Please sign in to comment.