Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -454,8 +454,8 @@ def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id,
|
|
454 |
agg_dict[original_lat_col] = 'first'
|
455 |
agg_dict[original_lon_col] = 'first'
|
456 |
aggregated_output = result_df.groupby('Location Name').agg(agg_dict).reset_index()
|
457 |
-
aggregated_output['Date Range'] = aggregated_output['Start Date'] + " to " + aggregated_output['End Date']
|
458 |
-
aggregated_output.rename(columns={'Calculated Value': 'Aggregated Value'}, inplace=True)
|
459 |
return aggregated_output.to_dict(orient='records'), processing_time
|
460 |
else:
|
461 |
return result_df.to_dict(orient='records'), processing_time
|
@@ -745,6 +745,87 @@ if file_upload is not None:
|
|
745 |
st.write("Map of Uploaded Polygons:")
|
746 |
m.to_streamlit()
|
747 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
748 |
if st.button(f"Calculate {custom_formula}"):
|
749 |
if not locations_df.empty:
|
750 |
with st.spinner("Processing Data..."):
|
@@ -769,6 +850,10 @@ if st.button(f"Calculate {custom_formula}"):
|
|
769 |
result_df = pd.DataFrame(results)
|
770 |
st.write(f"Processed Results Table ({aggregation_period}) for Formula: {custom_formula}")
|
771 |
st.dataframe(result_df)
|
|
|
|
|
|
|
|
|
772 |
filename = f"{main_selection}_{dataset_id}_{start_date.strftime('%Y%m%d')}_{end_date.strftime('%Y%m%d')}_{aggregation_period.lower()}.csv"
|
773 |
st.download_button(
|
774 |
label="Download results as CSV",
|
@@ -781,6 +866,16 @@ if st.button(f"Calculate {custom_formula}"):
|
|
781 |
# Graph Visualization Section
|
782 |
st.markdown("<h5>Graph Visualization</h5>", unsafe_allow_html=True)
|
783 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
784 |
# Dynamically identify the time column
|
785 |
if aggregation_period.lower() == 'custom (start date to end date)':
|
786 |
x_column = 'Date Range'
|
@@ -793,29 +888,41 @@ if st.button(f"Calculate {custom_formula}"):
|
|
793 |
elif 'Year' in result_df.columns:
|
794 |
x_column = 'Year'
|
795 |
else:
|
796 |
-
st.warning("No valid time column found for plotting.")
|
797 |
st.stop()
|
798 |
|
799 |
-
|
|
|
|
|
|
|
800 |
|
801 |
# Line Chart
|
802 |
-
|
803 |
-
|
|
|
|
|
|
|
804 |
|
805 |
# Bar Chart
|
806 |
-
|
807 |
-
|
|
|
|
|
|
|
808 |
|
809 |
# Advanced Plot (Plotly)
|
810 |
-
|
811 |
-
|
812 |
-
|
813 |
-
|
814 |
-
|
815 |
-
|
816 |
-
|
817 |
-
|
818 |
-
|
|
|
|
|
|
|
819 |
|
820 |
else:
|
821 |
st.warning("No results were generated. Check your inputs or formula.")
|
|
|
454 |
agg_dict[original_lat_col] = 'first'
|
455 |
agg_dict[original_lon_col] = 'first'
|
456 |
aggregated_output = result_df.groupby('Location Name').agg(agg_dict).reset_index()
|
457 |
+
# aggregated_output['Date Range'] = aggregated_output['Start Date'] + " to " + aggregated_output['End Date']
|
458 |
+
# aggregated_output.rename(columns={'Calculated Value': 'Aggregated Value'}, inplace=True)
|
459 |
return aggregated_output.to_dict(orient='records'), processing_time
|
460 |
else:
|
461 |
return result_df.to_dict(orient='records'), processing_time
|
|
|
745 |
st.write("Map of Uploaded Polygons:")
|
746 |
m.to_streamlit()
|
747 |
|
748 |
+
# if st.button(f"Calculate {custom_formula}"):
|
749 |
+
# if not locations_df.empty:
|
750 |
+
# with st.spinner("Processing Data..."):
|
751 |
+
# try:
|
752 |
+
# results, processing_time = process_aggregation(
|
753 |
+
# locations_df,
|
754 |
+
# start_date_str,
|
755 |
+
# end_date_str,
|
756 |
+
# dataset_id,
|
757 |
+
# selected_bands,
|
758 |
+
# reducer_choice,
|
759 |
+
# shape_type,
|
760 |
+
# aggregation_period,
|
761 |
+
# original_lat_col,
|
762 |
+
# original_lon_col,
|
763 |
+
# custom_formula,
|
764 |
+
# kernel_size,
|
765 |
+
# include_boundary,
|
766 |
+
# cloud_threshold=cloud_threshold
|
767 |
+
# )
|
768 |
+
# if results:
|
769 |
+
# result_df = pd.DataFrame(results)
|
770 |
+
# st.write(f"Processed Results Table ({aggregation_period}) for Formula: {custom_formula}")
|
771 |
+
# st.dataframe(result_df)
|
772 |
+
# filename = f"{main_selection}_{dataset_id}_{start_date.strftime('%Y%m%d')}_{end_date.strftime('%Y%m%d')}_{aggregation_period.lower()}.csv"
|
773 |
+
# st.download_button(
|
774 |
+
# label="Download results as CSV",
|
775 |
+
# data=result_df.to_csv(index=False).encode('utf-8'),
|
776 |
+
# file_name=filename,
|
777 |
+
# mime='text/csv'
|
778 |
+
# )
|
779 |
+
# st.success(f"Processing complete! Total processing time: {processing_time:.2f} seconds.")
|
780 |
+
|
781 |
+
# # Graph Visualization Section
|
782 |
+
# st.markdown("<h5>Graph Visualization</h5>", unsafe_allow_html=True)
|
783 |
+
|
784 |
+
# # Dynamically identify the time column
|
785 |
+
# if aggregation_period.lower() == 'custom (start date to end date)':
|
786 |
+
# x_column = 'Date Range'
|
787 |
+
# elif 'Date' in result_df.columns:
|
788 |
+
# x_column = 'Date'
|
789 |
+
# elif 'Week' in result_df.columns:
|
790 |
+
# x_column = 'Week'
|
791 |
+
# elif 'Month' in result_df.columns:
|
792 |
+
# x_column = 'Month'
|
793 |
+
# elif 'Year' in result_df.columns:
|
794 |
+
# x_column = 'Year'
|
795 |
+
# else:
|
796 |
+
# st.warning("No valid time column found for plotting.")
|
797 |
+
# st.stop()
|
798 |
+
|
799 |
+
# y_column = 'Calculated Value'
|
800 |
+
|
801 |
+
# # Line Chart
|
802 |
+
# st.subheader("Line Chart")
|
803 |
+
# st.line_chart(result_df.set_index(x_column)[y_column])
|
804 |
+
|
805 |
+
# # Bar Chart
|
806 |
+
# st.subheader("Bar Chart")
|
807 |
+
# st.bar_chart(result_df.set_index(x_column)[y_column])
|
808 |
+
|
809 |
+
# # Advanced Plot (Plotly)
|
810 |
+
# st.subheader("Advanced Interactive Plot (Plotly)")
|
811 |
+
# fig = px.line(
|
812 |
+
# result_df,
|
813 |
+
# x=x_column,
|
814 |
+
# y=y_column,
|
815 |
+
# color='Location Name',
|
816 |
+
# title=f"{custom_formula} Over Time"
|
817 |
+
# )
|
818 |
+
# st.plotly_chart(fig)
|
819 |
+
|
820 |
+
# else:
|
821 |
+
# st.warning("No results were generated. Check your inputs or formula.")
|
822 |
+
# st.info(f"Total processing time: {processing_time:.2f} seconds.")
|
823 |
+
|
824 |
+
# except Exception as e:
|
825 |
+
# st.error(f"An error occurred during processing: {str(e)}")
|
826 |
+
# else:
|
827 |
+
# st.warning("Please upload a valid file to proceed.")
|
828 |
+
|
829 |
if st.button(f"Calculate {custom_formula}"):
|
830 |
if not locations_df.empty:
|
831 |
with st.spinner("Processing Data..."):
|
|
|
850 |
result_df = pd.DataFrame(results)
|
851 |
st.write(f"Processed Results Table ({aggregation_period}) for Formula: {custom_formula}")
|
852 |
st.dataframe(result_df)
|
853 |
+
|
854 |
+
# Debug: Print column names to verify
|
855 |
+
st.write("Available columns in results:", result_df.columns.tolist())
|
856 |
+
|
857 |
filename = f"{main_selection}_{dataset_id}_{start_date.strftime('%Y%m%d')}_{end_date.strftime('%Y%m%d')}_{aggregation_period.lower()}.csv"
|
858 |
st.download_button(
|
859 |
label="Download results as CSV",
|
|
|
866 |
# Graph Visualization Section
|
867 |
st.markdown("<h5>Graph Visualization</h5>", unsafe_allow_html=True)
|
868 |
|
869 |
+
# Dynamically identify the value column (handle both 'Calculated Value' and 'Aggregated Value')
|
870 |
+
value_column = None
|
871 |
+
if 'Calculated Value' in result_df.columns:
|
872 |
+
value_column = 'Calculated Value'
|
873 |
+
elif 'Aggregated Value' in result_df.columns:
|
874 |
+
value_column = 'Aggregated Value'
|
875 |
+
else:
|
876 |
+
st.warning("No value column found for plotting. Available columns: " + ", ".join(result_df.columns))
|
877 |
+
st.stop()
|
878 |
+
|
879 |
# Dynamically identify the time column
|
880 |
if aggregation_period.lower() == 'custom (start date to end date)':
|
881 |
x_column = 'Date Range'
|
|
|
888 |
elif 'Year' in result_df.columns:
|
889 |
x_column = 'Year'
|
890 |
else:
|
891 |
+
st.warning("No valid time column found for plotting. Available columns: " + ", ".join(result_df.columns))
|
892 |
st.stop()
|
893 |
|
894 |
+
# Ensure we have valid data to plot
|
895 |
+
if result_df.empty:
|
896 |
+
st.warning("No data available for plotting.")
|
897 |
+
st.stop()
|
898 |
|
899 |
# Line Chart
|
900 |
+
try:
|
901 |
+
st.subheader("Line Chart")
|
902 |
+
st.line_chart(result_df.set_index(x_column)[value_column])
|
903 |
+
except Exception as e:
|
904 |
+
st.error(f"Error creating line chart: {str(e)}")
|
905 |
|
906 |
# Bar Chart
|
907 |
+
try:
|
908 |
+
st.subheader("Bar Chart")
|
909 |
+
st.bar_chart(result_df.set_index(x_column)[value_column])
|
910 |
+
except Exception as e:
|
911 |
+
st.error(f"Error creating bar chart: {str(e)}")
|
912 |
|
913 |
# Advanced Plot (Plotly)
|
914 |
+
try:
|
915 |
+
st.subheader("Advanced Interactive Plot (Plotly)")
|
916 |
+
fig = px.line(
|
917 |
+
result_df,
|
918 |
+
x=x_column,
|
919 |
+
y=value_column,
|
920 |
+
color='Location Name' if 'Location Name' in result_df.columns else None,
|
921 |
+
title=f"{custom_formula} Over Time"
|
922 |
+
)
|
923 |
+
st.plotly_chart(fig)
|
924 |
+
except Exception as e:
|
925 |
+
st.error(f"Error creating interactive plot: {str(e)}")
|
926 |
|
927 |
else:
|
928 |
st.warning("No results were generated. Check your inputs or formula.")
|