Update coding style.
[master-thesis.git] / Parasitemia / ParasitemiaUI / State.fs
index e15dc9b..3355a42 100644 (file)
@@ -9,56 +9,57 @@ open Emgu.CV.Structure
 
 open Types
 
-type State (defaultConfig: ParasitemiaCore.Config.Config) =
-    let sourceImages = List<SourceImage>()
+type State (defaultConfig : ParasitemiaCore.Config.Config) =
+    let sourceImages = List<SourceImage> ()
     let mutable alteredSinceLastSave = false
     let mutable patientID = ""
 
     member this.AlteredSinceLastSave = alteredSinceLastSave
-    member val CurrentImage: SourceImage option = None with get, set
-    member val FilePath: string = "" with get, set
+    member val CurrentImage : SourceImage option = None with get, set
+    member val FilePath : string = "" with get, set
 
     member this.PatientID
         with get () : string = patientID
         and set id =
-            if id <> patientID
-            then
+            if id <> patientID then
                 alteredSinceLastSave <- true
                 patientID <- id
 
-    member this.ImageParasitemia (srcImg: SourceImage) : int * int =
+    member this.ImageParasitemia (srcImg : SourceImage) : int * int =
         List.length srcImg.rbcs,
         srcImg.rbcs |> List.fold (fun nbInfected rbc -> if rbc.infected then nbInfected + 1 else nbInfected) 0
 
-    member this.ImageNbManuallyChangedRBC (srcImg: SourceImage) (setAsInfected: bool) : int * int =
+    member this.ImageNbManuallyChangedRBC (srcImg : SourceImage) (setAsInfected : bool) : int * int =
         List.length srcImg.rbcs,
         srcImg.rbcs |> List.fold (fun nb rbc -> if rbc.setManually && rbc.infected = setAsInfected then nb + 1 else nb) 0
 
-    member this.ImageNbManuallyChangedRBCStr (srcImg: SourceImage) (setAsInfected: bool) : string =
+    member this.ImageNbManuallyChangedRBCStr (srcImg : SourceImage) (setAsInfected : bool) : string =
         Utils.percentText (this.ImageNbManuallyChangedRBC srcImg setAsInfected)
 
-    member this.ImageManuallyChangedRBC (srcImg: SourceImage) (setAsInfected: bool) : int seq =
+    member this.ImageManuallyChangedRBC (srcImg : SourceImage) (setAsInfected : bool) : int seq =
         query {
             for rbc in srcImg.rbcs do
             where (rbc.setManually && rbc.infected = setAsInfected)
-            select rbc.num }
+            select rbc.num
+        }
 
-    member this.ImageManuallyChangedRBCStr (srcImg: SourceImage) (setAsInfected: bool) : string =
+    member this.ImageManuallyChangedRBCStr (srcImg : SourceImage) (setAsInfected : bool) : string =
         let listStr = Utils.listAsStr <| this.ImageManuallyChangedRBC srcImg setAsInfected
-        if listStr = ""
-        then ""
-        else "[" + listStr + "]"
+        if listStr = "" then
+            ""
+        else
+            "[" + listStr + "]"
 
     member this.GlobalParasitemia : int * int =
         sourceImages
-        |> Seq.fold (fun (nbTotal, nbTotalInfected) srcImg ->
-                        let nb, nbInfected = this.ImageParasitemia srcImg
-                        nbTotal + nb, nbTotalInfected + nbInfected) (0, 0)
-
-
-    member this.SetAsInfected (rbc: RBC) (infected: bool) =
-        if infected <> rbc.infected
-        then
+        |> Seq.fold (
+            fun (nbTotal, nbTotalInfected) srcImg ->
+                let nb, nbInfected = this.ImageParasitemia srcImg
+                nbTotal + nb, nbTotalInfected + nbInfected
+        ) (0, 0)
+
+    member this.SetAsInfected (rbc : RBC) (infected : bool) =
+        if infected <> rbc.infected then
             alteredSinceLastSave <- true
             rbc.infected <- infected
             rbc.setManually <- not rbc.setManually
@@ -79,54 +80,53 @@ type State (defaultConfig: ParasitemiaCore.Config.Config) =
     member this.Load () =
         let data = PiaZ.load this.FilePath defaultConfig
         this.PatientID <- data.patientID
-        sourceImages.Clear()
-        sourceImages.InsertRange(0, data.images)
+        sourceImages.Clear ()
+        sourceImages.InsertRange (0, data.images)
         this.CurrentImage <- if sourceImages.Count > 0 then Some sourceImages.[0] else None
         alteredSinceLastSave <- false
 
     /// <summary>
     /// </summary>
     /// <exception cref="System.IOException">If the image cannot be read</exception>
-    member this.AddSourceImage (filePath: string) (defaultConfig: ParasitemiaCore.Config.Config) : SourceImage =
+    member this.AddSourceImage (filePath : string) (defaultConfig : ParasitemiaCore.Config.Config) : SourceImage =
         let srcImg =
-            { num = sourceImages.Count + 1
-              name = System.IO.FileInfo(filePath).Name
-              config = defaultConfig.Copy()
-              dateLastAnalysis = DateTime(0L)
-              rbcs = []
-              img = new Image<Bgr, byte>(filePath)
-              healthyRBCBrightness = 1.f
-              infectedRBCBrightness = 1.f }
-
-        sourceImages.Add(srcImg)
-        if sourceImages.Count = 1
-        then this.CurrentImage <- Some sourceImages.[0]
+            {
+                num = sourceImages.Count + 1
+                name = System.IO.FileInfo(filePath).Name
+                config = defaultConfig.Copy ()
+                dateLastAnalysis = DateTime (0L)
+                rbcs = []
+                img = new Image<Bgr, byte> (filePath)
+                healthyRBCBrightness = 1.f
+                infectedRBCBrightness = 1.f
+            }
+
+        sourceImages.Add srcImg
+        if sourceImages.Count = 1 then
+            this.CurrentImage <- Some sourceImages.[0]
         alteredSinceLastSave <- true
         srcImg
 
-    member this.RemoveSourceImage (srcImg: SourceImage) =
+    member this.RemoveSourceImage (srcImg : SourceImage) =
         let isCurrent =
             match this.CurrentImage with
             | Some srcImg' -> srcImg = srcImg'
             | _ -> false
 
-        if sourceImages.Remove(srcImg)
-        then
+        if sourceImages.Remove srcImg then
             alteredSinceLastSave <- true
-            if isCurrent
-            then
+            if isCurrent then
                 this.CurrentImage <- if sourceImages.Count > 0 then Some sourceImages.[0] else None
         // Re-numbered the images.
         sourceImages |> Seq.iteri (fun i srcImg -> srcImg.num <- i + 1)
 
-    member this.SetName (srcImg: SourceImage) (name: string) =
-        if name <> srcImg.name
-        then
+    member this.SetName (srcImg : SourceImage) (name : string) =
+        if name <> srcImg.name then
             srcImg.name <- name
             alteredSinceLastSave <- true
 
-    member this.SetResult (imgNum: int) (cells: ParasitemiaCore.Types.Cell list) =
-        let sourceImage = sourceImages.Find(fun srcImg -> srcImg.num = imgNum)
+    member this.SetResult (imgNum : int) (cells : ParasitemiaCore.Types.Cell list) =
+        let sourceImage = sourceImages.Find (fun srcImg -> srcImg.num = imgNum)
 
         let w = sourceImage.img.Width
         let h = sourceImage.img.Height
@@ -136,26 +136,37 @@ type State (defaultConfig: ParasitemiaCore.Config.Config) =
         // To match with previously manually altered RBC.
         let manuallyAlteredPreviousRBCS = sourceImage.rbcs |> List.filter (fun rbc -> rbc.setManually)
         let tolerance = (float sourceImage.config.RBCRadius.Pixel) * 0.5 // +/-.
-        let getPreviousRBC (center: Point) : RBC option =
-            manuallyAlteredPreviousRBCS |> List.tryFind (fun rbc -> rbc.center.X > center.X - tolerance && rbc.center.X < center.X + tolerance &&
-                                                                    rbc.center.Y > center.Y - tolerance && rbc.center.Y < center.Y + tolerance)
-
-        sourceImage.rbcs <- cells
+        let getPreviousManuallyAlteredRBC (center : Point) : RBC option =
+            manuallyAlteredPreviousRBCS
+            |> List.tryFind (
+                fun rbc ->
+                    rbc.center.X > center.X - tolerance &&
+                    rbc.center.X < center.X + tolerance &&
+                    rbc.center.Y > center.Y - tolerance &&
+                    rbc.center.Y < center.Y + tolerance
+            )
+
+        sourceImage.rbcs <-
+            cells
             |> List.filter (fun cell -> match cell.cellClass with ParasitemiaCore.Types.HealthyRBC | ParasitemiaCore.Types.InfectedRBC -> true | _ -> false )
-            |> List.sortByDescending (fun cell -> cell.infectedArea, (w - cell.center.X) + (h - cell.center.Y))
-            |> List.mapi (fun i cell ->
-                let center = Point(float cell.center.X, float cell.center.Y)
-                let infected, setManually =
-                    match getPreviousRBC center with
-                    | Some rbc -> rbc.infected, true
-                    | _ -> cell.cellClass = ParasitemiaCore.Types.InfectedRBC, false
-
-                { num = i + 1
-                  infected = infected
-                  setManually = setManually
-                  center = center
-                  size = Size(float cell.elements.Width, float cell.elements.Height)
-                  infectedArea = cell.infectedArea })
+            |> List.sortByDescending (fun cell -> cell.nucleusArea, (w - cell.center.X) + (h - cell.center.Y))
+            |> List.mapi (
+                fun i cell ->
+                    let center = Point (float cell.center.X, float cell.center.Y)
+                    let infected, setManually =
+                        let infected = cell.cellClass = ParasitemiaCore.Types.InfectedRBC
+                        match getPreviousManuallyAlteredRBC center with
+                        | 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.
+                        | _ -> infected, false
+                    {
+                        num = i + 1
+                        infected = infected
+                        setManually = setManually
+                        center = center
+                        size = Size (float cell.elements.Width, float cell.elements.Height)
+                        infectedArea = cell.nucleusArea
+                    }
+            )
 
         alteredSinceLastSave <- true
 
@@ -166,5 +177,5 @@ type State (defaultConfig: ParasitemiaCore.Config.Config) =
         this.PatientID <- ""
         this.FilePath <- ""
         this.CurrentImage <- None
-        sourceImages.Clear()
+        sourceImages.Clear ()
         alteredSinceLastSave <- false
\ No newline at end of file