Skip to content

Commit

Permalink
FEAT: New class ReportPlotter to handle all matplotlib plot (#5297)
Browse files Browse the repository at this point in the history
Co-authored-by: maxcapodi78 <Shark78>
Co-authored-by: Samuelopez-ansys <samuel.lopez@ansys.com>
Co-authored-by: Sébastien Morais <146729917+SMoraisAnsys@users.noreply.github.com>
Co-authored-by: Samuel Lopez <85613111+Samuelopez-ansys@users.noreply.github.com>
  • Loading branch information
4 people authored Oct 18, 2024
1 parent d305939 commit 384b892
Show file tree
Hide file tree
Showing 10 changed files with 1,722 additions and 273 deletions.
9 changes: 9 additions & 0 deletions _unittest/test_12_1_PostProcessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ def test_09_manipulate_report(self):
assert self.aedtapp.post.create_report_from_configuration(
os.path.join(self.local_scratch.path, f"{plot.plot_name}.json"), solution_name=self.aedtapp.nominal_sweep
)
assert self.aedtapp.post.create_report_from_configuration(
os.path.join(self.local_scratch.path, f"{plot.plot_name}.json"),
solution_name=self.aedtapp.nominal_sweep,
matplotlib=True,
)
assert self.aedtapp.post.create_report(
expressions="MaxMagDeltaS",
variations={"Pass": ["All"]},
Expand Down Expand Up @@ -760,6 +765,7 @@ def test_67_sweep_from_json(self):
local_path = os.path.dirname(os.path.realpath(__file__))
dict_vals = read_json(os.path.join(local_path, "example_models", "report_json", "Modal_Report_Simple.json"))
assert self.aedtapp.post.create_report_from_configuration(report_settings=dict_vals)
assert self.aedtapp.post.create_report_from_configuration(report_settings=dict_vals, matplotlib=True)

@pytest.mark.skipif(
config["desktopVersion"] < "2022.2", reason="Not working in non graphical in version lower than 2022.2"
Expand All @@ -769,6 +775,9 @@ def test_70_sweep_from_json(self):
assert self.aedtapp.post.create_report_from_configuration(
os.path.join(local_path, "example_models", "report_json", "Modal_Report.json")
)
assert self.aedtapp.post.create_report_from_configuration(
os.path.join(local_path, "example_models", "report_json", "Modal_Report.json"), matplotlib=True
)

def test_74_dynamic_update(self):
val = self.aedtapp.post.update_report_dynamically
Expand Down
67 changes: 60 additions & 7 deletions _unittest/test_46_FarField.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import shutil

from ansys.aedt.core.visualization.advanced.farfield_visualization import FfdSolutionData
from ansys.aedt.core.visualization.plot.matplotlib import ReportPlotter

# from _unittest.conftest import config
from matplotlib.figure import Figure
import pytest
from pyvista.plotting.plotter import Plotter

Expand Down Expand Up @@ -150,9 +150,9 @@ def test_04_far_field_data(self, local_scratch):
ffdata.plot_cut(output_file=img3, show=False)
assert os.path.exists(img3)
curve_2d = ffdata.plot_cut(show=False)
assert isinstance(curve_2d, Figure)
assert isinstance(curve_2d, ReportPlotter)
data = ffdata.plot_3d_chart(show=False)
assert isinstance(data, Figure)
assert isinstance(data, ReportPlotter)

img4 = os.path.join(self.local_scratch.path, "ff_3d1.jpg")
ffdata.plot_3d(
Expand All @@ -161,6 +161,59 @@ def test_04_far_field_data(self, local_scratch):
assert os.path.exists(img4)
data_pyvista = ffdata.plot_3d(quantity="RealizedGain", show=False, background=[255, 0, 0], show_geometry=False)
assert isinstance(data_pyvista, Plotter)
matplot_lib = ffdata.plot_cut(
quantity="RealizedGain",
primary_sweep="theta",
secondary_sweep_value=[-180, -75, 75],
title=f"Azimuth at {ffdata.frequency}Hz",
quantity_format="dB10",
)
matplot_lib.add_note(
"Hello Pyaedt2",
[10, -10],
color=(1, 1, 0),
bold=True,
italic=True,
back_color=(0, 0, 1),
background_visibility=True,
)

matplot_lib.traces_by_index[0].trace_style = "--"
matplot_lib.x_scale = "log"
_ = matplot_lib.plot_2d()
matplot_lib.add_note(
"Hello Pyaedt",
[0, -10],
color=(1, 1, 0),
bold=False,
italic=False,
background_visibility=False,
)
matplot_lib.x_scale = "linear"
matplot_lib.traces_by_index[0].trace_color = (1, 0, 0)
matplot_lib.grid_enable_minor_x = True
_ = matplot_lib.plot_2d()

matplot_lib.traces["Phi=-180"].symbol_style = "v"
_ = matplot_lib.plot_2d()

matplot_lib.apply_style("dark_background")
matplot_lib.add_limit_line(
[[0, 20, 120], [15, 5, 5]],
properties={
"trace_color": (1, 0, 0),
},
name="LimitLine1",
)
_ = matplot_lib.plot_2d()

matplot_lib.traces_by_index[0].trace_color = (1, 0, 0)
matplot_lib.grid_enable_minor_x = True

matplot_lib.grid_enable_minor_x = False
matplot_lib.grid_enable_minor_y = False

_ = matplot_lib.plot_2d()

def test_05_antenna_plot(self, array_test):
ffdata = array_test.get_antenna_data(sphere="3D")
Expand All @@ -171,7 +224,7 @@ def test_05_antenna_plot(self, array_test):
img1 = os.path.join(self.local_scratch.path, "contour.jpg")
assert ffdata.farfield_data.plot_contour(
quantity="RealizedGain",
title="Contour at {}Hz".format(ffdata.farfield_data.frequency),
title=f"Contour at {ffdata.farfield_data.frequency}Hz",
output_file=img1,
show=False,
)
Expand All @@ -186,7 +239,7 @@ def test_05_antenna_plot(self, array_test):
quantity="RealizedGain",
primary_sweep="theta",
secondary_sweep_value=[-180, -75, 75],
title="Azimuth at {}Hz".format(ffdata.farfield_data.frequency),
title=f"Azimuth at {ffdata.farfield_data.frequency}Hz",
output_file=img2,
show=False,
)
Expand All @@ -197,7 +250,7 @@ def test_05_antenna_plot(self, array_test):
quantity="RealizedGain",
primary_sweep="phi",
secondary_sweep_value=30,
title="Azimuth at {}Hz".format(ffdata.farfield_data.frequency),
title=f"Azimuth at {ffdata.farfield_data.frequency}Hz",
output_file=img3,
show=False,
)
Expand All @@ -208,7 +261,7 @@ def test_05_antenna_plot(self, array_test):
quantity="RealizedGain",
primary_sweep="phi",
secondary_sweep_value=30,
title="Azimuth at {}Hz".format(ffdata.farfield_data.frequency),
title=f"Azimuth at {ffdata.farfield_data.frequency}Hz",
output_file=img3_polar,
show=False,
is_polar=True,
Expand Down
5 changes: 1 addition & 4 deletions doc/source/API/visualization/plot.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ PyAEDT benefits of `matplotlib <https://matplotlib.org/>`_ package and allows to
:toctree: _autosummary
:nosignatures:

plot_polar_chart
plot_3d_chart
plot_2d_chart
plot_contour
ReportPlotter
is_notebook


Expand Down
11 changes: 11 additions & 0 deletions src/ansys/aedt/core/generic/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def __init__(self):
else:
pyaedt_settings_path = os.path.join(os.environ["APPDATA"], "pyaedt_settings.yaml")
self.load_yaml_configuration(pyaedt_settings_path)
self.__block_figure_plot = False

# ########################## Logging properties ##########################

Expand All @@ -230,6 +231,16 @@ def logger(self):
def logger(self, val):
self.__logger = val

@property
def block_figure_plot(self):
"""Block matplotlib figure plot during python script run until the user close it manually.
Default is ``False``."""
return self.__block_figure_plot

@block_figure_plot.setter
def block_figure_plot(self, val):
self.__block_figure_plot = val

@property
def enable_desktop_logs(self):
"""Enable or disable the logging to the AEDT message window."""
Expand Down
2 changes: 1 addition & 1 deletion src/ansys/aedt/core/modeler/pcb/object_3d_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ def plot(
show_legend=True,
save_plot=None,
outline=None,
size=(2000, 1000),
size=(1920, 1440),
plot_components_on_top=False,
plot_components_on_bottom=False,
show=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@
from ansys.aedt.core.generic.general_methods import open_file
from ansys.aedt.core.generic.general_methods import pyaedt_function_handler
from ansys.aedt.core.visualization.advanced.touchstone_parser import read_touchstone
from ansys.aedt.core.visualization.plot.matplotlib import ReportPlotter
from ansys.aedt.core.visualization.plot.matplotlib import is_notebook
from ansys.aedt.core.visualization.plot.matplotlib import plot_2d_chart
from ansys.aedt.core.visualization.plot.matplotlib import plot_3d_chart
from ansys.aedt.core.visualization.plot.matplotlib import plot_contour
from ansys.aedt.core.visualization.plot.matplotlib import plot_polar_chart
from ansys.aedt.core.visualization.plot.pyvista import ModelPlotter
from ansys.aedt.core.visualization.plot.pyvista import get_structured_mesh

Expand Down Expand Up @@ -829,7 +826,7 @@ def plot_contour(
Returns
-------
:class:`matplotlib.pyplot.Figure`
:class:`ansys.aedt.core.visualization.plot.matplotlib.ReportPlotter`
Matplotlib figure object.
Examples
Expand Down Expand Up @@ -860,19 +857,25 @@ def plot_contour(
ph, th = np.meshgrid(data["Phi"], data["Theta"][select])
# Convert to radians for polar plot.
ph = np.radians(ph) if polar else ph

return plot_contour(
plot_data=[data_to_plot, th, ph],
xlabel=r"$\phi$ (Degrees)",
ylabel=r"$\theta$ (Degrees)",
title=title,
levels=levels,
new = ReportPlotter()
new.show_legend = False
new.title = title
props = {
"x_label": r"$\phi$ (Degrees)",
"y_label": r"$\theta$ (Degrees)",
}

new.add_trace([data_to_plot, th, ph], 2, props)
_ = new.plot_contour(
trace=0,
polar=polar,
snapshot_path=output_file,
levels=levels,
max_theta=max_theta,
color_bar=quantity_format,
snapshot_path=output_file,
show=show,
)
return new

@pyaedt_function_handler()
def plot_cut(
Expand Down Expand Up @@ -924,7 +927,7 @@ def plot_cut(
Returns
-------
:class:`matplotlib.pyplot.Figure`
:class:`ansys.aedt.core.visualization.plot.matplotlib.ReportPlotter`
Matplotlib figure object.
If ``show=True``, a Matplotlib figure instance of the plot is returned.
If ``show=False``, the plotted curve is returned.
Expand Down Expand Up @@ -963,7 +966,7 @@ def plot_cut(
y = conversion_function(y, quantity_format)
if not isinstance(y, np.ndarray): # pragma: no cover
raise Exception("Format of quantity is wrong.")
curves.append([x, y, "{}={}".format(y_key, el)])
curves.append([x, y, f"{y_key}={el}"])
elif isinstance(secondary_sweep_value, list):
list_inserted = []
for el in secondary_sweep_value:
Expand All @@ -973,36 +976,28 @@ def plot_cut(
y = conversion_function(y, quantity_format)
if not isinstance(y, np.ndarray): # pragma: no cover
raise Exception("Format of quantity is wrong.")
curves.append([x, y, "{}={}".format(y_key, el)])
curves.append([x, y, f"{y_key}={el}"])
list_inserted.append(theta_idx)
else:
theta_idx = self.__find_nearest(data[y_key], secondary_sweep_value)
y = temp[theta_idx]
y = conversion_function(y, quantity_format)
if not isinstance(y, np.ndarray): # pragma: no cover
raise Exception("Wrong format quantity.")
curves.append([x, y, "{}={}".format(y_key, data[y_key][theta_idx])])

curves.append([x, y, f"{y_key}={data[y_key][theta_idx]}"])

new = ReportPlotter()
new.show_legend = show_legend
new.title = title
props = {"x_label": x_key, "y_label": quantity}
for pdata in curves:
name = pdata[2] if len(pdata) > 2 else "Trace"
new.add_trace(pdata[:2], 0, props, name=name)
if is_polar:
return plot_polar_chart(
curves,
xlabel=x_key,
ylabel=quantity,
title=title,
snapshot_path=output_file,
show_legend=show_legend,
show=show,
)
_ = new.plot_polar(traces=None, snapshot_path=output_file, show=show)
else:
return plot_2d_chart(
curves,
xlabel=x_key,
ylabel=quantity,
title=title,
snapshot_path=output_file,
show_legend=show_legend,
show=show,
)
_ = new.plot_2d(None, output_file, show)
return new

@pyaedt_function_handler()
def plot_3d_chart(
Expand Down Expand Up @@ -1041,10 +1036,9 @@ def plot_3d_chart(
Returns
-------
:class:`matplotlib.pyplot.Figure`
:class:`ansys.aedt.core.visualization.plot.matplotlib.ReportPlotter`
Matplotlib figure object.
If ``show=True``, a Matplotlib figure instance of the plot is returned.
If ``show=False``, the plotted curve is returned.
Examples
--------
Expand Down Expand Up @@ -1078,7 +1072,13 @@ def plot_3d_chart(
x = r * np.sin(theta_grid) * np.cos(phi_grid)
y = r * np.sin(theta_grid) * np.sin(phi_grid)
z = r * np.cos(theta_grid)
return plot_3d_chart([x, y, z], xlabel="Theta", ylabel="Phi", title=title, snapshot_path=output_file, show=show)
new = ReportPlotter()
new.show_legend = False
new.title = title
props = {"x_label": "Theta", "y_label": "Phi", "z_label": quantity}
new.add_trace([x, y, z], 2, props, quantity)
_ = new.plot_3d(trace=0, snapshot_path=output_file, show=show)
return new

@pyaedt_function_handler()
def plot_3d(
Expand Down
Loading

0 comments on commit 384b892

Please sign in to comment.