petri_net_nn.rest¶
rest ¶
HTTP wrapper around a trained :class:PetriNetModule.
A REST API is the simplest "talk to PETRA from any language" surface. Non-Python consumers — Java services, Go workers, React dashboards, Postman scratchpads — speak JSON over HTTP and don't want to bother with model loading, torch weights, or the dict-of-tensor interface PETRA uses natively.
This module exposes a :func:build_app factory that takes a
trained module and returns a FastAPI application. The returned
app exposes six endpoints, listed in the order an analyst would
typically hit them:
GET /healthz— liveness probe (always 200).GET /schema— net structure (places, transitions, labels, initial marking).POST /forward— inference: marking + optional value channel → per-transition activations.POST /anomaly— trace scoring: events + marking → per-transition residuals + trace score.POST /counterfactual— minimal change to flip one target transition's firing decision.POST /sensitivity— per-input gradients of one target transition at a base point.
FastAPI auto-generates OpenAPI 3.x schemas and Swagger UI from
the Pydantic models below — point a browser at /docs to
exercise the API interactively. Request and response bodies are
plain JSON with no PETRA-specific encoding; place ids and
transition ids are just strings.
The fastapi / pydantic imports are guarded so that
import petri_net_nn works without the [rest] extra
installed — :func:build_app will raise a clear ImportError
when called if the dependencies are missing. The Pydantic
models below are only defined when the imports succeed; this
avoids the Pydantic-v2 forward-reference issue that hits when
models referencing each other are defined inside a function
scope.
What's deliberately NOT included in this slice:
- Multi-model registries (one app == one trained module). Spin up multiple FastAPI processes if you need multiple models served from one host.
- Training endpoints — this is an inference API. Training mutates the module's parameters and is expensive enough that it doesn't belong on a synchronous HTTP request path.
- Authentication / rate limiting / CORS — wire these in at the caller's app layer using FastAPI's standard middleware.
HealthResponse ¶
Bases: BaseModel
Liveness probe payload.
SchemaResponse ¶
Bases: BaseModel
Static structural information about the served net.
Returned by GET /schema; useful for discovering the
place and transition vocabulary the other endpoints
accept and emit.
ForwardRequest ¶
Bases: BaseModel
Inputs to the forward pass.
input_marking keys are place ids; values are
activations in [0, 1]. Places not present in the dict
default to their initial-marking activation (1.0 if
flagged in the net's initial marking, 0.0 otherwise).
input_values is the optional coloured-token value
channel — same shape, value scale chosen by the
modeller's domain.
ForwardResponse ¶
Bases: BaseModel
Per-transition firing activations from one forward pass, plus the per-place activations the net carries at the end of the pass.
TraceEvent ¶
Bases: BaseModel
One event in a trace.
name is matched against the net's transition labels
— the same convention the offline parse_xes /
anomaly_score pipeline uses. Unknown event names
are silently ignored (consistent with offline
semantics).
AnomalyRequest ¶
Bases: BaseModel
Score a trace under the trained module.
The request bundles the input marking the trace was observed under (so the model can compute its expected firings) and the list of events the trace actually contained.
AnomalyResponse ¶
Bases: BaseModel
Per-transition residuals plus the trace-level scalar (sum of residuals). Higher score = more anomalous; per-transition entries identify which parts of the process diverged.
CounterfactualRequest ¶
Bases: BaseModel
Find the minimum change to one input that flips the target transition's firing decision.
CounterfactualResponse ¶
Bases: BaseModel
Counterfactual analysis result. found is False
when no crossing of the firing-decision boundary exists
in the supplied search range; the other fields are then
absent.
SensitivityRequest ¶
Bases: BaseModel
Per-input gradient analysis of one transition's firing activation at the supplied base point.
SensitivityResponse ¶
Bases: BaseModel
Per-place gradient magnitudes for marking and value channels separately. Larger absolute values = the firing decision pivots more on that input locally.
build_app ¶
Build a FastAPI application that wraps a trained module.
The returned object is a fastapi.FastAPI instance; the
return annotation is Any to keep the fastapi dependency
optional at the type level.
Parameters¶
module
The trained :class:PetriNetModule to serve. The
module's net structure determines what places and
transitions the API endpoints accept and emit by name.
title, version, description
Forwarded to FastAPI(...) and surface in the
auto-generated OpenAPI / Swagger UI at /docs.
Returns¶
fastapi.FastAPI Mount with uvicorn or any ASGI server. A minimal production runner::
import uvicorn
from petri_net_nn import load_scenario, build_app
ctx = load_scenario("my_scenario.toml")
module, _ = ctx.train()
app = build_app(module)
uvicorn.run(app, host="0.0.0.0", port=8000)
Raises¶
ImportError
If fastapi or pydantic isn't available. Install with
pip install 'petra-nn[rest]'.
Source code in petri_net_nn/rest.py
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 | |