Sandbox Client
Simplified client for testing healthcare services with various data sources.
This class provides an intuitive interface for: - Loading test datasets (MIMIC-on-FHIR, Synthea, CSV) - Generating synthetic FHIR data - Sending requests to healthcare services - Managing request/response lifecycle
Examples:
Load from dataset registry:
>>> client = SandboxClient(
... api_url="http://localhost:8000",
... endpoint="/cds/cds-services/my-service"
... )
>>> client.load_from_registry("mimic-on-fhir", sample_size=10)
>>> responses = client.send_requests()
Load CDA file from path:
>>> client = SandboxClient(
... api_url="http://localhost:8000",
... endpoint="/notereader/fhir/",
... protocol="soap"
... )
>>> client.load_from_path("./data/clinical_note.xml")
>>> responses = client.send_requests()
Generate data from free text:
>>> client = SandboxClient(
... api_url="http://localhost:8000",
... endpoint="/cds/cds-services/discharge-summarizer"
... )
>>> client.load_free_text(
... csv_path="./data/notes.csv",
... column_name="text",
... workflow="encounter-discharge"
... )
>>> responses = client.send_requests()
Source code in healthchain/sandbox/sandboxclient.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 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 | |
__init__(api_url, endpoint, workflow=None, protocol='rest', timeout=10.0)
Initialize SandboxClient.
| PARAMETER | DESCRIPTION |
|---|---|
api_url
|
Base URL of the service (e.g., "http://localhost:8000")
TYPE:
|
endpoint
|
Service endpoint path (e.g., "/cds/cds-services/my-service")
TYPE:
|
workflow
|
Optional workflow specification (auto-detected if not provided)
TYPE:
|
protocol
|
Communication protocol - "rest" for CDS Hooks, "soap" for CDA
TYPE:
|
timeout
|
Request timeout in seconds
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If api_url or endpoint is invalid |
Source code in healthchain/sandbox/sandboxclient.py
__repr__()
String representation of SandboxClient.
Source code in healthchain/sandbox/sandboxclient.py
get_status()
Get current client status and statistics.
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
Dictionary containing client status information |
Source code in healthchain/sandbox/sandboxclient.py
load_free_text(csv_path, column_name, workflow, random_seed=None, **kwargs)
Generates a CDS prefetch from free text notes.
Reads clinical notes from a CSV file and wraps it in FHIR DocumentReferences in a CDS prefetch field for CDS Hooks workflows. Generates additional synthetic FHIR resources as needed based on the specified workflow.
| PARAMETER | DESCRIPTION |
|---|---|
csv_path
|
Path to CSV file containing clinical notes
TYPE:
|
column_name
|
Name of the column containing the text
TYPE:
|
workflow
|
CDS workflow type (e.g., "encounter-discharge", "patient-view")
TYPE:
|
random_seed
|
Seed for reproducible data generation
TYPE:
|
**kwargs
|
Additional parameters for data generation
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SandboxClient
|
Self for method chaining |
| RAISES | DESCRIPTION |
|---|---|
FileNotFoundError
|
If CSV file doesn't exist |
ValueError
|
If workflow is invalid or column not found |
Examples:
Generate discharge summaries:
>>> client.load_free_text(
... csv_path="./data/discharge_notes.csv",
... column_name="text",
... workflow="encounter-discharge",
... random_seed=42
... )
Source code in healthchain/sandbox/sandboxclient.py
load_from_path(path, pattern=None, workflow=None)
Load data from file system path.
Supports loading single files or directories. File type is auto-detected from extension and protocol: - .xml files with SOAP protocol → CDA documents - .json files with REST protocol → Pre-formatted Prefetch data
| PARAMETER | DESCRIPTION |
|---|---|
path
|
File path or directory path
TYPE:
|
pattern
|
Glob pattern for filtering files in directory (e.g., "*.xml")
TYPE:
|
workflow
|
Optional workflow override (auto-detected from protocol if not provided)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SandboxClient
|
Self for method chaining |
| RAISES | DESCRIPTION |
|---|---|
FileNotFoundError
|
If path doesn't exist |
ValueError
|
If no matching files found or unsupported file type |
Examples:
Load single CDA file:
Load directory of CDA files:
Load with explicit workflow:
Source code in healthchain/sandbox/sandboxclient.py
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 | |
load_from_registry(source, **kwargs)
Load data from the dataset registry.
Loads pre-configured datasets like MIMIC-on-FHIR, Synthea, or custom registered datasets.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
Dataset name (e.g., "mimic-on-fhir", "synthea")
TYPE:
|
**kwargs
|
Dataset-specific parameters (e.g., sample_size, num_patients)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SandboxClient
|
Self for method chaining |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If dataset not found in registry |
Examples:
Discover available datasets:
Load MIMIC dataset:
Source code in healthchain/sandbox/sandboxclient.py
save_results(directory='./output/')
Save request and response data to disk.
| PARAMETER | DESCRIPTION |
|---|---|
directory
|
Directory to save data to (default: "./output/")
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If no responses are available to save |
Source code in healthchain/sandbox/sandboxclient.py
send_requests()
Send all queued requests to the service.
| RETURNS | DESCRIPTION |
|---|---|
List[Dict]
|
List of response dictionaries |
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If no requests are queued |
Source code in healthchain/sandbox/sandboxclient.py
CdsDataGenerator
A class to generate CDS (Clinical Decision Support) data based on specified workflows and constraints.
This class provides functionality to generate synthetic FHIR resources for testing CDS systems. It uses registered data generators to create resources like Patients, Encounters, Conditions etc. based on configured workflows. It can also incorporate free text data from CSV files.
| ATTRIBUTE | DESCRIPTION |
|---|---|
registry |
A registry mapping generator names to generator classes.
TYPE:
|
mappings |
A mapping of workflow names to lists of required generators.
TYPE:
|
generated_data |
The most recently generated FHIR resources.
TYPE:
|
workflow |
The currently active workflow.
TYPE:
|
Example
generator = CdsDataGenerator() generator.set_workflow("encounter_discharge") data = generator.generate_prefetch( ... random_seed=42 ... )
Source code in healthchain/sandbox/generators/cdsdatagenerator.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 | |
fetch_generator(generator_name)
Fetches a data generator class by its name from the registry.
| PARAMETER | DESCRIPTION |
|---|---|
generator_name
|
The name of the data generator to fetch (e.g. "PatientGenerator", "EncounterGenerator")
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Callable
|
The data generator class that can be used to generate FHIR resources. Returns None if generator not found.
TYPE:
|
Example
generator = CdsDataGenerator() patient_gen = generator.fetch_generator("PatientGenerator") patient = patient_gen.generate()
Source code in healthchain/sandbox/generators/cdsdatagenerator.py
free_text_parser(path_to_csv, column_name)
Parse free text data from a CSV file.
This method reads a CSV file and extracts text data from a specified column. The text data can later be used to create DocumentReference resources.
| PARAMETER | DESCRIPTION |
|---|---|
path_to_csv
|
Path to the CSV file containing the free text data.
TYPE:
|
column_name
|
Name of the column in the CSV file to extract text from.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
List[str]
|
List[str]: List of text strings extracted from the specified column. |
| RAISES | DESCRIPTION |
|---|---|
FileNotFoundError
|
If the specified CSV file does not exist or is not a file. |
ValueError
|
If column_name is not provided. |
Exception
|
If any other error occurs while reading/parsing the CSV file. |
Source code in healthchain/sandbox/generators/cdsdatagenerator.py
generate_prefetch(constraints=None, free_text_path=None, column_name=None, random_seed=None)
Generates CDS data based on the current workflow, constraints, and optional free text data.
This method generates FHIR resources according to the configured workflow mapping. For each resource type in the workflow, it uses the corresponding generator to create a FHIR resource. If free text data is provided via CSV, it will also generate a DocumentReference containing randomly selected text from the CSV.
| PARAMETER | DESCRIPTION |
|---|---|
constraints
|
A list of constraints to apply to the data generation. Each constraint should match the format expected by the individual generators.
TYPE:
|
free_text_path
|
Path to a CSV file containing free text data to be included as DocumentReferences. If provided, column_name must also be specified.
TYPE:
|
column_name
|
The name of the column in the CSV file containing the free text data to use. Required if free_text_path is provided.
TYPE:
|
random_seed
|
Seed value for random number generation to ensure reproducible results. If not provided, generation will be truly random.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Prefetch
|
A dictionary mapping resource types to generated FHIR resources. The keys are lowercase resource type names (e.g. "patient", "encounter"). If free text is provided, includes a "document" key with a DocumentReference.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the configured workflow is not found in the mappings |
FileNotFoundError
|
If the free_text_path is provided but file not found |
ValueError
|
If free_text_path provided without column_name |
Source code in healthchain/sandbox/generators/cdsdatagenerator.py
set_workflow(workflow)
Sets the current workflow to be used for data generation.
| PARAMETER | DESCRIPTION |
|---|---|
workflow
|
The name of the workflow to set.
TYPE:
|
CDSRequest
Bases: BaseModel
A model representing the data structure for a CDS service call, triggered by specific hooks within a healthcare application.
| ATTRIBUTE | DESCRIPTION |
|---|---|
hook |
The hook that triggered this CDS Service call. For example, 'patient-view'.
TYPE:
|
hookInstance |
A universally unique identifier for this particular hook call.
TYPE:
|
fhirServer |
The base URL of the CDS Client's FHIR server. This field is required if
TYPE:
|
fhirAuthorization |
Optional authorization details providing a bearer access token for FHIR resources.
TYPE:
|
context |
Hook-specific contextual data required by the CDS service.
TYPE:
|
prefetch |
Optional FHIR data that was prefetched by the CDS Client.
TYPE:
|
Documentation: https://cds-hooks.org/specification/current/#http-request_1
Source code in healthchain/models/requests/cdsrequest.py
model_dump(**kwargs)
Model dump method to convert any nested datetime and byte objects to strings for readability. This is also a workaround to this Pydantic V2 issue https://github.com/pydantic/pydantic/issues/9571 For proper JSON serialization, should use model_dump_json() instead when issue is resolved.
Source code in healthchain/models/requests/cdsrequest.py
Action
Bases: BaseModel
Within a suggestion, all actions are logically AND'd together, such that a user selecting a suggestion selects all of the actions within it. When a suggestion contains multiple actions, the actions SHOULD be processed as per FHIR's rules for processing transactions with the CDS Client's fhirServer as the base url for the inferred full URL of the transaction bundle entries.
https://cds-hooks.org/specification/current/#action
Source code in healthchain/models/responses/cdsresponse.py
ActionTypeEnum
CDSResponse
Bases: BaseModel
Represents the response from a CDS service.
This class models the structure of a CDS Hooks response, which includes cards for displaying information or suggestions to the user, and optional system actions that can be executed automatically.
| ATTRIBUTE | DESCRIPTION |
|---|---|
cards |
A list of Card objects to be displayed to the end user. Default is an empty list.
TYPE:
|
systemActions |
A list of Action objects representing actions that the CDS Client should execute as part of performing the decision support requested. This field is optional.
TYPE:
|
For more information, see: https://cds-hooks.org/specification/current/#cds-service-response
Source code in healthchain/models/responses/cdsresponse.py
Card
Bases: BaseModel
Cards can provide a combination of information (for reading), suggested actions (to be applied if a user selects them), and links (to launch an app if the user selects them). The CDS Client decides how to display cards, but this specification recommends displaying suggestions using buttons, and links using underlined text.
https://cds-hooks.org/specification/current/#card-attributes
Source code in healthchain/models/responses/cdsresponse.py
IndicatorEnum
Bases: str, Enum
Urgency/importance of what Card conveys. Allowed values, in order of increasing urgency, are: info, warning, critical. The CDS Client MAY use this field to help make UI display decisions such as sort order or coloring.
Source code in healthchain/models/responses/cdsresponse.py
Link
Bases: BaseModel
-
CDS Client support for appContext requires additional coordination with the authorization server that is not described or specified in CDS Hooks nor SMART.
-
Autolaunchable is experimental
https://cds-hooks.org/specification/current/#link
Source code in healthchain/models/responses/cdsresponse.py
LinkTypeEnum
Bases: str, Enum
The type of the given URL. There are two possible values for this field. A type of absolute indicates that the URL is absolute and should be treated as-is. A type of smart indicates that the URL is a SMART app launch URL and the CDS Client should ensure the SMART app launch URL is populated with the appropriate SMART launch parameters.
Source code in healthchain/models/responses/cdsresponse.py
SelectionBehaviorEnum
Bases: str, Enum
Describes the intended selection behavior of the suggestions in the card. Allowed values are: at-most-one, indicating that the user may choose none or at most one of the suggestions; any, indicating that the end user may choose any number of suggestions including none of them and all of them. CDS Clients that do not understand the value MUST treat the card as an error.
Source code in healthchain/models/responses/cdsresponse.py
SimpleCoding
Bases: BaseModel
The Coding data type captures the concept of a code. This coding type is a standalone data type in CDS Hooks modeled after a trimmed down version of the FHIR Coding data type.
Source code in healthchain/models/responses/cdsresponse.py
Source
Bases: BaseModel
Grouping structure for the Source of the information displayed on this card. The source should be the primary source of guidance for the decision support Card represents.
https://cds-hooks.org/specification/current/#source
Source code in healthchain/models/responses/cdsresponse.py
Suggestion
Bases: BaseModel
Allows a service to suggest a set of changes in the context of the current activity (e.g. changing the dose of a medication currently being prescribed, for the order-sign activity). If suggestions are present, selectionBehavior MUST also be provided.
https://cds-hooks.org/specification/current/#suggestion
Source code in healthchain/models/responses/cdsresponse.py
CdaRequest
Bases: BaseModel
Source code in healthchain/models/requests/cdarequest.py
from_dict(data)
classmethod
model_dump(*args, **kwargs)
model_dump_xml(*args, **kwargs)
Decodes and dumps document as an xml string
Source code in healthchain/models/requests/cdarequest.py
CdaResponse
Bases: BaseModel
Source code in healthchain/models/responses/cdaresponse.py
from_dict(data)
classmethod
model_dump(*args, **kwargs)
model_dump_xml(*args, **kwargs)
Decodes and dumps document as an xml string