Forecasting Inference¶
twinweaver.utils.forecasting_inference ¶
Forecasting inference helpers for vLLM-based text generation.
Provides functions to generate forecasting predictions (future lab values,
vitals, etc.) by sending instruction prompts to an OpenAI-compatible API
(e.g. a local vLLM server) and parsing the model's text output back into
structured DataFrames via :class:~twinweaver.instruction.converter_instruction.ConverterInstruction.
The prompt construction is driven by
:class:~twinweaver.instruction.converter_instruction.ConverterInstruction
(specifically its forward_conversion_inference method), so the same
code works for any dataset / prompt template.
Typical usage
import asyncio from twinweaver.common.config import Config from twinweaver.utils.forecasting_inference import ( ... run_forecasting_inference, ... parse_forecasting_results, ... ) config = Config()
prompts_with_meta is a list of dicts with keys:¶
"patientid", "instruction", "split_date"¶
results = asyncio.run(run_forecasting_inference( ... prompts_with_meta, ... prediction_url="http://localhost:8000/v1/", ... prediction_model="my-model", ... )) df = parse_forecasting_results(results, converter, dm)
Functions¶
parse_forecasting_results ¶
parse_forecasting_results(
raw_results,
converter,
data_manager,
*,
drop_failures=False,
aggregate_samples=True
)
Parse raw generated texts into structured DataFrames via reverse conversion.
For each patient the function:
- Calls
converter.reverse_conversionon every generated text to obtain structured forecasting DataFrames. - When n_samples > 1 and
aggregate_samplesisTrue, aggregates the multiple trajectories usingconverter.aggregate_multiple_responses. - Returns a single long-format DataFrame with all patients' predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_results
|
list[dict or None]
|
Output of :func: |
required |
converter
|
ConverterInstruction
|
The same converter instance used to generate the instruction prompts.
Must expose |
required |
data_manager
|
DataManager
|
The data manager instance (passed to |
required |
drop_failures
|
bool
|
If True, silently drop |
False
|
aggregate_samples
|
bool
|
If True (default) and multiple samples were generated per patient,
aggregate them via |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A long-format DataFrame with columns from the reverse-converted
forecasting data plus |
Raises:
| Type | Description |
|---|---|
ValueError
|
If drop_failures is False and any result is |
Source code in twinweaver/utils/forecasting_inference.py
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 | |
run_forecasting_inference ¶
run_forecasting_inference(
prompts_with_meta,
*,
prediction_url="http://0.0.0.0:8000/v1/",
prediction_model="default-model",
max_concurrent_requests=40,
system_prompt=None,
max_new_tokens=512,
temperature=0.7,
top_p=0.9,
n_samples=1,
api_key="EMPTY",
timeout=600.0
)
Generate forecasting predictions for all patients via an OpenAI-compatible API.
This is the main synchronous entry-point. It calls asyncio.run
internally so it can be used from plain scripts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompts_with_meta
|
list[PromptPayload]
|
Each element is a dict with at least the following keys:
Any extra keys are passed through unchanged to the results. |
required |
prediction_url
|
str
|
Base URL of the OpenAI-compatible inference server. |
'http://0.0.0.0:8000/v1/'
|
prediction_model
|
str
|
Model name / path served by the inference server. |
'default-model'
|
max_concurrent_requests
|
int
|
Maximum number of concurrent API requests. |
40
|
system_prompt
|
str or None
|
Optional system prompt. |
None
|
max_new_tokens
|
int
|
Maximum number of tokens to generate per completion. |
512
|
temperature
|
float
|
Sampling temperature (0 = greedy). |
0.7
|
top_p
|
float
|
Nucleus-sampling probability mass. |
0.9
|
n_samples
|
int
|
Number of independent completions per prompt. Useful for
trajectory aggregation (see
:meth: |
1
|
api_key
|
str
|
API key ( |
'EMPTY'
|
timeout
|
float
|
Per-request timeout in seconds. |
600.0
|
Returns:
| Type | Description |
|---|---|
list[dict or None]
|
One dict per patient. Each dict contains all keys from the input
payload (except |
Source code in twinweaver/utils/forecasting_inference.py
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 | |
run_forecasting_inference_notebook ¶
run_forecasting_inference_notebook(
prompts_with_meta,
*,
prediction_url="http://0.0.0.0:8000/v1/",
prediction_model="default-model",
max_concurrent_requests=40,
system_prompt=None,
max_new_tokens=512,
temperature=0.7,
top_p=0.9,
n_samples=1,
api_key="EMPTY",
timeout=600.0
)
Generate forecasting predictions – async variant for Jupyter notebooks.
Identical to :func:run_forecasting_inference but returns a coroutine
that can be await-ed directly in a notebook cell (which already has
a running event loop).
Returns:
| Type | Description |
|---|---|
Coroutine[..., list[dict or None]]
|
|