IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
PatientMeta.cs
1 using UnityEngine;
2 using System;
3 using System.Collections;
4 using System.Collections.Generic;
5 using System.IO;
6 using System.Text;
7 using LitJson;
8 
9 public class PatientMeta
10 {
11  public enum OperationBodyPart {
12  Bone,
13  Liver,
14  Brain,
15  Pancreas,
16  Intestine,
17  Unknown
18  }
19 
20  public string firstName { get; private set; }
21  public string lastName { get; private set; }
22  public string name {
23  get { return this.firstName + " " + this.lastName; }
24  private set { }
25  }
26  public string birthDate { get; private set; }
27  public DateTime birthDateDT { get; private set; }
28  public string operationDate { get; private set; }
29  public string diagnosis { get; private set; }
30  public string details { get; private set; }
31  public string sex { get; private set; }
32  public string path { get; private set; }
33  public string dicomPath { get; private set; }
34  public string meshPath { get; private set; }
35  public int age { get; private set; }
36  public OperationBodyPart operationBodyPart { get; private set; }
37  public List<string> warnings { get; private set; }
38 
39 
40  public PatientMeta ( string folder )
41  {
42  path = folder;
43 
44  string metaFile = Path.Combine( path, "meta.json" );
45  string raw = File.ReadAllText(metaFile);
46  JsonData data;
47  try{
48  data = JsonMapper.ToObject(raw);
49  } catch {
50  throw new System.Exception("Cannot parse meta.json. Invalid syntax?");
51  }
52  warnings = new List<string> ();
53  operationBodyPart = OperationBodyPart.Unknown;
54 
55  if (data.Keys.Contains ("meta")) {
56  JsonData metaData = data["meta"];
57  if (metaData.Keys.Contains ("FirstName")) {
58  firstName = metaData ["FirstName"].ToString ();
59  }
60  if (metaData.Keys.Contains ("LastName")) {
61  lastName = metaData ["LastName"].ToString ();
62  }
63  if (metaData.Keys.Contains ("DateOfBirth")) {
64  birthDate = metaData ["DateOfBirth"].ToString ();
65 
66  try {
67  IFormatProvider culture = System.Threading.Thread.CurrentThread.CurrentCulture;
68  DateTime dt = DateTime.Parse(birthDate, culture, System.Globalization.DateTimeStyles.AssumeLocal);
69  age = DateTime.Now.Year - dt.Year;
70  birthDate = dt.Day + " " + dt.ToString("MMMM") + " " + dt.Year;
71  } catch {
72  age = -1;
73  }
74  }
75  if (metaData.Keys.Contains ("Diagnosis")) {
76  diagnosis = metaData ["Diagnosis"].ToString ();
77  }
78  if (metaData.Keys.Contains ("Details")) {
79  details = metaData ["Details"].ToString ();
80  }
81  if (metaData.Keys.Contains ("DateOfOperation")) {
82  operationDate = metaData ["DateOfOperation"].ToString ();
83  }
84  if (metaData.Keys.Contains ("Sex")) {
85  sex = metaData ["Sex"].ToString ();
86  }
87  if (metaData.Keys.Contains ("BodyPart")) {
88  string ot = metaData ["BodyPart"].ToString ();
89  try {
90  operationBodyPart = (OperationBodyPart)Enum.Parse (typeof(OperationBodyPart), ot);
91  } catch {
92  Debug.LogWarning ("Could not interpret BodyPart.");
93  }
94  }
95  if (metaData.Keys.Contains ("Warnings")) {
96  for (int i = 0; i < metaData ["Warnings"].Count; i++) {
97  warnings.Add (metaData ["Warnings"] [i].ToString ());
98  }
99  }
100  }
101 
102  if (data.Keys.Contains("mesh"))
103  {
104  JsonData metaData = data["mesh"];
105  if (metaData.Keys.Contains("path"))
106  {
107  meshPath = path + "/" + metaData["path"].ToString();
108  }
109  }
110 
111  if (data.Keys.Contains("dicom"))
112  {
113  JsonData metaData = data["dicom"];
114  if (metaData.Keys.Contains("path"))
115  {
116  dicomPath = path + "/" + metaData["path"].ToString(); ;
117  }
118  }
119  }
120 
121  // Copy constructor:
122  public PatientMeta( PatientMeta toCopy )
123  {
124  firstName = toCopy.firstName;
125  lastName = toCopy.lastName;
126  birthDate = toCopy.birthDate;
127  birthDateDT = toCopy.birthDateDT;
128  operationDate = toCopy.operationDate;
129  diagnosis = toCopy.diagnosis;
130  details = toCopy.details;
131  sex = toCopy.sex;
132  path = toCopy.path;
133  dicomPath = toCopy.dicomPath;
134  meshPath = toCopy.meshPath;
135  age = toCopy.age;
136  operationBodyPart = toCopy.operationBodyPart;
137  warnings = new List<string> (toCopy.warnings);
138  }
139 
140 
141  public static PatientMeta createFromFolder( string folder )
142  {
143  string patientsDirectoryFile = Path.Combine( folder, "meta.json" );
144  if (File.Exists(patientsDirectoryFile))
145  {
146  return new PatientMeta (folder);
147  } else {
148  Debug.LogWarning ("No meta.json found in\n\t" + folder + "\n\tInvalid patient folder.");
149  return null;
150  }
151  }
152 
153  public override string ToString()
154  {
155  return base.ToString () + " (Name: " + name + ")";
156  }
157 }
158