Spaces:
Running
Running
Ramkumar Shanmugam
fix(ui): sync Today's Briefing banner on import and update BUY score scoring
e06f207 Download scripts/test_spec_regression.py from RamkumarShan/portiq-backend: direct link, hf CLI and curl.
- Browser
- Download file 8.87 kB
-
https://huggingface.co/spaces/RamkumarShan/portiq-backend/resolve/main/scripts/test_spec_regression.py
- Command line
-
hf download hf://spaces/RamkumarShan/portiq-backend/scripts/test_spec_regression.py
-
curl -L -o test_spec_regression.py https://huggingface.co/spaces/RamkumarShan/portiq-backend/resolve/main/scripts/test_spec_regression.py
8.87 kB
| import time | |
| import json | |
| import sys | |
| import os | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| sys.stdout.reconfigure(encoding='utf-8') | |
| from modules.parser import load_portfolio, portfolio_to_text | |
| from modules.strategies import load_strategies, get_strategy_context, FINANCIALS_LENDING_SYMBOLS | |
| from modules.api_caller import run_analysis, enrich_with_signal_prices | |
| from modules.performance import MIN_BUY_SCORE_THRESHOLD | |
| print("================================================================") | |
| print(" PortIQ Regression Test: Score Threshold (>=25), Rebalanced") | |
| print(" Scoring Engine & BFSI / Lending Single-Buy Restriction") | |
| print(" Target File: holdings (18).csv") | |
| print("================================================================\n") | |
| # 1. Load holdings (18).csv | |
| df = load_portfolio('holdings (18).csv') | |
| port_text = portfolio_to_text(df) | |
| strat = load_strategies() | |
| ctx = get_strategy_context(df, strat) | |
| print("--- STRATEGY CONTEXT GENERATED ---\n") | |
| print(ctx) | |
| print("\n----------------------------------\n") | |
| # 2. Call Gemini 2.5 Flash | |
| print("Calling Gemini 2.5 Flash with Hard Allocation Gates & Strict JSON...") | |
| start_time = time.time() | |
| data, err = run_analysis(port_text, ctx, include_news=False) | |
| elapsed = time.time() - start_time | |
| print(f"\nExecution Time: {elapsed:.2f}s") | |
| if err: | |
| print(f"FAILED with error: {err}") | |
| sys.exit(1) | |
| # Enrich with signal prices and deterministic post-generation safety rules | |
| data = enrich_with_signal_prices(data, df) | |
| print("\n=== GEMINI 2.5 FLASH RESPONSE RECEIVED ===") | |
| print("Market Mood:", json.dumps(data.get("mood"), indent=2)) | |
| # Extract Signals | |
| portfolio_signals = {item["symbol"]: item for item in data.get("portfolio", [])} | |
| itc_signal = portfolio_signals.get("ITC", {}) | |
| wipro_signal = portfolio_signals.get("WIPRO", {}) | |
| iex_signal = portfolio_signals.get("IEX", {}) | |
| tatachem_signal = portfolio_signals.get("TATACHEM", {}) | |
| arem_signal = portfolio_signals.get("ARE&M", {}) | |
| natco_signal = portfolio_signals.get("NATCOPHARM", {}) | |
| hdfc_signal = portfolio_signals.get("HDFCBANK", {}) | |
| irfc_signal = portfolio_signals.get("IRFC", {}) | |
| muthoot_signal = portfolio_signals.get("MUTHOOTFIN", {}) | |
| print("\n--- SPECIFIC TARGET VERIFICATIONS ---") | |
| # 1. Verify ITC (Demerger Anomaly) | |
| print(f"1. ITC Signal: {itc_signal.get('signal')} | Score: {itc_signal.get('score')} | Reason: {itc_signal.get('signal_reason')}") | |
| if itc_signal.get("signal") == "HOLD": | |
| print(" [PASS] ITC correctly assigned HOLD (not BUY)!") | |
| else: | |
| print(f" [FAIL] ITC assigned {itc_signal.get('signal')}, expected HOLD") | |
| # 2. Verify WIPRO (Laggard Filter) | |
| print(f"2. WIPRO Signal: {wipro_signal.get('signal')} | Score: {wipro_signal.get('score')} | Reason: {wipro_signal.get('signal_reason')}") | |
| if wipro_signal.get("signal") in ["HOLD", "AVOID"]: | |
| print(" [PASS] WIPRO correctly constrained to HOLD/AVOID (not BUY)!") | |
| else: | |
| print(f" [FAIL] WIPRO assigned {wipro_signal.get('signal')}, expected HOLD/AVOID") | |
| # 3. Verify IEX (Regulatory Moat Threat) | |
| print(f"3. IEX Signal: {iex_signal.get('signal')} | Score: {iex_signal.get('score')} | Reason: {iex_signal.get('signal_reason')}") | |
| if iex_signal.get("signal") in ["WATCH", "AVOID"]: | |
| print(" [PASS] IEX correctly downgraded to WATCH/AVOID (not BUY)!") | |
| else: | |
| print(f" [FAIL] IEX assigned {iex_signal.get('signal')}, expected WATCH or AVOID") | |
| # 4. Verify TATACHEM (Commodity Trough & Conglomerate Cap) | |
| print(f"4. TATACHEM Signal: {tatachem_signal.get('signal')} | Score: {tatachem_signal.get('score')} | Reason: {tatachem_signal.get('signal_reason')}") | |
| if tatachem_signal.get("signal") in ["WATCH", "AVOID"]: | |
| print(" [PASS] TATACHEM correctly downgraded to WATCH/AVOID (not BUY)!") | |
| else: | |
| print(f" [FAIL] TATACHEM assigned {tatachem_signal.get('signal')}, expected WATCH or AVOID") | |
| # 5. Verify ARE&M (Quarterly Earnings & Margin Deterioration Gate) | |
| print(f"5. ARE&M Signal: {arem_signal.get('signal')} | Score: {arem_signal.get('score')} | Reason: {arem_signal.get('signal_reason')}") | |
| if arem_signal.get("signal") in ["WATCH", "AVOID"]: | |
| print(" [PASS] ARE&M correctly downgraded to WATCH due to [EARNINGS_MARGIN_PRESSURE]!") | |
| else: | |
| print(f" [FAIL] ARE&M assigned {arem_signal.get('signal')}, expected WATCH") | |
| # 6. Verify NATCOPHARM (Unified Pharma & Healthcare Sector Cap Exceeded) | |
| print(f"6. NATCOPHARM Signal: {natco_signal.get('signal')} | Score: {natco_signal.get('score')} | Reason: {natco_signal.get('signal_reason')}") | |
| if natco_signal.get("signal") == "HOLD": | |
| print(" [PASS] NATCOPHARM correctly assigned HOLD due to unified Pharma & Healthcare [SECTOR_CAP_EXCEEDED]!") | |
| else: | |
| print(f" [FAIL] NATCOPHARM assigned {natco_signal.get('signal')}, expected HOLD") | |
| # 7. Verify HDFCBANK (Under-Allocated Large-Cap Banking Compounder) | |
| print(f"7. HDFCBANK Signal: {hdfc_signal.get('signal')} | Score: {hdfc_signal.get('score')} | Reason: {hdfc_signal.get('signal_reason')}") | |
| if hdfc_signal.get("signal") == "BUY": | |
| print(" [PASS] HDFCBANK correctly selected as BUY compounder!") | |
| else: | |
| print(f" [FAIL] HDFCBANK assigned {hdfc_signal.get('signal')}, expected BUY") | |
| # 8. Verify IRFC (Defaults to HOLD/WATCH, not BUY) | |
| print(f"8. IRFC Signal: {irfc_signal.get('signal')} | Score: {irfc_signal.get('score')} | Reason: {irfc_signal.get('signal_reason')}") | |
| if irfc_signal.get("signal") in ["HOLD", "WATCH"]: | |
| print(" [PASS] IRFC correctly constrained to HOLD/WATCH (not BUY)!") | |
| else: | |
| print(f" [FAIL] IRFC assigned {irfc_signal.get('signal')}, expected HOLD/WATCH") | |
| # 9. Verify MUTHOOTFIN (Defaults to HOLD/WATCH, not BUY) | |
| print(f"9. MUTHOOTFIN Signal: {muthoot_signal.get('signal')} | Score: {muthoot_signal.get('score')} | Reason: {muthoot_signal.get('signal_reason')}") | |
| if muthoot_signal.get("signal") in ["HOLD", "WATCH"]: | |
| print(" [PASS] MUTHOOTFIN correctly constrained to HOLD/WATCH (not BUY)!") | |
| else: | |
| print(f" [FAIL] MUTHOOTFIN assigned {muthoot_signal.get('signal')}, expected HOLD/WATCH") | |
| # 10. Verify Minimum BUY Score Threshold (>= 25 pts) across ALL BUY signals | |
| print("\n--- MINIMUM BUY SCORE THRESHOLD VERIFICATION (>= 25 PTS) ---") | |
| all_buys = [item for item in data.get("portfolio", []) if item.get("signal") == "BUY"] | |
| score_pass = True | |
| for b in all_buys: | |
| sym = b.get("symbol") | |
| sc = b.get("score", 0) | |
| print(f" BUY Candidate: {sym:12} | Score: {sc}/100") | |
| if sc < MIN_BUY_SCORE_THRESHOLD: | |
| print(f" [FAIL] {sym} has BUY signal with score {sc} < {MIN_BUY_SCORE_THRESHOLD}!") | |
| score_pass = False | |
| if score_pass: | |
| print(f" [PASS] All {len(all_buys)} BUY signals satisfy score >= {MIN_BUY_SCORE_THRESHOLD} threshold!") | |
| # 11. Verify BFSI / Lending Concentration (Max 1 Financial BUY) | |
| print("\n--- BFSI / LENDING SINGLE-BUY RESTRICTION ---") | |
| financial_buys = [b for b in all_buys if b.get("symbol") in FINANCIALS_LENDING_SYMBOLS] | |
| print(f"Financial BUY count: {len(financial_buys)}: {[b.get('symbol') for b in financial_buys]}") | |
| if len(financial_buys) <= 1: | |
| print(" [PASS] Exactly <= 1 financial stock recommended for BUY per session!") | |
| else: | |
| print(f" [FAIL] Multiple financial stocks recommended for BUY: {[b.get('symbol') for b in financial_buys]}") | |
| # 12. Verify Top Picks | |
| top_picks = data.get("top_picks", []) | |
| print(f"\n--- Top Picks ({len(top_picks)}) ---") | |
| top_pick_symbols = [] | |
| financial_top_picks = [] | |
| for tp in top_picks: | |
| sym = tp.get('symbol') | |
| top_pick_symbols.append(sym) | |
| if sym in FINANCIALS_LENDING_SYMBOLS: | |
| financial_top_picks.append(sym) | |
| print(f" Rank {tp.get('rank')}: {sym} ({tp.get('action')})") | |
| print(f" Reason: {tp.get('reason')}") | |
| print(f" Why Now: {tp.get('why_now')}") | |
| print(f" Verdict: {tp.get('fundamental_verdict')}") | |
| print(f" Risk: {tp.get('risk')}") | |
| # Negative Assertions on Top Picks | |
| disqualified_candidates = ["IEX", "TATACHEM", "ITC", "WIPRO", "ARE&M", "NATCOPHARM", "IRFC", "MUTHOOTFIN"] | |
| for disq in disqualified_candidates: | |
| if disq in top_pick_symbols: | |
| print(f" [FAIL] {disq} appeared in top_picks!") | |
| else: | |
| print(f" [PASS] {disq} successfully excluded from top_picks!") | |
| # Positive Assertions: Capital rotation to HDFCBANK | |
| if "HDFCBANK" in top_pick_symbols: | |
| print(" [PASS] HDFCBANK successfully surfaced in top_picks as capital rotated into Large-Cap Banking!") | |
| else: | |
| print(f" [INFO] Top picks selected: {top_pick_symbols}") | |
| # Single Financial in Top Picks | |
| if len(financial_top_picks) <= 1: | |
| print(f" [PASS] At most 1 financial stock in top_picks: {financial_top_picks}") | |
| else: | |
| print(f" [FAIL] Multiple financial stocks in top_picks: {financial_top_picks}") | |
| # Check Avoid List | |
| print(f"\n--- Avoid Today ({len(data.get('avoid_today', []))}) ---") | |
| for av in data.get("avoid_today", []): | |
| print(f" - {av.get('symbol')}: {av.get('reason')}") | |
| print("\nRegression test finished!") | |