69662d502fb71ab9124cdc7ce8eb68c60f971469
[master-thesis.git] / Parasitemia / ParasitemiaUI / State.fs
1 module ParasitemiaUI.State
2
3 open System
4 open System.Collections.Generic
5 open System.Windows
6
7 open Emgu.CV
8 open Emgu.CV.Structure
9
10 open Types
11
12 type State (defaultConfig: ParasitemiaCore.Config.Config) =
13 let sourceImages = List<SourceImage>()
14 let mutable alteredSinceLastSave = false
15 let mutable patientID = ""
16
17 member this.AlteredSinceLastSave = alteredSinceLastSave
18 member val CurrentImage: SourceImage option = None with get, set
19 member val FilePath: string = "" with get, set
20
21 member this.PatientID
22 with get () : string = patientID
23 and set id =
24 if id <> patientID
25 then
26 alteredSinceLastSave <- true
27 patientID <- id
28
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
32
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
36
37 member this.ImageNbManuallyChangedRBCStr (srcImg: SourceImage) (setAsInfected: bool) : string =
38 Utils.percentText (this.ImageNbManuallyChangedRBC srcImg setAsInfected)
39
40 member this.ImageManuallyChangedRBC (srcImg: SourceImage) (setAsInfected: bool) : int seq =
41 query {
42 for rbc in srcImg.rbcs do
43 where (rbc.setManually && rbc.infected = setAsInfected)
44 select rbc.num }
45
46 member this.ImageManuallyChangedRBCStr (srcImg: SourceImage) (setAsInfected: bool) : string =
47 let listStr = Utils.listAsStr <| this.ImageManuallyChangedRBC srcImg setAsInfected
48 if listStr = ""
49 then ""
50 else "[" + listStr + "]"
51
52 member this.GlobalParasitemia : int * int =
53 sourceImages
54 |> Seq.fold (fun (nbTotal, nbTotalInfected) srcImg ->
55 let nb, nbInfected = this.ImageParasitemia srcImg
56 nbTotal + nb, nbTotalInfected + nbInfected) (0, 0)
57
58
59 member this.SetAsInfected (rbc: RBC) (infected: bool) =
60 if infected <> rbc.infected
61 then
62 alteredSinceLastSave <- true
63 rbc.infected <- infected
64 rbc.setManually <- not rbc.setManually
65
66 /// <summary>
67 /// Save the current state. 'FilePath' must have been defined.
68 /// </summary>
69 /// <exception cref="System.IOException">If the file cannot be saved</exception>
70 member this.Save () =
71 let data = { PiaZ.DocumentData.patientID = this.PatientID; PiaZ.DocumentData.images = List.ofSeq sourceImages }
72 PiaZ.save this.FilePath data
73 alteredSinceLastSave <- false
74
75 /// <summary>
76 /// Load the current state. 'FilePath' must have been defined.
77 /// </summary>
78 /// <exception cref="System.IOException">If the file cannot be loaded</exception>
79 member this.Load () =
80 let data = PiaZ.load this.FilePath defaultConfig
81 this.PatientID <- data.patientID
82 sourceImages.Clear()
83 sourceImages.InsertRange(0, data.images)
84 this.CurrentImage <- if sourceImages.Count > 0 then Some sourceImages.[0] else None
85 alteredSinceLastSave <- false
86
87 /// <summary>
88 /// </summary>
89 /// <exception cref="System.IOException">If the image cannot be read</exception>
90 member this.AddSourceImage (filePath: string) (defaultConfig: ParasitemiaCore.Config.Config) : SourceImage =
91 let srcImg =
92 { num = sourceImages.Count + 1
93 name = System.IO.FileInfo(filePath).Name
94 config = defaultConfig.Copy()
95 dateLastAnalysis = DateTime(0L)
96 rbcs = []
97 img = new Image<Bgr, byte>(filePath)
98 healthyRBCBrightness = 1.f
99 infectedRBCBrightness = 1.f }
100
101 sourceImages.Add(srcImg)
102 if sourceImages.Count = 1
103 then this.CurrentImage <- Some sourceImages.[0]
104 alteredSinceLastSave <- true
105 srcImg
106
107 member this.RemoveSourceImage (srcImg: SourceImage) =
108 let isCurrent =
109 match this.CurrentImage with
110 | Some srcImg' -> srcImg = srcImg'
111 | _ -> false
112
113 if sourceImages.Remove(srcImg)
114 then
115 alteredSinceLastSave <- true
116 if isCurrent
117 then
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)
121
122 member this.SetName (srcImg: SourceImage) (name: string) =
123 if name <> srcImg.name
124 then
125 srcImg.name <- name
126 alteredSinceLastSave <- true
127
128 member this.SetResult (imgNum: int) (cells: ParasitemiaCore.Types.Cell list) =
129 let sourceImage = sourceImages.Find(fun srcImg -> srcImg.num = imgNum)
130
131 let w = sourceImage.img.Width
132 let h = sourceImage.img.Height
133
134 sourceImage.dateLastAnalysis <- DateTime.UtcNow
135
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 getPreviousManuallyAlteredRBC (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)
142
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.nucleusArea, (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 let infected = cell.cellClass = ParasitemiaCore.Types.InfectedRBC
150 match getPreviousManuallyAlteredRBC center with
151 | 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.
152 | _ -> infected, false
153
154 { num = i + 1
155 infected = infected
156 setManually = setManually
157 center = center
158 size = Size(float cell.elements.Width, float cell.elements.Height)
159 infectedArea = cell.nucleusArea })
160
161 alteredSinceLastSave <- true
162
163 member this.SourceImages : SourceImage seq =
164 sourceImages :> SourceImage seq
165
166 member this.Reset () =
167 this.PatientID <- ""
168 this.FilePath <- ""
169 this.CurrentImage <- None
170 sourceImages.Clear()
171 alteredSinceLastSave <- false