1
module Parasitemia.GUI.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
.GlobalParasitemia : int * int =
35 |> Seq.fold
(fun (nbTotal
, nbTotalInfected
) srcImg
->
36 let nb, nbInfected = this
.ImageParasitemia srcImg
37 nbTotal
+ nb, nbTotalInfected
+ nbInfected) (0, 0)
40 member this
.SetAsInfected (rbc
: RBC) (infected
: bool) =
41 if infected
<> rbc
.infected
43 alteredSinceLastSave <- true
44 rbc
.infected
<- infected
45 rbc
.setManually
<- not
rbc.setManually
48 let data = { PiaZ.FileData.patientID = this
.PatientID; PiaZ.FileData.images
= List.ofSeq
sourceImages }
49 PiaZ.save
this.FilePath data
50 alteredSinceLastSave <- false
53 let data = PiaZ.load
this.FilePath
54 this.PatientID <- data.patientID
56 sourceImages.InsertRange(0, data.images
)
57 if sourceImages.Count > 0
58 then this.CurrentImage <- Some sourceImages.[0]
59 alteredSinceLastSave <- false
61 member this.AddSourceImage (filePath
: string) (defaultConfig
: Config.Config) : SourceImage =
62 let srcImg = { num
= sourceImages.Count + 1; config
= defaultConfig
.Copy(); dateLastAnalysis
= DateTime(0L); rbcs
= []; img
= new Image<Bgr, byte
>(filePath
) }
63 sourceImages.Add(srcImg)
64 if sourceImages.Count = 1
65 then this.CurrentImage <- Some sourceImages.[0]
66 alteredSinceLastSave <- true
69 member this.RemoveSourceImage (srcImg: SourceImage) =
71 match this.CurrentImage with
72 | Some srcImg' -> srcImg = srcImg'
75 if sourceImages.Remove(srcImg)
77 alteredSinceLastSave <- true
80 this.CurrentImage <- if sourceImages.Count > 0 then Some sourceImages.[0] else None
81 // Re-numbered the images.
82 sourceImages |> Seq.iteri
(fun i
srcImg -> srcImg.num
<- i
+ 1)
84 member this.SetResult (imgNum
: int) (cells
: Cell list) =
85 let sourceImage = sourceImages.Find(fun srcImg -> srcImg.num
= imgNum
)
87 let w = sourceImage.img
.Width
88 let h = sourceImage.img
.Height
90 sourceImage.dateLastAnalysis
<- DateTime.UtcNow
92 // To match with previously manually altered RBC.
93 let manuallyAlteredPreviousRBCS = sourceImage.rbcs
|> List.filter
(fun rbc -> rbc.setManually
)
94 let tolerance = (float sourceImage.config
.RBCRadius.Pixel) * 0.5 // +/-.
95 let getPreviousRBC (center
: Point) : RBC option =
96 manuallyAlteredPreviousRBCS |> List.tryFind
(fun rbc -> rbc.center
.X > center
.X - tolerance && rbc.center
.X < center
.X + tolerance &&
97 rbc.center
.Y > center
.Y - tolerance && rbc.center
.Y < center
.Y + tolerance)
99 sourceImage.rbcs
<- cells
100 |> List.filter
(fun cell
-> match cell
.cellClass
with HealthyRBC | InfectedRBC -> true | _ -> false )
101 |> List.sortByDescending
(fun cell
-> cell
.infectedArea
, (w - cell
.center
.X) + (h - cell
.center
.Y))
102 |> List.mapi
(fun i cell
->
103 let center = Point(float cell.center.X, float cell.center.Y)
104 let infected, setManually
=
105 match getPreviousRBC center with
106 | Some rbc -> rbc.infected, true
107 | _ -> cell.cellClass
= InfectedRBC, false
111 setManually
= setManually
113 size
= Size(float cell.elements
.Width, float cell.elements
.Height)
114 infectedArea
= cell.infectedArea
})
116 alteredSinceLastSave <- true
118 member this.SourceImages : SourceImage seq =
119 sourceImages :> SourceImage seq
121 member this.Reset () =
124 this.CurrentImage <- None
126 alteredSinceLastSave <- false