Skip to content
Snippets Groups Projects
state_checks.py 5.95 KiB
Newer Older
from util import BaseCheckList, invalidate_records, annotate_all, update_variable_qc
import pandas as pd
import numpy as np

def detector_check(frame, parameters):
    """
    Check that the detector temp is in range
    """
    if 'detectorTemp' not in frame.columns:
        return frame
Coda Phillips's avatar
Coda Phillips committed
    detector_temp_too_high = (frame['detectorTemp'] > 90)
    frame['detector_check'] = detector_temp_too_high * 1
    frame['qc_detectorTemp'] = detector_temp_too_high * 1
Coda Phillips's avatar
Coda Phillips committed
    annotate_all(frame, detector_temp_too_high, 'detector temperature too high')
    frame = invalidate_records(frame, 'detector_check')
    return frame

def hbb_thermistor_check(frame, parameters):
Coda Phillips's avatar
Coda Phillips committed
    return thermistor_check(frame, 'HBB', 331, 335)

def abb_thermistor_check(frame, parameters):
    return thermistor_check(frame, 'ABB', 150, 335)

def thermistor_check(frame, bb, low, high):
Coda Phillips's avatar
Coda Phillips committed
    Check that all thermistor temps are in range
Coda Phillips's avatar
Coda Phillips committed
    if not np.in1d([x.format(bb) for x in ['{}bottomTemp','{}apexTemp','{}topTemp']], frame.columns).all():
        return frame
Coda Phillips's avatar
Coda Phillips committed
    bbb_too_low = frame['{}bottomTemp'.format(bb)] < low
    bba_too_low = frame['{}apexTemp'.format(bb)] < low
    bbt_too_low = frame['{}topTemp'.format(bb)] < low
    bbb_too_high = frame['{}bottomTemp'.format(bb)] > high
    bba_too_high = frame['{}apexTemp'.format(bb)] > high
    bbt_too_high = frame['{}topTemp'.format(bb)] > high
    bbb_problem = bbb_too_low | bbb_too_high
    bba_problem = bba_too_low | bba_too_high
    bbt_problem = bbt_too_low | bbt_too_high

    # Record qc for each thermistor
    # qc variables are probabilites between 0 and 1
    variable_qcs = pd.DataFrame({
Coda Phillips's avatar
Coda Phillips committed
        'qc_{}bottomTemp'.format(bb) :bbb_problem * 1,
        'qc_{}apexTemp'.format(bb)  : bba_problem * 1,
        'qc_{}topTemp'.format(bb)  : bbt_problem * 1
    }, index=frame.index)
    frame = update_variable_qc(frame, variable_qcs)

    # Compute overall BB quality
Coda Phillips's avatar
Coda Phillips committed
    frame['{}_thermistor_check'.format(bb.lower())] = (bbb_problem | bba_problem | bbt_problem) * 1
    annotate_all(frame, bbb_too_low, '{} bottom temperature too low'.format(bb))
    annotate_all(frame, bbt_too_low, '{} top temperature too low'.format(bb))
    annotate_all(frame, bba_too_low, '{} apex temperature too low'.format(bb))
    annotate_all(frame, bbb_too_high, '{} bottom temperature too high'.format(bb))
    annotate_all(frame, bbt_too_high, '{} top temperature too high'.format(bb))
    annotate_all(frame, bba_too_high, '{} apex temperature too high'.format(bb))
    frame = invalidate_records(frame, '{}_thermistor_check'.format(bb.lower()))
    return frame

def hbb_stable_check(frame, parameters):
Coda Phillips's avatar
Coda Phillips committed
    interval_seconds = frame['datetime'].diff().astype(np.int64) / 1e9
    hbbb_diff = frame['HBBbottomTemp'].diff() / interval_seconds
    hbba_diff = frame['HBBapexTemp'].diff() / interval_seconds
    hbbt_diff = frame['HBBtopTemp'].diff() / interval_seconds
    hsr = parameters.get('hbb_stable_rate', .002)
    hbbb_diff_problem = abs(hbbb_diff.fillna(0)) > hsr
    hbba_diff_problem = abs(hbba_diff.fillna(0)) > hsr
    hbbt_diff_problem = abs(hbbt_diff.fillna(0)) > hsr

    variable_qcs = pd.DataFrame({
        'qc_HBBbottomTemp':hbbb_diff_problem * 1,
        'qc_HBBapexTemp' : hbba_diff_problem * 1,
        'qc_HBBtopTemp' : hbbt_diff_problem * 1
    }, index=frame.index)
    frame = update_variable_qc(frame, variable_qcs)

    frame['hbb_stable_check'] = (hbbb_diff_problem | hbbt_diff_problem | hbbt_diff_problem) * 1
    annotate_all(frame, hbbb_diff_problem, 'HBB bottom temperature not stable')
    annotate_all(frame, hbba_diff_problem, 'HBB apex temperature not stable')
    annotate_all(frame, hbbt_diff_problem, 'HBB top temperature not stable')
    frame = invalidate_records(frame, 'hbb_stable_check')
    return frame

class CheckList(BaseCheckList):
Coda Phillips's avatar
Coda Phillips committed
    checks = [detector_check, hbb_thermistor_check, hbb_stable_check, abb_thermistor_check]


#### TESTS ####

Coda Phillips's avatar
Coda Phillips committed
def test_hbb_stable_check():
    # Check with rates of .0016
    dummy_data = pd.DataFrame({
            'HBBbottomTemp':np.arange(0,2,.1),
            'HBBapexTemp':np.arange(0,2,.1),
            'HBBtopTemp':np.arange(0,2,.1),
            'datetime':pd.date_range('1/1/2000', periods=20, freq='60s'),
            'qc_notes':'',
            'qc_percent':0,
            'sceneMirrorPosition':ord('S')
        })
    frame = hbb_stable_check(
        dummy_data, {'hbb_stable_rate':.002}
    )
    assert all(frame['hbb_stable_check'] == 0)
    
    dummy_data.ix[10:,'HBBbottomTemp'] = np.arange(1,3,.2)
    dummy_data.ix[10:,'HBBtopTemp'] = np.arange(1,3,.2)
    dummy_data.ix[10:,'HBBbottomTemp'] = np.arange(1,3,.2)

    frame = hbb_stable_check(
        dummy_data, {'hbb_stable_rate':.002}
    )
    assert all(frame.ix[:10, 'hbb_stable_check'] == 0)
    assert all(frame.ix[11:, 'hbb_stable_check'] == 1)

def test_hbb_thermistor_check():
    frame = hbb_thermistor_check(pd.DataFrame({
        'HBBbottomTemp':[300,333,336],
        'HBBapexTemp':[300,333,336],
        'HBBtopTemp':[300,333,336],
Coda Phillips's avatar
Coda Phillips committed
        'sceneMirrorPosition':[ord('H'), ord('A'), ord('S')],
        'qc_notes':''
    }), {})
    assert all(frame['hbb_thermistor_check'] == [1,0,1])
Coda Phillips's avatar
Coda Phillips committed
    assert all('HBB {} temperature too low'.format(x) in frame.iloc[0].qc_notes.split(',') for x in ['bottom','apex','top'])
    assert all('HBB {} temperature too high'.format(x) in frame.iloc[2].qc_notes.split(',') for x in ['bottom','apex','top'])

def test_hbb_thermistor_check2():
    frame = hbb_thermistor_check(pd.DataFrame({
        'HBBbottomTemp':[300,333,333],
        'HBBapexTemp':[300,333,333],
        'HBBtopTemp':[300,333,333],
Coda Phillips's avatar
Coda Phillips committed
        'sceneMirrorPosition':[ord('H'), ord('A'), ord('S')],
        'qc_notes':''
    }), {})
    assert all(frame['hbb_thermistor_check'] == [1,0,1])

def test_detector_check():
    frame = detector_check(pd.DataFrame({
        'detectorTemp':[50,100],
Coda Phillips's avatar
Coda Phillips committed
        'sceneMirrorPosition':[ord('H'), ord('A')],
        'qc_notes':''
    }), {})
    assert all(frame['detector_check'] == [0,1])
Coda Phillips's avatar
Coda Phillips committed
    assert frame.iloc[1].qc_notes == 'detector temperature too high'