From 6c70577f0bcc17881c753f7a527d2ff4672bb6aa Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Wed, 20 Jan 2016 16:47:30 +0100 Subject: [PATCH] Use a maybe monad to be able abort an analysis. --- Parasitemia/Logger/Logger.fs | 6 +- Parasitemia/ParasitemiaCore/MainAnalysis.fs | 189 ++++++++++-------- .../ParasitemiaCore/ParasitesMarker.fs | 4 +- Parasitemia/ParasitemiaCore/Types.fs | 25 +++ Parasitemia/ParasitemiaUI/Analysis.fs | 14 +- Parasitemia/ParasitemiaUI/GUI.fs | 4 + Parasitemia/ParasitemiaUI/Program.fs | 15 +- .../ParasitemiaUI/XAML/MainWindow.xaml | 2 +- 8 files changed, 159 insertions(+), 100 deletions(-) diff --git a/Parasitemia/Logger/Logger.fs b/Parasitemia/Logger/Logger.fs index 552e37d..1f029d5 100644 --- a/Parasitemia/Logger/Logger.fs +++ b/Parasitemia/Logger/Logger.fs @@ -139,12 +139,14 @@ type Log () = static member AddListener (listener: IListener) = instance.AddListener(listener) static member RmListener (listener: IListener) = instance.RmListener(listener) - static member LogWithTime (message: string, severity: Severity, f: unit -> 'a, [] args: Object[]) : 'a = + static member LogWithTime (message: string, severity: Severity, f: unit -> 'a option, [] args: Object[]) : 'a option = let sw = Stopwatch() sw.Start() let res = f () sw.Stop() - instance.Write(String.Format(message, args) + sprintf " (time: %d ms)" sw.ElapsedMilliseconds, severity) + if res.IsSome + then + instance.Write(String.Format(message, args) + sprintf " (time: %d ms)" sw.ElapsedMilliseconds, severity) res static member Debug (message: string, [] args: Object[]) = diff --git a/Parasitemia/ParasitemiaCore/MainAnalysis.fs b/Parasitemia/ParasitemiaCore/MainAnalysis.fs index 0f368d8..be72a2b 100644 --- a/Parasitemia/ParasitemiaCore/MainAnalysis.fs +++ b/Parasitemia/ParasitemiaCore/MainAnalysis.fs @@ -16,123 +16,135 @@ open ImgTools open Config open Types -let doAnalysis (img: Image) (name: string) (config: Config) (reportProgress: (int -> unit) option) : Cell list = +let doAnalysis (img: Image) (name: string) (config: Config) (reportProgress: (int -> bool) option) : Cell list option = // To report the progress of this function from 0 to 100. - let inline report (percent: int) = + let reportWithVal (percent: int) (value: 'a) : 'a option = match reportProgress with - | Some f -> f percent - | _ -> () + | Some f -> + if f percent + then Some value + else None + | _ -> Some value + + let report (percent: int) : unit option = + reportWithVal percent () let inline buildLogWithName (text: string) = sprintf "(%s) %s" name text let logWithName mess = Log.User(buildLogWithName mess) - let inline logTimeWithName (text: string) (f: unit -> 'a) : 'a = Log.LogWithTime((buildLogWithName text), Severity.USER, f) - - logWithName "Starting analysis ..." + let inline logTimeWithName (text: string) (f: unit -> 'a option) : 'a option = Log.LogWithTime((buildLogWithName text), Severity.USER, f) - use green = img.Item(1) - let greenFloat = green.Convert() - let filteredGreen = gaussianFilter greenFloat config.LPFStandardDeviation + maybe { + do! report 0 - logWithName (sprintf "Nominal erytrocyte diameter: %A" config.RBCRadiusByResolution) + logWithName "Starting analysis ..." - let initialAreaOpening = int <| config.RBCRadiusByResolution.Area * config.Parameters.ratioAreaPaleCenter * 1.2f // We do an area opening a little larger to avoid to do a second one in the case the radius found is near the initial one. - logTimeWithName "First area opening" (fun () -> ImgTools.areaOpenF filteredGreen initialAreaOpening) + use green = img.Item(1) + let greenFloat = green.Convert() + let filteredGreen = gaussianFilter greenFloat config.LPFStandardDeviation - report 10 + logWithName (sprintf "Nominal erytrocyte diameter: %A" config.RBCRadiusByResolution) - let range = - let delta = config.Parameters.granulometryRange * config.RBCRadiusByResolution.Pixel - int <| config.RBCRadiusByResolution.Pixel - delta, int <| config.RBCRadiusByResolution.Pixel + delta - //let r1 = log "Granulometry (morpho)" (fun() -> Granulometry.findRadiusByClosing (filteredGreen.Convert()) range 1.0 |> float32) - config.SetRBCRadius <| logTimeWithName "Granulometry (area)" (fun() -> Granulometry.findRadiusByAreaClosing filteredGreen range |> float32) + let initialAreaOpening = int <| config.RBCRadiusByResolution.Area * config.Parameters.ratioAreaPaleCenter * 1.2f // We do an area opening a little larger to avoid to do a second one in the case the radius found is near the initial one. + do! logTimeWithName "First area opening" (fun () -> ImgTools.areaOpenF filteredGreen initialAreaOpening; report 10) - logWithName (sprintf "Found erytrocyte diameter: %A" config.RBCRadius) + //do! report 10 - report 20 + let range = + let delta = config.Parameters.granulometryRange * config.RBCRadiusByResolution.Pixel + int <| config.RBCRadiusByResolution.Pixel - delta, int <| config.RBCRadiusByResolution.Pixel + delta + //let r1 = log "Granulometry (morpho)" (fun() -> Granulometry.findRadiusByClosing (filteredGreen.Convert()) range 1.0 |> float32) + let! radius = logTimeWithName "Granulometry (area)" (fun() -> reportWithVal 10 (Granulometry.findRadiusByAreaClosing filteredGreen range |> float32)) + config.SetRBCRadius <| radius - let secondAreaOpening = int <| config.RBCRadius.Area * config.Parameters.ratioAreaPaleCenter - if secondAreaOpening > initialAreaOpening - then - logTimeWithName "Second area opening" (fun () -> ImgTools.areaOpenF filteredGreen secondAreaOpening) + logWithName (sprintf "Found erytrocyte diameter: %A" config.RBCRadius) - let parasites, filteredGreenWhitoutStain = ParasitesMarker.find filteredGreen config - //let parasites, filteredGreenWhitoutInfection, filteredGreenWhitoutStain = ParasitesMarker.findMa greenFloat filteredGreenFloat config + do! report 20 - let edges, xGradient, yGradient = logTimeWithName "Finding edges" (fun () -> - let edges, xGradient, yGradient = ImgTools.findEdges filteredGreenWhitoutStain - removeArea edges (config.RBCRadius.Pixel ** 2.f / 50.f |> int) - edges, xGradient, yGradient) + do! + let secondAreaOpening = int <| config.RBCRadius.Area * config.Parameters.ratioAreaPaleCenter + if secondAreaOpening > initialAreaOpening + then + logTimeWithName "Second area opening" (fun () -> ImgTools.areaOpenF filteredGreen secondAreaOpening; report 30) + else + report 30 - let matchingEllipses = logTimeWithName "Finding ellipses" (fun () -> Ellipse.find edges xGradient yGradient config) + let parasites, filteredGreenWhitoutStain = ParasitesMarker.find filteredGreen config + //let parasites, filteredGreenWhitoutInfection, filteredGreenWhitoutStain = ParasitesMarker.findMa greenFloat filteredGreenFloat config - report 60 + let! edges, xGradient, yGradient = logTimeWithName "Finding edges" (fun () -> + let edges, xGradient, yGradient = ImgTools.findEdges filteredGreenWhitoutStain + removeArea edges (config.RBCRadius.Pixel ** 2.f / 50.f |> int) + reportWithVal 40 (edges, xGradient, yGradient)) - let prunedEllipses = logTimeWithName "Ellipses pruning" (fun () -> matchingEllipses.PrunedEllipses) + let! matchingEllipses = logTimeWithName "Finding ellipses" (fun () -> reportWithVal 60 (Ellipse.find edges xGradient yGradient config)) - report 80 + let! prunedEllipses = logTimeWithName "Ellipses pruning" (fun () -> reportWithVal 80 (matchingEllipses.PrunedEllipses)) - let cells = logTimeWithName "Classifier" (fun () -> Classifier.findCells prunedEllipses parasites filteredGreenWhitoutStain config) + let! cells = logTimeWithName "Classifier" (fun () -> reportWithVal 100 (Classifier.findCells prunedEllipses parasites filteredGreenWhitoutStain config)) - report 100 + logWithName "Analysis finished" - logWithName "Analysis finished" + do + // Output pictures if debug flag is set. + match config.Debug with + | DebugOn output -> + let dirPath = System.IO.Path.Combine(output, name) + System.IO.Directory.CreateDirectory dirPath |> ignore - // Output pictures if debug flag is set. - match config.Debug with - | DebugOn output -> - let dirPath = System.IO.Path.Combine(output, name) - System.IO.Directory.CreateDirectory dirPath |> ignore + let buildFileName postfix = System.IO.Path.Combine(dirPath, name + postfix) - let buildFileName postfix = System.IO.Path.Combine(dirPath, name + postfix) + saveMat (edges * 255.0) (buildFileName " - edges.png") - saveMat (edges * 255.0) (buildFileName " - edges.png") + saveImg parasites.darkStain (buildFileName " - parasites - dark stain.png") + saveImg parasites.stain (buildFileName " - parasites - stain.png") + saveImg parasites.infection (buildFileName " - parasites - infection.png") - saveImg parasites.darkStain (buildFileName " - parasites - dark stain.png") - saveImg parasites.stain (buildFileName " - parasites - stain.png") - saveImg parasites.infection (buildFileName " - parasites - infection.png") + let imgAllEllipses = img.Copy() + drawEllipses imgAllEllipses matchingEllipses.Ellipses (Bgr(255.0, 255.0, 255.0)) 0.04 + saveImg imgAllEllipses (buildFileName " - ellipses - all.png") - let imgAllEllipses = img.Copy() - drawEllipses imgAllEllipses matchingEllipses.Ellipses (Bgr(255.0, 255.0, 255.0)) 0.04 - saveImg imgAllEllipses (buildFileName " - ellipses - all.png") + let imgEllipses = filteredGreenWhitoutStain.Convert() + drawEllipses imgEllipses prunedEllipses (Bgr(0.0, 240.0, 240.0)) 1.0 + saveImg imgEllipses (buildFileName " - ellipses.png") - let imgEllipses = filteredGreenWhitoutStain.Convert() - drawEllipses imgEllipses prunedEllipses (Bgr(0.0, 240.0, 240.0)) 1.0 - saveImg imgEllipses (buildFileName " - ellipses.png") + let imgCells = img.Copy() + drawCells imgCells false cells + saveImg imgCells (buildFileName " - cells.png") - let imgCells = img.Copy() - drawCells imgCells false cells - saveImg imgCells (buildFileName " - cells.png") + let imgCells' = img.Copy() + drawCells imgCells' true cells + saveImg imgCells' (buildFileName " - cells - full.png") - let imgCells' = img.Copy() - drawCells imgCells' true cells - saveImg imgCells' (buildFileName " - cells - full.png") + let filteredGreenMaxima = gaussianFilter greenFloat config.LPFStandardDeviation + for m in ImgTools.findMaxima filteredGreenMaxima do + ImgTools.drawPoints filteredGreenMaxima m 255.f + saveImg filteredGreenMaxima (buildFileName " - filtered - maxima.png") - let filteredGreenMaxima = gaussianFilter greenFloat config.LPFStandardDeviation - for m in ImgTools.findMaxima filteredGreenMaxima do - ImgTools.drawPoints filteredGreenMaxima m 255.f - saveImg filteredGreenMaxima (buildFileName " - filtered - maxima.png") + saveImg filteredGreen (buildFileName " - filtered.png") + saveImg filteredGreenWhitoutStain (buildFileName " - filtered closed stain.png") + //saveImg filteredGreenWhitoutInfection (buildFileName " - filtered closed infection.png") - saveImg filteredGreen (buildFileName " - filtered.png") - saveImg filteredGreenWhitoutStain (buildFileName " - filtered closed stain.png") - //saveImg filteredGreenWhitoutInfection (buildFileName " - filtered closed infection.png") + saveImg green (buildFileName " - green.png") - saveImg green (buildFileName " - green.png") + use blue = img.Item(0) + saveImg blue (buildFileName " - blue.png") - use blue = img.Item(0) - saveImg blue (buildFileName " - blue.png") + use red = img.Item(2) + saveImg red (buildFileName " - red.png") + | _ -> () - use red = img.Item(2) - saveImg red (buildFileName " - red.png") - | _ -> () + return cells } - cells - -// ID * cell list. -let doMultipleAnalysis (imgs: (string * Config * Image) list) (reportProgress: (int -> unit) option) : (string * Cell list) list = - let inline report (percent: int) = +/// +/// Do multiple analyses on the same time. The number of concurrent process depends if the number of the core. +/// +/// +/// An optional function to report progress. The process is aborted if the returned value is false +let doMultipleAnalysis (imgs: (string * Config * Image) list) (reportProgress: (int -> bool) option) : (string * Cell list) list option = + let report (percent: int) : bool = match reportProgress with | Some f -> f percent - | _ -> () + | _ -> true let progressPerAnalysis = System.Collections.Concurrent.ConcurrentDictionary() let nbImgs = List.length imgs @@ -143,7 +155,18 @@ let doMultipleAnalysis (imgs: (string * Config * Image) list) (report let n = Environment.ProcessorCount - imgs - |> PSeq.map (fun (id, config, img) -> id, doAnalysis img id config (Some (fun p -> reportProgressImg id p))) - |> PSeq.withDegreeOfParallelism n - |> PSeq.toList + let results = + imgs + |> PSeq.choose ( + fun (id, config, img) -> + match doAnalysis img id config (Some (fun p -> reportProgressImg id p)) with + | Some result -> Some (id, result) + | None -> None) + |> PSeq.withDegreeOfParallelism n + |> PSeq.toList + + // If one of the analyses has been aborted we return 'None'. + if List.length results <> List.length imgs + then None + else Some results + diff --git a/Parasitemia/ParasitemiaCore/ParasitesMarker.fs b/Parasitemia/ParasitemiaCore/ParasitesMarker.fs index 22ea041..ffe0d14 100644 --- a/Parasitemia/ParasitemiaCore/ParasitesMarker.fs +++ b/Parasitemia/ParasitemiaCore/ParasitesMarker.fs @@ -6,8 +6,6 @@ open System.Linq open Emgu.CV open Emgu.CV.Structure -open Logger - open Utils type Result = { @@ -21,7 +19,7 @@ type Result = { // * 'Infection' corresponds to the parasite. It shouldn't contain thrombocytes. let findMa (green: Image) (filteredGreen: Image) (config: Config.Config) : Result * Image * Image = // We use the filtered image to find the dark stain. - let kmediansResults = Log.LogWithTime("Finding fg/bg (k-medians)", Severity.USER, (fun () -> KMedians.kmedians filteredGreen)) + let kmediansResults = KMedians.kmedians filteredGreen let { KMedians.fg = fg; KMedians.median_bg = median_bg; KMedians.median_fg = median_fg; KMedians.d_fg = d_fg } = kmediansResults let darkStain = d_fg.Cmp(median_bg * float config.Parameters.darkStainLevel, CvEnum.CmpType.GreaterThan) darkStain._And(filteredGreen.Cmp(median_fg, CvEnum.CmpType.LessThan)) diff --git a/Parasitemia/ParasitemiaCore/Types.fs b/Parasitemia/ParasitemiaCore/Types.fs index 917e7d8..e2accb7 100644 --- a/Parasitemia/ParasitemiaCore/Types.fs +++ b/Parasitemia/ParasitemiaCore/Types.fs @@ -59,4 +59,29 @@ type Line (a: float32, b: float32) = member this.A = a member this.B = b +type MaybeBuilder () = + member this.Bind (x, f) = + match x with + | None -> None + | Some a -> f a + member this.ReturnFrom (x) = x + + member this.TryFinally (body, compensation) = + try this.ReturnFrom(body()) + finally compensation() + + member this.Using (disposable: 'a when 'a :> IDisposable, body) = + let body' = fun () -> body disposable + this.TryFinally(body', fun () -> + match disposable with + | null -> () + | disp -> disp.Dispose()) + + member this.Zero () = + None + + member this.Return (x) = + Some x + +let maybe = new MaybeBuilder() \ No newline at end of file diff --git a/Parasitemia/ParasitemiaUI/Analysis.fs b/Parasitemia/ParasitemiaUI/Analysis.fs index 24f9adb..9b9ed51 100644 --- a/Parasitemia/ParasitemiaUI/Analysis.fs +++ b/Parasitemia/ParasitemiaUI/Analysis.fs @@ -117,30 +117,34 @@ let showWindow (parent: Window) (state: State.State) : bool = then MessageBox.Show("No image selected", "Cannot start analysis", MessageBoxButton.OK, MessageBoxImage.Information) |> ignore else + stackSourceImagesSelection.IsEnabled <- false analysisPerformed <- false butStart.IsEnabled <- false butClose.Content <- "Abort" + async { - let results = + let maybeResults = ParasitemiaCore.Analysis.doMultipleAnalysis imagesToProcess - (Some (fun progress -> window.Root.Dispatcher.Invoke(fun () -> progressBar.Value <- float progress))) + (Some (fun progress -> window.Root.Dispatcher.Invoke(fun () -> progressBar.Value <- float progress); not analysisCancelled)) lock monitor ( fun() -> - if not analysisCancelled - then + match maybeResults with + | Some results -> for id, cells in results do state.SetResult (int id) cells window.Root.Dispatcher.Invoke(fun () -> + stackSourceImagesSelection.IsEnabled <- true butStart.IsEnabled <- true butClose.Content <- "Close" updateSourceImages ()) Logger.Log.User("All analyses terminated successfully") atLeastOneAnalysisPerformed <- true - analysisPerformed <- true) + analysisPerformed <- true + | None -> ()) } |> Async.Start | _ -> ()) diff --git a/Parasitemia/ParasitemiaUI/GUI.fs b/Parasitemia/ParasitemiaUI/GUI.fs index d30cb66..921654a 100644 --- a/Parasitemia/ParasitemiaUI/GUI.fs +++ b/Parasitemia/ParasitemiaUI/GUI.fs @@ -344,6 +344,10 @@ let run (defaultConfig: Config) (fileToOpen: string option) = if currentRemoved then updateCurrentImage() + + updateGlobalParasitemia() + + // Update image numbers. stackPreviews.Children |> Seq.cast |> Seq.iter (fun imgPreview -> imgPreview.txtImageNumber.Text <- (imgPreview.Tag :?> SourceImage).num.ToString())) imgCtrl.Tag <- srcImg diff --git a/Parasitemia/ParasitemiaUI/Program.fs b/Parasitemia/ParasitemiaUI/Program.fs index 74f0d35..24e33cd 100644 --- a/Parasitemia/ParasitemiaUI/Program.fs +++ b/Parasitemia/ParasitemiaUI/Program.fs @@ -74,12 +74,15 @@ let main args = let images = [ for file in files -> Path.GetFileNameWithoutExtension(FileInfo(file).Name), config.Copy(), new Image(file) ] Log.LogWithTime("Whole analyze", Severity.USER, (fun () -> - let results = ParasitemiaCore.Analysis.doMultipleAnalysis images None - - for id, cells in results do - let config = images |> List.pick (fun (id', config', _) -> if id' = id then Some config' else None) - let total, infected = countCells cells - fprintf resultFile "File: %s %d %d %.2f (diameter: %A)\n" id total infected (100. * (float infected) / (float total)) config.RBCRadius)) + match ParasitemiaCore.Analysis.doMultipleAnalysis images None with + | Some results -> + for id, cells in results do + let config = images |> List.pick (fun (id', config', _) -> if id' = id then Some config' else None) + let total, infected = countCells cells + fprintf resultFile "File: %s %d %d %.2f (diameter: %A)\n" id total infected (100. * (float infected) / (float total)) config.RBCRadius + | None -> + fprintf resultFile "Analysis aborted" + Some ())) |> ignore Log.RmListener(listener) 0 diff --git a/Parasitemia/ParasitemiaUI/XAML/MainWindow.xaml b/Parasitemia/ParasitemiaUI/XAML/MainWindow.xaml index 6037bc1..d8c1fc8 100644 --- a/Parasitemia/ParasitemiaUI/XAML/MainWindow.xaml +++ b/Parasitemia/ParasitemiaUI/XAML/MainWindow.xaml @@ -21,7 +21,7 @@ - + -- 2.45.2