|
| 1 | +# %% |
| 2 | +from io import StringIO |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import matplotlib as mpl |
| 6 | +import matplotlib.pyplot as plt |
| 7 | +import matplotlib.ticker as mticker |
| 8 | +import numpy as np |
| 9 | +import scipy.signal |
| 10 | +from multihead.config import DetectorROIs |
| 11 | +from multihead.file_io import open_data |
| 12 | +from multihead.raw_proc import get_roi_sum |
| 13 | + |
| 14 | +# %% |
| 15 | +roi_data = StringIO("""rois: |
| 16 | +- detector_number: 1 |
| 17 | + roi_bounds: |
| 18 | + cslc: |
| 19 | + - 0 |
| 20 | + - 208 |
| 21 | + rslc: |
| 22 | + - 120 |
| 23 | + - 136 |
| 24 | +- detector_number: 2 |
| 25 | + roi_bounds: |
| 26 | + cslc: |
| 27 | + - 25 |
| 28 | + - 240 |
| 29 | + rslc: |
| 30 | + - 77 |
| 31 | + - 94 |
| 32 | +- detector_number: 3 |
| 33 | + roi_bounds: |
| 34 | + cslc: |
| 35 | + - 11 |
| 36 | + - 219 |
| 37 | + rslc: |
| 38 | + - 176 |
| 39 | + - 194 |
| 40 | +- detector_number: 4 |
| 41 | + roi_bounds: |
| 42 | + cslc: |
| 43 | + - 25 |
| 44 | + - 236 |
| 45 | + rslc: |
| 46 | + - 188 |
| 47 | + - 204 |
| 48 | +- detector_number: 5 |
| 49 | + roi_bounds: |
| 50 | + cslc: |
| 51 | + - 12 |
| 52 | + - 221 |
| 53 | + rslc: |
| 54 | + - 185 |
| 55 | + - 202 |
| 56 | +- detector_number: 6 |
| 57 | + roi_bounds: |
| 58 | + cslc: |
| 59 | + - 30 |
| 60 | + - 240 |
| 61 | + rslc: |
| 62 | + - 122 |
| 63 | + - 139 |
| 64 | +- detector_number: 7 |
| 65 | + roi_bounds: |
| 66 | + cslc: |
| 67 | + - 15 |
| 68 | + - 220 |
| 69 | + rslc: |
| 70 | + - 162 |
| 71 | + - 179 |
| 72 | +- detector_number: 8 |
| 73 | + roi_bounds: |
| 74 | + cslc: |
| 75 | + - 30 |
| 76 | + - 238 |
| 77 | + rslc: |
| 78 | + - 70 |
| 79 | + - 87 |
| 80 | +- detector_number: 9 |
| 81 | + roi_bounds: |
| 82 | + cslc: |
| 83 | + - 13 |
| 84 | + - 217 |
| 85 | + rslc: |
| 86 | + - 166 |
| 87 | + - 183 |
| 88 | +- detector_number: 10 |
| 89 | + roi_bounds: |
| 90 | + cslc: |
| 91 | + - 32 |
| 92 | + - 238 |
| 93 | + rslc: |
| 94 | + - 115 |
| 95 | + - 132 |
| 96 | +- detector_number: 11 |
| 97 | + roi_bounds: |
| 98 | + cslc: |
| 99 | + - 0 |
| 100 | + - 211 |
| 101 | + rslc: |
| 102 | + - 122 |
| 103 | + - 139 |
| 104 | +- detector_number: 12 |
| 105 | + roi_bounds: |
| 106 | + cslc: |
| 107 | + - 36 |
| 108 | + - 237 |
| 109 | + rslc: |
| 110 | + - 59 |
| 111 | + - 78""") |
| 112 | + |
| 113 | +rois = DetectorROIs.from_yaml(roi_data) |
| 114 | + |
| 115 | +# %% |
| 116 | + |
| 117 | +root = Path("/mnt/store/bnl/cache/hrd/beamtime_cleanup/reorg/raw/v3/") |
| 118 | +# the full data set |
| 119 | +raw = open_data(root / "11bmb_2387_mda_defROI", 3) |
| 120 | +detector_number = 1 |
| 121 | +data = raw.get_detector(detector_number) |
| 122 | +# get the ROI subsection |
| 123 | +rslc, cslc = rois.rois[1].to_slices() |
| 124 | +roi_data = data[:, rslc, cslc] |
| 125 | +# treat detector as point detector |
| 126 | +point_eqiv = data.sum(axis=(2), dtype=np.uint32).todense().sum(axis=1) |
| 127 | +roi_sum = roi_data.sum(axis=(2), dtype=np.uint32).todense().sum(axis=1) |
| 128 | +khymo = roi_data.sum(axis=1, dtype=np.uint32).todense() |
| 129 | +# arm data |
| 130 | +arm_tth = raw.get_arm_tth() |
| 131 | + |
| 132 | +# %% |
| 133 | + |
| 134 | +locs, props = scipy.signal.find_peaks( |
| 135 | + point_eqiv, width=(None, None), height=(None, None) |
| 136 | +) |
| 137 | +indx = np.argsort(props["peak_heights"])[:-5:-1] |
| 138 | +peak_locations = locs[indx] |
| 139 | +peak_heights = props["peak_heights"][indx] |
| 140 | +peak_widths = props["widths"][indx] |
| 141 | + |
| 142 | +# %% |
| 143 | +fig, axa = plt.subplots(5, 5, layout="constrained", figsize=(3.75, 5)) |
| 144 | +# peak with lowest angle shows most dramatic effect |
| 145 | +center_frame = peak_locations[1] |
| 146 | +cmap = mpl.colormaps["plasma"].with_extremes(under="w") |
| 147 | + |
| 148 | +norm = mpl.colors.Normalize(1, 20) |
| 149 | +for j, ax in enumerate(axa.flat): |
| 150 | + offset = j - 15 |
| 151 | + frame = center_frame + offset |
| 152 | + ax.set_title(f"Δ={offset}", size="small") |
| 153 | + ax.xaxis.set_major_locator(mticker.NullLocator()) |
| 154 | + ax.yaxis.set_major_locator(mticker.NullLocator()) |
| 155 | + im = ax.imshow(data[frame].todense(), cmap=cmap, norm=norm, origin="lower") |
| 156 | +fig.colorbar(im, ax=axa[-1, :], shrink=0.6, location="bottom") |
| 157 | +# fig.savefig("/tmp/through_peak.png", dpi=300) |
| 158 | + |
| 159 | +# %% |
| 160 | + |
| 161 | +fig, axd = plt.subplot_mosaic( |
| 162 | + "A;B", layout="constrained", figsize=(3.5, 3.5), height_ratios=(5, 1), sharex=True |
| 163 | +) |
| 164 | +axd["A"].plot(point_eqiv / point_eqiv.max(), label="full detector", ls="--") |
| 165 | +axd["A"].plot(roi_sum / roi_sum.max(), label="roi", alpha=0.75) |
| 166 | +axd["B"].set_xlabel("frame number") |
| 167 | +axd["A"].set_ylabel("normalized I (arb)") |
| 168 | +axd["A"].legend() |
| 169 | + |
| 170 | +axd["B"].plot( |
| 171 | + point_eqiv / point_eqiv.max() - roi_sum / roi_sum.max(), label="point - roi" |
| 172 | +) |
| 173 | +axd["B"].set_ylim(top=0.02) |
| 174 | + |
| 175 | +fig.savefig("/tmp/detector_sum.png", dpi=300) |
| 176 | + |
| 177 | +# %% |
| 178 | + |
| 179 | +fig_kyho = plt.figure(layout="constrained", figsize=(4, 5)) |
| 180 | +ax_d = fig_kyho.subplot_mosaic("AB", width_ratios=(1, 5), sharey=True) |
| 181 | +im = ax_d["B"].imshow( |
| 182 | + khymo, |
| 183 | + aspect="auto", |
| 184 | + extent=( |
| 185 | + 0, |
| 186 | + khymo.shape[1], |
| 187 | + arm_tth.min(), |
| 188 | + arm_tth.max(), |
| 189 | + ), |
| 190 | + origin="lower", |
| 191 | + vmin=1, |
| 192 | + cmap=cmap, |
| 193 | +) |
| 194 | +ax_d["A"].plot(roi_sum, arm_tth) |
| 195 | +cb = fig.colorbar(im, ax=ax_d["B"], location="right") |
| 196 | +ax_d["A"].xaxis.set_major_formatter( |
| 197 | + lambda x, pos: f"{int(x // 1000)}k" if x != 0 else "0" |
| 198 | +) |
| 199 | +ax_d["A"].set_xlabel("$I_{sum}$ (photons)") |
| 200 | +ax_d["A"].set_ylabel(r"$2\Theta$ (deg)") |
| 201 | +ax_d["B"].set_xlabel("pixel") |
| 202 | +cb.set_label("Intensity (photons)") |
| 203 | +fig_kyho.savefig("/tmp/khymo_full.png", dpi=300) |
| 204 | +ax_d["A"].set_ylim(6.42, 6.48) |
| 205 | +fig_kyho.set_size_inches(3.9, 2.9) |
| 206 | +fig_kyho.savefig("/tmp/khymo_zoom.png", dpi=300) |
| 207 | +# %% |
| 208 | + |
| 209 | +cosmic_index = 13213 |
| 210 | +fig, ax = plt.subplots(layout="constrained") |
| 211 | +ax.imshow(data[cosmic_index].todense(), cmap=cmap, norm=norm, origin="lower") |
0 commit comments