Skip to content

Commit

Permalink
Merge branch 'master' into wf_plotting
Browse files Browse the repository at this point in the history
  • Loading branch information
JoranAngevaare committed Nov 30, 2020
2 parents 8b01a40 + a9b389d commit ddef339
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 1 deletion.
Empty file added tests/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ def test_straxen():

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, things=events)
# TODO: find a way to break up the tests
# surely pytest has common startup/cleanup?

Expand Down
2 changes: 1 addition & 1 deletion tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np
from immutabledict import immutabledict
from strax.testutils import run_id, recs_per_chunk
import os

# Number of chunks for the dummy raw records we are writing here
N_CHUNKS = 2
Expand Down Expand Up @@ -98,7 +99,6 @@ def _run_plugins(st,
int(strax.SaveWhen.TARGET)):
is_stored = st.is_stored(run_id, p)
assert is_stored, f"{p} did not save correctly!"

print("Wonderful all plugins work (= at least they don't fail), bye bye")


Expand Down
111 changes: 111 additions & 0 deletions tests/test_several.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
"""Test several functions distibuted over common.py, misc.py, scada.py"""
import straxen
import pandas
import os
import tempfile
from .test_basics import test_run_id
import numpy as np


def test_pmt_pos_1t():
"""
Test if we can get the 1T PMT positions
"""
pandas.DataFrame(straxen.pmt_positions(True))


def test_pmt_pos_nt():
"""
Test if we can get the nT PMT positions
"""
pandas.DataFrame(straxen.pmt_positions(False))


def test_secret():
"""
Check something in the sectets. This should not work because we
don't have any.
"""
try:
straxen.get_secret('somethingnonexistent')
except ValueError:
# Good we got some message we cannot load something that does
# not exist,
pass


# If one of the test below fail, perhaps these values need to be updated.
# They were added on 27/11/2020 and may be outdated by now
EXPECTED_OUTCOMES_TEST_SEVERAL = {
'n_peaks': 138,
'n_s1': 4,
'run_live_time': 0.17933107,
'n_events': 2
}


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

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_plots():
"""Make some plots"""
c = np.ones(straxen.n_tpc_pmts)
straxen.plot_pmts(c)
straxen.plot_pmts(c, log_scale=True)

0 comments on commit ddef339

Please sign in to comment.