Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Aug 3, 2024
1 parent f9f0e51 commit 5b2d022
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 86 deletions.
13 changes: 6 additions & 7 deletions src/python/impactx/dashboard/Analyze/analyzeFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import pandas as pd
from trame.app import get_server


# -----------------------------------------------------------------------------
# Trame setup
# -----------------------------------------------------------------------------
Expand All @@ -24,7 +23,7 @@ class analyzeFunctions:
# -----------------------------------------------------------------------------

@staticmethod
def load_data (file_path):
def load_data(file_path):
"""
Reads data from the provided file path.
:param file_path: The path to the file to be read.
Expand All @@ -34,7 +33,7 @@ def load_data (file_path):
return df

@staticmethod
def convert_to_dict (combined_data):
def convert_to_dict(combined_data):
"""
Converts data to dictionary format
for Vuetify data table.
Expand All @@ -50,7 +49,7 @@ def convert_to_dict (combined_data):
return dictionary, headers

@staticmethod
def combine_files (file1_name, file2_name):
def combine_files(file1_name, file2_name):
"""
Merges two files together.
:param file1_name: The name of the first file.
Expand All @@ -63,7 +62,7 @@ def combine_files (file1_name, file2_name):
return pd.merge(file1, file2, how="outer")

@staticmethod
def filter_headers (allHeaders, selected_headers):
def filter_headers(allHeaders, selected_headers):
"""
Retrieves only user-selected headers.
:param allHeaders: The list of all headers.
Expand All @@ -77,7 +76,7 @@ def filter_headers (allHeaders, selected_headers):
return filtered_headers

@staticmethod
def filter_data (allData, selected_headers):
def filter_data(allData, selected_headers):
"""
Retrieves data for user-selected headers.
:param allData: The list of all data.
Expand All @@ -97,7 +96,7 @@ def filter_data (allData, selected_headers):
# Function to print simulation output in terminal view
# -----------------------------------------------------------------------------

async def outputTerminal (simulation_function_name):
async def outputTerminal(simulation_function_name):

Check notice

Code scanning / CodeQL

First parameter of a method is not named 'self' Note

Normal methods should have 'self', rather than 'simulation_function_name', as their first parameter.
ctrl.terminal_println(f"Running {simulation_function_name}...")
ctrl.terminal_println(
f"npart: {state.npart}\nkin_energy_MeV: {state.kin_energy_MeV}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import plotly.graph_objects as go


def line_plot_1d (selected_headers, filtered_data):
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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

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

from impactx import ImpactX

Expand All @@ -21,6 +20,7 @@
from ...Input.latticeConfiguration.latticeMain import save_lattice_elements
from ..plot_PhaseSpaceProjections.phaseSpaceSettings import adjusted_settings_plot


def run_simulation(save_png=True):
"""
This tests using ImpactX and Pandas Dataframes
Expand Down
20 changes: 10 additions & 10 deletions src/python/impactx/dashboard/Analyze/plotsMain.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


# Call plot_over_s
def plot_over_s ():
def plot_over_s():
fig = line_plot_1d(state.selected_headers, state.filtered_data)
ctrl.plotly_figure_update(fig)

Expand All @@ -34,14 +34,14 @@ def plot_over_s ():
# -----------------------------------------------------------------------------


def validPlotOptions (simulationClicked):
def validPlotOptions(simulationClicked):
if simulationClicked:
return list(PLOTS.keys())
else:
return ["Run Simulation To See Options"]


def load_dataTable_data ():
def load_dataTable_data():
combined_files = analyzeFunctions.combine_files(
REDUCED_BEAM_DATA, REF_PARTICLE_DATA
)
Expand Down Expand Up @@ -75,7 +75,7 @@ def load_dataTable_data ():
# -----------------------------------------------------------------------------


def update_data_table ():
def update_data_table():
"""
Combines reducedBeam and refParticle files
and updates data table upon column selection by user
Expand All @@ -90,7 +90,7 @@ def update_data_table ():
)


def update_plot ():
def update_plot():
"""
Performs actions to display correct information,
based on the plot optin selected by the user
Expand All @@ -111,20 +111,20 @@ def update_plot ():


@state.change("selected_headers")
def on_header_selection_change (selected_headers, **kwargs):
def on_header_selection_change(selected_headers, **kwargs):
state.filtered_headers = analyzeFunctions.filter_headers(
state.all_headers, selected_headers
)
state.filtered_data = analyzeFunctions.filter_data(state.all_data, selected_headers)


@state.change("filtered_data", "active_plot")
def on_filtered_data_change (**kwargs):
def on_filtered_data_change(**kwargs):
update_plot()


@ctrl.add("run_simulation")
def run_simulation_and_store ():
def run_simulation_and_store():
workflow = state.selectedWorkflow

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable workflow is not used.
state.plot_options = validPlotOptions(simulationClicked=True)
state.simulation_data = run_simulation()
Expand All @@ -143,7 +143,7 @@ def run_simulation_and_store ():
class AnalyzeSimulation:

@staticmethod
def card ():
def card():
with vuetify.VContainer():
with vuetify.VCard(v_if=("show_table")):
with vuetify.VCol(style="width: 500px;"):
Expand All @@ -163,7 +163,7 @@ def card ():
)

@staticmethod
def plot ():
def plot():
with vuetify.VContainer(
v_if="active_plot === 'Plot Over S'", style="height: 90vh; width: 100vh;"
):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class DistributionFunctions:

@staticmethod
def classAndParametersAndDefaultValueAndType_Twiss ():
def classAndParametersAndDefaultValueAndType_Twiss():
file_path = "/mnt/c/Users/parth/Downloads/vsCode/TrameApplication/Input/distributionParametersCard/twissParameters.txt"

with open(file_path, "r") as file:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
# Main Functions
# -----------------------------------------------------------------------------

def populate_distribution_parameters (selectedDistribution):

def populate_distribution_parameters(selectedDistribution):
"""
Populates distribution parameters based on the selected distribution.
:param selectedDistribution (str): The name of the selected distribution
Expand Down Expand Up @@ -75,17 +76,18 @@ def populate_distribution_parameters (selectedDistribution):
generalFunctions.update_runSimulation_validation_checking()
return selectedDistributionParameters

def update_distribution_parameters (

def update_distribution_parameters(
parameterName, parameterValue, parameterErrorMessage
):
"""
Updates the value of a distribution parameter and its error message.
:param parameterName (str): The name of the parameter to update.
:param parameterValue: The new value for the parameter.
:param parameterErrorMessage: The error message related to the parameter's value.
"""

for param in state.selectedDistributionParameters:
if param["parameter_name"] == parameterName:
param["parameter_default_value"] = parameterValue
Expand All @@ -95,11 +97,13 @@ def update_distribution_parameters (
state.dirty("selectedDistributionParameters")
save_distribution_parameters()


# -----------------------------------------------------------------------------
# Write to file functions
# -----------------------------------------------------------------------------

def parameter_input_checker ():

def parameter_input_checker():
"""
Helper function to check if user input is valid.
:return: A dictionary with parameter names as keys and their validated values.
Expand All @@ -116,7 +120,8 @@ def parameter_input_checker ():

return parameter_input

def save_distribution_parameters ():

def save_distribution_parameters():
"""
Writes user input for distribution parameters in suitable format for simulation code.
:return: An instance of the selected distribution class, initialized with user-provided parameters.
Expand All @@ -128,20 +133,24 @@ def save_distribution_parameters ():
distr = getattr(distribution, distribution_name)(**parameters)
return distr


# -----------------------------------------------------------------------------
# Callbacks
# -----------------------------------------------------------------------------


@state.change("selectedDistribution")
def on_distribution_name_change (selectedDistribution, **kwargs):
def on_distribution_name_change(selectedDistribution, **kwargs):
populate_distribution_parameters(selectedDistribution)


@state.change("selectedDistributionType")
def on_distribution_type_change (**kwargs):
def on_distribution_type_change(**kwargs):
populate_distribution_parameters(state.selectedDistribution)


@ctrl.add("updateDistributionParameters")
def on_distribution_parameter_change (parameter_name, parameter_value, parameter_type):
def on_distribution_parameter_change(parameter_name, parameter_value, parameter_type):
parameter_value, input_type = generalFunctions.determine_input_type(parameter_value)
error_message = generalFunctions.validate_against(parameter_value, parameter_type)

Expand All @@ -150,14 +159,16 @@ def on_distribution_parameter_change (parameter_name, parameter_value, parameter
f"Parameter {parameter_name} was changed to {parameter_value} (type: {input_type})"
)


# -----------------------------------------------------------------------------
# Content
# -----------------------------------------------------------------------------


class DistributionParameters:

@staticmethod
def card ():
def card():
with vuetify.VCard(style="width: 340px; height: 300px"):
with vuetify.VCardTitle("Distribution Parameters"):
vuetify.VSpacer()
Expand Down
24 changes: 12 additions & 12 deletions src/python/impactx/dashboard/Input/generalFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
class generalFunctions:

@staticmethod
def documentation (section_name):
def documentation(section_name):
"""
Opens a tab to the specified section link in the documentation.
:param section_name (str): The name of the documentation section to open.
"""

if section_name == "LatticeElements":
url = "https://impactx.readthedocs.io/en/latest/usage/python.html#lattice-elements"
elif section_name == "BeamDistributions":
Expand All @@ -48,7 +48,7 @@ def documentation (section_name):
# -----------------------------------------------------------------------------

@staticmethod
def determine_input_type (value):
def determine_input_type(value):
"""
Determines the type of the input value.
:param value: The input value whose type needs to be determined.
Expand All @@ -64,7 +64,7 @@ def determine_input_type (value):
return value, str

@staticmethod
def validate_against (input_value, value_type):
def validate_against(input_value, value_type):
"""
Returns an error message if the input value type does not match the desired type.
:param input_value: The value to validate.
Expand Down Expand Up @@ -103,7 +103,7 @@ def validate_against (input_value, value_type):
return ["Unknown type"]

@staticmethod
def update_runSimulation_validation_checking ():
def update_runSimulation_validation_checking():
"""
Checks if any input fields are not provided with the correct input type.
Updates the state to enable or disable the run simulation button.
Expand Down Expand Up @@ -149,7 +149,7 @@ def update_runSimulation_validation_checking ():
# -----------------------------------------------------------------------------

@staticmethod
def findAllClasses (module_name):
def findAllClasses(module_name):
"""
Returns a list of all classes in the given module.
:param module_name: The module to inspect.
Expand All @@ -164,7 +164,7 @@ def findAllClasses (module_name):
return results

@staticmethod
def findInitDocstringForClasses (classes):
def findInitDocstringForClasses(classes):
"""
Retrieves the __init__ docstring of the given classes.
:param classes: A list of typles containing class names.
Expand All @@ -183,7 +183,7 @@ def findInitDocstringForClasses (classes):
return docstrings

@staticmethod
def extractParameters (docstring):
def extractParameters(docstring):
"""
Parses specific information from docstrings.
Aimed to retrieve parameter names, values, and types.
Expand Down Expand Up @@ -223,7 +223,7 @@ def extractParameters (docstring):
return parameters

@staticmethod
def classAndParametersAndDefaultValueAndType (module_name):
def classAndParametersAndDefaultValueAndType(module_name):
"""
Given a module name, outputs a dictionary of class names and their parameters.
Keys are class names, and values are lists of parameter information (name, default value, type).
Expand All @@ -243,19 +243,19 @@ def classAndParametersAndDefaultValueAndType (module_name):
return result

@staticmethod
def selectClasses (module_name):
def selectClasses(module_name):
"""
Given a module name, outputs a list of all class names in the module.
:param module_name: The module to inspect.
:return: A list of class names.
"""

return list(
generalFunctions.classAndParametersAndDefaultValueAndType(module_name)
)

@staticmethod
def convert_to_correct_type (value, desired_type):
def convert_to_correct_type(value, desired_type):
"""
Converts the given value to the desired type.
:param value: The value to convert.
Expand Down
Loading

0 comments on commit 5b2d022

Please sign in to comment.