top of page

Dump All Patient Data from ESAPI

If, for whatever reason, you need to dump out all of the data for a patient from ESAPI, there's a quick and dirty way to do it. Well, there are probably several ways to do it, but here's one I've found. Typically, I'll use this when I'm searching for something specific in the patient's data.


The trick is to use a serializer that goes through the entire Patient object graph and serializes it into a string you can output. The serializer I'm using here is a YAML serializer from the YamlDotNet library. You could probably use an XML or JSON serializer, but I've found YAML to be very concise.


The code, which is on GitHub (ExportPatientData), is actually very short:


public class Program
{
    [STAThread]
    public static void Main(string[] args)
    {
        if (args.Length < 1)
        {
            Console.Error.WriteLine("Missing parameters: patient-id");
            return;
        }

        using (var app = Application.CreateApplication(null, null))
        {
            var patientId = args[0];
            var patient = app.OpenPatientById(patientId);

            var serializer = new SerializerBuilder()
                .EmitDefaults()
                .WithAttributeOverride<Structure>(e => e.SegmentVolume, new YamlIgnoreAttribute())
                .WithAttributeOverride<Structure>(e => e.MeshGeometry, new YamlIgnoreAttribute())
                .WithAttributeOverride<Isodose>(e => e.MeshGeometry, new YamlIgnoreAttribute())
                .WithAttributeOverride<ControlPoint>(e => e.LeafPositions, new YamlIgnoreAttribute())
                .Build();

            var patientString = serializer.Serialize(patient);
            Console.WriteLine(patientString);
        }
    }
}

The YAML serializer is configured using the SerializerBuilder. First, I specify to EmitDefaults, which causes the serializer to output properties even if their value is the type's default (for example, 0 for int, false for bool, or null for string).


For some patients, accessing a structure's SegmentVolume raises an exception (when there's no volume), so I use the configuration WithAttributeOverride to tell the serializer to ignore this property when encountered. If you find that serializing a specific patient fails, try ignoring the misbehaving property and try again.


The remaining WithAttributeOverride specifications are optional, but without them a lot of data ends up being generated. For one sample patient, there was over 250 MB and 11 million lines of text generated without this specification. After adding them in, the resulting data was 5 MB and about 150 thousand lines of text. Of course, if you do need these data, don't tell the serializer to ignore them.


The serialized patient is output to the console, but at the command line I redirect the output to a file. If you want, of course, you can change the program to write directly to a file.

Related Posts

See All

ESAPI Essentials 1.1 and 2.0

A few months ago, I introduced ESAPI Essentials—a toolkit for working with ESAPI (available via NuGet). I've recently added one major feature: asynchronous access to ESAPI for binary plugin scripts. Y

Announcement: ESAPI Subreddit

A few months ago, Matt Schmidt started the ESAPI subreddit. It's another useful resource for finding and discussing ESAPI-related topics. According to the description, This community is to post and di

bottom of page