nmrium 2.6.0 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (220) hide show
  1. package/lib/component/1d/ApodizationLine.js +2 -2
  2. package/lib/component/1d/ApodizationLine.js.map +1 -1
  3. package/lib/component/1d/Line.js +2 -2
  4. package/lib/component/1d/Line.js.map +1 -1
  5. package/lib/component/1d/SVGContent1D.d.ts.map +1 -1
  6. package/lib/component/1d/SVGContent1D.js +4 -1
  7. package/lib/component/1d/SVGContent1D.js.map +1 -1
  8. package/lib/component/1d/baseline/BaselinePreview.js +2 -2
  9. package/lib/component/1d/baseline/BaselinePreview.js.map +1 -1
  10. package/lib/component/1d/database/DatabaseSpectrum.js +2 -2
  11. package/lib/component/1d/database/DatabaseSpectrum.js.map +1 -1
  12. package/lib/component/1d/database/ResurrectedDatabaseRanges.js +2 -2
  13. package/lib/component/1d/database/ResurrectedDatabaseRanges.js.map +1 -1
  14. package/lib/component/1d/matrix/Boxplot.js +5 -5
  15. package/lib/component/1d/matrix/Boxplot.js.map +1 -1
  16. package/lib/component/1d/matrix/Stocsy.js +2 -2
  17. package/lib/component/1d/matrix/Stocsy.js.map +1 -1
  18. package/lib/component/1d/peaks/PeakAnnotationsSpreadMode.d.ts.map +1 -1
  19. package/lib/component/1d/peaks/PeakAnnotationsSpreadMode.js +12 -3
  20. package/lib/component/1d/peaks/PeakAnnotationsSpreadMode.js.map +1 -1
  21. package/lib/component/1d/peaks/usePeakShapesPath.js +2 -2
  22. package/lib/component/1d/peaks/usePeakShapesPath.js.map +1 -1
  23. package/lib/component/1d/plugin_svg_chart.d.ts +2 -0
  24. package/lib/component/1d/plugin_svg_chart.d.ts.map +1 -0
  25. package/lib/component/1d/plugin_svg_chart.js +43 -0
  26. package/lib/component/1d/plugin_svg_chart.js.map +1 -0
  27. package/lib/component/1d-2d/components/RangeIndicator.d.ts.map +1 -1
  28. package/lib/component/1d-2d/components/RangeIndicator.js +1 -7
  29. package/lib/component/1d-2d/components/RangeIndicator.js.map +1 -1
  30. package/lib/component/2d/1d-tracer/HorizontalSliceChart.js +2 -2
  31. package/lib/component/2d/1d-tracer/HorizontalSliceChart.js.map +1 -1
  32. package/lib/component/2d/1d-tracer/Left1DChart.js +2 -2
  33. package/lib/component/2d/1d-tracer/Left1DChart.js.map +1 -1
  34. package/lib/component/2d/1d-tracer/Top1DChart.js +2 -2
  35. package/lib/component/2d/1d-tracer/Top1DChart.js.map +1 -1
  36. package/lib/component/2d/1d-tracer/VerticalSliceChart.js +2 -2
  37. package/lib/component/2d/1d-tracer/VerticalSliceChart.js.map +1 -1
  38. package/lib/component/2d/1d-tracer/phase-correction-traces/SpectrumPhaseTrace.js +3 -3
  39. package/lib/component/2d/1d-tracer/phase-correction-traces/SpectrumPhaseTrace.js.map +1 -1
  40. package/lib/component/2d/ft/Contours.js +2 -2
  41. package/lib/component/2d/ft/Contours.js.map +1 -1
  42. package/lib/component/context/ScaleContext.d.ts +1 -1
  43. package/lib/component/context/ScaleContext.d.ts.map +1 -1
  44. package/lib/component/elements/D3Axis.d.ts.map +1 -1
  45. package/lib/component/elements/D3Axis.js +15 -32
  46. package/lib/component/elements/D3Axis.js.map +1 -1
  47. package/lib/component/elements/export/ExportContent.d.ts.map +1 -1
  48. package/lib/component/elements/export/ExportContent.js +2 -2
  49. package/lib/component/elements/export/ExportContent.js.map +1 -1
  50. package/lib/component/elements/export/ExportOptionsModal.d.ts.map +1 -1
  51. package/lib/component/elements/export/ExportOptionsModal.js +9 -3
  52. package/lib/component/elements/export/ExportOptionsModal.js.map +1 -1
  53. package/lib/component/elements/export/useExportConfigurer.d.ts +1 -0
  54. package/lib/component/elements/export/useExportConfigurer.d.ts.map +1 -1
  55. package/lib/component/elements/export/useExportConfigurer.js +5 -0
  56. package/lib/component/elements/export/useExportConfigurer.js.map +1 -1
  57. package/lib/component/elements/print/PrintContent.d.ts.map +1 -1
  58. package/lib/component/elements/print/PrintContent.js +6 -25
  59. package/lib/component/elements/print/PrintContent.js.map +1 -1
  60. package/lib/component/elements/utilities/transferStyles.d.ts +2 -0
  61. package/lib/component/elements/utilities/transferStyles.d.ts.map +1 -0
  62. package/lib/component/elements/utilities/transferStyles.js +29 -0
  63. package/lib/component/elements/utilities/transferStyles.js.map +1 -0
  64. package/lib/component/hooks/useIntegralPath.js +2 -2
  65. package/lib/component/hooks/useIntegralPath.js.map +1 -1
  66. package/lib/component/modal/setting/tanstack_general_settings/errors/context.d.ts +106 -105
  67. package/lib/component/modal/setting/tanstack_general_settings/errors/context.d.ts.map +1 -1
  68. package/lib/component/modal/setting/tanstack_general_settings/errors/renderer.d.ts +106 -105
  69. package/lib/component/modal/setting/tanstack_general_settings/errors/renderer.d.ts.map +1 -1
  70. package/lib/component/modal/setting/tanstack_general_settings/general_settings_dialog_body.d.ts +106 -105
  71. package/lib/component/modal/setting/tanstack_general_settings/general_settings_dialog_body.d.ts.map +1 -1
  72. package/lib/component/modal/setting/tanstack_general_settings/general_settings_dialog_footer.d.ts +91 -90
  73. package/lib/component/modal/setting/tanstack_general_settings/general_settings_dialog_footer.d.ts.map +1 -1
  74. package/lib/component/modal/setting/tanstack_general_settings/general_settings_dialog_header.d.ts +91 -90
  75. package/lib/component/modal/setting/tanstack_general_settings/general_settings_dialog_header.d.ts.map +1 -1
  76. package/lib/component/modal/setting/tanstack_general_settings/hooks/use_safe_workspace.d.ts +15 -15
  77. package/lib/component/modal/setting/tanstack_general_settings/tabs/auto_processing_tab.d.ts +91 -90
  78. package/lib/component/modal/setting/tanstack_general_settings/tabs/auto_processing_tab.d.ts.map +1 -1
  79. package/lib/component/modal/setting/tanstack_general_settings/tabs/axis_tab.d.ts +91 -90
  80. package/lib/component/modal/setting/tanstack_general_settings/tabs/axis_tab.d.ts.map +1 -1
  81. package/lib/component/modal/setting/tanstack_general_settings/tabs/database_tab.d.ts +91 -90
  82. package/lib/component/modal/setting/tanstack_general_settings/tabs/database_tab.d.ts.map +1 -1
  83. package/lib/component/modal/setting/tanstack_general_settings/tabs/export_tab.d.ts +91 -90
  84. package/lib/component/modal/setting/tanstack_general_settings/tabs/export_tab.d.ts.map +1 -1
  85. package/lib/component/modal/setting/tanstack_general_settings/tabs/export_tab.fields.d.ts +12 -10
  86. package/lib/component/modal/setting/tanstack_general_settings/tabs/export_tab.fields.d.ts.map +1 -1
  87. package/lib/component/modal/setting/tanstack_general_settings/tabs/export_tab.fields.js +32 -40
  88. package/lib/component/modal/setting/tanstack_general_settings/tabs/export_tab.fields.js.map +1 -1
  89. package/lib/component/modal/setting/tanstack_general_settings/tabs/external_api_tab.d.ts +91 -90
  90. package/lib/component/modal/setting/tanstack_general_settings/tabs/external_api_tab.d.ts.map +1 -1
  91. package/lib/component/modal/setting/tanstack_general_settings/tabs/general_tab.d.ts +91 -90
  92. package/lib/component/modal/setting/tanstack_general_settings/tabs/general_tab.d.ts.map +1 -1
  93. package/lib/component/modal/setting/tanstack_general_settings/tabs/import_filters_tab.d.ts +91 -90
  94. package/lib/component/modal/setting/tanstack_general_settings/tabs/import_filters_tab.d.ts.map +1 -1
  95. package/lib/component/modal/setting/tanstack_general_settings/tabs/nuclei_tab.d.ts +91 -90
  96. package/lib/component/modal/setting/tanstack_general_settings/tabs/nuclei_tab.d.ts.map +1 -1
  97. package/lib/component/modal/setting/tanstack_general_settings/tabs/panels_tab.d.ts +91 -90
  98. package/lib/component/modal/setting/tanstack_general_settings/tabs/panels_tab.d.ts.map +1 -1
  99. package/lib/component/modal/setting/tanstack_general_settings/tabs/spectra_colors_tab.d.ts +91 -90
  100. package/lib/component/modal/setting/tanstack_general_settings/tabs/spectra_colors_tab.d.ts.map +1 -1
  101. package/lib/component/modal/setting/tanstack_general_settings/tabs/spectra_label_tab.d.ts +91 -90
  102. package/lib/component/modal/setting/tanstack_general_settings/tabs/spectra_label_tab.d.ts.map +1 -1
  103. package/lib/component/modal/setting/tanstack_general_settings/tabs/title_block_tab.d.ts +91 -90
  104. package/lib/component/modal/setting/tanstack_general_settings/tabs/title_block_tab.d.ts.map +1 -1
  105. package/lib/component/modal/setting/tanstack_general_settings/tabs/tools_tab.d.ts +91 -90
  106. package/lib/component/modal/setting/tanstack_general_settings/tabs/tools_tab.d.ts.map +1 -1
  107. package/lib/component/modal/setting/tanstack_general_settings/validation/export_tab_validation.d.ts +61 -17
  108. package/lib/component/modal/setting/tanstack_general_settings/validation/export_tab_validation.d.ts.map +1 -1
  109. package/lib/component/modal/setting/tanstack_general_settings/validation/export_tab_validation.js +79 -28
  110. package/lib/component/modal/setting/tanstack_general_settings/validation/export_tab_validation.js.map +1 -1
  111. package/lib/component/modal/setting/tanstack_general_settings/validation.d.ts +15 -15
  112. package/lib/component/panels/filtersPanel/processings_sections_panel.js +1 -1
  113. package/lib/component/panels/filtersPanel/processings_sections_panel.js.map +1 -1
  114. package/lib/component/panels/multipleAnalysisPanel/AlignSpectra.d.ts +3 -3
  115. package/lib/component/panels/multipleAnalysisPanel/AlignSpectra.d.ts.map +1 -1
  116. package/lib/component/panels/multipleAnalysisPanel/AlignSpectra.js +69 -56
  117. package/lib/component/panels/multipleAnalysisPanel/AlignSpectra.js.map +1 -1
  118. package/lib/component/panels/multipleAnalysisPanel/MultipleSpectraAnalysisPanel.js +1 -1
  119. package/lib/component/panels/multipleAnalysisPanel/MultipleSpectraAnalysisPanel.js.map +1 -1
  120. package/lib/component/toolbar/nmr_toolbar.js +1 -1
  121. package/lib/component/toolbar/nmr_toolbar.js.map +1 -1
  122. package/lib/component/utility/core_slots/core_operator_chart_svg.d.ts +8 -0
  123. package/lib/component/utility/core_slots/core_operator_chart_svg.d.ts.map +1 -0
  124. package/lib/component/utility/core_slots/core_operator_chart_svg.js +23 -0
  125. package/lib/component/utility/core_slots/core_operator_chart_svg.js.map +1 -0
  126. package/lib/component/utility/core_slots/core_operator_panel_tool.d.ts +2 -8
  127. package/lib/component/utility/core_slots/core_operator_panel_tool.d.ts.map +1 -1
  128. package/lib/component/utility/core_slots/core_operator_panel_tool.js +8 -6
  129. package/lib/component/utility/core_slots/core_operator_panel_tool.js.map +1 -1
  130. package/lib/component/utility/core_slots/core_operator_tool.commons.d.ts +11 -0
  131. package/lib/component/utility/core_slots/core_operator_tool.commons.d.ts.map +1 -0
  132. package/lib/component/utility/core_slots/core_operator_tool.commons.js +30 -0
  133. package/lib/component/utility/core_slots/core_operator_tool.commons.js.map +1 -0
  134. package/lib/component/utility/core_slots/core_operator_tool.d.ts +2 -8
  135. package/lib/component/utility/core_slots/core_operator_tool.d.ts.map +1 -1
  136. package/lib/component/utility/core_slots/core_operator_tool.js +8 -6
  137. package/lib/component/utility/core_slots/core_operator_tool.js.map +1 -1
  138. package/lib/component/utility/export.d.ts +5 -0
  139. package/lib/component/utility/export.d.ts.map +1 -1
  140. package/lib/component/utility/export.js +22 -0
  141. package/lib/component/utility/export.js.map +1 -1
  142. package/lib/data/data2d/Spectrum2D/contours.d.ts.map +1 -1
  143. package/lib/data/data2d/Spectrum2D/contours.js +23 -10
  144. package/lib/data/data2d/Spectrum2D/contours.js.map +1 -1
  145. package/lib/data/data2d/Spectrum2D/findBestMinContour/estimateNoiseLevel.d.ts +13 -0
  146. package/lib/data/data2d/Spectrum2D/findBestMinContour/estimateNoiseLevel.d.ts.map +1 -0
  147. package/lib/data/data2d/Spectrum2D/findBestMinContour/estimateNoiseLevel.js +40 -0
  148. package/lib/data/data2d/Spectrum2D/findBestMinContour/estimateNoiseLevel.js.map +1 -0
  149. package/lib/data/data2d/Spectrum2D/findBestMinContour/evaluation.d.ts +27 -0
  150. package/lib/data/data2d/Spectrum2D/findBestMinContour/evaluation.d.ts.map +1 -0
  151. package/lib/data/data2d/Spectrum2D/findBestMinContour/evaluation.js +230 -0
  152. package/lib/data/data2d/Spectrum2D/findBestMinContour/evaluation.js.map +1 -0
  153. package/lib/data/data2d/Spectrum2D/findBestMinContour/findAutomaticContourLevels.d.ts +227 -0
  154. package/lib/data/data2d/Spectrum2D/findBestMinContour/findAutomaticContourLevels.d.ts.map +1 -0
  155. package/lib/data/data2d/Spectrum2D/findBestMinContour/findAutomaticContourLevels.js +67 -0
  156. package/lib/data/data2d/Spectrum2D/findBestMinContour/findAutomaticContourLevels.js.map +1 -0
  157. package/lib/data/data2d/Spectrum2D/findBestMinContour/math.d.ts +4 -0
  158. package/lib/data/data2d/Spectrum2D/findBestMinContour/math.d.ts.map +1 -0
  159. package/lib/data/data2d/Spectrum2D/findBestMinContour/math.js +42 -0
  160. package/lib/data/data2d/Spectrum2D/findBestMinContour/math.js.map +1 -0
  161. package/lib/data/utilities/calculateSanPlot.d.ts +3 -1
  162. package/lib/data/utilities/calculateSanPlot.d.ts.map +1 -1
  163. package/lib/data/utilities/calculateSanPlot.js +2 -2
  164. package/lib/data/utilities/calculateSanPlot.js.map +1 -1
  165. package/lib/versionInfo.js +1 -1
  166. package/package.json +5 -5
  167. package/src/component/1d/ApodizationLine.tsx +2 -2
  168. package/src/component/1d/Line.tsx +2 -2
  169. package/src/component/1d/SVGContent1D.tsx +6 -0
  170. package/src/component/1d/baseline/BaselinePreview.tsx +2 -2
  171. package/src/component/1d/database/DatabaseSpectrum.tsx +2 -2
  172. package/src/component/1d/database/ResurrectedDatabaseRanges.tsx +2 -2
  173. package/src/component/1d/matrix/Boxplot.tsx +5 -5
  174. package/src/component/1d/matrix/Stocsy.tsx +2 -2
  175. package/src/component/1d/peaks/PeakAnnotationsSpreadMode.tsx +17 -3
  176. package/src/component/1d/peaks/usePeakShapesPath.ts +2 -2
  177. package/src/component/1d/plugin_svg_chart.tsx +60 -0
  178. package/src/component/1d-2d/components/RangeIndicator.tsx +8 -9
  179. package/src/component/2d/1d-tracer/HorizontalSliceChart.tsx +2 -2
  180. package/src/component/2d/1d-tracer/Left1DChart.tsx +2 -2
  181. package/src/component/2d/1d-tracer/Top1DChart.tsx +2 -2
  182. package/src/component/2d/1d-tracer/VerticalSliceChart.tsx +2 -2
  183. package/src/component/2d/1d-tracer/phase-correction-traces/SpectrumPhaseTrace.tsx +3 -3
  184. package/src/component/2d/ft/Contours.tsx +2 -2
  185. package/src/component/context/ScaleContext.tsx +1 -1
  186. package/src/component/elements/D3Axis.tsx +49 -67
  187. package/src/component/elements/export/ExportContent.tsx +3 -2
  188. package/src/component/elements/export/ExportOptionsModal.tsx +32 -1
  189. package/src/component/elements/export/useExportConfigurer.tsx +6 -0
  190. package/src/component/elements/print/PrintContent.tsx +6 -27
  191. package/src/component/elements/utilities/transferStyles.ts +32 -0
  192. package/src/component/hooks/useIntegralPath.ts +2 -2
  193. package/src/component/modal/setting/tanstack_general_settings/tabs/export_tab.fields.tsx +47 -57
  194. package/src/component/modal/setting/tanstack_general_settings/validation/export_tab_validation.ts +100 -28
  195. package/src/component/panels/filtersPanel/processings_sections_panel.tsx +1 -1
  196. package/src/component/panels/multipleAnalysisPanel/AlignSpectra.tsx +128 -98
  197. package/src/component/panels/multipleAnalysisPanel/MultipleSpectraAnalysisPanel.tsx +1 -1
  198. package/src/component/toolbar/nmr_toolbar.tsx +1 -1
  199. package/src/component/utility/core_slots/core_operator_chart_svg.tsx +61 -0
  200. package/src/component/utility/core_slots/core_operator_panel_tool.tsx +9 -27
  201. package/src/component/utility/core_slots/core_operator_tool.commons.ts +63 -0
  202. package/src/component/utility/core_slots/core_operator_tool.tsx +9 -27
  203. package/src/component/utility/export.ts +34 -0
  204. package/src/data/data2d/Spectrum2D/contours.ts +40 -14
  205. package/src/data/data2d/Spectrum2D/findBestMinContour/estimateNoiseLevel.ts +67 -0
  206. package/src/data/data2d/Spectrum2D/findBestMinContour/evaluation.ts +432 -0
  207. package/src/data/data2d/Spectrum2D/findBestMinContour/findAutomaticContourLevels.ts +431 -0
  208. package/src/data/data2d/Spectrum2D/findBestMinContour/math.ts +59 -0
  209. package/src/data/utilities/calculateSanPlot.ts +2 -1
  210. package/src/versionInfo.ts +1 -1
  211. package/lib/component/elements/export/utilities/transferDocumentStyles.d.ts +0 -2
  212. package/lib/component/elements/export/utilities/transferDocumentStyles.d.ts.map +0 -1
  213. package/lib/component/elements/export/utilities/transferDocumentStyles.js +0 -21
  214. package/lib/component/elements/export/utilities/transferDocumentStyles.js.map +0 -1
  215. package/lib/component/utility/PathBuilder.d.ts +0 -10
  216. package/lib/component/utility/PathBuilder.d.ts.map +0 -1
  217. package/lib/component/utility/PathBuilder.js +0 -38
  218. package/lib/component/utility/PathBuilder.js.map +0 -1
  219. package/src/component/elements/export/utilities/transferDocumentStyles.ts +0 -21
  220. package/src/component/utility/PathBuilder.ts +0 -41
@@ -1,41 +1,23 @@
1
- import type { ProcessingOperatorId } from '@zakodium/nmr-types';
2
- import type {
3
- ProcessingOperatorUI,
4
- ProcessingOperatorUIToolProps,
5
- } from '@zakodium/nmrium-core';
6
- import type { PartialPick } from '@zakodium/utils';
7
1
  import { ErrorBoundary } from 'react-error-boundary';
8
2
 
9
- import { useCore } from '../../context/CoreContext.tsx';
10
3
  import { ToolbarItemError } from '../toolbar_item_error.tsx';
11
4
 
12
- interface CoreOperatorToolProps {
13
- operator: ProcessingOperatorUI<ProcessingOperatorId>;
14
- }
5
+ import type { CoreOperatorToolProps } from './core_operator_tool.commons.ts';
6
+ import { useToolProps } from './core_operator_tool.commons.ts';
15
7
 
16
- export function CoreOperatorTool(
17
- props: PartialPick<
18
- Omit<ProcessingOperatorUIToolProps<ProcessingOperatorId>, 'core'>,
19
- 'spectrum'
20
- > &
21
- CoreOperatorToolProps,
22
- ) {
23
- const { spectrum, activeOperatorId, onTriggerOperation, operator } = props;
24
- const core = useCore();
8
+ export function CoreOperatorTool(props: CoreOperatorToolProps) {
9
+ const { spectrum, operatorUI } = props;
10
+ const toolProps = useToolProps(props);
25
11
 
26
12
  if (!spectrum) return null;
27
- if (!operator.Tool) return null;
13
+ if (!toolProps) return null;
14
+ if (!operatorUI.Tool) return null;
28
15
 
29
- const { Tool } = operator;
16
+ const { Tool } = operatorUI;
30
17
 
31
18
  return (
32
19
  <ErrorBoundary FallbackComponent={ToolbarItemError}>
33
- <Tool
34
- core={core}
35
- spectrum={spectrum}
36
- activeOperatorId={activeOperatorId}
37
- onTriggerOperation={onTriggerOperation}
38
- />
20
+ <Tool {...toolProps} />
39
21
  </ErrorBoundary>
40
22
  );
41
23
  }
@@ -139,6 +139,39 @@ interface CreateCanvasByChunksOptions {
139
139
  scaleFactor?: number;
140
140
  }
141
141
 
142
+ /**
143
+ * Browsers limits.
144
+ */
145
+ export const MAX_CANVAS_SIDE = 32_767;
146
+ export const MAX_CANVAS_AREA = 268_435_456;
147
+
148
+ function formatPixels(value: number) {
149
+ return Math.round(value).toLocaleString('en-US');
150
+ }
151
+
152
+ /** throw error if canvas size exceeds browsers limits */
153
+ function assertCanvasSize(width: number, height: number) {
154
+ const size = `${formatPixels(width)} x ${formatPixels(height)} px`;
155
+
156
+ if (width < 1 || height < 1) {
157
+ throw new Error(
158
+ `Export size is too small: ${size}. Increase the size or the DPI.`,
159
+ );
160
+ }
161
+
162
+ if (width > MAX_CANVAS_SIDE || height > MAX_CANVAS_SIDE) {
163
+ throw new Error(
164
+ `Export size is too large: ${size}. Each side must be at most ${formatPixels(MAX_CANVAS_SIDE)} px. Reduce the size or the DPI.`,
165
+ );
166
+ }
167
+
168
+ if (width * height > MAX_CANVAS_AREA) {
169
+ throw new Error(
170
+ `Export size is too large: ${size} is over the ${formatPixels(MAX_CANVAS_AREA)} px limit. Reduce the size or the DPI.`,
171
+ );
172
+ }
173
+ }
174
+
142
175
  interface DrawImageOptions {
143
176
  context: OffscreenCanvasRenderingContext2D;
144
177
  chunkX: number;
@@ -203,6 +236,7 @@ async function createCanvasByChunks(
203
236
  context: OffscreenCanvasRenderingContext2D;
204
237
  }> {
205
238
  const { width, height, scaleFactor = 1, chunkSize = 512 } = options;
239
+ assertCanvasSize(width, height);
206
240
  const canvas = new OffscreenCanvas(width, height);
207
241
  canvas.width = width;
208
242
  canvas.height = height;
@@ -2,10 +2,12 @@ import type { Spectrum2D, Spectrum } from '@zakodium/nmrium-core';
2
2
  import { isSpectrum2DFt } from '@zakodium/nmrium-core';
3
3
  import type { NmrData2DFt } from 'cheminfo-types';
4
4
  import { Conrec } from 'ml-conrec';
5
- import { xMaxAbsoluteValue } from 'ml-spectra-processing';
5
+ import { matrixToArray } from 'ml-spectra-processing';
6
6
 
7
7
  import type { SpectrumFTData } from '../../../component/hooks/use2DReducer.tsx';
8
- import { calculateSanPlot } from '../../utilities/calculateSanPlot.js';
8
+
9
+ import { estimateNoiseLevel } from './findBestMinContour/estimateNoiseLevel.js';
10
+ import { findAutomaticContourLevels } from './findBestMinContour/findAutomaticContourLevels.ts';
9
11
 
10
12
  interface Level {
11
13
  positive: ContourItem;
@@ -26,6 +28,7 @@ interface BaseWheelOptions {
26
28
  altKey: boolean;
27
29
  invertScroll?: boolean;
28
30
  }
31
+
29
32
  interface WheelOptions extends BaseWheelOptions {
30
33
  contourOptions: ContourOptions;
31
34
  }
@@ -40,6 +43,7 @@ const DEFAULT_CONTOURS_OPTIONS: ContourOptions = {
40
43
  numberOfLayers: 10,
41
44
  },
42
45
  };
46
+
43
47
  type LevelSign = keyof Level;
44
48
 
45
49
  const LEVEL_SIGNS: Readonly<[LevelSign, LevelSign]> = ['positive', 'negative'];
@@ -56,21 +60,43 @@ function getDefaultContoursLevel(spectrum: Spectrum2D, quadrant = 'rr') {
56
60
  // @ts-expect-error type of NmrData2D should have a discriminator field to separate fid and ft
57
61
  const quadrantData = data[quadrant];
58
62
 
59
- //@ts-expect-error will be included in nexts versions
60
- const { noise = calculateSanPlot('2D', quadrantData) } = info;
61
-
62
- const { positive = 0, negative = 0 } = noise;
63
- const max = Math.max(
64
- Math.abs(quadrantData.minZ),
65
- Math.abs(quadrantData.maxZ),
63
+ const {
64
+ experiment = '',
65
+ // @ts-expect-error will be included in nexts versions
66
+ noise,
67
+ } = info;
68
+ const matrix = matrixToArray(quadrantData.z);
69
+
70
+ const noiseLevel = (noise as { positive: number; negative: number })
71
+ ? Math.max(noise.positive, noise.negative)
72
+ : estimateNoiseLevel(matrix);
73
+
74
+ const bestMinLevel = findAutomaticContourLevels(
75
+ matrix,
76
+ quadrantData.z.length,
77
+ quadrantData.z[0].length,
78
+ noiseLevel,
79
+ {
80
+ maxFdr: 0.005,
81
+ diagnostics: false,
82
+ maxOccupancy: 0.005,
83
+ persistenceLevels: 5,
84
+ contourRatio: 1.8,
85
+ minPersistence: 0.5,
86
+ maxVerticalRidgeScore: 0.05,
87
+ maxHorizontalRidgeScore: 0.05,
88
+ ridgeCoverageThreshold: experiment.includes('jres') ? 0.9 : 0.1,
89
+ },
66
90
  );
67
91
 
68
- const minAbsPeakBase = 0.005 * max;
69
- const minAllowed = 3 * xMaxAbsoluteValue([positive, negative]);
70
-
71
- const minLevel = Math.max(minAbsPeakBase, minAllowed);
92
+ const { hasT1Noise, minLevelWithoutT1Noise, minLevel, maxAbsoluteValue } =
93
+ bestMinLevel;
72
94
  const minContourLevel = Math.min(
73
- calculateValueOfLevel(minLevel, max, true),
95
+ calculateValueOfLevel(
96
+ hasT1Noise ? minLevelWithoutT1Noise : minLevel,
97
+ maxAbsoluteValue,
98
+ true,
99
+ ),
74
100
  DEFAULT_CONTOURS_OPTIONS.positive.contourLevels[1] -
75
101
  DEFAULT_CONTOURS_OPTIONS.positive.numberOfLayers,
76
102
  );
@@ -0,0 +1,67 @@
1
+ import { xMedian } from 'ml-spectra-processing';
2
+
3
+ const GAUSSIAN_MAD_SCALE = 1 / 0.6744897501960817;
4
+ const DEFAULT_MAX_SAMPLES = 65_536;
5
+
6
+ export interface EstimateNoiseLevelOptions {
7
+ /**
8
+ * Maximum number of evenly spaced finite values used for estimation.
9
+ * @default 65536
10
+ */
11
+ maxSamples?: number;
12
+ }
13
+
14
+ /**
15
+ * Estimates the standard deviation of signed Gaussian noise using a
16
+ * deterministically downsampled median absolute deviation.
17
+ */
18
+ export function estimateNoiseLevel(
19
+ matrix: ArrayLike<number>,
20
+ options: EstimateNoiseLevelOptions = {},
21
+ ): number {
22
+ const { maxSamples = DEFAULT_MAX_SAMPLES } = options;
23
+
24
+ if (!Number.isInteger(maxSamples) || maxSamples <= 0) {
25
+ throw new Error('maxSamples must be a positive integer');
26
+ }
27
+
28
+ const sample = collectFiniteSample(matrix, maxSamples);
29
+
30
+ if (sample.length === 0) {
31
+ throw new Error('matrix must contain at least one finite value');
32
+ }
33
+
34
+ const median = xMedian(sample);
35
+
36
+ if (median === 0) {
37
+ return 1; //assume NUS reconstruction.
38
+ }
39
+
40
+ const averageDeviations = new Float64Array(sample.length);
41
+ for (let i = 0; i < sample.length; i++) {
42
+ averageDeviations[i] = Math.abs(sample[i] - median);
43
+ }
44
+
45
+ const noiseLevel = xMedian(averageDeviations) * GAUSSIAN_MAD_SCALE;
46
+
47
+ if (!Number.isFinite(noiseLevel) || noiseLevel < 0) {
48
+ throw new Error('unable to estimate a positive noise level');
49
+ }
50
+
51
+ return noiseLevel;
52
+ }
53
+
54
+ function collectFiniteSample(
55
+ matrix: ArrayLike<number>,
56
+ maxSamples: number,
57
+ ): Float64Array {
58
+ const step = Math.max(1, Math.ceil(matrix.length / maxSamples));
59
+ const sample = new Float64Array(Math.ceil(matrix.length / step));
60
+ let sampleIndex = 0;
61
+
62
+ for (let index = 0; index < matrix.length; index += step) {
63
+ sample[sampleIndex++] = matrix[index];
64
+ }
65
+
66
+ return sample;
67
+ }
@@ -0,0 +1,432 @@
1
+ import { gaussianTwoSidedTail, violation } from './math.js';
2
+
3
+ export interface EvaluatedThreshold {
4
+ sigmaMultiplier: number;
5
+
6
+ activePixels: number;
7
+ occupancy: number;
8
+ fdr: number;
9
+ persistence: number;
10
+
11
+ verticalRidgeScore: number;
12
+ horizontalRidgeScore: number;
13
+ }
14
+
15
+ export interface AnalysisMatrix {
16
+ values: Float64Array;
17
+ rows: number;
18
+ cols: number;
19
+ maxAbsoluteValue: number;
20
+ }
21
+
22
+ interface RidgeScores {
23
+ vertical: number;
24
+ horizontal: number;
25
+ }
26
+
27
+ export interface ThresholdIndex {
28
+ countAtLeast(threshold: number): number;
29
+ }
30
+
31
+ interface ThresholdMetrics {
32
+ activePixels: number;
33
+ ridge: RidgeScores;
34
+ }
35
+
36
+ export function createThresholdIndex(matrix: Float64Array): ThresholdIndex {
37
+ const sortedValues = new Float64Array(matrix.length);
38
+ let valueCount = 0;
39
+
40
+ for (const value of matrix) {
41
+ if (!Number.isNaN(value)) {
42
+ sortedValues[valueCount++] = value;
43
+ }
44
+ }
45
+
46
+ const values = sortedValues.subarray(0, valueCount);
47
+ values.sort();
48
+
49
+ return {
50
+ countAtLeast(threshold) {
51
+ if (Number.isNaN(threshold)) return 0;
52
+
53
+ let low = 0;
54
+ let high = values.length;
55
+
56
+ while (low < high) {
57
+ const middle = low + Math.floor((high - low) / 2);
58
+
59
+ if (values[middle] < threshold) {
60
+ low = middle + 1;
61
+ } else {
62
+ high = middle;
63
+ }
64
+ }
65
+
66
+ return values.length - low;
67
+ },
68
+ };
69
+ }
70
+
71
+ function evaluateThresholdMetrics(
72
+ matrix: Float64Array,
73
+ rows: number,
74
+ cols: number,
75
+ threshold: number,
76
+ ridgeCoverageThreshold: number,
77
+ includeRidgeScores: boolean,
78
+ thresholdIndex?: ThresholdIndex,
79
+ ): ThresholdMetrics {
80
+ if (!includeRidgeScores && thresholdIndex) {
81
+ return {
82
+ activePixels: thresholdIndex.countAtLeast(threshold),
83
+ ridge: { vertical: 0, horizontal: 0 },
84
+ };
85
+ }
86
+
87
+ const rowCounts = includeRidgeScores ? new Uint32Array(rows) : undefined;
88
+ const colCounts = includeRidgeScores ? new Uint32Array(cols) : undefined;
89
+ let activePixels = 0;
90
+
91
+ for (let row = 0; row < rows; row++) {
92
+ for (let col = 0; col < cols; col++) {
93
+ const index = row * cols + col;
94
+
95
+ if (!(matrix[index] >= threshold)) continue;
96
+
97
+ activePixels++;
98
+ if (rowCounts && colCounts) {
99
+ rowCounts[row]++;
100
+ colCounts[col]++;
101
+ }
102
+ }
103
+ }
104
+
105
+ const ridge =
106
+ rowCounts && colCounts
107
+ ? calculateRidgeScores(
108
+ rowCounts,
109
+ colCounts,
110
+ rows,
111
+ cols,
112
+ activePixels,
113
+ ridgeCoverageThreshold,
114
+ )
115
+ : { vertical: 0, horizontal: 0 };
116
+
117
+ return { activePixels, ridge };
118
+ }
119
+
120
+ export function evaluateThreshold(
121
+ matrix: Float64Array,
122
+ rows: number,
123
+ cols: number,
124
+ noiseLevel: number,
125
+ sigmaMultiplier: number,
126
+ contourRatio: number,
127
+ persistenceLevels: number,
128
+ ridgeCoverageThreshold: number,
129
+ includeRidgeScores = true,
130
+ thresholdIndex?: ThresholdIndex,
131
+ ): EvaluatedThreshold {
132
+ const threshold = sigmaMultiplier * noiseLevel;
133
+ const { activePixels, ridge } = evaluateThresholdMetrics(
134
+ matrix,
135
+ rows,
136
+ cols,
137
+ threshold,
138
+ ridgeCoverageThreshold,
139
+ includeRidgeScores,
140
+ thresholdIndex,
141
+ );
142
+ const totalPixels = rows * cols;
143
+ const occupancy = activePixels / totalPixels;
144
+
145
+ if (activePixels === 0) {
146
+ return {
147
+ sigmaMultiplier,
148
+ activePixels: 0,
149
+ occupancy: 0,
150
+ fdr: 0,
151
+ persistence: 0,
152
+ verticalRidgeScore: 0,
153
+ horizontalRidgeScore: 0,
154
+ };
155
+ }
156
+
157
+ const expectedNoisePixels =
158
+ totalPixels * gaussianTwoSidedTail(sigmaMultiplier);
159
+ const fdr = Math.min(1, expectedNoisePixels / activePixels);
160
+ const persistence = computePersistence(
161
+ matrix,
162
+ rows,
163
+ cols,
164
+ threshold,
165
+ contourRatio,
166
+ persistenceLevels,
167
+ thresholdIndex,
168
+ );
169
+
170
+ return {
171
+ sigmaMultiplier,
172
+ activePixels,
173
+ occupancy,
174
+ fdr,
175
+ persistence,
176
+ verticalRidgeScore: ridge.vertical,
177
+ horizontalRidgeScore: ridge.horizontal,
178
+ };
179
+ }
180
+
181
+ export function evaluateFallbackThreshold(
182
+ evaluations: EvaluatedThreshold[],
183
+ maxFdr: number,
184
+ maxOccupancy: number,
185
+ minPersistence: number,
186
+ ): EvaluatedThreshold {
187
+ let best: EvaluatedThreshold | undefined;
188
+ let bestScore = Infinity;
189
+
190
+ for (const evaluation of evaluations) {
191
+ const score =
192
+ violation(evaluation.fdr, maxFdr) * 4 +
193
+ violation(evaluation.occupancy, maxOccupancy) * 2 +
194
+ violation(minPersistence, evaluation.persistence) * 2;
195
+
196
+ if (score < bestScore) {
197
+ bestScore = score;
198
+ best = evaluation;
199
+ }
200
+ }
201
+
202
+ if (!best) {
203
+ throw new Error('Unable to evaluate contour thresholds');
204
+ }
205
+
206
+ return best;
207
+ }
208
+
209
+ export function findRidgeFreeThresholdTopDown(
210
+ matrix: Float64Array,
211
+ rows: number,
212
+ cols: number,
213
+ noiseLevel: number,
214
+ minLevel: number,
215
+ maxLevel: number,
216
+ contourRatio: number,
217
+ maxVerticalRidgeScore: number,
218
+ maxHorizontalRidgeScore: number,
219
+ ridgeCoverageThreshold: number,
220
+ ridgePersistenceLevels = 1,
221
+ ): number {
222
+ let level = maxLevel;
223
+ let lastCleanLevel = level;
224
+ let consecutiveRidgeLevels = 0;
225
+
226
+ while (level >= minLevel) {
227
+ const ridge = evaluateRidgeScores(
228
+ matrix,
229
+ rows,
230
+ cols,
231
+ (level / noiseLevel) * noiseLevel,
232
+ ridgeCoverageThreshold,
233
+ );
234
+
235
+ const hasRidge =
236
+ ridge.vertical > maxVerticalRidgeScore ||
237
+ ridge.horizontal > maxHorizontalRidgeScore;
238
+
239
+ if (hasRidge) {
240
+ consecutiveRidgeLevels++;
241
+
242
+ if (consecutiveRidgeLevels >= ridgePersistenceLevels) {
243
+ return lastCleanLevel;
244
+ }
245
+ } else {
246
+ consecutiveRidgeLevels = 0;
247
+ lastCleanLevel = level;
248
+ }
249
+
250
+ level /= contourRatio;
251
+ }
252
+
253
+ return lastCleanLevel;
254
+ }
255
+
256
+ export function generateCandidateSigmaLevels(
257
+ minSigma: number,
258
+ maxSigma: number,
259
+ contourRatio: number,
260
+ exponent: number,
261
+ ): number[] {
262
+ const step = contourRatio ** exponent;
263
+ const candidates: number[] = [];
264
+
265
+ let sigma = minSigma;
266
+
267
+ while (sigma <= maxSigma * (1 + 1e-12)) {
268
+ candidates.push(sigma);
269
+ sigma *= step;
270
+ }
271
+
272
+ return candidates;
273
+ }
274
+
275
+ export function computePersistence(
276
+ matrix: Float64Array,
277
+ rows: number,
278
+ cols: number,
279
+ initialThreshold: number,
280
+ contourRatio: number,
281
+ levels: number,
282
+ thresholdIndex?: ThresholdIndex,
283
+ ): number {
284
+ let threshold = initialThreshold;
285
+ let previousArea = thresholdIndex
286
+ ? thresholdIndex.countAtLeast(threshold)
287
+ : countValuesAboveThreshold(matrix, threshold);
288
+
289
+ if (previousArea === 0) {
290
+ return 0;
291
+ }
292
+
293
+ let minimumPersistence = 1;
294
+
295
+ for (let level = 1; level < levels; level++) {
296
+ threshold *= contourRatio;
297
+
298
+ const currentArea = thresholdIndex
299
+ ? thresholdIndex.countAtLeast(threshold)
300
+ : countValuesAboveThreshold(matrix, threshold);
301
+
302
+ if (currentArea === 0) {
303
+ return 0;
304
+ }
305
+
306
+ const persistence = currentArea / previousArea;
307
+ minimumPersistence = Math.min(minimumPersistence, persistence);
308
+ previousArea = currentArea;
309
+ }
310
+
311
+ return minimumPersistence;
312
+ }
313
+
314
+ function calculateRidgeScores(
315
+ rowCounts: Uint32Array,
316
+ colCounts: Uint32Array,
317
+ rows: number,
318
+ cols: number,
319
+ activePixels: number,
320
+ coverageThreshold: number,
321
+ ): RidgeScores {
322
+ if (activePixels === 0) {
323
+ return {
324
+ vertical: 0,
325
+ horizontal: 0,
326
+ };
327
+ }
328
+
329
+ let verticalRidgePixels = 0;
330
+
331
+ for (let col = 0; col < cols; col++) {
332
+ const coverage = colCounts[col] / rows;
333
+
334
+ if (coverage >= coverageThreshold) {
335
+ verticalRidgePixels += colCounts[col];
336
+ }
337
+ }
338
+
339
+ let horizontalRidgePixels = 0;
340
+
341
+ for (let row = 0; row < rows; row++) {
342
+ const coverage = rowCounts[row] / cols;
343
+
344
+ if (coverage >= coverageThreshold) {
345
+ horizontalRidgePixels += rowCounts[row];
346
+ }
347
+ }
348
+
349
+ return {
350
+ vertical: verticalRidgePixels / activePixels,
351
+ horizontal: horizontalRidgePixels / activePixels,
352
+ };
353
+ }
354
+
355
+ function evaluateRidgeScores(
356
+ matrix: Float64Array,
357
+ rows: number,
358
+ cols: number,
359
+ threshold: number,
360
+ coverageThreshold: number,
361
+ ): RidgeScores {
362
+ return evaluateThresholdMetrics(
363
+ matrix,
364
+ rows,
365
+ cols,
366
+ threshold,
367
+ coverageThreshold,
368
+ true,
369
+ ).ridge;
370
+ }
371
+
372
+ export function maxPoolAbsolute(
373
+ matrix: ArrayLike<number>,
374
+ rows: number,
375
+ cols: number,
376
+ maxDimension: number,
377
+ ): AnalysisMatrix {
378
+ const scale = Math.max(1, Math.ceil(Math.max(rows, cols) / maxDimension));
379
+ const analysisRows = Math.ceil(rows / scale);
380
+ const analysisCols = Math.ceil(cols / scale);
381
+ const values = new Float64Array(analysisRows * analysisCols);
382
+ let maxAbsoluteValue = 0;
383
+
384
+ for (let analysisRow = 0; analysisRow < analysisRows; analysisRow++) {
385
+ const sourceRowStart = analysisRow * scale;
386
+ const sourceRowEnd = Math.min(rows, sourceRowStart + scale);
387
+
388
+ for (let analysisCol = 0; analysisCol < analysisCols; analysisCol++) {
389
+ const sourceColStart = analysisCol * scale;
390
+ const sourceColEnd = Math.min(cols, sourceColStart + scale);
391
+
392
+ let maximum = 0;
393
+
394
+ for (let row = sourceRowStart; row < sourceRowEnd; row++) {
395
+ const offset = row * cols;
396
+
397
+ for (let col = sourceColStart; col < sourceColEnd; col++) {
398
+ const value = Math.abs(matrix[offset + col]);
399
+
400
+ if (Number.isFinite(value) && value > maximum) {
401
+ maximum = value;
402
+ }
403
+ }
404
+ }
405
+
406
+ values[analysisRow * analysisCols + analysisCol] = maximum;
407
+ maxAbsoluteValue = Math.max(maxAbsoluteValue, maximum);
408
+ }
409
+ }
410
+
411
+ return {
412
+ values,
413
+ rows: analysisRows,
414
+ cols: analysisCols,
415
+ maxAbsoluteValue: maxAbsoluteValue * 0.95,
416
+ };
417
+ }
418
+
419
+ export function countValuesAboveThreshold(
420
+ matrix: Float64Array,
421
+ threshold: number,
422
+ ): number {
423
+ let count = 0;
424
+
425
+ for (const value of matrix) {
426
+ if (value >= threshold) {
427
+ count++;
428
+ }
429
+ }
430
+
431
+ return count;
432
+ }