Skip to content

Instantly share code, notes, and snippets.

@dangpzanco
Last active October 18, 2019 20:22
Show Gist options
  • Save dangpzanco/2906aa9417450f2ad12448aaea97c910 to your computer and use it in GitHub Desktop.
Save dangpzanco/2906aa9417450f2ad12448aaea97c910 to your computer and use it in GitHub Desktop.
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