Skip to content
Snippets Groups Projects
Commit 04bbe979 authored by Coda Phillips's avatar Coda Phillips
Browse files

Add some tests and checks

parent 3dd58491
No related branches found
No related tags found
No related merge requests found
from util import BaseCheckList
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)
frame['safing_check'] = hatch_closing & frame.sceneMirrorPosition.isin([ord('H'), ord('A')])
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')]
})
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')]
})
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')]
})
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')]
})
assert safing_check(frame, {})['safing_check'].values.tolist() == [1,1,0,1]
from util import BaseCheckList
from util import BaseCheckList, invalidate_records
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
frame['detector_check'] = (frame['detectorTemp'] > 90) * 1
frame = invalidate_records(frame, 'detector_check')
return frame
def hbb_thermistor_check(frame, parameters):
"""
Check that all HBB thermistor temps are in range
"""
if not np.in1d(['HBBbottomTemp','HBBapexTemp','HBBtopTemp'], frame.columns).all():
return frame
frame['hbb_thermistor_check'] = (
(abs(frame['HBBbottomTemp'] - 333) > 2) |
(abs(frame['HBBapexTemp'] - 333) > 2) |
(abs(frame['HBBtopTemp'] - 333) > 2)
) * 1
frame = invalidate_records(frame, 'hbb_thermistor_check')
return frame
def hbb_stable_check(frame, parameters):
......@@ -11,3 +31,32 @@ def hbb_stable_check(frame, parameters):
class CheckList(BaseCheckList):
checks = [detector_check, hbb_thermistor_check, hbb_stable_check]
#### TESTS ####
def test_hbb_thermistor_check():
frame = hbb_thermistor_check(pd.DataFrame({
'HBBbottomTemp':[300,333,336],
'HBBapexTemp':[300,333,336],
'HBBtopTemp':[300,333,336],
'sceneMirrorPosition':[ord('H'), ord('A'), ord('S')]
}), {})
assert all(frame['hbb_thermistor_check'] == [1,0,1])
def test_hbb_thermistor_check2():
frame = hbb_thermistor_check(pd.DataFrame({
'HBBbottomTemp':[300,333,333],
'HBBapexTemp':[300,333,333],
'HBBtopTemp':[300,333,333],
'sceneMirrorPosition':[ord('H'), ord('A'), ord('S')]
}), {})
assert all(frame['hbb_thermistor_check'] == [1,0,1])
def test_detector_check():
frame = detector_check(pd.DataFrame({
'detectorTemp':[50,100],
'sceneMirrorPosition':[ord('H'), ord('A')]
}), {})
assert all(frame['detector_check'] == [0,1])
from main import check_frame
from util import invalidate_record, invalidate_records
import pandas as pd
......@@ -9,3 +10,33 @@ def test_it_works():
qc_frame = check_frame(random_dataframe, {})
assert 'qc_percent' in qc_frame.columns
print(qc_frame)
def test_invalidate_record():
invalidated = invalidate_record(three_scenes.copy(), 2, 'test', .5)
assert invalidated.iloc[2].test == .5
assert pd.np.isnan(invalidated.iloc[3].test)
assert pd.np.isnan(invalidated.iloc[1].test)
tricky_index = three_scenes.copy()
tricky_index.index = tricky_index.index - 1
invalidated = invalidate_record(tricky_index, 2, 'test', .5)
assert invalidated.iloc[2].test == .5, invalidated.test
assert pd.np.isnan(invalidated.iloc[3].test)
assert pd.np.isnan(invalidated.iloc[1].test)
corrupt_cal = three_scenes.copy()
invalidated = invalidate_record(corrupt_cal, 3, 'test', .5)
assert invalidated.iloc[2].test == .5, invalidated.test
assert invalidated.iloc[5].test == .5, invalidated.test
def test_invalidate_records():
corrupt_cal = three_scenes.copy()
corrupt_cal['test'] = 0
corrupt_cal['test'][3] = 1
invalidated = invalidate_records(corrupt_cal, 'test')
assert invalidated.iloc[2].test == 1, invalidated.test
assert invalidated.iloc[5].test == 1, invalidated.test
three_scenes = pd.DataFrame({'sceneMirrorPosition': list(map(ord, 'HASAHSAHSAH'))})
from itertools import takewhile
import numpy as np
import pandas as pd
def invalidate_record(frame, record, reason):
pass
def invalidate_records(frame, check_name):
for index,percent in frame.ix[frame[check_name] > 0, check_name].iteritems():
invalidate_record(frame, index, check_name, percent)
return frame
def invalidate_record(frame, index, check_name, value):
frame.ix[frame.index[index], check_name] = value
corrupt_view = frame.iloc[index].sceneMirrorPosition
if corrupt_view in [ord('H'),ord('A')]:
# Corrupt calibration view, must also invalidate neighboring scenes
_idx = index + 1
while _idx < len(frame):
if frame.sceneMirrorPosition.iloc[_idx] == corrupt_view:
# Made one cycle
break
elif frame.sceneMirrorPosition.iloc[_idx] in [ord('H'), ord('A')]:
# Skip opposite calibration views
_idx += 1
continue
else:
# Invalidate non-calibration views
frame.ix[frame.index[_idx], check_name] = value
_idx += 1
_idx = index - 1
while _idx >= 0:
if frame.sceneMirrorPosition.iloc[_idx] == corrupt_view:
# Made one cycle
break
elif frame.sceneMirrorPosition.iloc[_idx] in [ord('H'), ord('A')]:
# Skip opposite calibration views
_idx -= 1
continue
else:
# Invalidate non-calibration views
frame.ix[frame.index[_idx], check_name] = value
_idx -= 1
return frame
class BaseCheckList:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment