From 0ad189aac7196b79069cee9acb35e5585f6c469c Mon Sep 17 00:00:00 2001
From: David Hendriks <davidhendriks93@gmail.com>
Date: Thu, 19 Aug 2021 19:33:27 +0100
Subject: [PATCH] fixing the api log issue when running the tests

---
 examples/notebook_individual_systems.ipynb | 110 ++++++++++++---------
 1 file changed, 65 insertions(+), 45 deletions(-)

diff --git a/examples/notebook_individual_systems.ipynb b/examples/notebook_individual_systems.ipynb
index 19c2d29fb..fdf37b7e7 100644
--- a/examples/notebook_individual_systems.ipynb
+++ b/examples/notebook_individual_systems.ipynb
@@ -27,7 +27,18 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 1,
+   "execution_count": 24,
+   "id": "e32dcdee",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "from binarycpython.utils.functions import temp_dir\n",
+    "TMP_DIR = temp_dir(\"notebooks\", \"notebook_individual_systems\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 13,
    "id": "425efed3-d8e3-432d-829e-41d8ebe05162",
    "metadata": {},
    "outputs": [],
@@ -38,7 +49,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 2,
+   "execution_count": 14,
    "id": "b2abab48-433d-4936-8434-14804c52c9f6",
    "metadata": {},
    "outputs": [
@@ -46,7 +57,7 @@
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "SINGLE_STAR_LIFETIME 1 12462\n",
+      "SINGLE_STAR_LIFETIME 1 12461.9\n",
       "\n"
      ]
     }
@@ -66,7 +77,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 3,
+   "execution_count": 15,
    "id": "029fc3f2-f09a-49af-a32b-248505738f2e",
    "metadata": {},
    "outputs": [
@@ -74,24 +85,25 @@
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "      TIME      M1       M2   K1  K2           SEP   ECC  R1/ROL1 R2/ROL2  TYPE RANDOM_SEED=67365 RANDOM_COUNT=0\n",
-      "     0.0000    1.000    0.000  1  15            -1 -1.00   0.000   0.000  \"INITIAL \"\n",
-      " 11003.1302    1.000    0.000  2  15            -1 -1.00   0.000   0.000  \"OFF_MS\"\n",
-      " 11003.1302    1.000    0.000  2  15            -1 -1.00   0.000   0.000  \"TYPE_CHNGE\"\n",
-      " 11582.2424    1.000    0.000  3  15            -1 -1.00   0.000   0.000  \"TYPE_CHNGE\"\n",
-      " 12325.1085    0.817    0.000  4  15            -1 -1.00   0.000   0.000  \"TYPE_CHNGE\"\n",
-      " 12457.1300    0.783    0.000  5  15            -1 -1.00   0.000   0.000  \"TYPE_CHNGE\"\n",
-      " 12460.8955    0.774    0.000  6  15            -1 -1.00   0.000   0.000  \"TYPE_CHNGE\"\n",
-      " 12460.8955    0.774    0.000  6  15            -1 -1.00   0.000   0.000  \"shrinkAGB\"\n",
-      " 12461.9514    0.523    0.000 11  15            -1 -1.00   0.000   0.000  \"TYPE_CHNGE\"\n",
-      " 15000.0000    0.523    0.000 11  15            -1 -1.00   0.000   0.000  \"MAX_TIME\"\n",
+      "      TIME      M1       M2   K1  K2           SEP      PER    ECC  R1/ROL1 R2/ROL2  TYPE RANDOM_SEED=84984 RANDOM_COUNT=0\n",
+      "     0.0000    1.000    0.000  1  15            -1       -1  -1.00   0.000   0.000  \"INITIAL \"\n",
+      " 11003.1302    1.000    0.000  2  15            -1       -1  -1.00   0.000   0.000  \"OFF_MS\"\n",
+      " 11003.1302    1.000    0.000  2  15            -1       -1  -1.00   0.000   0.000  \"TYPE_CHNGE\"\n",
+      " 11582.2424    1.000    0.000  3  15            -1       -1  -1.00   0.000   0.000  \"TYPE_CHNGE\"\n",
+      " 12325.1085    0.817    0.000  4  15            -1       -1  -1.00   0.000   0.000  \"TYPE_CHNGE\"\n",
+      " 12457.1301    0.783    0.000  5  15            -1       -1  -1.00   0.000   0.000  \"TYPE_CHNGE\"\n",
+      " 12460.9983    0.716    0.000  6  15            -1       -1  -1.00   0.000   0.000  \"TYPE_CHNGE\"\n",
+      " 12460.9983    0.716    0.000  6  15            -1       -1  -1.00   0.000   0.000  \"shrinkAGB\"\n",
+      " 12461.9458    0.518    0.000 11  15            -1       -1  -1.00   0.000   0.000  \"TYPE_CHNGE\"\n",
+      " 15000.0000    0.518    0.000 11  15            -1       -1  -1.00   0.000   0.000  \"MAX_TIME\"\n",
       "\n"
      ]
     }
    ],
    "source": [
-    "output = run_system(M_1=1, log_filename='/tmp/test_logfile.txt')\n",
-    "with open('/tmp/test_logfile.txt', 'r') as f:\n",
+    "log_filename = os.path.join(TMP_DIR, 'test_logfile.txt')\n",
+    "output = run_system(M_1=1, log_filename=log_filename, api_log_filename_prefix=TMP_DIR)\n",
+    "with open(log_filename, 'r') as f:\n",
     "    print(f.read())"
    ]
   },
@@ -105,7 +117,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 6,
+   "execution_count": 16,
    "id": "e6a23b55-ca42-440d-83ac-e76a24a83a67",
    "metadata": {
     "tags": []
@@ -137,7 +149,7 @@
     "# Generate entire shared lib code around logging lines\n",
     "custom_logging_code = binary_c_log_code(custom_logging_print_statement)\n",
     "\n",
-    "output = run_system(M_1=1, custom_logging_code=custom_logging_code)\n",
+    "output = run_system(M_1=1, custom_logging_code=custom_logging_code, api_log_filename_prefix=TMP_DIR)\n",
     "print(output.splitlines()[:4])"
    ]
   },
@@ -151,7 +163,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 7,
+   "execution_count": 17,
    "id": "3822721f-217a-495b-962e-d57137b9e290",
    "metadata": {},
    "outputs": [
@@ -201,7 +213,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 8,
+   "execution_count": 18,
    "id": "654a07ed-2a88-46ff-9da0-b7759580f9f3",
    "metadata": {},
    "outputs": [
@@ -216,13 +228,13 @@
       "4       2e-06         1            1            1\n",
       "5       3e-06         1            1            1\n",
       "...       ...       ...          ...          ...\n",
-      "1612  12461.8  0.577754            1            6\n",
-      "1613    12462  0.522806            1           11\n",
-      "1614    13462  0.522806            1           11\n",
-      "1615    14462  0.522806            1           11\n",
-      "1616    15000  0.522806            1           11\n",
+      "1617  12461.8  0.546683            1            6\n",
+      "1618  12461.9  0.517749            1           11\n",
+      "1619  13461.9  0.517749            1           11\n",
+      "1620  14461.9  0.517749            1           11\n",
+      "1621    15000  0.517749            1           11\n",
       "\n",
-      "[1616 rows x 4 columns]\n"
+      "[1621 rows x 4 columns]\n"
      ]
     }
    ],
@@ -252,7 +264,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 9,
+   "execution_count": 19,
    "id": "4a98ffca-1b72-4bb8-8df1-3bf3187d882f",
    "metadata": {},
    "outputs": [],
@@ -271,7 +283,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 10,
+   "execution_count": 20,
    "id": "bff1cc2e-6b32-4ba0-879f-879ffbabd223",
    "metadata": {},
    "outputs": [
@@ -283,7 +295,7 @@
       "Creating and loading custom logging functionality\n",
       "Running binary_c M_1 10\n",
       "Cleaning up the custom logging stuff. type: single\n",
-      "SINGLE_STAR_LIFETIME 10 27.7358\n",
+      "SINGLE_STAR_LIFETIME 10 28.4838\n",
       "\n"
      ]
     }
@@ -294,7 +306,8 @@
     "\n",
     "# Set some parameters\n",
     "example_pop.set(\n",
-    "    verbosity=1\n",
+    "    verbosity=1,\n",
+    "    api_log_filename_prefix=TMP_DIR\n",
     ")\n",
     "example_pop.set(\n",
     "    M_1=10\n",
@@ -315,7 +328,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 13,
+   "execution_count": 22,
    "id": "dd748bab-b57e-4129-8350-9ea11fa179d0",
    "metadata": {
     "scrolled": true,
@@ -340,8 +353,11 @@
       "Creating and loading custom logging functionality\n",
       "Running binary_c M_1 10\n",
       "Cleaning up the custom logging stuff. type: single\n",
-      "Removed /tmp/binary_c_python/custom_logging/libcustom_logging_eac2dfc438a14e5a9f5be98b1b6b4294.so\n",
-      "['EXAMPLE_MASSLOSS             0.000000000000e+00 10 0 10 1', 'EXAMPLE_MASSLOSS             0.000000000000e+00 10 10 10 1', 'EXAMPLE_MASSLOSS             1.000000000000e-06 10 10 10 1', 'EXAMPLE_MASSLOSS             2.000000000000e-06 10 10 10 1']\n"
+      "Removed /tmp/binary_c_python/custom_logging/libcustom_logging_273b1d161cc245acb09b7c20ee60be99.so\n",
+      "EXAMPLE_MASSLOSS             0.000000000000e+00 10 0 10 1\n",
+      "EXAMPLE_MASSLOSS             0.000000000000e+00 10 10 10 1\n",
+      "EXAMPLE_MASSLOSS             1.000000000000e-06 10 10 10 1\n",
+      "EXAMPLE_MASSLOSS             2.000000000000e-06 10 10 10 1\n"
      ]
     }
    ],
@@ -362,7 +378,7 @@
     "\n",
     "# get output and print\n",
     "output = example_pop.evolve_single()\n",
-    "print(output.splitlines()[:4])"
+    "print('\\n'.join(output.splitlines()[:4]))"
    ]
   },
   {
@@ -377,7 +393,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 14,
+   "execution_count": 10,
    "id": "fec39154-cce6-438c-8c2c-509d76b00f34",
    "metadata": {},
    "outputs": [],
@@ -428,7 +444,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 15,
+   "execution_count": 26,
    "id": "57347512-fd4a-434b-b13c-5e6dbd3ac415",
    "metadata": {
     "scrolled": true,
@@ -439,12 +455,13 @@
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "adding: parse_function=<function object_parse_function at 0x7f9265091598> to grid_options\n",
-      "<<<< Warning: Key does not match previously known parameter:                     adding: output_dir=/tmp/ to custom_options >>>>\n",
+      "adding: parse_function=<function object_parse_function at 0x7fbe0c5d1950> to grid_options\n",
+      "<<<< Warning: Key does not match previously known parameter:                     adding: output_dir=/tmp/binary_c_python/notebooks/notebook_individual_systems to custom_options >>>>\n",
+      "adding: api_log_filename_prefix=/tmp/binary_c_python/notebooks/notebook_individual_systems to BSE_options\n",
       "Creating and loading custom logging functionality\n",
-      "Running binary_c M_1 10\n",
+      "Running binary_c M_1 10 api_log_filename_prefix /tmp/binary_c_python/notebooks/notebook_individual_systems\n",
       "Cleaning up the custom logging stuff. type: single\n",
-      "Removed /tmp/binary_c_python/custom_logging/libcustom_logging_e9c2bec7f15541eb847fc6013e48e7ed.so\n",
+      "Removed /tmp/binary_c_python/custom_logging/libcustom_logging_6136984d369d4ba3b18ac029add5ee33.so\n",
       "[['time', 'mass', 'initial_mass', 'stellar_type'], [0.0, 10.0, 0.0, 10.0, 1.0], [0.0, 10.0, 10.0, 10.0, 1.0], [1e-06, 10.0, 10.0, 10.0, 1.0]]\n",
       "dict_keys(['time', 'mass', 'initial_mass', 'stellar_type'])\n"
      ]
@@ -453,7 +470,8 @@
    "source": [
     "example_pop.set(\n",
     "    parse_function=object_parse_function,\n",
-    "    output_dir='/tmp/'\n",
+    "    output_dir=TMP_DIR,\n",
+    "    api_log_filename_prefix=TMP_DIR\n",
     ")\n",
     "output = example_pop.evolve_single()\n",
     "print(output[:4])\n",
@@ -488,7 +506,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 16,
+   "execution_count": 25,
    "id": "ec48125c-6bf5-48f4-9357-8261800b5d8b",
    "metadata": {},
    "outputs": [
@@ -496,7 +514,7 @@
      "name": "stdout",
      "output_type": "stream",
      "text": [
-      "SINGLE_STAR_LIFETIME 15 14.2383\n",
+      "SINGLE_STAR_LIFETIME 15 14.9947\n",
       "\n"
      ]
     }
@@ -510,10 +528,11 @@
     "eccentricity = 0.0\n",
     "metallicity = 0.02\n",
     "max_evolution_time = 15000  # Myr. You need to include this argument.\n",
+    "api_log_filename_prefix = TMP_DIR\n",
     "\n",
     "# Here we set up the argument string that is passed to the bindings\n",
     "argstring = \"\"\"\n",
-    "binary_c M_1 {M_1} M_2 {M_2} separation {separation} orbital_period {orbital_period} eccentricity {eccentricity} metallicity {metallicity} max_evolution_time {max_evolution_time}\n",
+    "binary_c M_1 {M_1} M_2 {M_2} separation {separation} orbital_period {orbital_period} eccentricity {eccentricity} metallicity {metallicity} max_evolution_time {max_evolution_time} api_log_filename_prefix {api_log_filename_prefix}\n",
     "\"\"\".format(\n",
     "    M_1=M_1,\n",
     "    M_2=M_2,\n",
@@ -522,6 +541,7 @@
     "    eccentricity=eccentricity,\n",
     "    metallicity=metallicity,\n",
     "    max_evolution_time=max_evolution_time,\n",
+    "    api_log_filename_prefix=TMP_DIR\n",
     ").strip()\n",
     "\n",
     "from binarycpython import _binary_c_bindings\n",
-- 
GitLab