image.domain\self_evolve\degrade_detect.mbt

98.4% · 60 / 61 points · 221 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 退化检测 self_evolve/degrade_detect
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 退化场景自动发现 — 基于 4.7.4 领域规则
  7. 7// Degradation detection: based on 4.7.4 domain rules
  8. 8//
  9. 9// 规则集:
  10. 10// 1. 数值鲁棒性:除零 / 奇异矩阵 / NaN / Inf
  11. 11// 2. 退化几何:除零 / 零面积
  12. 12// 3. 场间断:边界不连续 / 插值外推
  13. 13// 4. 时空拓扑:时间超出 [0,1]
  14. 14// 5. 四目标兼容:纯 MoonBit(静态保证,运行时无需检测)
  15. 15
  16. 16///|
  17. 17/// 检测单个算子的退化场景。
  18. 18/// 检测 Detect degradation cases for a single op.
  19. 19pub fn detect_degradation(op : @imgir.IROp) -> Array[DegradationCase] {
  20. 20● let tag = op.tag()
  21. 21● match op {
  22. 22 Resize(w, h) =>
  23. 23● if w == 0 || h == 0 {
  24. 24● [
  25. 25 {
  26. 26 kind: DivisionByZero,
  27. 27 op_tag: tag,
  28. 28 description: "Resize 到零尺寸(w=" +
  29. 29● w.to_string() +
  30. 30 ", h=" +
  31. 31● h.to_string() +
  32. 32 "),可能触发除零",
  33. 33 severity: Fatal,
  34. 34 },
  35. 35 ]
  36. 36 } else {
  37. 37● []
  38. 38 }
  39. 39 Crop(_x, _y, w, h) =>
  40. 40● if w == 0 || h == 0 {
  41. 41● [
  42. 42 {
  43. 43 kind: DivisionByZero,
  44. 44 op_tag: tag,
  45. 45 description: "Crop 零尺寸区域,可能触发除零",
  46. 46 severity: Fatal,
  47. 47 },
  48. 48 ]
  49. 49 } else {
  50. 50● []
  51. 51 }
  52. 52 GaussianBlur(_r, sigma) =>
  53. 53● if sigma == 0.0F {
  54. 54● [
  55. 55 {
  56. 56 kind: DivisionByZero,
  57. 57 op_tag: tag,
  58. 58 description: "GaussianBlur sigma=0.0,高斯核归一化除零",
  59. 59 severity: Critical,
  60. 60 },
  61. 61 ]
  62. 62● } else if sigma < 0.0F {
  63. 63● [
  64. 64 {
  65. 65 kind: NaNValue,
  66. 66 op_tag: tag,
  67. 67 description: "GaussianBlur sigma<0.0,负 sigma 产生 NaN/Inf",
  68. 68 severity: Critical,
  69. 69 },
  70. 70 ]
  71. 71 } else {
  72. 72● []
  73. 73 }
  74. 74 Sharpen(amount) =>
  75. 75● if amount > 100.0F || amount < -100.0F {
  76. 76● [
  77. 77 {
  78. 78 kind: Overflow,
  79. 79 op_tag: tag,
  80. 80 description: "Sharpen amount 极端值(" +
  81. 81● amount.to_string() +
  82. 82 "),可能溢出",
  83. 83 severity: Warning,
  84. 84 },
  85. 85 ]
  86. 86 } else {
  87. 87● []
  88. 88 }
  89. 89 FieldClamp(lo, hi) =>
  90. 90● if lo > hi {
  91. 91● [
  92. 92 {
  93. 93 kind: FieldDiscontinuity,
  94. 94 op_tag: tag,
  95. 95 description: "FieldClamp 下界 > 上界(lo=" +
  96. 96● lo.to_string() +
  97. 97 " > hi=" +
  98. 98● hi.to_string() +
  99. 99 "),行为未定义",
  100. 100 severity: Critical,
  101. 101 },
  102. 102 ]
  103. 103 } else {
  104. 104● []
  105. 105 }
  106. 106 FieldMask(threshold) =>
  107. 107● if threshold > 1000000.0F || threshold < -1000000.0F {
  108. 108● [
  109. 109 {
  110. 110 kind: FieldDiscontinuity,
  111. 111 op_tag: tag,
  112. 112 description: "FieldMask 阈值极端(" +
  113. 113● threshold.to_string() +
  114. 114 "),全场单值",
  115. 115 severity: Warning,
  116. 116 },
  117. 117 ]
  118. 118 } else {
  119. 119● []
  120. 120 }
  121. 121 STSnapshot(t) =>
  122. 122● if t < 0.0F || t > 1.0F {
  123. 123● [
  124. 124 {
  125. 125 kind: FieldDiscontinuity,
  126. 126 op_tag: tag,
  127. 127 description: "STSnapshot t=" +
  128. 128● t.to_string() +
  129. 129 " 超出 [0,1],时间外推",
  130. 130 severity: Warning,
  131. 131 },
  132. 132 ]
  133. 133 } else {
  134. 134● []
  135. 135 }
  136. 136 STInterpolate(t) =>
  137. 137● if t < 0.0F || t > 1.0F {
  138. 138● [
  139. 139 {
  140. 140 kind: FieldDiscontinuity,
  141. 141 op_tag: tag,
  142. 142 description: "STInterpolate t=" +
  143. 143● t.to_string() +
  144. 144 " 超出 [0,1],插值外推",
  145. 145 severity: Warning,
  146. 146 },
  147. 147 ]
  148. 148 } else {
  149. 149● []
  150. 150 }
  151. 151● _ => []
  152. 152 }
  153. 153}
  154. 154
  155. 155///|
  156. 156/// 批量扫描退化场景。
  157. 157/// 描述 Batch scan degradations for multiple ops.
  158. 158pub fn scan_degradations(ops : Array[@imgir.IROp]) -> Array[DegradationCase] {
  159. 159● let all : Array[DegradationCase] = []
  160. 160 for op in ops {
  161. 161● for d in detect_degradation(op) {
  162. 162● all.push(d)
  163. 163 }
  164. 164 }
  165. 165 all
  166. 166}
  167. 167
  168. 168///|
  169. 169/// 按严重程度过滤退化场景。
  170. 170/// 滤波 Filter degradations by severity.
  171. 171pub fn filter_by_severity(
  172. 172 cases : Array[DegradationCase],
  173. 173 min_severity : Severity,
  174. 174) -> Array[DegradationCase] {
  175. 175● let min_level = match min_severity {
  176. 176○ Warning => 0
  177. 177● Critical => 1
  178. 178● Fatal => 2
  179. 179 }
  180. 180 let result : Array[DegradationCase] = []
  181. 181 for c in cases {
  182. 182● let level = match c.severity {
  183. 183● Warning => 0
  184. 184● Critical => 1
  185. 185● Fatal => 2
  186. 186 }
  187. 187 if level >= min_level {
  188. 188● result.push(c)
  189. 189 }
  190. 190 }
  191. 191 result
  192. 192}
  193. 193
  194. 194///|
  195. 195/// 按退化类型分组计数。
  196. 196/// 计数 Count degradations by kind.
  197. 197pub fn count_by_kind(
  198. 198 cases : Array[DegradationCase],
  199. 199) -> Array[(DegradationKind, Int32)] {
  200. 200● let kinds = [
  201. 201 DivisionByZero,
  202. 202 SingularMatrix,
  203. 203 FieldDiscontinuity,
  204. 204 Overflow,
  205. 205 NaNValue,
  206. 206 InfValue,
  207. 207 ]
  208. 208 let result : Array[(DegradationKind, Int32)] = []
  209. 209 for k in kinds {
  210. 210● let mut count = 0
  211. 211 for c in cases {
  212. 212● if c.kind == k {
  213. 213● count = count + 1
  214. 214 }
  215. 215 }
  216. 216 if count > 0 {
  217. 217● result.push((k, count))
  218. 218 }
  219. 219 }
  220. 220 result
  221. 221}