Created
July 22, 2020 18:51
-
-
Save joecastelo/beed94a70ac0db256118decc5bbfe5e4 to your computer and use it in GitHub Desktop.
Code that receives a PlanSetup and change Fields ID based on gantry angle. Also ranks if STATIC fields are complementary or field in fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
using System.Threading.Tasks; | |
using System.Windows; | |
using VMS.TPS.Common.Model.API; | |
namespace FieldIdFromGantry | |
{ | |
public class BeamIdChanger | |
{ | |
public PlanSetup Plan { get; set; } | |
public IEnumerable<Beam> TreatmentBeams { get; set; } | |
public string Log = ""; | |
public BeamIdChanger(PlanSetup plan) | |
{ | |
Plan = plan; | |
TreatmentBeams = plan.Beams.Where(e=>!e.IsSetupField); | |
} | |
public void ChangeBeamId(Beam beam, string append) | |
{ | |
var lastId = beam.Id; | |
var newId = Regex.Match(lastId,@"^(\d+)(\.?)(\d+)?").Value + " " + append; | |
try | |
{ | |
beam.Id = newId; | |
Log += $"Success changing {lastId} Id to {newId} \n"; | |
} | |
catch (Exception) | |
{ | |
Log += $"Could not change {lastId} Id to {newId} \n"; | |
throw; | |
} | |
} | |
public void ChangeBeamIdFromGantryAngle(Beam beam) | |
{ | |
var futureId = "G" + beam.ControlPoints.First().GantryAngle.ToString("F0"); | |
if (beam.ControlPoints.First().GantryAngle != beam.ControlPoints.Last().GantryAngle) | |
{ | |
futureId = "G" + beam.ControlPoints.First().GantryAngle.ToString("F0") + "_" + beam.ControlPoints.Last().GantryAngle.ToString("F0"); | |
} | |
ChangeBeamId(beam, futureId); | |
} | |
public void ChangeBeamsIdsFromGantryAngles() | |
{ | |
FamilyNaming(TreatmentBeams.Where(e => e.Technique.Id == "STATIC")); | |
RegularNaming(TreatmentBeams.Where(e => e.Technique.Id != "STATIC")); | |
} | |
public void FamilyNaming(IEnumerable<Beam> beams) | |
{ | |
var angleBeams = beams.Select(e => e.ControlPoints.First().GantryAngle).Distinct().ToDictionary(e => e,e => beams.Where(s => s.ControlPoints.First().GantryAngle == e)); | |
var Fams = angleBeams.Select(e => FiFFamily.CreateFifFamily(e.Value)); | |
Fams.Select(e => e.MotherBeam).ToList(). | |
ForEach(s => ChangeBeamId(s, "G" + s.ControlPoints.First().GantryAngle.ToString("F0"))); // MotherBeams | |
Fams.SelectMany(e => e.FiFBeams).ToList(). | |
ForEach(s => ChangeBeamId(s, "G" + s.ControlPoints.First().GantryAngle.ToString("F0") + " FF")); // Fifs Beams; | |
Fams.SelectMany(e => e.NotFiFBeams).ToList(). | |
ForEach(s => ChangeBeamId(s, "G"+ s.ControlPoints.First().GantryAngle.ToString("F0") + " Comp")); // Lower MU but not Fifs Beams; | |
} | |
public void RegularNaming(IEnumerable<Beam> beams) | |
{ | |
beams.ToList().ForEach(e => ChangeBeamIdFromGantryAngle(e)); | |
} | |
} | |
public class FiFFamily | |
{ | |
public IEnumerable<Beam> FiFBeams { get; set; } | |
public Beam MotherBeam { get; set; } | |
public IEnumerable<Beam> NotFiFBeams { get; set; } | |
public double GantryAngle { get => MotherBeam.ControlPoints.First().GantryAngle; set { } } | |
public FiFFamily(IEnumerable<Beam> fiFBeams, Beam motherBeam, IEnumerable<Beam> notFiFBeams) | |
{ | |
FiFBeams = fiFBeams; | |
MotherBeam = motherBeam; | |
NotFiFBeams = notFiFBeams; | |
} | |
public static FiFFamily CreateFifFamily(IEnumerable<Beam> beams) | |
{ | |
var highMU = beams.OrderBy(e => e.Meterset.Value).Last(); // Using the hghest MU to predict mother field | |
var sameColTableJaw = beams.Where( | |
e => e.ControlPoints.First().CollimatorAngle == highMU.ControlPoints.First().CollimatorAngle | |
&& | |
e.ControlPoints.First().PatientSupportAngle == highMU.ControlPoints.First().PatientSupportAngle | |
&& | |
e.ControlPoints.First().JawPositions == highMU.ControlPoints.First().JawPositions).Where(x=>x!=highMU); | |
var notFif = beams.Except(sameColTableJaw).Where(x => x != highMU); | |
return new FiFFamily(sameColTableJaw, highMU,notFif); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment