Skip to content

Commit

Permalink
Merge pull request #11 from IGITUGraz/Maintenance
Browse files Browse the repository at this point in the history
Documentation Updates + Minor Maintenance
  • Loading branch information
maharjun authored May 1, 2023
2 parents f823e66 + 6e2dbf9 commit ddf8c2c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 231 deletions.
69 changes: 51 additions & 18 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,62 @@ How to use
==========
.. code:: python
from simmanager import SimManager
if __name__ == '__main__':
# Store your simulation meta-data in the 'output-root-dir/simulation-name/*'
#^ and use the paths object to get the location of the directories for data/simulation/results
with SimManager("simulation-name", "output-root-dir") as simman:
# paths object gives you access to the paths for your logs/data/results
# see simmanager.paths.Paths for documentation
paths = simman.paths
main() # Run your actual main function with the simulation
import os
from simmanager import SimManager, Paths
For read-only access to the simulation directory for analysis
def simulate_dice_rolls(n_rolls):
# Placeholder for the actual simulation function
import random
return [random.randint(1, 6) for _ in range(n_rolls)]
.. code:: python
import os
from simmanager import Paths
if __name__ == '__main__':
# Use the root dir and simulation name where the simulation data is present
output_dir_path = os.path.join("output-root-dir", "simulation-name")
paths = Paths(output_dir_path)
# Do your analysis here ...
def main_sim(output_paths: Paths):
n_rolls = 1000
rolls = simulate_dice_rolls(n_rolls)
# Save the simulation data using output_paths
with open(output_paths.simulation_path / "dice_rolls.txt", "w") as f:
f.write("\n".join(map(str, rolls)))
def analysis_sim(output_paths: Paths):
# Read the simulation data
with open(output_paths.simulation_path / "dice_rolls.txt", "r") as f:
rolls = [int(line.strip()) for line in f.readlines()]
# Calculate the average roll
avg_roll = sum(rolls) / len(rolls)
# Analysis section
try:
with open(output_paths.simulation_path / "analysis.txt", "w") as f:
f.write("This should fail.")
except PermissionError:
print("Cannot write to the simulation directory. It's write-protected after the simulation.")
# Save analysis results to the results directory
with open(output_paths.results_path / "analysis.txt", "w") as f:
f.write(f"Average roll: {avg_roll:.2f}")
if __name__ == "__main__":
SimName = "DiceSimulation"
root_dir = os.environ.get("RESULTS_ROOT_DIR")
if not root_dir:
raise ValueError("RESULTS_ROOT_DIR environment variable must be set and non-empty")
with SimManager(SimName, root_dir) as simman:
main_sim(simman.paths)
# Initialize a new Paths object with the output path from simman.paths
new_paths = Paths(simman.paths.output_dir_path)
#---------------------------
# Analysis portion
#---------------------------
analysis_sim(new_paths)
.. _tools:

Expand Down
211 changes: 0 additions & 211 deletions simmanager/simdatamanager.py

This file was deleted.

4 changes: 2 additions & 2 deletions simmanager/simmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class SimManager:
2. After creating the directory, the SimManager creates 4 files in the output
directory that contain all the information necessary to reproduce the
simulation. For more details look at the documentation of
:meth:`simmanager.simdatamanager.SimDataManager.create_simulation_data` in the
:meth:`simmanager.simdatamanager.SimDataManager` class.
:meth:`simmanager.simmetadatamanager.SimDataManager.create_simulation_data` in the
:meth:`simmanager.simmetadatamanager.SimDataManager` class.
3. On exit (whether due to exception or not), The simulation manager removes write
permission from all subdirectories of the output directory EXCEPT the results
Expand Down

0 comments on commit ddf8c2c

Please sign in to comment.