roi_n = 3
ncols = len(roi_elements_list[roi_n])
hspace = 0.02
wspace = 0.02
xylims = xylims_list[roi_n]
l, r, b, t = xylims
w = r - l
h = b - t
roi_extent = [0, w, h, 0]
fig = plt.figure(layout='constrained', figsize=(15, 15))
topfigs, midfig, botfig = fig.subfigures(nrows=3, hspace=hspace, height_ratios=[2, 1, 2], squeeze=True)
topleftfig, topmidfig, toprightfig = topfigs.subfigures(ncols=3, wspace=wspace, width_ratios=[1, 1, 2], squeeze=True)
ax_00 = topleftfig.subplots()
ax_01 = topmidfig.subplots()
ax_020, ax_021 = toprightfig.subplots(nrows=2, squeeze=True, sharex=True)
spectra_axs = midfig.subplots(ncols=ncols, squeeze=True, sharex=True) #, sharey=True)
maps_axs = botfig.subplots(nrows=2, ncols=ncols, sharex=True, sharey=True)
########## TOP #################
# RGB overview and RGB ROI
ax_00.imshow(imvis_highres, extent=extent)
for i, xylims in enumerate(xylims_list):
x0, x1, y0, y1 = xylims
ax_00.add_patch(Rectangle([x0, y1], x1 -x0, y0 - y1, linewidth=1, edgecolor=edgecolors[i], facecolor='none'))
ax_00.annotate(f'ROI {i}', xy=[x0, y1], xytext=[0,5], textcoords='offset points', color='w', fontsize=8)
ax_00.set_title(object_num)
ax_01.imshow(roi_ims[roi_n], extent=roi_extent)
ax_01.set_title(f'ROI {roi_n}')
# ROI spectral plots and peek pattern atlas
# compute roi cube (core) maxspectrum
hotmax_idxs, spectra = get_hotmax_spectra(roi_cubes[roi_n])
roi_cube = roi_cubes[roi_n]
roi_h, roi_w, roi_d = roi_cube.shape
roi_flat = roi_cube.reshape([-1, roi_d])
roi_maxspectrum = roi_flat.max(axis=0) # actually this is the roi core max spectrum
roi_elements = roi_elements_list[roi_n]
ax_020.plot(x_keVs, roi_maxspectrum, label='ROI maxspectrum')
ax_020.plot(x_keVs, y_maxspectrum, color='r', alpha=0.2, label='full page maxspectrum')
ax_020.set_xlim([-1, 25])
ax_021.set_xlabel('energy [keV]')
#ax_020.legend() # need to fix blocking of view
# plot peak pattern atlas
select_elems = roi_elements_list[roi_n]
select_elems = [re.sub('_.*', '', elem) for elem in select_elems]
ppa.plot_atlas(ax=ax_021)
for i, element in enumerate(roi_elements):
# based on element now compute element slice map and related hotmax properties
slice_map, slice_idx = get_slice_map(datastack_file, element)
hx, hy, roi_x, roi_y, hotmax_spectrum = get_roi_slice_hotmax(datastack_file, slice_idx, xylims_list[roi_n])
# add hotmax locations to TOP image plots
ax_01.scatter(roi_x, roi_y)
# add slice line to spectral plots
#spectra_axs[i].axvline(x_keVs[slice_idx], color='r')
# add hotmax locations to slice maps
maps_axs[0, i].scatter(hx, hy, color='w', marker='+')
# add hotmax spectra and element shadows to mid figure
spectra_axs[i].plot(x_keVs, hotmax_spectrum)
spectra_axs[i].scatter(x_keVs[slice_idx], hotmax_spectrum[slice_idx], marker='s', edgecolor='r', facecolor='none')
#plot_element_shadow(element, x_keVs, flip=1, ax=spectra_axs[i], y=hotmax_spectrum)
spectra_axs[i].plot(x_keVs, y_maxspectrum, color='r', alpha=0.15)
spectra_axs[i].set_xlim([-1, 25])
spectra_axs[i].set_title(f'{element} (slice {x_keVs[slice_idx]:.1f} keV)', fontsize=8)
plot_element_lines(element, x_keVs, ax=spectra_axs[i], color='r', y=hotmax_spectrum)
# add NMF map for element
nmf_map = pick_nmf_elementmap(datastack_file, nmf_elementmaps, element)
maps_axs[0, i].imshow(slice_map)
maps_axs[0, i].set_title(f'{element} (slice {x_keVs[slice_idx]:.1f} keV)', fontsize=8)
maps_axs[1, i].imshow(nmf_map)
maps_axs[1, i].set_title(f'{element} (NMF)', fontsize=8)
# add ROI rectangle
x0, x1, y0, y1 = xylims_list[roi_n]
maps_axs[0, i].add_patch(Rectangle([x0, y1], x1 -x0, y0 - y1, linewidth=1, edgecolor=edgecolors[roi_n], facecolor='none'))
maps_axs[0, i].annotate(f'ROI {roi_n}', xy=[x0, y1], xytext=[0,5], textcoords='offset points', color='w', fontsize=10)
maps_axs[1, i].add_patch(Rectangle([x0, y1], x1 -x0, y0 - y1, linewidth=1, edgecolor=edgecolors[roi_n], facecolor='none'))
maps_axs[1, i].annotate(f'ROI {roi_n}', xy=[x0, y1], xytext=[0,5], textcoords='offset points', color='w', fontsize=10)