# Programmatically accessing patient data loaded from a file

**URL:** https://discourse.kitware.com/t/programmatically-accessing-patient-data-loaded-from-a-file/443
**Category:** Pulse Physiology Engine
**Created:** [April 23, 2020, 6:08pm UTC](https://discourse.kitware.com/t/programmatically-accessing-patient-data-loaded-from-a-file/443 "2020-04-23T18:08:37Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![bob.marinier](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@bob.marinier](https://discourse.kitware.com/u/bob.marinier)
#### Post date: [April 23, 2020, 6:08pm UTC](https://discourse.kitware.com/t/programmatically-accessing-patient-data-loaded-from-a-file/443/1 "2020-04-23T18:08:37Z")

</div>

I’m loading the `Soldier@0s.json` patient file, and I’d like to extract data from it programmatically (e.g. the patient’s age).

This code (Java) doesn’t work:

```java
boolean success = this.pulseEngine.serializeFromFile("isaac-pulse.log", patientStateFile, dataRequests);
this.pulseEngine.patient.getSex();

```

The issue is that the sex is null, even though it is defined in the patient file. (The patient is not null, but every field in it is null.) Is there a way to get this data populated in the patient data structures? Or maybe it should be and I’m doing something else wrong?

---

<div class="post-metadata">

### Author: ![bob.marinier](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@bob.marinier](https://discourse.kitware.com/u/bob.marinier)
#### Post date: [April 23, 2020, 6:42pm UTC](https://discourse.kitware.com/t/programmatically-accessing-patient-data-loaded-from-a-file/443/2 "2020-04-23T18:42:45Z")

</div>

Actually, I’m not loading a patient file directly, I’m loading a state file, in case that makes a difference.

---

<div class="post-metadata">

### Author: ![bob.marinier](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@bob.marinier](https://discourse.kitware.com/u/bob.marinier)
#### Post date: [April 23, 2020, 7:20pm UTC](https://discourse.kitware.com/t/programmatically-accessing-patient-data-loaded-from-a-file/443/3 "2020-04-23T19:20:19Z")

</div>

I should also say that it looks like for many patient fields the intention is for the getter to return a default if the field is null, but `getSex()` just returns the value, which is null. I wasn’t expecting this, so I got a `NullPointerException`.

As for getting the patient instantiated, it looks like I can do it if I don’t use a state file, but then I have a longer startup time. As a workaround, I can probably load the state file and the patient file separately, but I’m not sure if this is what is intended.

---

<div class="post-metadata">

### Author: ![abray](https://discourse.kitware.com/user_avatar/discourse.kitware.com/abray/32/12_2.png) [@abray](https://discourse.kitware.com/u/abray)
#### Post date: [April 23, 2020, 7:43pm UTC](https://discourse.kitware.com/t/programmatically-accessing-patient-data-loaded-from-a-file/443/4 "2020-04-23T19:43:55Z")

</div>

You can load a state file and grab patient data from the engine.  
But anything you want to get passed over a language barrier (C++ to Java in this case) has to be specified as a Data Request.

So you need to add this to your Data Requests:

```
   SEDataRequest age = new SEDataRequest();
   age.setCategory(eCategory.Patient);
   age.setPropertyName("Age");
   age.setUnit(TimeUnit.yr.toString());
   dataRequests.getRequestedData().add(age);

```

Then you will get a value back for

```
    Log.info("Patient Age "+ pe.patient.getAge());

```

Again, I think the Java API should be refactored to behave more like the Python and C# API’s

Also note, that I do not have a way of getting enum values back for any language currently.

---

<div class="post-metadata">

### Author: ![bob.marinier](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@bob.marinier](https://discourse.kitware.com/u/bob.marinier)
#### Post date: [April 23, 2020, 8:12pm UTC](https://discourse.kitware.com/t/programmatically-accessing-patient-data-loaded-from-a-file/443/5 "2020-04-23T20:12:37Z")

</div>

The enum values are pretty important. Here’s what I’m doing now:

```java
SEPatient patient = new SEPatient();
StateData.Builder builder = StateData.newBuilder();
JsonFormat.parser().merge(FileUtils.readFile(patientStateFile), builder);
SEPatient.load(builder.getCurrentPatient(), patient); 

```

Then I create the `PulseEngine` with the patient state file and just keep the `SEPatient` around to get the various patient info from as needed.

---

<div class="post-metadata">

### Author: ![abray](https://discourse.kitware.com/user_avatar/discourse.kitware.com/abray/32/12_2.png) [@abray](https://discourse.kitware.com/u/abray)
#### Post date: [April 23, 2020, 8:23pm UTC](https://discourse.kitware.com/t/programmatically-accessing-patient-data-loaded-from-a-file/443/6 "2020-04-23T20:23:11Z")

</div>

That works!

It would also be nice for Java/C#/Python to be able to get whole systems/objects from the engine.

Something like,

```
  engine.getPatient(patient)

```

You provide the patient object, and this method would automatically pull the entire object out of the engine and populate it with that data. (Of course you should not do this each time step, unless you really need all values. For optimal data that needs to be updated each time step, still use Data Requests)

---

<div class="post-metadata">

### Author: ![bob.marinier](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@bob.marinier](https://discourse.kitware.com/u/bob.marinier)
#### Post date: [April 23, 2020, 8:47pm UTC](https://discourse.kitware.com/t/programmatically-accessing-patient-data-loaded-from-a-file/443/7 "2020-04-23T20:47:59Z")

</div>

Yes, I agree that would be nice. In my case, I just need it at startup, because the values I care about (age, sex, etc.) will never change. It’s “background” information about the patient that I want to display.

---

<div class="post-metadata">

### Author: ![bob.marinier](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@bob.marinier](https://discourse.kitware.com/u/bob.marinier)
#### Post date: [April 23, 2020, 9:22pm UTC](https://discourse.kitware.com/t/programmatically-accessing-patient-data-loaded-from-a-file/443/8 "2020-04-23T21:22:41Z")

</div>

Here’s a potentially nicer solution that gets the data into the pulse engine, so I don’t have to track another object, and it seems more natural to me to have this data in the engine anyway.

```java
PulseEngine pulseEngine = new PulseEngine();
pulseEngine.serializeFromFile("my.log", patientStateFile, dataRequests);

// have to do this after engine initialization because otherwise the patient fields will be reset back to null
StateData.Builder builder = StateData.newBuilder();
JsonFormat.parser().merge(FileUtils.readFile(patientStateFile), builder);
SEPatient.load(builder.getCurrentPatient(), pulseEngine.patient); 

```

---

<div class="post-metadata">

### Author: ![abray](https://discourse.kitware.com/user_avatar/discourse.kitware.com/abray/32/12_2.png) [@abray](https://discourse.kitware.com/u/abray)
#### Post date: [April 23, 2020, 11:02pm UTC](https://discourse.kitware.com/t/programmatically-accessing-patient-data-loaded-from-a-file/443/9 "2020-04-23T23:02:00Z")

</div>

That is pretty nice

I could add that to always happen in the serializeFromFile method, so the patient object is always updated on a load

---

<div class="post-metadata">

### Author: ![bob.marinier](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@bob.marinier](https://discourse.kitware.com/u/bob.marinier)
#### Post date: [April 23, 2020, 11:07pm UTC](https://discourse.kitware.com/t/programmatically-accessing-patient-data-loaded-from-a-file/443/10 "2020-04-23T23:07:33Z")

</div>

I like that idea a lot. It would be more efficient since I wouldn’t have to deserialize the state file twice, and it seems like it would generally be useful. Are there other parts of the pulse engine that are null without a data request that could be initialized this way? (I don’t need anything else yet, it just seems like it may be useful to do this for as much as possible.)

---

<div class="post-metadata">

### Author: ![abray](https://discourse.kitware.com/user_avatar/discourse.kitware.com/abray/32/12_2.png) [@abray](https://discourse.kitware.com/u/abray)
#### Post date: [April 23, 2020, 11:13pm UTC](https://discourse.kitware.com/t/programmatically-accessing-patient-data-loaded-from-a-file/443/11 "2020-04-23T23:13:11Z")

</div>

Well, technically there are some patient parameters that do change during the simulation.  
So I would really want to selectively copy a subset of properties out of the state file into the patient object, not all of them. Depending on what you do, baseline values can change.

There are no other ‘static’ properties in the state file that could change in the next time step…

What we do in C++ is have 2 patient object, the original patient, and the current patient.  
Which might actually be in the state… or they should be… and I should automagically set the ‘original’ patient object in the PulseEngine…

---

<div class="post-metadata">

### Author: ![bob.marinier](https://discourse.kitware.com/letter_avatar_proxy/v4/letter/b/7ab992/32.png) [@bob.marinier](https://discourse.kitware.com/u/bob.marinier)
#### Post date: [April 23, 2020, 11:14pm UTC](https://discourse.kitware.com/t/programmatically-accessing-patient-data-loaded-from-a-file/443/12 "2020-04-23T23:14:38Z")

</div>

Makes sense! I think this would be a good addition to the behavior.
