module ParasitemiaUI.Analysis open System open System.IO open System.Linq open System.Windows open System.Windows.Media open System.Windows.Markup open System.Windows.Shapes open System.Windows.Controls open System.Diagnostics open Microsoft.Win32 // For the common dialogs. open Emgu.CV.WPF open ParasitemiaCore.UnitsOfMeasure open ParasitemiaCore.Config open Types let showWindow (parent: Window) (state: State.State) : bool = let window = Views.AnalysisWindow() window.Root.Owner <- parent window.Root.Left <- parent.Left + parent.ActualWidth / 2. - window.Root.Width / 2. window.Root.Top <- parent.Top + parent.ActualHeight / 2. - window.Root.Height / 2. let ctrl (name: string): 'a = window.Root.FindName(name) :?> 'a let butClose: Button = ctrl "butClose" let butStart: Button = ctrl "butStart" let stackSourceImagesSelection: StackPanel = ctrl "stackSourceImagesSelection" let progressBar: ProgressBar = ctrl "progress" let textLog: TextBlock = ctrl "textLog" let scrollLog: ScrollViewer = ctrl "scrollLog" let logListener = { new Logger.IListener with member this.NewEntry severity mess = window.Root.Dispatcher.Invoke(fun () -> textLog.Inlines.Add(Documents.Run(mess)) textLog.Inlines.Add(Documents.LineBreak()) scrollLog.ScrollToBottom()) } Logger.Log.AddListener(logListener) let minPPI = 1. let maxPPI = 10e6 let parseAndValidatePPI (input: string) : float option = let res = ref 0. if Double.TryParse(input, res) && !res >= minPPI && !res <= maxPPI then Some !res else None let monitor = Object() let mutable atLeastOneAnalysisPerformed = false let mutable analysisPerformed = false let mutable analysisCancelled = false let updateSourceImages () = stackSourceImagesSelection.Children.Clear() let width = int stackSourceImagesSelection.ActualWidth for srcImg in state.SourceImages do let imageSourceSelection = Views.ImageSourceSelection(Tag = srcImg, Margin = Thickness(3.)) imageSourceSelection.Tag <- srcImg imageSourceSelection.txtImageNumber.Text <- srcImg.num.ToString() let height = srcImg.img.Height * width / srcImg.img.Width imageSourceSelection.imagePreview.Source <- BitmapSourceConvert.ToBitmapSource(srcImg.img.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic)) imageSourceSelection.chkSelection.IsChecked <- Nullable(srcImg.dateLastAnalysis.Ticks = 0L) imageSourceSelection.lblDateLastAnalysis.Content <- if srcImg.dateLastAnalysis.Ticks = 0L then "" else srcImg.dateLastAnalysis.ToString() imageSourceSelection.txtResolution.Text <- if srcImg.dateLastAnalysis.Ticks = 0L then "" else srcImg.config.Parameters.resolution.ToString() imageSourceSelection.menuZoom50X.Click.AddHandler(fun obj args -> imageSourceSelection.txtResolution.Text <- "230000") imageSourceSelection.menuZoom100X.Click.AddHandler(fun obj args -> imageSourceSelection.txtResolution.Text <- "460000") imageSourceSelection.txtResolution.PreviewTextInput.AddHandler(fun obj args -> let text = imageSourceSelection.txtResolution.Text + args.Text args.Handled <- match parseAndValidatePPI text with Some _ -> false | None -> true) imageSourceSelection.imagePreview.MouseLeftButtonDown.AddHandler(fun obj args -> let checkbox = imageSourceSelection.chkSelection checkbox.IsChecked <- Nullable(not (checkbox.IsChecked.HasValue && checkbox.IsChecked.Value))) stackSourceImagesSelection.Children.Add(imageSourceSelection) |> ignore // Get the new parameters for each image. If an error occurs then 'None' is returned and a message box is displayed. // The boolean is 'true' if the image is selected (checked). let getInputImagesParameters () : (SourceImage * bool * Parameters) list option = let sourceImagesControls = stackSourceImagesSelection.Children |> Seq.cast let parameters = seq { for srcImgCtrl in sourceImagesControls do let srcImg = srcImgCtrl.Tag :?> SourceImage let isChecked = srcImgCtrl.chkSelection.IsChecked match parseAndValidatePPI srcImgCtrl.txtResolution.Text with | Some resolution -> yield Some (srcImg, isChecked.HasValue && isChecked.Value, { srcImg.config.Parameters with resolution = resolution * 1. }) | None -> MessageBox.Show(sprintf "No resolution defined for the image number %d" srcImg.num, "No resolution defined", MessageBoxButton.OK, MessageBoxImage.Information) |> ignore yield None } |> Seq.takeWhile (fun e -> e.IsSome) |> Seq.map (fun e -> e.Value) |> List.ofSeq if parameters.Count() <> sourceImagesControls.Count() then None else Some parameters butClose.Click.AddHandler(fun obj args -> window.Root.Close()) butStart.Click.AddHandler(fun obj args -> match getInputImagesParameters () with | Some imagesParameters -> let imagesToProcess = [ for srcImg, selected, parameters in imagesParameters do srcImg.config.Parameters <- parameters // Save parameters. if selected then yield srcImg.num.ToString(), srcImg.config, srcImg.img ] if imagesToProcess.IsEmpty 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 maybeResults = ParasitemiaCore.Analysis.doMultipleAnalysis imagesToProcess (Some (fun progress -> window.Root.Dispatcher.Invoke(fun () -> progressBar.Value <- float progress); not analysisCancelled)) lock monitor ( fun() -> 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 | None -> ()) } |> Async.Start | _ -> ()) window.Root.Loaded.AddHandler(fun obj args -> updateSourceImages ()) window.Root.ShowDialog() |> ignore Logger.Log.RmListener(logListener) lock monitor (fun () -> if not analysisPerformed then analysisCancelled <- true atLeastOneAnalysisPerformed)