Skip to content

Commit

Permalink
Plots for relative measurements paper
Browse files Browse the repository at this point in the history
  • Loading branch information
tryuan99 committed Jun 12, 2024
1 parent 0157a08 commit de92a4f
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 28 deletions.
2 changes: 2 additions & 0 deletions simulation/differential_mesh/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ py_binary(
":differential_mesh_graph_factory",
requirement("absl-py"),
requirement("matplotlib"),
requirement("numpy"),
requirement("SciencePlots"),
],
)
Expand Down Expand Up @@ -101,6 +102,7 @@ py_binary(
":differential_mesh_graph_factory",
":differential_mesh_solver",
"//utils/visualization:color_maps",
"//utils/regression:exponential_regression",
requirement("absl-py"),
requirement("matplotlib"),
requirement("numpy"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,13 @@ def plot_standard_error_sweep(standard_errors: str) -> None:

# Plot the squared standard error as a function of the grid dimensions.
plt.style.use("science")
plt.rcParams.update({
"font.size": 16,
"lines.linewidth": 3,
"lines.markersize": 8,
})
fig, ax = plt.subplots(
figsize=(12, 8),
figsize=(6, 6),
subplot_kw={"projection": "3d"},
)
surf = ax.plot_trisurf(
Expand All @@ -58,7 +63,7 @@ def plot_standard_error_sweep(standard_errors: str) -> None:
ax.set_ylabel("Number of columns")
ax.set_zlabel("Squared standard error")
ax.view_init(30, -45)
plt.colorbar(surf)
# plt.colorbar(surf)
plt.show()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,31 @@ def plot_standard_error_sweep(standard_errors: str) -> None:
logging.info("Logarithmic regression: a = %f, b = %f",
logarithmic_regression.a, logarithmic_regression.b)

# Plot the squared standard error as a function of the grid dimensions.
plt.style.use("science")
fig, ax = plt.subplots(figsize=(12, 8))
plt.rcParams.update({
"font.size": 16,
"lines.linewidth": 3,
"lines.markersize": 8,
})
fig, (ax, ax2) = plt.subplots(1, 2, figsize=(12, 3))

# Plot the squared standard error as a function of the grid dimensions.
ax.plot(df[num_rows_column], df[standard_error_squared_column])
ax.plot(df[num_rows_column],
logarithmic_regression.evaluate(df[num_rows_column]), "--")
ax.set_xlabel("Square grid dimensions")
ax.set_ylabel("Squared standard error")
plt.show()
# plt.show()

# Plot the squared standard error as a function of the grid dimensions on a
# semilog x-axis.
plt.style.use("science")
fig, ax = plt.subplots(figsize=(12, 8))
ax.semilogx(df[num_rows_column],
df[standard_error_squared_column],
linewidth=2)
ax.semilogx(df[num_rows_column],
logarithmic_regression.evaluate(df[num_rows_column]), "--")
ax.set_xlabel("Square grid dimensions")
ax.set_ylabel("Squared standard error")
ax2.semilogx(df[num_rows_column],
df[standard_error_squared_column],
linewidth=2)
ax2.semilogx(df[num_rows_column],
logarithmic_regression.evaluate(df[num_rows_column]), "--")
ax2.set_xlabel("Square grid dimensions")
ax2.set_ylabel("Squared standard error")
plt.show()


Expand Down
19 changes: 15 additions & 4 deletions simulation/differential_mesh/differential_mesh_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,32 @@ def get_incident_edges(self, node: int) -> Iterator[int]:

def draw(self) -> None:
"""Draws the differential mesh graph."""
fig, ax = plt.subplots(figsize=(12, 8))
plt.rcParams.update({
"font.size": 16,
"lines.linewidth": 3,
})
fig, ax = plt.subplots(figsize=(7.5, 4.5))
pos = nx.get_node_attributes(
self.graph, DIFFERENTIAL_MESH_GRAPH_NODE_POSITION_ATTRIBUTE)

# Round the node potentials for drawing.
potentials = nx.get_node_attributes(
self.graph, DIFFERENTIAL_MESH_GRAPH_NODE_POTENTIAL_ATTRIBUTE)
if potentials:
self._round_map_values(potentials)
self._round_map_values(potentials, decimals=2)
node_labels = potentials
else:
node_labels = {node: node for node in self.graph.nodes}

# Round the edge measurements for drawing.
measurements = nx.get_edge_attributes(
self.graph, DIFFERENTIAL_MESH_GRAPH_EDGE_MEASUREMENT_ATTRIBUTE)
self._round_map_values(measurements)
self._round_map_values(measurements, decimals=2)

edge_labels = {
(a, b): rf"$\mathrm{{V}}_{{{a}, {b}}}$"
for a, b in nx.edges(self.graph)
}

# Draw the graph with node and edge labels.
nx.draw_networkx_nodes(
Expand All @@ -182,6 +191,7 @@ def draw(self) -> None:
pos=pos,
ax=ax,
labels=node_labels,
font_size=16,
)
nx.draw_networkx_edges(
self.graph,
Expand All @@ -196,8 +206,9 @@ def draw(self) -> None:
self.graph,
pos=pos,
ax=ax,
edge_labels=measurements,
edge_labels=edge_labels,
font_color=DIFFERENTIAL_MESH_GRAPH_EDGE_COLOR,
font_size=16,
)
ax.axis("off")
plt.show()
Expand Down
14 changes: 8 additions & 6 deletions simulation/differential_mesh/differential_mesh_grid_main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import matplotlib.pyplot as plt
import numpy as np
import scienceplots
from absl import app, flags

Expand All @@ -14,19 +15,20 @@ def main(argv):
# Create the grid graph from the number of rows and columns.
grid = DifferentialMeshGraphFactory.create_zero_2d_graph(
FLAGS.num_rows, FLAGS.num_cols)
np.random.seed(1)
grid.add_edge_measurement_noise(FLAGS.noise)

# Draw the grid.
plt.style.use("science")
grid.draw()

# Create the graph from the edge list.
grid = DifferentialMeshGraphFactory.create_from_edge_list(FLAGS.edgelist)
grid.add_edge_measurement_noise(FLAGS.noise)
# # Create the graph from the edge list.
# grid = DifferentialMeshGraphFactory.create_from_edge_list(FLAGS.edgelist)
# grid.add_edge_measurement_noise(FLAGS.noise)

# Draw the grid.
plt.style.use("science")
grid.draw()
# # Draw the grid.
# plt.style.use("science")
# grid.draw()


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
DifferentialMeshGraphFactory
from simulation.differential_mesh.differential_mesh_solver import (
DIFFERENTIAL_MESH_SOLVERS, IterativeDifferentialMeshSolver)
from utils.regression.exponential_regression import ExponentialRegression
from utils.visualization.color_maps import COLOR_MAPS

FLAGS = flags.FLAGS
Expand Down Expand Up @@ -73,8 +74,12 @@ def plot_num_iterations(num_iterations_per_grid: str) -> None:

# Plot the number of iterations as a function of the grid dimensions.
plt.style.use("science")
plt.rcParams.update({
"font.size": 16,
"lines.linewidth": 3,
})
fig, ax = plt.subplots(
figsize=(12, 8),
figsize=(6, 6),
subplot_kw={"projection": "3d"},
)
surf = ax.plot_surface(
Expand All @@ -87,8 +92,36 @@ def plot_num_iterations(num_iterations_per_grid: str) -> None:
ax.set_xlabel("Number of rows")
ax.set_ylabel("Number of columns")
ax.set_zlabel("Mean iterations")
ax.view_init(30, -45)
plt.colorbar(surf)
ax.view_init(25, -60)
# plt.colorbar(surf)
plt.show()

mean_num_iterations = df[
df[num_rows_column] == df[num_cols_column]].groupby(
[num_rows_column, num_cols_column])[num_iterations_column].mean()

logging.info("Mean number of iterations:")
num_iterations = np.zeros((max_num_cols, max_num_rows))
for num_rows in range(1, max_num_rows + 1):
for num_cols in range(1, max_num_cols + 1):
if (num_rows, num_cols) in mean_num_iterations.index:
mean_iterations = mean_num_iterations.loc[(num_rows, num_cols)]
num_iterations[num_cols - 1, num_rows - 1] = mean_iterations
logging.info("(%d, %d) %f", num_rows, num_cols, mean_iterations)

plt.style.use("science")
plt.rcParams.update({
"font.size": 16,
"lines.linewidth": 3,
})
fig, ax = plt.subplots(figsize=(4, 4))
dimensions = np.arange(2, 21)
ax.plot(dimensions, mean_num_iterations.to_numpy())
data = mean_num_iterations.to_numpy()
exponential_regression = ExponentialRegression(dimensions, data)
ax.plot(dimensions, exponential_regression.evaluate(dimensions), "--")
ax.set_xlabel("Square grid dimensions")
ax.set_ylabel("Mean iterations")
plt.show()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ def plot_standard_error(num_rows: int, num_cols: int) -> None:

# Plot the standard error across the grid.
plt.style.use("science")
plt.rcParams.update({
"font.size": 16,
"lines.linewidth": 3,
"lines.markersize": 8,
})
fig, ax = plt.subplots(
figsize=(12, 8),
figsize=(6, 6),
subplot_kw={"projection": "3d"},
)
surf = ax.plot_surface(
Expand Down

0 comments on commit de92a4f

Please sign in to comment.