From 63158b2245d93195821d01a0e61ff828befd42b5 Mon Sep 17 00:00:00 2001 From: shimwell Date: Tue, 28 Nov 2023 16:00:54 +0000 Subject: [PATCH] [skip ci] Apply formatting changes --- examples/plot_minimal_example.py | 2 +- examples/plot_plotly_example.py | 12 ++-- src/openmc_regular_mesh_plotter/core.py | 79 ++++++++++++++----------- 3 files changed, 51 insertions(+), 42 deletions(-) diff --git a/examples/plot_minimal_example.py b/examples/plot_minimal_example.py index f5a02b0..8ca865d 100644 --- a/examples/plot_minimal_example.py +++ b/examples/plot_minimal_example.py @@ -80,4 +80,4 @@ plot.title.set_text("") plot.figure.savefig("example_openmc_regular_mesh_plotter.png") -print('file created example_openmc_regular_mesh_plotter.png') \ No newline at end of file +print("file created example_openmc_regular_mesh_plotter.png") diff --git a/examples/plot_plotly_example.py b/examples/plot_plotly_example.py index 1446bb6..faf345e 100644 --- a/examples/plot_plotly_example.py +++ b/examples/plot_plotly_example.py @@ -54,7 +54,7 @@ model = openmc.model.Model(my_geometry, my_materials, my_settings, my_tallies) # sp_filename = model.run() -sp_filename = '/home/jshimwell/reactor_csg_neutronics_model/statepoint.12.h5' +sp_filename = "/home/jshimwell/reactor_csg_neutronics_model/statepoint.12.h5" # post process simulation result statepoint = openmc.StatePoint(sp_filename) @@ -77,19 +77,19 @@ # in a regular mesh all the voxels have the same volume so the [0][0][0] just picks the first volume plot = plot_mesh_tally( - plotting_backend='plotly', + plotting_backend="plotly", tally=my_mesh_tally, outline=True, # enables an outline around the geometry geometry=my_geometry, # needed for outline norm=LogNorm(), # log scale colorbar=True, scaling_factor=scaling_factor, - colorbar_kwargs={'title':'Heating [MJ cm-3s-1]'}, - basis='xz', + colorbar_kwargs={"title": "Heating [MJ cm-3s-1]"}, + basis="xz", ) # setting title of the plot -plot.update_layout({'title': ' made with openmc_regular_mesh_plotter'}) +plot.update_layout({"title": " made with openmc_regular_mesh_plotter"}) plot.show() # plot.write_html("example_openmc_regular_mesh_plotter.html") -print('file created example_openmc_regular_mesh_plotter.html') \ No newline at end of file +print("file created example_openmc_regular_mesh_plotter.html") diff --git a/src/openmc_regular_mesh_plotter/core.py b/src/openmc_regular_mesh_plotter/core.py index 6ab0e1e..325b7ba 100644 --- a/src/openmc_regular_mesh_plotter/core.py +++ b/src/openmc_regular_mesh_plotter/core.py @@ -45,7 +45,7 @@ def plot_mesh_tally( scaling_factor: typing.Optional[float] = None, colorbar_kwargs: dict = {}, outline_kwargs: dict = _default_outline_kwargs, - plotting_backend: str = 'matplotlib', + plotting_backend: str = "matplotlib", **kwargs, ) -> "matplotlib.image.AxesImage": """Display a slice plot of the mesh tally score. @@ -176,10 +176,11 @@ def plot_mesh_tally( slice_index, ) - if plotting_backend == 'plotly': + if plotting_backend == "plotly": import plotly.graph_objects as go - dx = abs(x_min - x_max)/(data.shape[0]-1) - dy = abs(y_min - y_max)/(data.shape[1]-1) + + dx = abs(x_min - x_max) / (data.shape[0] - 1) + dy = abs(y_min - y_max) / (data.shape[1] - 1) fig = go.Figure() fig.add_trace( @@ -191,9 +192,10 @@ def plot_mesh_tally( dy=dy, showscale=colorbar, colorbar=colorbar_kwargs, - colorscale='viridis', - line = {'width': 0} - )) + colorscale="viridis", + line={"width": 0}, + ) + ) fig.update_layout( xaxis_title=xlabel, yaxis_title=ylabel, @@ -207,43 +209,46 @@ def plot_mesh_tally( ) if outline and geometry is not None: - - image_value = get_outline(mesh, basis, slice_index, geometry, outline_by, pixels) - - dx = abs(x_min - x_max)/(image_value.shape[0]-1) - dy = abs(y_min - y_max)/(image_value.shape[1]-1) + image_value = get_outline( + mesh, basis, slice_index, geometry, outline_by, pixels + ) + + dx = abs(x_min - x_max) / (image_value.shape[0] - 1) + dy = abs(y_min - y_max) / (image_value.shape[1] - 1) fig.add_trace( - go.Contour( - z=image_value, - x0=x_min, - dx=dx, - y0=y_min, - dy=dy, - contours_coloring='lines', - colorscale=[[0, 'black'], [1.0, 'black']], - line = {'width': 1, 'color': 'rgb(50,50,50)'} - # contours=dict( - # start=0, - # end=8, - # size=2, - # ), - # ncontours=1 - # line = {'width': 0} - ) + go.Contour( + z=image_value, + x0=x_min, + dx=dx, + y0=y_min, + dy=dy, + contours_coloring="lines", + colorscale=[[0, "black"], [1.0, "black"]], + line={"width": 1, "color": "rgb(50,50,50)"} + # contours=dict( + # start=0, + # end=8, + # size=2, + # ), + # ncontours=1 + # line = {'width': 0} + ) ) print() return fig - elif plotting_backend == 'matplotlib': - - im = axes.imshow(data, extent=(x_min, x_max, y_min, y_max), **default_imshow_kwargs) + elif plotting_backend == "matplotlib": + im = axes.imshow( + data, extent=(x_min, x_max, y_min, y_max), **default_imshow_kwargs + ) if colorbar: fig.colorbar(im, **colorbar_kwargs) if outline and geometry is not None: - - image_value = get_outline(mesh, basis, slice_index, geometry, outline_by, pixels) + image_value = get_outline( + mesh, basis, slice_index, geometry, outline_by, pixels + ) # Plot image and return the axes axes.contour( @@ -256,7 +261,10 @@ def plot_mesh_tally( return axes else: - raise ValueError(f'plotting_backend must be either matplotlib or plotly, not {plotting_backend}') + raise ValueError( + f"plotting_backend must be either matplotlib or plotly, not {plotting_backend}" + ) + def get_outline(mesh, basis, slice_index, geometry, outline_by, pixels): import matplotlib.image as mpimg @@ -327,6 +335,7 @@ def get_outline(mesh, basis, slice_index, geometry, outline_by, pixels): image_value = np.rot90(image_value, 2) return image_value + # TODO currently we allow slice index, but this code will be useful if want to # allow slicing by axis values / coordinates. def get_index_where(self, value: float, basis: str = "xy"):