diff --git a/binarycpython/__init__.py b/binarycpython/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..4e61002d8790367b28495e2049dc1e1951af7651 --- /dev/null +++ b/binarycpython/__init__.py @@ -0,0 +1,2 @@ +def ding(): + print("ding") diff --git a/binarycpython/utils/#functions.py# b/binarycpython/utils/#functions.py# new file mode 100644 index 0000000000000000000000000000000000000000..1917a3eb08e8e7ad6aae7fdc92d5cd20062cc392 --- /dev/null +++ b/binarycpython/utils/#functions.py# @@ -0,0 +1,198 @@ +from collections import defaultdict + + +import binary_c +from binarycpython.binaryc_python_utils +from binaryc_python_utils.custom_logging_functions import ( + create_and_load_logging_function, +) + + +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 get_defaults(): + """ + Function that calls the binaryc get args function and cast it into a dictionary + All the values are strings + """ + default_output = binary_c.return_arglines() + default_dict = {} + + for default in default_output.split("\n"): + if not default in ["__ARG_BEGIN", "__ARG_END", ""]: + key, value = default.split(" = ") + + # Filter out NULLS (not compiled anyway) + if not value in ["NULL", "Function"]: + if not value == "": + default_dict[key] = value + return default_dict + + +def get_arg_keys(): + """ + Function that return the list of possible keys to give in the arg string + """ + + return get_defaults().keys() + + +def run_system(**kwargs): + """ + Wrapper to run a system with settings + + This function determines which underlying python-c api function will be called based upon the arguments that are passed via kwargs. + + - if custom_logging_code or custom_logging_dict is included in the kwargs then it will + - if + + """ + + # Load default args + args = get_defaults() + if "custom_logging_code" in kwargs: + # Use kwarg value to override defaults and add new args + for key in kwargs.keys(): + if not key == "custom_logging_code": + args[key] = kwargs[key] + + # Generate library and get memaddr + func_memaddr = create_and_load_logging_function(kwargs["custom_logging_code"]) + + # Construct arguments string and final execution string + arg_string = create_arg_string(args) + arg_string = "binary_c {}".format(arg_string) + + # Run it and get output + output = binary_c.run_binary_custom_logging(arg_string, func_memaddr) + return output + + elif "log_filename" in kwargs: + # Use kwarg value to override defaults and add new args + for key in kwargs.keys(): + args[key] = kwargs[key] + + # Construct arguments string and final execution string + arg_string = create_arg_string(args) + arg_string = "binary_c {}".format(arg_string) + + # Run it and get output + output = binary_c.run_binary_with_logfile(arg_string) + return output + + else: # run the plain basic type + + # Use kwarg value to override defaults and add new args + for key in kwargs.keys(): + args[key] = kwargs[key] + + # Construct arguments string and final execution string + arg_string = create_arg_string(args) + arg_string = "binary_c {}".format(arg_string) + + # Run it and get output + output = binary_c.run_binary(arg_string) + + return output + + +def run_system_with_log(**kwargs): + """ + Wrapper to run a system with settings AND logs the files to a designated place defined by the log_filename parameter. + """ + + # Load default args + args = get_defaults() + # args = {} + + # 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(): + args[key] = kwargs[key] + + # Construct arguments string and final execution string + arg_string = create_arg_string(args) + arg_string = "binary_c {}".format(arg_string) + + # print(arg_string) + + # Run it and get output + buffer = "" + output = binary_c.run_binary_with_log(arg_string) + + return output + + +def parse_output(output, selected_header): + """ + Function that parses output of binary_c: + + This function works in two cases: + if the caught line contains output like 'example_header time=12.32 mass=0.94 ..' + or if the line contains output like 'example_header 12.32 0.94' + + You can give a 'selected_header' to catch any line that starts with that. + Then the values will be put into a dictionary. + + TODO: Think about exporting to numpy array or pandas instead of a defaultdict + """ + + 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] + values_list = split_line[1:] + + # print(values_list) + # Catch line starting with selected header + if header == selected_header: + # Check if the line contains '=' symbols: + value_dict = {} + if all('=' in el for el in values_list): + for el in values_list: + key, val = el.split("=") + value_dict[key.strip()] = val.strip() + value_dicts.append(value_dict) + else: + if any('=' in el for el in values_list): + raise ValueError('Caught line contains some = symbols but not all of them do. aborting run') + else: + for i, val in enumerate(values_list): + value_dict[i] = val + value_dicts.append(value_dict) + + if len(value_dicts) == 0: + print( + "Sorry, didnt find any line matching your header {}".format(selected_header) + ) + return None + + 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 diff --git a/binaryc_python_utils/__init__.py b/binarycpython/utils/__init__.py similarity index 100% rename from binaryc_python_utils/__init__.py rename to binarycpython/utils/__init__.py diff --git a/binaryc_python_utils/custom_logging_functions.py b/binarycpython/utils/custom_logging_functions.py similarity index 100% rename from binaryc_python_utils/custom_logging_functions.py rename to binarycpython/utils/custom_logging_functions.py diff --git a/binaryc_python_utils/functions.py b/binarycpython/utils/functions.py similarity index 100% rename from binaryc_python_utils/functions.py rename to binarycpython/utils/functions.py diff --git a/binaryc_python_utils/stellar_types.py b/binarycpython/utils/stellar_types.py similarity index 100% rename from binaryc_python_utils/stellar_types.py rename to binarycpython/utils/stellar_types.py diff --git a/setup.py b/setup.py index 3ed03641a4ca446b148fbe25cf6d04777eedb24e..78eb6c3d23fe3765af96f99577df19beaa09d2e0 100644 --- a/setup.py +++ b/setup.py @@ -89,14 +89,27 @@ def readme(): return f.read() setup( - name="binary_c", - version="1.0", + name="binarycpython", + version="0.1", description="This is a python API for binary_c by Rob Izzard and collaborators", author="Jeff Andrews and Robert Izzard and David Hendriks", author_email="andrews@physics.uoc.gr and r.izzard@surrey.ac.uk and rob.izzard@gmail.com and davidhendriks93@gmail.com", long_description=readme(), url="https://gitlab.eps.surrey.ac.uk/ri0005/binary_c-python", license="", + package_dir = { + 'binarycpython': 'binarycpython', + 'binarycpython.utils': 'binarycpython/utils', + }, + packages=['binarycpython', + 'binarycpython.utils', + ] + + + + + + # ext_modules=[ # binary_c_python_api_module # ], # binary_c must be loaded