Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
148 lines (121 loc) · 5.03 KB
/
Copy pathutils.py
File metadata and controls
148 lines (121 loc) · 5.03 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import re
import ast
import subprocess
from pathlib import Path
import pandas as pd
import os
import sys
def _get_objectives(config):
if isinstance(config, dict):
return config["OBJECTIVES"]
if hasattr(config, "objectives"):
return config.objectives
raise TypeError("Unsupported config type: expected dict-like or ExperimentConfig")
def parse_bash_value(value_str):
"""Parse a bash value into a Python value."""
# Remove any surrounding quotes
value_str = value_str.strip().strip('"\'')
# Handle Python expressions
if value_str.startswith('$(') and value_str.endswith(')'):
# Extract the Python code
python_code = value_str[2:-1]
if python_code.startswith('python3 -c "print('):
# Extract the actual Python expression
expr = python_code[len('python3 -c "print('):-2]
try:
return ast.literal_eval(expr)
except:
return expr
return value_str
# Handle arrays
if value_str.startswith('(') and value_str.endswith(')'):
# Convert bash array to Python list
items = value_str[1:-1].split()
return [ast.literal_eval(item) if item.isdigit() else item.strip('"\'') for item in items]
# Handle numbers
if value_str.isdigit():
return int(value_str)
try:
return float(value_str)
except ValueError:
pass
# Handle booleans
if value_str.lower() in ('true', 'false'):
return value_str.lower() == 'true'
return value_str
def load_bash_config(bash_script_path):
"""Load variables from a bash script into a Python dictionary."""
config = {}
# Read the bash script
with open(bash_script_path, 'r') as f:
content = f.read()
# Find all variable assignments
# This regex matches: VAR=value or VAR="value" or VAR='value' or VAR=(value1 value2)
pattern = r'^([A-Za-z0-9_]+)=(.*?)(?=\n[A-Za-z0-9_]+=|$)'
matches = re.finditer(pattern, content, re.MULTILINE | re.DOTALL)
for match in matches:
var_name = match.group(1)
var_value = match.group(2).strip()
# Skip commented lines
if var_value.startswith('#'):
continue
# Parse the value
try:
config[var_name] = parse_bash_value(var_value)
except Exception as e:
print(f"Warning: Could not parse {var_name}={var_value}: {e}")
config[var_name] = var_value
return config
def load_data_normalized(input_file,config):
if not os.path.exists(input_file):
raise FileNotFoundError(f"File does not exist: {input_file}")
columns = ["date", "time", "generation"]
columns += [f"o{i+1}" for i,_ in enumerate(_get_objectives(config))]
df = pd.read_csv(input_file,sep=" ",header=None)
df.columns = columns
return df
def load_data_merged_hybrids(input_file):
if not os.path.exists(input_file):
raise FileNotFoundError(f"File does not exist: {input_file}")
df = pd.read_csv(input_file,sep=",")
return df
def load_data_hybrids(input_file,config,seq = 77):
if not os.path.exists(input_file):
raise FileNotFoundError(f"File does not exist: {input_file}")
objective_columns = [f"o{i+1}" for i, _ in enumerate(_get_objectives(config))]
base_columns = ["date", "time", "pf", "generation"] + objective_columns
# Read first, then infer how many hybrid transfer columns are present.
# This avoids hard failures when SEQ_HYBRIDS differs across experiments.
try:
df = pd.read_csv(input_file, sep=r"\s+", header=None, engine="python")
except pd.errors.EmptyDataError:
print(f"[load_data_hybrids] Warning: empty file, skipping: {input_file}")
return pd.DataFrame(columns=base_columns)
detected_total_cols = df.shape[1]
min_required_cols = len(base_columns)
if detected_total_cols < min_required_cols:
raise ValueError(
f"Unexpected hybrid file format: {input_file}. "
f"Detected {detected_total_cols} columns, expected at least {min_required_cols}."
)
detected_seq = detected_total_cols - min_required_cols
if seq is not None and detected_seq != seq:
print(
f"[load_data_hybrids] Warning: requested seq={seq}, "
f"but file has seq={detected_seq}. Using detected value."
)
pair_columns = [f"tt{i}" for i in range(detected_seq)]
df.columns = base_columns + pair_columns
return df
if __name__ == "__main__":
# Get the path to script_constants.sh
script_path = Path("script_constants.sh")
# Load the configuration
config = load_bash_config(script_path)
# Print the configuration in a Python-friendly format
print("Configuration loaded. You can access it in your notebook like this:")
print("\nfrom load_config import load_bash_config")
print("config = load_bash_config('script_constants.sh')")
print("\nAvailable variables:")
for key in sorted(config.keys()):
print(f"config['{key}'] = {config[key]}")