Files
cristallina_analysis_package/tests/test_utils.py

61 lines
2.0 KiB
Python
Executable File

import pytest
import os
import numpy as np
import cristallina.utils as utils
from sfdata import SFDataFiles
from cristallina.utils import ROI, print_run_info, heuristic_extract_pgroup, gauss, find_nearest, xray_transmission,stand_table,check_pid_offset
def test_print(capsys):
print_run_info(185, base_path="tests/data/p20841/raw/")
captured = capsys.readouterr()
assert "17259343145" in captured.out
assert "scan_parameters" in captured.out
def test_collect_metadata():
test_pgroup_dir = "tests/data/p20841"
df = utils.collect_runs_metadata(test_pgroup_dir)
assert df.iloc[1]["user_tag"] == "PMS, Magnet at 78K, 400V excitation"
assert df.iloc[1]["start_pulseid"] == 17358560870
def test_ROI():
"""API Tests"""
r = ROI(left=1, right=2, top=4, bottom=2)
assert r.width == 1
assert r.height == 2
assert r.name is not None
def test_extract_pgroup():
cwd = os.getcwd()
os.chdir("tests/data/p20841/raw/")
assert heuristic_extract_pgroup() == '20841'
os.chdir(cwd)
def test_gauss():
# Test a random Gauss value and check whether the value didn't change in time
assert gauss(1.5,2.7,7,4.33,3) == 7.186044070987355
def test_find_nearest():
# Make an array of 1 to 100 and check that the nearest index is the value
a = np.linspace(0,99,100)
assert find_nearest(a,10.1) == 10
def test_xray_transmission():
T = xray_transmission(9000, 100e-6, material = 'Si')
assert T == 0.342385039732607
def test_check_pid_offset():
with SFDataFiles(f"tests/data/p20841/raw/run0205/data/acq*.h5") as data:
assert check_pid_offset(data,'SARFE10-PBPG050:HAMP-INTENSITY-CAL','SARFE10-PBIG050-EVR0:CALCI') == 0
# This test can only be run localy (github has no access to /sf/cristallina), therefore it's commented out.
# def test_stand_table():
# # Load run from a p-group where we have saved the stand table. First run recorded was number 27, so check that.
# table = stand_table(21563)
# assert table['run'][0] == 27