1
module ParasitemiaUI.State
4 open System.Collections.Generic
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
.ImageNbAltered (srcImg
: SourceImage) : int * int =
34 List.length srcImg
.rbcs
,
35 srcImg
.rbcs
|> List.fold
(fun nbAltered rbc
-> if rbc
.setManually
then nbAltered
+ 1 else nbAltered) 0
37 member this
.GlobalParasitemia : int * int =
39 |> Seq.fold
(fun (nbTotal
, nbTotalInfected
) srcImg
->
40 let nb, nbInfected = this
.ImageParasitemia srcImg
41 nbTotal
+ nb, nbTotalInfected
+ nbInfected) (0, 0)
44 member this
.SetAsInfected (rbc
: RBC) (infected
: bool) =
45 if infected
<> rbc
.infected
47 alteredSinceLastSave <- true
48 rbc
.infected
<- infected
49 rbc
.setManually
<- not
rbc.setManually
52 /// Save the current state. 'FilePath' must have been defined.
54 /// <exception cref="System.IOException">If the file cannot be saved</exception>
56 let data = { PiaZ.DocumentData.patientID = this
.PatientID; PiaZ.DocumentData.images
= List.ofSeq
sourceImages }
57 PiaZ.save
this.FilePath data
58 alteredSinceLastSave <- false
61 /// Load the current state. 'FilePath' must have been defined.
63 /// <exception cref="System.IOException">If the file cannot be loaded</exception>
65 let data = PiaZ.load
this.FilePath
66 this.PatientID <- data.patientID
68 sourceImages.InsertRange(0, data.images
)
69 this.CurrentImage <- if sourceImages.Count > 0 then Some sourceImages.[0] else None
70 alteredSinceLastSave <- false
74 /// <exception cref="System.IOException">If the image cannot be read</exception>
75 member this.AddSourceImage (filePath
: string) (defaultConfig
: ParasitemiaCore.Config.Config) : SourceImage =
77 { num
= sourceImages.Count + 1
78 config
= defaultConfig
.Copy()
79 dateLastAnalysis
= DateTime(0L)
81 img
= new Image<Bgr, byte
>(filePath
)
82 healthyRBCBrightness
= 1.f
83 infectedRBCBrightness
= 1.f
}
85 sourceImages.Add(srcImg)
86 if sourceImages.Count = 1
87 then this.CurrentImage <- Some sourceImages.[0]
88 alteredSinceLastSave <- true
91 member this.RemoveSourceImage (srcImg: SourceImage) =
93 match this.CurrentImage with
94 | Some srcImg' -> srcImg = srcImg'
97 if sourceImages.Remove(srcImg)
99 alteredSinceLastSave <- true
102 this.CurrentImage <- if sourceImages.Count > 0 then Some sourceImages.[0] else None
103 // Re-numbered the images.
104 sourceImages |> Seq.iteri
(fun i
srcImg -> srcImg.num
<- i
+ 1)
106 member this.SetResult (imgNum
: int) (cells
: ParasitemiaCore.Types.Cell list) =
107 let sourceImage = sourceImages.Find(fun srcImg -> srcImg.num
= imgNum
)
109 let w = sourceImage.img
.Width
110 let h = sourceImage.img
.Height
112 sourceImage.dateLastAnalysis
<- DateTime.UtcNow
114 // To match with previously manually altered RBC.
115 let manuallyAlteredPreviousRBCS = sourceImage.rbcs
|> List.filter
(fun rbc -> rbc.setManually
)
116 let tolerance = (float sourceImage.config
.RBCRadius.Pixel) * 0.5 // +/-.
117 let getPreviousRBC (center
: Point) : RBC option =
118 manuallyAlteredPreviousRBCS |> List.tryFind
(fun rbc -> rbc.center
.X > center
.X - tolerance && rbc.center
.X < center
.X + tolerance &&
119 rbc.center
.Y > center
.Y - tolerance && rbc.center
.Y < center
.Y + tolerance)
121 sourceImage.rbcs
<- cells
122 |> List.filter
(fun cell
-> match cell
.cellClass
with ParasitemiaCore.Types.HealthyRBC | ParasitemiaCore.Types.InfectedRBC -> true | _ -> false )
123 |> List.sortByDescending
(fun cell
-> cell
.infectedArea
, (w - cell
.center
.X) + (h - cell
.center
.Y))
124 |> List.mapi
(fun i cell
->
125 let center = Point(float cell.center.X, float cell.center.Y)
126 let infected, setManually
=
127 match getPreviousRBC center with
128 | Some rbc -> rbc.infected, true
129 | _ -> cell.cellClass
= ParasitemiaCore.Types.InfectedRBC, false
133 setManually
= setManually
135 size
= Size(float cell.elements
.Width, float cell.elements
.Height)
136 infectedArea
= cell.infectedArea
})
138 alteredSinceLastSave <- true
140 member this.SourceImages : SourceImage seq =
141 sourceImages :> SourceImage seq
143 member this.Reset () =
146 this.CurrentImage <- None
148 alteredSinceLastSave <- false