Add an about window.
[master-thesis.git] / Parasitemia / Parasitemia / GUI / State.fs
1 module Parasitemia.GUI.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.GlobalParasitemia : int * int =
34 sourceImages
35 |> Seq.fold (fun (nbTotal, nbTotalInfected) srcImg ->
36 let nb, nbInfected = this.ImageParasitemia srcImg
37 nbTotal + nb, nbTotalInfected + nbInfected) (0, 0)
38
39
40 member this.SetAsInfected (rbc: RBC) (infected: bool) =
41 if infected <> rbc.infected
42 then
43 alteredSinceLastSave <- true
44 rbc.infected <- infected
45 rbc.setManually <- not rbc.setManually
46
47 member this.Save () =
48 let data = { PiaZ.FileData.patientID = this.PatientID; PiaZ.FileData.images = List.ofSeq sourceImages }
49 PiaZ.save this.FilePath data
50 alteredSinceLastSave <- false
51
52 member this.Load () =
53 let data = PiaZ.load this.FilePath
54 this.PatientID <- data.patientID
55 sourceImages.Clear()
56 sourceImages.InsertRange(0, data.images)
57 if sourceImages.Count > 0
58 then this.CurrentImage <- Some sourceImages.[0]
59 alteredSinceLastSave <- false
60
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
67 srcImg
68
69 member this.RemoveSourceImage (srcImg: SourceImage) =
70 let isCurrent =
71 match this.CurrentImage with
72 | Some srcImg' -> srcImg = srcImg'
73 | _ -> false
74
75 if sourceImages.Remove(srcImg)
76 then
77 alteredSinceLastSave <- true
78 if isCurrent
79 then
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)
83
84 member this.SetResult (imgNum: int) (cells: Cell list) =
85 let sourceImage = sourceImages.Find(fun srcImg -> srcImg.num = imgNum)
86
87 let w = sourceImage.img.Width
88 let h = sourceImage.img.Height
89
90 sourceImage.dateLastAnalysis <- DateTime.UtcNow
91
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)
98
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
108
109 { num = i + 1
110 infected = infected
111 setManually = setManually
112 center = center
113 size = Size(float cell.elements.Width, float cell.elements.Height)
114 infectedArea = cell.infectedArea })
115
116 alteredSinceLastSave <- true
117
118 member this.SourceImages : SourceImage seq =
119 sourceImages :> SourceImage seq
120
121 member this.Reset () =
122 this.PatientID <- ""
123 this.FilePath <- ""
124 this.CurrentImage <- None
125 sourceImages.Clear()
126 alteredSinceLastSave <- false