RunCard¶
RunCards
are used to store metrics and artifacts related to DataCards
and ModelCards
. While a RunCard can be used as a object itself, it's best when used as part of a Project
run.
Creating A Run¶
Runs are unique context-managed executions associated with a Project
that record all created cards and their associated metrics, params, and artifacts to a single card called a RunCard
.
The following example shows how to create a simple run as well as use CardInfo
to store helper info
from sklearn.linear_model import LinearRegression
from opsml import (
CardInfo,
DataCard,
DataSplit,
ModelCard,
OpsmlProject,
PandasData,
ProjectInfo,
SklearnModel,
)
from opsml.helpers.data import create_fake_data
info = ProjectInfo(name="opsml-project", repository="opsml", contact="user@email.com")
# create card info and set NAME, REPOSITORY, and CONTACT as environment variables
card_info = CardInfo(name="linear-reg", repository="opsml", contact="user@email.com").set_env()
# create project
project = OpsmlProject(info=info)
with project.run() as run:
# create fake data
X, y = create_fake_data(n_samples=1000, task_type="regression")
X["target"] = y
# Create data interface
data_interface = PandasData(
data=X,
data_splits=[
DataSplit(label="train", column_name="col_1", column_value=0.5, inequality=">="),
DataSplit(label="test", column_name="col_1", column_value=0.5, inequality="<"),
],
dependent_vars=["target"],
)
# Create datacard
datacard = DataCard(interface=data_interface)
run.register_card(card=datacard)
# split data
data = datacard.split_data()
# fit model
reg = LinearRegression()
reg.fit(data["train"].X.to_numpy(), data["train"].y.to_numpy())
# create model interface
interface = SklearnModel(model=reg, sample_data=data["train"].X.to_numpy())
# create modelcard
modelcard = ModelCard(interface=interface, to_onnx=True, datacard_uid=datacard.uid)
# you can log metrics view log_metric or log_metrics
run.log_metric("test_metric", 10)
run.log_metrics({"test_metric2": 20})
# log parameter
run.log_parameter("test_parameter", 10)
# register modelcard
run.register_card(card=modelcard)
# example of logging artifact to file
with Path("artifact.txt").open("w") as f:
f.write("This is a test")
run.log_artifact_from_file("artifact", "artifact.txt")
You can now log into the OpsML
server and see your recent run and associated metadata
Logging Hardware¶
Runs
can also log hardware information by simply changing 1 input argument.
-
Instead of
with project.run() as run:
usewith project.run(log_hardware=True) as run:
and now hardware information will be logged. For information on what hardware information is logged, see the documentation below. -
If you want to change the hardware logging time interval, simply change the
hardware_interval
in therun
methodwith project.run(log_hardware=True, hardware_interval=10) as run:
. Note: thehardware_interval
is in seconds and the lowest value is 10 seconds. Default is 30 seconds.
opsml.RunCard
¶
Bases: ArtifactCard
Create a RunCard from specified arguments.
Apart from required args, a RunCard must be associated with one of datacard_uid, modelcard_uids or pipelinecard_uid
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
Run name |
required | |
repository |
Repository that this card is associated with |
required | |
contact |
Contact to associate with card |
required | |
info |
Name, repository, and contact are required arguments for all cards. They can be provided
directly or through a |
required | |
datacard_uids |
Optional DataCard uids associated with this run |
required | |
modelcard_uids |
Optional List of ModelCard uids to associate with this run |
required | |
pipelinecard_uid |
Optional PipelineCard uid to associate with this experiment |
required | |
metrics |
Optional dictionary of key (str), value (int, float) metric paris. Metrics can also be added via class methods. |
required | |
parameters |
Parameters associated with a RunCard |
required | |
artifact_uris |
Optional dictionary of artifact uris associated with artifacts. |
required | |
uid |
Unique id (assigned if card has been registered) |
required | |
version |
Current version (assigned if card has been registered) |
required |
Source code in opsml/cards/run.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
|
add_tag(key, value)
¶
Logs tags to current RunCard
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
Key for tag |
required |
value |
str
|
value for tag |
required |
Source code in opsml/cards/run.py
211 212 213 214 215 216 217 218 219 220 221 |
|
add_tags(tags)
¶
Logs tags to current RunCard
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tags |
Dict[str, str]
|
Dictionary of tags |
required |
Source code in opsml/cards/run.py
223 224 225 226 227 228 229 230 231 |
|
log_parameter(key, value)
¶
Logs parameter to current RunCard
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
Param name |
required |
value |
Union[int, float, str]
|
Param value |
required |
Source code in opsml/cards/run.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
|
log_parameters(parameters)
¶
Logs parameters to current RunCard
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parameters |
Dict[str, Union[float, int, str]]
|
Dictionary of parameters |
required |
Source code in opsml/cards/run.py
311 312 313 314 315 316 317 318 319 320 321 322 |
|
log_metric(key, value, timestamp=None, step=None)
¶
Logs metric to the existing RunCard metric dictionary
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
Metric name |
required |
value |
Union[int, float]
|
Metric value |
required |
timestamp |
Optional[int]
|
Optional timestamp |
None
|
step |
Optional[int]
|
Optional step associated with name and value |
None
|
Source code in opsml/cards/run.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
|
log_metrics(metrics, step=None)
¶
Log metrics to the existing RunCard metric dictionary
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metrics |
Dict[str, Union[float, int]]
|
Dictionary containing key (str) and value (float or int) pairs to add to the current metric set |
required |
step |
Optional[int]
|
Optional step associated with metrics |
None
|
Source code in opsml/cards/run.py
381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
|
opsml.projects.OpsmlProject
¶
Source code in opsml/projects/project.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
|
datacard_uids: List[str]
property
¶
DataCards associated with the current run
modelcard_uids: List[str]
property
¶
ModelCards associated with the current run
run_id: str
property
writable
¶
Current run id associated with project
__init__(info)
¶
Instantiates a project which creates cards, metrics and parameters to the opsml registry via a "run" object.
If info.run_id is set, that run_id will be loaded as read only. In read only mode, you can retrieve cards, metrics, and parameters, however you cannot write new data. If you wish to record data/create a new run, you will need to enter the run context.
In order to create new cards, you need to create a run using the run
context manager.
Example:
project: OpsmlProject = OpsmlProject(
ProjectInfo(
name="test-project",
# If run_id is omitted, a new run is created.
run_id="123ab123kaj8u8naskdfh813",
)
)
# the project is in "read only" mode. all read operations will work
for k, v in project.parameters:
logger.info("{} = {}", k, v)
# creating a project run
with project.run() as run:
# Now that the run context is entered, it's in read/write mode
# You can write cards, parameters, and metrics to the project.
run.log_parameter(key="my_param", value="12.34")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
info |
ProjectInfo
|
Run information. if a run_id is given, that run is set as the project's current run. |
required |
Source code in opsml/projects/project.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
|
get_metric(name)
¶
Get metric by name
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
str |
required |
Returns:
Type | Description |
---|---|
List[Metric]
|
List of Metric or Metric |
Source code in opsml/projects/project.py
218 219 220 221 222 223 224 225 226 227 228 229 |
|
get_parameter(name)
¶
Get param by name
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
str |
required |
Returns:
Type | Description |
---|---|
List[Param]
|
List of Param or Param |
Source code in opsml/projects/project.py
235 236 237 238 239 240 241 242 243 244 245 246 |
|
list_runs(limit=100)
¶
Lists all runs for the current project, sorted by timestamp
Returns:
Type | Description |
---|---|
List[Dict[str, Any]]
|
List of RunCard |
Source code in opsml/projects/project.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
|
load_card(registry_name, info)
¶
Loads a Card.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
registry_name |
str
|
Name of registry to load card from |
required |
info |
CardInfo
|
Card information to retrieve. |
required |
Returns
Card
Source code in opsml/projects/project.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
|
run(run_name=None, log_hardware=False, hardware_interval=_DEFAULT_INTERVAL, code_dir=None)
¶
Starts a new run for the project
Parameters:
Name | Type | Description | Default |
---|---|---|---|
run_name |
Optional[str]
|
Optional run name |
None
|
log_hardware |
bool
|
Whether to log hardware metrics |
False
|
hardware_interval |
int
|
Interval to log hardware metrics. Default is 30 seconds. |
_DEFAULT_INTERVAL
|
code_dir |
Optional[Union[str, Path]]
|
Top-level directory containing code to be logged. If not provided, the directory containing the current file will be used. |
None
|
Source code in opsml/projects/project.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
opsml.types.hardware.HardwareMetrics
¶
Bases: BaseModel
Source code in opsml/types/hardware.py
53 54 55 56 57 |
|
opsml.types.hardware.CPUMetrics
¶
Bases: BaseModel
CPU metrics data model.
Source code in opsml/types/hardware.py
16 17 18 19 20 21 22 23 |
|
opsml.types.hardware.MemoryMetrics
¶
Bases: BaseModel
Memory metrics data model.
Source code in opsml/types/hardware.py
33 34 35 36 37 38 39 40 41 42 43 |
|
opsml.types.hardware.NetworkRates
¶
Bases: BaseModel
Network rates data model.
Source code in opsml/types/hardware.py
46 47 48 49 50 |
|
opsml.types.hardware.GPUMetrics
¶
Bases: BaseModel
GPU metrics data model.
Source code in opsml/types/hardware.py
26 27 28 29 30 |
|