-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainView.xaml.cs
251 lines (205 loc) · 11.3 KB
/
MainView.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VMS.TPS.Common.Model.API;
using VMS.TPS.Common.Model.Types;
namespace BED_Calc
{
/// <summary>
/// Interaction logic for MainView.xaml
/// </summary>
public partial class MainView : UserControl
{
// specify properties, including one for the open script context
private int numberOfFractions;
private string organName;
private float alphaBetaRatio;
public float bed;
public float eqd2;
private PlanSetup planSetup;
private ScriptContext context;
public MainView(ScriptContext context)
{
InitializeComponent();
this.context = context; //Copy currently loaded patient data content
planSetup = context.PlanSetup;
numberOfFractions= (int)planSetup.NumberOfFractions; //extract number of fractions from open plancontext
}
public MainView()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//set organ name and alpha beta ratio on what was specified
organName=OrganNameTB.Text;
alphaBetaRatio = float.Parse(AlphaBetaRatioTB.Text);
StructureSet structureSet = planSetup.StructureSet;
if (structureSet == null)
throw new ApplicationException("The selected plan does not reference a StructureSet.");
//find structure if it exists, if it does, set target equal to that structure. throw exce
Structure target = null;
bool StructureEmpty = false;//boolean to check if empty structure
foreach (var structure in structureSet.Structures)
{
if (structure.Id == organName) //if structure Id matches the f
{
if (structure.IsEmpty)
{
StructureEmpty = true;
MessageBox.Show(String.Format("Structure {0} is empty, try another structure", organName));
target = null;
break;
}
else
{
target = structure;
break;
}
}
}
//Message for debugging
//MessageBox.Show(String.Format("StructureEmpty is {0}. targetnull is {1}", StructureEmpty, target == null));
if (!StructureEmpty)
{
if (target == null)
MessageBox.Show(String.Format("I can't find a structure called {0}", organName));
else if (DoseTypeTB.Text.Equals("Mean")) //if target is not null and structure is not empty then we can retriev our dose, we split into cases
{
DVHData dvhData = planSetup.GetDVHCumulativeData(target,
DoseValuePresentation.Absolute,
VolumePresentation.AbsoluteCm3, 0.1);
float dose = (float)dvhData.MeanDose.Dose;//compute mean dose
float dosePerFraction = dose / numberOfFractions;
bed = dose * (1.0f + dosePerFraction / alphaBetaRatio);
eqd2 = dose * ((dosePerFraction + alphaBetaRatio) / (2.0f + alphaBetaRatio));
AbsoluteDoseTB.Text=dose.ToString("0.0");
BEDTB.Text = bed.ToString("0.0");
EQD2TB.Text = eqd2.ToString("0.0");
/*this is for debugging only
MessageBox.Show(String.Format("Organ={0}, MeanDose={1}," +
"Dose Per Fraction={2}, Number of Fractions= {3}", organName, dose,dosePerFraction, numberOfFractions));*/
}
else if (DoseTypeTB.Text.Equals("Max")) //if target is not null and structure is not empty then we can retriev our dose, we split into cases
{
DVHData dvhData = planSetup.GetDVHCumulativeData(target,
DoseValuePresentation.Absolute,
VolumePresentation.AbsoluteCm3, 0.1);
float dose = (float)dvhData.MaxDose.Dose;//compute mean dose
float dosePerFraction = dose / numberOfFractions;
bed = dose * (1.0f + dosePerFraction / alphaBetaRatio);
eqd2 = dose * ((dosePerFraction + alphaBetaRatio) / (2.0f + alphaBetaRatio));
AbsoluteDoseTB.Text = dose.ToString("0.0");
BEDTB.Text = bed.ToString("0.0");
EQD2TB.Text = eqd2.ToString("0.0");
/*this is for debugging only
MessageBox.Show(String.Format("Organ={0}, MeanDose={1}," +
"Dose Per Fraction={2}, Number of Fractions= {3}", organName, dose,dosePerFraction, numberOfFractions));*/
}
else if (DoseTypeTB.Text.Equals("Min")) //if target is not null and structure is not empty then we can retriev our dose, we split into cases
{
DVHData dvhData = planSetup.GetDVHCumulativeData(target,
DoseValuePresentation.Absolute,
VolumePresentation.AbsoluteCm3, 0.1);
float dose = (float)dvhData.MinDose.Dose;//compute mean dose
float dosePerFraction = dose / numberOfFractions;
bed = dose * (1.0f + dosePerFraction / alphaBetaRatio);
eqd2 = dose * ((dosePerFraction + alphaBetaRatio) / (2.0f + alphaBetaRatio));
AbsoluteDoseTB.Text = dose.ToString("0.0");
BEDTB.Text = bed.ToString("0.0");
EQD2TB.Text = eqd2.ToString("0.0");
/*this is for debugging only
MessageBox.Show(String.Format("Organ={0}, MeanDose={1}," +
"Dose Per Fraction={2}, Number of Fractions= {3}", organName, dose,dosePerFraction, numberOfFractions));*/
}
else if (DoseTypeTB.Text.EndsWith("cc") && DoseTypeTB.Text.StartsWith("D"))
{
string volString = FindStringBetween("D", "cc", DoseTypeTB.Text);
bool success = float.TryParse(volString, out float volFloat);
if (success)
{
//MessageBox.Show(String.Format("Debug: Sucessfully converted absolute volume to float, value is {0}cc", volFloat));
}
else
{
MessageBox.Show("Incorrect Dose Format, your dose must have format \"DNcc\" where N is the volume in cc. N must be a number");
}
//at this point, we have sucessfully converted the volume in cc to a float from the entered string. Let's find the dose that corresponds to this
DVHData dvhData = planSetup.GetDVHCumulativeData(target, //extract absolute dvh
DoseValuePresentation.Absolute,
VolumePresentation.AbsoluteCm3, 0.1);
float dose = (float)DoseAtVolume(dvhData, volFloat).Dose;
float dosePerFraction = dose / numberOfFractions;
bed = dose * (1.0f + dosePerFraction / alphaBetaRatio);
eqd2 = dose * ((dosePerFraction + alphaBetaRatio) / (2.0f + alphaBetaRatio));
AbsoluteDoseTB.Text = dose.ToString("0.0");
BEDTB.Text = bed.ToString("0.0");
EQD2TB.Text = eqd2.ToString("0.0");
//MessageBox.Show("Debug: volume is " + volString+"cc");
}
else if (DoseTypeTB.Text.EndsWith("%") && DoseTypeTB.Text.StartsWith("D"))
{
//since this is the % case, we convert from % volume to cc by multiplying through. This is built into the Dose at volume function if you extract your dvh properly
string volString = FindStringBetween("D", "%", DoseTypeTB.Text);
bool success = float.TryParse(volString, out float volFloat);
if (success)
{
//MessageBox.Show(String.Format("Debug: Sucessfully converted volume % to float, value is {0}%", volFloat));
}
else
{
MessageBox.Show("Incorrect Dose Format, your dose must have format \"DN%\" where N is the volume in %. N must be a number");
}
DVHData dvhData = planSetup.GetDVHCumulativeData(target,
DoseValuePresentation.Absolute,
VolumePresentation.Relative,0.1);
float dose = (float)DoseAtVolume(dvhData, volFloat).Dose;
float dosePerFraction = dose / numberOfFractions;
bed = dose * (1.0f + dosePerFraction / alphaBetaRatio);
eqd2 = dose * ((dosePerFraction + alphaBetaRatio) / (2.0f + alphaBetaRatio));
AbsoluteDoseTB.Text = dose.ToString("0.0");
BEDTB.Text = bed.ToString("0.0");
EQD2TB.Text = eqd2.ToString("0.0");
//MessageBox.Show("Debug: volume is "+volString+"%");
//MessageBox.Show("Still working on retriving % doses");
}
else
{
MessageBox.Show("Invalid Dose type, please enter either enter either \"Mean\", \"Max\", \"Min\", \"DNcc\" or \"DN%\" into the Input Dose box.");
}
}
}
private static string FindStringBetween(string firstString, string secondString, string wholeString)
{
int pFrom = wholeString.IndexOf(firstString) + firstString.Length;
int pTo = wholeString.LastIndexOf(secondString);
return wholeString.Substring(pFrom, pTo - pFrom);
}
private static DoseValue DoseAtVolume(DVHData dvhData, double volume) //volume lookup
{
if (dvhData == null || dvhData.CurveData.Count() == 0) //if the DVH is empty then return undefined dose
return DoseValue.UndefinedDose();
double absVolume = dvhData.CurveData[0].VolumeUnit == "%" ? volume * dvhData.Volume * 0.01 : volume;
if (volume < 0.0 || absVolume > dvhData.Volume)
return DoseValue.UndefinedDose();
DVHPoint[] hist = dvhData.CurveData;
for (int i = 0; i < hist.Length; i++)
{
if (hist[i].Volume < volume)
return hist[i].DoseValue;
}
return DoseValue.UndefinedDose();
}
}
}