Add a logger assembly and split the main assembly in two : the UI and the parasitemia...
[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.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 /// <summary>
48 /// Save the current state. 'FilePath' must have been defined.
49 /// </summary>
50 /// <exception cref="System.IOException">If the file cannot be saved</exception>
51 member this.Save () =
52 let data = { PiaZ.DocumentData.patientID = this.PatientID; PiaZ.DocumentData.images = List.ofSeq sourceImages }
53 PiaZ.save this.FilePath data
54 alteredSinceLastSave <- false
55
56 /// <summary>
57 /// Load the current state. 'FilePath' must have been defined.
58 /// </summary>
59 /// <exception cref="System.IOException">If the file cannot be laoded</exception>
60 member this.Load () =
61 let data = PiaZ.load this.FilePath
62 this.PatientID <- data.patientID
63 sourceImages.Clear()
64 sourceImages.InsertRange(0, data.images)
65 if sourceImages.Count > 0
66 then this.CurrentImage <- Some sourceImages.[0]
67 alteredSinceLastSave <- false
68
69 member this.AddSourceImage (filePath: string) (defaultConfig: ParasitemiaCore.Config.Config) : SourceImage =
70 let srcImg = { num = sourceImages.Count + 1; config = defaultConfig.Copy(); dateLastAnalysis = DateTime(0L); rbcs = []; img = new Image<Bgr, byte>(filePath) }
71 sourceImages.Add(srcImg)
72 if sourceImages.Count = 1
73 then this.CurrentImage <- Some sourceImages.[0]
74 alteredSinceLastSave <- true
75 srcImg
76
77 member this.RemoveSourceImage (srcImg: SourceImage) =
78 let isCurrent =
79 match this.CurrentImage with
80 | Some srcImg' -> srcImg = srcImg'
81 | _ -> false
82
83 if sourceImages.Remove(srcImg)
84 then
85 alteredSinceLastSave <- true
86 if isCurrent
87 then
88 this.CurrentImage <- if sourceImages.Count > 0 then Some sourceImages.[0] else None
89 // Re-numbered the images.
90 sourceImages |> Seq.iteri (fun i srcImg -> srcImg.num <- i + 1)
91
92 member this.SetResult (imgNum: int) (cells: ParasitemiaCore.Types.Cell list) =
93 let sourceImage = sourceImages.Find(fun srcImg -> srcImg.num = imgNum)
94
95 let w = sourceImage.img.Width
96 let h = sourceImage.img.Height
97
98 sourceImage.dateLastAnalysis <- DateTime.UtcNow
99
100 // To match with previously manually altered RBC.
101 let manuallyAlteredPreviousRBCS = sourceImage.rbcs |> List.filter (fun rbc -> rbc.setManually)
102 let tolerance = (float sourceImage.config.RBCRadius.Pixel) * 0.5 // +/-.
103 let getPreviousRBC (center: Point) : RBC option =
104 manuallyAlteredPreviousRBCS |> List.tryFind (fun rbc -> rbc.center.X > center.X - tolerance && rbc.center.X < center.X + tolerance &&
105 rbc.center.Y > center.Y - tolerance && rbc.center.Y < center.Y + tolerance)
106
107 sourceImage.rbcs <- cells
108 |> List.filter (fun cell -> match cell.cellClass with ParasitemiaCore.Types.HealthyRBC | ParasitemiaCore.Types.InfectedRBC -> true | _ -> false )
109 |> List.sortByDescending (fun cell -> cell.infectedArea, (w - cell.center.X) + (h - cell.center.Y))
110 |> List.mapi (fun i cell ->
111 let center = Point(float cell.center.X, float cell.center.Y)
112 let infected, setManually =
113 match getPreviousRBC center with
114 | Some rbc -> rbc.infected, true
115 | _ -> cell.cellClass = ParasitemiaCore.Types.InfectedRBC, false
116
117 { num = i + 1
118 infected = infected
119 setManually = setManually
120 center = center
121 size = Size(float cell.elements.Width, float cell.elements.Height)
122 infectedArea = cell.infectedArea })
123
124 alteredSinceLastSave <- true
125
126 member this.SourceImages : SourceImage seq =
127 sourceImages :> SourceImage seq
128
129 member this.Reset () =
130 this.PatientID <- ""
131 this.FilePath <- ""
132 this.CurrentImage <- None
133 sourceImages.Clear()
134 alteredSinceLastSave <- false