-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
121 lines (94 loc) · 3.4 KB
/
Copy pathconfig.py
File metadata and controls
121 lines (94 loc) · 3.4 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
"""
Configuration management for cellmap challenge analysis.
Loads configuration from a local untracked config file, prompting for values if missing.
"""
import json
from pathlib import Path
CONFIG_FILE = Path(__file__).parent / ".config.json"
DEFAULT_CONFIG = {
"RESULTS_DIR": "~/cellmap-segmentation-challenge/results",
"EVALUATIONS_CSV": "~/cellmap-segmentation-challenge/results/evaluations.csv",
"GT_DATA_PATH": None, # Optional - will be skipped if not available
}
CONFIG_DESCRIPTIONS = {
"RESULTS_DIR": "Directory containing eval_*.results files",
"EVALUATIONS_CSV": "Path to evaluations.csv file",
"GT_DATA_PATH": "Path to ground truth data",
}
# Keys that are optional and won't prompt user input if missing
OPTIONAL_KEYS = {"GT_DATA_PATH"}
def load_config():
"""
Load configuration from file, prompting for missing required values if needed.
Optional keys (like GT_DATA_PATH) will not prompt for user input if missing,
and will be set to None to indicate they should be skipped downstream.
Returns:
dict: Configuration dictionary with all required keys
"""
config = {}
# Try to load existing config file
if CONFIG_FILE.exists():
try:
with open(CONFIG_FILE, "r") as f:
config = json.load(f)
print(f"Loaded configuration from {CONFIG_FILE}")
except Exception as e:
print(f"Warning: Could not load config file: {e}")
config = {}
# Check for missing required keys (exclude optional keys from prompting)
missing_required_keys = [
key
for key in DEFAULT_CONFIG.keys()
if key not in OPTIONAL_KEYS and (key not in config or not config[key])
]
if missing_required_keys:
print("\n" + "=" * 60)
print("Configuration Setup")
print("=" * 60)
print("Some configuration values are missing. Please provide them:")
print()
# Prompt for missing required values
for key in missing_required_keys:
description = CONFIG_DESCRIPTIONS.get(key, key)
default_value = DEFAULT_CONFIG.get(key, "")
print(f"{description} ({key})")
if default_value:
print(f" Default: {default_value}")
user_input = input(f" Enter value (or press Enter for default): ").strip()
if user_input:
config[key] = user_input
elif default_value:
config[key] = default_value
else:
config[key] = ""
print()
# Save updated config
save_config(config)
print(f"Configuration saved to {CONFIG_FILE}")
print("=" * 60 + "\n")
# Ensure all keys exist with defaults (optional keys may be None)
for key in DEFAULT_CONFIG.keys():
if key not in config:
config[key] = DEFAULT_CONFIG[key]
return config
def save_config(config):
"""
Save configuration to file.
Args:
config: Configuration dictionary to save
"""
try:
with open(CONFIG_FILE, "w") as f:
json.dump(config, f, indent=2)
except Exception as e:
print(f"Warning: Could not save config file: {e}")
def get_config_value(key):
"""
Get a specific configuration value.
Args:
key: Configuration key
Returns:
Configuration value
"""
config = load_config()
return config.get(key)