-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCF.py
1388 lines (1194 loc) · 56.1 KB
/
LCF.py
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 argparse
import time
import pandas as pd
import requests
import folium
from folium.plugins import HeatMap
import os
import json
import logging
from logging.handlers import RotatingFileHandler
import datetime
import sys
from rich.console import Console
from rich.table import Table
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.prompt import Prompt, Confirm
from rich import print as rprint
# Constants
CACHE_TIMEOUT = 43200
MUSHROOM_FILE = 'mushrooms.txt'
DATA_DIR = 'mushroom_data'
API_RATE_LIMIT = 0.5 # seconds between requests
API_BASE_URL = 'https://api.inaturalist.org/v1'
PLACE_IDS = [10] # Oregon
DEFAULT_MAP_CENTER = [44.1, -120.5] # Oregon/Washington center
REPORTS_DIR = 'reports'
QUALITY_GRADES = ["casual", "needs_id", "research"]
# Initialize console
console = Console()
class MushroomObserver:
def __init__(self):
"""Initialize the MushroomObserver class."""
self.setup_logging()
self.setup_directories()
self.mushrooms = self.load_mushrooms()
def setup_logging(self):
"""Configure logging."""
if not os.path.exists('logs'):
os.makedirs('logs')
self.logger = logging.getLogger('mushroom_observer')
self.logger.setLevel(logging.INFO)
handler = RotatingFileHandler(
'logs/mushroom_observer.log',
maxBytes=1024 * 1024,
backupCount=10
)
formatter = logging.Formatter(
'%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'
)
handler.setFormatter(formatter)
self.logger.addHandler(handler)
def setup_directories(self):
"""Create required directories."""
for directory in [DATA_DIR, 'logs', REPORTS_DIR]:
if not os.path.exists(directory):
os.makedirs(directory)
self.logger.info(f"Created directory: {directory}")
def load_mushrooms(self):
"""Load mushrooms from text file."""
mushrooms = {}
if os.path.exists(MUSHROOM_FILE):
with open(MUSHROOM_FILE, 'r') as f:
for line in f:
if line.strip():
try:
name, taxon_id = line.strip().split(',')
mushrooms[name] = int(taxon_id)
except ValueError as e:
self.logger.error(f"Invalid line in mushroom file: {line.strip()} - {e}")
return dict(sorted(mushrooms.items()))
def save_mushrooms(self):
"""Save mushrooms to text file."""
try:
with open(MUSHROOM_FILE, 'w') as f:
for name, taxon_id in sorted(self.mushrooms.items()):
f.write(f"{name},{taxon_id}\n")
return True
except Exception as e:
self.logger.error(f"Error saving mushrooms: {e}")
return False
def view_mushrooms(self):
"""Display list of tracked mushrooms."""
console.clear()
if not self.mushrooms:
rprint("[yellow]No mushrooms in the list![/yellow]")
else:
table = Table(show_header=True, header_style="bold magenta")
table.add_column("Name", style="cyan")
table.add_column("Taxon ID", style="green")
for name, taxon_id in self.mushrooms.items():
table.add_row(name, str(taxon_id))
console.print(table)
input("\nPress Enter to continue...")
def add_mushroom(self):
"""Add a new mushroom to track."""
console.clear()
rprint("[bold]Add New Mushroom[/bold]")
name = Prompt.ask("\nEnter mushroom name")
if name in self.mushrooms:
rprint("[red]This mushroom is already in the list![/red]")
input("\nPress Enter to continue...")
return
try:
taxon_id = int(Prompt.ask("Enter iNaturalist taxon ID"))
self.mushrooms[name] = taxon_id
if self.save_mushrooms():
rprint("[green]Mushroom added successfully![/green]")
rprint("[yellow]\nFetching initial observation data...[/yellow]")
data = self.fetch_observations(taxon_id, name)
if not data.empty:
rprint(f"[green]Successfully loaded {len(data)} observations for {name}[/green]")
else:
rprint("[red]No observations found for this mushroom[/red]")
else:
rprint("[red]Error saving mushroom data![/red]")
except ValueError:
rprint("[red]Invalid taxon ID! Please enter a number.[/red]")
input("\nPress Enter to continue...")
def remove_mushroom(self):
"""Remove a mushroom from tracking."""
console.clear()
if not self.mushrooms:
rprint("[yellow]No mushrooms to remove![/yellow]")
input("\nPress Enter to continue...")
return
rprint("[bold]Remove Mushroom[/bold]\n")
for i, name in enumerate(self.mushrooms.keys(), 1):
rprint(f"{i}. {name}")
try:
choice = int(Prompt.ask("\nEnter number to remove (0 to cancel)"))
if choice == 0:
return
if 1 <= choice <= len(self.mushrooms):
name = list(self.mushrooms.keys())[choice-1]
if Confirm.ask(f"Remove {name}?"):
del self.mushrooms[name]
if self.save_mushrooms():
rprint("[green]Mushroom removed successfully![/green]")
else:
rprint("[red]Error saving mushroom data![/red]")
else:
rprint("[red]Invalid choice![/red]")
except ValueError:
rprint("[red]Invalid input! Please enter a number.[/red]")
input("\nPress Enter to continue...")
def edit_mushroom(self):
"""Edit name or taxon ID for an existing mushroom."""
console.clear()
if not self.mushrooms:
rprint("[yellow]No mushrooms to edit![/yellow]")
input("\nPress Enter to continue...")
return
rprint("[bold]Edit Mushroom[/bold]\n")
for i, (name, taxon_id) in enumerate(self.mushrooms.items(), 1):
rprint(f"{i}. {name} (Taxon ID: {taxon_id})")
try:
choice = int(Prompt.ask("\nEnter number to edit (0 to cancel)"))
if choice == 0:
return
if 1 <= choice <= len(self.mushrooms):
old_name = list(self.mushrooms.keys())[choice-1]
old_taxon_id = self.mushrooms[old_name]
new_name = Prompt.ask("Enter new name (or press Enter to keep current)", default=old_name)
new_taxon_str = Prompt.ask(
"Enter new taxon ID (or press Enter to keep current)",
default=str(old_taxon_id)
)
try:
new_taxon_id = int(new_taxon_str)
if new_name != old_name:
del self.mushrooms[old_name]
old_cache = os.path.join(DATA_DIR, f'taxon_{old_taxon_id}.json')
if os.path.exists(old_cache):
os.rename(old_cache, os.path.join(DATA_DIR, f'taxon_{new_taxon_id}.json'))
self.mushrooms[new_name] = new_taxon_id
if self.save_mushrooms():
rprint("[green]Mushroom updated successfully![/green]")
else:
rprint("[red]Error saving mushroom data![/red]")
except ValueError:
rprint("[red]Invalid taxon ID! Please enter a number.[/red]")
else:
rprint("[red]Invalid choice![/red]")
except ValueError:
rprint("[red]Invalid input! Please enter a number.[/red]")
input("\nPress Enter to continue...")
def validate_observation(self, observation):
"""Validate individual observation data."""
try:
if not all(field in observation for field in ['id', 'observed_on', 'geojson']):
return False
if not observation['observed_on'] or not observation['geojson']:
return False
coords = observation['geojson'].get('coordinates', [])
if len(coords) != 2:
return False
lon, lat = coords
if not (-180 <= lon <= 180 and -90 <= lat <= 90):
return False
return True
except Exception as e:
self.logger.error(f"Error validating observation: {e}")
return False
def fetch_observations(self, taxon_id, mushroom_name=None):
"""Fetch observation data from iNaturalist API."""
all_data = []
try:
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=console
) as progress:
task = progress.add_task(f"Fetching data for {mushroom_name}...", total=None)
# Check cache first
cached_data = self.load_cached_data(taxon_id)
if cached_data:
progress.update(task, description=f"Loaded cached data for {mushroom_name}")
return pd.DataFrame(cached_data)
headers = {
"User-Agent": "MushroomObserver/1.0", # Fixed typo in User-Agent
"Accept": "application/json"
}
try:
for place_id in PLACE_IDS:
for quality_grade in QUALITY_GRADES:
page = 1
while True:
try:
url = f"{API_BASE_URL}/observations"
params = {
"taxon_id": taxon_id,
"place_id": place_id,
"per_page": 200,
"page": page,
"quality_grade": quality_grade,
"photos": "true",
"geo": "true"
}
response = requests.get(url, params=params, headers=headers, timeout=30) # Added timeout
response.raise_for_status()
data = response.json()
results = data.get('results', [])
if not results:
break
for result in results:
result['quality_grade'] = quality_grade
valid_results = [obs for obs in results if self.validate_observation(obs)]
all_data.extend(valid_results)
progress.update(task,
description=f"Loaded {len(all_data)} observations for {mushroom_name} ({quality_grade})")
if len(results) < 200:
break
page += 1
time.sleep(API_RATE_LIMIT)
except requests.RequestException as e:
self.logger.error(f"Error fetching page {page}: {e}")
break
except KeyboardInterrupt:
rprint("\n[yellow]Data fetch interrupted by user[/yellow]")
break
if all_data:
self.save_cached_data(taxon_id, all_data)
return pd.DataFrame(all_data)
except Exception as e:
self.logger.error(f"Error fetching observations: {e}")
return pd.DataFrame()
except KeyboardInterrupt:
rprint("\n[yellow]Operation cancelled by user[/yellow]")
return pd.DataFrame(all_data) if all_data else pd.DataFrame()
except Exception as e:
self.logger.error(f"Unexpected error in fetch_observations: {e}")
return pd.DataFrame()
def fetch_observations_since(self, taxon_id, mushroom_name, since_date=None):
"""Fetch only new observations since the given date."""
all_data = []
headers = {
"User-Agent": "MushroomObserver/1.0", # Fixed typo
"Accept": "application/json"
}
try:
# Convert since_date to proper format if it exists
if since_date:
# Ensure we're using a datetime object
if isinstance(since_date, str):
since_date = pd.to_datetime(since_date)
# Format for iNaturalist API
since_date = since_date.strftime('%Y-%m-%d')
for place_id in PLACE_IDS:
for quality_grade in QUALITY_GRADES:
page = 1
while True:
try:
url = f"{API_BASE_URL}/observations"
params = {
"taxon_id": taxon_id,
"place_id": place_id,
"per_page": 200,
"page": page,
"quality_grade": quality_grade,
"photos": "true",
"geo": "true",
"order_by": "observed_on",
"order": "desc" # Get newest first
}
if since_date:
params["d1"] = since_date # Date must be in YYYY-MM-DD format
self.logger.info(f"Fetching observations since {since_date}")
response = requests.get(url, params=params, headers=headers, timeout=30)
response.raise_for_status()
data = response.json()
results = data.get('results', [])
if not results:
break
# Add quality grade to each observation
for result in results:
result['quality_grade'] = quality_grade
valid_results = [obs for obs in results if self.validate_observation(obs)]
# Log the number of new observations found
if valid_results:
self.logger.info(f"Found {len(valid_results)} new observations for {mushroom_name}")
all_data.extend(valid_results)
if len(results) < 200:
break
page += 1
time.sleep(API_RATE_LIMIT)
except requests.RequestException as e:
self.logger.error(f"Error fetching page {page}: {e}")
break
return all_data
except Exception as e:
self.logger.error(f"Error fetching new observations: {e}")
return []
def load_cached_data(self, taxon_id):
"""Load cached observation data."""
cache_file = os.path.join(DATA_DIR, f'taxon_{taxon_id}.json')
if os.path.exists(cache_file):
with open(cache_file, 'r') as f:
return json.load(f)
return None
def save_cached_data(self, taxon_id, data):
"""Save observation data to cache."""
cache_file = os.path.join(DATA_DIR, f'taxon_{taxon_id}.json')
with open(cache_file, 'w') as f:
json.dump(data, f)
def calculate_monthly_totals(self, data):
"""Calculate monthly observation totals with historical breakdowns."""
if 'observed_on' not in data.columns or data.empty:
return pd.DataFrame(), pd.DataFrame()
data['observed_on'] = pd.to_datetime(data['observed_on'])
data['year'] = data['observed_on'].dt.year
monthly_counts = data.groupby([
data['observed_on'].dt.month,
'quality_grade'
]).size().unstack(fill_value=0)
historical_counts = data.groupby([
data['observed_on'].dt.year,
data['observed_on'].dt.month,
'quality_grade'
]).size().unstack(fill_value=0)
monthly_counts['Total'] = monthly_counts.sum(axis=1)
historical_counts['Total'] = historical_counts.sum(axis=1)
month_names = {
1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun',
7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec'
}
monthly_counts.index = monthly_counts.index.map(month_names)
historical_counts.index = historical_counts.index.map(
lambda x: f"{month_names[x[1]]} {x[0]}"
)
return monthly_counts, historical_counts
def get_seasonal_predictions(self, all_mushroom_data):
"""Calculate seasonal predictions for mushroom occurrence."""
current_date = datetime.datetime.now()
current_month = current_date.month
last_month = (current_month - 1) if current_month > 1 else 12
next_month = (current_month + 1) if current_month < 12 else 1
predictions = {}
for name, data in all_mushroom_data.items():
if data.empty or 'observed_on' not in data.columns:
continue
data['observed_on'] = pd.to_datetime(data['observed_on'])
data['month'] = data['observed_on'].dt.month
data['year'] = data['observed_on'].dt.year
# Calculate monthly averages
monthly_counts = data.groupby(['month']).size()
yearly_counts = data.groupby(['year', 'month']).size()
yearly_averages = yearly_counts.groupby('month').mean()
# Calculate totals by month
monthly_totals = monthly_counts.to_dict()
predictions[name] = {
'last_month': {
'month': last_month,
'avg': yearly_averages.get(last_month, 0),
'total': int(monthly_totals.get(last_month, 0))
},
'current_month': {
'month': current_month,
'avg': yearly_averages.get(current_month, 0),
'total': int(monthly_totals.get(current_month, 0))
},
'next_month': {
'month': next_month,
'avg': yearly_averages.get(next_month, 0),
'total': int(monthly_totals.get(next_month, 0))
}
}
return predictions
def manual_update_mushroom(self):
"""Manually update data for a specific mushroom."""
console.clear()
if not self.mushrooms:
rprint("[yellow]No mushrooms to update![/yellow]")
input("\nPress Enter to continue...")
return
rprint("[bold]Manual Mushroom Update[/bold]\n")
for i, name in enumerate(self.mushrooms.keys(), 1):
rprint(f"{i}. {name}")
try:
choice = int(Prompt.ask("\nEnter number to update (0 to cancel)"))
if choice == 0:
return
if 1 <= choice <= len(self.mushrooms):
name = list(self.mushrooms.keys())[choice-1]
taxon_id = self.mushrooms[name]
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=console
) as progress:
task = progress.add_task(f"Fetching new data for {name}...")
cache_file = os.path.join(DATA_DIR, f'taxon_{taxon_id}.json')
backup_file = os.path.join(DATA_DIR, f'taxon_{taxon_id}.json.bak')
if os.path.exists(cache_file):
os.rename(cache_file, backup_file)
try:
data = self.fetch_observations(taxon_id, name)
if not data.empty:
rprint(f"[green]Successfully updated {name} with {len(data)} observations[/green]")
if os.path.exists(backup_file):
os.remove(backup_file)
else:
rprint("[red]No data retrieved. Restoring previous data...[/red]")
if os.path.exists(backup_file):
os.rename(backup_file, cache_file)
except Exception as e:
rprint(f"[red]Error updating data: {e}[/red]")
if os.path.exists(backup_file):
os.rename(backup_file, cache_file)
else:
rprint("[red]Invalid choice![/red]")
except ValueError:
rprint("[red]Invalid input! Please enter a number.[/red]")
input("\nPress Enter to continue...")
def generate_mushroom_report(self):
"""Generate report for a single mushroom."""
console.clear()
if not self.mushrooms:
rprint("[yellow]No mushrooms to generate report for![/yellow]")
input("\nPress Enter to continue...")
return
rprint("[bold]Generate Mushroom Report[/bold]\n")
for i, name in enumerate(self.mushrooms.keys(), 1):
rprint(f"{i}. {name}")
try:
choice = int(Prompt.ask("\nEnter number to generate report (0 to cancel)"))
if choice == 0:
return
if 1 <= choice <= len(self.mushrooms):
name = list(self.mushrooms.keys())[choice-1]
taxon_id = self.mushrooms[name]
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}")
) as progress:
task = progress.add_task(f"Generating report for {name}...")
data = self.fetch_observations(taxon_id, name)
if not data.empty:
report_path = self.generate_report(data, name)
rprint(f"[green]Report generated: {report_path}[/green]")
else:
rprint("[red]No data available for this mushroom![/red]")
else:
rprint("[red]Invalid choice![/red]")
except ValueError:
rprint("[red]Invalid input! Please enter a number.[/red]")
input("\nPress Enter to continue...")
def generate_report(self, data, mushroom_name):
"""Generate HTML report with visualizations."""
timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
report_dir = os.path.join(REPORTS_DIR, f"{mushroom_name}_{timestamp}")
os.makedirs(report_dir, exist_ok=True)
m = folium.Map(location=DEFAULT_MAP_CENTER,
zoom_start=7,
width='100%',
height='100%')
if not data.empty:
locations = []
for _, row in data.iterrows():
if pd.notnull(row['geojson']):
coords = row['geojson']['coordinates']
locations.append([coords[1], coords[0]]) # Folium uses [lat, lon]
if locations:
HeatMap(locations).add_to(m)
monthly_data, historical_data = self.calculate_monthly_totals(data)
predictions = self.get_seasonal_predictions({mushroom_name: data})
report_path = os.path.join(report_dir, 'report.html')
self.create_html_report(
report_path,
mushroom_name,
m,
monthly_data,
historical_data,
predictions.get(mushroom_name, {}),
data
)
return report_path
def create_consolidated_html_report(self, filepath, all_mushroom_data, consolidated_predictions):
"""Create consolidated HTML report for all mushrooms."""
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
all_stats = {}
overall_summary = {
'total_observations': 0,
'most_active_month': None,
'most_active_year': None,
'quality_distribution': {},
'yearly_trends': {}
}
for name, data in all_mushroom_data.items():
if not data.empty:
# Create heatmap for this mushroom
m = folium.Map(location=DEFAULT_MAP_CENTER,
zoom_start=7,
width='100%',
height='100%')
locations = []
for _, row in data.iterrows():
if pd.notnull(row['geojson']):
coords = row['geojson']['coordinates']
locations.append([coords[1], coords[0]]) # Folium uses [lat, lon]
if locations:
HeatMap(locations).add_to(m)
# Convert to datetime for analysis
data['observed_on'] = pd.to_datetime(data['observed_on'])
# Calculate statistics
monthly_data, historical_data = self.calculate_monthly_totals(data)
yearly_observations = data.groupby(data['observed_on'].dt.year).size()
monthly_breakdown = data.groupby(data['observed_on'].dt.month).size()
quality_grades = data['quality_grade'].value_counts()
# Calculate year-over-year growth
yearly_growth = yearly_observations.pct_change() * 100
# Find peak months and years
peak_month = monthly_breakdown.idxmax()
peak_year = yearly_observations.idxmax()
# Calculate relative frequency compared to other mushrooms
total_obs = len(data)
overall_summary['total_observations'] += total_obs
# Update quality grade distribution
for grade, count in quality_grades.items():
overall_summary['quality_distribution'][grade] = \
overall_summary['quality_distribution'].get(grade, 0) + count
# Track yearly trends
for year, count in yearly_observations.items():
overall_summary['yearly_trends'][year] = \
overall_summary['yearly_trends'].get(year, 0) + count
all_stats[name] = {
'total_observations': total_obs,
'monthly_data': monthly_data,
'historical_data': historical_data,
'predictions': consolidated_predictions.get(name, {}),
'heatmap': m._repr_html_(),
'peak_month': peak_month,
'peak_year': peak_year,
'yearly_growth': yearly_growth,
'quality_breakdown': quality_grades,
'yearly_observations': yearly_observations
}
# Calculate overall trends
overall_summary['most_active_year'] = max(overall_summary['yearly_trends'].items(),
key=lambda x: x[1])[0]
# Generate HTML content
html_content = f"""
<!DOCTYPE html>
<html>
<head>
<title>Enhanced Mushroom Report</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<style>
body {{ padding: 20px; background-color: #f5f5f5; }}
.container {{
max-width: 1200px;
background-color: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 0 20px rgba(0,0,0,0.1);
}}
h1, h2, h3 {{
color: #2c3e50;
margin-bottom: 20px;
padding-bottom: 10px;
border-bottom: 2px solid #eee;
}}
.mushroom-section {{
margin-bottom: 40px;
padding: 20px;
border: 1px solid #ddd;
border-radius: 5px;
background-color: white;
}}
.map-container {{
height: 600px;
width: 100%;
border: 1px solid #ddd;
border-radius: 5px;
overflow: hidden;
margin: 20px 0;
}}
.summary-card {{
background-color: #f8f9fa;
padding: 15px;
border-radius: 8px;
margin-bottom: 20px;
}}
.trend-indicator {{
font-weight: bold;
padding: 2px 6px;
border-radius: 3px;
}}
.trend-up {{ color: #28a745; }}
.trend-down {{ color: #dc3545; }}
.leaflet-container {{
height: 100% !important;
width: 100% !important;
position: relative !important;
}}
</style>
</head>
<body>
<div class="container">
<h1>Consolidated Mushroom Report</h1>
<p class="lead">Report generated on: {timestamp}</p>
<div class="row mt-4">
<div class="col-12">
<h2>Overall Summary</h2>
<div class="summary-card">
<div class="row">
<div class="col-md-4">
<h5>Total Observations</h5>
<p class="h3">{overall_summary['total_observations']:,}</p>
</div>
<div class="col-md-4">
<h5>Most Active Year</h5>
<p class="h3">{overall_summary['most_active_year']}</p>
</div>
<div class="col-md-4">
<h5>Quality Distribution</h5>
{self._create_quality_distribution_html(overall_summary['quality_distribution'])}
</div>
</div>
</div>
</div>
</div>
{self._create_mushroom_sections(all_stats)}
</div>
<script>
// Force Leaflet maps to update their size
setTimeout(function() {{
document.querySelectorAll('.leaflet-container').forEach(function(map) {{
map._leaflet_map && map._leaflet_map.invalidateSize();
}});
}}, 100);
</script>
</body>
</html>
"""
with open(filepath, 'w', encoding='utf-8') as f:
f.write(html_content)
def _create_mushroom_sections(self, all_stats):
"""Create HTML for all mushroom sections."""
sections = []
for name, stats in all_stats.items():
try:
yearly_trend = self._create_yearly_trend_chart(stats['yearly_observations'], name)
except Exception as e:
self.logger.error(f"Error creating trend chart for {name}: {e}")
yearly_trend = "<div>Error generating trend chart</div>"
section = f"""
<div class="mushroom-section">
<h2>{name}</h2>
<div class="row">
<div class="col-md-4">
<div class="summary-card">
<h5>Quick Stats</h5>
<p>Peak Month: {self._get_month_name(stats['peak_month'])}<br>
Peak Year: {stats['peak_year']}<br>
Latest Growth: {stats['yearly_growth'].iloc[-1]:.1f}%</p>
</div>
</div>
<div class="col-md-8">
<div class="summary-card">
<h5>Yearly Trend</h5>
{yearly_trend}
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<h3>Observation Heatmap</h3>
<div class="map-container">
{stats['heatmap']}
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<h3>Monthly Patterns</h3>
<div class="table-responsive">
{stats['monthly_data'].to_html(classes='table table-striped')}
</div>
</div>
</div>
<div class="row mt-4">
<div class="col-12">
<h3>Seasonal Predictions</h3>
<div class="card-deck">
{self._create_prediction_cards(stats['predictions'])}
</div>
</div>
</div>
</div>"""
sections.append(section)
return '\n'.join(sections)
def _get_month_name(self, month_num):
"""Convert month number to name."""
months = {
1: 'January', 2: 'February', 3: 'March',
4: 'April', 5: 'May', 6: 'June',
7: 'July', 8: 'August', 9: 'September',
10: 'October', 11: 'November', 12: 'December'
}
return months.get(month_num, 'Unknown')
def _create_quality_distribution_html(self, distribution):
"""Create HTML for quality grade distribution."""
total = sum(distribution.values())
html = '<div class="quality-grades">'
for grade, count in distribution.items():
percentage = (count / total) * 100
html += f'<div>{grade}: {percentage:.1f}%</div>'
html += '</div>'
return html
def _create_yearly_trend_chart(self, yearly_data, name):
"""Create a yearly trend visualization."""
# Convert data to lists for JSON serialization
years = list(yearly_data.index)
counts = list(yearly_data.values)
return f"""
<div id="trend-chart-{name.replace(' ', '-')}" style="height: 200px;"></div>
<script>
var chartData = {{
x: {years},
y: {counts},
type: 'scatter',
mode: 'lines+markers',
name: 'Observations'
}};
var chartLayout = {{
margin: {{
t: 20,
r: 20,
b: 40,
l: 40
}},
xaxis: {{
title: 'Year'
}},
yaxis: {{
title: 'Observations'
}}
}};
Plotly.newPlot('trend-chart-{name.replace(' ', '-')}', [chartData], chartLayout);
</script>
"""
def _create_prediction_cards(self, predictions):
"""Helper method to create prediction cards HTML."""
month_names = {
1: 'January', 2: 'February', 3: 'March', 4: 'April',
5: 'May', 6: 'June', 7: 'July', 8: 'August',
9: 'September', 10: 'October', 11: 'November', 12: 'December'
}
cards_html = ""
for period in ['last_month', 'current_month', 'next_month']:
if period in predictions:
pred = predictions[period]
month_num = pred.get('month', 1)
month_name = month_names.get(month_num, 'Unknown')
# Get raw values directly
total = pred.get('total', 0)
avg = pred.get('avg', 0)
cards_html += f"""
<div class="card">
<div class="card-body">
<h5 class="card-title">{month_name}</h5>
<p class="card-text">
Historical Average: {avg:.1f}<br>
All-time Total: {total:,}
</p>
</div>
</div>"""
return cards_html
def generate_consolidated_report(self):
"""Generate a consolidated report for all mushrooms."""
console.clear()
if not self.mushrooms:
rprint("[yellow]No mushrooms to generate report for![/yellow]")
input("\nPress Enter to continue...")
return
rprint("[bold]Generating Consolidated Report[/bold]\n")
timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
report_dir = os.path.join(REPORTS_DIR, f"consolidated_{timestamp}")
os.makedirs(report_dir, exist_ok=True)
all_mushroom_data = {}
consolidated_predictions = {}
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}")
) as progress:
task = progress.add_task("Collecting mushroom data...", total=len(self.mushrooms))
for name, taxon_id in self.mushrooms.items():
progress.update(task, description=f"Loading data for {name}")
data = self.fetch_observations(taxon_id, name)
if not data.empty:
all_mushroom_data[name] = data
progress.advance(task)
consolidated_predictions = self.get_seasonal_predictions(all_mushroom_data)
if not all_mushroom_data:
rprint("[red]No data available for any mushrooms![/red]")
input("\nPress Enter to continue...")
return
report_path = os.path.join(report_dir, 'consolidated_report.html')
self.create_consolidated_html_report(report_path, all_mushroom_data, consolidated_predictions)
rprint(f"[green]Consolidated report generated: {report_path}[/green]")
input("\nPress Enter to continue...")
def create_html_report(self, filepath, mushroom_name, heatmap, monthly_data, historical_data, seasonal_pred, full_data):
"""Create enhanced HTML report with monthly totals and predictions."""
quality_dist = full_data['quality_grade'].value_counts()
monthly_totals = monthly_data.copy()
grand_total = monthly_totals['Total'].sum()