Skip to content

Node Wrapper #1584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
chore: move python wrapper into scripts/
  • Loading branch information
Candice0313 committed Apr 21, 2025
commit 3bcac31f78af6026ec70b57515ba64011b9dfcaa
Binary file not shown.
Binary file not shown.
24 changes: 24 additions & 0 deletions scripts/python_wrapper/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from wrapper import SyntheaGenerator
import os

gen = SyntheaGenerator(
state="California",
gender="F",
age="30-40",
patients=2,
module="diabetes"
)

patients = gen.generate()


if not patients:
print("No patients generated.")
else:
print(f"Generated {len(patients)} patients")
print("Sample patient resourceType:", patients[0].get("resourceType"))

gen.save("my_output/diabetes_test")

saved_files = os.listdir("my_output/diabetes_test")
print("Saved files:", saved_files)
66 changes: 66 additions & 0 deletions scripts/python_wrapper/wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import subprocess
import os
import json

class SyntheaGenerator:
def __init__(self, state="Massachusetts", gender=None, age=None, patients=1, module=None, synth_path=None):
if synth_path is None:
synth_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))

self.state = state
self.gender = gender
self.age = age
self.patients = patients
self.module = module
self.synth_path = synth_path
self.output_path = os.path.join(self.synth_path, "output", "fhir")

def _clear_output(self):
if os.path.exists(self.output_path):
for f in os.listdir(self.output_path):
if f.endswith(".json"):
os.remove(os.path.join(self.output_path, f))

def _build_command(self):
cmd = ["./run_synthea", self.state, "-p", str(self.patients)]
if self.age:
cmd += ["-a", self.age]
if self.gender:
cmd += ["-g", self.gender]
if self.module:
cmd += ["-m", self.module]
return cmd

def generate(self):
self._clear_output()
cmd = self._build_command()
try:
subprocess.run(cmd, cwd=self.synth_path, check=True)
except subprocess.CalledProcessError as e:
raise RuntimeError(f"[Synthea Error] CLI failed: {e}")

if not os.path.exists(self.output_path):
raise RuntimeError("No output folder found.")

json_files = [f for f in os.listdir(self.output_path) if f.endswith(".json")]
patients = []
for file in json_files:
with open(os.path.join(self.output_path, file), "r") as f:
data = json.load(f)
entries = data.get("entry", [])
has_patient = any(
e.get("resource", {}).get("resourceType") == "Patient" for e in entries
)
if data.get("resourceType") == "Bundle" and has_patient:
patients.append(data)

return patients

def save(self, output_dir):
os.makedirs(output_dir, exist_ok=True)
for file in os.listdir(self.output_path):
if file.endswith(".json"):
src = os.path.join(self.output_path, file)
dst = os.path.join(output_dir, file)
with open(src, "r") as f_in, open(dst, "w") as f_out:
f_out.write(f_in.read())