bobleesj commited on
Commit
58a2fbd
·
verified ·
1 Parent(s): d128d47

Delete denoise/synthetic_detector.py

Browse files
Files changed (1) hide show
  1. denoise/synthetic_detector.py +0 -145
denoise/synthetic_detector.py DELETED
@@ -1,145 +0,0 @@
1
- """Regenerate tutorial measurements from shared abTEM truth on CUDA.
2
-
3
- This is tutorial support, not a detector calibration implementation. Denova's
4
- public simulator draws the measurements; its calibration API independently
5
- fits the saved dark and flat exposures in the notebook.
6
- """
7
-
8
- import json
9
- from pathlib import Path
10
-
11
- import torch
12
- from quantem.gpu import io as qio
13
-
14
- from denova import load
15
- from denova.fourdstem import simulate
16
- from denova.noise import PoissonGaussian
17
-
18
-
19
- RECIPE = {
20
- "detector_shape": [192, 192],
21
- "gain": 1.5, "gain_modulation": 0.04,
22
- "gain_row_scale": 13, "gain_col_scale": 19,
23
- "offset": 2, "offset_modulation": 0.15, "offset_row_scale": 21,
24
- "read_noise_sd": 0.6, "max_events": 32768,
25
- "invalid_pixels": [[12, 22], [30, 161], [150, 32], [177, 170]],
26
- "science_seed": 1000, "dark_seed": 2000, "flat_seed": 2001,
27
- "check_seed": 2002, "dark_repeats": 512, "flat_repeats": 256,
28
- "check_repeats": 128, "flat_levels": [1., 16., 256., 4096., 32768.],
29
- }
30
-
31
-
32
- def detector_model(recipe: dict[str, object]) -> PoissonGaussian:
33
- """Construct the declared synthetic detector maps on CUDA.
34
-
35
- Parameters
36
- ----------
37
- recipe : dict
38
- Numerical parameters from the example's ``noise-recipe.json``.
39
-
40
- Returns
41
- -------
42
- PoissonGaussian
43
- Known generator, used for simulation and final truth evaluation only.
44
- Reconstruction uses a separate model fitted from dark/flat repeats.
45
-
46
- Examples
47
- --------
48
- >>> known = detector_model(RECIPE)
49
- """
50
- detector_rows, detector_cols = recipe["detector_shape"]
51
- row_t, col_t = torch.meshgrid(
52
- torch.arange(detector_rows, device="cuda"),
53
- torch.arange(detector_cols, device="cuda"), indexing="ij",
54
- )
55
- gain_t = recipe["gain"] * (
56
- 1 + recipe["gain_modulation"]
57
- * torch.sin(row_t / recipe["gain_row_scale"])
58
- * torch.cos(col_t / recipe["gain_col_scale"])
59
- )
60
- offset_t = recipe["offset"] + recipe["offset_modulation"] * torch.cos(
61
- row_t / recipe["offset_row_scale"]
62
- ).expand(detector_rows, detector_cols)
63
- read_t = torch.full_like(gain_t, recipe["read_noise_sd"])
64
- valid_t = torch.ones_like(gain_t, dtype=torch.bool)
65
- invalid_t = torch.tensor(recipe["invalid_pixels"], device="cuda")
66
- valid_t[invalid_t[:, 0], invalid_t[:, 1]] = False
67
- return PoissonGaussian(
68
- gain_t, offset_t, read_t, valid_t, recipe["max_events"],
69
- calibration={"method": "assumed synthetic generator, not hardware fit"},
70
- )
71
-
72
-
73
- def prepare_measurements(source: Path, output: Path, *, dose: float = 1.0) -> Path:
74
- """Generate one science observation and independent calibration repeats.
75
-
76
- Parameters
77
- ----------
78
- source : pathlib.Path
79
- Downloaded folder with abTEM event truth, geometry and noise recipe.
80
- output : pathlib.Path
81
- Local output directory outside the read-only download cache. Existing
82
- files of the same names are regenerated with the declared seeds.
83
- dose : float, optional
84
- Positive relative exposure multiplier. The default is 1.0.
85
-
86
- Returns
87
- -------
88
- pathlib.Path
89
- Folder of QuantEM.GPU HDF5 measurements and locally generated maps.
90
- Neither abTEM nor abtem-cuda is required for this step.
91
-
92
- Examples
93
- --------
94
- >>> data = prepare_measurements(source, Path("4dstem-results/inputs"))
95
- """
96
- recipe = json.loads((source / "noise-recipe.json").read_text())
97
- geometry = json.loads((source / "geometry.json").read_text())
98
- output.mkdir(parents=True, exist_ok=True)
99
- known = detector_model(recipe)
100
- scan_rows, scan_cols = geometry["scan_shape"]
101
- truth = load(
102
- source / "truth-events-dose-1.0_master.h5",
103
- scan_region=(0, scan_rows, 0, scan_cols),
104
- )
105
- observed_t = simulate(
106
- dose * truth.array, noise=known, seed=recipe["science_seed"],
107
- )
108
- axis_metadata = {
109
- "sampling": [geometry["scan_step_A"]] * 2
110
- + [geometry["angular_sampling_mrad"]] * 2,
111
- "units": ["angstrom", "angstrom", "mrad", "mrad"],
112
- "origin": [0., 0., *[-v * geometry["angular_sampling_mrad"]
113
- for v in geometry["detector_center"]]],
114
- "intensity_units": "ADU",
115
- }
116
- qio.save(
117
- output / f"dose-{dose:.1f}_master.h5", observed_t, metadata=axis_metadata,
118
- )
119
- dark_t = simulate(
120
- torch.zeros(
121
- (recipe["dark_repeats"], *recipe["detector_shape"]), device="cuda",
122
- ),
123
- noise=known, seed=recipe["dark_seed"],
124
- )
125
- levels_t = torch.tensor(recipe["flat_levels"], device="cuda")
126
- flats_t = simulate(
127
- levels_t[:, None, None, None].expand(
128
- -1, recipe["flat_repeats"], *recipe["detector_shape"],
129
- ),
130
- noise=known, seed=recipe["flat_seed"],
131
- )
132
- qio.save(
133
- output / "dark_master.h5", dark_t[None],
134
- metadata={"exposure_axes": "dark_set, repeat, detector_row, detector_col"},
135
- )
136
- qio.save(
137
- output / "flats_master.h5", flats_t,
138
- metadata={
139
- "exposure_axes": "illumination_level, repeat, detector_row, detector_col",
140
- },
141
- )
142
- torch.save(known.valid, output / "detector-valid.pt")
143
- known.save(output / "generator-only.pt")
144
- (output / "geometry.json").write_text(json.dumps(geometry, indent=2) + "\n")
145
- return output