def test_calc_livetime(self): """Use straxen.get_livetime_sec""" try: live_time = straxen.get_livetime_sec(self.st, nt_test_run_id) except strax.RunMetadataNotAvailable: things = self.st.get_array(nt_test_run_id, 'peaks') live_time = straxen.get_livetime_sec(self.st, nt_test_run_id, things=things) assertion_statement = "Live-time calculation is wrong" expected = self._expected_test_results['run_live_time'] self.assertTrue(live_time == expected, assertion_statement)
def test_straxen(): with tempfile.TemporaryDirectory() as temp_dir: try: print("Temporary directory is ", temp_dir) os.chdir(temp_dir) print("Downloading test data (if needed)") st = straxen.contexts.demo() # Ignore strax-internal warnings st.set_context_config({'free_options': tuple(st.config.keys())}) run_df = st.select_runs(available='raw_records') print(run_df) run_id = run_df.iloc[0]['name'] assert run_id == test_run_id_1T print("Test processing") df = st.get_df(run_id, 'event_info') assert len(df) > 0 assert 'cs1' in df.columns assert df['cs1'].sum() > 0 assert not np.all(np.isnan(df['x'].values)) print('Test common.get_livetime_sec') events = st.get_array(run_id, 'peaks') straxen.get_livetime_sec(st, test_run_id_1T, things=events) # TODO: find a way to break up the tests # surely pytest has common startup/cleanup? print("Test mini analysis") @straxen.mini_analysis(requires=('raw_records', )) def count_rr(raw_records): return len(raw_records) n = st.count_rr(test_run_id_1T) assert n > 100 # On windows, you cannot delete the current process' # working directory, so we have to chdir out first. finally: os.chdir('..')
def plot_peaks_aft_histogram( context, run_id, peaks, pe_bins=np.logspace(0, 7, 120), rt_bins=np.geomspace(2, 1e5, 120), extra_labels=tuple(), rate_range=(1e-4, 1), aft_range=(0, .85), figsize=(14, 5)): """Plot side-by-side (area, width) histograms of the peak rate and mean area fraction top. :param pe_bins: Array of bin edges for the peak area dimension [PE] :param rt_bins: array of bin edges for the rise time dimension [ns] :param extra_labels: List of (area, risetime, text, color) extra labels to put on the plot :param rate_range: Range of rates to show [peaks/(bin*s)] :param aft_range: Range of mean S1 area fraction top / bin to show :param figsize: Figure size to use """ livetime_sec = straxen.get_livetime_sec(context, run_id, peaks) mh = Histdd(peaks, dimensions=( ('area', pe_bins), ('range_50p_area', rt_bins), ('area_fraction_top', np.linspace(0, 1, 100)) )) f, axes = plt.subplots(1, 2, figsize=figsize) def std_axes(): plt.gca().set_facecolor('k') plt.yscale('log') plt.xscale('log') plt.xlabel("Area [PE]") plt.ylabel("Range 50% area [ns]") labels = [ (12, 8, "AP?", 'white'), (3, 150, "1PE\npileup", 'gray'), (30, 200, "1e", 'gray'), (100, 1000, "n-e", 'w'), (2000, 2e4, "Train", 'gray'), (1200, 50, "S1", 'w'), (45e3, 60, "αS1", 'w'), (2e5, 800, "S2", 'w'), ] + list(extra_labels) for x, w, text, color in labels: plt.text(x, w, text, color=color, verticalalignment='center', horizontalalignment='center') plt.sca(axes[0]) (mh / livetime_sec).sum(axis=2).plot( log_scale=True, vmin=rate_range[0], vmax=rate_range[1], colorbar_kwargs=dict(extend='both'), cblabel='Peaks / (bin * s)') std_axes() plt.sca(axes[1]) mh.average(axis=2).plot( vmin=aft_range[0], vmax=aft_range[1], colorbar_kwargs=dict(extend='max'), cmap=plt.cm.jet, cblabel='Mean area fraction top') std_axes() plt.tight_layout()
def test_several(): """ Test several other functions in straxen. Is kind of messy but saves time as we won't load data many times :return: """ with tempfile.TemporaryDirectory() as temp_dir: try: print("Temporary directory is ", temp_dir) os.chdir(temp_dir) print("Downloading test data (if needed)") st = straxen.contexts.demo() st.make(test_run_id_1T, 'records') # Ignore strax-internal warnings st.set_context_config({'free_options': tuple(st.config.keys())}) st.make(test_run_id_1T, 'records') print("Get peaks") p = st.get_array(test_run_id_1T, 'peaks') # Do checks on there number of peaks assertion_statement = ("Got /more peaks than expected, perhaps " "the test is outdated or clustering has " "really changed") assert np.abs(len(p) - EXPECTED_OUTCOMES_TEST_SEVERAL['n_peaks'] ) < 5, assertion_statement events = st.get_array(test_run_id_1T, 'event_info') print('plot wf') peak_i = 0 st.plot_waveform(test_run_id_1T, time_range=(p[peak_i]['time'], strax.endtime(p[peak_i]))) plt_clf() print('plot hit pattern') peak_i = 1 st.plot_hit_pattern(test_run_id_1T, time_range=(p[peak_i]['time'], strax.endtime(p[peak_i])), xenon1t=True) plt_clf() print('plot (raw)records matrix') peak_i = 2 assert st.is_stored(test_run_id_1T, 'records'), "no records" assert st.is_stored(test_run_id_1T, 'raw_records'), "no raw records" st.plot_records_matrix(test_run_id_1T, time_range=(p[peak_i]['time'], strax.endtime(p[peak_i]))) st.raw_records_matrix(test_run_id_1T, time_range=(p[peak_i]['time'], strax.endtime(p[peak_i]))) st.plot_waveform(test_run_id_1T, time_range=(p[peak_i]['time'], strax.endtime(p[peak_i])), deep=True) plt_clf() straxen.analyses.event_display.plot_single_event( st, test_run_id_1T, events, xenon1t=True, event_number=0, records_matrix='raw') st.event_display_simple(test_run_id_1T, time_range=(events[0]['time'], events[0]['endtime']), xenon1t=True) plt_clf() st.event_display_interactive(test_run_id_1T, time_range=(events[0]['time'], events[0]['endtime']), xenon1t=True) plt_clf() print('plot aft') st.plot_peaks_aft_histogram(test_run_id_1T) plt_clf() print('plot event scatter') st.event_scatter(test_run_id_1T) plt_clf() print('plot event scatter') st.plot_energy_spectrum(test_run_id_1T) plt_clf() print('plot peak clsassification') st.plot_peak_classification(test_run_id_1T) plt_clf() print("plot holoviews") peak_i = 3 st.waveform_display(test_run_id_1T, time_range=(p[peak_i]['time'], strax.endtime(p[peak_i]))) st.hvdisp_plot_pmt_pattern(test_run_id_1T, time_range=(p[peak_i]['time'], strax.endtime(p[peak_i]))) st.hvdisp_plot_peak_waveforms( test_run_id_1T, time_range=(p[peak_i]['time'], strax.endtime(p[peak_i]))) print('Plot single pulse:') st.plot_pulses_tpc(test_run_id_1T, max_plots=2, plot_hits=True, ignore_time_warning=True) print("Check live-time") live_time = straxen.get_livetime_sec(st, test_run_id_1T, things=p) assertion_statement = "Live-time calculation is wrong" assert live_time == EXPECTED_OUTCOMES_TEST_SEVERAL[ 'run_live_time'], assertion_statement print('Check the peak_basics') df = st.get_df(test_run_id_1T, 'peak_basics') assertion_statement = ("Got less/more S1s than expected, perhaps " "the test is outdated or classification " "has really changed.") assert np.abs( np.sum(df['type'] == 1) - EXPECTED_OUTCOMES_TEST_SEVERAL['n_s1'] ) < 2, assertion_statement df = df[:10] print("Check that we can write nice wiki dfs") straxen.dataframe_to_wiki(df) print("Abuse the peaks to show that _average_scada works") p = p[:10] p_t, p_a = straxen.scada._average_scada(p['time'] / 1e9, p['time'], 1) assert len(p_a) == len(p), 'Scada deleted some of my 10 peaks!' print('Check the number of events') events = st.get_array(test_run_id_1T, 'event_info_double') assertion_statement = ("Got less/ore events than expected, " "perhaps the test is outdated or something " "changed in the processing.") assert len(events) == EXPECTED_OUTCOMES_TEST_SEVERAL[ 'n_events'], assertion_statement print("Plot bokkeh:") fig = st.event_display_interactive( test_run_id_1T, time_range=(events[0]['time'], events[0]['endtime']), xenon1t=True, plot_record_matrix=True, ) fig.save('test_display.html') # Test data selector: from straxen.analyses.bokeh_waveform_plot import DataSelectionHist ds = DataSelectionHist('ds') fig = ds.histogram2d(p, p['area'], p['area'], bins=50, hist_range=((0, 200), (0, 2000)), log_color_scale=True, clim=(10, None), undeflow_color='white') import bokeh.plotting as bklt bklt.save(fig, 'test_data_selector.html') # On windows, you cannot delete the current process' # working directory, so we have to chdir out first. finally: os.chdir('..')
def test_several(): """ Test several other functions in straxen. Is kind of messy but saves time as we won't load data many times :return: """ with tempfile.TemporaryDirectory() as temp_dir: try: print("Temporary directory is ", temp_dir) os.chdir(temp_dir) print("Downloading test data (if needed)") st = straxen.contexts.demo() print("Get peaks") p = st.get_array(test_run_id, 'peaks') # Do checks on there number of peaks assertion_statement = ("Got /more peaks than expected, perhaps " "the test is outdated or clustering has " "really changed") assert np.abs(len(p) - EXPECTED_OUTCOMES_TEST_SEVERAL['n_peaks'] ) < 5, assertion_statement st.make(test_run_id, 'event_info') print('plot wf') peak_i = 0 st.plot_waveform(test_run_id, time_range=(p[peak_i]['time'], strax.endtime(p[peak_i]))) plt_clf() print('plot hit pattern') peak_i = 1 st.plot_hit_pattern(test_run_id, time_range=(p[peak_i]['time'], strax.endtime(p[peak_i])), xenon1t=True) plt_clf() print('plot records matrix') peak_i = 2 assert st.is_stored(test_run_id, 'records'), "no records" st.plot_records_matrix(test_run_id, time_range=(p[peak_i]['time'], strax.endtime(p[peak_i]))) plt_clf() print('plot aft') st.plot_peaks_aft_histogram(test_run_id) plt_clf() print('plot event scatter') st.event_scatter(test_run_id) plt_clf() print('plot event scatter') st.plot_energy_spectrum(test_run_id) plt_clf() print("Check live-time") live_time = straxen.get_livetime_sec(st, test_run_id, things=p) assertion_statement = ("Live-time calculation is wrong") assert live_time == EXPECTED_OUTCOMES_TEST_SEVERAL[ 'run_live_time'], assertion_statement print('Check the peak_basics') df = st.get_df(test_run_id, 'peak_basics') assertion_statement = ("Got less/more S1s than expected, perhaps " "the test is outdated or classification " "has really changed.") assert np.abs( np.sum(df['type'] == 1) - EXPECTED_OUTCOMES_TEST_SEVERAL['n_s1'] ) < 2, assertion_statement df = df[:10] print("Check that we can write nice wiki dfs") straxen.dataframe_to_wiki(df) print("Abbuse the peaks to show that _average_scada works") p = p[:10] p_t, p_a = straxen.scada._average_scada(p['time'] / 1e9, p['time'], 1) assert len(p_a) == len(p), 'Scada deleted some of my 10 peaks!' print('Check the number of events') events = st.get_array(test_run_id, 'event_info') assertion_statement = ("Got less/ore events than expected, " "perhaps the test is outdated or something " "changed in the processing.") assert len(events) == EXPECTED_OUTCOMES_TEST_SEVERAL[ 'n_events'], assertion_statement # On windows, you cannot delete the current process' # working directory, so we have to chdir out first. finally: os.chdir('..')
def test_get_livetime_sec(self): st = self.st events = st.get_array(self.run_id, 'events') if len(events): straxen.get_livetime_sec(st, self.run_id, things=events)
def test_get_livetime_sec(self): st = self.st events = st.get_array(self.run_id, 'events') straxen.get_livetime_sec(st, test_run_id_1T, things=events)