Exemple #1
0
        public static double VolumeAtDose(DVHData dvhData, double dose, bool absolute = false)
        {
            if (dvhData == null)
            {
                return(Double.NaN);
            }

            DVHPoint[] hist  = dvhData.CurveData;
            int        index = (int)(hist.Length * dose / dvhData.MaxDose.Dose);

            if (index < 0 || index > hist.Length)
            {
                return(0.0);
            }
            else
            {
                double relVolume = hist[index].Volume;
                double absVolume = relVolume * dvhData.Volume * 0.01;
                if (absolute)
                {
                    return(absVolume);
                }
                else
                {
                    return(relVolume);
                }
            }
        }
 public static string GetCoveredVolumeAtDose(StructureSet structureSet, PlanningItemViewModel planningItem, StructureViewModel evalStructure, MatchCollection testMatch, Group evalunit)
 {
     try
     {
         var structure = structureSet.Structures.FirstOrDefault(x => x.Id == evalStructure.StructureName);
         //check for sufficient sampling and dose coverage
         DVHData dvh = planningItem.PlanningItemObject.GetDVHCumulativeData(structure, DoseValuePresentation.Absolute, VolumePresentation.Relative, 0.1);
         if ((dvh.SamplingCoverage < 0.9) || (dvh.Coverage < 0.9))
         {
             return("Unable to calculate - insufficient dose or sampling coverage");
         }
         Group eval            = testMatch[0].Groups["evalpt"];
         Group unit            = testMatch[0].Groups["unit"];
         DoseValue.DoseUnit du = (unit.Value.CompareTo("%") == 0) ? DoseValue.DoseUnit.Percent :
                                 (unit.Value.CompareTo("cGy") == 0) ? DoseValue.DoseUnit.cGy : DoseValue.DoseUnit.Unknown;
         VolumePresentation    vp             = (unit.Value.CompareTo("%") == 0) ? VolumePresentation.Relative : VolumePresentation.AbsoluteCm3;
         DoseValue             dv             = new DoseValue(double.Parse(eval.Value), du);
         double                volume         = double.Parse(eval.Value);
         VolumePresentation    vpFinal        = (evalunit.Value.CompareTo("%") == 0) ? VolumePresentation.Relative : VolumePresentation.AbsoluteCm3;
         DoseValuePresentation dvpFinal       = (evalunit.Value.CompareTo("%") == 0) ? DoseValuePresentation.Relative : DoseValuePresentation.Absolute;
         double                volumeAchieved = planningItem.PlanningItemObject.GetVolumeAtDose(structure, dv, vpFinal);
         double                organVolume    = Convert.ToDouble(evalStructure.VolumeValue);
         double                coveredVolume  = organVolume - volumeAchieved;
         return(string.Format("{0:0.00} {1}", coveredVolume, evalunit.Value));   // todo: better formatting based on VolumePresentation
     }
     catch (NullReferenceException)
     {
         return("Unable to calculate - DVH is not valid");
     }
 }
Exemple #3
0
        public void addPQMInfo(PlanningItem plan, Structure organ, XmlWriter writer)
        {
            DVHData dvh = plan.GetDVHCumulativeData(organ, dvp, VolumePresentation.Relative, 0.1);

            if (dvh != null)
            {
                DoseValue dv = dvh.MeanDose;
                if (name.Length > 0)
                {
                    PQMUtilities.addDosePQMInfo(name, PQMUtilities.DosePQMType.MeanDose, dv, doseConstraint, upperLimit, writer);
                }
                else
                {
                    PQMUtilities.addDosePQMInfo(PQMUtilities.DosePQMType.MeanDose, dv, doseConstraint, upperLimit, writer);
                }
            }
            else
            {
                XElement pqm = new XElement("PQM",
                                            new XAttribute("type", "MeanDose"),
                                            new XAttribute("name", "Dmean < " + doseConstraint.ToString()),
                                            new XElement("Error", string.Format("Error calculating DVH for structure '{0}'.", organ.Id))
                                            );
                pqm.WriteTo(writer);
            }
        }
Exemple #4
0
        private void exportDVH(PlanSetup plan, Structure target, string fileName)
        {
            // extract DVH data
            DVHData dvhData = plan.GetDVHCumulativeData(target,
                                                        DoseValuePresentation.Relative,
                                                        VolumePresentation.AbsoluteCm3, 0.1);

            if (dvhData == null)
            {
                throw new ApplicationException("No DVH data for target '" + target.Id + "'.");
            }

            // export DVH data as a CSV file
            using (System.IO.StreamWriter sw =
                       new System.IO.StreamWriter(fileName, false, Encoding.ASCII))
            {
                // write the header, assume the first dvh point represents the others
                DVHPoint rep = dvhData.CurveData[0];
                sw.WriteLine(
                    string.Format("Relative dose [{0}],Structure volume [{1}],",
                                  rep.DoseValue.UnitAsString, rep.VolumeUnit)
                    );
                // write each row of dose / volume data
                foreach (DVHPoint pt in dvhData.CurveData)
                {
                    sw.WriteLine(string.Format("{0:0.0},{1:0.00000}",
                                               pt.DoseValue.Dose, pt.Volume));
                }
            }
        }
Exemple #5
0
        public void AddDvhCurve(Structure structure)
        {
            DVHData dvh = CalculateDvh(structure);

            PlotModel.Series.Insert(0, CreateDvhSeries(structure, dvh));
            UpdatePlot();
        }
        public void DrawDVH(DVHData dvhData, Structure s)
        {
            // Calculate multipliers for scaling DVH to canvas.
            ps.DoseValuePresentation = DoseValuePresentation.Absolute;
            double xCoeff = MainCanvas.Width / ps.Dose.DoseMax3D.Dose;
            double yCoeff = MainCanvas.Height / 100;

            // Set Y axis label
            DoseMaxLabel.Content = string.Format("{0:F1}", ps.Dose.DoseMax3D.ToString());
            TextBlock tb = new TextBlock();

            tb.Text = string.Format("{0}: Max Dose: {1}; Min Dose: {2}; Mean Dose: {3}",
                                    s.Id, dvhData.MaxDose.ToString(), dvhData.MinDose.ToString(), dvhData.MeanDose.ToString());
            stats_sp.Children.Add(tb);
            // Draw histogram
            for (int i = 0; i < dvhData.CurveData.Length - 1; i++)
            {
                // Set drawing line parameters
                var line = new Line()
                {
                    Stroke = new SolidColorBrush(s.Color), StrokeThickness = 4.0
                };

                // Set line coordinates
                line.X1 = dvhData.CurveData[i].DoseValue.Dose * xCoeff;
                line.X2 = dvhData.CurveData[i + 1].DoseValue.Dose * xCoeff;
                // Y axis start point is top-left corner of window, convert it to bottom-left.
                line.Y1 = MainCanvas.Height - dvhData.CurveData[i].Volume * yCoeff;
                line.Y2 = MainCanvas.Height - dvhData.CurveData[i + 1].Volume * yCoeff;

                // Add line to the existing canvas
                MainCanvas.Children.Add(line);
            }
        }
        // this method works through the entire Eclipse database, opening each patient,
        // looking through each course for a plan with valid dose.  A DHV csv file
        // is dumped for each plan's target structure in the directory c:\temp\dvhdump.
        static void Execute(Application app)
        {
            string outputdir = @"c:\temp\dvhdump";

            System.IO.Directory.CreateDirectory(outputdir);

            var patSummaries = app.PatientSummaries;

            foreach (PatientSummary ps in patSummaries)
            {
                Patient p = app.OpenPatient(ps);
                foreach (Course c in p.Courses)
                {
                    // select plans from the course that have valid dose
                    foreach (PlanSetup plan in c.PlanSetups.Where(x => x.IsDoseValid))
                    {
                        // find the planning target
                        Structure target = plan.StructureSet.Structures.Where(x => x.Id == plan.TargetVolumeID).FirstOrDefault();
                        // extract the DVH of the planning target, dump to a file in c:\temp\dvhdump directory.
                        if (plan.Dose != null && target != null)
                        {
                            DVHData dvh      = plan.GetDVHCumulativeData(target, DoseValuePresentation.Absolute, VolumePresentation.Relative, 0.1);
                            string  filename = string.Format(@"{0}\{1}_{2}_{3}_{4}-dvh.csv",
                                                             outputdir, p.Id, c.Id, plan.Id, target.Id);
                            DumpDVH(filename, dvh);
                            Console.WriteLine(filename);
                        }
                    }
                }
                app.ClosePatient();
            }
        }
 private void OnStructureSelectionChanged(StructureSelectionModel selStructure)
 {
     if (selStructure.bIsChecked)
     {
         //add to the plot model
         Structure s   = _plan.StructureSet.Structures.FirstOrDefault(x => x.Id == selStructure.Id);
         DVHData   dvh = _plan.GetDVHCumulativeData(s,
                                                    DoseValuePresentation.Absolute,
                                                    VolumePresentation.Relative,
                                                    1);
         if (dvh != null)
         {
             GeneratePlotSeries(s, dvh);
         }
     }
     else
     {
         //remove from teh plot model
         if (DVHPlotModel.Series.FirstOrDefault(x => x.Title == selStructure.Id) != null)
         {
             DVHPlotModel.Series.Remove(DVHPlotModel.Series.FirstOrDefault(x => x.Title == selStructure.Id));
         }
     }
     DVHPlotModel.InvalidatePlot(true);
 }
Exemple #9
0
        public static DoseValue DoseAtVolume(DVHData dvhData, double volume, DoseValue?psumTotalDose = null)
        {
            if (dvhData == null || dvhData.CurveData.Count() == 0)
            {
                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)
                {
                    if (psumTotalDose == null)
                    {
                        return(hist[i].DoseValue);
                    }
                    else
                    {
                        return(new DoseValue(hist[i].DoseValue.Dose / ((DoseValue)psumTotalDose).Dose * 100, DoseValue.DoseUnit.Percent));
                    }
                }
            }
            return(DoseValue.UndefinedDose());
        }
Exemple #10
0
 private void DrawDVH()
 {
     MyPlotModel.Series.Clear();
     foreach (Structure s in _planSetup.StructureSet.Structures)
     {
         if (s.DicomType != "MARKER" && s.DicomType != "SUPPORT" && s.HasSegment)
         {
             //get the DVH
             DVHData dvh = _planSetup.GetDVHCumulativeData(s,
                                                           VMS.TPS.Common.Model.Types.DoseValuePresentation.Absolute,
                                                           VMS.TPS.Common.Model.Types.VolumePresentation.Relative,
                                                           1);
             //draw dvh to MyPlotModel
             LineSeries series = new LineSeries {
                 Title = $"{s.Id}"
             };
             foreach (var datapoint in dvh.CurveData)
             {
                 series.Points.Add(new DataPoint(datapoint.DoseValue.Dose,
                                                 datapoint.Volume));
             }
             MyPlotModel.Series.Add(series);
         }
     }
 }
        public static string GetVolumeAtDose(StructureSet structureSet, PlanningItem planningItem, Structure evalStructure, MatchCollection testMatch, Group evalunit)
        {
            //check for sufficient sampling and dose coverage
            DVHData dvh = planningItem.GetDVHCumulativeData(evalStructure, DoseValuePresentation.Absolute, VolumePresentation.Relative, 0.1);

            //MessageBox.Show(evalStructure.Id + "- Eval unit: " + evalunit.Value.ToString() + "Achieved unit: " + dvAchieved.UnitAsString + " - Sampling coverage: " + dvh.SamplingCoverage.ToString() + " Coverage: " + dvh.Coverage.ToString());
            if ((dvh.SamplingCoverage < 0.9) || (dvh.Coverage < 0.9))
            {
                return("Unable to calculate - insufficient dose or sampling coverage");
            }
            Group eval = testMatch[0].Groups["evalpt"];
            Group unit = testMatch[0].Groups["unit"];

            DoseValue.DoseUnit du = (unit.Value.CompareTo("%") == 0) ? DoseValue.DoseUnit.Percent :
                                    (unit.Value.CompareTo("cGy") == 0) ? DoseValue.DoseUnit.cGy : DoseValue.DoseUnit.Unknown;
            VolumePresentation    vp             = (unit.Value.CompareTo("%") == 0) ? VolumePresentation.Relative : VolumePresentation.AbsoluteCm3;
            DoseValue             dv             = new DoseValue(double.Parse(eval.Value), du);
            double                volume         = double.Parse(eval.Value);
            VolumePresentation    vpFinal        = (evalunit.Value.CompareTo("%") == 0) ? VolumePresentation.Relative : VolumePresentation.AbsoluteCm3;
            DoseValuePresentation dvpFinal       = (evalunit.Value.CompareTo("%") == 0) ? DoseValuePresentation.Relative : DoseValuePresentation.Absolute;
            double                volumeAchieved = planningItem.GetVolumeAtDose(evalStructure, dv, vpFinal);

            return(string.Format("{0:0.00} {1}", volumeAchieved, evalunit.Value));   // todo: better formatting based on VolumePresentation
//#if false
//            string message = string.Format("{0} - Dose unit = {1}, Volume Presentation = {2}, vpFinal = {3}, dvpFinal ={4}",
//               objective.DVHObjective, du.ToString(), vp.ToString(), vpFinal.ToString(), dvpFinal.ToString());
//           MessageBox.Show(message);
//#endif
        }
        //AAPM Report NO. 166:
        //The use and QA of Biologically Related Models for Treatment Planning
        //gEUD = (sum(viDi^a))^1/a
        //"where vi is the fractional organ volume receiving a dose Di
        //and a is a tissue-specific parameter that describes the volume affect."
        private double CalculateGEUD(Structure s, PlanningItem pi, KeyValuePair <string, double> a_lookup)
        {
            //collect the DVH
            //if volume is not relative, make sure to normalize over the total volume during geud calculation.
            //double volume = s.Volume;
            //remember plansums must be absolute dose.
            DVHData dvh = pi.GetDVHCumulativeData(s, DoseValuePresentation.Absolute, VolumePresentation.Relative, 0.1);

            if (dvh == null)
            {
                MessageBox.Show("Could not calculate DVH");
                return(Double.NaN);
            }
            //we need to get the differential volume from the definition. Loop through Volumes and take the difference with the previous dvhpoint
            double running_sum = 0;
            int    counter     = 0;

            foreach (DVHPoint dvhp in dvh.CurveData.Skip(1))
            {
                //volume units are in % (divide by 100)
                double vol_diff = Math.Abs(dvhp.Volume - dvh.CurveData[counter].Volume) / 100;
                double dose     = dvhp.DoseValue.Dose;
                running_sum += vol_diff * Math.Pow(dose, a_lookup.Value);
                counter++;
            }
            double geud = Math.Pow(running_sum, 1 / a_lookup.Value);

            return(geud);
        }
Exemple #13
0
        // Draw DVH
        private void DrawDVH()
        {
            if (null != _planSetup.StructureSet)
            {
                foreach (var item in
                         _planSetup.StructureSet.Structures.Where(
                             x => x.DicomType != "MARKER" && x.DicomType != "SUPPORT" && x.HasSegment))
                {
                    // get dvh
                    DVHData dvhData = _planSetup.GetDVHCumulativeData(
                        item, VMS.TPS.Common.Model.Types.DoseValuePresentation.Absolute, VMS.TPS.Common.Model.Types.VolumePresentation.Relative, 1);

                    // draw dvh to MyPlotModel
                    LineSeries series = new LineSeries {
                        Title = $"{item.Id}"
                    };
                    foreach (var dataPoint in dvhData.CurveData)
                    {
                        series.Points.Add(new DataPoint(dataPoint.DoseValue.Dose, dataPoint.Volume));
                    }

                    MyPlotModel.Series.Add(series);
                }
            }
            else
            {
                throw new Exception("Empty StructureSet");
            }
        }
        public void Execute(ScriptContext context /*, System.Windows.Window window, ScriptEnvironment environment*/)
        {
            // declare local variables that reference the objects we need.
            PlanSetup plan = context.PlanSetup;

            if (plan == null)
            {
                MessageBox.Show("Please load a plan before running this script.");
                return;
            }
            StructureSet ss = context.StructureSet;

            if (ss == null)
            {
                MessageBox.Show("Please load a structure set before running this script.");
                return;
            }
            var listStructures = context.StructureSet.Structures;
            // 'listStructures' if of type IEnumerable<Structure>


            // loop through structure list and find a structure named "Bladder"
            Structure bladder = null;

            foreach (Structure scan in listStructures)
            {
                if (scan.Id == "Bladder")
                {
                    bladder = scan;
                }
            }
            if (bladder == null)
            {
                MessageBox.Show("Bladder not found!");
                return;
            }

            string msg = string.Format("bladder volume = {0}", bladder.Volume);

            MessageBox.Show(msg, "Varian Developer");

            // extract DVH data for bladder using bin width of 0.1.
            DVHData dvh = plan.GetDVHCumulativeData(bladder, DoseValuePresentation.Absolute, VolumePresentation.Relative, 0.1);

            string filename = string.Format(@"c:\temp\bladder_dvh-{0}.csv", context.CurrentUser.Name);

            System.IO.StreamWriter dvhFile = new System.IO.StreamWriter(filename);
            // write a header
            dvhFile.WriteLine("Dose,Volume");
            // write all dvh points for the PTV.
            foreach (DVHPoint pt in dvh.CurveData)
            {
                string line = string.Format("{0},{1}", pt.DoseValue.Dose, pt.Volume);
                dvhFile.WriteLine(line);
            }
            dvhFile.Close();
            msg = string.Format("dvh file written to {0}", filename);
            MessageBox.Show(msg, "Varian Developer");
        }
Exemple #15
0
        public static OxyPlot.Series.Series CreateDVHSeries(DVHData dvh)
        {
            var series = new LineSeries();
            var points = CreateDataPoints(dvh);

            series.Points.AddRange(points);
            return(series);
        }
        public static PlanSetup CopyAndMakeNewIscPlan(double thresholdPc, string newPlanId, Course course, PlanSetup originalPlanSetup, int numberOfSteps = 1)
        {
            if (course.PlanSetups.Where(p => p.Id == newPlanId).Count() > 0)
            {
                throw new ArgumentException($"{newPlanId} already exists");
            }

            Patient patient = originalPlanSetup.Course.Patient;

            patient.BeginModifications();

            CopyPlan.CopyDynamicMlcPlan(newPlanId, course, (ExternalPlanSetup)originalPlanSetup);
            PlanSetup newPlanSetup = Esapi.IscFluenceOptimizer.Helpers.GetPlanSetup(course, newPlanId);

            // Get Body and Maximum dose
            var query = from s in originalPlanSetup.StructureSet.Structures where (s.Id == "BODY" || s.Id == "Body") select s;

            if (query.Count() != 1)
            {
                throw new InvalidOperationException("No BODY in StructureSet");
            }
            var     body          = query.First();
            double  binWidth      = 0.001;
            DVHData dvh           = originalPlanSetup.GetDVHCumulativeData(body, DoseValuePresentation.Relative, VolumePresentation.Relative, binWidth);
            double  maximumDosePc = dvh.MaxDose.Dose;

            int numberOfMainBeams = originalPlanSetup.Beams.Count();

            if (maximumDosePc <= thresholdPc)
            {
                throw new InvalidOperationException($"Maximum dose ({maximumDosePc}) is less than threholdPc ({thresholdPc})");
            }

            double thresholdPcStep = (maximumDosePc - thresholdPc) / numberOfSteps;

            for (int i = 0; i < numberOfSteps; i++)
            {
                double thresholdPcTmp      = thresholdPc + thresholdPcStep * (numberOfSteps - 1 - i);
                var    iscFluenceOptimizer = new IscFluenceOptimizer(newPlanSetup, thresholdPcTmp);

                foreach (var beam in iscFluenceOptimizer.BeamInfos)
                {
                    var newFluence = beam.GetReducedFluence();
                    beam.BeamEsapi.SetOptimalFluence(newFluence);
                }

                var externalPlanSetup = (ExternalPlanSetup)newPlanSetup;

                externalPlanSetup.CalculateLeafMotions(new LMCVOptions(true));
                externalPlanSetup.CalculateDose();
                var newDvh           = newPlanSetup.GetDVHCumulativeData(body, DoseValuePresentation.Relative, VolumePresentation.Relative, binWidth);
                var newMaximumDosePc = newDvh.MaxDose.Dose;
            }

            return(newPlanSetup);
        }
        public void Cb_Checked(object sender, RoutedEventArgs e)
        {
            //throw new NotImplementedException();
            Structure s       = ps.StructureSet.Structures.First(x => x.Id == (sender as CheckBox).Content.ToString());
            DVHData   dvhData = ps.GetDVHCumulativeData(s,
                                                        DoseValuePresentation.Absolute,
                                                        VolumePresentation.Relative, 1);

            DrawDVH(dvhData, s);
        }
        private Series CreateDvhSeries(string structureId, DVHData dvh)
        {
            var series = new LineSeries {
                Tag = structureId
            };
            var points = dvh.CurveData.Select(CreateDataPoint);

            series.Points.AddRange(points);
            return(series);
        }
 //Homogeneity Index: An objective tool for assessment of conformal radiation treatment.
 //J Med Phys 2012 Oct-Dec; 37(4): 207-213
 //HI = (D2-D98)/Dpx100 where "D2 = minimum dose to 2% of the target volume...
 //D98 = minimum dose to 98% of the target volume...
 //and Dp = prescribed dose."
 private double CalculateHI(Structure s, PlanningItem pi)
 {
     //hi only needs to be calculated for ptv, so filter those out here.
     if (!s.Id.ToUpper().Contains("PTV"))
     {
         return(Double.NaN);
     }
     //now check if the planning item is a plansetup or sum.
     if (pi is PlanSetup)
     {
         //plansetups have a method called GetDoseAtVolume.
         if (pi.Dose == null)
         {
             MessageBox.Show("Plan has no dose");
             return(Double.NaN);
         }
         double d2  = (pi as PlanSetup).GetDoseAtVolume(s, 2, VolumePresentation.Relative, DoseValuePresentation.Absolute).Dose;
         double d98 = (pi as PlanSetup).GetDoseAtVolume(s, 98, VolumePresentation.Relative, DoseValuePresentation.Absolute).Dose;
         double hi  = ((d2 - d98) / (pi as PlanSetup).TotalPrescribedDose.Dose) * 100;
         return(hi);
     }
     else if (pi is PlanSum)
     {
         //must manually calculate value from DVH
         DVHData dvh = pi.GetDVHCumulativeData(s, DoseValuePresentation.Absolute, VolumePresentation.Relative, 0.1);
         if (dvh == null)
         {
             MessageBox.Show("Could not collect DVH");
             return(Double.NaN);
         }
         double        d98      = dvh.CurveData.FirstOrDefault(x => x.Volume <= 98).DoseValue.Dose;
         double        d2       = dvh.CurveData.FirstOrDefault(x => x.Volume <= 2).DoseValue.Dose;
         List <double> rx_doses = new List <double>();
         foreach (PlanSetup ps in (pi as PlanSum).PlanSetups)
         {
             try
             {
                 rx_doses.Add(ps.TotalPrescribedDose.Dose);
             }
             catch
             {
                 MessageBox.Show("One of the prescriptions for the plansum is not defined");
                 return(Double.NaN);
             }
         }
         double rx = rx_doses.Sum();
         double hi = ((d2 - d98) / rx) * 100;
         return(hi);
     }
     else
     {
         MessageBox.Show("Plan not handled correctly");
         return(Double.NaN);
     }
 }
Exemple #20
0
        private OxyPlot.Series.Series CreateDvhSeries(Structure structure, DVHData dvh)
        {
            var series = new OxyPlot.Series.LineSeries {
                Tag = structure.Id, Title = structure.Id, Color = OxyColor.FromRgb(structure.Color.R, structure.Color.G, structure.Color.B)
            };
            var points = dvh.CurveData.Select(CreateDataPoint);

            series.Points.AddRange(points);
            series.TrackerFormatString = "{0} " + Environment.NewLine + "{1}: {2:0.000} " + Environment.NewLine + "{3}: {4:0.0} ";
            return(series);
        }
Exemple #21
0
        public static List <DataPoint> CreateDataPoints(DVHData dvh)
        {
            var points = new List <DataPoint>();

            foreach (var dvhPoint in dvh.CurveData)
            {
                var point = CreateDataPoint(dvhPoint);
                points.Add(point);
            }
            return(points);
        }
Exemple #22
0
 public static double GetVolumeAtDose(this PlanningItem pitem, Structure structure, DoseValue dose, VolumePresentation requestedVolumePresentation)
 {
     if (pitem is PlanSetup)
     {
         return(((PlanSetup)pitem).GetVolumeAtDose(structure, dose, requestedVolumePresentation));
     }
     else
     {
         DVHData dvh = pitem.GetDVHCumulativeData(structure, DoseValuePresentation.Absolute, requestedVolumePresentation, 0.001);
         return(DVHExtensions.VolumeAtDose(dvh, dose.Dose));
     }
 }
        // Calculate Volume at Dose.
        public static double GetVolumeAtDose(this PlanningItem pitem, Structure structure, DoseValue dose, VolumePresentation requestedVolumePresentation)
        {
            if (pitem is PlanSetup)
            {
                //try catch statement to switch dose units to system presentation. Otherwise exception "Dose Units do not match to system settings
                try
                {
                    return(((PlanSetup)pitem).GetVolumeAtDose(structure, dose, requestedVolumePresentation));
                }
                catch
                {
                    if (dose.Unit.CompareTo(DoseValue.DoseUnit.cGy) == 0)
                    {
                        return(((PlanSetup)pitem).GetVolumeAtDose(structure, new DoseValue(dose.Dose / 100, DoseValue.DoseUnit.Gy), requestedVolumePresentation));
                    }
                    else if (dose.Unit.CompareTo(DoseValue.DoseUnit.Gy) == 0)
                    {
                        return(((PlanSetup)pitem).GetVolumeAtDose(structure, new DoseValue(dose.Dose * 100, DoseValue.DoseUnit.cGy), requestedVolumePresentation));
                    }
                    else
                    {
                        return(((PlanSetup)pitem).GetVolumeAtDose(structure, dose, requestedVolumePresentation));
                    }
                }
            }
            else
            {
                DVHData dvh = pitem.GetDVHCumulativeData(structure, DoseValuePresentation.Absolute, requestedVolumePresentation, 0.001);

                // Convert dose unit to system unit: otherwise false output without warning.
                try
                {
                    ((PlanSum)pitem).PlanSetups.First().GetVolumeAtDose(structure, dose, requestedVolumePresentation);
                    return(DvhExtensions.VolumeAtDose(dvh, dose.Dose));
                }
                catch
                {
                    if (dose.Unit.CompareTo(DoseValue.DoseUnit.cGy) == 0)
                    {
                        return(DvhExtensions.VolumeAtDose(dvh, dose.Dose / 100));
                    }
                    else if (dose.Unit.CompareTo(DoseValue.DoseUnit.Gy) == 0)
                    {
                        return(DvhExtensions.VolumeAtDose(dvh, dose.Dose * 100));
                    }
                    else
                    {
                        return(DvhExtensions.VolumeAtDose(dvh, dose.Dose));
                    }
                }
            }
        }
Exemple #24
0
 static void DumpDVH(string filename, DVHData dvh)
 {
     System.IO.StreamWriter dvhFile = new System.IO.StreamWriter(filename);
     // write a header
     dvhFile.WriteLine("Dose,Volume");
     // write all dvh points for the PTV.
     foreach (DVHPoint pt in dvh.CurveData)
     {
         string line = string.Format("{0},{1}", pt.DoseValue.Dose, pt.Volume);
         dvhFile.WriteLine(line);
     }
     dvhFile.Close();
 }
Exemple #25
0
        public void Execute(ScriptContext context /*, System.Windows.Window window*/)
        {
            // TODO : Add here your code that is called when the script is launched from Eclipse
            string msg = string.Format(
                "Context:\n\tPatient=\t\t{0}\n\tImage=\t\t{1}\n\tCourse=\t\t{2}\n\tPlan =\t\t{3}\n\tStructure Set =\t{4}\n",
                context.Patient.Id,
                context.Image.Id,
                context.Course.Id,
                context.PlanSetup.Id,
                context.StructureSet.Id);

            MessageBox.Show(msg, "Varian Developer");

            // declare local variables that reference the objects we need.
            PlanSetup    plan           = context.PlanSetup;
            StructureSet ss             = context.StructureSet;
            var          listStructures = context.StructureSet.Structures;
            // 'listStructures' if of type IEnumerable<Structure>

            // loop through structure list and find the PTV
            Structure ptv = null;

            foreach (Structure scan in listStructures)
            {
                if (scan.Id == "PTV")
                {
                    ptv = scan;
                }
            }
            msg = string.Format("PTV volume = {0}", ptv.Volume);
            MessageBox.Show(msg, "Varian Developer");

            // extract DVH data for PTV using bin width of 0.1.
            DVHData dvh = plan.GetDVHCumulativeData(ptv, DoseValuePresentation.Absolute, VolumePresentation.Relative, 0.1);

            string filename = @"c:\temp\keranen_dvh.csv";

            System.IO.StreamWriter dvhFile = new System.IO.StreamWriter(filename);
            // write a header
            dvhFile.WriteLine("Dose,Volume");
            // write all dvh points for the PTV.
            foreach (DVHPoint pt in dvh.CurveData)
            {
                string line = string.Format("{0},{1}", pt.DoseValue.Dose, pt.Volume);
                dvhFile.WriteLine(line);
            }
            dvhFile.Close();
            msg = string.Format("dvh file written to {0}", filename);
            MessageBox.Show(msg, "Varian Developer");
        }
        //dvh generation is controlled only by events from the StructureSelectionModel.
        //private void GetDefaultDVH()
        //{
        //    foreach(var structure in _plan.StructuresSelectedForDvh)
        //    {
        //        var dvh = _plan.GetDVHCumulativeData(structure,
        //            DoseValuePresentation.Absolute,
        //            VolumePresentation.Relative,
        //            1);
        //        if (dvh != null)
        //        {
        //            GeneratePlotSeries(structure, dvh);
        //        }
        //    }
        //}
        private void GeneratePlotSeries(Structure structure, DVHData dvh)
        {
            var dvhSeries = new LineSeries
            {
                Title = structure.Id,
                Color = OxyColor.FromArgb(structure.Color.A, structure.Color.R, structure.Color.G, structure.Color.B)
            };

            foreach (var dvhPoint in dvh.CurveData)
            {
                dvhSeries.Points.Add(new DataPoint(dvhPoint.DoseValue.Dose, dvhPoint.Volume));
            }
            DVHPlotModel.Series.Add(dvhSeries);
        }
Exemple #27
0
        static void DrawDVH(DVHPlot.ViewModel.MainWindowModel viewModel, VMS.TPS.Common.Model.API.PlanSetup curps)
        {
            StructureSet structureSet = curps.StructureSet;

            foreach (var structure in structureSet.Structures)
            {
                DVHData dvhData = curps.GetDVHCumulativeData(structure,
                                                             DoseValuePresentation.Absolute,
                                                             VolumePresentation.Relative, 1);
                OxyPlot.OxyColor   color  = new OxyPlot.OxyColor();
                OxyPlot.MarkerType marker = new OxyPlot.MarkerType();
                viewModel.AddData(dvhData, structure.Id, color, marker);
            }
        }
Exemple #28
0
 private void Button_Click(object sender, RoutedEventArgs e)
 {
     if (MyComboBox.SelectedIndex != -1)
     {
         Structure s = plan.StructureSet.Structures
                       .FirstOrDefault(x => x.Id == MyComboBox.SelectedItem.ToString());
         //This should always be true, but we would have an issue if it wasn't
         DVHData dvhData = plan.GetDVHCumulativeData(s, DoseValuePresentation.Absolute,
                                                     VolumePresentation.Relative, 0.1);
         if (dvhData != null)
         {
             DrawDVH(dvhData, s);
         }
     }
 }
Exemple #29
0
        private void OnAddMetric()
        {
            DVHMetric dvh_temp = new DVHMetric
            {
                StructureId = SelectedStructure,
                DoseMetric  = SelectedMetric,
                OutputValue = "N/A"
            };
            Structure s   = _planSetup.StructureSet.Structures.FirstOrDefault(x => x.Id == SelectedStructure);
            DVHData   dvh = _planSetup.GetDVHCumulativeData(
                s,
                VMS.TPS.Common.Model.Types.DoseValuePresentation.Absolute,
                VMS.TPS.Common.Model.Types.VolumePresentation.Relative,
                1);

            if (dvh != null)
            {
                switch (Metrics.IndexOf(SelectedMetric))
                {
                case (int)MetricType.Max:
                    dvh_temp.OutputValue = dvh.MaxDose.ToString();
                    break;

                case (int)MetricType.Min:
                    dvh_temp.OutputValue = dvh.MinDose.ToString();
                    break;

                case (int)MetricType.Mean:
                    dvh_temp.OutputValue = dvh.MeanDose.ToString();
                    break;

                case (int)MetricType.Volume:
                    dvh_temp.OutputValue = s.Volume.ToString("F2") + "cc";
                    break;

                case (int)MetricType.VolA_at_DoseA:
                    DoseValue dv = new DoseValue(Convert.ToDouble(CustomMetric),
                                                 DoseValue.DoseUnit.cGy);
                    double volume = _planSetup.GetVolumeAtDose(s,
                                                               dv,
                                                               VolumePresentation.AbsoluteCm3);
                    dvh_temp.OutputValue = volume.ToString("F2") + "cc";
                    dvh_temp.DoseMetric  = $"Volume at {dv}";
                    break;
                }
            }
            DQPs.Add(dvh_temp);
        }
Exemple #30
0
        public string NTCPreturn(string strnum, string gradename, StructureSet list, PlanSum plan, double n, double m, double td50)
        {
            Structure retstructure = null;
            int       bit          = 0;
            double    EUD          = 0;
            double    Ntcp         = 0;
            string    NTCPstring   = "";

            foreach (Structure scan in list.Structures)
            {
                if (scan.Id == strnum)
                {
                    retstructure = scan;
                    bit          = 1;
                    if (retstructure.IsEmpty == true)
                    {
                        bit = 2; break;
                    }
                    DVHData dvh     = plan.GetDVHCumulativeData(retstructure, DoseValuePresentation.Absolute, VolumePresentation.Relative, 1);
                    var     dvhdata = dvh.CurveData.ToList();
                    string  volume  = dvh.Volume.ToString();
                    string  dvhstr  = volume;
                    double  fEUD    = 0;
                    string  dvhnums = dvhdata.Count.ToString();
                    for (int i = 0; i < dvhdata.Count - 1; i++)
                    {
                        fEUD += (dvhdata[i].Volume - dvhdata[i + 1].Volume) / 100 * Math.Pow(dvhdata[i].DoseValue.Dose / 100, 1 / n);
                    }
                    EUD = Math.Pow(fEUD, n);
                    double t = treturn(EUD, m, td50);
                    Ntcp        = NTCP(t);
                    NTCPstring += string.Format("NTCP of {0}, {1} is {2}%\n", strnum, gradename, (Ntcp * 100).ToString("F2"));
                }
            }
            if (bit == 0)
            {
                NTCPstring += "";
                return(NTCPstring);
            }
            else if (bit == 2)
            {
                NTCPstring += ""; return(NTCPstring);
            }
            else
            {
                return(NTCPstring);
            }
        }