e15dc9b7a06b4325cbc9170e4ccf516628be2244
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
26 alteredSinceLastSave <- true
29 member this
.ImageParasitemia (srcImg
: SourceImage) : int * int =
30 List.length srcImg
.rbcs
,
31 srcImg
.rbcs
|> List.fold
(fun nbInfected rbc
-> if rbc
.infected
then nbInfected
+ 1 else nbInfected) 0
33 member this
.ImageNbManuallyChangedRBC (srcImg
: SourceImage) (setAsInfected
: bool) : int * int =
34 List.length srcImg
.rbcs
,
35 srcImg
.rbcs
|> List.fold
(fun nb rbc
-> if rbc
.setManually
&& rbc
.infected
= setAsInfected
then nb
+ 1 else nb) 0
37 member this
.ImageNbManuallyChangedRBCStr (srcImg
: SourceImage) (setAsInfected
: bool) : string =
38 Utils.percentText
(this
.ImageNbManuallyChangedRBC srcImg setAsInfected
)
40 member this
.ImageManuallyChangedRBC (srcImg
: SourceImage) (setAsInfected
: bool) : int seq =
42 for rbc
in srcImg
.rbcs
do
43 where
(rbc
.setManually
&& rbc
.infected
= setAsInfected
)
46 member this
.ImageManuallyChangedRBCStr (srcImg
: SourceImage) (setAsInfected
: bool) : string =
47 let listStr = Utils.listAsStr
<| this.ImageManuallyChangedRBC srcImg setAsInfected
50 else "[" + listStr + "]"
52 member this.GlobalParasitemia : int * int =
54 |> Seq.fold
(fun (nbTotal
, nbTotalInfected
) srcImg
->
55 let nb, nbInfected = this.ImageParasitemia srcImg
56 nbTotal
+ nb, nbTotalInfected
+ nbInfected) (0, 0)
59 member this.SetAsInfected (rbc: RBC) (infected
: bool) =
60 if infected
<> rbc.infected
62 alteredSinceLastSave <- true
63 rbc.infected
<- infected
64 rbc.setManually
<- not
rbc.setManually
67 /// Save the current state. 'FilePath' must have been defined.
69 /// <exception cref="System.IOException">If the file cannot be saved</exception>
71 let data = { PiaZ.DocumentData.patientID = this.PatientID; PiaZ.DocumentData.images
= List.ofSeq
sourceImages }
72 PiaZ.save
this.FilePath data
73 alteredSinceLastSave <- false
76 /// Load the current state. 'FilePath' must have been defined.
78 /// <exception cref="System.IOException">If the file cannot be loaded</exception>
80 let data = PiaZ.load
this.FilePath defaultConfig
81 this.PatientID <- data.patientID
83 sourceImages.InsertRange(0, data.images
)
84 this.CurrentImage <- if sourceImages.Count > 0 then Some sourceImages.[0] else None
85 alteredSinceLastSave <- false
89 /// <exception cref="System.IOException">If the image cannot be read</exception>
90 member this.AddSourceImage (filePath
: string) (defaultConfig
: ParasitemiaCore.Config.Config) : SourceImage =
92 { num
= sourceImages.Count + 1
93 name
= System.IO.FileInfo(filePath
).Name
94 config
= defaultConfig
.Copy()
95 dateLastAnalysis
= DateTime(0L)
97 img
= new Image<Bgr, byte
>(filePath
)
98 healthyRBCBrightness
= 1.f
99 infectedRBCBrightness
= 1.f
}
101 sourceImages.Add(srcImg)
102 if sourceImages.Count = 1
103 then this.CurrentImage <- Some sourceImages.[0]
104 alteredSinceLastSave <- true
107 member this.RemoveSourceImage (srcImg: SourceImage) =
109 match this.CurrentImage with
110 | Some srcImg' -> srcImg = srcImg'
113 if sourceImages.Remove(srcImg)
115 alteredSinceLastSave <- true
118 this.CurrentImage <- if sourceImages.Count > 0 then Some sourceImages.[0] else None
119 // Re-numbered the images.
120 sourceImages |> Seq.iteri
(fun i
srcImg -> srcImg.num
<- i
+ 1)
122 member this.SetName (srcImg: SourceImage) (name
: string) =
123 if name
<> srcImg.name
126 alteredSinceLastSave <- true
128 member this.SetResult (imgNum
: int) (cells
: ParasitemiaCore.Types.Cell list) =
129 let sourceImage = sourceImages.Find(fun srcImg -> srcImg.num
= imgNum
)
131 let w = sourceImage.img
.Width
132 let h = sourceImage.img
.Height
134 sourceImage.dateLastAnalysis
<- DateTime.UtcNow
136 // To match with previously manually altered RBC.
137 let manuallyAlteredPreviousRBCS = sourceImage.rbcs
|> List.filter
(fun rbc -> rbc.setManually
)
138 let tolerance = (float sourceImage.config
.RBCRadius.Pixel) * 0.5 // +/-.
139 let getPreviousRBC (center
: Point) : RBC option =
140 manuallyAlteredPreviousRBCS |> List.tryFind
(fun rbc -> rbc.center
.X > center
.X - tolerance && rbc.center
.X < center
.X + tolerance &&
141 rbc.center
.Y > center
.Y - tolerance && rbc.center
.Y < center
.Y + tolerance)
143 sourceImage.rbcs
<- cells
144 |> List.filter
(fun cell
-> match cell
.cellClass
with ParasitemiaCore.Types.HealthyRBC | ParasitemiaCore.Types.InfectedRBC -> true | _ -> false )
145 |> List.sortByDescending
(fun cell
-> cell
.infectedArea
, (w - cell
.center
.X) + (h - cell
.center
.Y))
146 |> List.mapi
(fun i cell
->
147 let center = Point(float cell.center.X, float cell.center.Y)
148 let infected, setManually
=
149 match getPreviousRBC center with
150 | Some rbc -> rbc.infected, true
151 | _ -> cell.cellClass
= ParasitemiaCore.Types.InfectedRBC, false
155 setManually
= setManually
157 size
= Size(float cell.elements
.Width, float cell.elements
.Height)
158 infectedArea
= cell.infectedArea
})
160 alteredSinceLastSave <- true
162 member this.SourceImages : SourceImage seq =
163 sourceImages :> SourceImage seq
165 member this.Reset () =
168 this.CurrentImage <- None
170 alteredSinceLastSave <- false