810494e679ec3a01f5ee09651468d10ac9120b35
[master-thesis.git] / Parasitemia / ParasitemiaUI / Utils.fs
1 module ParasitemiaUI.Utils
2
3 open System.IO
4
5 open Newtonsoft.Json
6 open Newtonsoft.Json.Converters
7
8 open Types
9
10 let inline listAsStr (s : 'a seq) =
11 s |> Seq.fold (fun acc obj -> acc + (if acc = "" then "" else ", ") + string obj) ""
12
13 let percentText (nbTotal : int, nb : int) : string =
14 if nbTotal = 0 then
15 ""
16 else
17 let percent = 100. * (float nb) / (float nbTotal)
18 sprintf "%.1f %% (%d / %d)" percent nb nbTotal
19
20 let roamingDir =
21 Path.Combine (System.Environment.GetFolderPath (System.Environment.SpecialFolder.ApplicationData), "Parasitemia")
22
23 let predefinedPPIFilename = "predefined-ppi.json"
24 let predefinedPPIFilepath = Path.Combine (roamingDir, predefinedPPIFilename)
25
26 let sensorSizesFilename = "sensor-sizes.json"
27 let sensorSizesFilepath = Path.Combine (roamingDir, sensorSizesFilename)
28
29 let private savePredefinedPPIToFile (predefinedPPI : PredefinedPPI list) =
30 try
31 Directory.CreateDirectory roamingDir |> ignore
32 use file = new StreamWriter (predefinedPPIFilepath)
33 file.Write (JsonConvert.SerializeObject (predefinedPPI, JsonSerializerSettings (Formatting = Formatting.Indented)))
34 with
35 ex ->
36 Logger.Log.Error "Unable to save predefined PPI to file \"%s\": %O" predefinedPPIFilepath ex
37
38 let private saveSensorSizesToFile (sensorSizes : SensorSize list) =
39 try
40 Directory.CreateDirectory roamingDir |> ignore
41 use file = new StreamWriter (sensorSizesFilepath)
42 file.Write (JsonConvert.SerializeObject (sensorSizes, JsonSerializerSettings (Formatting = Formatting.Indented)))
43 with
44 ex ->
45 Logger.Log.Error "Unable to save sensor sizes to file \"%s\": %O" sensorSizesFilepath ex
46
47 let predefinedPPI : PredefinedPPI list =
48 try
49 use file = new StreamReader (predefinedPPIFilepath)
50 JsonConvert.DeserializeObject<PredefinedPPI list> (file.ReadToEnd ())
51 with
52 | _ex ->
53 savePredefinedPPIToFile defaultPredefinedPPI
54 defaultPredefinedPPI
55
56 let sensorSizes : SensorSize list =
57 try
58 use file = new StreamReader (sensorSizesFilepath)
59 JsonConvert.DeserializeObject<SensorSize list> (file.ReadToEnd ())
60 with
61 | _ex ->
62 saveSensorSizesToFile defaultSensorSizes
63 defaultSensorSizes
64
65 let argsHelp =
66 let programName = System.AppDomain.CurrentDomain.FriendlyName
67 "Usage of Parasitemia:\n" +
68 "Non-interactive mode:\n" +
69 (sprintf " %s (--folder <folder>|--file <file>) --output <folder> [--debug]\n" programName) +
70 " --folder <folder> : an input folder containing images to analyze\n" +
71 " --file <file> : an image file to be analyzed\n" +
72 " --output <folder> : a folder to put the results\n" +
73 " --debug : output more information like intermediate images (it takes more CPU and memory)\n" +
74
75 "Interactive mode:\n" +
76 (sprintf " %s [<document-file>] [--debug]\n" programName) +
77 " <document-file> : a PIAZ file to automatically open at startup\n" +
78 " --debug : output information like intermediate images in the current directory (it takes more CPU and memory)"