Skip to content

Commit

Permalink
Added Close button for Bokeh Python program and branch clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Natsoulas committed Nov 5, 2024
1 parent 1516d00 commit 7747f4d
Show file tree
Hide file tree
Showing 16 changed files with 173 additions and 1,076 deletions.
5 changes: 0 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,3 @@ venv/
# Python packaging
*.egg-info
build/

# Monte Carlo ignore generated data files and test init file.
examples/MonteCarloExamples/scenarioBskSimAttFeedbackMC/
src/utilities/tests/__init__.py
examples/montecarlo_test*/
30 changes: 16 additions & 14 deletions examples/MonteCarloExamples/scenarioBskSimAttFeedbackMC.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# ISC License
#
# Copyright (c) 2016, Autonomous Vehicle Systems Lab, University of Colorado at Boulder
# Copyright (c) 2024, Autonomous Vehicle Systems Lab, University of Colorado at Boulder
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
Expand All @@ -22,7 +22,8 @@
Monte Carlo Simulation for Attitude Feedback Scenario
=====================================================
This script demonstrates how to set up and run Monte Carlo simulations using the Basilisk framework. It uses the attitude feedback scenario as a base and applies various dispersions to create multiple simulation runs.
This script demonstrates how to set up and run Monte Carlo simulations using the Basilisk framework.
It uses the attitude feedback scenario as a base and applies various dispersions to create multiple simulation runs.
Key Features:
-------------
Expand All @@ -34,22 +35,22 @@
How to Use:
-----------
1. Ensure you have Basilisk installed with all required dependencies.
2. Run this script directly to execute the Monte Carlo simulations:
2. Run this script directly to execute the Monte Carlo simulations::
python scenarioBskSimAttFeedbackMC.py
python scenarioBskSimAttFeedbackMC.py
3. The script will run 20 Monte Carlo simulations by default.
3. The script will run 4 Monte Carlo simulations by default.
4. Results will be saved in the 'scenarioBskSimAttFeedbackMC' directory within the script's location.
Monte Carlo Configuration:
--------------------------
- Simulation Function: Uses `scenario_AttFeedback.scenario_AttFeedback` to set up the base scenario.
- Execution Function: Uses `scenario_AttFeedback.runScenario` to run each simulation.
- Execution Count: Set to 20 simulations.
- Archive Directory: Results are saved in 'scenarioBskSimAttFeedbackMC'.
- Simulation Function: Uses ``scenario_AttFeedback.scenario_AttFeedback`` to set up the base scenario.
- Execution Function: Uses ``scenario_AttFeedback.runScenario`` to run each simulation.
- Execution Count: Set to 4 simulations.
- Archive Directory: Results are saved in ``scenarioBskSimAttFeedbackMC``.
- Seed Dispersion: Enabled to randomize the seed for each module.
- Variable Casting: Downcasts retained numbers to float32 to save storage space.
- Dispersion Magnitude File: Produces a .txt file showing dispersion in standard deviation units.
- Dispersion Magnitude File: Produces a ``.txt`` file showing dispersion in standard deviation units.
Applied Dispersions:
--------------------
Expand All @@ -61,9 +62,9 @@
Retention Policy:
-----------------
- Logs 'r_BN_N' from 'sNavTransMsg'
- Logs 'sigma_BR' and 'omega_BR_B' from 'attGuidMsg'
- Uses a callback function 'displayPlots' for data visualization
- Logs ``r_BN_N`` from ``sNavTransMsg``
- Logs ``sigma_BR`` and ``omega_BR_B`` from ``attGuidMsg``
- Uses a callback function ``displayPlots`` for data visualization
Output:
-------
Expand All @@ -72,7 +73,8 @@
Note:
-----
This script serves as a template for setting up Monte Carlo simulations in Basilisk. Users can modify the dispersions, retention policy, and analysis methods to suit their specific simulation needs.
This script serves as a template for setting up Monte Carlo simulations in Basilisk.
Users can modify the dispersions, retention policy, and analysis methods to suit their specific simulation needs.
"""

import inspect
Expand Down
2 changes: 1 addition & 1 deletion examples/MonteCarloExamples/scenarioRerunMonteCarlo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# ISC License
#
# Copyright (c) 2016, Autonomous Vehicle Systems Lab, University of Colorado at Boulder
# Copyright (c) 2024, Autonomous Vehicle Systems Lab, University of Colorado at Boulder
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
Expand Down
111 changes: 55 additions & 56 deletions examples/MonteCarloExamples/scenarioVisualizeMonteCarlo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# ISC License
#
# Copyright (c) 2016, Autonomous Vehicle Systems Lab, University of Colorado at Boulder
# Copyright (c) 2024, Autonomous Vehicle Systems Lab, University of Colorado at Boulder
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
Expand All @@ -20,93 +20,103 @@
r"""
Motivation
----------
This script demonstrates how to plot Monte Carlo data using Bokeh. This tool efficiently visualizes large amounts of simulation data typically generated in Monte Carlo sensitivity analysis studies. Running this script creates an interactive HTML view of the simulation data, allowing users to dynamically zoom into the data for more detail.
This script demonstrates how to plot Monte Carlo data using Bokeh. This tool
efficiently visualizes large amounts of simulation data typically generated
in Monte Carlo sensitivity analysis studies. Running this script creates an
interactive HTML view of the simulation data, allowing users to dynamically
zoom into the data for more detail.
The following plots illustrate what this particular simulation setup will yield:
.. include:: ../MonteCarloPlots_attGuidMsg.sigma_BR_1.rst
.. include:: ../../_images/Scenarios/MonteCarloPlots_attGuidMsg.sigma_BR_1.rst
.. include:: ../MonteCarloPlots_attGuidMsg.sigma_BR_2.rst
.. include:: ../../_images/Scenarios/MonteCarloPlots_attGuidMsg.sigma_BR_2.rst
.. include:: ../MonteCarloPlots_attGuidMsg.sigma_BR_3.rst
.. include:: ../../_images/Scenarios/MonteCarloPlots_attGuidMsg.sigma_BR_3.rst
.. include:: ../MonteCarloPlots_attGuidMsg.omega_BR_B_1.rst
.. include:: ../../_images/Scenarios/MonteCarloPlots_attGuidMsg.omega_BR_B_1.rst
.. include:: ../MonteCarloPlots_attGuidMsg.omega_BR_B_2.rst
.. include:: ../../_images/Scenarios/MonteCarloPlots_attGuidMsg.omega_BR_B_2.rst
.. include:: ../MonteCarloPlots_attGuidMsg.omega_BR_B_3.rst
.. include:: ../../_images/Scenarios/MonteCarloPlots_attGuidMsg.omega_BR_B_3.rst
Efficient Handling of Large Datasets
------------------------------------
This implementation uses Bokeh to efficiently handle and visualize large datasets, including those exceeding one gigabyte in size. Here's how it achieves this:
This implementation uses Bokeh to efficiently handle and visualize large datasets,
including those exceeding one gigabyte in size. Here's how it achieves this:
1. Data Loading: The script uses pandas to load data from pickle files, which is an efficient method for handling large datasets.
1. Data Loading: The script uses pandas to load data from pickle files, which is
an efficient method for handling large datasets.
2. Downsampling: While the full dataset is loaded, not all points are plotted at once. The plot is initially rendered at a lower resolution, and more detail is added as the user zooms in.
2. Dynamic Sampling: While the full dataset is loaded, not all points are plotted at
once. The plot is sized by the user's zoom level and location within the data set.
3. Efficient Rendering: Bokeh uses HTML5 Canvas for rendering, which is highly efficient for displaying large numbers of data points.
3. Efficient Rendering: Bokeh uses HTML5 Canvas for rendering, which is highly
efficient for displaying large numbers of data points.
4. Client-Side Interaction: Most of the interactivity (panning, zooming) happens on the client-side in the browser, reducing the need for constant server communication.
4. Client-Side Interaction: Most of the interactivity (panning, zooming) happens
on the client-side in the browser, reducing the need for constant server
communication.
5. Data Streaming: The plot is updated dynamically as the user interacts with it, loading only the necessary data for the current view.
5. Data Streaming: The plot is updated dynamically as the user interacts with it,
loading only the necessary data for the current view.
This approach allows for smooth interaction with large datasets that would be impractical to plot all at once using traditional plotting libraries.
This approach allows for smooth interaction with large datasets that would be
impractical to plot all at once using traditional plotting libraries.
Configuring a Python Environment For this Script
------------------------------------------------
To run this script, you need to set up a specific Python environment:
1. Use Python 3.8 or higher.
2. Create a dedicated virtual environment and compile Basilisk for this environment.
3. Install the required packages. The necessary dependencies are listed in the `requirements_optional.txt` file in the Basilisk root directory. You can install them using:
#. Ensure Basilisk is compiled for the Python environment you are using.
#. Either compile Basilisk with the optional dependencies already installed,
or install them manually using::
pip install -r requirements_optional.txt
pip install -r requirements_optional.txt
This will install Bokeh and other optional dependencies needed for this script.
This will install Bokeh and other optional dependencies needed for this script.
How to Run the Script
---------------------
Follow these steps to run the script:
1. First, run the `scenarioBskSimAttFeedbackMC.py` script to generate the necessary data files.
#. First, run the ``scenarioBskSimAttFeedbackMC.py`` script to generate the necessary data files::
2. Ensure that the `run()` function call at the bottom of this script is uncommented.
python scenarioBskSimAttFeedbackMC.py
3. Run this script from the command line using:
#. Run this script from the command line using:
To generate a static HTML file:
To generate a static HTML file::
.. code-block:: python
from scenarioVisualizeMonteCarlo import run
run(use_bokeh_server=False)
from scenarioVisualizeMonteCarlo import run
run(use_bokeh_server=False)
To start an interactive Bokeh server::
To start an interactive Bokeh server:
from scenarioVisualizeMonteCarlo import run
run(use_bokeh_server=True)
.. code-block:: python
Alternatively, you can run this script directly from the command line::
from scenarioVisualizeMonteCarlo import run
run(use_bokeh_server=True)
# For static HTML generation
python scenarioVisualizeMonteCarlo.py
Alternatively, you can run this script directly from the command line:
# For interactive Bokeh server
python scenarioVisualizeMonteCarlo.py --bokeh-server
.. code-block:: bash
This will process the data created by ``scenarioBskSimAttFeedbackMC.py``
and open a browser window showing the interactive plot.
# For static HTML generation
python scenarioVisualizeMonteCarlo.py
#. Use the dropdown menus to select different variables and components to plot.
# For interactive Bokeh server
python scenarioVisualizeMonteCarlo.py --bokeh-server
#. Enter specific run numbers in the text input field to highlight those runs on the plot.
This will process the data created by `scenarioBskSimAttFeedbackMC.py` and open a browser window showing the interactive plot.
#. Use the pan, zoom, and reset tools to explore the data in detail.
4. Use the dropdown menus to select different variables and components to plot.
#. Use the close button to close the application, then close the browser tab in your browser.
5. Enter specific run numbers in the text input field to highlight those runs on the plot.
6. Use the pan, zoom, and reset tools to explore the data in detail.
The script will display information about the loaded data, including the number of runs and total data size, at the top of the plot.
The script will display information about the loaded data, including the number of runs
and total data size, at the top of the plot.
"""

Expand All @@ -124,32 +134,21 @@
bokeh_available = bokeh_spec is not None

if bokeh_available:
from bokeh.io import output_file, show, curdoc
from bokeh.io import output_file, curdoc
from bokeh.models import Div
from bokeh.server.server import Server
from bokeh.application import Application
from bokeh.application.handlers.function import FunctionHandler
from tornado.ioloop import IOLoop

def create_document(doc):
logger.info("Starting the create_document function")

data_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "scenarioBskSimAttFeedbackMC")
doc_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "docs")
plot_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "saved_plots")

logger.info(f"Data directory: {data_dir}")
logger.info(f"Doc directory: {doc_dir}")
logger.info(f"Plot directory: {plot_dir}")

try:
logger.info("Creating MonteCarloPlotter instance")
plotter = MonteCarloPlotter(data_dir, save_plots=True, doc_dir=doc_dir)

logger.info("Loading data")
plotter.load_data(['attGuidMsg.sigma_BR', 'attGuidMsg.omega_BR_B'])

logger.info("Creating plot layout")
layout = plotter.show_plots()

doc.add_root(layout)
Expand Down
4 changes: 1 addition & 3 deletions examples/_default.rst
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,9 @@ Monte Carlo Simulations

MC run with RW control <scenarioMonteCarloAttRW>
MC run using Python Spice setup <scenarioMonteCarloSpice>
MC run for Electro Adhesion betweeen two spacecraft <scenarioInOrbitEAdhesionMC>
MC bskSim example folder with bokeh interactive visualization <MonteCarloExamples/index>
MC bskSim example folder with bokeh visualization <MonteCarloExamples/index>
Sensitivity Analysis of a differential drag spacecraft control <scenarioDragSensitivity>


Spacecraft Formation Flying
---------------------------

Expand Down
Loading

0 comments on commit 7747f4d

Please sign in to comment.