Skip to content

Latest commit

 

History

History
180 lines (117 loc) · 5.13 KB

USING_LOCAL_MODEL_PYTHON.md

File metadata and controls

180 lines (117 loc) · 5.13 KB

📘 How to Use the Simulation Script

This guide explains how to use the provided example script run_model.py, which contains all parts of the simulation workflow. The script includes input generation, calling the executable, and post-processing the output. This documentation serves as a guide to understanding the run_model.py script workflow and interpreting its outputs.


🧵 Table of Contents

  1. Introduction
  2. Requirements
  3. Setting Up the Script
  4. Step-by-Step Instructions
  5. Troubleshooting
  6. Conclusion

🔍 Introduction

You will have the run_model.py script available, which consists of the following steps:

  • Input Generation: Creates input data for the simulation
  • Simulation Execution: Calls the executable to run the model
  • Output Processing: Reads and reshapes the output data into desired format

This guide outlines each step to help you understand the workflow and results.


⚙️ Requirements

  • Python 3.x installed
  • model.exe executable in the working folder
  • Required Python packages:
    pip install numpy pandas

📜 Setting Up the Script

  1. Ensure model.exe is located in the same folder as your run_model.py script.
  2. The first time you run the script, you may not have the Y_out.csv file, as it is generated by the executable.
  3. Verify all paths and dependencies are correctly configured in your working directory.

🚀 Quick Run Instructions

If you just want to run the run_model.py script, follow these steps:

  1. Configure the Script:
    Set your desired variables directly within the run_model.py script. This includes input parameters, file paths, and any other configuration options.

  2. Open Your Terminal or Command Prompt.

  3. Execute the Script:
    Run the following command:

    python run_model.py
    
    

📝 Detailed Step-by-Step Instructions for the Content of the Script :


📚 Section 1: Creating Input and Calling the Executable

This part of the run_model.py script generates input data, saves it to a file, and calls the executable.


✅ Step 1: Define Input Parameters

# Define the input vector X_input with the desired values
X_input = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 42])

# Optionally, create a batch of N input vectors (N copies of X_input)
N = 3
X_input_batch = np.tile(X_input, (N, 1))  # Shape will be (N, 9)

✅ Step 2: Save the Input Data to input.txt

input_file_path = 'input.txt'
np.savetxt(input_file_path, X_input_batch, delimiter=',')
print(f'Input data written to {input_file_path}')

✅ Step 3: Run the Executable and Capture Its Output

exe_path = './model.exe'  # Path to the executable in the current folder
command = [exe_path, input_file_path]

print('Running simulation executable...')
result = subprocess.run(command, capture_output=True, text=True)

# Print the output from the executable
print(result.stdout)

📚 Section 2: Post-processing the Output

After running the simulation, the run_model.py script processes the generated Y_out.csv file.


✅ Step 1: Load the Output Data

output_file_path = 'Y_out.csv'
df = pd.read_csv(output_file_path, header=None)

print(f'Simulation output data loaded from {output_file_path}')

✅ Step 2: Extract Sample Indices and Clean Data

# Extract unique sample indices (Column 7 contains sample indices)
sample_indices = df[6].unique()
num_samples = len(sample_indices)

# Drop the sample index column
df = df.drop(columns=[6])
  • Column 7 contains sample indices, which are extracted and then dropped to clean the output Matrix

✅ Step 3: Reshape the Output Data

# Convert the DataFrame to a NumPy array and reshape into the desired 3D format (num_time_steps x num_features x num_samples)
Y_out = df.to_numpy().reshape(num_samples, 60, 6).transpose(1, 2, 0)

✅ Step 4: Example: Access Output Data for Analysis

print('Output values for the first timestep for all 6 features of sample 3:')
print(Y_out[0, :, 2])

🛠️ Troubleshooting

  • Executable Not Found: Ensure model.exe is in the correct folder or update the exe_path.
  • Missing Y_out.csv: This file is only generated after running the executable. Run the executable first.
  • Package Errors: Ensure required packages are installed:
    pip install numpy pandas

✅ Conclusion

  • The run_model.py script contains the entire workflow of input generation, executing the simulation, and processing the output.
  • You can customize input vectors, batch sizes, etc. as needed for your specific use case.
  • This guide provides a clear understanding of each part of the workflow and how the output data is structured.