e5b853cc488a8fd7d412bbc91df5733eb992e051
[master-thesis.git] / Parasitemia / ParasitemiaCore / Config.fs
1 module ParasitemiaCore.Config
2
3 open System
4
5 open Const
6 open UnitsOfMeasure
7
8 type Debug =
9 | DebugOff
10 | DebugOn of string // Output directory.
11
12 type Parameters =
13 {
14 rbcDiameter : float<μm>
15 resolution : float<ppi>
16
17 ratioAreaPaleCenter : float32 // The area of the second opening is 'ratioSecondAreaOpen' * mean RBC area. It's applied only if greater than 'initialAreaOpen'.
18
19 granulometryRange : float32 // The radius will be seeked from radius - granulometryRange * radius to radius + granulometryRange * radius.
20
21 minRbcRadius : float32 // Factor of the mean RBC radius.
22 maxRbcRadius : float32 // Factor of the mean RBC radius.
23
24 LPFStandardDeviationParasite : float<μm> // Sigma parameter of the gaussian to remove the high frequency noise.
25 LPFStandardDeviationRBC : float<μm>
26
27 // Ellipse.
28 nbPickElementsMin : int
29 factorNbValidPick : float // The number of computed ellipse per edge pixel.
30 factorNbMaxPick : float
31
32 // Parasites detection.
33 darkStainLevel : float // Lower -> more sensitive. Careful about illumination on the borders.
34 maxDarkStainRatio : float // When a cell must own less than this ratio to be a RBC.
35
36 parasiteRadiusRatio : float32 // The ratio of the parasite radius of the RBC radius.
37 minimumParasiteAreaRatio : float32 // Factor of a RBC area. 0.5 means the half of RBC area.
38
39 cytoplasmSizeRatio : float32
40 cytoplasmSensitivity : float // between 0 (the least sensitive) and 1 (the most sensitive).
41
42 nucleusAreaRatio : float32 // Factor of a RBC area. 0.5 means the half of RBC area.
43 infectionSensitivity : float // between 0 (the least sensitive) and 1 (the most sensitive).
44
45 // [<Obsolete>] standardDeviationMaxRatio: float // The standard deviation of the pixel values of a cell can't be greater than standardDeviationMaxRatio * global standard deviation
46 minimumCellAreaFactor : float32 // Factor of the mean RBC area. A cell with an area below this will be rejected.
47 }
48
49 let defaultParameters =
50 {
51 rbcDiameter = 7.5<μm>
52 resolution = 230.e3<ppi> // 230.e3<ppi> Correspond to 50X.
53
54 ratioAreaPaleCenter = 2.f / 5.f // The ratio between an RBC area and the area of the its pale center.
55
56 granulometryRange = 0.5f
57
58 minRbcRadius = -0.23f
59 maxRbcRadius = 0.23f
60
61 LPFStandardDeviationParasite = 0.15<μm>
62 LPFStandardDeviationRBC = 0.22<μm>
63
64 nbPickElementsMin = 10
65 factorNbValidPick = 0.06 //1.0
66 factorNbMaxPick = 4.
67
68 darkStainLevel = 1.
69 maxDarkStainRatio = 0.1 // 10 %
70
71 parasiteRadiusRatio = 0.5f // 50 %
72 minimumParasiteAreaRatio = 0.02f // 2 %
73
74 cytoplasmSizeRatio = 1.f / 5.f
75 cytoplasmSensitivity = 0.96
76
77 nucleusAreaRatio = 0.01f // 1.0 %
78 infectionSensitivity = 0.92
79
80 // standardDeviationMaxRatio = 0.6 // Obsolete.
81 minimumCellAreaFactor = 0.4f
82 }
83
84 type RBCRadius (radius : float32, parameters : Parameters) =
85 member this.Pixel = radius
86 member this.μm : float<μm> =
87 1.<px> * (float radius) / parameters.resolution |> inchToμm
88
89 member this.Min = radius + parameters.minRbcRadius * radius
90 member this.Max = radius + parameters.maxRbcRadius * radius
91
92 member this.Area = PI * radius ** 2.f
93 member this.MinArea = parameters.minimumCellAreaFactor * this.Area
94
95 member this.ParasiteRadius = parameters.parasiteRadiusRatio * radius
96 member this.CytoplasmSize = parameters.cytoplasmSizeRatio * radius
97
98 member this.NucleusArea = parameters.nucleusAreaRatio * this.Area
99 member this.MinimumParasiteArea = parameters.minimumParasiteAreaRatio * this.Area
100
101 override this.ToString () =
102 sprintf "%d px (%.1f μm)" (Utils.roundInt <| 2.f * radius) (2. * this.μm)
103
104 type Config (param : Parameters) =
105 let RBCadiusInPixels (rbcDiameter : float<μm>) (resolution : float<ppi>) : float32 =
106 let rbcRadiusInch : float<inch> = (μmToInch rbcDiameter) / 2.
107 let rbcRadiusPx : float<px> = resolution * rbcRadiusInch
108 float32 rbcRadiusPx
109
110 let mutable parameters : Parameters = param
111 let mutable rbcRadiusByResolution = RBCRadius (RBCadiusInPixels parameters.rbcDiameter parameters.resolution, parameters)
112 let mutable rbcRadius = RBCRadius (0.f, parameters)
113
114 new () = Config defaultParameters
115
116 member this.Parameters
117 with get () = parameters
118 and set param =
119 parameters <- param
120 rbcRadiusByResolution <- RBCRadius (RBCadiusInPixels parameters.rbcDiameter parameters.resolution, param)
121 rbcRadius <- RBCRadius (rbcRadius.Pixel, param)
122
123 member val Debug = DebugOff with get, set
124
125 member this.LPFStandardDeviationParasite =
126 let stdDeviation : float<px> = (μmToInch parameters.LPFStandardDeviationParasite) * parameters.resolution
127 float stdDeviation
128
129 member this.LPFStandardDeviationRBC =
130 let stdDeviation : float<px> = (μmToInch parameters.LPFStandardDeviationRBC) * parameters.resolution
131 float stdDeviation
132
133 member this.RBCRadiusByResolution = rbcRadiusByResolution
134 member this.RBCRadius = rbcRadius
135
136 member this.SetRBCRadius (radiusPixel : float32) =
137 rbcRadius <- RBCRadius (radiusPixel, parameters)
138
139 member this.Copy () =
140 this.MemberwiseClone () :?> Config
141
142 override this.ToString () =
143 $"{{{nameof Config}: " +
144 $"{nameof this.LPFStandardDeviationParasite} = {this.LPFStandardDeviationParasite}, " +
145 $"{nameof this.LPFStandardDeviationRBC} = {this.LPFStandardDeviationRBC}, " +
146 $"{nameof this.RBCRadiusByResolution} = {this.RBCRadiusByResolution}, " +
147 $"{nameof this.RBCRadius} = {this.RBCRadius}, " +
148 $"{nameof this.Parameters} = {this.Parameters}}}"