-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
86 lines (65 loc) · 2.49 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
import pandas as pd
import time
from scipy.io import arff
def load_datasets(directory, dataset_name):
"""
Loads training and testing datasets from ARFF files across multiple folds.
Args:
directory (str): Directory containing the ARFF files.
dataset_name (str): Base name of the dataset.
n_folds (int, optional): Number of folds to load. Defaults to 10.
Returns:
Tuple[List[pd.DataFrame], List[pd.DataFrame], List[pd.DataFrame]]:
- List of training DataFrames.
- List of testing DataFrames.
- List of combined training and testing DataFrames per fold.
"""
train_dfs = []
test_dfs = []
train_and_test_dfs = []
for i in range(10):
train_file = os.path.join(directory, f"{dataset_name}.fold.{i:06d}.train.arff")
test_file = os.path.join(directory, f"{dataset_name}.fold.{i:06d}.test.arff")
data_train, _ = arff.loadarff(train_file)
data_test, _ = arff.loadarff(test_file)
df_train = pd.DataFrame(data_train)
df_test = pd.DataFrame(data_test)
df_train.attrs["fold"] = i
df_test.attrs["fold"] = i
df_train.attrs["origin"] = "train"
df_test.attrs["origin"] = "test"
combined_df = pd.concat([df_train, df_test], axis=0, ignore_index=True)
train_dfs.append(df_train)
test_dfs.append(df_test)
train_and_test_dfs.append(combined_df)
return train_dfs, test_dfs, train_and_test_dfs
def get_process_time(process, start_time):
"""
Calculates and logs the elapsed time for a given process.
Args:
process (str): Description of the process.
start_time (float): Timestamp when the process started.
Returns:
float: Current timestamp after the process completion.
"""
current_time = time.time()
process_time = current_time - start_time
print(f"Finished {process} in {process_time:.3f} seconds.")
return current_time
def create_directory(directory, filename):
"""
Creates a directory if it does not exist.
Args:
directory (str): Directory path.
filename (str): File name.
Returns:
Tuple[bool, str]:
- bool: True if the file exists, False otherwise.
- str: Full path to the output file.
"""
if not os.path.exists(directory):
os.makedirs(directory)
output_file = os.path.join(directory, filename)
file_exists = os.path.isfile(output_file)
return file_exists, output_file