Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved plotly plots #26

Merged
merged 2 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 61 additions & 92 deletions src/openmc_geometry_plot/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def main():
👉 Create your ```openmc.Geometry()``` and export the geometry xml file using ```export_to_xml()```.

Not got a geometry.xml file handy, right mouse 🖱️ click and save these links
[ example 1 ](https://fusion-energy.github.io/openmc_geometry_plot/examples/csg_tokamak/geometry.xml),
[ example 2 ](https://fusion-energy.github.io/openmc_geometry_plot/examples/csg_cylinder_box/geometry.xml)
[ example 1 ](https://raw.githubusercontent.com/fusion-energy/openmc_geometry_plot/31be0556f3f34c102cab3de094df08f48acad5ca/examples/csg_tokamak/geometry.xml),
[ example 2 ](https://raw.githubusercontent.com/fusion-energy/openmc_geometry_plot/31be0556f3f34c102cab3de094df08f48acad5ca/examples/csg_cylinder_box/geometry.xml)

"""
)
Expand All @@ -57,7 +57,7 @@ def main():
👉 Create your DAGMC h5m file using tools like [CAD-to-h5m](https://github.com/fusion-energy/cad_to_dagmc)

Not got a DAGMC h5m file handy, right mouse 🖱️ click and save these links
[ example 1 ](https://fusion-energy.github.io/openmc_geometry_plot/examples/dagmc_tokamak/dagmc_180_tokamak.h5m)
[ example 1 ](https://github.com/fusion-energy/openmc_geometry_plot/raw/31be0556f3f34c102cab3de094df08f48acad5ca/examples/dagmc_tokamak/dagmc_180_tokamak.h5m)

"""
)
Expand Down Expand Up @@ -91,6 +91,10 @@ def main():

mat_ids = range(0, len(dag_universe.material_names) + 1)
# mat_names = dag_universe.material_names
all_cells_ids = range(0, dag_universe.n_cells + 1)
all_cells ={}
for cell_id in all_cells_ids:
all_cells[cell_id] =openmc.Cell(cell_id=cell_id)

if len(mat_ids) >= 1:
set_mat_ids = set(mat_ids)
Expand All @@ -113,6 +117,11 @@ def main():
# find all material names
mat_ids = range(0, len(dag_universe.material_names) + 1)

all_cells_ids = range(0, dag_universe.n_cells + 1)
all_cells ={}
for cell_id in all_cells_ids:
all_cells[cell_id] =openmc.Cell(cell_id=cell_id)

if len(mat_ids) >= 1:
set_mat_ids = set(mat_ids)
else:
Expand Down Expand Up @@ -172,18 +181,11 @@ def main():
)
backend = st.sidebar.selectbox(
label="Ploting backend",
options=("matplotlib", "plotly"),
options=("plotly", "matplotlib"),
index=0,
key="geometry_plotting_backend",
help="Create png images with MatPlotLib or HTML plots with Plotly",
)
outline = st.sidebar.selectbox(
label="Outline",
options=("materials", "cells", None),
index=0,
key="outline",
help="Allows an outline to be drawn around the cells or materials, select None for no outline",
)
legend = st.sidebar.selectbox(
label="Legend",
options=(True, False),
Expand Down Expand Up @@ -249,6 +251,15 @@ def main():
slice_min = float(bb[0][slice_index])
slice_max = float(bb[1][slice_index])

if isinstance(slice_min, float) and isinstance(slice_max, float):
slice_value = st.sidebar.slider(
label="Slice value",
min_value=slice_min,
max_value=slice_max,
value=(slice_min + slice_max) / 2,
key="slice_slider",
help="Set the value of the slice axis",
)
if isinstance(x_min, float) and isinstance(x_max, float):
plot_right, plot_left = st.sidebar.slider(
label="Left and right values for the horizontal axis",
Expand All @@ -268,15 +279,6 @@ def main():
key="bottom_top_slider",
help="Set the lowest visible value and highest visible value on the vertical axis",
)
if isinstance(slice_min, float) and isinstance(slice_max, float):
slice_value = st.sidebar.slider(
label="Slice value",
min_value=slice_min,
max_value=slice_max,
value=(slice_min + slice_max) / 2,
key="slice_slider",
help="Set the value of the slice axis",
)

color_by = st.sidebar.selectbox(
label="Color by",
Expand All @@ -285,7 +287,13 @@ def main():
key="color_by",
help="Should the plot be colored by material or by cell",
)

outline = st.sidebar.selectbox(
label="Outline",
options=(True, False),
index=0,
key="outline",
help="Allows an outline to be drawn around the cells or materials, select None for no outline",
)
selected_color_map = st.sidebar.selectbox(
label="Color map", options=colormaps(), index=82
) # index 82 is tab20c
Expand Down Expand Up @@ -355,26 +363,27 @@ def main():
and isinstance(plot_bottom, float)
):

if basis == 'xy':
origin=(
(plot_left+plot_right)/2,
(plot_top+plot_bottom)/2,
slice_value,
)
elif basis == 'yz':
origin=(
slice_value,
(plot_left+plot_right)/2,
(plot_top+plot_bottom)/2,
)
elif basis == 'xz':
origin=(
(plot_left+plot_right)/2,
slice_value,
(plot_top+plot_bottom)/2,
)

if backend == "matplotlib":

if basis == 'xy':
origin=(
(plot_left+plot_right)/2,
(plot_top+plot_bottom)/2,
slice_value,
)
elif basis == 'yz':
origin=(
slice_value,
(plot_left+plot_right)/2,
(plot_top+plot_bottom)/2,
)
elif basis == 'xz':
origin=(
(plot_left+plot_right)/2,
slice_value,
(plot_top+plot_bottom)/2,
)
width_x=plot_left-plot_right
width_y=plot_top-plot_bottom
print('origin',origin)
Expand All @@ -394,7 +403,6 @@ def main():
plt.title(title)
plt.savefig("openmc_plot_geometry_image.png")
st.pyplot(plt)
# st.image("openmc_plot_geometry_image.png", use_column_width="always")

with open("openmc_plot_geometry_image.png", "rb") as file:
st.sidebar.download_button(
Expand All @@ -404,58 +412,19 @@ def main():
mime="image/png",
)
else:
data = [
go.Heatmap(
z=color_data_slice,
showscale=False,
colorscale="viridis",
x0=plot_left,
dx=abs(plot_left - plot_right) / (len(color_data_slice[0]) - 1),
y0=plot_bottom,
dy=abs(plot_bottom - plot_top) / (len(color_data_slice) - 1),
# colorbar=dict(title=dict(side="right", text=cbar_label)),
# text = material_ids,
# hovertemplate=
# # 'material ID = %{z}<br>'+
# "Cell ID = %{z}<br>" +
# # '<br>%{text}<br>'+
# xlabel[:2].title()
# + ": %{x} cm<br>"
# + ylabel[:2].title()
# + ": %{y} cm<br>",
)
]

if outline is not None:
data.append(
go.Contour(
z=outline_data_slice,
x0=plot_left,
dx=abs(plot_left - plot_right)
/ (len(outline_data_slice[0]) - 1),
y0=plot_bottom,
dy=abs(plot_bottom - plot_top)
/ (len(outline_data_slice) - 1),
contours_coloring="lines",
line_width=1,
colorscale=[[0, "rgb(0, 0, 0)"], [1.0, "rgb(0, 0, 0)"]],
showscale=False,
)
)

plot = go.Figure(data=data)

plot.update_layout(
xaxis={"title": xlabel},
# reversed autorange is required to avoid image needing rotation/flipping in plotly
yaxis={"title": ylabel, "autorange": "reversed"},
title=title,
autosize=False,
height=800,
)
plot.update_yaxes(
scaleanchor="x",
scaleratio=1,
from openmc_geometry_plot import plot_plotly
plot = plot_plotly(
my_geometry,
origin=origin,
# width=[width_x,width_y],
pixels=pixels,
basis=basis,
color_by=color_by,
colors=my_colors,
legend=legend,
axis_units=axis_units,
outline=outline,
title=title
)

plot.write_html("openmc_plot_geometry_image.html")
Expand Down
Loading
Loading