Skip to content

Commit 895f13d

Browse files
authored
ip
1 parent b8e9d54 commit 895f13d

File tree

3 files changed

+137
-87
lines changed

3 files changed

+137
-87
lines changed

07_send_to_Electron_MOBIUS/0701_Utils.fs

+42-8
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,66 @@ open FsToolkit.ErrorHandling
88
module Utils =
99

1010
// Path to the log file
11-
let logFilePath: string = "log.txt"
11+
let logFilePath : string = "log.txt"
1212

1313
// Function to log a message to the log file with a timestamp
14-
let log (msg: string) : unit =
14+
let log
15+
(msg: string)
16+
: unit =
17+
1518
let timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
1619
let logMessage = sprintf "[%s] %s" timestamp msg
1720
// Append the log message to the file
1821
File.AppendAllText(logFilePath, logMessage + Environment.NewLine)
1922

20-
let warn (msg: string) : unit =
23+
let warn
24+
(msg: string)
25+
: unit =
26+
2127
MessageBox.Show(msg)
2228
|> ignore
2329

24-
let warnAndQuit (msg: string) : unit =
30+
let warnAndQuit
31+
(msg: string)
32+
: unit =
33+
2534
MessageBox.Show(msg)
2635
|> failwith msg
2736

28-
let logAndWarn (msg: string) : unit =
37+
let logAndWarn
38+
(msg: string)
39+
: unit =
40+
2941
msg |> log
3042
msg |> warnAndQuit
3143

32-
let roundTo1 (f: float) : float = Math.Round(f, 1)
44+
let toString =
45+
(expr: 'a)
46+
: string =
47+
48+
try
49+
expr.ToString()
50+
with ex ->
51+
logAndWarn errorMsg
52+
raise ex
53+
54+
let roundTo1
55+
(f: float)
56+
: float =
57+
58+
Math.Round(f, 1)
3359

34-
let roundTo0 (f: float) : float = Math.Round(f, 0)
60+
let roundTo0
61+
(f: float)
62+
: float =
63+
64+
Math.Round(f, 0)
3565

36-
let tryMe (expr: 'a) (errorMsg: string) : 'a =
66+
let tryMe
67+
(expr: 'a)
68+
(errorMsg: string)
69+
: 'a =
70+
3771
try
3872
expr
3973
with ex ->

07_send_to_Electron_MOBIUS/0702_Url.fs

+4-7
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ module Url =
2626
(tpsMu: string)
2727
: string =
2828

29-
// Base URL
30-
let url = baseUrl + "?"
31-
3229
// Query parameters for all fields (including patient name and id)
3330
let queryParams =
3431
[ "patientName_str", urlEncode patientName
@@ -44,16 +41,16 @@ module Url =
4441
"beamName_str0", urlEncode beamName
4542
"tpsMu_int0", tpsMu ]
4643

47-
// Join the parameters to create the query string
44+
// Joins the parameters to create the query string
4845
let queryString =
4946
queryParams
5047
|> List.map (fun (key, value) -> sprintf "%s=%s" key value)
5148
|> String.concat "&"
5249

53-
// Return the full URL
54-
url + queryString
50+
// Returns the full URL
51+
url + "?" + queryString
5552

56-
// Function to open the generated URL in the default browser
53+
// Opens a URL in the default browser
5754
let openUrlInBrowser (url: string) =
5855
let psi = new ProcessStartInfo(url)
5956
psi.UseShellExecute <- true

07_send_to_Electron_MOBIUS/0704_ElectronMobius.fs

+91-72
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,37 @@ module ElectronMobius =
1818
let baseUrl = "http://mobiusfx/quick/electron"
1919

2020
// Shortcuts wrapped in try-catch to handle potential exceptions
21-
let pt = tryMe context.Patient "No Patient"
22-
let plan = tryMe context.ExternalPlanSetup "No Plan"
23-
let beam = tryMe (context.ExternalPlanSetup.Beams |> Seq.head) "No Beam"
24-
let dose = tryMe context.ExternalPlanSetup.Dose "No Dose"
21+
let pt =
22+
tryMe
23+
context.Patient
24+
"No Patient"
25+
26+
let plan =
27+
tryMe
28+
context.ExternalPlanSetup
29+
"No Plan"
30+
31+
let beam =
32+
let firstBeam =
33+
context.ExternalPlanSetup.Beams
34+
|> Seq.head
35+
36+
tryMe
37+
firstBeam
38+
"No Beam"
39+
40+
let dose =
41+
tryMe
42+
context.ExternalPlanSetup.Dose
43+
"No Dose"
2544

2645
if not (plan.IsEntireBodyAndBolusesCoveredByCalculationArea()) then
27-
warnAndQuit "Calculation Area does not cover entire Body and Boluses"
46+
warnAndQuit
47+
"Calculation Area does not cover entire Body and Boluses"
2848

2949
let trySelectEnergy
30-
(beam: Beam) : Result<string, string> =
50+
(beam: Beam)
51+
: Result<string, string> =
3152

3253
match beam.EnergyModeDisplayName with
3354
| "6E" -> Ok "6"
@@ -37,7 +58,8 @@ module ElectronMobius =
3758
| _ -> Error "Beam Energy did not match 6E / 9E / 12E or 16E"
3859

3960
let trySelectApplicator
40-
(beam: Beam) : Result<string, string> =
61+
(beam: Beam)
62+
: Result<string, string> =
4163

4264
match beam.Applicator.Id with
4365
| "A06" -> Ok "6x6"
@@ -48,73 +70,70 @@ module ElectronMobius =
4870
| _ -> Error "Applicator is outside of expected values"
4971

5072
// Single Computation Expression to either full working data or the first Error msg
51-
let urlCE = result {
52-
53-
let patientName = pt.Name //cannot be null
54-
55-
let patientId = pt.Id //cannot be null
56-
57-
let machineName = beam.TreatmentUnit.Id //"Accelerator08" //cannot be null
73+
let urlCE =
74+
result {
75+
let patientName = pt.Name //cannot be null
76+
let patientId = pt.Id //cannot be null
5877

59-
let! beamEnergy = trySelectEnergy beam
60-
61-
let! applicator = trySelectApplicator beam
62-
63-
let! width, length = guessApertureDiameters dose
64-
65-
let cutoutWidth = (width * 0.1 |> roundTo1).ToString()
66-
let cutoutLength = (length * 0.1 |> roundTo1).ToString()
67-
68-
let! ssd =
69-
match (beam.Boluses |> Seq.toList) with // head?
70-
| [b] ->
71-
let ssdToBolusInCm = (beam.GetSourceToBolusDistance(b) * 0.1 |> roundTo1).ToString()
72-
Ok (ssdToBolusInCm)
73-
| [] ->
74-
warn "No Bolus found on the beam. Really? \nCalculation will continue without bolus"
75-
let ssdInCm = (beam.SSD * 0.1 |> roundTo1).ToString()
76-
Ok (ssdInCm)
77-
| _ ->
78-
Error "We cannot calculate if there are more than one bolus on the beam"
78+
let machineName = beam.TreatmentUnit.Id //"Accelerator08" //cannot be null
79+
let! beamEnergy = trySelectEnergy beam
80+
let! applicator = trySelectApplicator beam
7981

80-
let depthPct =
81-
82-
let dmaxInPercent =
83-
if plan.Dose.DoseMax3D.IsRelativeDoseValue then
84-
plan.Dose.DoseMax3D.Dose
85-
else
86-
(plan.Dose.DoseMax3D.Dose / plan.TotalDose.Dose) * 100.0
87-
88-
((200.0 - dmaxInPercent) |> roundTo1 ).ToString()
89-
90-
let beamDose =
91-
(plan.PlannedDosePerFraction.Dose * 100.0 ).ToString() // in cGy
92-
93-
let beamName = beam.Id
94-
95-
let tpsMu = //cannot be null. dose is calculated
96-
(beam.Meterset.Value |> roundTo1).ToString()
97-
98-
// Generate the URL
99-
let url =
100-
generateUrl
101-
baseUrl
102-
patientName
103-
patientId
104-
machineName
105-
beamEnergy
106-
applicator
107-
cutoutWidth
108-
cutoutLength
109-
ssd
110-
depthPct
111-
beamDose
112-
beamName
113-
tpsMu
114-
115-
return url
116-
117-
}
82+
let! width, length = guessApertureDiameters dose
83+
84+
let cutoutWidth = (width * 0.1 |> roundTo1).ToString()
85+
let cutoutLength = (length * 0.1 |> roundTo1).ToString()
86+
87+
let! ssd =
88+
match (beam.Boluses |> Seq.toList) with // head?
89+
| [b] ->
90+
let ssdToBolusInCm = (beam.GetSourceToBolusDistance(b) * 0.1 |> roundTo1).ToString()
91+
Ok (ssdToBolusInCm)
92+
| [] ->
93+
warn "No Bolus found on the beam. Really? \nCalculation will continue without bolus"
94+
let ssdInCm = (beam.SSD * 0.1 |> roundTo1).ToString()
95+
Ok (ssdInCm)
96+
| _ ->
97+
Error "We cannot calculate if there are more than one bolus on the beam"
98+
99+
let depthPct =
100+
101+
let dmaxInPercent =
102+
if plan.Dose.DoseMax3D.IsRelativeDoseValue then
103+
plan.Dose.DoseMax3D.Dose
104+
else
105+
(plan.Dose.DoseMax3D.Dose / plan.TotalDose.Dose) * 100.0
106+
107+
((200.0 - dmaxInPercent) |> roundTo1 ).ToString()
108+
109+
let beamDose =
110+
(plan.PlannedDosePerFraction.Dose * 100.0 ).ToString() // in cGy
111+
112+
let beamName = beam.Id
113+
114+
let tpsMu = //cannot be null. dose is calculated
115+
(beam.Meterset.Value |> roundTo1).ToString()
116+
117+
// Generate the URL
118+
let url =
119+
generateUrl
120+
baseUrl
121+
patientName
122+
patientId
123+
machineName
124+
beamEnergy
125+
applicator
126+
cutoutWidth
127+
cutoutLength
128+
ssd
129+
depthPct
130+
beamDose
131+
beamName
132+
tpsMu
133+
134+
return url
135+
136+
}
118137

119138
match urlCE with
120139
| Ok url -> openUrlInBrowser url

0 commit comments

Comments
 (0)