From 802f10864ce5448c349904372ea439b9a5e89641 Mon Sep 17 00:00:00 2001 From: Yaraslau Tamashevich Date: Fri, 8 Sep 2023 09:45:04 +0300 Subject: [PATCH 1/4] Add label for geometrical object --- python/geom.py | 3 ++- python/simulation.py | 4 ++++ python/visualization.py | 41 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/python/geom.py b/python/geom.py index b4756ab21..a4bf4950c 100755 --- a/python/geom.py +++ b/python/geom.py @@ -1067,7 +1067,7 @@ class GeometricObject: ``` """ - def __init__(self, material=Medium(), center=Vector3(), epsilon_func=None): + def __init__(self, material=Medium(), center=Vector3(), epsilon_func=None, label=None): """ Construct a `GeometricObject`. @@ -1095,6 +1095,7 @@ def __init__(self, material=Medium(), center=Vector3(), epsilon_func=None): epsilon_func.eps = True material = epsilon_func + self.label = label self.material = material self.center = Vector3(*center) diff --git a/python/simulation.py b/python/simulation.py index 390dc6121..0908e3821 100644 --- a/python/simulation.py +++ b/python/simulation.py @@ -4745,6 +4745,7 @@ def plot2D( output_plane: Optional[Volume] = None, fields: Optional = None, labels: bool = False, + label_geometry: bool = False, eps_parameters: Optional[dict] = None, boundary_parameters: Optional[dict] = None, source_parameters: Optional[dict] = None, @@ -4797,6 +4798,8 @@ def plot2D( no fields are superimposed. * `labels`: if `True`, then labels will appear over each of the simulation elements. Defaults to `False`. + * `label_geometry`: if `True`, then labels will appear over each of the geometry + elements. Defaults to `False`. * `eps_parameters`: a `dict` of optional plotting parameters that override the default parameters for the geometry. - `interpolation='spline36'`: interpolation algorithm used to upsample the pixels. @@ -4871,6 +4874,7 @@ def plot2D( output_plane=output_plane, fields=fields, labels=labels, + label_geometry = label_geometry, eps_parameters=eps_parameters, boundary_parameters=boundary_parameters, source_parameters=source_parameters, diff --git a/python/visualization.py b/python/visualization.py index a0545ec14..e658ee1b0 100644 --- a/python/visualization.py +++ b/python/visualization.py @@ -92,6 +92,8 @@ default_label_parameters = {"label_color": "r", "offset": 20, "label_alpha": 0.3} +default_label_geometry_parameters = {"label_color": "w", "arrow_color": "r", "offset": 20, "label_alpha": 0.8} + # Used to remove the elements of a dictionary (dict_to_filter) that # don't correspond to the keyword arguments of a particular # function (func_with_kwargs.) @@ -138,6 +140,10 @@ def place_label( offset = label_parameters["offset"] alpha = label_parameters["label_alpha"] color = label_parameters["label_color"] + if "arrow_color" in label_parameters: + arrow_color = label_parameters["arrow_color"] + else: + arrow_color = color if x > centerx: xtext = -offset @@ -156,7 +162,7 @@ def place_label( ha="center", va="bottom", bbox=dict(boxstyle="round,pad=0.2", fc=color, alpha=alpha), - arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0.5", color=color), + arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0.5", color=arrow_color), ) return ax @@ -507,6 +513,7 @@ def plot_eps( eps_parameters: Optional[dict] = None, colorbar_parameters: Optional[dict] = None, frequency: Optional[float] = None, + label_geometry: bool = False, ) -> Union[Axes, Any]: # consolidate plotting parameters if eps_parameters is None: @@ -615,6 +622,36 @@ def plot_eps( colorbar_parameters=colorbar_parameters, ) + + if label_geometry: + for el in sim.geometry: + if sim_size.x == 0: + center_first = el.center.y + center_second = el.center.z + sim_first = sim_center.y + sim_second = sim_center.z + elif sim_size.y == 0: + center_first = el.center.y + center_second = el.center.z + sim_first = sim_center.y + sim_second = sim_center.z + elif sim_size.z == 0: + center_first = el.center.x + center_second = el.center.y + sim_first = sim_center.x + sim_second = sim_center.y + ax = place_label( + ax, + el.label, + center_first, + center_second, + sim_first, + sim_second, + label_parameters = default_label_geometry_parameters, + ) + + + ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) return ax @@ -903,6 +940,7 @@ def plot2D( output_plane: Optional[Volume] = None, fields: Optional = None, labels: bool = False, + label_geometry: bool = False, eps_parameters: Optional[dict] = None, boundary_parameters: Optional[dict] = None, source_parameters: Optional[dict] = None, @@ -945,6 +983,7 @@ def plot2D( eps_parameters=eps_parameters, colorbar_parameters=colorbar_parameters, frequency=frequency, + label_geometry=label_geometry, ) # Plot boundaries From fb4cc8f796e8a3fa841037b26ce9429092402659 Mon Sep 17 00:00:00 2001 From: Yaraslau Tamashevich Date: Fri, 8 Sep 2023 10:36:28 +0300 Subject: [PATCH 2/4] Fix formatting --- python/geom.py | 4 +++- python/simulation.py | 2 +- python/visualization.py | 16 ++++++++++------ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/python/geom.py b/python/geom.py index a4bf4950c..6642bf31b 100755 --- a/python/geom.py +++ b/python/geom.py @@ -1067,7 +1067,9 @@ class GeometricObject: ``` """ - def __init__(self, material=Medium(), center=Vector3(), epsilon_func=None, label=None): + def __init__( + self, material=Medium(), center=Vector3(), epsilon_func=None, label=None + ): """ Construct a `GeometricObject`. diff --git a/python/simulation.py b/python/simulation.py index 0908e3821..931942fe8 100644 --- a/python/simulation.py +++ b/python/simulation.py @@ -4874,7 +4874,7 @@ def plot2D( output_plane=output_plane, fields=fields, labels=labels, - label_geometry = label_geometry, + label_geometry=label_geometry, eps_parameters=eps_parameters, boundary_parameters=boundary_parameters, source_parameters=source_parameters, diff --git a/python/visualization.py b/python/visualization.py index e658ee1b0..b08e68b2f 100644 --- a/python/visualization.py +++ b/python/visualization.py @@ -92,7 +92,12 @@ default_label_parameters = {"label_color": "r", "offset": 20, "label_alpha": 0.3} -default_label_geometry_parameters = {"label_color": "w", "arrow_color": "r", "offset": 20, "label_alpha": 0.8} +default_label_geometry_parameters = { + "label_color": "w", + "arrow_color": "r", + "offset": 20, + "label_alpha": 0.8, +} # Used to remove the elements of a dictionary (dict_to_filter) that # don't correspond to the keyword arguments of a particular @@ -162,7 +167,9 @@ def place_label( ha="center", va="bottom", bbox=dict(boxstyle="round,pad=0.2", fc=color, alpha=alpha), - arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0.5", color=arrow_color), + arrowprops=dict( + arrowstyle="->", connectionstyle="arc3,rad=0.5", color=arrow_color + ), ) return ax @@ -622,7 +629,6 @@ def plot_eps( colorbar_parameters=colorbar_parameters, ) - if label_geometry: for el in sim.geometry: if sim_size.x == 0: @@ -647,11 +653,9 @@ def plot_eps( center_second, sim_first, sim_second, - label_parameters = default_label_geometry_parameters, + label_parameters=default_label_geometry_parameters, ) - - ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) return ax From e547bfcee4e942559e5a2fd2512a95571f1cc494 Mon Sep 17 00:00:00 2001 From: Yaraslau Tamashevich Date: Sun, 10 Sep 2023 18:29:41 +0300 Subject: [PATCH 3/4] Change default behaviour and check label for None --- python/simulation.py | 4 ++-- python/visualization.py | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/python/simulation.py b/python/simulation.py index 931942fe8..08eaa61a5 100644 --- a/python/simulation.py +++ b/python/simulation.py @@ -4745,7 +4745,7 @@ def plot2D( output_plane: Optional[Volume] = None, fields: Optional = None, labels: bool = False, - label_geometry: bool = False, + label_geometry: bool = True, eps_parameters: Optional[dict] = None, boundary_parameters: Optional[dict] = None, source_parameters: Optional[dict] = None, @@ -4799,7 +4799,7 @@ def plot2D( * `labels`: if `True`, then labels will appear over each of the simulation elements. Defaults to `False`. * `label_geometry`: if `True`, then labels will appear over each of the geometry - elements. Defaults to `False`. + elements. Defaults to `True`. * `eps_parameters`: a `dict` of optional plotting parameters that override the default parameters for the geometry. - `interpolation='spline36'`: interpolation algorithm used to upsample the pixels. diff --git a/python/visualization.py b/python/visualization.py index b08e68b2f..df1377178 100644 --- a/python/visualization.py +++ b/python/visualization.py @@ -646,15 +646,16 @@ def plot_eps( center_second = el.center.y sim_first = sim_center.x sim_second = sim_center.y - ax = place_label( - ax, - el.label, - center_first, - center_second, - sim_first, - sim_second, - label_parameters=default_label_geometry_parameters, - ) + if el.label is not None: + ax = place_label( + ax, + el.label, + center_first, + center_second, + sim_first, + sim_second, + label_parameters=default_label_geometry_parameters, + ) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) From 4730bb59980cbcd8351c5e69b60b5a88498680c2 Mon Sep 17 00:00:00 2001 From: Yaraslau Tamashevich Date: Wed, 11 Oct 2023 11:22:01 +0300 Subject: [PATCH 4/4] fix bug --- python/visualization.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/visualization.py b/python/visualization.py index df1377178..ff1589a73 100644 --- a/python/visualization.py +++ b/python/visualization.py @@ -637,9 +637,9 @@ def plot_eps( sim_first = sim_center.y sim_second = sim_center.z elif sim_size.y == 0: - center_first = el.center.y + center_first = el.center.x center_second = el.center.z - sim_first = sim_center.y + sim_first = sim_center.x sim_second = sim_center.z elif sim_size.z == 0: center_first = el.center.x