Skip to content

Commit

Permalink
Updated Hotmaxatlas with ragged lists
Browse files Browse the repository at this point in the history
  • Loading branch information
fligt committed Sep 13, 2024
1 parent a0ef3e2 commit ebafc07
Show file tree
Hide file tree
Showing 5 changed files with 6,085 additions and 164 deletions.
106 changes: 44 additions & 62 deletions maxrf4u/hotmax.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# %% auto 0
__all__ = ['HotmaxAtlas', 'compute_hotmax_spectra', 'compute_hotmax_noise', 'compute_subpeaks']

# %% ../notebooks/40_hotmax.ipynb 37
# %% ../notebooks/40_hotmax.ipynb 39
import maxrf4u
import scipy.signal as ssg
import numpy as np
Expand All @@ -14,18 +14,18 @@
import scipy.interpolate as sip
from IPython.display import SVG

# %% ../notebooks/40_hotmax.ipynb 38
# %% ../notebooks/40_hotmax.ipynb 40
class HotmaxAtlas():

def __init__(self, datastack_file, prominence=0.2):

# add datastack name as attribute
self.datastack_file = datastack_file

# read from datastack file (arrays)

ds = maxrf4u.DataStack(datastack_file)

self.x_keVs = ds.read('maxrf_energies')
self.y_max = ds.read('maxrf_maxspectrum')
self.hotmax_pixels = ds.read('hotmax_pixels')
self.hotmax_spectra = ds.read('hotmax_spectra')
self.baselines = ds.read('hotmax_baselines')
self.noiselines = ds.read('hotmax_noiselines')
Expand All @@ -34,61 +34,55 @@ def __init__(self, datastack_file, prominence=0.2):
self.submax_spectrum = np.max(self.hotmax_spectra, axis=0) # core max spectrum

# read from datastack file (ragged lists)

self.peak_idxs_list = ds.read_list('hotmax_peak_idxs_list')
self.subpeak_idxs_list = ds.read_list('hotmax_subpeak_idxs_list')
self.hotmax_peak_idxs_list = ds.read_list('hotmax_peak_idxs_list')
self.hotmax_subpeak_idxs_list = ds.read_list('hotmax_subpeak_idxs_list')

# flatten ragged hotmax peak indexes list
self.hotmax_peak_idxs_flat = []
for idxs in self.hotmax_peak_idxs_list:
self.hotmax_peak_idxs_flat.extend(idxs)


def plot_spectrum(self, n, ax=None, legend=False, headspace=1, footspace=0.1,
hotlines_ticklabels=True, tight_layout=False):

if ax is None:

fig, ax = plt.subplots(figsize=[9, 3])

# NO LONGER
#hmp_i = self.hotmax_pixels[n, 2]

# the hotmax spectrum
# hotmax spectrum
ax.plot(self.x_keVs, self.hotmax_spectra[n],
zorder=-1, label=f'hotmax spectrum #{n}')
zorder=-1, label='hotmax spectrum')

# maxspectrum
ax.fill_between(self.x_keVs, self.y_max, color='r', alpha=0.15, zorder=8-30, label='max spectrum')
ax.fill_between(self.x_keVs, self.submax_spectrum, color='r', alpha=0.1)

# noise envelope
ax.fill_between(self.x_keVs, self.baselines[n], self.noiselines[n],
color=[0.5, 0.5, 0.9], zorder=10-30, alpha=0.4, label='noise envelope')
color=[0.5, 0.9, 0.5], zorder=10-30, alpha=0.5, label='Poisson noise envelope')

# fill subpeaks
ax.fill_between(self.x_keVs, self.noiselines[n], self.hotmax_spectra[n],
where=self.hotmax_spectra[n] > self.noiselines[n],
color='b', alpha=0.5, zorder=8-30, label='max spectrum')

# NEED DIFFERENT CODE
# the hotmax pixel peak (square marker)
#hmp_x = self.x_keVs[hmp_i]
#hmp_y = self.hotmax_spectra[n, hmp_i]
#ax.scatter(hmp_x, hmp_y, marker='s', zorder=1, edgecolor='r',
# facecolor='w', label=f'hotmax peak #{n}')

hmp_x = self.x_keVs[self.peak_idxs_list[n]]
hmp_y = self.hotmax_spectra[n][self.peak_idxs_list[n]]
color='b', alpha=0.5, zorder=8-30)

# hotmax peaks (red square markers)
hmp_x = self.x_keVs[self.hotmax_peak_idxs_list[n]]
hmp_y = self.hotmax_spectra[n][self.hotmax_peak_idxs_list[n]]
ax.scatter(hmp_x, hmp_y, marker='s', zorder=1, edgecolor='r',
facecolor='w', label=f'hotmax peak #{n}')
facecolor='w', label='hotmax peak(s)')

# submax peaks (round markers)
peaks_x = self.x_keVs[self.subpeak_idxs_list[n]]
peaks_y = self.hotmax_spectra[n, self.subpeak_idxs_list[n]]
ax.scatter(peaks_x, peaks_y, edgecolor='b', alpha=1, zorder=0, facecolor='w')
# (puzzle) sub peaks (round markers)
subpeaks_x = self.x_keVs[self.hotmax_subpeak_idxs_list[n]]
subpeaks_y = self.hotmax_spectra[n, self.hotmax_subpeak_idxs_list[n]]
ax.scatter(subpeaks_x, subpeaks_y, edgecolor='b', alpha=1, zorder=0, facecolor='w', label='puzzle peaks')

# annotate submax peaks
peak_labels = [f'({i})' for i, _ in enumerate(self.subpeak_idxs_list[n])]
# annotate puzzle peaks
subpeak_labels = [f'({i})' for i, _ in enumerate(self.hotmax_subpeak_idxs_list[n])]
ann_list = []
for i, plabel in enumerate(peak_labels):
peak_xy = peaks_x[i], peaks_y[i]
ann = ax.annotate(plabel, peak_xy, xytext=[0, 10], color='b',
for i, plabel in enumerate(subpeak_labels):
subpeak_xy = subpeaks_x[i], subpeaks_y[i]
ann = ax.annotate(plabel, subpeak_xy, xytext=[0, 10], color='b',
textcoords='offset points', ha='center')
ann_list.append(ann)

Expand All @@ -97,7 +91,8 @@ def plot_spectrum(self, n, ax=None, legend=False, headspace=1, footspace=0.1,
ymax = 1.15 * self.hotmax_spectra[n].max() # add space for peak labels
ymin = -ymax / 5
ax.set_ylim(footspace * ymin, headspace * ymax)
xlim = self.x_keVs[max(self.hotmax_pixels[:, 2])] + 2
#xlim = self.x_keVs[max(self.hotmax_pixels[:, 2])] + 2
xlim = self.x_keVs[max(self.hotmax_peak_idxs_list)[0]] + 2
ax.set_xlim(-1, xlim)

# remove negative yticks from footspace
Expand All @@ -107,34 +102,20 @@ def plot_spectrum(self, n, ax=None, legend=False, headspace=1, footspace=0.1,
# again why ??
ax.set_ylim(footspace * ymin, headspace * ymax)

# label
ax.text(0.995, 0.98, f'#{n}', c='grey', ha='right', va='top', transform=ax.transAxes)

# plot text label
ax.text(0.995, 0.96, f'#{n}', ha='right', va='top', transform=ax.transAxes)

# NEED TO ADJUST TO RAGGED LIST
# plot lines in pattern overview for all hotmax peaks
lines_x = self.x_keVs[self.hotmax_pixels[:, 2]]
lines_x = self.x_keVs[self.hotmax_peak_idxs_flat]
ax.vlines(lines_x, ymin=0, ymax=2*ymax*headspace, color='r', alpha=0.2, zorder=9-30)


# add legend and labels etcetera if standalone
if legend:
ax.set_xlabel('energy [keV]')
ax.set_xlabel('Energy [keV]')
ax.set_ylabel('Intensity [#counts]')
ax.legend()


# add hotlines ticklabels
if hotlines_ticklabels:
x_keVs = self.x_keVs
hotmax_z = self.hotmax_pixels[:, 2]

#ax, _ = hma.plot_spectrum(10)
twax = ax.twiny()
twax.set_xlim(ax.get_xlim())
twax.set_xticks(x_keVs[hotmax_z])
twax.set_xticklabels(range(len(hotmax_z)), fontsize=6, color='r')

twax.tick_params(color=[1, 0.5, 0.5], pad=0)
ax.legend(loc='upper left', bbox_to_anchor=(1.01, 0.98))

if tight_layout:
plt.tight_layout()
Expand All @@ -149,15 +130,16 @@ def plot_spectra(self, svg=True):

fig, axs = plt.subplots(nrows=self.n_spectra, figsize=[9, self.n_spectra], sharex=True)

axs[0].set_title('hotmax spectra')

xlim = self.x_keVs[max(self.hotmax_pixels[:, 2])] + 2
axs[0].set_title(f'Hotmax spectra')

# set plot limits
xlim = self.x_keVs[max(self.hotmax_peak_idxs_list)[0]] + 2

for n, ax in enumerate(axs):
self.plot_spectrum(n, ax=ax, hotlines_ticklabels=False)
self.plot_spectrum(n, ax=ax)

# limits
ylim = 1.3 * self.hotmax_spectra[n].max()
ylim = 1.5 * self.hotmax_spectra[n].max()
ax.set_ylim(-1, ylim)
ax.set_xlim(-1, xlim)
#ax.text(0.95, 0.95, f'{n}', ha='right', va='top', transform=ax.transAxes)
Expand Down
2 changes: 1 addition & 1 deletion maxrf4u/peakpuzzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def plot_puzzle(datastack_file, n, elements=None, color_select=None, footspace=0

ax_spectr.set_xlabel('Energy (keV)')

# add tick labels explictly to avoid issues with panning
# add tick labels explicitly to avoid issues with panning
_add_hotlines_ticklabels(datastack_file, ax_spectr)
_add_hotlines_ticklabels(datastack_file, ax_ptrns, clip_vline=False)

Expand Down
6,080 changes: 5,998 additions & 82 deletions notebooks/40_hotmax.ipynb

Large diffs are not rendered by default.

59 changes: 41 additions & 18 deletions notebooks/50_peak-pattern-puzzle.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion notebooks/55_peak-pattern-puzzle-solver.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1188,5 +1188,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}

0 comments on commit ebafc07

Please sign in to comment.