YashMK89 commited on
Commit
4429f9e
·
verified ·
1 Parent(s): 5a6fe9a

update app.py

Browse files
Files changed (1) hide show
  1. app.py +147 -16
app.py CHANGED
@@ -431,14 +431,13 @@ def process_aggregation(locations_df, start_date_str, end_date_str, dataset_id,
431
  agg_dict = {
432
  'Start Date': 'first',
433
  'End Date': 'first',
434
- 'Calculated Value': 'mean'
435
  }
436
  if shape_type.lower() == 'point':
437
  agg_dict[original_lat_col] = 'first'
438
  agg_dict[original_lon_col] = 'first'
439
  aggregated_output = result_df.groupby('Location Name').agg(agg_dict).reset_index()
440
  aggregated_output['Date Range'] = aggregated_output['Start Date'] + " to " + aggregated_output['End Date']
441
- aggregated_output.rename(columns={'Calculated Value': 'Aggregated Value'}, inplace=True)
442
  return aggregated_output.to_dict(orient='records'), processing_time
443
  else:
444
  return result_df.to_dict(orient='records'), processing_time
@@ -748,10 +747,12 @@ if file_upload is not None:
748
  m.add_gdf(gdf=gdf, layer_name=row.get('name', 'Unnamed Polygon'))
749
  st.write("Map of Uploaded Polygons:")
750
  m.to_streamlit()
 
751
  if st.button(f"Calculate {custom_formula}"):
752
  if not locations_df.empty:
753
  with st.spinner("Processing Data..."):
754
  try:
 
755
  results, processing_time = process_aggregation(
756
  locations_df,
757
  start_date_str,
@@ -763,17 +764,21 @@ if st.button(f"Calculate {custom_formula}"):
763
  aggregation_period,
764
  original_lat_col,
765
  original_lon_col,
766
- custom_formula,
767
- kernel_size,
768
- include_boundary,
769
  tile_cloud_threshold=tile_cloud_threshold if "tile_cloud_threshold" in locals() else 0,
770
  pixel_cloud_threshold=pixel_cloud_threshold if "pixel_cloud_threshold" in locals() else 0,
771
  user_scale=user_scale
772
  )
 
 
773
  if results:
774
  result_df = pd.DataFrame(results)
775
  st.write(f"Processed Results Table ({aggregation_period}) for Formula: {custom_formula}")
776
  st.dataframe(result_df)
 
 
777
  filename = f"{main_selection}_{dataset_id}_{start_date.strftime('%Y%m%d')}_{end_date.strftime('%Y%m%d')}_{aggregation_period.lower()}.csv"
778
  st.download_button(
779
  label="Download results as CSV",
@@ -781,10 +786,16 @@ if st.button(f"Calculate {custom_formula}"):
781
  file_name=filename,
782
  mime='text/csv'
783
  )
 
 
784
  st.success(f"Processing complete! Total processing time: {processing_time:.2f} seconds.")
 
 
785
  st.markdown("<h5>Graph Visualization</h5>", unsafe_allow_html=True)
 
 
786
  if aggregation_period.lower() == 'custom (start date to end date)':
787
- x_column = 'Date Range'
788
  elif 'Date' in result_df.columns:
789
  x_column = 'Date'
790
  elif 'Week' in result_df.columns:
@@ -796,19 +807,139 @@ if st.button(f"Calculate {custom_formula}"):
796
  else:
797
  st.warning("No valid time column found for plotting.")
798
  st.stop()
799
- y_column = 'Calculated Value'
800
- fig = px.line(
801
- result_df,
802
- x=x_column,
803
- y=y_column,
804
- color='Location Name',
805
- title=f"{custom_formula} Over Time"
806
- )
807
- st.plotly_chart(fig)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
808
  else:
809
  st.warning("No results were generated. Check your inputs or formula.")
810
  st.info(f"Total processing time: {processing_time:.2f} seconds.")
 
811
  except Exception as e:
812
  st.error(f"An error occurred during processing: {str(e)}")
813
  else:
814
- st.warning("Please upload a valid file to proceed.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
431
  agg_dict = {
432
  'Start Date': 'first',
433
  'End Date': 'first',
434
+ 'Calculated Value': 'mean' # Ensure this column is named 'Calculated Value'
435
  }
436
  if shape_type.lower() == 'point':
437
  agg_dict[original_lat_col] = 'first'
438
  agg_dict[original_lon_col] = 'first'
439
  aggregated_output = result_df.groupby('Location Name').agg(agg_dict).reset_index()
440
  aggregated_output['Date Range'] = aggregated_output['Start Date'] + " to " + aggregated_output['End Date']
 
441
  return aggregated_output.to_dict(orient='records'), processing_time
442
  else:
443
  return result_df.to_dict(orient='records'), processing_time
 
747
  m.add_gdf(gdf=gdf, layer_name=row.get('name', 'Unnamed Polygon'))
748
  st.write("Map of Uploaded Polygons:")
749
  m.to_streamlit()
750
+
751
  if st.button(f"Calculate {custom_formula}"):
752
  if not locations_df.empty:
753
  with st.spinner("Processing Data..."):
754
  try:
755
+ # Call the aggregation function with updated parameters
756
  results, processing_time = process_aggregation(
757
  locations_df,
758
  start_date_str,
 
764
  aggregation_period,
765
  original_lat_col,
766
  original_lon_col,
767
+ custom_formula=custom_formula,
768
+ kernel_size=kernel_size,
769
+ include_boundary=include_boundary,
770
  tile_cloud_threshold=tile_cloud_threshold if "tile_cloud_threshold" in locals() else 0,
771
  pixel_cloud_threshold=pixel_cloud_threshold if "pixel_cloud_threshold" in locals() else 0,
772
  user_scale=user_scale
773
  )
774
+
775
+ # Process and display results
776
  if results:
777
  result_df = pd.DataFrame(results)
778
  st.write(f"Processed Results Table ({aggregation_period}) for Formula: {custom_formula}")
779
  st.dataframe(result_df)
780
+
781
+ # Download button for CSV
782
  filename = f"{main_selection}_{dataset_id}_{start_date.strftime('%Y%m%d')}_{end_date.strftime('%Y%m%d')}_{aggregation_period.lower()}.csv"
783
  st.download_button(
784
  label="Download results as CSV",
 
786
  file_name=filename,
787
  mime='text/csv'
788
  )
789
+
790
+ # Success message
791
  st.success(f"Processing complete! Total processing time: {processing_time:.2f} seconds.")
792
+
793
+ # Graph Visualization Section
794
  st.markdown("<h5>Graph Visualization</h5>", unsafe_allow_html=True)
795
+
796
+ # Dynamically identify the time column
797
  if aggregation_period.lower() == 'custom (start date to end date)':
798
+ x_column = 'Date Range'
799
  elif 'Date' in result_df.columns:
800
  x_column = 'Date'
801
  elif 'Week' in result_df.columns:
 
807
  else:
808
  st.warning("No valid time column found for plotting.")
809
  st.stop()
810
+
811
+ # Dynamically identify the value column
812
+ y_column = None
813
+ if 'Calculated Value' in result_df.columns:
814
+ y_column = 'Calculated Value'
815
+ elif 'Aggregated Value' in result_df.columns:
816
+ y_column = 'Aggregated Value'
817
+ else:
818
+ st.warning("No value column found for plotting. Available columns: " + ", ".join(result_df.columns))
819
+ st.stop()
820
+
821
+ # Ensure we have valid data to plot
822
+ if result_df.empty:
823
+ st.warning("No data available for plotting.")
824
+ st.stop()
825
+
826
+ # Line Chart
827
+ try:
828
+ st.subheader("Line Chart")
829
+ if x_column == 'Location Name':
830
+ st.line_chart(result_df.set_index(x_column)[y_column])
831
+ else:
832
+ # Convert to datetime for better sorting
833
+ result_df[x_column] = pd.to_datetime(result_df[x_column], errors='ignore')
834
+ result_df = result_df.sort_values(x_column)
835
+ st.line_chart(result_df.set_index(x_column)[y_column])
836
+ except Exception as e:
837
+ st.error(f"Error creating line chart: {str(e)}")
838
+
839
+ # Bar Chart
840
+ try:
841
+ st.subheader("Bar Chart")
842
+ if x_column == 'Location Name':
843
+ st.bar_chart(result_df.set_index(x_column)[y_column])
844
+ else:
845
+ result_df[x_column] = pd.to_datetime(result_df[x_column], errors='ignore')
846
+ result_df = result_df.sort_values(x_column)
847
+ st.bar_chart(result_df.set_index(x_column)[y_column])
848
+ except Exception as e:
849
+ st.error(f"Error creating bar chart: {str(e)}")
850
+
851
+ # Advanced Plot (Plotly)
852
+ try:
853
+ st.subheader("Advanced Interactive Plot (Plotly)")
854
+ if x_column == 'Location Name':
855
+ fig = px.bar(
856
+ result_df,
857
+ x=x_column,
858
+ y=y_column,
859
+ color='Location Name',
860
+ title=f"{custom_formula} by Location"
861
+ )
862
+ else:
863
+ fig = px.line(
864
+ result_df,
865
+ x=x_column,
866
+ y=y_column,
867
+ color='Location Name',
868
+ title=f"{custom_formula} Over Time"
869
+ )
870
+ st.plotly_chart(fig)
871
+ except Exception as e:
872
+ st.error(f"Error creating interactive plot: {str(e)}")
873
+
874
  else:
875
  st.warning("No results were generated. Check your inputs or formula.")
876
  st.info(f"Total processing time: {processing_time:.2f} seconds.")
877
+
878
  except Exception as e:
879
  st.error(f"An error occurred during processing: {str(e)}")
880
  else:
881
+ st.warning("Please upload a valid file to proceed.")
882
+ # if st.button(f"Calculate {custom_formula}"):
883
+ # if not locations_df.empty:
884
+ # with st.spinner("Processing Data..."):
885
+ # try:
886
+ # results, processing_time = process_aggregation(
887
+ # locations_df,
888
+ # start_date_str,
889
+ # end_date_str,
890
+ # dataset_id,
891
+ # selected_bands,
892
+ # reducer_choice,
893
+ # shape_type,
894
+ # aggregation_period,
895
+ # original_lat_col,
896
+ # original_lon_col,
897
+ # custom_formula,
898
+ # kernel_size,
899
+ # include_boundary,
900
+ # tile_cloud_threshold=tile_cloud_threshold if "tile_cloud_threshold" in locals() else 0,
901
+ # pixel_cloud_threshold=pixel_cloud_threshold if "pixel_cloud_threshold" in locals() else 0,
902
+ # user_scale=user_scale
903
+ # )
904
+ # if results:
905
+ # result_df = pd.DataFrame(results)
906
+ # st.write(f"Processed Results Table ({aggregation_period}) for Formula: {custom_formula}")
907
+ # st.dataframe(result_df)
908
+ # filename = f"{main_selection}_{dataset_id}_{start_date.strftime('%Y%m%d')}_{end_date.strftime('%Y%m%d')}_{aggregation_period.lower()}.csv"
909
+ # st.download_button(
910
+ # label="Download results as CSV",
911
+ # data=result_df.to_csv(index=False).encode('utf-8'),
912
+ # file_name=filename,
913
+ # mime='text/csv'
914
+ # )
915
+ # st.success(f"Processing complete! Total processing time: {processing_time:.2f} seconds.")
916
+ # st.markdown("<h5>Graph Visualization</h5>", unsafe_allow_html=True)
917
+ # if aggregation_period.lower() == 'custom (start date to end date)':
918
+ # x_column = 'Date Range'
919
+ # elif 'Date' in result_df.columns:
920
+ # x_column = 'Date'
921
+ # elif 'Week' in result_df.columns:
922
+ # x_column = 'Week'
923
+ # elif 'Month' in result_df.columns:
924
+ # x_column = 'Month'
925
+ # elif 'Year' in result_df.columns:
926
+ # x_column = 'Year'
927
+ # else:
928
+ # st.warning("No valid time column found for plotting.")
929
+ # st.stop()
930
+ # y_column = 'Calculated Value'
931
+ # fig = px.line(
932
+ # result_df,
933
+ # x=x_column,
934
+ # y=y_column,
935
+ # color='Location Name',
936
+ # title=f"{custom_formula} Over Time"
937
+ # )
938
+ # st.plotly_chart(fig)
939
+ # else:
940
+ # st.warning("No results were generated. Check your inputs or formula.")
941
+ # st.info(f"Total processing time: {processing_time:.2f} seconds.")
942
+ # except Exception as e:
943
+ # st.error(f"An error occurred during processing: {str(e)}")
944
+ # else:
945
+ # st.warning("Please upload a valid file to proceed.")