f1336402a4945f4b819401e575dbdd2b5c308eff
[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 () =
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.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
36
37 member this.GlobalParasitemia : int * int =
38 sourceImages
39 |> Seq.fold (fun (nbTotal, nbTotalInfected) srcImg ->
40 let nb, nbInfected = this.ImageParasitemia srcImg
41 nbTotal + nb, nbTotalInfected + nbInfected) (0, 0)
42
43
44 member this.SetAsInfected (rbc: RBC) (infected: bool) =
45 if infected <> rbc.infected
46 then
47 alteredSinceLastSave <- true
48 rbc.infected <- infected
49 rbc.setManually <- not rbc.setManually
50
51 /// <summary>
52 /// Save the current state. 'FilePath' must have been defined.
53 /// </summary>
54 /// <exception cref="System.IOException">If the file cannot be saved</exception>
55 member this.Save () =
56 let data = { PiaZ.DocumentData.patientID = this.PatientID; PiaZ.DocumentData.images = List.ofSeq sourceImages }
57 PiaZ.save this.FilePath data
58 alteredSinceLastSave <- false
59
60 /// <summary>
61 /// Load the current state. 'FilePath' must have been defined.
62 /// </summary>
63 /// <exception cref="System.IOException">If the file cannot be loaded</exception>
64 member this.Load () =
65 let data = PiaZ.load this.FilePath
66 this.PatientID <- data.patientID
67 sourceImages.Clear()
68 sourceImages.InsertRange(0, data.images)
69 this.CurrentImage <- if sourceImages.Count > 0 then Some sourceImages.[0] else None
70 alteredSinceLastSave <- false
71
72 /// <summary>
73 /// </summary>
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
81 srcImg
82
83 member this.RemoveSourceImage (srcImg: SourceImage) =
84 let isCurrent =
85 match this.CurrentImage with
86 | Some srcImg' -> srcImg = srcImg'
87 | _ -> false
88
89 if sourceImages.Remove(srcImg)
90 then
91 alteredSinceLastSave <- true
92 if isCurrent
93 then
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)
97
98 member this.SetResult (imgNum: int) (cells: ParasitemiaCore.Types.Cell list) =
99 let sourceImage = sourceImages.Find(fun srcImg -> srcImg.num = imgNum)
100
101 let w = sourceImage.img.Width
102 let h = sourceImage.img.Height
103
104 sourceImage.dateLastAnalysis <- DateTime.UtcNow
105
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)
112
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
122
123 { num = i + 1
124 infected = infected
125 setManually = setManually
126 center = center
127 size = Size(float cell.elements.Width, float cell.elements.Height)
128 infectedArea = cell.infectedArea })
129
130 alteredSinceLastSave <- true
131
132 member this.SourceImages : SourceImage seq =
133 sourceImages :> SourceImage seq
134
135 member this.Reset () =
136 this.PatientID <- ""
137 this.FilePath <- ""
138 this.CurrentImage <- None
139 sourceImages.Clear()
140 alteredSinceLastSave <- false