Repository navigation
Expand file tree
/
Copy pathannotate_regime.py
More file actions
337 lines (273 loc) · 13 KB
/
Copy pathannotate_regime.py
File metadata and controls
337 lines (273 loc) · 13 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
"""
───────────────────────────────────────────────────────────────────────────────
REGIME ANNOTATION TOOL — Interactive CLI
───────────────────────────────────────────────────────────────────────────────
Lets you label regime segments from a trading session interactively and
appends them to data/regime_annotations.csv for use in supervised HMM training.
Usage:
python annotate_regimes.py
Input format (flexible):
09:15-10:30 Trending
11:00 - 13:45 Choppy high
14:00-15:25 Range medium
10:10-10:50 choppy ← case-insensitive
(blank line or 'done' to finish the session)
Regimes: Trending | Range | Choppy
Confidence: high (default) | medium | low
Author: Jeevan Jonas / Claude 2026
───────────────────────────────────────────────────────────────────────────────
"""
import csv
import os
import re
import sys
from datetime import datetime, date, timedelta
from pathlib import Path
# ── Config ────────────────────────────────────────────────────────────────────
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
ANNOTATION_FILE = os.path.join(BASE_DIR, "data", "regime_annotations.csv")
VALID_REGIMES = {"trending", "range", "choppy"}
VALID_CONF = {"high", "medium", "low"}
DEFAULT_CONF = "high"
# Direction-aware aliases — Trending-Up/Down both normalise to "Trending"
# The trainer only uses Trending/Range/Choppy; direction is inferred at inference.
REGIME_ALIASES = {
"trending-up": "Trending", "trending-down": "Trending",
"trending_up": "Trending", "trending_down": "Trending",
"t-up": "Trending", "t-down": "Trending",
"tup": "Trending", "tdown": "Trending",
"tu": "Trending", "td": "Trending",
"t": "Trending",
"r": "Range",
"c": "Choppy",
}
FIELDNAMES = ["datetime_start", "datetime_end", "regime", "confidence"]
# ── Colours (optional, graceful fallback) ────────────────────────────────────
try:
from colorama import Fore, Style, init as colorama_init
colorama_init()
C_PROMPT = Fore.CYAN
C_OK = Fore.GREEN
C_WARN = Fore.YELLOW
C_ERR = Fore.RED
C_DIM = Style.DIM
C_RESET = Style.RESET_ALL
except ImportError:
C_PROMPT = C_OK = C_WARN = C_ERR = C_DIM = C_RESET = ""
# ── Helpers ───────────────────────────────────────────────────────────────────
def print_header():
print(f"\n{C_PROMPT}{'─'*60}")
print(" REGIME ANNOTATION TOOL")
print(f"{'─'*60}{C_RESET}")
print(f" Output: {ANNOTATION_FILE}")
print(f" Regimes: Trending[-Up/-Down] | T-Up | T-Down | Range | Choppy")
print(f" Confidence: high (default) | medium | low")
print(f"\n Format: HH:MM-HH:MM <regime> [confidence]")
print(f" Example: 09:15-10:30 Trending-Up or just Trending")
print(f" 11:00-13:45 Choppy medium")
print(f" 14:00-15:25 T-Down (shorthand for Trending-Down)")
print(f" 14:00-15:25 Range")
print(f"\n Commands: done / skip / quit / show\n")
def parse_date_input(raw: str) -> date | None:
"""Accept YYYY-MM-DD or DD-MM-YYYY or DD/MM/YYYY or 'today'."""
raw = raw.strip().lower()
if raw in ("today", "t"):
return date.today()
if raw in ("yesterday", "y"):
return date.today() - timedelta(days=1)
for fmt in ("%Y-%m-%d", "%d-%m-%Y", "%d/%m/%Y", "%Y/%m/%d"):
try:
return datetime.strptime(raw, fmt).date()
except ValueError:
continue
return None
def parse_segment(line: str, session_date: date) -> dict | None:
"""
Parse one annotation line into a dict ready for CSV.
Accepted patterns:
HH:MM-HH:MM regime [confidence]
HH:MM - HH:MM regime [confidence]
"""
line = line.strip()
if not line:
return None
# Extract time range — allow spaces around dash
time_pattern = r"(\d{1,2}:\d{2})\s*[-–]\s*(\d{1,2}:\d{2})"
m = re.search(time_pattern, line)
if not m:
return None
t_start_str, t_end_str = m.group(1), m.group(2)
remainder = line[m.end():].strip()
# Parse regime and optional confidence from remainder
tokens = remainder.lower().split()
if not tokens:
return None
raw_regime = tokens[0]
# Resolve alias first, then check against VALID_REGIMES
regime = REGIME_ALIASES.get(raw_regime, raw_regime.capitalize())
if regime.lower() not in VALID_REGIMES:
return None
confidence = DEFAULT_CONF
if len(tokens) >= 2 and tokens[1] in VALID_CONF:
confidence = tokens[1]
# Build full datetimes
def make_dt(t_str: str) -> datetime:
h, mi = map(int, t_str.split(":"))
return datetime.combine(session_date, datetime.min.time().replace(hour=h, minute=mi))
try:
dt_start = make_dt(t_start_str)
dt_end = make_dt(t_end_str)
except ValueError:
return None
if dt_end <= dt_start:
return None
return {
"datetime_start": dt_start.strftime("%Y-%m-%d %H:%M:%S"),
"datetime_end": dt_end.strftime("%Y-%m-%d %H:%M:%S"),
"regime": regime,
"confidence": confidence,
}
def load_existing() -> list[dict]:
"""Load existing annotations from CSV, return list of dicts."""
if not os.path.exists(ANNOTATION_FILE):
return []
with open(ANNOTATION_FILE, newline="") as f:
return list(csv.DictReader(f))
def save_annotations(rows: list[dict]):
"""Write all rows to CSV, sorted by datetime_start."""
rows_sorted = sorted(rows, key=lambda r: r["datetime_start"])
os.makedirs(os.path.dirname(ANNOTATION_FILE), exist_ok=True)
with open(ANNOTATION_FILE, "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=FIELDNAMES)
writer.writeheader()
writer.writerows(rows_sorted)
def show_annotations(rows: list[dict], session_date: date | None = None):
"""Print current annotations, optionally filtered to one date."""
if not rows:
print(f" {C_DIM}(no annotations yet){C_RESET}")
return
date_str = session_date.strftime("%Y-%m-%d") if session_date else None
shown = [r for r in rows if (date_str is None or r["datetime_start"].startswith(date_str))]
if not shown:
print(f" {C_DIM}(no annotations for {date_str}){C_RESET}")
return
print(f"\n {'START':<22} {'END':<22} {'REGIME':<12} {'CONF'}")
print(f" {'─'*22} {'─'*22} {'─'*12} {'─'*8}")
for r in shown:
regime_col = {
"Trending": C_OK,
"Range": C_PROMPT,
"Choppy": C_WARN,
}.get(r["regime"], "")
print(f" {r['datetime_start']:<22} {r['datetime_end']:<22} "
f"{regime_col}{r['regime']:<12}{C_RESET} {r['confidence']}")
print()
def confirm(prompt: str) -> bool:
ans = input(f"{C_PROMPT}{prompt} [y/N]: {C_RESET}").strip().lower()
return ans in ("y", "yes")
# ── Main loop ─────────────────────────────────────────────────────────────────
def run():
print_header()
# Load existing
all_rows = load_existing()
print(f" {C_DIM}Loaded {len(all_rows)} existing annotation(s).{C_RESET}\n")
while True:
# ── Ask for date ──────────────────────────────────────────────────────
raw_date = input(
f"{C_PROMPT}Session date{C_RESET} (YYYY-MM-DD, 'today', 'yesterday', or 'quit'): "
).strip()
if raw_date.lower() in ("quit", "q", "exit"):
print(f"\n{C_OK}Saved {len(all_rows)} annotation(s) to:{C_RESET}")
print(f" {ANNOTATION_FILE}")
break
if raw_date.lower() == "show":
show_annotations(all_rows)
continue
session_date = parse_date_input(raw_date)
if session_date is None:
print(f" {C_ERR}Could not parse date. Try: 2022-06-30 or 30-06-2022{C_RESET}\n")
continue
date_label = session_date.strftime("%Y-%m-%d (%A)")
print(f"\n {C_OK}Session: {date_label}{C_RESET}")
print(f" Enter segments one per line. Blank line or 'done' to finish.\n")
# Show existing annotations for this date
show_annotations(all_rows, session_date)
session_rows = []
while True:
try:
raw = input(f" {C_PROMPT}>{C_RESET} ").strip()
except (EOFError, KeyboardInterrupt):
print()
break
if not raw:
break
cmd = raw.lower()
if cmd in ("done", "d"):
break
if cmd in ("quit", "q", "exit"):
# Save and exit entirely
if session_rows:
all_rows.extend(session_rows)
save_annotations(all_rows)
print(f"\n{C_OK}Saved. Exiting.{C_RESET}")
sys.exit(0)
if cmd == "show":
show_annotations(all_rows, session_date)
continue
if cmd == "skip":
print(f" {C_DIM}Session skipped.{C_RESET}")
break
if cmd in ("undo", "u") and session_rows:
removed = session_rows.pop()
print(f" {C_WARN}Removed: {removed['datetime_start']} – "
f"{removed['datetime_end']} {removed['regime']}{C_RESET}")
continue
if cmd.startswith("help") or cmd == "?":
print(f" Format: HH:MM-HH:MM <Regime> [confidence] (direction optional)")
print(f" Example: 09:15-10:30 Trending")
print(f" 11:00-13:45 Choppy medium")
print(f" Commands: done | skip | show | undo | quit")
continue
seg = parse_segment(raw, session_date)
if seg is None:
print(f" {C_ERR}Could not parse: '{raw}'{C_RESET}")
print(f" {C_DIM}Expected: HH:MM-HH:MM Regime [confidence]{C_RESET}")
continue
session_rows.append(seg)
print(f" {C_OK}✔ {seg['datetime_start']} → {seg['datetime_end']} "
f"{seg['regime']} ({seg['confidence']}){C_RESET}")
# ── Confirm and save session ──────────────────────────────────────────
if session_rows:
print(f"\n {C_PROMPT}Summary for {date_label}:{C_RESET}")
for s in session_rows:
print(f" {s['datetime_start'][11:16]}–{s['datetime_end'][11:16]} "
f"{s['regime']:<12} {s['confidence']}")
if confirm("\n Save these annotations?"):
# Remove any existing annotations for this date before appending
date_str = session_date.strftime("%Y-%m-%d")
existing_other = [r for r in all_rows
if not r["datetime_start"].startswith(date_str)]
existing_same = [r for r in all_rows
if r["datetime_start"].startswith(date_str)]
if existing_same:
print(f" {C_WARN}Found {len(existing_same)} existing annotation(s) "
f"for {date_str}.{C_RESET}")
if confirm(" Replace them with the new ones?"):
all_rows = existing_other + session_rows
else:
all_rows = all_rows + session_rows
else:
all_rows.extend(session_rows)
save_annotations(all_rows)
print(f" {C_OK}✔ Saved. Total annotations: {len(all_rows)}{C_RESET}\n")
else:
print(f" {C_WARN}Discarded.{C_RESET}\n")
else:
print(f" {C_DIM}No segments entered for {date_label}.{C_RESET}\n")
# Ask to continue with another date
if not confirm(" Annotate another session?"):
print(f"\n{C_OK}Done. {len(all_rows)} annotation(s) in:{C_RESET}")
print(f" {ANNOTATION_FILE}\n")
break
if __name__ == "__main__":
run()