Commit 6904c611 authored by David Hendriks's avatar David Hendriks
Browse files

added function to parse arguments via commandline, added parsing of version...

added function to parse arguments via commandline, added parsing of version history and and the loading of the generated grid function
parent 633f1dee
Loading
Loading
Loading
Loading
+89 −0
Original line number Diff line number Diff line
@@ -10,6 +10,95 @@ from binarycpython.utils.custom_logging_functions import (
    create_and_load_logging_function,
)

def parse_binary_c_version_info(version_info_string):
    version_info_dict = {}

    for el in version_info_string.splitlines():
        el = el.strip()
        if el =='':
            continue
        if ' is ' in el:
            split = el.split(" is ")
            version_info_dict[split[0].strip()] = split[1].strip()
        else:
            if el.startswith('Binary_c/nucsyn'):
                version_info_dict['intro'] = el
            elif el.startswith('Email'):
                emails = el.split('Email ')[1].split(',')
                cleaned_emails = [email.strip() for email in emails]
                version_info_dict['emails'] = cleaned_emails
            elif el.startswith('DTlimit'):
                split = el.split(' : ')
                version_info_dict[split[0]] = ': '.join(split[1:])
            elif el.startswith('Version'):
                split = el.split("Version ")
                version_number = split[1]
                version_info_dict['version_number'] = version_number
            elif el.startswith("git URL"):
                split = el.split("git URL ")
                git_url = split[1]
                version_info_dict['git_url'] = git_url
            elif el.startswith("Build: "):
                split = el.split('Build: ')
                build = split[1]
                version_info_dict['build'] = build
            elif el.startswith("Compiled for "):
                split = el.split('Compiled for ')
                compiled_for = split[1]
                version_info_dict['compiled_for'] = compiled_for
            elif el.startswith("Stack limit "):
                split = el.split('Stack limit ')
                stack_limit = split[1]
                version_info_dict['stack_limit'] = stack_limit
            elif el.startswith('SVN URL '):
                split = el.split('SVN URL ')
                svn_url = split[1]
                version_info_dict['svn_url'] = svn_url
            elif el.startswith('git branch '):
                split = el.split('git branch ')
                git_branch = split[1]
                version_info_dict['git_branch'] = git_branch
            elif el.startswith('_SC_CLK_TCK'):
                split = el.split(' = ')
                _SC_CLK_TCK = split[1]
                version_info_dict['_SC_CLK_TCK'] = _SC_CLK_TCK
            elif el.startswith('Random number mean '):
                split = el.split('Random number mean ')
                random_number_mean = split[1]
                version_info_dict['Random number mean'] = random_number_mean
            elif el.startswith('SVN revision '):
                split = el.split('SVN revision ')
                svn_revision = split[1]
                version_info_dict['svn_revision'] = svn_revision
            elif el.startswith('Size of :'):
                split = el.split('Size of :')
                data_type_sizes = split[1]
                version_info_dict['data_type_sizes'] = data_type_sizes
            elif el.startswith('git revision '):
                split = el.split('git revision ')
                git_revision = split[1]
                version_info_dict['git_revision'] = git_revision
            elif el.startswith('BINARY_C_PRE_VERSION '):
                split = el.split('BINARY_C_PRE_VERSION ')
                binary_c_pre_version = split[1]
                version_info_dict['binary_c_pre_version'] = binary_c_pre_version
            elif el.startswith('Comenv accretion:'):
                split = el.split('Comenv accretion:')
                comenv_accretion = split[1]
                version_info_dict['comenv_accretion'] = comenv_accretion
            elif el.startswith('Compiled in parameters:'):
                split = el.split('Compiled in parameters:')
                compiled_in_parameters = split[1]
                version_info_dict['compiled_in_parameters'] = compiled_in_parameters
            elif el.startswith('__short__ is'):
                split = el.split('__short__ is')
                short_type = split[1]
                version_info_dict['short_type'] = short_type
            else:
                print("Still found unmatched items!:\n{}".format(repr(el)))

    return version_info_dict


def create_hdf5(data_dir):
    """
+66 −16
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ from binarycpython.utils.custom_logging_functions import (
    create_and_load_logging_function,
    temp_custom_logging_dir,
)
from binarycpython.utils.functions import get_defaults
from binarycpython.utils.functions import get_defaults, parse_binary_c_version_info


# TODO list
@@ -100,6 +100,39 @@ class Population(object):
                )
                self.custom_options[key] = kwargs[key]


    def parse_cmdline(self):
        """
        Function to handle settings values via the command line:
        TODO: remove the need for --cmdline
        """

        import argparse
        parser = argparse.ArgumentParser()
        parser.add_argument('--cmdline', help='Setting values via the commandline. Input like --cmdline "metallicity=0.02"')
        args = parser.parse_args()


        # How its set up now is that as input you need to give --cmdline "metallicity=0.002"
        # Its checked if this exists and handled accordingly.
        if args.cmdline:
            # Grab the input and split them up, while accepting only non-empty entries
            cmdline_args = args.cmdline
            split_args = [cmdline_arg for cmdline_arg in cmdline_args.split(' ') if not cmdline_arg=='']

            # Make dict and fill it
            cmdline_dict = {}
            for cmdline_arg in split_args:
                split = cmdline_arg.split('=')
                parameter = split[0]
                value=split[1]

                # Add to dict
                cmdline_dict[parameter] = value

            # unpack the dictionary into the setting function that handles where the values are set
            self.set(**cmdline_dict)

    def return_argline(self, parameter_dict=None):
        """
        Function to create the string for the arg line from a parameter dict
@@ -200,8 +233,6 @@ class Population(object):
    def return_binary_c_version_info(self, parsed=False):
        """
        Function that returns the version information of binary_c
    
        TODO: Put in a nice dict. 
        """

        version_info = binary_c_python_api.return_version_info().strip()
@@ -211,13 +242,6 @@ class Population(object):

        return version_info

    def parse_binary_c_version_info(self):
        """
        Function that parses the output of the version info that binary_c gives. This again is alot of string manipulation. Sensitive to breaking somewhere along the line. 
        """
        # TODO: write parsing function
        pass

    def return_binary_c_defaults(self):
        """
        Function that returns the defaults of the binary_c version that is used.
@@ -232,11 +256,13 @@ class Population(object):

        from binarycpython.utils.functions import get_help_all

        # 
        population_settings = self.return_population_settings()
        binary_c_defaults = self.return_binary_c_defaults()
        binary_c_version_info = self.return_binary_c_version_info()
        binary_c_version_info = self.return_binary_c_version_info(parsed=True)
        binary_c_help_all_info = get_help_all(print_help=False, return_dict=True)

        # 
        all_info = {}
        all_info["population_settings"] = population_settings
        all_info["binary_c_defaults"] = binary_c_defaults
@@ -445,6 +471,7 @@ class Population(object):
        # TODO: make a generator for this.  
        # TODO: Add correct logging everywhere
        # TODO: add different types of grid. 
        # TODO: Load the generated file and execute.
        """

        code_string = ""
@@ -455,19 +482,27 @@ class Population(object):

        # TODO: add imports
        # TODO: 

        #
        code_string += "from binarycpython.utils.probability_distributions import *\n"  
        code_string += "import math\n"
        code_string += "import numpy as np\n"
        code_string += "\n\n"

        code_string += "starcount = 0\n"
        code_string += "probabilities = {}\n"
        code_string += "parameter_dict = {}\n"

        code_string += "def grid_code():\n"

        depth += 1

        # 
        code_string += indent*depth + "starcount = 0\n"
        code_string += indent*depth + "probabilities = {}\n"
        code_string += indent*depth + "parameter_dict = {}\n"
        
        # Prepare the probability
        for el in sorted(self.grid_options["grid_variables"].items(), key=lambda x: x[1]['grid_variable_number']):
            grid_variable = el[1]
            code_string += 'probabilities["{}"] = 0\n'.format(grid_variable['parameter_name'])
            code_string += indent*depth + 'probabilities["{}"] = 0\n'.format(grid_variable['parameter_name'])

        # Generate code
        print("Generating grid code")
@@ -498,14 +533,29 @@ class Population(object):
            depth += 1

        # placeholder for calls to threading
        code_string += indent * (depth) + 'print(parameter_dict)\n'
        # starcount
        code_string += indent * (depth) + 'starcount += 1\n'
        code_string += indent * (depth) + 'print(probabilities)\n'
        code_string += indent * (depth) + 'print("starcount: ", starcount)\n'
        code_string += indent * (depth) + 'yield(parameter_dict)\n'

        # Write to file
        gridcode_filename = os.path.join(temp_custom_logging_dir(), 'example_grid.py')
        with open(gridcode_filename, 'w') as f:
            f.write(code_string)

    def load_grid_function(self):
        """
        Test function to run grid stuff. mostly to test the import
        """
        import importlib.util
        spec = importlib.util.spec_from_file_location("binary_c_python_grid", os.path.join(temp_custom_logging_dir(), 'example_grid.py'))
        grid_file = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(grid_file)
        generator = grid_file.grid_code()


        print(next(generator))
        print(next(generator))
        print(next(generator))
################################################################################################
+7 −4
Original line number Diff line number Diff line
@@ -202,9 +202,7 @@ test_pop.set(
# plt.savefig('sizes_for_commands.png')
# plt.show()



### Grid generating test.
### Grid generating testing
test_pop.add_grid_variable(
    name='lnm1',
    longname='log primary mass',
@@ -233,3 +231,8 @@ test_pop.add_grid_variable(
)

test_pop.generate_grid_code()


# test_pop.run_grid()

test_pop.load_grid_function()
 No newline at end of file
+15 −0
Original line number Diff line number Diff line
import os
import json
import time
import pickle
import sys

import matplotlib.pyplot as plt


from binarycpython.utils.grid import Population
from binarycpython.utils.functions import get_help_all, get_help

test_pop = Population()

test_pop.parse_cmdline()
 No newline at end of file