Add documentation.
[master-thesis.git] / Parasitemia / ParasitemiaUI / Analysis.fs
1 module ParasitemiaUI.Analysis
2
3 open System
4 open System.IO
5 open System.Linq
6 open System.Windows
7 open System.Windows.Media
8 open System.Windows.Markup
9 open System.Windows.Shapes
10 open System.Windows.Controls
11 open System.Diagnostics
12 open Microsoft.Win32 // For the common dialogs.
13
14 open Emgu.CV.WPF
15
16 open ParasitemiaCore.UnitsOfMeasure
17 open ParasitemiaCore.Config
18
19 open Types
20
21 let showWindow (parent: Window) (state: State.State) : bool =
22 let window = Views.AnalysisWindow()
23 window.Root.Owner <- parent
24 window.Root.Left <- parent.Left + parent.ActualWidth / 2. - window.Root.Width / 2.
25 window.Root.Top <- parent.Top + parent.ActualHeight / 2. - window.Root.Height / 2.
26
27 let ctrl (name: string): 'a = window.Root.FindName(name) :?> 'a
28
29 let butClose: Button = ctrl "butClose"
30 let butStart: Button = ctrl "butStart"
31
32 let stackImagesSourceSelection: StackPanel = ctrl "stackImagesSourceSelection"
33 let progressBar: ProgressBar = ctrl "progress"
34 let textLog: TextBlock = ctrl "textLog"
35 let scrollLog: ScrollViewer = ctrl "scrollLog"
36
37 let logListener =
38 { new Logger.IListener with
39 member this.NewEntry severity mess =
40 window.Root.Dispatcher.Invoke(fun () ->
41 textLog.Inlines.Add(Documents.Run(mess))
42 textLog.Inlines.Add(Documents.LineBreak())
43 scrollLog.ScrollToBottom()) }
44
45 Logger.Log.AddListener(logListener)
46
47 let minPPI = 1.
48 let maxPPI = 10e6
49 let parseAndValidatePPI (input: string) : float option =
50 let res = ref 0.
51 if Double.TryParse(input, res) && !res >= minPPI && !res <= maxPPI
52 then Some !res
53 else None
54
55 let monitor = Object()
56 let mutable atLeastOneAnalysisPerformed = false
57 let mutable analysisPerformed = false
58 let mutable analysisCancelled = false
59
60 let updateSourceImages () =
61 stackImagesSourceSelection.Children.Clear()
62 let width = int stackImagesSourceSelection.ActualWidth
63 for srcImg in state.SourceImages do
64 let imageSourceSelection = Views.ImageSourceSelection(Tag = srcImg, Margin = Thickness(3.))
65
66 let updateResolution () =
67 match parseAndValidatePPI imageSourceSelection.txtResolution.Text with
68 | Some resolution -> srcImg.config.Parameters <- { srcImg.config.Parameters with resolution = resolution * 1.<ppi> }
69 | None -> ()
70
71 imageSourceSelection.txtImageNumber.Text <- srcImg.num.ToString()
72 let height = srcImg.img.Height * width / srcImg.img.Width
73 imageSourceSelection.imagePreview.Source <- BitmapSourceConvert.ToBitmapSource(srcImg.img.Resize(width, height, Emgu.CV.CvEnum.Inter.Cubic))
74 imageSourceSelection.chkSelection.IsChecked <- Nullable<bool>(srcImg.dateLastAnalysis.Ticks = 0L)
75 imageSourceSelection.lblDateLastAnalysis.Content <- if srcImg.dateLastAnalysis.Ticks = 0L then "<Never>" else srcImg.dateLastAnalysis.ToString()
76
77 imageSourceSelection.txtResolution.Text <- srcImg.config.Parameters.resolution.ToString()
78 imageSourceSelection.menuZoom50X.Click.AddHandler(fun obj args -> imageSourceSelection.txtResolution.Text <- "230000"; updateResolution ())
79 imageSourceSelection.menuZoom100X.Click.AddHandler(fun obj args -> imageSourceSelection.txtResolution.Text <- "460000"; updateResolution ())
80
81 imageSourceSelection.txtResolution.PreviewTextInput.AddHandler(fun obj args ->
82 let text = imageSourceSelection.txtResolution.Text + args.Text
83 args.Handled <- match parseAndValidatePPI text with Some _ -> false | None -> true)
84
85 imageSourceSelection.imagePreview.MouseLeftButtonDown.AddHandler(fun obj args ->
86 let checkbox = imageSourceSelection.chkSelection
87 checkbox.IsChecked <- Nullable<bool>(not (checkbox.IsChecked.HasValue && checkbox.IsChecked.Value)))
88
89 imageSourceSelection.txtResolution.LostFocus.AddHandler(fun obj args -> updateResolution ())
90
91 stackImagesSourceSelection.Children.Add(imageSourceSelection) |> ignore
92
93 butClose.Click.AddHandler(fun obj args -> window.Root.Close())
94
95 butStart.Click.AddHandler(fun obj args ->
96 let imagesToProcess = [
97 for imageSelection in stackImagesSourceSelection.Children |> Seq.cast<Views.ImageSourceSelection> do
98 let chk = imageSelection.chkSelection.IsChecked
99 if chk.HasValue && chk.Value
100 then
101 let srcImg = imageSelection.Tag :?> SourceImage
102 yield srcImg.num.ToString(), srcImg.config, srcImg.img ]
103
104 if imagesToProcess.IsEmpty
105 then
106 MessageBox.Show("No image selected", "Cannot start analysis", MessageBoxButton.OK, MessageBoxImage.Information) |> ignore
107 else
108 analysisPerformed <- false
109 butStart.IsEnabled <- false
110 butClose.Content <- "Abort"
111 async {
112 let results =
113 ParasitemiaCore.Analysis.doMultipleAnalysis
114 imagesToProcess
115 (Some (fun progress -> window.Root.Dispatcher.Invoke(fun () -> progressBar.Value <- float progress)))
116
117 lock monitor (
118 fun() ->
119 if not analysisCancelled
120 then
121 for id, cells in results do
122 state.SetResult (int id) cells
123
124 window.Root.Dispatcher.Invoke(fun () ->
125 butStart.IsEnabled <- true
126 butClose.Content <- "Close"
127 updateSourceImages ())
128
129 Logger.Log.User("All analyses terminated successfully")
130 atLeastOneAnalysisPerformed <- true
131 analysisPerformed <- true)
132 } |> Async.Start)
133
134 window.Root.Loaded.AddHandler(fun obj args -> updateSourceImages ())
135
136 window.Root.ShowDialog() |> ignore
137
138 Logger.Log.RmListener(logListener)
139
140 lock monitor (fun () ->
141 if not analysisPerformed
142 then
143 analysisCancelled <- true
144 atLeastOneAnalysisPerformed)