Skip to content

Commit

Permalink
get_detector_setuop
Browse files Browse the repository at this point in the history
  • Loading branch information
lcross-sandia committed Jun 22, 2023
1 parent 55cfffb commit 6422348
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
14 changes: 12 additions & 2 deletions riid/gadras/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import numpy as np
from numpy.random import Generator
from typing import List
import copy

from jsonschema import validate

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
57 changes: 38 additions & 19 deletions tests/config_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
---
Expand Down Expand Up @@ -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,
Expand All @@ -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}')
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

0 comments on commit 6422348

Please sign in to comment.