Commit 89584759 authored by David Hendriks's avatar David Hendriks
Browse files

moved around functions, fixed the super-help function, changed some of the...

moved around functions, fixed the super-help function, changed some of the tests for persistent_data
parent 3fd1d856
Loading
Loading
Loading
Loading
+428 −283

File changed.

Preview size limit exceeded, changes collapsed.

+18 −48
Original line number Diff line number Diff line
@@ -8,46 +8,7 @@ import json
import textwrap
import binary_c_python_api

from mergedict import ConfigDict
from mergedict import MergeDict

class SumDict(MergeDict):
      @MergeDict.dispatch(float)
      def merge_float(this, other):
          return this + other


class Decoder(json.JSONDecoder):
    """
    Custom decoder to transform the numbers that are strings to actual floats
    """

    def decode(self, s):
        result = super().decode(s)  # result = super(Decoder, self).decode(s) for Python 2.x
        return self._decode(result)

    def _decode(self, o):
        """
        Depending on the type of object is will determine whether to loop over the elements,
        or try to change the type of the object from string to float

        The try except might be a somewhat rough solution but it catches all cases.
        """


        # Check if we can turn it into a float
        # if isinstance(o, str) or isinstance(o, unicode):
        if isinstance(o, str):
            try:
                return float(o)
            except ValueError:
                return o
        elif isinstance(o, dict):
            return {k: self._decode(v) for k, v in o.items()}
        elif isinstance(o, list):
            return [self._decode(v) for v in o]
        else:
            return o
from binarycpython.utils.functions import binarycDecoder

####

@@ -108,7 +69,6 @@ def test_passing_persistent_data_to_run_system():
    # printf("combined double system vs deferred double system?:")

def ensemble_output():

    m1 = 15.0  # Msun
    m2 = 14.0  # Msun
    separation = 0  # 0 = ignored, use period
@@ -130,7 +90,7 @@ def ensemble_output():
    ensemble_jsons_1 = [line for line in output_1.splitlines() if line.startswith("ENSEMBLE_JSON")]

    start = time.time()
    json_1 = json.loads(ensemble_jsons_1[0][len("ENSEMBLE_JSON "):], cls=Decoder)
    json_1 = json.loads(ensemble_jsons_1[0][len("ENSEMBLE_JSON "):], cls=binarycDecoder)
    stop = time.time()

    print(json.dumps(json_1, indent=4))
@@ -169,8 +129,8 @@ def adding_ensemble_output():
    ensemble_jsons_1 = [line for line in output_1.splitlines() if line.startswith("ENSEMBLE_JSON")]
    ensemble_jsons_2 = [line for line in output_2.splitlines() if line.startswith("ENSEMBLE_JSON")]

    json_1 = json.loads(ensemble_jsons_1[0][len("ENSEMBLE_JSON "):], cls=Decoder)
    json_2 = json.loads(ensemble_jsons_2[0][len("ENSEMBLE_JSON "):], cls=Decoder)
    json_1 = json.loads(ensemble_jsons_1[0][len("ENSEMBLE_JSON "):], cls=binarycDecoder)
    json_2 = json.loads(ensemble_jsons_2[0][len("ENSEMBLE_JSON "):], cls=binarycDecoder)

    # test_1_total_dict = SumDict(json_1)
    # test_1_total_dict.merge(json_2)
@@ -211,7 +171,7 @@ def adding_ensemble_output():

    ensemble_jsons_deferred = [line for line in output_total_deferred.splitlines() if line.startswith("ENSEMBLE_JSON")]

    json_deferred = json.loads(ensemble_jsons_deferred[0][len("ENSEMBLE_JSON "):], cls=Decoder)
    json_deferred = json.loads(ensemble_jsons_deferred[0][len("ENSEMBLE_JSON "):], cls=binarycDecoder)

    with open("json_deferred.json", 'w') as f:
        f.write(json.dumps(json_deferred, indent=4))
@@ -237,7 +197,7 @@ def adding_ensemble_output():

    ensemble_jsons_deferred_and_output = [line for line in output_2_deferred_and_output.splitlines() if line.startswith("ENSEMBLE_JSON")]

    json_deferred_and_output = json.loads(ensemble_jsons_deferred_and_output[0][len("ENSEMBLE_JSON "):], cls=Decoder)
    json_deferred_and_output = json.loads(ensemble_jsons_deferred_and_output[0][len("ENSEMBLE_JSON "):], cls=binarycDecoder)

    with open("json_deferred_and_output.json", 'w') as f:
        f.write(json.dumps(json_deferred_and_output, indent=4))
@@ -245,6 +205,10 @@ def adding_ensemble_output():
    print("Single deferred done\n")

def test_free_and_json_output():
    """
    Function that tests the freeing of the memory adress and the output of the json
    """

    m1 = 2  # Msun
    m2 = 0.1  # Msun
    separation = 0  # 0 = ignored, use period
@@ -263,12 +227,14 @@ def test_free_and_json_output():
    argstring_1 = argstring_template.format(
        m1, m2, separation, orbital_period, eccentricity, metallicity, max_evolution_time, "1")

    # Evolve and defer output
    print("evolving")
    output_1_deferred = binary_c_python_api.run_system(argstring=argstring_1, persistent_data_memaddr=persistent_data_memaddr)
    print("Evolved")
    print("Output:")
    print(textwrap.indent(str(output_1_deferred), "\t"))

    # Free memory adress
    print("freeing")
    json_output_by_freeing = binary_c_python_api.free_persistent_data_memaddr_and_return_json_output(persistent_data_memaddr)
    print("Freed")
@@ -298,7 +264,7 @@ def full_output():
    ensemble_jsons_1 = [line for line in output_1.splitlines() if line.startswith("ENSEMBLE_JSON")]

    start = time.time()
    json_1 = json.loads(ensemble_jsons_1[0][len("ENSEMBLE_JSON "):], cls=Decoder)
    json_1 = json.loads(ensemble_jsons_1[0][len("ENSEMBLE_JSON "):], cls=binarycDecoder)
    stop = time.time()

    print("took {}s to decode".format(stop-start))
@@ -306,6 +272,10 @@ def full_output():
    with open("json_full_ensemble.json", 'w') as f:
        f.write(json.dumps(json_1, indent=4))





####
if __name__ == "__main__":
    # test_return_persistent_data_memaddr()