# -*- coding: utf-8 -*-
#
# This file is part of the ska-mid-cbf-mcs project
#
# Distributed under the terms of the BSD 3-Clause license.
# See LICENSE for more info.
from __future__ import annotations
import random
from abc import ABCMeta, abstractmethod
from pathlib import Path
from typing import Any, Dict, List, Tuple
import orjson
__all__ = ["ParameterGen"]
[docs]
class ParameterGen(metaclass=ABCMeta):
"""An abstract base class for Mid.CBF test parameter generation."""
parameter_output: List[Dict[str, Any]] # Parameter File to be output
def __init__(
self: ParameterGen,
seed: Any,
max_subarrays: int,
max_vcc_all_bands: int,
max_vcc_units: int,
max_fsp_units: int,
test_data_path: Path,
sys_param_template_path: Path,
):
"""
Initialize an instance of ParameterGen()
:param seed: seed for random.seed()
:param max_subarrays: max number of subarray devices to use in parameter generation
:param max_vcc_all_bands: max number of FHS VCC all bands devices to use in parameter generation
:param max_vcc_units: max number of VCC unit devices to use in parameter generation
:param max_fsp_units: max number of FSP unit devices to use in parameter generation
:param test_data_path: path to test data files
:param sys_param_template_path: path to template file for InitSysparam input
"""
self.seed = seed
random.seed(seed)
self.max_subarrays = max_subarrays
self.max_vcc_all_bands = max_vcc_all_bands
self.max_vcc_units = max_vcc_units
self.max_fsp_units = max_fsp_units
self.test_data_path = test_data_path
self.sys_param_template_path = sys_param_template_path
self.sys_param_dict = None
self.parameter_output = None
[docs]
def generate_sys_parameters(
self: ParameterGen,
vcc_ids: List[int],
k_range: Tuple[int],
tm_data: Dict[str, Any] | None = None,
) -> Dict[str, Any]:
"""
Generate InitSysParam input from template.
Only generates one set of sys params per ParameterGen instance.
"""
if self.sys_param_dict is None:
with self.sys_param_template_path.open("rb") as opened_file:
sys_param_template_dict = orjson.loads(opened_file.read())
sys_param_dict = {
"interface": sys_param_template_dict["interface"],
}
if tm_data is None:
sys_param_dict["dish_parameters"] = {}
for dish_id, vcc_data in sys_param_template_dict[
"dish_parameters"
].items():
if vcc_data["vcc"] in vcc_ids:
vcc_data["k"] = random.randint(k_range[0], k_range[1])
sys_param_dict["dish_parameters"][dish_id] = vcc_data
else:
sys_param_dict.update(tm_data)
self.sys_param_dict = sys_param_dict
return self.sys_param_dict
[docs]
@abstractmethod
def generate_parameters(self: ParameterGen) -> Dict[str, Any]:
"""
To be overriden in child class
Generates test parameters based on inputs specific to the test suite
"""
raise NotImplementedError("generate_parameters is abstract")