YashMK89 commited on
Commit
670b8c2
·
verified ·
1 Parent(s): c3435ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +194 -194
app.py CHANGED
@@ -745,86 +745,86 @@ 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..."):
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
 
830
 
@@ -935,141 +935,141 @@ if file_upload is not None:
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..."):
941
- try:
942
- results, processing_time = process_aggregation(
943
- locations_df,
944
- start_date_str,
945
- end_date_str,
946
- dataset_id,
947
- selected_bands,
948
- reducer_choice,
949
- shape_type,
950
- aggregation_period,
951
- original_lat_col,
952
- original_lon_col,
953
- custom_formula,
954
- kernel_size,
955
- include_boundary,
956
- cloud_threshold=cloud_threshold
957
- )
958
- if results:
959
- result_df = pd.DataFrame(results)
960
 
961
- # Reorder and rename columns
962
- column_mapping = {
963
- 'Location Name': 'Location Name',
964
- 'Start Date': 'Start Date',
965
- 'End Date': 'End Date',
966
- 'Date Range': 'Date Range',
967
- original_lat_col: 'Latitude',
968
- original_lon_col: 'Longitude',
969
- 'Aggregated Value': 'Calculated Value',
970
- 'Calculated Value': 'Calculated Value'
971
- }
972
 
973
- # Keep only columns that exist in the results
974
- available_columns = [col for col in column_mapping.keys() if col in result_df.columns]
975
- result_df = result_df[available_columns]
976
- result_df = result_df.rename(columns={k:v for k,v in column_mapping.items() if k in available_columns})
977
 
978
- st.write(f"Processed Results Table ({aggregation_period}) for Formula: {custom_formula}")
979
- st.dataframe(result_df)
980
 
981
- # Graph Visualization Section
982
- st.markdown("<h5>Graph Visualization</h5>", unsafe_allow_html=True)
983
 
984
- # Determine time column based on aggregation period
985
- time_column_map = {
986
- 'custom (start date to end date)': 'Date Range',
987
- 'daily': 'Date',
988
- 'weekly': 'Week',
989
- 'monthly': 'Month',
990
- 'yearly': 'Year'
991
- }
992
 
993
- x_column = time_column_map.get(aggregation_period.lower())
994
 
995
- if x_column not in result_df.columns:
996
- # Try to find any time-related column
997
- time_columns = ['Date Range', 'Date', 'Week', 'Month', 'Year', 'day', 'month', 'year']
998
- x_column = next((col for col in time_columns if col in result_df.columns), None)
999
 
1000
- if x_column is None:
1001
- st.warning("No time column found for plotting. Showing data without time axis.")
1002
- x_column = 'Location Name'
1003
 
1004
- value_column = 'Calculated Value'
1005
 
1006
- if value_column not in result_df.columns:
1007
- st.error("No calculated values found for plotting.")
1008
- st.stop()
1009
 
1010
- # Line Chart
1011
- try:
1012
- st.subheader("Line Chart")
1013
- if x_column == 'Location Name':
1014
- st.line_chart(result_df.set_index(x_column)[value_column])
1015
- else:
1016
- # Convert to datetime for better sorting
1017
- result_df[x_column] = pd.to_datetime(result_df[x_column], errors='ignore')
1018
- result_df = result_df.sort_values(x_column)
1019
- st.line_chart(result_df.set_index(x_column)[value_column])
1020
- except Exception as e:
1021
- st.error(f"Error creating line chart: {str(e)}")
1022
 
1023
- # Bar Chart
1024
- try:
1025
- st.subheader("Bar Chart")
1026
- if x_column == 'Location Name':
1027
- st.bar_chart(result_df.set_index(x_column)[value_column])
1028
- else:
1029
- result_df[x_column] = pd.to_datetime(result_df[x_column], errors='ignore')
1030
- result_df = result_df.sort_values(x_column)
1031
- st.bar_chart(result_df.set_index(x_column)[value_column])
1032
- except Exception as e:
1033
- st.error(f"Error creating bar chart: {str(e)}")
1034
 
1035
- # Advanced Plot (Plotly)
1036
- try:
1037
- st.subheader("Advanced Interactive Plot (Plotly)")
1038
- if x_column == 'Location Name':
1039
- fig = px.bar(
1040
- result_df,
1041
- x=x_column,
1042
- y=value_column,
1043
- color='Location Name',
1044
- title=f"{custom_formula} by Location"
1045
- )
1046
- else:
1047
- fig = px.line(
1048
- result_df,
1049
- x=x_column,
1050
- y=value_column,
1051
- color='Location Name',
1052
- title=f"{custom_formula} Over Time"
1053
- )
1054
- st.plotly_chart(fig)
1055
- except Exception as e:
1056
- st.error(f"Error creating interactive plot: {str(e)}")
1057
 
1058
- # Download button
1059
- filename = f"{main_selection}_{dataset_id}_{start_date.strftime('%Y%m%d')}_{end_date.strftime('%Y%m%d')}_{aggregation_period.lower()}.csv"
1060
- st.download_button(
1061
- label="Download results as CSV",
1062
- data=result_df.to_csv(index=False).encode('utf-8'),
1063
- file_name=filename,
1064
- mime='text/csv'
1065
- )
1066
- st.success(f"Processing complete! Total processing time: {processing_time:.2f} seconds.")
1067
 
1068
- else:
1069
- st.warning("No results were generated. Check your inputs or formula.")
1070
- st.info(f"Total processing time: {processing_time:.2f} seconds.")
1071
 
1072
- except Exception as e:
1073
- st.error(f"An error occurred during processing: {str(e)}")
1074
- else:
1075
- st.warning("Please upload a valid file to proceed.")
 
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
 
830
 
 
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..."):
941
+ # try:
942
+ # results, processing_time = process_aggregation(
943
+ # locations_df,
944
+ # start_date_str,
945
+ # end_date_str,
946
+ # dataset_id,
947
+ # selected_bands,
948
+ # reducer_choice,
949
+ # shape_type,
950
+ # aggregation_period,
951
+ # original_lat_col,
952
+ # original_lon_col,
953
+ # custom_formula,
954
+ # kernel_size,
955
+ # include_boundary,
956
+ # cloud_threshold=cloud_threshold
957
+ # )
958
+ # if results:
959
+ # result_df = pd.DataFrame(results)
960
 
961
+ # # Reorder and rename columns
962
+ # column_mapping = {
963
+ # 'Location Name': 'Location Name',
964
+ # 'Start Date': 'Start Date',
965
+ # 'End Date': 'End Date',
966
+ # 'Date Range': 'Date Range',
967
+ # original_lat_col: 'Latitude',
968
+ # original_lon_col: 'Longitude',
969
+ # 'Aggregated Value': 'Calculated Value',
970
+ # 'Calculated Value': 'Calculated Value'
971
+ # }
972
 
973
+ # # Keep only columns that exist in the results
974
+ # available_columns = [col for col in column_mapping.keys() if col in result_df.columns]
975
+ # result_df = result_df[available_columns]
976
+ # result_df = result_df.rename(columns={k:v for k,v in column_mapping.items() if k in available_columns})
977
 
978
+ # st.write(f"Processed Results Table ({aggregation_period}) for Formula: {custom_formula}")
979
+ # st.dataframe(result_df)
980
 
981
+ # # Graph Visualization Section
982
+ # st.markdown("<h5>Graph Visualization</h5>", unsafe_allow_html=True)
983
 
984
+ # # Determine time column based on aggregation period
985
+ # time_column_map = {
986
+ # 'custom (start date to end date)': 'Date Range',
987
+ # 'daily': 'Date',
988
+ # 'weekly': 'Week',
989
+ # 'monthly': 'Month',
990
+ # 'yearly': 'Year'
991
+ # }
992
 
993
+ # x_column = time_column_map.get(aggregation_period.lower())
994
 
995
+ # if x_column not in result_df.columns:
996
+ # # Try to find any time-related column
997
+ # time_columns = ['Date Range', 'Date', 'Week', 'Month', 'Year', 'day', 'month', 'year']
998
+ # x_column = next((col for col in time_columns if col in result_df.columns), None)
999
 
1000
+ # if x_column is None:
1001
+ # st.warning("No time column found for plotting. Showing data without time axis.")
1002
+ # x_column = 'Location Name'
1003
 
1004
+ # value_column = 'Calculated Value'
1005
 
1006
+ # if value_column not in result_df.columns:
1007
+ # st.error("No calculated values found for plotting.")
1008
+ # st.stop()
1009
 
1010
+ # # Line Chart
1011
+ # try:
1012
+ # st.subheader("Line Chart")
1013
+ # if x_column == 'Location Name':
1014
+ # st.line_chart(result_df.set_index(x_column)[value_column])
1015
+ # else:
1016
+ # # Convert to datetime for better sorting
1017
+ # result_df[x_column] = pd.to_datetime(result_df[x_column], errors='ignore')
1018
+ # result_df = result_df.sort_values(x_column)
1019
+ # st.line_chart(result_df.set_index(x_column)[value_column])
1020
+ # except Exception as e:
1021
+ # st.error(f"Error creating line chart: {str(e)}")
1022
 
1023
+ # # Bar Chart
1024
+ # try:
1025
+ # st.subheader("Bar Chart")
1026
+ # if x_column == 'Location Name':
1027
+ # st.bar_chart(result_df.set_index(x_column)[value_column])
1028
+ # else:
1029
+ # result_df[x_column] = pd.to_datetime(result_df[x_column], errors='ignore')
1030
+ # result_df = result_df.sort_values(x_column)
1031
+ # st.bar_chart(result_df.set_index(x_column)[value_column])
1032
+ # except Exception as e:
1033
+ # st.error(f"Error creating bar chart: {str(e)}")
1034
 
1035
+ # # Advanced Plot (Plotly)
1036
+ # try:
1037
+ # st.subheader("Advanced Interactive Plot (Plotly)")
1038
+ # if x_column == 'Location Name':
1039
+ # fig = px.bar(
1040
+ # result_df,
1041
+ # x=x_column,
1042
+ # y=value_column,
1043
+ # color='Location Name',
1044
+ # title=f"{custom_formula} by Location"
1045
+ # )
1046
+ # else:
1047
+ # fig = px.line(
1048
+ # result_df,
1049
+ # x=x_column,
1050
+ # y=value_column,
1051
+ # color='Location Name',
1052
+ # title=f"{custom_formula} Over Time"
1053
+ # )
1054
+ # st.plotly_chart(fig)
1055
+ # except Exception as e:
1056
+ # st.error(f"Error creating interactive plot: {str(e)}")
1057
 
1058
+ # # Download button
1059
+ # filename = f"{main_selection}_{dataset_id}_{start_date.strftime('%Y%m%d')}_{end_date.strftime('%Y%m%d')}_{aggregation_period.lower()}.csv"
1060
+ # st.download_button(
1061
+ # label="Download results as CSV",
1062
+ # data=result_df.to_csv(index=False).encode('utf-8'),
1063
+ # file_name=filename,
1064
+ # mime='text/csv'
1065
+ # )
1066
+ # st.success(f"Processing complete! Total processing time: {processing_time:.2f} seconds.")
1067
 
1068
+ # else:
1069
+ # st.warning("No results were generated. Check your inputs or formula.")
1070
+ # st.info(f"Total processing time: {processing_time:.2f} seconds.")
1071
 
1072
+ # except Exception as e:
1073
+ # st.error(f"An error occurred during processing: {str(e)}")
1074
+ # else:
1075
+ # st.warning("Please upload a valid file to proceed.")