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

Trame Dashboard #651

Merged
merged 112 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
112 commits
Select commit Hold shift + click to select a range
87c7149
Added trame dashboard
proy30 Jul 22, 2024
490b747
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 22, 2024
5c172f4
Added 'variable referencing' tab in lattice settings
proy30 Jul 22, 2024
5cff7a4
Fix merge conflict
proy30 Jul 22, 2024
8e30ff0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 22, 2024
76e53da
Imported elements module for proper simulation running
proy30 Jul 23, 2024
e094e65
Merge branch 'development' of github.com:proy30/impactx into development
proy30 Jul 23, 2024
261e436
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 23, 2024
40b6d68
Labeled static methods in analyzeFunctions
proy30 Jul 23, 2024
fd7fdc3
Merge branch 'development' of github.com:proy30/impactx into development
proy30 Jul 23, 2024
62ae889
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 23, 2024
2267efe
Added static methods across all files which need
proy30 Jul 23, 2024
30ee774
fix: fixed CodeQL warning
proy30 Jul 23, 2024
cd0b99e
fix: ensuring itrable input in generalFunctions.py
proy30 Jul 23, 2024
ea303ab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 23, 2024
87bd1b8
Fixed minor CodeQL errors
proy30 Jul 24, 2024
cf51f35
Added base functionality to print in terminal view
proy30 Jul 24, 2024
0581082
Added files to .gitignore for Trame Dashboard
proy30 Aug 1, 2024
8391a8e
Minor format update and remove obsolete file now in .gitignore
proy30 Aug 1, 2024
dfb421b
Nearly completed terminal integration
proy30 Aug 1, 2024
aeb2bc2
Updated Trame Dashboard .gitignore
proy30 Aug 1, 2024
f9e98ce
Removed file reading for beam distribution and lattice elements
proy30 Aug 1, 2024
11608f4
Removed 'workflow' options
proy30 Aug 2, 2024
caaeacd
Added entry point to impactx-dashboard
proy30 Aug 2, 2024
b2ed9fa
Added __init__ files
proy30 Aug 2, 2024
de0795b
Created entry point
proy30 Aug 2, 2024
3f4edd5
File references are relative paths
proy30 Aug 2, 2024
8e3e30e
Deletion of main.py (part of file rename)
proy30 Aug 2, 2024
9f0efa8
Updated .gitignore
proy30 Aug 2, 2024
f683975
Updated file path reference for plotting over position plot
proy30 Aug 2, 2024
a75fe11
distribution and lattice variables call another function
proy30 Aug 2, 2024
2b80bd6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 2, 2024
74dc33f
Renamed class names
proy30 Aug 3, 2024
214b9e3
fixing pre-commit.ci errors
proy30 Aug 3, 2024
77173ff
Removed unused code in simulation
proy30 Aug 3, 2024
2f93347
Fixed convention and updated docstring/function names for Distributio…
proy30 Aug 3, 2024
9b8c4f3
Updated docstrings
proy30 Aug 3, 2024
9e82486
Fixed function convention spacing
proy30 Aug 3, 2024
160e7ee
Updated function conventions/renamed
proy30 Aug 3, 2024
0802ad8
Renamed files
proy30 Aug 3, 2024
f9f0e51
Deleted/removed unused files, some format update
proy30 Aug 3, 2024
5b2d022
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 3, 2024
da7eb88
Fixing pre-commit.ci
proy30 Aug 3, 2024
a25a080
Corrected f-string formatting
proy30 Aug 3, 2024
172a901
update .gitignore
proy30 Aug 3, 2024
b0e1315
moved main() to own file, jupyter-specific code in own file
proy30 Aug 4, 2024
0dd5ec7
Added class docstrings
proy30 Aug 4, 2024
e37c617
Updated requirements.txt
proy30 Aug 4, 2024
ffe6cd5
Modified code to follow Python/ImpactX styling and convention
proy30 Aug 4, 2024
12903a7
Merge branch 'development' of github.com:proy30/impactx into development
proy30 Aug 4, 2024
f52b0e1
Fixing some more conventions
proy30 Aug 4, 2024
98c1e96
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 4, 2024
736ed10
Allow phase space projections to display
proy30 Aug 4, 2024
a6adc6d
Squash, Adding originalPhaseSpaceProjections plot to Trame
proy30 Aug 4, 2024
14694d0
Squash, removed histogram from originalPlot and changed to scatter
proy30 Aug 4, 2024
14d062e
Squash
proy30 Aug 4, 2024
429d055
Updated phase space projection
proy30 Aug 5, 2024
6692ea7
Disabled not fully completed functionalities
proy30 Aug 5, 2024
c481e5b
fixed class naming convention
proy30 Aug 5, 2024
8282410
Merge branch 'development' of github.com:proy30/impactx into development
proy30 Aug 5, 2024
d40b5fd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 5, 2024
cd75577
Added trame_setup.py
proy30 Aug 5, 2024
47da53e
Updated requirements.txt
proy30 Aug 5, 2024
ecd281c
Added ability to switch between png and interactive for phase space
proy30 Aug 5, 2024
89aa970
Updated gitignore
proy30 Aug 5, 2024
0a32b52
Disabled non-completed features
proy30 Aug 5, 2024
d3427a4
Added initial doxygen
proy30 Aug 5, 2024
5fa1120
Added xterm with broken functionality
proy30 Aug 5, 2024
c0e73a9
Merge branch 'development' of github.com:proy30/impactx into development
proy30 Aug 5, 2024
c5f45b8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 5, 2024
16c3ec1
Added xterm library
proy30 Aug 5, 2024
b7d04a5
Merge branch 'development' of github.com:proy30/impactx into development
proy30 Aug 5, 2024
41b5192
xTerm init function created
proy30 Aug 7, 2024
2d376ab
Fixed simulation code
proy30 Aug 7, 2024
1257e38
Added simple integration of simulation progress in terminal
proy30 Aug 7, 2024
1f19694
Attempt at fixing loading diags file
proy30 Aug 7, 2024
dcb1ad0
Commented out unused toolbar elements
proy30 Aug 7, 2024
82ad3a3
delete code for unused toolbar elements
proy30 Aug 7, 2024
664ac96
Removed commented out code and fixed some format
proy30 Aug 7, 2024
5c3b351
Modified doxygen for dashboard
proy30 Aug 7, 2024
42b15dd
Updated minor bugs
proy30 Aug 7, 2024
2ae0f79
Removed code that is not used
proy30 Aug 7, 2024
e80563e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 7, 2024
2811b5a
MPI support in phase_space
proy30 Aug 7, 2024
b591d2c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 7, 2024
df15f65
MPI support in phase_space
proy30 Aug 7, 2024
ad4e331
Merge branch 'development' of github.com:proy30/impactx into development
proy30 Aug 7, 2024
e637917
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 7, 2024
9d7051c
Doc: Update Dashboard
ax3l Aug 8, 2024
997eb84
Jupyter: Simplify/Rename Imports
ax3l Aug 8, 2024
bab94e0
Update Screenshot
ax3l Aug 8, 2024
075f99e
Run `dos2unix` on all files
ax3l Aug 8, 2024
c979354
Docs: Developer Module
ax3l Aug 8, 2024
a3be9d9
Copyright Headers
ax3l Aug 8, 2024
f72b2c2
Removed debug statements
proy30 Aug 8, 2024
4f2cf0c
Removed validation check for pos. number
proy30 Aug 8, 2024
05511a5
Merge branch 'development' of github.com:proy30/impactx into development
proy30 Aug 8, 2024
a883e3a
Update Doc Lingo
ax3l Aug 8, 2024
56fc74a
Usage Navigation
ax3l Aug 8, 2024
65ba614
removed unused code
proy30 Aug 8, 2024
c58de55
Adjusted plotting over s default columns
proy30 Aug 8, 2024
ef4d68b
Set slice_step_diagnostics to True
proy30 Aug 8, 2024
f88255e
Updated diags file paths to use globbing for flexible naming
proy30 Aug 8, 2024
4baeee9
Removed all print debug statements
proy30 Aug 8, 2024
fa3719a
Moved reading of diags filed to once simulation is ran
proy30 Aug 8, 2024
c11c455
Renamed save_distribution/lattice functions
proy30 Aug 8, 2024
ca16f77
renamed simulation variables
proy30 Aug 8, 2024
6b73060
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 8, 2024
6829202
Removed 'step' and 's' headers for user selection for plot_over_s
proy30 Aug 8, 2024
083d8fd
Added 'keV' to kin_energy units option
proy30 Aug 9, 2024
ea65aa3
Merge branch 'development' of github.com:proy30/impactx into development
proy30 Aug 9, 2024
5b7a7df
Docs: Developer Deps for Trame
ax3l Aug 9, 2024
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
103 changes: 103 additions & 0 deletions src/python/impactx/dashboard/Analyze/analyzeFunctions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import pandas as pd

from impactx import distribution

distribution_parameters_file_path = "output_distribution_parameters.txt"
latticeElement_parameters_file_path = "output_latticeElements_parameters.txt"


class analyzeFunctions:

# -----------------------------------------------------------------------------
# Functions for Beam Characteristic and Ref particle data table
# -----------------------------------------------------------------------------

@staticmethod
def load_data(file_path):
"""
Function to read provided file_path
"""
df = pd.read_csv(file_path, sep=" ")
return df

@staticmethod
def convert_to_dict(combined_data):
"""
Function to convert data into dictionary format.
Used to have correct dataType in Vuetify data table.
"""
dictionary = combined_data.to_dict(orient="records")
columns = combined_data.columns
headers = [
{"text": column.strip(), "value": column.strip()} for column in columns
]
return dictionary, headers

@staticmethod
def combine_files(file1_name, file2_name):
"""
Function to merge two files together.
"""
file1 = analyzeFunctions.load_data(file1_name)
file2 = analyzeFunctions.load_data(file2_name)
return pd.merge(file1, file2, how="outer")

@staticmethod
def filter_headers(allHeaders, selected_headers):
"""
Function to retrieve only retrieve
user selected headers
"""
filtered_headers = []
for selectedHeader in allHeaders:
if selectedHeader["value"] in selected_headers:
filtered_headers.append(selectedHeader)
return filtered_headers

@staticmethod
def filter_data(allData, selected_headers):
"""
Function to retrieve only retrieve data for
user selected headers
"""
filtered_data = []
for row in allData:
filtered_row = {}
for key, value in row.items():
if key in selected_headers:
filtered_row[key] = value
filtered_data.append(filtered_row)
return filtered_data

# -----------------------------------------------------------------------------
# Helper functions to read lattice elements and distribution parameter list
# -----------------------------------------------------------------------------

def read_latticeElements_file():
"""
Function to help run impactX simulation
Returns a list in correct format to read latticeElements.
"""
file_path = latticeElement_parameters_file_path
elements_list = []
with open(file_path, "r") as file:
lines = file.readlines()
for line in lines:
line = line.strip()
if line.startswith("elements."):
element_code = line.replace("elements.", "").rstrip(",")
elements_list.append(eval(f"elements.{element_code}"))
return elements_list

def read_distribution_file():
"""
Function to help run impactX simulation
Reads distribution file path line by line.
"""
file_path = distribution_parameters_file_path
safe_env = {"distribution": distribution, "distr": None}

with open(file_path, "r") as file:
exec(file.read(), safe_env)

return safe_env["distr"]
32 changes: 32 additions & 0 deletions src/python/impactx/dashboard/Analyze/plot_over_s/overS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import plotly.graph_objects as go


def line_plot_1d(selected_headers, filtered_data):
x_axis = selected_headers[1] if len(selected_headers) > 1 else None
y_axis = selected_headers[2:] if len(selected_headers) > 2 else None

x = [row[x_axis] for row in filtered_data] if x_axis else []

figure_data = []
if y_axis:
for column in y_axis:
y = [row[column] for row in filtered_data]
trace = go.Scatter(
x=x,
y=y,
mode="lines+markers",
name=column,
line=dict(width=2),
marker=dict(size=8),
)
figure_data.append(trace)

return go.Figure(
data=figure_data,
layout=go.Layout(
title="Plot Over S",
xaxis=dict(title="s"),
yaxis=dict(title=""),
margin=dict(l=20, r=20, t=25, b=30),
),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
### Only the single plot shows deltaX_delxaP_X


def adjusted_settings_plot(pc, num_bins=50, root_rank=0):
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import gaussian_kde

# Data Histogramming
df = pc.to_df(local=True)

if df is None:
print("DataFrame is None")
return None

# Update for plot unit system
m2mm = 1.0e3
rad2mrad = 1.0e3

# Convert positions to mm and momenta to mrad
df.position_x = df.position_x.multiply(m2mm)
df.momentum_x = df.momentum_x.multiply(rad2mrad)

# Calculate the density of points using a 2D histogram
xy = np.vstack([df.position_x, df.momentum_x])
z = gaussian_kde(xy)(xy)

# Simple scatter plot of position_x and momentum_x with density coloring
fig, ax = plt.subplots(figsize=(8, 4), constrained_layout=True)
scatter = ax.scatter(df.position_x, df.momentum_x, c=z, cmap="viridis", alpha=0.5)
ax.set_xlabel("Delta x [mm]")
ax.set_ylabel("Delta p_x [mrad]")

# Adding a color bar
cbar = plt.colorbar(scatter, ax=ax)
cbar.set_label("Particle Density")

# Adding a legend for the scatter plot (optional if needed for more information)
# We can create custom legend if needed, but color bar usually serves the purpose for density.

"""
# Beam Characteristics
rbc = pc.reduced_beam_characteristics()

# calculate local histograms
xpx, x_edges, px_edges = np.histogram2d(
df["position_x"],
df["momentum_x"],
bins=num_bins,
range=[
[rbc["x_min"] * m2mm, rbc["x_max"] * m2mm],
[rbc["px_min"] * rad2mrad, rbc["px_max"] * rad2mrad],
],
)

# histograms per axis
x = np.sum(xpx, axis=1)
px = np.sum(xpx, axis=0)

# Check histogram contents
print(f"xpx:\n{xpx}\nx:\n{x}\npx:\n{px}\n")

# Matplotlib canvas: figure and plottable axes areas
fig, ax_xpx = plt.subplots(figsize=(8, 4), constrained_layout=True)

# projected axes
ax_x, ax_px = ax_xpx.twinx(), ax_xpx.twiny()

def plot_2d(hist, r, p, r_edges, p_edges, ax_r, ax_p, ax_rp):
hist = np.ma.masked_where(hist == 0, hist)
im = ax_rp.imshow(
hist.T,
origin="lower",
aspect="auto",
extent=[r_edges[0], r_edges[-1], p_edges[0], p_edges[-1]],
)
cbar = fig.colorbar(im, ax=ax_rp)

r_mids = (r_edges[:-1] + r_edges[1:]) / 2
p_mids = (p_edges[:-1] + p_edges[1:]) / 2
ax_r.plot(r_mids, r, c="w", lw=0.8, alpha=0.7)
ax_r.plot(r_mids, r, c="k", lw=0.5, alpha=0.7)
ax_r.fill_between(r_mids, r, facecolor="k", alpha=0.2)
ax_p.plot(p, p_mids, c="w", lw=0.8, alpha=0.7)
ax_p.plot(p, p_mids, c="k", lw=0.5, alpha=0.7)
ax_p.fill_betweenx(p_mids, p, facecolor="k", alpha=0.2)

return cbar

cbar_xpx = plot_2d(xpx, x, px, x_edges, px_edges, ax_x, ax_px, ax_xpx)

def set_limits(r, p, r_edges, p_edges, ax_r, ax_p, ax_rp):
pad = 0.1
len_r = r_edges[-1] - r_edges[0]
len_p = p_edges[-1] - p_edges[0]
ax_rp.set_xlim(r_edges[0] - len_r * pad, r_edges[-1] + len_r * pad)
ax_rp.set_ylim(p_edges[0] - len_p * pad, p_edges[-1] + len_p * pad)

def on_xlims_change(axes):
if not axes.xlim_reset_in_progress:
pad = 6.0
axes.xlim_reset_in_progress = True
axes.set_xlim(0, np.max(p) * pad)
axes.xlim_reset_in_progress = False

ax_p.xlim_reset_in_progress = False
ax_p.callbacks.connect("xlim_changed", on_xlims_change)
on_xlims_change(ax_p)

def on_ylims_change(axes):
if not axes.ylim_reset_in_progress:
pad = 6.0
axes.ylim_reset_in_progress = True
axes.set_ylim(0, np.max(r) * pad)
axes.ylim_reset_in_progress = False

ax_r.ylim_reset_in_progress = False
ax_r.callbacks.connect("ylim_changed", on_ylims_change)
on_ylims_change(ax_r)

set_limits(x, px, x_edges, px_edges, ax_x, ax_px, ax_xpx)

ax_xpx.set_xlabel("Delta x [mm]")
ax_xpx.set_ylabel("Delta p_x [mrad]")
cbar_xpx.set_label("Q [C/bin]")
ax_x.set_yticks([])
ax_px.set_xticks([])

return fig
"""

return fig
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env python3
#
# Copyright 2022-2023 The ImpactX Community
#
# Authors: Axel Huebl
# License: BSD-3-Clause-LBNL
#
# -*- coding: utf-8 -*-

from trame.app import get_server

server = get_server(client_type="vue2")
state, ctrl = server.state, server.controller
import importlib

import pytest
from Analyze.analyzeFunctions import analyzeFunctions
from Analyze.plot_phase_space.phaseSpaceSettings import adjusted_settings_plot

from impactx import ImpactX


@pytest.mark.skipif(
importlib.util.find_spec("pandas") is None, reason="pandas is not available"
)
def run_simulation(save_png=True):
"""
This tests using ImpactX and Pandas Dataframes
"""
sim = ImpactX()

sim.particle_shape = state.particle_shape
sim.space_charge = False
sim.slice_step_diagnostics = False
sim.init_grids()

# init particle beam
kin_energy_MeV = state.kin_energy_MeV
bunch_charge_C = state.bunch_charge_C
npart = state.npart

# reference particle
pc = sim.particle_container()
ref = pc.ref_particle()
ref.set_charge_qe(-1.0).set_mass_MeV(0.510998950).set_kin_energy_MeV(kin_energy_MeV)

# particle bunch
# distr = distribution.Waterbag(
# lambdaX=3.9984884770e-5,
# lambdaY=3.9984884770e-5,
# lambdaT=1.0e-3,
# lambdaPx=2.6623538760e-5,
# lambdaPy=2.6623538760e-5,
# lambdaPt=2.0e-3,
# muxpx=-0.846574929020762,
# muypy=0.846574929020762,
# mutpt=0.0,
# )
distr = analyzeFunctions.read_distribution_file()
sim.add_particles(bunch_charge_C, distr, npart)

assert pc.total_number_of_particles() == npart

# init accelerator lattice
# fodo = [
# elements.Drift(0.25),
# elements.Quad(1.0, 1.0),
# elements.Drift(0.5),
# elements.Quad(1.0, -1.0),
# elements.Drift(0.25),
# ]
fodo = analyzeFunctions.read_latticeElements_file()

sim.lattice.extend(fodo)

# simulate
sim.evolve()

# check local particles
df = pc.to_df(local=True)
print(df)

# ensure the column heads are correctly labeled
assert df.columns.tolist() == [
"idcpu",
"position_x",
"position_y",
"position_t",
"momentum_x",
"momentum_y",
"momentum_t",
"qm",
"weighting",
]

# fig = pc.plot_phasespace()
fig = adjusted_settings_plot(pc)

return fig
Loading