We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Fix timeouts related to metalearnings tests


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `scripts/03_calculate_metafeatures.py`
Content:
```
1 from argparse import ArgumentParser
2 from collections import defaultdict, OrderedDict
3 import copy
4 import logging
5 import os
6 import sys
7 import unittest.mock
8 
9 import arff
10 import joblib
11 import numpy as np
12 import pandas as pd
13 
14 from autosklearn.constants import (
15     BINARY_CLASSIFICATION,
16     MULTICLASS_CLASSIFICATION,
17     REGRESSION,
18 )
19 from autosklearn.metalearning.metafeatures import metafeatures
20 from autosklearn.smbo import (
21     _calculate_metafeatures,
22     _calculate_metafeatures_encoded,
23     EXCLUDE_META_FEATURES_REGRESSION,
24     EXCLUDE_META_FEATURES_CLASSIFICATION,
25 )
26 from autosklearn.util.stopwatch import StopWatch
27 
28 sys.path.append(".")
29 from update_metadata_util import load_task, classification_tasks, regression_tasks
30 
31 logger = logging.getLogger("03_calculate_metafeatures")
32 
33 
34 def calculate_metafeatures(task_id):
35     X_train, y_train, X_test, y_test, cat, task_type, dataset_name = load_task(task_id)
36     stopwatch = StopWatch()
37 
38     if task_type == "classification":
39         if len(np.unique(y_train)) == 2:
40             task_type = BINARY_CLASSIFICATION
41         else:
42             task_type = MULTICLASS_CLASSIFICATION
43     else:
44         task_type = REGRESSION
45 
46     _metafeatures_labels = _calculate_metafeatures(
47         x_train=X_train,
48         y_train=y_train,
49         data_feat_type=cat,
50         data_info_task=task_type,
51         basename=dataset_name,
52         logger_=logger,
53         stopwatch=stopwatch,
54     )
55 
56     _metafeatures_encoded_labels = _calculate_metafeatures_encoded(
57         x_train=X_train,
58         y_train=y_train,
59         data_feat_type=cat,
60         task=task_type,
61         basename=dataset_name,
62         logger_=logger,
63         stopwatch=stopwatch,
64     )
65 
66     mf = _metafeatures_labels
67     mf.metafeature_values.update(_metafeatures_encoded_labels.metafeature_values)
68 
69     return mf
70 
71 
72 if __name__ == "__main__":
73     parser = ArgumentParser()
74     parser.add_argument("--working-directory", type=str, required=True)
75     parser.add_argument("--memory-limit", type=int, default=3072)
76     parser.add_argument("--test-mode", action="store_true")
77 
78     args = parser.parse_args()
79     working_directory = args.working_directory
80     memory_limit = args.memory_limit
81     test_mode = args.test_mode
82 
83     for task_type in ("classification", "regression"):
84         output_directory = os.path.join(working_directory, "metafeatures", task_type)
85         try:
86             os.makedirs(output_directory)
87         except:
88             pass
89 
90         all_metafeatures = {}
91 
92         if task_type == "classification":
93             tasks = classification_tasks
94         else:
95             tasks = regression_tasks
96 
97         if test_mode:
98             tasks = [tasks[0]]
99 
100         tasks = copy.deepcopy(tasks)
101         np.random.shuffle(tasks)
102 
103         def producer():
104             for task_id in tasks:
105                 yield task_id
106 
107         memory = joblib.Memory(location="/tmp/joblib", verbose=10)
108         cached_calculate_metafeatures = memory.cache(calculate_metafeatures)
109         mfs = [cached_calculate_metafeatures(task_id) for task_id in producer()]
110 
111         for mf in mfs:
112             if mf is not None:
113                 all_metafeatures[mf.dataset_name] = mf
114 
115         # Write the calculation times as a csv file to disc (can be viewed in
116         # LibreOffice calc afterwards)
117         calculation_times = defaultdict(dict)
118         metafeature_values = defaultdict(dict)
119         helperfunction_values = defaultdict(dict)
120 
121         for i, task_id in enumerate(all_metafeatures):
122             calculation_times[task_id] = dict()
123             for metafeature_name in sorted(
124                 all_metafeatures[task_id].metafeature_values
125             ):
126                 metafeature_value = all_metafeatures[task_id].metafeature_values[
127                     metafeature_name
128                 ]
129                 calculation_times[task_id][metafeature_name] = metafeature_value.time
130                 if metafeature_value.type_ == "HELPERFUNCTION":
131                     helperfunction_values[task_id][
132                         metafeature_name
133                     ] = metafeature_value.value
134                 else:
135                     metafeature_values[task_id][
136                         metafeature_name
137                     ] = metafeature_value.value
138 
139         calculation_times = pd.DataFrame(calculation_times).transpose()
140         calculation_times = calculation_times.sort_index()
141         with open(os.path.join(output_directory, "calculation_times.csv"), "w") as fh:
142             fh.write(calculation_times.to_csv())
143 
144         # Write all metafeatures in the aslib1.0 format
145         metafeature_values = metafeature_values = pd.DataFrame(
146             metafeature_values
147         ).transpose()
148         metafeature_values = metafeature_values.sort_index()
149         arff_object = dict()
150         arff_object["attributes"] = [
151             ("instance_id", "STRING"),
152             ("repetition", "NUMERIC"),
153         ] + [("%s" % name, "NUMERIC") for name in metafeature_values.columns]
154         arff_object["relation"] = "FEATURE_VALUES"
155         arff_object["description"] = ""
156 
157         data = []
158         for idx in metafeature_values.index:
159             line = [idx, 1]
160             line += [
161                 value if np.isfinite(value) else None
162                 for value in metafeature_values.loc[idx, :].values
163             ]
164             data.append(line)
165         arff_object["data"] = data
166 
167         with open(os.path.join(output_directory, "feature_values.arff"), "w") as fh:
168             arff.dump(arff_object, fh)
169 
170         # Feature steps and runtimes according to the aslib1.0 format
171         feature_steps = defaultdict(list)
172         metafeature_names = list()
173 
174         exclude_metafeatures = (
175             EXCLUDE_META_FEATURES_CLASSIFICATION
176             if task_type == "classification"
177             else EXCLUDE_META_FEATURES_REGRESSION
178         )
179 
180         for metafeature_name in metafeatures.metafeatures.functions:
181 
182             if metafeature_name in exclude_metafeatures:
183                 continue
184 
185             dependency = metafeatures.metafeatures.get_dependency(metafeature_name)
186             if dependency is not None:
187                 feature_steps[dependency].append(metafeature_name)
188             feature_steps[metafeature_name].append(metafeature_name)
189 
190             metafeature_names.append(metafeature_name)
191 
192         # Write the feature runstatus in the aslib1.0 format
193         arff_object = dict()
194         arff_object["attributes"] = [
195             ("instance_id", "STRING"),
196             ("repetition", "NUMERIC"),
197         ] + [
198             ("%s" % name, ["ok", "timeout", "memout", "presolved", "crash", "other"])
199             for name in feature_steps
200         ]
201         arff_object["relation"] = "FEATURE_RUNSTATUS"
202         arff_object["description"] = ""
203 
204         data = []
205         for idx in metafeature_values.index:
206             line = [idx, 1]
207             for feature_step in feature_steps:
208                 if feature_step in helperfunction_values[idx]:
209                     line.append(
210                         "ok"
211                         if helperfunction_values[feature_step] is not None
212                         else "other"
213                     )
214                 elif feature_step in metafeature_values.loc[idx]:
215                     line.append(
216                         "ok"
217                         if np.isfinite(metafeature_values.loc[idx][feature_step])
218                         else "other"
219                     )
220                 else:
221                     line.append("other")
222 
223             data.append(line)
224         arff_object["data"] = data
225 
226         with open(os.path.join(output_directory, "feature_runstatus.arff"), "w") as fh:
227             arff.dump(arff_object, fh)
228 
229         arff_object = dict()
230         arff_object["attributes"] = [
231             ("instance_id", "STRING"),
232             ("repetition", "NUMERIC"),
233         ] + [("%s" % feature_step, "NUMERIC") for feature_step in feature_steps]
234         arff_object["relation"] = "FEATURE_COSTS"
235         arff_object["description"] = ""
236 
237         data = []
238         for instance_id in calculation_times.index:
239             calculation_times_per_group = dict()
240             line = [instance_id, 1]
241             for feature_step in feature_steps:
242                 time_ = 0.0
243                 for feature in feature_steps[feature_step]:
244                     time_ += calculation_times[feature][instance_id]
245                 if not np.isfinite(time_):
246                     raise ValueError(
247                         "Feature cost %s for instance %s and feature "
248                         "step %s not finite" % (time_, instance_id, feature)
249                     )
250                 line.append(time_)
251             data.append(line)
252         arff_object["data"] = data
253 
254         with open(os.path.join(output_directory, "feature_costs.arff"), "w") as fh:
255             arff.dump(arff_object, fh)
256 
257         # Write the features part of the description.txt to a file
258         description = OrderedDict()
259         description["features_cutoff_time"] = "3600"
260         description["features_cutoff_memory"] = args.memory_limit
261         description["number_of_feature_steps"] = str(len(feature_steps))
262 
263         for feature_step in feature_steps:
264             description["feature_step %s" % feature_step] = ", ".join(
265                 feature_steps[feature_step]
266             )
267         description["features_deterministic"] = ", ".join(
268             [metafeature_name for metafeature_name in metafeature_names]
269         )
270         description["features_stochastic"] = ""
271         description["default_steps"] = ", ".join(feature_steps)
272 
273         with open(
274             os.path.join(output_directory, "description.features.txt"), "w"
275         ) as fh:
276             for entry in description:
277                 fh.write("%s: %s\n" % (entry, description[entry]))
278
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/scripts/03_calculate_metafeatures.py b/scripts/03_calculate_metafeatures.py
--- a/scripts/03_calculate_metafeatures.py
+++ b/scripts/03_calculate_metafeatures.py
@@ -5,6 +5,7 @@
 import os
 import sys
 import unittest.mock
+import tempfile
 
 import arff
 import joblib
@@ -82,10 +83,7 @@
 
     for task_type in ("classification", "regression"):
         output_directory = os.path.join(working_directory, "metafeatures", task_type)
-        try:
-            os.makedirs(output_directory)
-        except:
-            pass
+        os.makedirs(output_directory, exist_ok=True)
 
         all_metafeatures = {}
 
@@ -100,13 +98,10 @@
         tasks = copy.deepcopy(tasks)
         np.random.shuffle(tasks)
 
-        def producer():
-            for task_id in tasks:
-                yield task_id
-
-        memory = joblib.Memory(location="/tmp/joblib", verbose=10)
+        tmpdir = os.path.join(tempfile.gettempdir(), "joblib")
+        memory = joblib.Memory(location=tmpdir, verbose=10)
         cached_calculate_metafeatures = memory.cache(calculate_metafeatures)
-        mfs = [cached_calculate_metafeatures(task_id) for task_id in producer()]
+        mfs = [cached_calculate_metafeatures(task_id) for task_id in tasks]
 
         for mf in mfs:
             if mf is not None:

{"golden_diff": "diff --git a/scripts/03_calculate_metafeatures.py b/scripts/03_calculate_metafeatures.py\n--- a/scripts/03_calculate_metafeatures.py\n+++ b/scripts/03_calculate_metafeatures.py\n@@ -5,6 +5,7 @@\n import os\n import sys\n import unittest.mock\n+import tempfile\n \n import arff\n import joblib\n@@ -82,10 +83,7 @@\n \n     for task_type in (\"classification\", \"regression\"):\n         output_directory = os.path.join(working_directory, \"metafeatures\", task_type)\n-        try:\n-            os.makedirs(output_directory)\n-        except:\n-            pass\n+        os.makedirs(output_directory, exist_ok=True)\n \n         all_metafeatures = {}\n \n@@ -100,13 +98,10 @@\n         tasks = copy.deepcopy(tasks)\n         np.random.shuffle(tasks)\n \n-        def producer():\n-            for task_id in tasks:\n-                yield task_id\n-\n-        memory = joblib.Memory(location=\"/tmp/joblib\", verbose=10)\n+        tmpdir = os.path.join(tempfile.gettempdir(), \"joblib\")\n+        memory = joblib.Memory(location=tmpdir, verbose=10)\n         cached_calculate_metafeatures = memory.cache(calculate_metafeatures)\n-        mfs = [cached_calculate_metafeatures(task_id) for task_id in producer()]\n+        mfs = [cached_calculate_metafeatures(task_id) for task_id in tasks]\n \n         for mf in mfs:\n             if mf is not None:\n", "issue": "Fix timeouts related to metalearnings tests\n\n", "before_files": [{"content": "from argparse import ArgumentParser\nfrom collections import defaultdict, OrderedDict\nimport copy\nimport logging\nimport os\nimport sys\nimport unittest.mock\n\nimport arff\nimport joblib\nimport numpy as np\nimport pandas as pd\n\nfrom autosklearn.constants import (\n    BINARY_CLASSIFICATION,\n    MULTICLASS_CLASSIFICATION,\n    REGRESSION,\n)\nfrom autosklearn.metalearning.metafeatures import metafeatures\nfrom autosklearn.smbo import (\n    _calculate_metafeatures,\n    _calculate_metafeatures_encoded,\n    EXCLUDE_META_FEATURES_REGRESSION,\n    EXCLUDE_META_FEATURES_CLASSIFICATION,\n)\nfrom autosklearn.util.stopwatch import StopWatch\n\nsys.path.append(\".\")\nfrom update_metadata_util import load_task, classification_tasks, regression_tasks\n\nlogger = logging.getLogger(\"03_calculate_metafeatures\")\n\n\ndef calculate_metafeatures(task_id):\n    X_train, y_train, X_test, y_test, cat, task_type, dataset_name = load_task(task_id)\n    stopwatch = StopWatch()\n\n    if task_type == \"classification\":\n        if len(np.unique(y_train)) == 2:\n            task_type = BINARY_CLASSIFICATION\n        else:\n            task_type = MULTICLASS_CLASSIFICATION\n    else:\n        task_type = REGRESSION\n\n    _metafeatures_labels = _calculate_metafeatures(\n        x_train=X_train,\n        y_train=y_train,\n        data_feat_type=cat,\n        data_info_task=task_type,\n        basename=dataset_name,\n        logger_=logger,\n        stopwatch=stopwatch,\n    )\n\n    _metafeatures_encoded_labels = _calculate_metafeatures_encoded(\n        x_train=X_train,\n        y_train=y_train,\n        data_feat_type=cat,\n        task=task_type,\n        basename=dataset_name,\n        logger_=logger,\n        stopwatch=stopwatch,\n    )\n\n    mf = _metafeatures_labels\n    mf.metafeature_values.update(_metafeatures_encoded_labels.metafeature_values)\n\n    return mf\n\n\nif __name__ == \"__main__\":\n    parser = ArgumentParser()\n    parser.add_argument(\"--working-directory\", type=str, required=True)\n    parser.add_argument(\"--memory-limit\", type=int, default=3072)\n    parser.add_argument(\"--test-mode\", action=\"store_true\")\n\n    args = parser.parse_args()\n    working_directory = args.working_directory\n    memory_limit = args.memory_limit\n    test_mode = args.test_mode\n\n    for task_type in (\"classification\", \"regression\"):\n        output_directory = os.path.join(working_directory, \"metafeatures\", task_type)\n        try:\n            os.makedirs(output_directory)\n        except:\n            pass\n\n        all_metafeatures = {}\n\n        if task_type == \"classification\":\n            tasks = classification_tasks\n        else:\n            tasks = regression_tasks\n\n        if test_mode:\n            tasks = [tasks[0]]\n\n        tasks = copy.deepcopy(tasks)\n        np.random.shuffle(tasks)\n\n        def producer():\n            for task_id in tasks:\n                yield task_id\n\n        memory = joblib.Memory(location=\"/tmp/joblib\", verbose=10)\n        cached_calculate_metafeatures = memory.cache(calculate_metafeatures)\n        mfs = [cached_calculate_metafeatures(task_id) for task_id in producer()]\n\n        for mf in mfs:\n            if mf is not None:\n                all_metafeatures[mf.dataset_name] = mf\n\n        # Write the calculation times as a csv file to disc (can be viewed in\n        # LibreOffice calc afterwards)\n        calculation_times = defaultdict(dict)\n        metafeature_values = defaultdict(dict)\n        helperfunction_values = defaultdict(dict)\n\n        for i, task_id in enumerate(all_metafeatures):\n            calculation_times[task_id] = dict()\n            for metafeature_name in sorted(\n                all_metafeatures[task_id].metafeature_values\n            ):\n                metafeature_value = all_metafeatures[task_id].metafeature_values[\n                    metafeature_name\n                ]\n                calculation_times[task_id][metafeature_name] = metafeature_value.time\n                if metafeature_value.type_ == \"HELPERFUNCTION\":\n                    helperfunction_values[task_id][\n                        metafeature_name\n                    ] = metafeature_value.value\n                else:\n                    metafeature_values[task_id][\n                        metafeature_name\n                    ] = metafeature_value.value\n\n        calculation_times = pd.DataFrame(calculation_times).transpose()\n        calculation_times = calculation_times.sort_index()\n        with open(os.path.join(output_directory, \"calculation_times.csv\"), \"w\") as fh:\n            fh.write(calculation_times.to_csv())\n\n        # Write all metafeatures in the aslib1.0 format\n        metafeature_values = metafeature_values = pd.DataFrame(\n            metafeature_values\n        ).transpose()\n        metafeature_values = metafeature_values.sort_index()\n        arff_object = dict()\n        arff_object[\"attributes\"] = [\n            (\"instance_id\", \"STRING\"),\n            (\"repetition\", \"NUMERIC\"),\n        ] + [(\"%s\" % name, \"NUMERIC\") for name in metafeature_values.columns]\n        arff_object[\"relation\"] = \"FEATURE_VALUES\"\n        arff_object[\"description\"] = \"\"\n\n        data = []\n        for idx in metafeature_values.index:\n            line = [idx, 1]\n            line += [\n                value if np.isfinite(value) else None\n                for value in metafeature_values.loc[idx, :].values\n            ]\n            data.append(line)\n        arff_object[\"data\"] = data\n\n        with open(os.path.join(output_directory, \"feature_values.arff\"), \"w\") as fh:\n            arff.dump(arff_object, fh)\n\n        # Feature steps and runtimes according to the aslib1.0 format\n        feature_steps = defaultdict(list)\n        metafeature_names = list()\n\n        exclude_metafeatures = (\n            EXCLUDE_META_FEATURES_CLASSIFICATION\n            if task_type == \"classification\"\n            else EXCLUDE_META_FEATURES_REGRESSION\n        )\n\n        for metafeature_name in metafeatures.metafeatures.functions:\n\n            if metafeature_name in exclude_metafeatures:\n                continue\n\n            dependency = metafeatures.metafeatures.get_dependency(metafeature_name)\n            if dependency is not None:\n                feature_steps[dependency].append(metafeature_name)\n            feature_steps[metafeature_name].append(metafeature_name)\n\n            metafeature_names.append(metafeature_name)\n\n        # Write the feature runstatus in the aslib1.0 format\n        arff_object = dict()\n        arff_object[\"attributes\"] = [\n            (\"instance_id\", \"STRING\"),\n            (\"repetition\", \"NUMERIC\"),\n        ] + [\n            (\"%s\" % name, [\"ok\", \"timeout\", \"memout\", \"presolved\", \"crash\", \"other\"])\n            for name in feature_steps\n        ]\n        arff_object[\"relation\"] = \"FEATURE_RUNSTATUS\"\n        arff_object[\"description\"] = \"\"\n\n        data = []\n        for idx in metafeature_values.index:\n            line = [idx, 1]\n            for feature_step in feature_steps:\n                if feature_step in helperfunction_values[idx]:\n                    line.append(\n                        \"ok\"\n                        if helperfunction_values[feature_step] is not None\n                        else \"other\"\n                    )\n                elif feature_step in metafeature_values.loc[idx]:\n                    line.append(\n                        \"ok\"\n                        if np.isfinite(metafeature_values.loc[idx][feature_step])\n                        else \"other\"\n                    )\n                else:\n                    line.append(\"other\")\n\n            data.append(line)\n        arff_object[\"data\"] = data\n\n        with open(os.path.join(output_directory, \"feature_runstatus.arff\"), \"w\") as fh:\n            arff.dump(arff_object, fh)\n\n        arff_object = dict()\n        arff_object[\"attributes\"] = [\n            (\"instance_id\", \"STRING\"),\n            (\"repetition\", \"NUMERIC\"),\n        ] + [(\"%s\" % feature_step, \"NUMERIC\") for feature_step in feature_steps]\n        arff_object[\"relation\"] = \"FEATURE_COSTS\"\n        arff_object[\"description\"] = \"\"\n\n        data = []\n        for instance_id in calculation_times.index:\n            calculation_times_per_group = dict()\n            line = [instance_id, 1]\n            for feature_step in feature_steps:\n                time_ = 0.0\n                for feature in feature_steps[feature_step]:\n                    time_ += calculation_times[feature][instance_id]\n                if not np.isfinite(time_):\n                    raise ValueError(\n                        \"Feature cost %s for instance %s and feature \"\n                        \"step %s not finite\" % (time_, instance_id, feature)\n                    )\n                line.append(time_)\n            data.append(line)\n        arff_object[\"data\"] = data\n\n        with open(os.path.join(output_directory, \"feature_costs.arff\"), \"w\") as fh:\n            arff.dump(arff_object, fh)\n\n        # Write the features part of the description.txt to a file\n        description = OrderedDict()\n        description[\"features_cutoff_time\"] = \"3600\"\n        description[\"features_cutoff_memory\"] = args.memory_limit\n        description[\"number_of_feature_steps\"] = str(len(feature_steps))\n\n        for feature_step in feature_steps:\n            description[\"feature_step %s\" % feature_step] = \", \".join(\n                feature_steps[feature_step]\n            )\n        description[\"features_deterministic\"] = \", \".join(\n            [metafeature_name for metafeature_name in metafeature_names]\n        )\n        description[\"features_stochastic\"] = \"\"\n        description[\"default_steps\"] = \", \".join(feature_steps)\n\n        with open(\n            os.path.join(output_directory, \"description.features.txt\"), \"w\"\n        ) as fh:\n            for entry in description:\n                fh.write(\"%s: %s\\n\" % (entry, description[entry]))\n", "path": "scripts/03_calculate_metafeatures.py"}], "after_files": [{"content": "from argparse import ArgumentParser\nfrom collections import defaultdict, OrderedDict\nimport copy\nimport logging\nimport os\nimport sys\nimport unittest.mock\nimport tempfile\n\nimport arff\nimport joblib\nimport numpy as np\nimport pandas as pd\n\nfrom autosklearn.constants import (\n    BINARY_CLASSIFICATION,\n    MULTICLASS_CLASSIFICATION,\n    REGRESSION,\n)\nfrom autosklearn.metalearning.metafeatures import metafeatures\nfrom autosklearn.smbo import (\n    _calculate_metafeatures,\n    _calculate_metafeatures_encoded,\n    EXCLUDE_META_FEATURES_REGRESSION,\n    EXCLUDE_META_FEATURES_CLASSIFICATION,\n)\nfrom autosklearn.util.stopwatch import StopWatch\n\nsys.path.append(\".\")\nfrom update_metadata_util import load_task, classification_tasks, regression_tasks\n\nlogger = logging.getLogger(\"03_calculate_metafeatures\")\n\n\ndef calculate_metafeatures(task_id):\n    X_train, y_train, X_test, y_test, cat, task_type, dataset_name = load_task(task_id)\n    stopwatch = StopWatch()\n\n    if task_type == \"classification\":\n        if len(np.unique(y_train)) == 2:\n            task_type = BINARY_CLASSIFICATION\n        else:\n            task_type = MULTICLASS_CLASSIFICATION\n    else:\n        task_type = REGRESSION\n\n    _metafeatures_labels = _calculate_metafeatures(\n        x_train=X_train,\n        y_train=y_train,\n        data_feat_type=cat,\n        data_info_task=task_type,\n        basename=dataset_name,\n        logger_=logger,\n        stopwatch=stopwatch,\n    )\n\n    _metafeatures_encoded_labels = _calculate_metafeatures_encoded(\n        x_train=X_train,\n        y_train=y_train,\n        data_feat_type=cat,\n        task=task_type,\n        basename=dataset_name,\n        logger_=logger,\n        stopwatch=stopwatch,\n    )\n\n    mf = _metafeatures_labels\n    mf.metafeature_values.update(_metafeatures_encoded_labels.metafeature_values)\n\n    return mf\n\n\nif __name__ == \"__main__\":\n    parser = ArgumentParser()\n    parser.add_argument(\"--working-directory\", type=str, required=True)\n    parser.add_argument(\"--memory-limit\", type=int, default=3072)\n    parser.add_argument(\"--test-mode\", action=\"store_true\")\n\n    args = parser.parse_args()\n    working_directory = args.working_directory\n    memory_limit = args.memory_limit\n    test_mode = args.test_mode\n\n    for task_type in (\"classification\", \"regression\"):\n        output_directory = os.path.join(working_directory, \"metafeatures\", task_type)\n        os.makedirs(output_directory, exist_ok=True)\n\n        all_metafeatures = {}\n\n        if task_type == \"classification\":\n            tasks = classification_tasks\n        else:\n            tasks = regression_tasks\n\n        if test_mode:\n            tasks = [tasks[0]]\n\n        tasks = copy.deepcopy(tasks)\n        np.random.shuffle(tasks)\n\n        tmpdir = os.path.join(tempfile.gettempdir(), \"joblib\")\n        memory = joblib.Memory(location=tmpdir, verbose=10)\n        cached_calculate_metafeatures = memory.cache(calculate_metafeatures)\n        mfs = [cached_calculate_metafeatures(task_id) for task_id in tasks]\n\n        for mf in mfs:\n            if mf is not None:\n                all_metafeatures[mf.dataset_name] = mf\n\n        # Write the calculation times as a csv file to disc (can be viewed in\n        # LibreOffice calc afterwards)\n        calculation_times = defaultdict(dict)\n        metafeature_values = defaultdict(dict)\n        helperfunction_values = defaultdict(dict)\n\n        for i, task_id in enumerate(all_metafeatures):\n            calculation_times[task_id] = dict()\n            for metafeature_name in sorted(\n                all_metafeatures[task_id].metafeature_values\n            ):\n                metafeature_value = all_metafeatures[task_id].metafeature_values[\n                    metafeature_name\n                ]\n                calculation_times[task_id][metafeature_name] = metafeature_value.time\n                if metafeature_value.type_ == \"HELPERFUNCTION\":\n                    helperfunction_values[task_id][\n                        metafeature_name\n                    ] = metafeature_value.value\n                else:\n                    metafeature_values[task_id][\n                        metafeature_name\n                    ] = metafeature_value.value\n\n        calculation_times = pd.DataFrame(calculation_times).transpose()\n        calculation_times = calculation_times.sort_index()\n        with open(os.path.join(output_directory, \"calculation_times.csv\"), \"w\") as fh:\n            fh.write(calculation_times.to_csv())\n\n        # Write all metafeatures in the aslib1.0 format\n        metafeature_values = metafeature_values = pd.DataFrame(\n            metafeature_values\n        ).transpose()\n        metafeature_values = metafeature_values.sort_index()\n        arff_object = dict()\n        arff_object[\"attributes\"] = [\n            (\"instance_id\", \"STRING\"),\n            (\"repetition\", \"NUMERIC\"),\n        ] + [(\"%s\" % name, \"NUMERIC\") for name in metafeature_values.columns]\n        arff_object[\"relation\"] = \"FEATURE_VALUES\"\n        arff_object[\"description\"] = \"\"\n\n        data = []\n        for idx in metafeature_values.index:\n            line = [idx, 1]\n            line += [\n                value if np.isfinite(value) else None\n                for value in metafeature_values.loc[idx, :].values\n            ]\n            data.append(line)\n        arff_object[\"data\"] = data\n\n        with open(os.path.join(output_directory, \"feature_values.arff\"), \"w\") as fh:\n            arff.dump(arff_object, fh)\n\n        # Feature steps and runtimes according to the aslib1.0 format\n        feature_steps = defaultdict(list)\n        metafeature_names = list()\n\n        exclude_metafeatures = (\n            EXCLUDE_META_FEATURES_CLASSIFICATION\n            if task_type == \"classification\"\n            else EXCLUDE_META_FEATURES_REGRESSION\n        )\n\n        for metafeature_name in metafeatures.metafeatures.functions:\n\n            if metafeature_name in exclude_metafeatures:\n                continue\n\n            dependency = metafeatures.metafeatures.get_dependency(metafeature_name)\n            if dependency is not None:\n                feature_steps[dependency].append(metafeature_name)\n            feature_steps[metafeature_name].append(metafeature_name)\n\n            metafeature_names.append(metafeature_name)\n\n        # Write the feature runstatus in the aslib1.0 format\n        arff_object = dict()\n        arff_object[\"attributes\"] = [\n            (\"instance_id\", \"STRING\"),\n            (\"repetition\", \"NUMERIC\"),\n        ] + [\n            (\"%s\" % name, [\"ok\", \"timeout\", \"memout\", \"presolved\", \"crash\", \"other\"])\n            for name in feature_steps\n        ]\n        arff_object[\"relation\"] = \"FEATURE_RUNSTATUS\"\n        arff_object[\"description\"] = \"\"\n\n        data = []\n        for idx in metafeature_values.index:\n            line = [idx, 1]\n            for feature_step in feature_steps:\n                if feature_step in helperfunction_values[idx]:\n                    line.append(\n                        \"ok\"\n                        if helperfunction_values[feature_step] is not None\n                        else \"other\"\n                    )\n                elif feature_step in metafeature_values.loc[idx]:\n                    line.append(\n                        \"ok\"\n                        if np.isfinite(metafeature_values.loc[idx][feature_step])\n                        else \"other\"\n                    )\n                else:\n                    line.append(\"other\")\n\n            data.append(line)\n        arff_object[\"data\"] = data\n\n        with open(os.path.join(output_directory, \"feature_runstatus.arff\"), \"w\") as fh:\n            arff.dump(arff_object, fh)\n\n        arff_object = dict()\n        arff_object[\"attributes\"] = [\n            (\"instance_id\", \"STRING\"),\n            (\"repetition\", \"NUMERIC\"),\n        ] + [(\"%s\" % feature_step, \"NUMERIC\") for feature_step in feature_steps]\n        arff_object[\"relation\"] = \"FEATURE_COSTS\"\n        arff_object[\"description\"] = \"\"\n\n        data = []\n        for instance_id in calculation_times.index:\n            calculation_times_per_group = dict()\n            line = [instance_id, 1]\n            for feature_step in feature_steps:\n                time_ = 0.0\n                for feature in feature_steps[feature_step]:\n                    time_ += calculation_times[feature][instance_id]\n                if not np.isfinite(time_):\n                    raise ValueError(\n                        \"Feature cost %s for instance %s and feature \"\n                        \"step %s not finite\" % (time_, instance_id, feature)\n                    )\n                line.append(time_)\n            data.append(line)\n        arff_object[\"data\"] = data\n\n        with open(os.path.join(output_directory, \"feature_costs.arff\"), \"w\") as fh:\n            arff.dump(arff_object, fh)\n\n        # Write the features part of the description.txt to a file\n        description = OrderedDict()\n        description[\"features_cutoff_time\"] = \"3600\"\n        description[\"features_cutoff_memory\"] = args.memory_limit\n        description[\"number_of_feature_steps\"] = str(len(feature_steps))\n\n        for feature_step in feature_steps:\n            description[\"feature_step %s\" % feature_step] = \", \".join(\n                feature_steps[feature_step]\n            )\n        description[\"features_deterministic\"] = \", \".join(\n            [metafeature_name for metafeature_name in metafeature_names]\n        )\n        description[\"features_stochastic\"] = \"\"\n        description[\"default_steps\"] = \", \".join(feature_steps)\n\n        with open(\n            os.path.join(output_directory, \"description.features.txt\"), \"w\"\n        ) as fh:\n            for entry in description:\n                fh.write(\"%s: %s\\n\" % (entry, description[entry]))\n", "path": "scripts/03_calculate_metafeatures.py"}]}