Skip to content
Snippets Groups Projects
scene_checks.py 2.16 KiB
from util import BaseCheckList, invalidate_records
import pandas as pd
import numpy as np

def hatch_check(frame, parameters):
    """
    Check that the hatch is open on sky views
    """
    if not np.in1d(['hatchOpen','sceneMirrorPosition'], frame.columns).all():
        return frame

    frame['hatch_check'] = ((frame.hatchOpen != 1) &
        (~frame.sceneMirrorPosition.isin([ord('H'), ord('A')]))) * 1

    return frame


def safing_check(frame, parameters):
    """
    Check that the mirror doesn't safe during a calibration view and contaminate other records
    """
    if not np.in1d(['hatchOpen','sceneMirrorPosition'], frame.columns).all():
        return frame

    hatch_closing = ((frame.hatchOpen == 1)  & ((frame.hatchOpen == -3).diff(-1) == 1)).shift(1)
    mirror_safing = (hatch_closing & frame.sceneMirrorPosition.isin([ord('H'), ord('A')]))
    frame['safing_check'] = mirror_safing * 1
    frame.ix[mirror_safing, 'qc_notes'] = 'mirror likely safed during view'
    frame = invalidate_records(frame, 'safing_check')
    
    return frame


def encoder_check(frame, parameters):
    return frame


class CheckList(BaseCheckList):

    checks = [hatch_check, safing_check, encoder_check]
    

#### TESTS #####

def test_hatch_check():
    frame = pd.DataFrame({
        'hatchOpen':[1,1,0],
        'sceneMirrorPosition':[ord('H'), ord('A'), ord('S')],
        'qc_notes':''
    })
    assert hatch_check(frame, {})['hatch_check'].values.tolist() == [0,0,1]

    frame = pd.DataFrame({
        'hatchOpen':[1,0,1],
        'sceneMirrorPosition':[ord('H'), ord('A'), ord('S')],
        'qc_notes':''
    })
    assert hatch_check(frame, {})['hatch_check'].values.tolist() == [0,0,0]
    

def test_safing_check():
    frame = pd.DataFrame({
        'hatchOpen':[0,0,0],
        'sceneMirrorPosition':[ord('H'), ord('A'), ord('S')],
        'qc_notes':''
    })
    assert safing_check(frame, {})['safing_check'].values.tolist() == [0,0,0]

    frame = pd.DataFrame({
        'hatchOpen':[1,-3,0,0],
        'sceneMirrorPosition':[ord('S'), ord('H'), ord('A'), ord('S')],
        'qc_notes':''
    })
    assert safing_check(frame, {})['safing_check'].values.tolist() == [1,1,0,1]