Core API Reference¶
Core classes and functionality for idmtools.
Overview¶
The idmtools.core package provides the foundational classes and interfaces for building simulations, experiments, and interacting with platforms.
Quick Reference¶
Core Classes¶
| Class | Module | Description |
|---|---|---|
Platform | idmtools.core.platform_factory | Creates a connection to a compute platform |
Experiment | idmtools.entities.experiment | A collection of simulations |
Simulation | idmtools.entities.simulation | A single model run with specific parameters |
SimulationBuilder | idmtools.builders | Builds simulations from parameter sweeps |
CommandLine | idmtools.entities | Defines the command to execute |
AssetCollection | idmtools.assets | A collection of files for simulations |
Asset | idmtools.assets | A single file asset |
ITask | idmtools.entities.itask | Base class for all tasks |
IAnalyzer | idmtools.entities | Base class for analyzers |
Common Methods¶
# Experiment
experiment = Experiment.from_builder(builder, task, name="My Experiment")
experiment.run(platform=platform, wait_until_done=True)
# SimulationBuilder
builder = SimulationBuilder()
builder.add_sweep_definition(callback, values)
# Task
task.set_parameter(key, value)
task.common_assets.add_assets(asset_collection)
task.transient_assets.add_asset("file.py")
# Asset
Asset(filename="config.json", content='{"key": "value"}')
AssetCollection.from_id_file("path/to/id_file.id")
# Platform
platform = Platform("PlatformName")
Platform Factory¶
Platform¶
The main entry point for creating platform instances:
from idmtools.core.platform_factory import Platform
# Create platform
platform = Platform("COMPS", endpoint="...", environment="...")
Key Methods:
get_item()- Retrieve entity by IDcreate_items()- Create entities on platformget_directory()- Get working directory for entityrefresh_status()- Update entity status
Entities¶
IEntity¶
Base interface for all entities: Experiment, Simulation, Suite, ITask, AssetCollection, IWorkflowItem
Methods:
pre_creation()- Hook called before creationpost_creation()- Hook called after creationto_dict()- Serialize to dictionaryfrom_dict()- Deserialize from dictionary
Simulation¶
Represents a single simulation:
from idmtools.entities.simulation import Simulation
from idmtools_models.python.python_task import PythonTask
task = PythonTask(script_path="model.py")
sim = Simulation.from_task(task, name="My Simulation")
Key Methods:
- from_task() - Create from task
- add_asset() - Add input file
- set_parameter() - Set task parameter
Experiment¶
Collection of simulations:
from idmtools.entities.experiment import Experiment
sims_template = TemplatedSimulations(base_task=task)
experiment = Experiment.from_template(sims_template, name="My Experiment")
experiment.run(platform=platform, wait_until_done=True)
Key Methods:
- from_task() - Create from Task
- from_template() - Create from TemplatedSimulations
- from_builder() - Create from SimulationBuilder
- run() - Execute on platform
- refresh_status() - Update status
Suite¶
Collection of experiments:
from idmtools.entities.suite import Suite
suite = Suite(name="Complete Study")
suite.add_experiment(exp1)
suite.add_experiment(exp2)
suite.run(platform=platform)
Key Methods:
add_experiment()- Add experiment to suiterun()- Execute all experiments
Tasks¶
ITask¶
Base interface for tasks:
Methods:
set_parameter()- Set parameter valueget_parameter()- Get parameter valuepre_creation()- Pre-execution hook
CommandTask¶
Execute shell commands:
from idmtools.entities.command_task import CommandTask
task = CommandTask(command="python model.py --beta 0.5")
task.add_asset("model.py")
Attributes:
command- Command to executeparameters- Command parameters
Builders¶
SimulationBuilder¶
Build parameter sweeps:
from idmtools.builders import SimulationBuilder
builder = SimulationBuilder()
builder.add_sweep_definition(
lambda sim, beta: sim.task.set_parameter("beta", beta),
[0.1, 0.2, 0.3, 0.4, 0.5]
)
Key Methods:
add_sweep_definition()- Add parameter sweepbuild()- Generate simulations
Assets¶
AssetCollection¶
Manage input files:
from idmtools.assets import AssetCollection
assets = AssetCollection()
assets.add_asset("config.json")
assets.add_directory("data/")
Key Methods:
add_asset()- Add single fileadd_directory()- Add directoryget_asset()- Get asset by nameremove_asset()- Remove asset
Asset¶
Represents a single file:
from idmtools.assets import Asset
asset = Asset(
absolute_path="/path/to/file.txt",
relative_path="inputs/file.txt"
)
Attributes:
absolute_path- Source file pathrelative_path- Destination path in simulationfilename- File name
Platform Interface¶
IPlatform¶
Interface all platforms implement:
Key Methods:
create_items()- Create entitiesget_item()- Retrieve entityrefresh_status()- Update statusget_directory()- Get working directoryget_files()- List entity filesretrieve_output_files()- Download outputs
EntityStatus¶
Entity status values:
from idmtools.core import EntityStatus
EntityStatus.CREATED
EntityStatus.RUNNING
EntityStatus.SUCCEEDED
EntityStatus.FAILED
EntityStatus.CANCELLED
Configuration¶
IdmConfigParser¶
Configuration file parser:
from idmtools.core.idmtools_ini import IdmConfigParser
config = IdmConfigParser.load()
comps_config = config.get_section("COMPS")
Key Methods:
load()- Load configurationget_section()- Get config sectionget_option()- Get config option
Hooks and Callbacks¶
Pre/Post Creation Hooks¶
def on_pre_creation(item, platform):
print(f"Creating {item.name}")
def on_post_creation(item, platform):
print(f"Created {item.uid}")
experiment.pre_creation_hooks.append(on_pre_creation)
experiment.post_creation_hooks.append(on_post_creation)
See Also¶
- Models API - Task implementations
- Platforms API - Platform implementations
- User Guide - Usage examples
- Tutorials - Hands-on examples