Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import pathlib # Handle paths (https://pbpython.com/pathlib-intro.html)
import numpy as np
def run_simulation(n=1000):
x = np.random.randn(n)
y = np.random.randn(n,n) @ x
simulation_results = dict(x=x, y=y)
return simulation_results
# Define output folder
output_folder = pathlib.Path('output')
# Create folder if not exists (create parents too)
output_folder.mkdir(parents=True, exist_ok=True)
# Define output path (simulation filename)
output_path = output_folder / 'simulation.npz'
completed = output_path.exists()
if not completed:
simulation_results = run_simulation()
np.savez(str(output_path), **simulation_results)
else:
simulation_results = dict(np.load(str(output_path)))
print(simulation_results['x'].mean(), simulation_results['x'].std())
print(simulation_results['y'].mean(), simulation_results['y'].std())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment