Back to all questions

How do I use the MatCraft Python SDK?

Getting Started
python
sdk
programming

The Python SDK (materia) gives you full programmatic control over MatCraft. It is the most flexible way to interact with the platform and is ideal for integration into existing Python workflows, Jupyter notebooks, and CI/CD pipelines.

Installation

bash
pip install matcraft

For GPU-accelerated surrogate training:

bash
pip install matcraft[gpu]

Basic Workflow

python
from materia import Campaign, Material
from materia.surrogate import MLPSurrogate
from materia.optimize import CMAESOptimizer
from materia.active_learning import ActiveLearningLoop

# 1. Define material from YAML or programmatically
material = Material(
    name="my-alloy",
    components=[
        {"name": "iron", "type": "continuous", "bounds": [0.5, 0.95]},
        {"name": "chromium", "type": "continuous", "bounds": [0.05, 0.3]},
        {"name": "nickel", "type": "continuous", "bounds": [0.0, 0.2]},
    ],
    objectives=[
        {"name": "hardness", "direction": "maximize"},
        {"name": "corrosion_resistance", "direction": "maximize"},
    ],
    constraints=[
        {"type": "sum_equals", "components": ["iron", "chromium", "nickel"], "value": 1.0},
    ],
)

# 2. Load seed data
import pandas as pd
data = pd.read_csv("initial_measurements.csv")
material.add_data(data)

# 3. Configure and run campaign
campaign = Campaign(
    material=material,
    surrogate=MLPSurrogate(hidden_layers=[64, 64]),
    optimizer=CMAESOptimizer(sigma0=0.3),
    max_iterations=20,
    batch_size=5,
)

# Run the full optimization loop
results = campaign.run()

# 4. Analyze results
print(results.best_candidates(n=10))
print(results.pareto_front())
results.plot_convergence()  # Renders in Jupyter or saves to file

Connecting to a Remote Server

python
from materia import Client

client = Client(
    base_url="https://api.matcraft.ai/api/v1",
    token="mc_live_abc123...",
)

# List existing campaigns
campaigns = client.campaigns.list()

# Create a new campaign on the server
campaign = client.campaigns.create(
    material_id="mat-001",
    max_iterations=20,
)

# Stream real-time updates
for event in client.campaigns.stream(campaign.id):
    print(f"Iteration {event.iteration}: best = {event.best_value}")

Key SDK Classes

| Class | Purpose | |———-|————-| | Material | Define components, objectives, constraints | | Campaign | Orchestrate the optimization loop | | MLPSurrogate | Neural network surrogate model | | CMAESOptimizer | CMA-ES optimization strategy | | ActiveLearningLoop | Acquisition-driven iteration | | Client | Remote API client |

The SDK is fully typed with comprehensive docstrings. Use your IDE's autocomplete or run help(Campaign) in a Python REPL for detailed parameter documentation.

Related Questions