Added error handling for: Calls not including winter months; Calls with less than 5 months
Browse files
app.py
CHANGED
|
@@ -533,6 +533,8 @@ def run_app():
|
|
| 533 |
start_date = st.date_input("Start Date")
|
| 534 |
end_date = st.date_input("End Date")
|
| 535 |
past_date = st.date_input("Cutoff Date")
|
|
|
|
|
|
|
| 536 |
current_date = datetime.datetime.now()
|
| 537 |
|
| 538 |
with st.form("nucmonitor_form"):
|
|
@@ -545,6 +547,7 @@ def run_app():
|
|
| 545 |
st.write("Data received from Flask:")
|
| 546 |
df_nucmonitor = get_nucmonitor_data(start_date, end_date, current_date)
|
| 547 |
df_photo_date = get_nucmonitor_data(start_date, end_date, past_date)
|
|
|
|
| 548 |
current_date_str = str(current_date.strftime('%Y-%m-%d'))
|
| 549 |
past_date_str = str(past_date.strftime('%Y-%m-%d'))
|
| 550 |
st.write("Nucmonitor")
|
|
@@ -577,81 +580,185 @@ def run_app():
|
|
| 577 |
|
| 578 |
st.write(df_photo_date_2)
|
| 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 |
# # Set Nucmonitor as a dotted line until the current date
|
| 657 |
|
|
|
|
| 533 |
start_date = st.date_input("Start Date")
|
| 534 |
end_date = st.date_input("End Date")
|
| 535 |
past_date = st.date_input("Cutoff Date")
|
| 536 |
+
# winter_date = st.date_input("Winter Cutoff Date")
|
| 537 |
+
|
| 538 |
current_date = datetime.datetime.now()
|
| 539 |
|
| 540 |
with st.form("nucmonitor_form"):
|
|
|
|
| 547 |
st.write("Data received from Flask:")
|
| 548 |
df_nucmonitor = get_nucmonitor_data(start_date, end_date, current_date)
|
| 549 |
df_photo_date = get_nucmonitor_data(start_date, end_date, past_date)
|
| 550 |
+
# df_winter_date = get_nucmonitor_data(start_date, end_date, winter_date)
|
| 551 |
current_date_str = str(current_date.strftime('%Y-%m-%d'))
|
| 552 |
past_date_str = str(past_date.strftime('%Y-%m-%d'))
|
| 553 |
st.write("Nucmonitor")
|
|
|
|
| 580 |
|
| 581 |
st.write(df_photo_date_2)
|
| 582 |
|
| 583 |
+
# --------------------------------- AVERAGE EXPECTED AVAILABILITY M-1 M M+1 M+2 PIPELINE --------------------------------- #
|
| 584 |
+
|
| 585 |
+
# Create a Table that displays the forecast of each dataframe total for two months before date and two months after
|
| 586 |
+
# Filter dates for two months before and after the current date
|
| 587 |
+
# Define date ranges
|
| 588 |
+
two_months_before = (current_date - pd.DateOffset(months=2)).strftime('%Y-%m')
|
| 589 |
+
one_month_before = (current_date - pd.DateOffset(months=1)).strftime('%Y-%m')
|
| 590 |
+
one_month_after = (current_date + pd.DateOffset(months=1)).strftime('%Y-%m')
|
| 591 |
+
two_months_after = (current_date + pd.DateOffset(months=2)).strftime('%Y-%m')
|
| 592 |
+
|
| 593 |
+
# Assuming df is the DataFrame containing the date index and the 'Total' column
|
| 594 |
+
|
| 595 |
+
# # Convert the index to datetime if it's not already
|
| 596 |
+
# df_nucmonitor_2.index = pd.to_datetime(df_nucmonitor_2.index)
|
| 597 |
+
# df_photo_date_2.index = pd.to_datetime(df_photo_date_2.index)
|
| 598 |
+
|
| 599 |
+
# # Calculate monthly averages with date in yyyy-mm format
|
| 600 |
+
# monthly_average_nucmonitor = df_nucmonitor_2.resample('M').mean()
|
| 601 |
+
# monthly_average_photo_date = df_photo_date_2.resample('M').mean()
|
| 602 |
+
|
| 603 |
+
# Convert the index to datetime if it's not already
|
| 604 |
+
df_nucmonitor_2.index = pd.to_datetime(df_nucmonitor_2.index)
|
| 605 |
+
df_photo_date_2.index = pd.to_datetime(df_photo_date_2.index)
|
| 606 |
+
|
| 607 |
+
# Calculate monthly averages with date in yyyy-mm format
|
| 608 |
+
monthly_average_nucmonitor = df_nucmonitor_2.resample('M').mean()
|
| 609 |
+
monthly_average_nucmonitor.index = monthly_average_nucmonitor.index.strftime('%Y-%m')
|
| 610 |
+
|
| 611 |
+
monthly_average_photo_date = df_photo_date_2.resample('M').mean()
|
| 612 |
+
monthly_average_photo_date.index = monthly_average_photo_date.index.strftime('%Y-%m')
|
| 613 |
+
|
| 614 |
+
|
| 615 |
+
print(monthly_average_nucmonitor)
|
| 616 |
+
print(monthly_average_nucmonitor.index)
|
| 617 |
+
print(len(monthly_average_nucmonitor.index) < 5)
|
| 618 |
+
if (len(monthly_average_nucmonitor.index) < 5) or (two_months_before not in monthly_average_nucmonitor.index or two_months_after not in monthly_average_nucmonitor.index):
|
| 619 |
+
df_display_normal_bool = False
|
| 620 |
+
|
| 621 |
+
else:
|
| 622 |
+
print(two_months_before, one_month_before, current_date.strftime('%Y-%m'), one_month_after, two_months_after)
|
| 623 |
+
# Filter DataFrames based on date ranges
|
| 624 |
+
df_nucmonitor_filtered = monthly_average_nucmonitor[
|
| 625 |
+
(monthly_average_nucmonitor.index == two_months_before) |
|
| 626 |
+
(monthly_average_nucmonitor.index == one_month_before) |
|
| 627 |
+
(monthly_average_nucmonitor.index == current_date.strftime('%Y-%m')) |
|
| 628 |
+
(monthly_average_nucmonitor.index == one_month_after) |
|
| 629 |
+
(monthly_average_nucmonitor.index == two_months_after)
|
| 630 |
+
]
|
| 631 |
+
|
| 632 |
+
df_photo_date_filtered = monthly_average_photo_date[
|
| 633 |
+
(monthly_average_photo_date.index == two_months_before) |
|
| 634 |
+
(monthly_average_photo_date.index == one_month_before) |
|
| 635 |
+
(monthly_average_photo_date.index == current_date.strftime('%Y-%m')) |
|
| 636 |
+
(monthly_average_photo_date.index == one_month_after) |
|
| 637 |
+
(monthly_average_photo_date.index == two_months_after)
|
| 638 |
+
]
|
| 639 |
+
|
| 640 |
+
# Display the filtered DataFrames
|
| 641 |
+
st.write(f"Forecast update {current_date_str}")
|
| 642 |
+
st.write(df_nucmonitor_filtered)
|
| 643 |
+
st.write(f"Forecast update {past_date_str}")
|
| 644 |
+
st.write(df_photo_date_filtered)
|
| 645 |
+
|
| 646 |
+
current_forecast_update = df_nucmonitor_filtered.tolist()
|
| 647 |
+
past_forecast_update = df_photo_date_filtered.tolist()
|
| 648 |
+
delta = [current - past for current, past in zip(current_forecast_update, past_forecast_update)]
|
| 649 |
+
|
| 650 |
+
print('Dates:', [two_months_before, one_month_before, current_date.strftime('%Y-%m'), one_month_after, two_months_after])
|
| 651 |
+
print(f"Forecast update {current_date_str}", current_forecast_update)
|
| 652 |
+
print(f"Forecast update {past_date_str}", past_forecast_update,)
|
| 653 |
+
print('Delta', delta)
|
| 654 |
+
|
| 655 |
+
# Create a DataFrame for display
|
| 656 |
+
data_avg_expected_normal = {
|
| 657 |
+
'Dates': [two_months_before, one_month_before, current_date.strftime('%Y-%m'), one_month_after, two_months_after],
|
| 658 |
+
f"Forecast update {current_date_str}": current_forecast_update,
|
| 659 |
+
f"Forecast update {past_date_str}": past_forecast_update,
|
| 660 |
+
'Delta': delta
|
| 661 |
+
}
|
| 662 |
+
df_display_normal_bool = True
|
| 663 |
+
|
| 664 |
+
# --------------------------------- AVERAGE EXPECTED AVAILABILITY M-1 M M+1 M+2 PIPELINE --------------------------------- #
|
| 665 |
+
|
| 666 |
+
# --------------------------------- AVERAGE EXPECTED AVAILABILITY WINTER PIPELINE --------------------------------- #
|
| 667 |
+
# Create a Table that displays the forecast of each dataframe for the Winter months (Nov, Dec, Jan, Feb, Mar)
|
| 668 |
+
|
| 669 |
+
# Create a table that gets the forecast for winter. This involves creating a new dataframe with
|
| 670 |
+
# only the winter months with the total of each day, and another dataframe with the average of each month. Each month
|
| 671 |
+
# included will only be 20xx-11, 12, and 20xx+1-01, 02, 03
|
| 672 |
|
| 673 |
+
# Define date ranges for winter months
|
| 674 |
+
# winter_start_date = current_date.replace(month=11, day=1)
|
| 675 |
+
# winter_end_date = (current_date.replace(year=current_date.year+1, month=3, day=31))
|
| 676 |
+
winter_start = f"{current_date.year}-11"
|
| 677 |
+
winter_end = f"{current_date.year+1}-03"
|
| 678 |
+
winter_start_str = str(winter_start)
|
| 679 |
+
winter_end_str = str(winter_end)
|
| 680 |
+
print("winter_start_str", winter_start)
|
| 681 |
+
print("winter_end_str", winter_end)
|
| 682 |
+
print("monthly_average_nucmonitor.index", monthly_average_nucmonitor.index)
|
| 683 |
+
print(monthly_average_nucmonitor.index == winter_start)
|
| 684 |
+
print(monthly_average_nucmonitor.index == winter_end)
|
| 685 |
+
if monthly_average_nucmonitor.index.any() != winter_start or monthly_average_nucmonitor.index.any() != winter_end:
|
| 686 |
+
df_display_winter_bool = False
|
| 687 |
+
|
| 688 |
+
else:
|
| 689 |
+
# Filter DataFrames based on winter date range
|
| 690 |
+
df_nucmonitor_winter = monthly_average_nucmonitor[(monthly_average_nucmonitor.index >= winter_start_str) & (monthly_average_nucmonitor.index <= winter_end_str)]
|
| 691 |
+
|
| 692 |
+
df_photo_date_winter = monthly_average_photo_date[(monthly_average_photo_date.index >= winter_start_str) & (monthly_average_photo_date.index <= winter_end_str)]
|
| 693 |
+
|
| 694 |
+
# Display the forecast DataFrames for winter
|
| 695 |
+
st.title("Forecast for Winter Months")
|
| 696 |
+
st.write(f"Forecast for {current_date.year}-{current_date.year+1} (Nov, Dec, Jan, Feb, Mar)")
|
| 697 |
+
st.write("Nucmonitor Forecast:")
|
| 698 |
+
st.write(df_nucmonitor_winter)
|
| 699 |
+
st.write("Photo Date Forecast:")
|
| 700 |
+
st.write(df_photo_date_winter)
|
| 701 |
+
|
| 702 |
+
current_winter_forecast_update = df_nucmonitor_winter.tolist()
|
| 703 |
+
past_winter_forecast_update = df_photo_date_winter.tolist()
|
| 704 |
+
winter_delta = [current - past for current, past in zip(current_winter_forecast_update, past_winter_forecast_update)]
|
| 705 |
+
print("current_winter_forecast_update:", current_winter_forecast_update)
|
| 706 |
+
print("past_winter_forecast_update:", past_winter_forecast_update)
|
| 707 |
+
|
| 708 |
+
# Create a DataFrame for display
|
| 709 |
+
data_avg_expected_winter = {
|
| 710 |
+
'Dates': [f'Nov-{current_date.year}', f'Dec-{current_date.year}', f'Jan-{current_date.year+1}', f'Feb-{current_date.year+1}', f'Mar-{current_date.year+1}'],
|
| 711 |
+
f"Forecast update {current_date_str}": current_winter_forecast_update,
|
| 712 |
+
f"Forecast update {past_date_str}": past_winter_forecast_update,
|
| 713 |
+
'Delta': winter_delta
|
| 714 |
+
}
|
| 715 |
+
print(data_avg_expected_winter)
|
| 716 |
+
df_display_winter_bool = True
|
| 717 |
+
|
| 718 |
+
# --------------------------------- AVERAGE EXPECTED AVAILABILITY WINTER PIPELINE --------------------------------- #
|
| 719 |
|
| 720 |
+
# --------------------------------- VISUALIZE --------------------------------- #
|
| 721 |
+
if df_display_normal_bool:
|
| 722 |
+
df_display_normal = pd.DataFrame(data_avg_expected_normal)
|
| 723 |
+
# Display the DataFrame as a horizontal table
|
| 724 |
+
st.write("Table 1. Average expected availability on the French nuclear fleet (MW) - M-1, M, M+1, M+2, M+3")
|
| 725 |
+
st.table(df_display_normal)
|
| 726 |
+
|
| 727 |
+
if df_display_winter_bool:
|
| 728 |
+
df_display_winter = pd.DataFrame(data_avg_expected_winter)
|
| 729 |
+
st.write(f"Table 2. Average expected availability on the French nuclear fleet (MW) - Winter {winter_start}/{winter_end}")
|
| 730 |
+
st.table(df_display_winter)
|
| 731 |
+
|
| 732 |
+
# Line charts of the forecasts (need to combine them so they appear in the same chart)
|
| 733 |
+
st.write("Current forecast")
|
| 734 |
+
st.line_chart(df_nucmonitor_2)
|
| 735 |
+
|
| 736 |
+
st.write("Previous forecast")
|
| 737 |
+
st.line_chart(df_photo_date_2)
|
| 738 |
+
# Create a new dataframe out of df_nucmonitor_2 call real_forecast that contains df_nucmonitor_2 up until current_date
|
| 739 |
+
|
| 740 |
+
# Slice the DataFrame to include data up until current_date
|
| 741 |
+
real_forecast = df_nucmonitor_2.loc[df_nucmonitor_2.index <= current_date_str]
|
| 742 |
+
|
| 743 |
+
# Winter forecast still not the correct one, this is just a placeholder
|
| 744 |
+
# winter_forecast = df_nucmonitor_2.loc[(df_nucmonitor_2.index >= winter_start_date) & (df_nucmonitor_2.index <= winter_end_date)]
|
| 745 |
+
|
| 746 |
+
# Optionally, if you want to reset the index
|
| 747 |
+
# real_forecast = real_forecast.reset_index()
|
| 748 |
+
print(real_forecast)
|
| 749 |
+
st.write("Real forecast")
|
| 750 |
+
st.line_chart(real_forecast)
|
| 751 |
+
|
| 752 |
+
# Combine dataframes
|
| 753 |
+
# combined_df = pd.concat([df_nucmonitor_2, df_photo_date_2, real_forecast, winter_forecast], axis=1)
|
| 754 |
+
combined_df = pd.concat([df_nucmonitor_2, df_photo_date_2, real_forecast], axis=1)
|
| 755 |
+
|
| 756 |
+
# combined_df.columns = [f'Forecast {current_date_str}', f'Forecast {past_date_str}', 'Real Forecast', f'Winter forecast {winter_start}/{winter_end}']
|
| 757 |
+
combined_df.columns = [f'Forecast {current_date_str}', f'Forecast {past_date_str}', 'Real Forecast']
|
| 758 |
+
|
| 759 |
+
print(combined_df)
|
| 760 |
+
st.write(f"Graph 1. {start_date} to {end_date}")
|
| 761 |
+
st.line_chart(combined_df)
|
| 762 |
|
| 763 |
# # Set Nucmonitor as a dotted line until the current date
|
| 764 |
|