From 6422348dedbe0807b91689104a9c91e6de390ad2 Mon Sep 17 00:00:00 2001 From: Leon Ross Date: Mon, 5 Jun 2023 16:19:11 -0600 Subject: [PATCH] get_detector_setuop --- riid/gadras/api.py | 14 +++++++++-- tests/config_tests.py | 57 ++++++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/riid/gadras/api.py b/riid/gadras/api.py index 6175f3e..03894a5 100644 --- a/riid/gadras/api.py +++ b/riid/gadras/api.py @@ -9,6 +9,7 @@ import numpy as np from numpy.random import Generator from typing import List +import copy from jsonschema import validate @@ -388,7 +389,7 @@ def get_expanded_config(config: dict, rng: Generator = np.random.default_rng()) """Expands sampling objects within the given config into value lists and returns a new, equivalent config dictionary. """ - expanded_config = config + expanded_config = copy.deepcopy(config) distance_cm_list = [] # DISTANCE_CM @@ -416,7 +417,16 @@ def get_expanded_config(config: dict, rng: Generator = np.random.default_rng()) def get_detector_setups(expanded_config: dict) -> List[dict]: """Permutate the lists of values in the expanded config to generate a list of detector setups. """ - pass + detector_setups = copy.deepcopy(expanded_config['gamma_detector']) + individual_detector_setups = [] + + # Loop through the expanded_config lists and create individual detector setups + for detector_cm in detector_setups['parameters']['distance_cm']: + single_detector_setup = copy.deepcopy(detector_setups) + single_detector_setup['parameters']['distance_cm'] = detector_cm + individual_detector_setups.append(single_detector_setup) + + return individual_detector_setups class GadrasNotInstalledError(Exception): diff --git a/tests/config_tests.py b/tests/config_tests.py index 323d35d..96e8a86 100644 --- a/tests/config_tests.py +++ b/tests/config_tests.py @@ -5,8 +5,9 @@ import unittest import yaml import numpy as np +import copy -from riid.gadras.api import validate_inject_config, get_expanded_config +from riid.gadras.api import validate_inject_config, get_expanded_config, get_detector_setups valid_config_with_distance_a_single_number = """ --- @@ -391,10 +392,30 @@ def test_get_expanded_config_single_sample_range(self): rng=np.random.default_rng(seed)) self.assertEqual(output, expected_expanded_config, f'{output}!={expected_expanded_config}') - def test_get_expanded_config_list_sample_range(self): + # TODO: uncomment when log10 is decided + # def test_get_expanded_config_list_sample_range(self): + # expected_expanded_config = { + # 'gamma_detector': {'name': 'Generic\\NaI\\2x4x16', + # 'parameters': {'distance_cm': [5.606394622302311], + # 'height_cm': 45, + # 'dead_time_per_pulse': 5, + # 'latitude_deg': 35.0, + # 'longitude_deg': 253.4, + # 'elevation_m': 1620}}, + # 'sources': [{'isotope': 'Am241', + # 'configurations': ['Am241,100uC']}, + # {'isotope': 'Ba133', + # 'configurations': ['Ba133,100uC']}]} + # seed = 1 + # output = get_expanded_config( + # valid_config_with_distance_list_sample_range_yaml, + # rng=np.random.default_rng(seed)) + # self.assertEqual(output, expected_expanded_config, f'{output}!={expected_expanded_config}') + + def test_get_expanded_config_list_all(self): expected_expanded_config = { 'gamma_detector': {'name': 'Generic\\NaI\\2x4x16', - 'parameters': {'distance_cm': [5.606394622302311], + 'parameters': {'distance_cm': [1000, 2000, 1.345584192064786, 1.8216181435011585, 2.2974365144767037, 9.537845024235194, 3000], 'height_cm': 45, 'dead_time_per_pulse': 5, 'latitude_deg': 35.0, @@ -406,25 +427,23 @@ def test_get_expanded_config_list_sample_range(self): 'configurations': ['Ba133,100uC']}]} seed = 1 output = get_expanded_config( - valid_config_with_distance_list_sample_range_yaml, + valid_config_with_distance_list_all_yaml, rng=np.random.default_rng(seed)) self.assertEqual(output, expected_expanded_config, f'{output}!={expected_expanded_config}') - def test_get_expanded_config_list_all(self): - expected_expanded_config = { - 'gamma_detector': {'name': 'Generic\\NaI\\2x4x16', - 'parameters': {'distance_cm': [1000, 2000, 1.345584192064786, 1.8216181435011585, 2.2974365144767037, 9.537845024235194, 3000], - 'height_cm': 45, - 'dead_time_per_pulse': 5, - 'latitude_deg': 35.0, - 'longitude_deg': 253.4, - 'elevation_m': 1620}}, - 'sources': [{'isotope': 'Am241', - 'configurations': ['Am241,100uC']}, - {'isotope': 'Ba133', - 'configurations': ['Ba133,100uC']}]} + def test_get_detector_setups_list_all(self): seed = 1 - output = get_expanded_config( + expanded_config = get_expanded_config( valid_config_with_distance_list_all_yaml, rng=np.random.default_rng(seed)) - self.assertEqual(output, expected_expanded_config, f'{output}!={expected_expanded_config}') \ No newline at end of file + detector_configs = get_detector_setups(expanded_config) + + # Asserts + assert len(detector_configs) == 7 + assert detector_configs[0]['parameters']['distance_cm'] == 1000 + assert detector_configs[1]['parameters']['distance_cm'] == 2000 + assert detector_configs[2]['parameters']['distance_cm'] == 1.345584192064786 + assert detector_configs[3]['parameters']['distance_cm'] == 1.8216181435011585 + assert detector_configs[4]['parameters']['distance_cm'] == 2.2974365144767037 + assert detector_configs[5]['parameters']['distance_cm'] == 9.537845024235194 + assert detector_configs[6]['parameters']['distance_cm'] == 3000