Skip to content
Snippets Groups Projects
Commit 989586da authored by David Hendriks's avatar David Hendriks
Browse files

added tests and output parsing

parent f8bf42c9
No related branches found
No related tags found
No related merge requests found
File moved
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
File moved
......@@ -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
......
......@@ -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
......
import os, sys
import binary_c
# sys.path.append(os.path.dirname(file))
print(os.getenv('PYTHONPATH'))
#paths = os.getenv('PYTHONPATH').split(':' if os.name=='posix' else ';')
\ No newline at end of file
%% Cell type:code id: tags:
``` python
import binary_c
```
%% Cell type:code id: tags:
``` python
```
import os, sys
import matplotlib.pyplot as plt
import os, sys, time
# # Append root dir of this project to include functionality
# sys.path.append(os.path.dirname(os.getcwd()))
# print(sys.path)
# quit()
import matplotlib.pyplot as plt
from collections import defaultdict
import numpy as np
import pandas as pd
import binary_c
from utils.defaults import physics_defaults
from utils.functions import create_arg_string
from binaryc_python_utils.defaults import physics_defaults
from binaryc_python_utils.functions import create_arg_string, parse_output, run_system
"""
Script to test some auto reading out.
todo: make to hdf5
"""
start = time.time()
output = run_system(M_1=10, M_2=20, separation=0, orbital_period=100000000000)
result = parse_output(output, 'DAVID_SINGLE_ANALYSIS')
stop = time.time()
print("Took {:.2f}s to run single system".format(stop-start))
print("The following keys are present in the results:\n{}".format(result.keys()))
#### Now do whatever you want with it:
#t_res = np.asarray(result['t'], dtype=np.float64, order='C')
#m_res = np.asarray(result['mass'], dtype=np.float64, order='C')
# Cast the data into a dataframe.
df = pd.DataFrame.from_dict(result, dtype=np.float64)
sliced_df = df[df.t < 1000] # Cut off late parts of evolution
print(sliced_df["t"])
plt.plot(sliced_df['t'], sliced_df['radius'])
plt.xlabel('Time (Myr)')
plt.ylabel('Radius (Rsol)')
plt.show()
\ No newline at end of file
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment