1
module ParasitemiaUI.State
4 open System.Collections.Generic
12 type State (defaultConfig
: ParasitemiaCore.Config.Config) =
13 let sourceImages = List<SourceImage> ()
14 let mutable alteredSinceLastSave = false
15 let mutable patientID = ""
17 member this
.AlteredSinceLastSave = alteredSinceLastSave
18 member val CurrentImage : SourceImage option = None with get
, set
19 member val FilePath : string = "" with get
, set
22 with get
() : string = patientID
24 if id <> patientID then
25 alteredSinceLastSave <- true
28 member this
.GlobalParasitemia : int * int =
31 fun (nbTotal
, nbTotalInfected
) srcImg
->
32 let nb, nbInfected
= srcImg
.ImageParasitemia
33 nbTotal
+ nb, nbTotalInfected
+ nbInfected
36 member this
.SetAsInfected (rbc
: RBC) (infected
: bool) =
37 if infected
<> rbc
.infected
then
38 alteredSinceLastSave <- true
39 rbc
.infected
<- infected
40 rbc
.setManually
<- not
rbc.setManually
43 /// Save the current state. 'FilePath' must have been defined.
45 /// <exception cref="System.IOException">If the file cannot be saved</exception>
47 let data = { PiaZ.DocumentData.patientID = this
.PatientID; PiaZ.DocumentData.images
= List.ofSeq
sourceImages }
48 PiaZ.save
this.FilePath data
49 alteredSinceLastSave <- false
52 /// Load the current state. 'FilePath' must have been defined.
54 /// <exception cref="System.IOException">If the file cannot be loaded</exception>
56 let data = PiaZ.load
this.FilePath defaultConfig
57 this.PatientID <- data.patientID
59 sourceImages.InsertRange (0, data.images
)
60 this.CurrentImage <- if sourceImages.Count > 0 then Some sourceImages.[0] else None
61 alteredSinceLastSave <- false
65 /// <exception cref="System.IOException">If the image cannot be read</exception>
66 member this.AddSourceImage (filePath
: string) (defaultConfig
: ParasitemiaCore.Config.Config) : SourceImage =
67 let filename = System.IO.FileInfo(filePath
).Name
68 let srcImg = SourceImage (sourceImages.Count + 1, filename, filename, defaultConfig
.Copy (), DateTime (0L), FromFile filePath, [])
69 sourceImages.Add srcImg
70 if sourceImages.Count = 1 then
71 this.CurrentImage <- Some sourceImages.[0]
72 alteredSinceLastSave <- true
75 member this.RemoveSourceImage (srcImg : SourceImage) =
77 match this.CurrentImage with
78 | Some srcImg' -> srcImg = srcImg'
81 if sourceImages.Remove srcImg then
82 alteredSinceLastSave <- true
84 this.CurrentImage <- if sourceImages.Count > 0 then Some sourceImages.[0] else None
85 // Re-numbered the images.
86 sourceImages |> Seq.iteri
(fun i
srcImg -> srcImg.Num <- i
+ 1)
88 member this.SetName (srcImg : SourceImage) (name
: string) =
89 if name
<> srcImg.Name then
91 alteredSinceLastSave <- true
93 member this.SetResult (imgId
: string) (result
: ParasitemiaCore.Types.AnalysisResult) =
94 let sourceImage = sourceImages.Find (fun srcImg -> srcImg.RomanNum = imgId
)
96 let w = sourceImage.Img.Width
97 let h = sourceImage.Img.Height
99 sourceImage.DateLastAnalysis <- DateTime.UtcNow
101 // To match with previously manually altered RBC.
102 let manuallyAlteredPreviousRBCS = sourceImage.RBCs |> List.filter
(fun rbc -> rbc.setManually
)
103 let tolerance = (float sourceImage.Config.RBCRadius.Pixel) * 0.5 // +/-.
104 let getPreviousManuallyAlteredRBC (center
: Point) : RBC option =
105 manuallyAlteredPreviousRBCS
108 rbc.center
.X > center
.X - tolerance &&
109 rbc.center
.X < center
.X + tolerance &&
110 rbc.center
.Y > center
.Y - tolerance &&
111 rbc.center
.Y < center
.Y + tolerance
116 |> List.filter
(fun cell
-> match cell
.cellClass
with ParasitemiaCore.Types.HealthyRBC | ParasitemiaCore.Types.InfectedRBC -> true | _ -> false )
117 |> List.sortByDescending
(fun cell
-> cell
.nucleusArea
, (w - cell
.center
.X) + (h - cell
.center
.Y))
120 let center = Point (float cell.center.X, float cell.center.Y)
121 let infected, setManually
=
122 let infected = cell.cellClass
= ParasitemiaCore.Types.InfectedRBC
123 match getPreviousManuallyAlteredRBC center with
124 | Some rbc when rbc.infected <> infected -> rbc.infected, true // If it has been previously manually changed and now match the result, the manually flag is removed.
125 | _ -> infected, false
129 setManually
= setManually
131 size
= Size (float cell.elements
.Width, float cell.elements
.Height)
132 infectedArea
= cell.nucleusArea
136 alteredSinceLastSave <- true
138 member this.SourceImages : SourceImage seq =
139 sourceImages :> SourceImage seq
141 member this.Reset () =
144 this.CurrentImage <- None
145 sourceImages.Clear ()
146 alteredSinceLastSave <- false