-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_plotter.py
More file actions
1535 lines (1333 loc) · 62.1 KB
/
simple_plotter.py
File metadata and controls
1535 lines (1333 loc) · 62.1 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import re
import warnings
import logging
import pandas as pd
import matplotlib.pyplot as plt
import base64
from io import BytesIO
from typing import List, Dict, Any, Optional, Tuple
from utils_query import (
match_variable_from_yaml,
extract_region_from_query,
find_closest_variable_name,
resolve_natural_language_variable_universal,
resolve_natural_language_variable_candidates,
resolve_natural_language_variable_with_score,
resolve_natural_language_variable_ranked,
format_region_label,
)
def _is_capacity_additions_mismatch(question: str, variable: str | None) -> bool:
ql = str(question or "").lower()
vl = str(variable or "").lower()
if "capacity additions" not in vl:
return False
if "capacity" not in ql:
return False
return not any(
token in ql
for token in ["addition", "additions", "new capacity", "build rate", "annual build"]
)
def _pretty_variable_name(variable: str) -> str:
value = str(variable or "").strip()
lower = value.lower()
if "capacity|electricity|solar" in lower:
return "Solar Capacity"
if "capacity|electricity|wind" in lower:
return "Wind Capacity"
if "secondary energy|electricity|solar" in lower:
return "Solar Electricity"
if "secondary energy|electricity|wind" in lower:
return "Wind Electricity"
if "secondary energy|electricity" in lower:
return "Electricity Generation"
if "emissions|co2" in lower or lower.startswith("gross emissions|co2"):
return "CO2 Emissions"
if "final energy" in lower and "oil" in lower:
return "Oil Final Energy Demand"
if "primary energy" in lower and "oil" in lower:
return "Oil Primary Energy"
parts = [part.strip() for part in value.split("|") if part.strip()]
if not parts:
return value
if parts[0].lower() == "capacity" and len(parts) >= 3:
return f"{parts[-1]} Capacity"
if parts[0].lower() == "secondary energy" and len(parts) >= 3 and parts[1].lower() == "electricity":
return f"{parts[-1]} Electricity"
if parts[0].lower().startswith("emissions") and len(parts) >= 2:
return f"{parts[1]} Emissions"
return parts[-1]
def _year_range_text(start_year: int | None = None, end_year: int | None = None) -> str:
if start_year or end_year:
return f" ({start_year or '?'}-{end_year or '?'})"
return ""
def _plot_subject(variable: str, region: str | None = None) -> str:
label = _pretty_variable_name(variable)
if region:
return f"{label} in {region}"
return label
def _preferred_plot_family_matches(question: str, available_vars: set[str]) -> list[str]:
"""
Keep plot-side variable resolution aligned with data-side resolution for
common plain-language requests like "solar energy" and "oil demand".
"""
ql = str(question or "").lower()
candidates: list[str] = []
if "solar" in ql:
if "capacity" in ql:
candidates.extend(
v for v in available_vars
if "solar" in v.lower() and "capacity|electricity" in v.lower()
)
if any(token in ql for token in ["energy", "electricity", "power", "generation"]):
candidates.extend(
v for v in available_vars
if "solar" in v.lower()
and (
"secondary energy|electricity" in v.lower()
or "generation|electricity" in v.lower()
or "capacity|electricity" in v.lower()
)
and "investment" not in v.lower()
)
if "oil" in ql and any(token in ql for token in ["demand", "consumption", "energy", "use"]):
candidates.extend(
v for v in available_vars
if "oil" in v.lower()
and any(token in v.lower() for token in ["final energy", "primary energy", "secondary energy", "demand"])
and "investment" not in v.lower()
)
if "electricity" in ql and not any(
token in ql for token in [
"solar", "wind", "hydro", "nuclear", "oil", "gas", "coal", "hydrogen", "bioenergy", "biomass",
"capacity", "generation", "demand", "supply", "emission", "emissions", "co2",
"price", "cost", "investment", "share",
]
):
candidates.extend(
v for v in available_vars
if v in {
"Secondary Energy|Electricity",
"Final Energy|Electricity",
"Capacity|Electricity",
}
)
deduped: list[str] = []
for candidate in candidates:
if candidate and candidate not in deduped:
deduped.append(candidate)
def _score(candidate: str) -> tuple[int, int, int, str]:
lower = candidate.lower()
exact_capacity = lower == "capacity|electricity|solar"
exact_solar_electricity = lower == "secondary energy|electricity|solar"
exact_oil_final = lower == "final energy|oil"
exact_oil_primary = lower == "primary energy|oil"
exact_electricity = lower == "secondary energy|electricity"
broad_energy = (
"secondary energy|electricity|solar" in lower
or "capacity|electricity|solar" in lower
or "final energy|oil" in lower
or "primary energy|oil" in lower
or lower == "secondary energy|electricity"
or lower == "final energy|electricity"
or lower == "capacity|electricity"
)
return (
0 if (exact_solar_electricity or exact_capacity or exact_oil_final or exact_oil_primary or exact_electricity) else 1,
0 if broad_energy else 1,
lower.count("|"),
len(lower),
)
return sorted(deduped, key=_score)
def _wrap_plot_markdown(
plot_str: str,
variable: str,
region: str | None = None,
scenario: str | None = None,
scenarios_in_data: list | None = None,
start_year: int | None = None,
end_year: int | None = None,
prefix: str = "Showing",
) -> str:
subject = _plot_subject(variable, region)
years = _year_range_text(start_year, end_year)
if scenario:
caption = f"{prefix} {subject} for scenario `{scenario}`{years}."
elif scenarios_in_data and len([s for s in scenarios_in_data if s]) > 1:
caption = f"{prefix} {subject} across available scenarios{years}."
else:
caption = f"{prefix} {subject}{years}."
return caption + "\n" + plot_str
from utils.yaml_loader import load_all_yaml_files
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
# Global metadata instance (lazy loaded)
_metadata = None
logger = logging.getLogger(__name__)
# Suppress tight_layout warnings globally for cleaner output
warnings.filterwarnings(
"ignore",
message="Tight layout not applied.*",
category=UserWarning
)
def get_metadata(ts_data: List[Dict] = None, models: List[Dict] = None):
"""Get or create DataMetadata instance."""
global _metadata
if _metadata is None and ts_data is not None:
from data_metadata import build_metadata_with_cache
_metadata = build_metadata_with_cache(ts_data, models)
return _metadata
def generate_llm_suggestion(query: str, variable: str, region: str,
available_regions: List[str], available_scenarios: List[str],
api_key: str) -> str:
"""
Use LLM to generate helpful suggestions when data is not found.
Args:
query: Original user query
variable: Requested variable
region: Requested region
available_regions: List of available regions for the variable
available_scenarios: List of available scenarios for the variable
api_key: OpenAI API key
Returns:
Helpful suggestion message
"""
llm = ChatOpenAI(
model_name="gpt-4-turbo",
temperature=0.7,
api_key=api_key
)
prompt = ChatPromptTemplate.from_messages([
SystemMessagePromptTemplate.from_template("""You are a helpful assistant for the IAM PARIS climate data platform.
A user requested data that is not available. Generate a helpful, friendly response that:
1. Explains that the specific data combination is not available
2. Suggests similar alternatives from the available data
3. Offers to help the user find what they're looking for
Be concise and helpful. Use Markdown formatting.
## Context:
- Requested variable: {variable}
- Requested region: {region}
- Available regions for this variable: {available_regions}
- Available scenarios for this variable: {available_scenarios}
Generate a helpful response:"""),
HumanMessagePromptTemplate.from_template("User query: {query}")
])
chain = prompt | llm
response = chain.invoke({
"query": query,
"variable": variable,
"region": region,
"available_regions": ", ".join(available_regions[:20]) if available_regions else "None",
"available_scenarios": ", ".join(available_scenarios[:10]) if available_scenarios else "None"
})
return response.content
def detect_multi_variable_comparison(query: str) -> List[str]:
"""
Detect if query is asking to compare multiple variables.
Args:
query: User query string
Returns:
List of variable keywords found in comparison context
"""
query_lower = query.lower()
# Patterns that indicate multi-variable comparison
comparison_patterns = [
r'compare\s+(\w+)\s+(?:and|vs|versus|with)\s+(\w+)',
r'(\w+)\s+(?:and|vs|versus)\s+(\w+)\s+(?:capacity|generation|energy|emissions)',
r'(\w+)\s+vs\s+(\w+)', # Simple "X vs Y" pattern
r'both\s+(\w+)\s+and\s+(\w+)',
r'(\w+)\s+or\s+(\w+)',
]
for pattern in comparison_patterns:
match = re.search(pattern, query_lower)
if match:
return [match.group(1), match.group(2)]
return []
def detect_region_comparison(question: str, metadata) -> List[str]:
"""
Detect region comparison like 'USA vs EU' and return matched regions.
"""
if not metadata:
return []
ql = question.lower()
def _match_region(text: str) -> str | None:
t = text.lower()
if re.search(r"\\busa\\b|\\bunited\\s+states\\b|\\bu\\.s\\.\\b|\\bus\\b", t):
return "USA"
if re.search(r"\\beu\\b|\\beurope\\b|\\beuropean\\b", t):
return "EU"
if re.search(r"\\bchina\\b|\\bchn\\b", t):
return "CHN"
if re.search(r"\\bindia\\b|\\bind\\b", t):
return "IND"
# Prefer exact region names if present in text
all_regions = sorted({reg for regs in metadata.variable_regions.values() for reg in regs})
candidates = []
for r in all_regions:
if r and r.lower() in t:
candidates.append(r)
if candidates:
return max(candidates, key=len)
return metadata._find_best_region_match(text)
if " vs " in ql or " versus " in ql:
splitter = " vs " if " vs " in ql else " versus "
left, right = ql.split(splitter, 1)
r1 = _match_region(left)
r2 = _match_region(right)
if r1 and r2 and r1 != r2:
return [r1, r2]
# Handle "for X and Y" or "in X and Y"
if " and " in ql and (" for " in ql or " in " in ql):
anchor = " for " if " for " in ql else " in "
tail = ql.rsplit(anchor, 1)[-1]
parts = [p.strip() for p in tail.split(" and ") if p.strip()]
if len(parts) >= 2:
r1 = _match_region(parts[0])
r2 = _match_region(parts[1])
if r1 and r2 and r1 != r2:
return [r1, r2]
return []
def plot_variable_across_regions(question: str, model_data: List[Dict], ts_data: List[Dict],
variable: str, regions: List[str],
scenario: str = None, start_year: int = None,
end_year: int = None) -> str:
"""
Plot a single variable across multiple regions.
"""
if not variable or not regions:
return "Could not identify enough regions to compare."
# Collect data for each region
all_data = {}
unit = None
for region in regions:
filtered = []
for r in ts_data:
if r is None:
continue
if str(r.get('variable', '')) != variable:
continue
if scenario and r.get('scenario') != scenario:
continue
if region and str(r.get('region', '')).lower() != region.lower():
continue
filtered.append(r)
if filtered:
all_data[region] = filtered
if unit is None:
unit = filtered[0].get('unit', '')
if not all_data:
return f"No data found for **{variable}** in the requested regions."
plt.figure(figsize=(12, 7))
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b']
markers = ['o', 's', '^', 'D', 'v', '<']
scenarios_in_data = set()
for idx, (region, data) in enumerate(all_data.items()):
df = pd.DataFrame(data)
if 'years' in df.columns:
years_df = df['years'].apply(pd.Series)
df = df.drop('years', axis=1).join(years_df)
year_cols = [col for col in df.columns if str(col).isdigit()]
if start_year or end_year:
filtered_year_cols = []
for col in year_cols:
year_int = int(col)
if start_year and year_int < start_year:
continue
if end_year and year_int > end_year:
continue
filtered_year_cols.append(col)
if filtered_year_cols:
year_cols = filtered_year_cols
if len(df) > 1:
df = df.groupby('scenario').first().reset_index()
# Plot first row per region (scenario already filtered above)
row = df.iloc[0]
if 'scenario' in row:
scenarios_in_data.add(str(row.get('scenario', '')).strip())
values = [row.get(str(year), 0) for year in sorted(year_cols, key=int)]
plt.plot(sorted(year_cols, key=int), values,
label=region,
color=colors[idx % len(colors)],
marker=markers[idx % len(markers)],
linewidth=2)
title = f"{_pretty_variable_name(variable)}: " + " vs ".join(format_region_label(r) for r in regions)
plt.title(title, fontsize=12, fontweight='bold')
plt.xlabel("Year", fontsize=10)
if unit:
plt.ylabel(f"Value ({unit})", fontsize=10)
plt.legend()
plt.grid(alpha=0.3)
plt.tight_layout()
plot_str = save_plot_to_base64()
return _wrap_plot_markdown(plot_str, variable, None, scenario, list(scenarios_in_data), start_year, end_year, prefix="Showing")
def plot_multiple_variables(question: str, model_data: List[Dict], ts_data: List[Dict],
variables: List[str], region: str = None,
scenario: str = None, start_year: int = None,
end_year: int = None) -> str:
"""
Generate a plot comparing multiple variables.
Args:
question: Original user query
model_data: List of model metadata
ts_data: List of time series data
variables: List of variable names to compare (can be keywords or exact names)
region: Optional region filter
scenario: Optional scenario filter
start_year: Optional start year for filtering
end_year: Optional end year for filtering
Returns:
Base64 encoded PNG image or error message
"""
metadata = get_metadata(ts_data, model_data)
# Check if variables are already exact names (from LLM extraction)
# or if they need to be resolved (from regex detection)
resolved_variables = []
for var in variables:
# Check if it's an exact variable name
exact_match = False
for r in ts_data:
if r and r.get('variable') == var:
resolved_variables.append(var)
exact_match = True
logger.debug("Using exact variable: '%s'", var)
break
# If not exact, try to resolve using metadata
if not exact_match and metadata:
suggestions = metadata.suggest_variables(var, limit=3)
if suggestions:
resolved_variables.append(suggestions[0][0])
logger.debug("Resolved '%s' to '%s'", var, suggestions[0][0])
if len(resolved_variables) < 2:
return f"Could not identify enough variables to compare. Found: {resolved_variables}"
# Extract region from query if not provided
if region is None:
# Check for common region names in query first
region_keywords = ['world', 'europe', 'eu', 'usa', 'china', 'india', 'africa', 'asia', 'greece', 'germany', 'brazil']
question_lower = question.lower()
for kw in region_keywords:
if kw in question_lower:
if metadata:
matched = metadata._find_best_region_match(kw)
if matched:
region = matched
break
else:
region = kw.title()
break
# Fallback: Use metadata to find region in query
if region is None and metadata:
region = metadata._find_best_region_match(question)
logger.debug("Using region: %s", region)
# Collect data for each variable
all_data = {}
units = {}
for variable in resolved_variables:
filtered_data = []
for r in ts_data:
if r is None:
continue
if str(r.get('variable', '')) != variable:
continue
if scenario and r.get('scenario') != scenario:
continue
if region:
r_region = str(r.get('region', ''))
if r_region.lower() != region.lower():
continue
filtered_data.append(r)
if filtered_data:
all_data[variable] = filtered_data
units[variable] = filtered_data[0].get('unit', '')
if not all_data:
return f"No data found for the requested variables in region '{region}'."
# Create comparison plot
plt.figure(figsize=(12, 7))
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b']
markers = ['o', 's', '^', 'D', 'v', '<']
scenarios_in_data = set()
for idx, (variable, data) in enumerate(all_data.items()):
df = pd.DataFrame(data)
# Handle years column
if 'years' in df.columns:
years_df = df['years'].apply(pd.Series)
df = df.drop('years', axis=1).join(years_df)
# Get year columns
year_cols = [col for col in df.columns if str(col).isdigit()]
if not year_cols:
continue
# Filter to specific year range if requested
if start_year or end_year:
filtered_year_cols = []
for col in year_cols:
year_int = int(col)
if start_year and year_int < start_year:
continue
if end_year and year_int > end_year:
continue
filtered_year_cols.append(col)
if filtered_year_cols:
year_cols = filtered_year_cols
# Aggregate if multiple rows (take mean)
if len(df) > 1:
# Group by scenario and take first of each
df = df.groupby('scenario').first().reset_index()
# Plot each row
for _, row in df.iterrows():
label = variable
if len(df) > 1:
label = f"{variable} ({row.get('scenario', '')})"
if 'scenario' in row:
scenarios_in_data.add(str(row.get('scenario', '')).strip())
values = [row.get(str(year), 0) for year in sorted(year_cols, key=int)]
plt.plot(sorted(year_cols, key=int), values,
label=label,
color=colors[idx % len(colors)],
marker=markers[idx % len(markers)],
linewidth=2)
# Build title
title = f"Comparison: {' vs '.join([_pretty_variable_name(v) for v in all_data.keys()])}"
if region:
title += f" for {format_region_label(region)}"
plt.title(title, fontsize=12, fontweight='bold')
plt.xlabel("Year", fontsize=10)
# Use first unit as Y-axis label (assuming same units)
if units:
first_unit = list(units.values())[0]
plt.ylabel(f"Value ({first_unit})", fontsize=10)
plt.legend(loc='best', fontsize=9)
plt.grid(True, alpha=0.3)
plt.tight_layout()
# Save to base64
buf = BytesIO()
plt.savefig(buf, format='png', dpi=100)
buf.seek(0)
img_base64 = base64.b64encode(buf.read()).decode('utf-8')
plt.close()
plot_str = f""
caption_var = " vs ".join([_pretty_variable_name(v) for v in all_data.keys()])
return _wrap_plot_markdown(plot_str, caption_var, region, scenario, list(scenarios_in_data), start_year, end_year, prefix="Showing comparison of")
def plot_model_comparison(question: str, model_data: List[Dict], ts_data: List[Dict],
variable: str, models: List[str], region: str = None,
scenario: str = None, start_year: int = None,
end_year: int = None) -> str:
"""
Generate a plot comparing the same variable across different models.
Args:
question: Original user query
model_data: List of model metadata
ts_data: List of time series data
variable: Variable name to compare
models: List of model names to compare
region: Optional region filter
scenario: Optional scenario filter
start_year: Optional start year for filtering
end_year: Optional end year for filtering
Returns:
Base64 encoded PNG image or error message
"""
metadata = get_metadata(ts_data, model_data)
# Resolve variable name if needed
resolved_variable = None
for r in ts_data:
if r and r.get('variable') == variable:
resolved_variable = variable
break
if not resolved_variable and metadata:
suggestions = metadata.suggest_variables(variable, limit=3)
if suggestions:
resolved_variable = suggestions[0][0]
logger.debug("Resolved variable '%s' to '%s'", variable, resolved_variable)
if not resolved_variable:
return f"Could not identify variable '{variable}'."
# Resolve model names (fuzzy match)
# Note: ts_data uses 'modelName' field, not 'model'
resolved_models = []
available_models = sorted({str(r.get('modelName', '') or r.get('model', '')) for r in ts_data if r and (r.get('modelName') or r.get('model'))})
logger.debug("ts_data length: %s", len(ts_data))
logger.debug("Available models in ts_data: %s...", available_models[:20])
logger.debug("Looking for models: %s", models)
# If ts_data is empty, try to get models from model_data
if not available_models and model_data:
available_models = sorted({str(m.get('modelName', '')) for m in model_data if m and m.get('modelName')})
logger.debug("Using model_data instead. Available models: %s...", available_models[:20])
for model_name in models:
# Try exact match first
if model_name in available_models:
resolved_models.append(model_name)
logger.debug("Using exact model: '%s'", model_name)
continue
# Try case-insensitive match
for avail in available_models:
if avail.lower() == model_name.lower():
resolved_models.append(avail)
logger.debug("Matched model '%s' to '%s'", model_name, avail)
break
else:
# Try partial match
for avail in available_models:
if model_name.lower() in avail.lower() or avail.lower() in model_name.lower():
resolved_models.append(avail)
logger.debug("Partial matched model '%s' to '%s'", model_name, avail)
break
if len(resolved_models) < 2:
return f"Could not identify enough models to compare. Found: {resolved_models}. Available models include: {', '.join(available_models[:10])}..."
# Extract region from query if not provided
if region is None and metadata:
region = metadata._find_best_region_match(question)
logger.debug("Model comparison - variable: %s, models: %s, region: %s", resolved_variable, resolved_models, region)
# Collect data for each model
all_data = {}
units = {}
for model_name in resolved_models:
filtered_data = []
for r in ts_data:
if r is None:
continue
if str(r.get('variable', '')) != resolved_variable:
continue
# Use modelName field (the actual field name in ts_data)
r_model = str(r.get('modelName', '') or r.get('model', ''))
if r_model != model_name:
continue
if scenario and r.get('scenario') != scenario:
continue
if region:
r_region = str(r.get('region', ''))
if r_region.lower() != region.lower():
continue
filtered_data.append(r)
if filtered_data:
all_data[model_name] = filtered_data
units[model_name] = filtered_data[0].get('unit', '')
if not all_data:
return f"No data found for '{resolved_variable}' across models {resolved_models} in region '{region}'."
# Create comparison plot
plt.figure(figsize=(12, 7))
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b']
markers = ['o', 's', '^', 'D', 'v', '<']
scenarios_in_data = set()
for idx, (model_name, data) in enumerate(all_data.items()):
df = pd.DataFrame(data)
# Handle years column
if 'years' in df.columns:
years_df = df['years'].apply(pd.Series)
df = df.drop('years', axis=1).join(years_df)
# Get year columns
year_cols = [col for col in df.columns if str(col).isdigit()]
if not year_cols:
continue
# Filter to specific year range if requested
if start_year or end_year:
filtered_year_cols = []
for col in year_cols:
year_int = int(col)
if start_year and year_int < start_year:
continue
if end_year and year_int > end_year:
continue
filtered_year_cols.append(col)
if filtered_year_cols:
year_cols = filtered_year_cols
# Aggregate if multiple rows (take mean by scenario)
if len(df) > 1:
# Group by scenario and take first of each
df = df.groupby('scenario').first().reset_index()
# Plot each row
for _, row in df.iterrows():
label = model_name
if len(df) > 1:
label = f"{model_name} ({row.get('scenario', '')})"
if 'scenario' in row:
scenarios_in_data.add(str(row.get('scenario', '')).strip())
values = [row.get(str(year), 0) for year in sorted(year_cols, key=int)]
plt.plot(sorted(year_cols, key=int), values,
label=label,
color=colors[idx % len(colors)],
marker=markers[idx % len(markers)],
linewidth=2)
# Build title
title = f"Model Comparison: {_pretty_variable_name(resolved_variable)}"
if region:
title += f" for {format_region_label(region)}"
if start_year or end_year:
title += f" ({start_year or '?'}-{end_year or '?'})"
plt.title(title, fontsize=12, fontweight='bold')
plt.xlabel("Year", fontsize=10)
# Use first unit as Y-axis label
if units:
first_unit = list(units.values())[0]
plt.ylabel(f"{_pretty_variable_name(resolved_variable)} ({first_unit})", fontsize=10)
plt.legend(loc='best', fontsize=9)
plt.grid(True, alpha=0.3)
plt.tight_layout()
# Save to base64
buf = BytesIO()
plt.savefig(buf, format='png', dpi=100)
buf.seek(0)
img_base64 = base64.b64encode(buf.read()).decode('utf-8')
plt.close()
plot_str = f""
return _wrap_plot_markdown(plot_str, f"model comparison of {_pretty_variable_name(resolved_variable)}", region, scenario, list(scenarios_in_data), start_year, end_year, prefix="Showing")
def simple_plot_query_with_entities(question: str, model_data: List[Dict], ts_data: List[Dict],
entities: Dict[str, Any], region: str = None) -> str:
"""
Generate a plot using pre-extracted entities for better accuracy.
Args:
question: Original user query
model_data: List of model metadata
ts_data: List of time series data
entities: Pre-extracted entities from QueryEntityExtractor
region: Optional region override
Returns:
Base64 encoded PNG image or error message
"""
# Check for multi-variable comparison from LLM-extracted entities
variables_list = entities.get('variables')
models_list = entities.get('models')
comparison_type = entities.get('comparison')
# Check for model comparison
if models_list and len(models_list) >= 2:
logger.debug("LLM detected multi-model comparison: %s", models_list)
variable = entities.get('variable')
scenario = entities.get('scenario')
region_from_entities = entities.get('region')
start_year = entities.get('start_year')
end_year = entities.get('end_year')
return plot_model_comparison(question, model_data, ts_data, variable, models_list,
region or region_from_entities, scenario,
start_year, end_year)
metadata = get_metadata(ts_data, model_data)
region_compare = detect_region_comparison(question, metadata)
available_vars = {str(r.get('variable', '')).strip() for r in ts_data if r and r.get('variable')}
ranked_vars = []
significant_words = []
if variables_list and len(variables_list) >= 2:
region_keywords = {
"usa", "us", "united", "states", "eu", "europe", "china", "chn", "india", "ind",
"asia", "africa", "world", "global", "oecd", "latin", "america", "european"
}
if any(v.lower() in region_keywords for v in variables_list):
variables_list = []
logger.debug("LLM detected multi-variable comparison: %s", variables_list)
scenario = entities.get('scenario')
region_from_entities = entities.get('region')
start_year = entities.get('start_year')
end_year = entities.get('end_year')
return plot_multiple_variables(question, model_data, ts_data, variables_list,
region or region_from_entities, scenario,
start_year, end_year)
# Fallback: Check for multi-variable comparison using regex patterns
comparison_vars = detect_multi_variable_comparison(question)
if region_compare and comparison_vars:
region_keywords = {
"usa", "us", "united", "states", "eu", "europe", "china", "chn", "india", "ind",
"asia", "africa", "world", "global", "oecd", "latin", "america", "european"
}
if any(v.lower() in region_keywords for v in comparison_vars):
comparison_vars = []
if len(comparison_vars) >= 2:
logger.debug("Regex detected multi-variable comparison: %s", comparison_vars)
scenario = entities.get('scenario')
start_year = entities.get('start_year')
end_year = entities.get('end_year')
return plot_multiple_variables(question, model_data, ts_data, comparison_vars, region, scenario,
start_year, end_year)
# Use extracted entities directly
variable = entities.get('variable')
if isinstance(variable, str):
variable = variable.strip()
scenario = entities.get('scenario')
model = entities.get('model')
start_year = entities.get('start_year')
end_year = entities.get('end_year')
unit = entities.get('unit')
comparison = entities.get('comparison')
if variable:
v = str(variable).lower()
ql = question.lower()
if "emission" in ql and "emission" not in v:
variable = None
elif "co2" in ql and "co2" not in v:
variable = None
if variable and "solar" in ql and "solar" not in v:
variable = None
if variable and "wind" in ql and "wind" not in v:
variable = None
if variable and "capacity" in ql and "capacity" not in v:
variable = None
if variable and _is_capacity_additions_mismatch(question, variable):
variable = None
# Use region from entities if not overridden
if region is None:
region = entities.get('region')
if not variable:
preferred_family = _preferred_plot_family_matches(question, available_vars)
if preferred_family:
variable = preferred_family[0]
# If no variable extracted, fall back to keyword extraction
if not variable:
from pathlib import Path
variable_path = Path('definitions/variable').resolve()
variable_dict = load_all_yaml_files(str(variable_path))
ranked_vars = resolve_natural_language_variable_ranked(question, variable_dict, top_k=5)
natural_variable, var_score, _, significant_words = resolve_natural_language_variable_with_score(question, variable_dict)
if natural_variable and isinstance(natural_variable, str):
natural_variable = natural_variable.strip()
if natural_variable in available_vars:
var_lower = natural_variable.lower()
if any(t in significant_words for t in ["emission", "emissions"]) and "emission" not in var_lower:
natural_variable = None
elif "co2" in significant_words and "co2" not in var_lower:
natural_variable = None
if "capacity" in significant_words and "capacity" not in var_lower:
natural_variable = None
if "solar" in significant_words and "solar" not in var_lower:
natural_variable = None
if "wind" in significant_words and "wind" not in var_lower:
natural_variable = None
if natural_variable and _is_capacity_additions_mismatch(question, natural_variable):
natural_variable = None
explicit_variable = "|" in question
min_conf = 6
if any(w in significant_words for w in ["capacity", "investment", "investments", "invest"]):
min_conf = 4
if var_score is not None and not explicit_variable:
top1 = ranked_vars[0][1] if ranked_vars else None
top2 = ranked_vars[1][1] if ranked_vars and len(ranked_vars) > 1 else None
ambiguous = top1 is not None and top2 is not None and (top1 - top2) < 3
if var_score < min_conf or ambiguous:
natural_variable = None
if natural_variable:
variable = natural_variable
if not variable:
candidates = []
preferred_family = _preferred_plot_family_matches(question, available_vars)
if preferred_family:
candidates = preferred_family[:3]
if ranked_vars:
ranked_candidates = [name for name, _, _, _ in ranked_vars if name in available_vars][:3]
for candidate in ranked_candidates:
if candidate not in candidates:
candidates.append(candidate)
candidates = candidates[:3]
if not candidates:
candidates = resolve_natural_language_variable_candidates(question, variable_dict, top_k=3)
if candidates:
sample = ", ".join(candidates)
return (
"Which variable should I use?\n"
f"Recommended variables: {sample}\n"
"Reply with the variable you want."
)
if not variable:
# Final attempt: use metadata to suggest similar variables
metadata = get_metadata(ts_data, model_data)
if metadata:
similar = metadata._suggest_similar_variables(question)
if similar:
return f"Could not identify a variable to plot. Did you mean: {', '.join(similar[:3])}?"
return "Could not identify a variable to plot. Please specify a variable like 'solar capacity' or 'CO2 emissions'."
metadata = get_metadata(ts_data, model_data)
region_compare = detect_region_comparison(question, metadata)
if region_compare and variable:
return plot_variable_across_regions(question, model_data, ts_data, variable, region_compare, scenario, start_year, end_year)
# Guard: if variable doesn't exist in loaded data, ask for a valid one
available_vars = {str(r.get('variable', '')).strip() for r in ts_data if r and r.get('variable')}
if variable and variable not in available_vars:
candidates = []
if ranked_vars:
key_terms = {"methane", "ch4", "demand", "electricity", "emission", "emissions", "co2", "capacity",
"solar", "wind", "oil", "gas", "transport", "industry", "buildings", "final", "primary"}
query_terms = {w for w in significant_words if w in key_terms}
ranked_names = [name for name, _, _, _ in ranked_vars if name in available_vars]
if query_terms:
filtered = [n for n in ranked_names if any(t in n.lower() for t in query_terms)]
candidates = filtered[:3]
else:
candidates = ranked_names[:3]
if not candidates:
try:
variable_dict
except NameError:
from pathlib import Path
variable_path = Path('definitions/variable').resolve()
variable_dict = load_all_yaml_files(str(variable_path))
candidates = resolve_natural_language_variable_candidates(question, variable_dict, top_k=3)
if candidates:
sample = ", ".join(candidates)
return (
f"Variable '{variable}' not found in loaded data.\n"
"Which variable should I use?\n"
f"Recommended variables: {sample}\n"
"Reply with the variable you want."
)
return f"Variable '{variable}' not found in loaded data. Try `list variables`."