YashMK89 commited on
Commit
4b0c37d
·
verified ·
1 Parent(s): f5b376a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -14
app.py CHANGED
@@ -826,6 +826,115 @@ if file_upload is not None:
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..."):
@@ -848,12 +957,32 @@ if st.button(f"Calculate {custom_formula}"):
848
  )
849
  if results:
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,15 +995,8 @@ if st.button(f"Calculate {custom_formula}"):
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)':
@@ -888,7 +1010,7 @@ if st.button(f"Calculate {custom_formula}"):
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
@@ -917,7 +1039,7 @@ if st.button(f"Calculate {custom_formula}"):
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)
 
826
  # else:
827
  # st.warning("Please upload a valid file to proceed.")
828
 
829
+
830
+
831
+ # if st.button(f"Calculate {custom_formula}"):
832
+ # if not locations_df.empty:
833
+ # with st.spinner("Processing Data..."):
834
+ # try:
835
+ # results, processing_time = process_aggregation(
836
+ # locations_df,
837
+ # start_date_str,
838
+ # end_date_str,
839
+ # dataset_id,
840
+ # selected_bands,
841
+ # reducer_choice,
842
+ # shape_type,
843
+ # aggregation_period,
844
+ # original_lat_col,
845
+ # original_lon_col,
846
+ # custom_formula,
847
+ # kernel_size,
848
+ # include_boundary,
849
+ # cloud_threshold=cloud_threshold
850
+ # )
851
+ # if results:
852
+ # result_df = pd.DataFrame(results)
853
+ # st.write(f"Processed Results Table ({aggregation_period}) for Formula: {custom_formula}")
854
+ # st.dataframe(result_df)
855
+
856
+ # # Debug: Print column names to verify
857
+ # st.write("Available columns in results:", result_df.columns.tolist())
858
+
859
+ # filename = f"{main_selection}_{dataset_id}_{start_date.strftime('%Y%m%d')}_{end_date.strftime('%Y%m%d')}_{aggregation_period.lower()}.csv"
860
+ # st.download_button(
861
+ # label="Download results as CSV",
862
+ # data=result_df.to_csv(index=False).encode('utf-8'),
863
+ # file_name=filename,
864
+ # mime='text/csv'
865
+ # )
866
+ # st.success(f"Processing complete! Total processing time: {processing_time:.2f} seconds.")
867
+
868
+ # # Graph Visualization Section
869
+ # st.markdown("<h5>Graph Visualization</h5>", unsafe_allow_html=True)
870
+
871
+ # # Dynamically identify the value column (handle both 'Calculated Value' and 'Aggregated Value')
872
+ # value_column = None
873
+ # if 'Calculated Value' in result_df.columns:
874
+ # value_column = 'Calculated Value'
875
+ # elif 'Aggregated Value' in result_df.columns:
876
+ # value_column = 'Aggregated Value'
877
+ # else:
878
+ # st.warning("No value column found for plotting. Available columns: " + ", ".join(result_df.columns))
879
+ # st.stop()
880
+
881
+ # # Dynamically identify the time column
882
+ # if aggregation_period.lower() == 'custom (start date to end date)':
883
+ # x_column = 'Date Range'
884
+ # elif 'Date' in result_df.columns:
885
+ # x_column = 'Date'
886
+ # elif 'Week' in result_df.columns:
887
+ # x_column = 'Week'
888
+ # elif 'Month' in result_df.columns:
889
+ # x_column = 'Month'
890
+ # elif 'Year' in result_df.columns:
891
+ # x_column = 'Year'
892
+ # else:
893
+ # st.warning("No valid time column found for plotting. Available columns: " + ", ".join(result_df.columns))
894
+ # st.stop()
895
+
896
+ # # Ensure we have valid data to plot
897
+ # if result_df.empty:
898
+ # st.warning("No data available for plotting.")
899
+ # st.stop()
900
+
901
+ # # Line Chart
902
+ # try:
903
+ # st.subheader("Line Chart")
904
+ # st.line_chart(result_df.set_index(x_column)[value_column])
905
+ # except Exception as e:
906
+ # st.error(f"Error creating line chart: {str(e)}")
907
+
908
+ # # Bar Chart
909
+ # try:
910
+ # st.subheader("Bar Chart")
911
+ # st.bar_chart(result_df.set_index(x_column)[value_column])
912
+ # except Exception as e:
913
+ # st.error(f"Error creating bar chart: {str(e)}")
914
+
915
+ # # Advanced Plot (Plotly)
916
+ # try:
917
+ # st.subheader("Advanced Interactive Plot (Plotly)")
918
+ # fig = px.line(
919
+ # result_df,
920
+ # x=x_column,
921
+ # y=value_column,
922
+ # color='Location Name' if 'Location Name' in result_df.columns else None,
923
+ # title=f"{custom_formula} Over Time"
924
+ # )
925
+ # st.plotly_chart(fig)
926
+ # except Exception as e:
927
+ # st.error(f"Error creating interactive plot: {str(e)}")
928
+
929
+ # else:
930
+ # st.warning("No results were generated. Check your inputs or formula.")
931
+ # st.info(f"Total processing time: {processing_time:.2f} seconds.")
932
+
933
+ # except Exception as e:
934
+ # st.error(f"An error occurred during processing: {str(e)}")
935
+ # else:
936
+ # st.warning("Please upload a valid file to proceed.")
937
+
938
  if st.button(f"Calculate {custom_formula}"):
939
  if not locations_df.empty:
940
  with st.spinner("Processing Data..."):
 
957
  )
958
  if results:
959
  result_df = pd.DataFrame(results)
960
+
961
+ # Reorder columns as requested
962
+ desired_columns = [
963
+ 'Location Name',
964
+ 'Start Date',
965
+ 'End Date',
966
+ 'Date Range',
967
+ original_lat_col,
968
+ original_lon_col,
969
+ 'Aggregated Value' if 'Aggregated Value' in result_df.columns else 'Calculated Value'
970
+ ]
971
+
972
+ # Filter to only include columns that exist in the DataFrame
973
+ final_columns = [col for col in desired_columns if col in result_df.columns]
974
+ result_df = result_df[final_columns]
975
+
976
+ # Rename the value column to be consistent
977
+ result_df = result_df.rename(columns={
978
+ 'Aggregated Value': 'Calculated Value',
979
+ original_lat_col: 'Latitude',
980
+ original_lon_col: 'Longitude'
981
+ })
982
+
983
  st.write(f"Processed Results Table ({aggregation_period}) for Formula: {custom_formula}")
984
  st.dataframe(result_df)
985
 
 
 
 
986
  filename = f"{main_selection}_{dataset_id}_{start_date.strftime('%Y%m%d')}_{end_date.strftime('%Y%m%d')}_{aggregation_period.lower()}.csv"
987
  st.download_button(
988
  label="Download results as CSV",
 
995
  # Graph Visualization Section
996
  st.markdown("<h5>Graph Visualization</h5>", unsafe_allow_html=True)
997
 
998
+ # Dynamically identify the value column
999
+ value_column = 'Calculated Value'
 
 
 
 
 
 
 
1000
 
1001
  # Dynamically identify the time column
1002
  if aggregation_period.lower() == 'custom (start date to end date)':
 
1010
  elif 'Year' in result_df.columns:
1011
  x_column = 'Year'
1012
  else:
1013
+ st.warning("No valid time column found for plotting.")
1014
  st.stop()
1015
 
1016
  # Ensure we have valid data to plot
 
1039
  result_df,
1040
  x=x_column,
1041
  y=value_column,
1042
+ color='Location Name',
1043
  title=f"{custom_formula} Over Time"
1044
  )
1045
  st.plotly_chart(fig)