Skip to content

Commit 1fe04e1

Browse files
committed
Added central post-processing script
1 parent d57e436 commit 1fe04e1

File tree

5 files changed

+101
-64
lines changed

5 files changed

+101
-64
lines changed

rocketpy/stochastic/post_processing/scripts/stochastic_apogee.py

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
from pathlib import Path
2-
31
import matplotlib.pyplot as plt
42
import numpy as np
53

6-
from rocketpy.stochastic.post_processing.stochastic_cache import \
7-
SimulationCache
4+
from rocketpy.stochastic.post_processing.stochastic_cache import SimulationCache
85

96

10-
def compute_apogee(file_name, batch_path):
11-
cache = SimulationCache(
12-
file_name,
13-
batch_path,
14-
)
7+
def compute_apogee(cache):
158
apogee = cache.read_outputs('apogee')
169

1710
mean_apogee = float(np.nanmean(apogee, axis=0))
@@ -36,17 +29,21 @@ def plot_apogee(batch_path, apogee, mean_apogee, save=False, show=True):
3629
ax.legend()
3730

3831
if save:
39-
plt.savefig(batch_path / 'mean_apogee_distribution.png')
32+
plt.savefig(batch_path / "Figures" / 'mean_apogee_distribution.png')
4033
if show:
4134
plt.show()
4235

4336

44-
def run(file_name, batch_path, save, show):
45-
apogee, mean_apogee = compute_apogee(file_name, batch_path)
46-
plot_apogee(batch_path, apogee, mean_apogee, save=save, show=show)
37+
def run(cache, save, show):
38+
apogee, mean_apogee = compute_apogee(cache)
39+
plot_apogee(cache.batch_path, apogee, mean_apogee, save=save, show=show)
4740

4841

4942
if __name__ == '__main__':
50-
batch_path = Path("mc_simulations/")
43+
import easygui
44+
45+
# configuration
5146
file_name = 'monte_carlo_class_example'
52-
run(file_name, batch_path, save=True, show=True)
47+
batch_path = easygui.diropenbox(title="Select the batch path")
48+
cache = SimulationCache(file_name, batch_path)
49+
run(cache, save=True, show=True)

rocketpy/stochastic/post_processing/scripts/stochastic_impact_point.py

+12-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
from pathlib import Path
2-
31
import matplotlib.pyplot as plt
42
import numpy as np
53
from matplotlib.patches import Ellipse
64

7-
from rocketpy.stochastic.post_processing.stochastic_cache import \
8-
SimulationCache
5+
from rocketpy.stochastic.post_processing.stochastic_cache import SimulationCache
96

107
# 1-3 sigma
118
lower_percentiles = [0.16, 0.03, 0.003]
@@ -19,11 +16,9 @@ def eigsorted(cov):
1916
return vals[order], vecs[:, order]
2017

2118

22-
def compute_impact(file_name, batch_path, save, show):
23-
cache = SimulationCache(
24-
file_name,
25-
batch_path,
26-
)
19+
def compute_impact(cache, save, show):
20+
batch_path = cache.batch_path
21+
2722
x_impact = cache.read_outputs('x_impact')
2823
y_impact = cache.read_outputs('y_impact')
2924

@@ -68,20 +63,21 @@ def compute_impact(file_name, batch_path, save, show):
6863
ax.grid()
6964

7065
if save:
71-
plt.savefig(batch_path / 'mean_impact_distribution.png')
66+
plt.savefig(batch_path / "Figures" / 'mean_impact_distribution.png')
7267

7368
if show:
7469
plt.show()
75-
plt.show()
7670

7771

78-
def run(file_name, batch_path, save, show):
79-
compute_impact(file_name, batch_path, save, show)
72+
def run(cache, save, show):
73+
compute_impact(cache, save, show)
8074

8175

8276
if __name__ == '__main__':
83-
# import easygui
77+
import easygui
8478

85-
batch_path = Path("mc_simulations/")
79+
# configuration
8680
file_name = 'monte_carlo_class_example'
87-
run(file_name, batch_path, save=True, show=True)
81+
batch_path = easygui.diropenbox(title="Select the batch path")
82+
cache = SimulationCache(file_name, batch_path)
83+
run(cache, save=True, show=True)
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
from pathlib import Path
2-
31
import matplotlib.pyplot as plt
42

5-
from rocketpy.stochastic.post_processing.stochastic_cache import \
6-
SimulationCache
3+
from rocketpy.stochastic.post_processing.stochastic_cache import SimulationCache
4+
75

6+
def compute_mach(cache, save, show):
7+
batch_path = cache.batch_path
88

9-
def compute_mach(file_name, batch_path, save, show):
10-
cache = SimulationCache(
11-
file_name,
12-
batch_path,
13-
)
149
mach = cache.read_outputs('mach_number')
1510

1611
fig, ax = plt.subplots()
@@ -19,24 +14,24 @@ def compute_mach(file_name, batch_path, save, show):
1914
ax.set_xlabel('Time [s]')
2015
ax.set_ylabel('Mach [-]')
2116
ax.set_title('Mach number Distribution')
22-
ax.legend()
2317
ax.grid()
2418

2519
if save:
26-
plt.savefig(batch_path / 'mach_distribution.png')
20+
plt.savefig(batch_path / "Figures" / 'mach_distribution.png')
2721

2822
if show:
2923
plt.show()
30-
plt.show()
3124

3225

33-
def run(file_name, batch_path, save, show):
34-
compute_mach(file_name, batch_path, save, show)
26+
def run(cache, save, show):
27+
compute_mach(cache, save, show)
3528

3629

3730
if __name__ == '__main__':
38-
# import easygui
31+
import easygui
3932

40-
batch_path = Path("mc_simulations/")
33+
# configuration
4134
file_name = 'monte_carlo_class_example'
42-
run(file_name, batch_path, save=True, show=True)
35+
batch_path = easygui.diropenbox(title="Select the batch path")
36+
cache = SimulationCache(file_name, batch_path)
37+
run(cache, save=True, show=True)
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
from pathlib import Path
2-
31
import matplotlib.pyplot as plt
42
import numpy as np
53

6-
from rocketpy.stochastic.post_processing.stochastic_cache import \
7-
SimulationCache
4+
from rocketpy.stochastic.post_processing.stochastic_cache import SimulationCache
5+
86

7+
def compute_maxQ(cache, save, show):
8+
batch_path = cache.batch_path
99

10-
def compute_maxQ(file_name, batch_path, save, show):
11-
cache = SimulationCache(
12-
file_name,
13-
batch_path,
14-
)
1510
dyn_press = cache.read_outputs('dynamic_pressure') / 1000
1611
maxQarg = np.nanargmax(dyn_press[:, :, 1], axis=1)
1712
maxQ = dyn_press[np.arange(len(dyn_press)), maxQarg]
@@ -27,20 +22,21 @@ def compute_maxQ(file_name, batch_path, save, show):
2722
ax.grid()
2823

2924
if save:
30-
plt.savefig(batch_path / 'maxQ_distribution.png')
25+
plt.savefig(batch_path / "Figures" / 'maxQ_distribution.png')
3126

3227
if show:
3328
plt.show()
34-
plt.show()
3529

3630

37-
def run(file_name, batch_path, save, show):
38-
compute_maxQ(file_name, batch_path, save, show)
31+
def run(cache, save, show):
32+
compute_maxQ(cache, save, show)
3933

4034

4135
if __name__ == '__main__':
42-
# import easygui
36+
import easygui
4337

44-
batch_path = Path("mc_simulations/")
38+
# configuration
4539
file_name = 'monte_carlo_class_example'
46-
run(file_name, batch_path, save=True, show=True)
40+
batch_path = easygui.diropenbox(title="Select the batch path")
41+
cache = SimulationCache(file_name, batch_path)
42+
run(cache, save=True, show=True)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# from pathlib import Path
2+
import importlib
3+
import pkgutil
4+
from pathlib import Path
5+
6+
from rocketpy.stochastic.post_processing import scripts
7+
from rocketpy.stochastic.post_processing.stochastic_cache import SimulationCache
8+
9+
10+
def list_scripts(package):
11+
scripts = []
12+
for module in pkgutil.iter_modules(package.__path__):
13+
scripts.append(module)
14+
return scripts
15+
16+
17+
def run_all_scripts(file_name, batch_path, save, show):
18+
# initialize path
19+
batch_path = Path(batch_path)
20+
21+
figures_path = batch_path / "Figures"
22+
figures_path.mkdir(parents=True, exist_ok=True)
23+
24+
# list all post_processing scripts
25+
scripts_list = list_scripts(scripts)
26+
27+
# iniialize cache
28+
cache = SimulationCache(file_name, batch_path)
29+
30+
# for each script, execute 'run' giving the cache as argument
31+
for module in scripts_list:
32+
print("Running script: ", module.name)
33+
34+
# get the module
35+
# Import the module using the module name
36+
module = importlib.import_module(f"{scripts.__name__}.{module.name}")
37+
38+
# Extract the function named 'run'
39+
run_function = getattr(module, 'run')
40+
41+
run_function(cache, save, show)
42+
43+
44+
if __name__ == "__main__":
45+
import easygui
46+
47+
# configuration
48+
file_name = 'monte_carlo_class_example'
49+
batch_path = easygui.diropenbox(title="Select the batch path")
50+
save = True
51+
show = False
52+
53+
run_all_scripts(file_name, Path(batch_path), save, show)

0 commit comments

Comments
 (0)