//--------------------------------------------------------------------------------------------- void UpdateDvhLookup() { if (m_closing || SelectedStructure == null) { return; } bool doseAbsolute = m_absDoseCheckbox.IsChecked.Value; bool volAbsolute = m_absVolCheckbox.IsChecked.Value; m_volumeAtDoseResultLabel.Content = ""; m_doseAtVolumeResultLabel.Content = ""; m_resultVolumeAtDose.Content = ""; m_resultDoseAtVolume.Content = ""; m_structureVolume.Text = ""; double inputVolume = Double.NaN; if (m_volumeTextBox.Text != null) { Double.TryParse(m_volumeTextBox.Text, out inputVolume); } double inputDose = Double.NaN; if (m_doseTextBox.Text != null) { Double.TryParse(m_doseTextBox.Text, out inputDose); } DoseValuePresentation dosePres = doseAbsolute ? DoseValuePresentation.Absolute : DoseValuePresentation.Relative; VolumePresentation volPres = volAbsolute ? VolumePresentation.AbsoluteCm3 : VolumePresentation.Relative; DVHData dvhData = SelectedPlanningItem.GetDVHCumulativeData(SelectedStructure, dosePres, volPres, s_binWidth); m_structureVolume.Text = dvhData.Volume.ToString("F5"); if (SelectedPlanningItem != null && SelectedPlanningItem.Dose != null && SelectedStructure != null) { if (!Double.IsNaN(inputVolume)) { DoseValue val = SelectedPlanningItem.GetDoseAtVolume(SelectedStructure, inputVolume, volPres, dosePres); DoseValue controlVal = DvhExtensions.DoseAtVolume(dvhData, inputVolume); double err = Math.Abs((val.Dose - controlVal.Dose) / val.Dose); if (err > 0.001) { MessageBox.Show("Value : " + val.ToString() + " Control Val : " + controlVal.ToString()); } m_resultDoseAtVolume.Content = val.ToString(); string doseAtVolumeResultType = volAbsolute ? "cm3 D(" : "% D("; doseAtVolumeResultType += inputVolume.ToString("F1") + (volAbsolute ? "cm3" : "%") + ") ="; m_doseAtVolumeResultLabel.Content = doseAtVolumeResultType; } if (!Double.IsNaN(inputDose)) { DoseValue.DoseUnit doseUnit = dvhData.MaxDose.Unit; double vol = SelectedPlanningItem.GetVolumeAtDose(SelectedStructure, new DoseValue(inputDose, doseUnit), volPres); double controlVal = DvhExtensions.VolumeAtDose(dvhData, inputDose); double err = Math.Abs((vol - controlVal) / vol); if (err > 0.001) { MessageBox.Show("Value : " + vol.ToString("F3") + " Control Val : " + controlVal.ToString("F3")); } m_resultVolumeAtDose.Content = vol.ToString("F5") + (volAbsolute ? "cm3" : "%"); string volumeAtDoseResultType = doseUnit.ToString() + " V("; volumeAtDoseResultType += inputDose.ToString("F1") + doseUnit.ToString() + " ) ="; m_volumeAtDoseResultLabel.Content = volumeAtDoseResultType; } } }
private string EvaluateConstraints(ScriptContext context, string filename, DataGrid data) { // Load plan/sum based on user input; check for valid dose and structures PlanSetup plan = null; PlanSum psum = null; foreach (PlanSetup p in context.PlansInScope) { if (p.Id == planList[planSelectorMenu.SelectedIndex]) { plan = p; } } foreach (PlanSum s in context.PlanSumsInScope) { if (s.Id == planList[planSelectorMenu.SelectedIndex]) { psum = s; } } SelectedPlanningItem = plan != null ? (PlanningItem)plan : (PlanningItem)psum; // Plans in plansum can have different structuresets but here we only use structureset to allow chosing one structure //SelectedStructureSet = plan != null ? plan.StructureSet : psum.PlanSetups.First().StructureSet; SelectedStructureSet = plan != null ? plan.StructureSet : psum.StructureSet; if (SelectedPlanningItem.Dose == null) { MessageBox.Show("Error: No calculated dose"); return("Error: No calculated dose"); } if (SelectedStructureSet == null) { MessageBox.Show("Error: Could not find a structure set"); return("Error: Could not find a structure set"); } // Other calculation variables double cValue, pValue; string excludedList = "Constraints Excluded:\n"; bool any_missing = false; // Load QUANTEC dose constraint data and dictionary List <ConstraintData> Constraints = LoadConstraintData(filename); string d_file = mainFolder + "\\Resources\\OrganDictionary.csv"; if (!File.Exists(d_file)) { d_file = "Resources\\OrganDictionary.csv"; } List <List <string> > Dictionary = LoadDictionary(d_file); // Make list of plan structure names List <string> PlanNames = new List <string>(); foreach (var s in SelectedStructureSet.Structures) { if (!s.IsEmpty) { PlanNames.Add(s.Id); } } // For each organ constraint, see if any plan structure names match // If so, compare constraint and add to list List <PlanComparison> results = new List <PlanComparison>(); for (int n = 0; n < Constraints.Count(); n++) { // Find matching plan structure name, if able List <string> matchNames = MatchOrganName(Constraints[n].OrganName, Constraints[n].Bilateral, PlanNames, Dictionary); if (matchNames[0] == "None") { excludedList += (Constraints[n].OrganName + ", "); any_missing = true; continue; } // If matching plan structure name found, compare constraint(s) for (int m = 0; m < matchNames.Count(); m++) { for (int p = 0; p < Constraints[n].ConstraintList.Count(); p++) { // Temporary container for constraint row data PlanComparison tempResult = new PlanComparison(); // Set name and load constraint name & value from file tempResult.OrganName = Constraints[n].OrganName; tempResult.StructureName = matchNames[m]; tempResult.ConstraintName = Constraints[n].ConstraintList[p]; cValue = Constraints[n].ConstraintValues[p]; tempResult.ConstraintValue = cValue.ToString() + " " + Constraints[n].ConstraintUnits[p]; tempResult.ConstraintComplication = Constraints[n].ConstraintComplications[p]; // Load DVH for particular structure Structure oar = (from s in SelectedStructureSet.Structures where s.Id == tempResult.StructureName select s).FirstOrDefault(); DVHData dvhData = SelectedPlanningItem.GetDVHCumulativeData(oar, DoseValuePresentation.Absolute, VolumePresentation.Relative, 0.1); // Calculate plan value from DVH if (tempResult.ConstraintName == "Max") { pValue = dvhData.MaxDose.Dose / 100.0; tempResult.PlanValue = pValue.ToString("F1") + " " + Constraints[n].ConstraintUnits[p]; } else if (tempResult.ConstraintName == "Mean") { pValue = dvhData.MeanDose.Dose / 100.0; tempResult.PlanValue = pValue.ToString("F1") + " " + Constraints[n].ConstraintUnits[p]; } else if (tempResult.ConstraintName == "Min") { pValue = dvhData.MinDose.Dose / 100.0; tempResult.PlanValue = pValue.ToString("F1") + " " + Constraints[n].ConstraintUnits[p]; } else { string current = Constraints[n].ConstraintList[p]; double cNumber = Convert.ToDouble(current.Substring(1)); if (current[0] == 'V') { if (Constraints[n].ConstraintUnits[p] == "cc") { pValue = MyDVH.VolumeAtDose(dvhData, cNumber * 100.0, true); } else { pValue = MyDVH.VolumeAtDose(dvhData, cNumber * 100.0); } tempResult.PlanValue = pValue.ToString("F1") + " " + Constraints[n].ConstraintUnits[p]; } else { pValue = (MyDVH.DoseAtVolume(dvhData, cNumber)).Dose / 100.0; tempResult.PlanValue = pValue.ToString("F1") + " " + Constraints[n].ConstraintUnits[p]; } } // Compare plan and constraint values if (Constraints[n].ConstraintComparators[p] == "<") { if (cValue >= pValue) { tempResult.PassFail = "Pass"; } else { tempResult.PassFail = "Fail"; } } else { if (cValue <= pValue) { tempResult.PassFail = "Pass"; } else { tempResult.PassFail = "Fail"; } } tempResult.ConstraintName += " (" + Constraints[n].ConstraintComparators[p] + ")"; results.Add(tempResult); } } } data.ItemsSource = results; if (any_missing) { excludedList = excludedList.Remove(excludedList.Count() - 2); } else { excludedList += ("None"); } return(excludedList); }