All checks were successful
gitea-physics/kalpa/pipeline/head This commit looks good
132 lines
4.1 KiB
Python
132 lines
4.1 KiB
Python
import re
|
|
import kalpaa.read_bin_csv
|
|
import pathlib
|
|
import dataclasses
|
|
import logging
|
|
import typing
|
|
import numpy
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
TEST_DATA_DIR = pathlib.Path(__file__).resolve().parent / "test_files"
|
|
|
|
|
|
def test_regex_matches():
|
|
apsd_v_1 = "APSD_V_dot1_mean"
|
|
|
|
actual_match1 = re.match(kalpaa.read_bin_csv.BINNED_HEADER_REGEX, apsd_v_1)
|
|
|
|
# For reference, REGEX is currently: APSD_(?P<measurement_type>\w+)_(?P<dot_name>\w+)_(?P<summary_stat>mean|stdev)\s*
|
|
assert actual_match1 is not None
|
|
groups = actual_match1.groupdict()
|
|
assert groups["measurement_type"] == "V"
|
|
assert groups["dot_name"] == "dot1"
|
|
assert groups["summary_stat"] == "mean"
|
|
|
|
|
|
def test_parse_headers(snapshot):
|
|
example_headers = [
|
|
# using these headers from recent run: APSD_V_dot1_mean, APSD_V_dot1_stdev, APSD_V_dot2_mean, APSD_V_dot2_stdev, APSD_V_line_mean, APSD_V_line_stdev, APSD_V_triangle1_mean, APSD_V_triangle1_stdev, APSD_V_triangle2_mean, APSD_V_triangle2_stdev, APSD_V_uprise1_mean, APSD_V_uprise1_stdev, APSD_V_uprise2_mean, APSD_V_uprise2_stdev
|
|
"APSD_V_dot1_mean",
|
|
"APSD_V_dot1_stdev",
|
|
"APSD_V_dot2_mean",
|
|
"APSD_V_dot2_stdev",
|
|
"APSD_V_line_mean",
|
|
"APSD_V_line_stdev",
|
|
"APSD_V_triangle1_mean",
|
|
"APSD_V_triangle1_stdev",
|
|
"APSD_V_triangle2_mean",
|
|
"APSD_V_triangle2_stdev",
|
|
"APSD_V_uprise1_mean",
|
|
"This is not a valid header",
|
|
"CPSD_correlation_V_dot1_dot2_mean",
|
|
"CPSD_correlation_V_dot1_dot2_stdev",
|
|
"CPSD_phase_V_dot1_dot2_mean",
|
|
"CPSD_phase_V_dot1_dot2_stdev",
|
|
]
|
|
|
|
# force logger to be used for now
|
|
_logger.debug("parsing headers for test")
|
|
|
|
def null_asdict(dataclz) -> typing.Optional[dict]:
|
|
if dataclz is None:
|
|
return None
|
|
return dataclasses.asdict(dataclz)
|
|
|
|
actual_parsed = [
|
|
null_asdict(kalpaa.read_bin_csv._parse_bin_header(h)) for h in example_headers
|
|
]
|
|
assert actual_parsed == snapshot
|
|
|
|
|
|
def test_binned_data_dot_measurement(snapshot):
|
|
|
|
dots_json = TEST_DATA_DIR / "dots.json"
|
|
csv_file = TEST_DATA_DIR / "test_binned_apsd_V.csv"
|
|
|
|
actual_read = kalpaa.read_bin_csv.read_dots_and_binned(dots_json, csv_file)
|
|
|
|
assert dataclasses.asdict(actual_read) == snapshot
|
|
|
|
|
|
def test_binned_data_dot_measurement_costs(snapshot):
|
|
|
|
dots_json = TEST_DATA_DIR / "dots.json"
|
|
v_csv_file = TEST_DATA_DIR / "test_binned_apsd_V.csv"
|
|
ex_csv_file = TEST_DATA_DIR / "test_binned_apsd_Ex.csv"
|
|
|
|
# it's overkill but while we have the mental model of what the input form is of these numpy arrays we should record it!
|
|
test_dipole_1 = [10, 20, 30, 0.4, 0.5, 0.6, 0.7]
|
|
test_dipole_2 = [15, 25, 35, -4, 0, 0, 0.11]
|
|
test_dipoles_configuration_1 = [test_dipole_1]
|
|
test_one_dipole_config2 = [test_dipole_2]
|
|
all_one_dipole_configs = numpy.array(
|
|
[test_dipoles_configuration_1, test_one_dipole_config2]
|
|
)
|
|
|
|
test_two_dipole_config1 = [test_dipole_1, test_dipole_2]
|
|
all_two_dipole_configs = numpy.array([test_two_dipole_config1])
|
|
|
|
binned_v = kalpaa.read_bin_csv.read_dots_and_binned(dots_json, v_csv_file)
|
|
measurements_v = binned_v.measurements(["dot1"])
|
|
|
|
binned_ex = kalpaa.read_bin_csv.read_dots_and_binned(dots_json, ex_csv_file)
|
|
measurements_ex = binned_ex.measurements(["dot1"])
|
|
_logger.warning(measurements_ex)
|
|
|
|
v_log_noise_stdev_cost_func = binned_v._stdev_cost_function(measurements_v, True)
|
|
ex_log_noise_stdev_cost_func = binned_ex._stdev_cost_function(measurements_ex, True)
|
|
|
|
v_linear_noise_stdev_cost_func = binned_v._stdev_cost_function(
|
|
measurements_v, False
|
|
)
|
|
ex_linear_noise_stdev_cost_func = binned_ex._stdev_cost_function(
|
|
measurements_ex, False
|
|
)
|
|
|
|
result_dict = {
|
|
"log": {
|
|
"v": {
|
|
"one": v_log_noise_stdev_cost_func(all_one_dipole_configs),
|
|
"two": v_log_noise_stdev_cost_func(all_two_dipole_configs),
|
|
},
|
|
"ex": {
|
|
"one": ex_log_noise_stdev_cost_func(all_one_dipole_configs),
|
|
"two": ex_log_noise_stdev_cost_func(all_two_dipole_configs),
|
|
},
|
|
},
|
|
"linear": {
|
|
"v": {
|
|
"one": v_linear_noise_stdev_cost_func(all_one_dipole_configs),
|
|
"two": v_linear_noise_stdev_cost_func(all_two_dipole_configs),
|
|
},
|
|
"ex": {
|
|
"one": ex_linear_noise_stdev_cost_func(all_one_dipole_configs),
|
|
"two": ex_linear_noise_stdev_cost_func(all_two_dipole_configs),
|
|
},
|
|
},
|
|
}
|
|
|
|
assert result_dict == snapshot
|