Save imported image in the same format (WIP)
[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 then
25 alteredSinceLastSave <- true
26 patientID <- id
27
28 member this.GlobalParasitemia : int * int =
29 sourceImages
30 |> Seq.fold (
31 fun (nbTotal, nbTotalInfected) srcImg ->
32 let nb, nbInfected = srcImg.ImageParasitemia
33 nbTotal + nb, nbTotalInfected + nbInfected
34 ) (0, 0)
35
36 member this.SetAsInfected (rbc : RBC) (infected : bool) =
37 if infected <> rbc.infected then
38 alteredSinceLastSave <- true
39 rbc.infected <- infected
40 rbc.setManually <- not rbc.setManually
41
42 /// <summary>
43 /// Save the current state. 'FilePath' must have been defined.
44 /// </summary>
45 /// <exception cref="System.IOException">If the file cannot be saved</exception>
46 member this.Save () =
47 let data = { PiaZ.DocumentData.patientID = this.PatientID; PiaZ.DocumentData.images = List.ofSeq sourceImages }
48 PiaZ.save this.FilePath data
49 alteredSinceLastSave <- false
50
51 /// <summary>
52 /// Load the current state. 'FilePath' must have been defined.
53 /// </summary>
54 /// <exception cref="System.IOException">If the file cannot be loaded</exception>
55 member this.Load () =
56 let data = PiaZ.load this.FilePath defaultConfig
57 this.PatientID <- data.patientID
58 sourceImages.Clear ()
59 sourceImages.InsertRange (0, data.images)
60 this.CurrentImage <- if sourceImages.Count > 0 then Some sourceImages.[0] else None
61 alteredSinceLastSave <- false
62
63 /// <summary>
64 /// </summary>
65 /// <exception cref="System.IOException">If the image cannot be read</exception>
66 member this.AddSourceImage (filePath : string) (defaultConfig : ParasitemiaCore.Config.Config) : SourceImage =
67 let filename = System.IO.FileInfo(filePath).Name
68 let srcImg = SourceImage (sourceImages.Count + 1, filename, filename, defaultConfig.Copy (), DateTime (0L), FromFile filePath, [])
69 sourceImages.Add srcImg
70 if sourceImages.Count = 1 then
71 this.CurrentImage <- Some sourceImages.[0]
72 alteredSinceLastSave <- true
73 srcImg
74
75 member this.RemoveSourceImage (srcImg : SourceImage) =
76 let isCurrent =
77 match this.CurrentImage with
78 | Some srcImg' -> srcImg = srcImg'
79 | _ -> false
80
81 if sourceImages.Remove srcImg then
82 alteredSinceLastSave <- true
83 if isCurrent then
84 this.CurrentImage <- if sourceImages.Count > 0 then Some sourceImages.[0] else None
85 // Re-numbered the images.
86 sourceImages |> Seq.iteri (fun i srcImg -> srcImg.Num <- i + 1)
87
88 member this.SetName (srcImg : SourceImage) (name : string) =
89 if name <> srcImg.Name then
90 srcImg.Name <- name
91 alteredSinceLastSave <- true
92
93 member this.SetResult (imgId : string) (result : ParasitemiaCore.Types.AnalysisResult) =
94 let sourceImage = sourceImages.Find (fun srcImg -> srcImg.RomanNum = imgId)
95
96 let w = sourceImage.Img.Width
97 let h = sourceImage.Img.Height
98
99 sourceImage.DateLastAnalysis <- DateTime.UtcNow
100
101 // To match with previously manually altered RBC.
102 let manuallyAlteredPreviousRBCS = sourceImage.RBCs |> List.filter (fun rbc -> rbc.setManually)
103 let tolerance = (float sourceImage.Config.RBCRadius.Pixel) * 0.5 // +/-.
104 let getPreviousManuallyAlteredRBC (center : Point) : RBC option =
105 manuallyAlteredPreviousRBCS
106 |> List.tryFind (
107 fun rbc ->
108 rbc.center.X > center.X - tolerance &&
109 rbc.center.X < center.X + tolerance &&
110 rbc.center.Y > center.Y - tolerance &&
111 rbc.center.Y < center.Y + tolerance
112 )
113
114 sourceImage.RBCs <-
115 result.Cells
116 |> List.filter (fun cell -> match cell.cellClass with ParasitemiaCore.Types.HealthyRBC | ParasitemiaCore.Types.InfectedRBC -> true | _ -> false )
117 |> List.sortByDescending (fun cell -> cell.nucleusArea, (w - cell.center.X) + (h - cell.center.Y))
118 |> List.mapi (
119 fun i cell ->
120 let center = Point (float cell.center.X, float cell.center.Y)
121 let infected, setManually =
122 let infected = cell.cellClass = ParasitemiaCore.Types.InfectedRBC
123 match getPreviousManuallyAlteredRBC center with
124 | 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.
125 | _ -> infected, false
126 {
127 num = i + 1
128 infected = infected
129 setManually = setManually
130 center = center
131 size = Size (float cell.elements.Width, float cell.elements.Height)
132 infectedArea = cell.nucleusArea
133 }
134 )
135
136 alteredSinceLastSave <- true
137
138 member this.SourceImages : SourceImage seq =
139 sourceImages :> SourceImage seq
140
141 member this.Reset () =
142 this.PatientID <- ""
143 this.FilePath <- ""
144 this.CurrentImage <- None
145 sourceImages.Clear ()
146 alteredSinceLastSave <- false