f1336402a4945f4b819401e575dbdd2b5c308eff
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 =
76 let srcImg = { num
= sourceImages.Count + 1; config
= defaultConfig
.Copy(); dateLastAnalysis
= DateTime(0L); rbcs
= []; img
= new Image<Bgr, byte
>(filePath
) }
77 sourceImages.Add(srcImg)
78 if sourceImages.Count = 1
79 then this.CurrentImage <- Some sourceImages.[0]
80 alteredSinceLastSave <- true
83 member this.RemoveSourceImage (srcImg: SourceImage) =
85 match this.CurrentImage with
86 | Some srcImg' -> srcImg = srcImg'
89 if sourceImages.Remove(srcImg)
91 alteredSinceLastSave <- true
94 this.CurrentImage <- if sourceImages.Count > 0 then Some sourceImages.[0] else None
95 // Re-numbered the images.
96 sourceImages |> Seq.iteri
(fun i
srcImg -> srcImg.num
<- i
+ 1)
98 member this.SetResult (imgNum
: int) (cells
: ParasitemiaCore.Types.Cell list) =
99 let sourceImage = sourceImages.Find(fun srcImg -> srcImg.num
= imgNum
)
101 let w = sourceImage.img
.Width
102 let h = sourceImage.img
.Height
104 sourceImage.dateLastAnalysis
<- DateTime.UtcNow
106 // To match with previously manually altered RBC.
107 let manuallyAlteredPreviousRBCS = sourceImage.rbcs
|> List.filter
(fun rbc -> rbc.setManually
)
108 let tolerance = (float sourceImage.config
.RBCRadius.Pixel) * 0.5 // +/-.
109 let getPreviousRBC (center
: Point) : RBC option =
110 manuallyAlteredPreviousRBCS |> List.tryFind
(fun rbc -> rbc.center
.X > center
.X - tolerance && rbc.center
.X < center
.X + tolerance &&
111 rbc.center
.Y > center
.Y - tolerance && rbc.center
.Y < center
.Y + tolerance)
113 sourceImage.rbcs
<- cells
114 |> List.filter
(fun cell
-> match cell
.cellClass
with ParasitemiaCore.Types.HealthyRBC | ParasitemiaCore.Types.InfectedRBC -> true | _ -> false )
115 |> List.sortByDescending
(fun cell
-> cell
.infectedArea
, (w - cell
.center
.X) + (h - cell
.center
.Y))
116 |> List.mapi
(fun i cell
->
117 let center = Point(float cell.center.X, float cell.center.Y)
118 let infected, setManually
=
119 match getPreviousRBC center with
120 | Some rbc -> rbc.infected, true
121 | _ -> cell.cellClass
= ParasitemiaCore.Types.InfectedRBC, false
125 setManually
= setManually
127 size
= Size(float cell.elements
.Width, float cell.elements
.Height)
128 infectedArea
= cell.infectedArea
})
130 alteredSinceLastSave <- true
132 member this.SourceImages : SourceImage seq =
133 sourceImages :> SourceImage seq
135 member this.Reset () =
138 this.CurrentImage <- None
140 alteredSinceLastSave <- false