Skip to Content
CLICLI Examples

CLI Examples

Real-world examples of using OptixLog CLI to instrument Python code.

Basic Python Script

Before

# simulation.py import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 10, 100) y = np.sin(x) plt.plot(x, y) plt.title("Sine Wave") plt.savefig("plot.png")

After (Basic Mode)

optixlog add-logging simulation.py
import optixlog import os import numpy as np import matplotlib.pyplot as plt # Initialize OptixLog with optixlog.run( run_name="experiment", api_key=os.getenv("OPTIX_API_KEY"), project=os.getenv("OPTIX_PROJECT", "MyProject"), config={}, create_project_if_not_exists=True ) as client: x = np.linspace(0, 10, 100) y = np.sin(x) plt.plot(x, y) plt.title("Sine Wave") # Log the plot fig = plt.gcf() client.log_matplotlib("plot", fig) plt.savefig("plot.png")

After (Smart Mode)

optixlog add-logging simulation.py --smart
import optixlog import os import numpy as np import matplotlib.pyplot as plt if __name__ == "__main__": # Initialize OptixLog with context manager with optixlog.run( run_name="experiment", api_key=os.getenv("OPTIX_API_KEY"), project=os.getenv("OPTIX_PROJECT", "MyProject"), config={}, create_project_if_not_exists=True ) as client: # Your experiment code here x = np.linspace(0, 10, 100) y = np.sin(x) # Use client.log_matplotlib() instead of manual PIL conversion fig, ax = plt.subplots() ax.plot(x, y) ax.set_title("Sine Wave") client.log_matplotlib("plot", fig) plt.savefig("plot.png")

Simulation with Metrics

Before

# training.py import numpy as np losses = [] accuracies = [] for epoch in range(100): loss = train_step() accuracy = validate() losses.append(loss) accuracies.append(accuracy) print(f"Epoch {epoch}: loss={loss:.4f}, accuracy={accuracy:.4f}")

After

optixlog add-logging training.py --smart
import optixlog import os import numpy as np if __name__ == "__main__": with optixlog.run( run_name="experiment", api_key=os.getenv("OPTIX_API_KEY"), project=os.getenv("OPTIX_PROJECT", "MyProject"), config={}, create_project_if_not_exists=True ) as client: losses = [] accuracies = [] for epoch in range(100): loss = train_step() accuracy = validate() losses.append(loss) accuracies.append(accuracy) # Log metrics client.log(step=epoch, loss=loss, accuracy=accuracy) print(f"Epoch {epoch}: loss={loss:.4f}, accuracy={accuracy:.4f}") # Log final plot client.log_plot("loss_curve", range(100), losses, title="Training Loss", ylabel="Loss") client.log_plot("accuracy_curve", range(100), accuracies, title="Accuracy", ylabel="Accuracy")

Jupyter Notebook

Before

# Cell 1 import numpy as np import matplotlib.pyplot as plt # Cell 2 x = np.linspace(0, 10, 100) y = np.sin(x) # Cell 3 plt.plot(x, y) plt.title("Sine Wave")

After

optixlog add-logging notebook.ipynb

The CLI transforms the notebook to include OptixLog initialization and logging in appropriate cells.

Parameter Sweep

Before

# sweep.py wavelengths = [1.3, 1.4, 1.5, 1.6] for wavelength in wavelengths: transmission = simulate(wavelength) print(f"λ={wavelength}: T={transmission:.4f}")

After

optixlog add-logging sweep.py --smart
import optixlog import os wavelengths = [1.3, 1.4, 1.5, 1.6] for wavelength in wavelengths: with optixlog.run( run_name=f"sweep_{wavelength}", api_key=os.getenv("OPTIX_API_KEY"), project=os.getenv("OPTIX_PROJECT", "MyProject"), config={"wavelength": wavelength}, create_project_if_not_exists=True ) as client: transmission = simulate(wavelength) # Log result client.log(step=0, transmission=transmission, wavelength=wavelength) print(f"λ={wavelength}: T={transmission:.4f}")

Meep Simulation

Before

# meep_sim.py import meep as mp sim = mp.Simulation( cell_size=mp.Vector3(10, 10, 0), resolution=30, sources=[...] ) for step in range(100): sim.run(until=1) field = sim.get_array(...) # Save field data

After

optixlog add-logging meep_sim.py --smart
import optixlog import os import meep as mp if __name__ == "__main__": with optixlog.run( run_name="experiment", api_key=os.getenv("OPTIX_API_KEY"), project=os.getenv("OPTIX_PROJECT", "MyProject"), config={"resolution": 30, "cell_size": [10, 10, 0]}, create_project_if_not_exists=True ) as client: sim = mp.Simulation( cell_size=mp.Vector3(10, 10, 0), resolution=30, sources=[...] ) for step in range(100): sim.run(until=1) field = sim.get_array(...) # Log field snapshot every 10 steps if step % 10 == 0: client.log_array_as_image(f"field_{step}", field, cmap='RdBu', title=f"Field at step {step}")

Batch Processing Multiple Files

# Instrument all Python files in directory for file in *.py; do echo "Instrumenting $file..." optixlog add-logging "$file" --smart done # Run all for file in *.py; do echo "Running $file..." python "$file" done # View all runs optixlog runs --limit 20

Comparison: Basic vs Smart Mode

Basic Mode (Backend Transformation)

  • Uses backend API for transformation
  • Standard OptixLog initialization
  • Manual PIL conversion for plots
  • Requires backend connection

Smart Mode (Local Transformation)

  • Works locally (no backend needed)
  • Uses SDK helper methods
  • Automatic matplotlib → PIL conversion
  • Context manager wrapping
  • Helpful comments and suggestions
  • Recommended for most use cases

Next Steps