Skip to content

Commit

Permalink
WIP towards requirements for PR 36
Browse files Browse the repository at this point in the history
  • Loading branch information
cfirth-nasa committed Sep 13, 2023
1 parent e5cfee4 commit 35650c5
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 16 deletions.
2 changes: 1 addition & 1 deletion onair/config/default_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ TelemetryFiles = ['700_crash_to_earth_1.csv']
ParserFileName = csv_parser
ParserName = CSV
SimName = CSV
PluginList = {'generic_plugin':'plugins/generic/generic_plugin.py'} # format: {plugin_name : plugin_path}
PluginList = {'generic_plugin':'plugins/generic/generic_plugin.py'}

[RUN_FLAGS]
IO_Flag = true
Expand Down
15 changes: 9 additions & 6 deletions onair/src/data_driven_components/data_driven_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@

class DataDrivenLearning:
def __init__(self, headers, _ai_plugins={}):
assert(len(headers)>0)
assert(len(headers)>0), 'Headers are required'
self.headers = headers
self.ai_constructs = []

for module_name in list(_ai_plugins.keys()):
spec = importlib.util.spec_from_file_location(module_name, _ai_plugins[module_name])
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
self.ai_constructs.append(module.Plugin(module_name,headers))
try:
spec = importlib.util.spec_from_file_location(module_name, _ai_plugins[module_name])
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
self.ai_constructs.append(module.Plugin(module_name,headers))
except AttributeError:
raise Exception(f'Path passed for module {module_name} is not pointing to a usable module file. Double check that the config points to a Python file with a class Plugin.')



def update(self, curr_data, status):
Expand Down
4 changes: 2 additions & 2 deletions onair/src/reasoning/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ..reasoning.diagnosis import Diagnosis

class Agent:
def __init__(self, plugin_list, vehicle):
def __init__(self, vehicle, plugin_list):
self.vehicle_rep = vehicle
self.learning_systems = DataDrivenLearning(self.vehicle_rep.get_headers(),plugin_list)
self.mission_status = self.vehicle_rep.get_status()
Expand All @@ -29,7 +29,7 @@ def reason(self, frame):

def diagnose(self, time_step):
""" Grab the mnemonics from the """
learning_system_results = self.learning_systems.render_reasoning()
learning_system_results = self.learning_systems.render_reasoning()
diagnosis = Diagnosis(time_step,
learning_system_results,
self.bayesian_status,
Expand Down
11 changes: 9 additions & 2 deletions onair/src/run_scripts/execution_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,15 @@ def parse_configs(self, config_filepath):
self.benchmarkIndices = config['DEFAULT']['BenchmarkIndices']
except:
pass
## Sort Data: Plugins
self.plugin_list = ast.literal_eval(config['DEFAULT']['PluginList'])
## Plugins
temp_plugin_list = ast.literal_eval(config['DEFAULT']['PluginList'])
if len(temp_plugin_list.keys()) == 0:
raise AttributeError(f'No plugins have been specified in the config. Please specify a plugin in the PluginList dictionary with key of form plugin_name and value of form path/to/plugin')
for plugin_name in temp_plugin_list.keys():
if not(os.path.exists(temp_plugin_list[plugin_name])):
raise FileNotFoundError(f'Path given for {plugin_name} does not exist or is formatted incorrectly.')
self.plugin_list = temp_plugin_list


def parse_data(self, parser_name, parser_file_name, dataFilePath, metadataFilePath, subsystems_breakdown=False):
parser = importlib.import_module('onair.data_handling.parsers.' + parser_file_name)
Expand Down
2 changes: 1 addition & 1 deletion onair/src/run_scripts/sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self, simType, parsedData, plugin_list, SBN_Flag):

else:
self.simData = DataSource(parsedData.get_sim_data())
self.agent = Agent(plugin_list,vehicle)
self.agent = Agent(vehicle, plugin_list)

def run_sim(self, IO_Flag=False, dev_flag=False, viz_flag = True):
if IO_Flag == True: print_sim_header()
Expand Down
13 changes: 13 additions & 0 deletions test/onair/src/data_driven_components/test_data_driven_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ def test_DataDrivenLearning__init__sets_instance_headers_to_given_headers_and_do
# Assert
assert cut.headers == arg_headers

def test_DataDrivenLearning__init__throws_AttributeError_when_given_module_file_has_no_attribute_Plugin(mocker):
# Arrange
fake_module_name = MagicMock()
arg_headers = []

arg__ai_plugins = {MagicMock()}



# Act

# Assert

def test_DataDrivenLearning__init__sets_instance_ai_constructs_to_a_list_of_the_calls_AIPlugIn_with_plugin_and_given_headers_for_each_item_in_given__ai_plugins_when_given__ai_plugins_is_occupied(mocker):
# Arrange
arg_headers = []
Expand Down
2 changes: 1 addition & 1 deletion test/onair/src/reasoning/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_Agent__init__sets_vehicle_rep_to_given_vehicle_and_learning_systems_and
cut = Agent.__new__(Agent)

# Act
result = cut.__init__(fake_plugin_list, arg_vehicle)
result = cut.__init__(arg_vehicle, fake_plugin_list)

# Assert
assert cut.vehicle_rep == arg_vehicle
Expand Down
10 changes: 9 additions & 1 deletion test/onair/src/run_scripts/test_execution_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,22 @@ def test_ExecutionEngine_parse_configs_bypasses_benchmarks_when_access_raises_er
fake_Dev_flags = MagicMock()
fake_SBN_flags = MagicMock()
fake_Viz_flags = MagicMock()
fake_plugin_dict = dict(MagicMock())
fake_plugin_dict = MagicMock()

fake_keys = MagicMock()
fake_keys2 = MagicMock()

fake_keys.__len__.return_value = 1
fake_keys2.__iter__.return_value = iter(list([0,0]))

cut = ExecutionEngine.__new__(ExecutionEngine)

mocker.patch(execution_engine.__name__ + '.configparser.ConfigParser', return_value=fake_config)
mocker.patch.object(fake_config, 'read', return_value=fake_config_read_result)
mocker.patch.object(fake_run_flags, 'getboolean', side_effect=[fake_IO_flags, fake_Dev_flags, fake_SBN_flags, fake_Viz_flags])
mocker.patch('ast.literal_eval',return_value=fake_plugin_dict)
mocker.patch.object(fake_plugin_dict, 'keys', return_value=fake_keys2)


# Act
cut.parse_configs(arg_config_filepath)
Expand Down
4 changes: 2 additions & 2 deletions test/onair/src/run_scripts/test_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_Simulator__init__creates_Vehicle_and_DataSource_from_parsed_data_and_Ag
assert sim.DataSource.call_count == 1
assert sim.DataSource.call_args_list[0].args == (fake_sim_data, )
assert sim.Agent.call_count == 1
assert sim.Agent.call_args_list[0].args == (arg_plugin_list, fake_vehicle)
assert sim.Agent.call_args_list[0].args == (fake_vehicle, arg_plugin_list)
assert cut.agent == fake_agent

def test_Simulator__init__creates_Vehicle_and_AdapterDataSource_from_parsed_data_and_Agent_with_vehicle_when_SBN_Flag_resolves_to_True(mocker):
Expand Down Expand Up @@ -143,7 +143,7 @@ def connect(self):
assert FakeDataAdapterSource.sim_data == fake_sim_data
assert FakeDataAdapterSource.connect_call_count == 1
assert sim.Agent.call_count == 1
assert sim.Agent.call_args_list[0].args == (fake_plugin_list,fake_vehicle)
assert sim.Agent.call_args_list[0].args == (fake_vehicle, fake_plugin_list)
assert cut.agent == fake_agent

# run_sim tests
Expand Down

0 comments on commit 35650c5

Please sign in to comment.