muvis-align 0.7.1__py3-none-any.whl

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 (64) hide show
  1. muvis_align/GlobalOptProgress.py +169 -0
  2. muvis_align/MVSRegistration.py +2594 -0
  3. muvis_align/Pipeline.py +141 -0
  4. muvis_align/Timer.py +59 -0
  5. muvis_align/__init__.py +11 -0
  6. muvis_align/_sample_data.py +98 -0
  7. muvis_align/_version.py +24 -0
  8. muvis_align/_widget.py +128 -0
  9. muvis_align/constants.py +191 -0
  10. muvis_align/file/project_yaml.py +41 -0
  11. muvis_align/file/resources.py +15 -0
  12. muvis_align/file/transforms.py +37 -0
  13. muvis_align/fusion_blocks.py +631 -0
  14. muvis_align/fusion_methods/FusionMethod.py +7 -0
  15. muvis_align/fusion_methods/FusionMethodAdditive.py +20 -0
  16. muvis_align/fusion_methods/FusionMethodAverage.py +25 -0
  17. muvis_align/fusion_methods/FusionMethodExclusive.py +25 -0
  18. muvis_align/image/ImageSource.py +340 -0
  19. muvis_align/image/TiffImageSource.py +85 -0
  20. muvis_align/image/Video.py +31 -0
  21. muvis_align/image/ZarrImageSource.py +111 -0
  22. muvis_align/image/color_conversion.py +23 -0
  23. muvis_align/image/flatfield.py +99 -0
  24. muvis_align/image/lazy_overview.py +389 -0
  25. muvis_align/image/ome_helper.py +56 -0
  26. muvis_align/image/ome_tiff_helper.py +391 -0
  27. muvis_align/image/ome_zarr_helper.py +323 -0
  28. muvis_align/image/ome_zarr_util.py +173 -0
  29. muvis_align/image/reg_util.py +47 -0
  30. muvis_align/image/source_helper.py +95 -0
  31. muvis_align/image/util.py +2549 -0
  32. muvis_align/logging.py +66 -0
  33. muvis_align/metrics.py +373 -0
  34. muvis_align/muvis-align-logo256.png +0 -0
  35. muvis_align/napari.yaml +43 -0
  36. muvis_align/registration_methods/RegistrationMethod.py +40 -0
  37. muvis_align/registration_methods/RegistrationMethodCPD.py +80 -0
  38. muvis_align/registration_methods/RegistrationMethodCvFeatures.py +125 -0
  39. muvis_align/registration_methods/RegistrationMethodDummy.py +17 -0
  40. muvis_align/registration_methods/RegistrationMethodSkFeatures.py +223 -0
  41. muvis_align/robust_resolution.py +179 -0
  42. muvis_align/split_registration.py +205 -0
  43. muvis_align/ui/Interface.py +2263 -0
  44. muvis_align/ui/MagicColorPicker.py +91 -0
  45. muvis_align/ui/ModelTable.py +115 -0
  46. muvis_align/ui/NapariDaskProgress.py +32 -0
  47. muvis_align/ui/NapariMVSProgress.py +113 -0
  48. muvis_align/ui/NapariPhaseProgress.py +404 -0
  49. muvis_align/ui/ParamWidget.py +84 -0
  50. muvis_align/ui/PathList.py +122 -0
  51. muvis_align/ui/QtNapariTqdm.py +69 -0
  52. muvis_align/ui/__init__.py +0 -0
  53. muvis_align/ui/_utils.py +241 -0
  54. muvis_align/ui/bilayers_util.py +132 -0
  55. muvis_align/ui/create_widgets.py +225 -0
  56. muvis_align/ui/project_template.yaml +616 -0
  57. muvis_align/util.py +1433 -0
  58. muvis_align/zarr_compat.py +69 -0
  59. muvis_align-0.7.1.dist-info/METADATA +174 -0
  60. muvis_align-0.7.1.dist-info/RECORD +64 -0
  61. muvis_align-0.7.1.dist-info/WHEEL +5 -0
  62. muvis_align-0.7.1.dist-info/entry_points.txt +2 -0
  63. muvis_align-0.7.1.dist-info/licenses/LICENSE.md +7 -0
  64. muvis_align-0.7.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,169 @@
1
+ """Progress for multiview_stitcher's global optimization, read out of its own debug log.
2
+
3
+ groupwise_resolution() is one long blocking call with nothing to hook (no dask tasks, no tqdm), but it logs
4
+ each inner-loop iteration. The bar counts outer-loop passes, each removing one edge, so the edge count bounds
5
+ them; a pass also advances with its iterations. Headless, the progress and residual are logged every
6
+ `heartbeat_seconds`, a falling residual telling slow from stuck.
7
+ """
8
+ import logging
9
+ import time
10
+
11
+ from muvis_align.util import raise_if_cancelled
12
+
13
+
14
+ # upstream's logger and messages: if they change, this degrades to a single phase rather than failing
15
+ GLOBAL_OPT_LOGGER = 'multiview_stitcher.param_resolution.global_optimization'
16
+ ITERATION_MESSAGE = 'Glob opt iter %s, node %s, mean residual %s, max residual %s'
17
+ MAX_ITER_MESSAGE = 'Global optimization: setting max_iter to %s'
18
+ FINISHED_MESSAGE = 'Finished glob opt. Max and mean residuals: %s \t %s'
19
+
20
+ DEFAULT_MAX_ITER = 500
21
+
22
+
23
+ class GlobalOptProgress:
24
+ """Reports multiview_stitcher's global optimization into `progress_factory`'s bar, or only the log without one."""
25
+
26
+ heartbeat_seconds = 30
27
+
28
+ def __init__(self, progress_factory=None, desc='Global registration', max_passes=None,
29
+ max_iter=None, weight=1, heartbeat_seconds=None):
30
+ self.progress_factory = progress_factory
31
+ self.desc = desc
32
+ self.max_passes = max(int(max_passes), 1) if max_passes else None
33
+ self.max_iter = max_iter or DEFAULT_MAX_ITER
34
+ self.weight = weight
35
+ if heartbeat_seconds is not None:
36
+ self.heartbeat_seconds = heartbeat_seconds
37
+ self._logger = None
38
+ self._handler = None
39
+ self._prior_level = None
40
+ self._forward_level = logging.NOTSET
41
+ self._prior_propagate = None
42
+ self._phase = None
43
+ self._pass = 0
44
+ self._iteration = None
45
+ self._reported = 0.0
46
+ self._reported_at = None
47
+ self._saw_iterations = False
48
+
49
+ def __enter__(self):
50
+ self._logger = logging.getLogger(GLOBAL_OPT_LOGGER)
51
+ # debug level is needed for the iteration lines, so propagation is off and forward() passes on
52
+ # only what the level in force would have shown
53
+ self._prior_level = self._logger.level
54
+ self._forward_level = self._logger.getEffectiveLevel()
55
+ self._prior_propagate = self._logger.propagate
56
+ self._handler = _RecordListener(self)
57
+ self._logger.addHandler(self._handler)
58
+ self._logger.setLevel(logging.DEBUG)
59
+ self._logger.propagate = False
60
+ self._pass = 1
61
+ self._saw_iterations = False
62
+ return self
63
+
64
+ def __exit__(self, exc_type, exc_value, traceback):
65
+ if self._logger is not None:
66
+ self._logger.removeHandler(self._handler)
67
+ self._logger.setLevel(self._prior_level)
68
+ self._logger.propagate = self._prior_propagate
69
+ self._handler = None
70
+ if not self._saw_iterations and exc_type is None:
71
+ # nothing recognised: still cross one phase
72
+ self._open_phase()
73
+ self._end_phase(exc_type)
74
+ return False
75
+
76
+ def forward(self, record):
77
+ if record.levelno < self._forward_level:
78
+ return
79
+ parent = self._logger.parent if self._logger is not None else None
80
+ if parent is not None:
81
+ parent.handle(record)
82
+
83
+ def note_max_iter(self, max_iter):
84
+ try:
85
+ self.max_iter = int(max_iter)
86
+ except (TypeError, ValueError):
87
+ pass
88
+
89
+ def note_iteration(self, iteration, max_residual=None):
90
+ try:
91
+ iteration = int(iteration)
92
+ except (TypeError, ValueError):
93
+ return
94
+ if self._iteration is not None and iteration <= self._iteration:
95
+ # the count restarting means the outer loop dropped an edge and began again
96
+ self._pass += 1
97
+ self._iteration = iteration
98
+ self._saw_iterations = True
99
+ self._open_phase()
100
+ self._advance(self._pass - 1 + min((iteration + 1) / self.max_iter, 1.0))
101
+ self._log_heartbeat(max_residual)
102
+
103
+ def note_finished(self):
104
+ if self._saw_iterations:
105
+ self._advance(self._pass)
106
+
107
+ def _open_phase(self):
108
+ # opened at the first iteration: max_iter is only logged after __enter__
109
+ if self._phase is not None or self.progress_factory is None:
110
+ return
111
+ self._phase = self.progress_factory(total=self.max_passes, desc=self.desc,
112
+ weight=self.weight)
113
+ self._phase.__enter__()
114
+
115
+ def _end_phase(self, exc_type):
116
+ if self._phase is not None:
117
+ self._phase.__exit__(exc_type, None, None)
118
+ self._phase = None
119
+
120
+ def _advance(self, progress):
121
+ """Move the phase to `progress` passes done, which only ever grows."""
122
+ progress = max(progress, self._reported)
123
+ if self.max_passes is not None:
124
+ progress = min(progress, self.max_passes)
125
+ step = progress - self._reported
126
+ self._reported = progress
127
+ if self._phase is not None and step > 0:
128
+ self._phase.update(step)
129
+
130
+ def _log_heartbeat(self, max_residual):
131
+ if not self.heartbeat_seconds:
132
+ return
133
+ now = time.monotonic()
134
+ if self._reported_at is not None and now - self._reported_at < self.heartbeat_seconds:
135
+ return
136
+ self._reported_at = now
137
+ residual = ''
138
+ if max_residual is not None:
139
+ try:
140
+ residual = f', max residual {float(max_residual):.4g}'
141
+ except (TypeError, ValueError):
142
+ pass
143
+ of_passes = f'/{self.max_passes}' if self.max_passes else ''
144
+ logging.info(f'{self.desc}: optimisation pass {self._pass}{of_passes},'
145
+ f' iteration {self._iteration + 1}/{self.max_iter}{residual}')
146
+
147
+
148
+ class _RecordListener(logging.Handler):
149
+ """Takes the optimiser's progress lines and hands everything else back (forward())."""
150
+
151
+ def __init__(self, progress):
152
+ super().__init__(level=logging.DEBUG)
153
+ self.progress = progress
154
+
155
+ def emit(self, record):
156
+ # raised from inside the optimiser's own log call: the one way to stop that blocking call
157
+ raise_if_cancelled()
158
+ try:
159
+ if record.msg == ITERATION_MESSAGE and record.args:
160
+ max_residual = record.args[3] if len(record.args) > 3 else None
161
+ self.progress.note_iteration(record.args[0], max_residual)
162
+ return
163
+ if record.msg == MAX_ITER_MESSAGE and record.args:
164
+ self.progress.note_max_iter(record.args[0])
165
+ elif record.msg == FINISHED_MESSAGE:
166
+ self.progress.note_finished()
167
+ self.progress.forward(record)
168
+ except Exception: # pragma: no cover - progress must never break the run it watches
169
+ self.handleError(record)