@@ -33,29 +33,62 @@ How to use
3333==========
3434.. code :: python
3535
36- from simmanager import SimManager
37- if __name__ == ' __main__' :
38- # Store your simulation meta-data in the 'output-root-dir/simulation-name/*'
39- # ^ and use the paths object to get the location of the directories for data/simulation/results
40- with SimManager(" simulation-name" , " output-root-dir" ) as simman:
41- # paths object gives you access to the paths for your logs/data/results
42- # see simmanager.paths.Paths for documentation
43- paths = simman.paths
44- main() # Run your actual main function with the simulation
36+ import os
37+ from simmanager import SimManager, Paths
4538
4639
47- For read-only access to the simulation directory for analysis
40+ def simulate_dice_rolls (n_rolls ):
41+ # Placeholder for the actual simulation function
42+ import random
43+ return [random.randint(1 , 6 ) for _ in range (n_rolls)]
4844
49- .. code :: python
5045
51- import os
52- from simmanager import Paths
53- if __name__ == ' __main__' :
54- # Use the root dir and simulation name where the simulation data is present
55- output_dir_path = os.path.join(" output-root-dir" , " simulation-name" )
56- paths = Paths(output_dir_path)
57- # Do your analysis here ...
46+ def main_sim (output_paths : Paths):
47+ n_rolls = 1000
48+ rolls = simulate_dice_rolls(n_rolls)
49+ # Save the simulation data using output_paths
50+ with open (output_paths.simulation_path / " dice_rolls.txt" , " w" ) as f:
51+ f.write(" \n " .join(map (str , rolls)))
52+
53+
54+ def analysis_sim (output_paths : Paths):
55+ # Read the simulation data
56+ with open (output_paths.simulation_path / " dice_rolls.txt" , " r" ) as f:
57+ rolls = [int (line.strip()) for line in f.readlines()]
58+
59+ # Calculate the average roll
60+ avg_roll = sum (rolls) / len (rolls)
61+
62+ # Analysis section
63+ try :
64+ with open (output_paths.simulation_path / " analysis.txt" , " w" ) as f:
65+ f.write(" This should fail." )
66+ except PermissionError :
67+ print (" Cannot write to the simulation directory. It's write-protected after the simulation." )
68+
69+ # Save analysis results to the results directory
70+ with open (output_paths.results_path / " analysis.txt" , " w" ) as f:
71+ f.write(f " Average roll: { avg_roll:.2f } " )
72+
73+
74+ if __name__ == " __main__" :
75+ SimName = " DiceSimulation"
76+ root_dir = os.environ.get(" RESULTS_ROOT_DIR" )
77+
78+ if not root_dir:
79+ raise ValueError (" RESULTS_ROOT_DIR environment variable must be set and non-empty" )
80+
81+ with SimManager(SimName, root_dir) as simman:
82+ main_sim(simman.paths)
83+
84+ # Initialize a new Paths object with the output path from simman.paths
85+ new_paths = Paths(simman.paths.output_dir_path)
86+
87+ # ---------------------------
88+ # Analysis portion
89+ # ---------------------------
5890
91+ analysis_sim(new_paths)
5992
6093 .. _tools :
6194
0 commit comments