Commit 989586da authored by David Hendriks's avatar David Hendriks
Browse files

added tests and output parsing

parent f8bf42c9
Loading
Loading
Loading
Loading
+0 −0

File moved.

+84 −0
Original line number Diff line number Diff line
import binary_c

from collections import defaultdict
from binaryc_python_utils.defaults import physics_defaults

def create_arg_string(arg_dict):
    """
    Function that creates the arg string
    """

    arg_string = '' 
    for key in arg_dict.keys():
        arg_string += "{key} {value} ".format(key=key, value=arg_dict[key])
    arg_string = arg_string.strip()
    return arg_string

def run_system(**kwargs):
    """
    Wrapper to run a system with settings 
    """

    # Load args
    physics_args = physics_defaults.copy()

    # For example
    # physics_args['M_1'] = 20
    # physics_args['separation'] = 0 # 0 = ignored, use period
    # physics_args['orbital_period'] = 100000000000 # To make it single

    # Use kwarg value to override defaults and add new args
    for key in kwargs.keys():
        physics_args[key] = kwargs[key]

    # Construct arguments string and final execution string
    arg_string = create_arg_string(physics_args)
    arg_string = f'binary_c {arg_string}' 

    # Run it and get output
    buffer = ""
    output = binary_c.run_binary(arg_string)

    return output


def parse_output(output, selected_header):
    """
    Function that parses output of binaryc when it is construction like this:
    DAVID_SINGLE_ANALYSIS t=0 mass=20 

    You can give a 'selected_header' to catch any line that starts with that. 
    Then the values will be put into a 
    """
    value_dicts = []
    val_lists = []

    # split output on newlines
    for i, line in enumerate(output.split('\n')):
        # Skip any blank lines
        if not line=='':
            split_line = line.split()

            # Select parts
            header = split_line[0]
            value_array = split_line[1:]

            # Catch line starting with selected header
            if header==selected_header:
                # print(value_array)
                # Make a dict 
                value_dict = {}
                for el in value_array:
                    key, val = el.split('=')
                    value_dict[key] = val
                value_dicts.append(value_dict)

    keys = value_dicts[0].keys()

    # Construct final dict.
    final_values_dict = defaultdict(list)
    for value_dict in value_dicts:
        for key in keys:
            final_values_dict[key].append(value_dict[key])

    return final_values_dict
 No newline at end of file
+0 −3
Original line number Diff line number Diff line
@@ -4,10 +4,7 @@ import matplotlib.pyplot as plt
# Append root dir of this project to include functionality
sys.path.append(os.path.dirname(os.getcwd()))
import binary_c
<<<<<<< HEAD
=======

>>>>>>> 19cf329fcbd85a0dff06eaec60030d6bf3ebc0b0
from utils.defaults import physics_defaults
from utils.functions import create_arg_string

+1 −1
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ import os
import binary_c
import matplotlib.pyplot as plt

from utils.defaults import physics_defaults
from binaryc_python_utils.defaults import physics_defaults

############################################################
# Test script to run a binary using the binary_c Python
Loading