Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,16 @@ import pandas as pd
|
|
| 3 |
import plotly.graph_objects as go
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
-
def plot_zip_code_correlation(zip_codes_str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Convert input string to list of zip codes
|
| 8 |
zip_codes = [z.strip() for z in zip_codes_str.split(",")]
|
| 9 |
|
|
@@ -16,16 +25,20 @@ def plot_zip_code_correlation(zip_codes_str):
|
|
| 16 |
if df.empty:
|
| 17 |
raise ValueError("No data found for the provided ZIP codes.")
|
| 18 |
|
| 19 |
-
# Extract columns
|
| 20 |
date_columns = []
|
| 21 |
for col in df.columns[7:]:
|
| 22 |
try:
|
| 23 |
-
pd.to_datetime(col)
|
| 24 |
-
|
|
|
|
| 25 |
except:
|
| 26 |
continue
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
price_matrix = []
|
| 30 |
zip_list = []
|
| 31 |
|
|
@@ -37,25 +50,23 @@ def plot_zip_code_correlation(zip_codes_str):
|
|
| 37 |
price_matrix.append(prices)
|
| 38 |
zip_list.append(zip_code)
|
| 39 |
|
| 40 |
-
# Check if there is enough data
|
| 41 |
if len(price_matrix) < 2:
|
| 42 |
raise ValueError("Not enough data for correlation calculation.")
|
| 43 |
|
| 44 |
price_matrix_df = pd.DataFrame(price_matrix, index=zip_list, columns=date_columns)
|
| 45 |
price_matrix_df = price_matrix_df.T.dropna()
|
| 46 |
|
| 47 |
-
# Calculate correlation
|
| 48 |
corr_matrix = price_matrix_df.corr()
|
| 49 |
|
| 50 |
-
# Prepare
|
| 51 |
z_data = corr_matrix.values
|
| 52 |
x_data, y_data = np.meshgrid(zip_list, zip_list)
|
| 53 |
|
| 54 |
-
#
|
| 55 |
fig = go.Figure(data=[go.Surface(z=z_data, x=x_data, y=y_data)])
|
| 56 |
-
|
| 57 |
fig.update_layout(
|
| 58 |
-
title='3D Correlation Matrix
|
| 59 |
scene=dict(
|
| 60 |
xaxis_title='ZIP Code',
|
| 61 |
yaxis_title='ZIP Code',
|
|
@@ -68,9 +79,13 @@ def plot_zip_code_correlation(zip_codes_str):
|
|
| 68 |
|
| 69 |
iface = gr.Interface(
|
| 70 |
fn=plot_zip_code_correlation,
|
| 71 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
outputs=gr.Plot(),
|
| 73 |
-
title="ZIP Code 3D Correlation Matrix"
|
| 74 |
)
|
| 75 |
|
| 76 |
-
iface.launch(debug=True)
|
|
|
|
| 3 |
import plotly.graph_objects as go
|
| 4 |
import numpy as np
|
| 5 |
|
| 6 |
+
def plot_zip_code_correlation(zip_codes_str, start_date, end_date):
|
| 7 |
+
# Validate dates
|
| 8 |
+
start_year = pd.to_datetime(start_date).year
|
| 9 |
+
end_year = pd.to_datetime(end_date).year
|
| 10 |
+
if start_year < 2000 or end_year < 2000:
|
| 11 |
+
raise ValueError("Please select dates no earlier than the year 2000.")
|
| 12 |
+
|
| 13 |
+
if start_year > end_year:
|
| 14 |
+
raise ValueError("Start date must be before end date.")
|
| 15 |
+
|
| 16 |
# Convert input string to list of zip codes
|
| 17 |
zip_codes = [z.strip() for z in zip_codes_str.split(",")]
|
| 18 |
|
|
|
|
| 25 |
if df.empty:
|
| 26 |
raise ValueError("No data found for the provided ZIP codes.")
|
| 27 |
|
| 28 |
+
# Extract valid date columns within the selected date range
|
| 29 |
date_columns = []
|
| 30 |
for col in df.columns[7:]:
|
| 31 |
try:
|
| 32 |
+
date = pd.to_datetime(col)
|
| 33 |
+
if start_date <= str(date.date()) <= end_date:
|
| 34 |
+
date_columns.append(col)
|
| 35 |
except:
|
| 36 |
continue
|
| 37 |
|
| 38 |
+
if not date_columns:
|
| 39 |
+
raise ValueError("No data available within the selected date range.")
|
| 40 |
+
|
| 41 |
+
# Initialize price matrix
|
| 42 |
price_matrix = []
|
| 43 |
zip_list = []
|
| 44 |
|
|
|
|
| 50 |
price_matrix.append(prices)
|
| 51 |
zip_list.append(zip_code)
|
| 52 |
|
|
|
|
| 53 |
if len(price_matrix) < 2:
|
| 54 |
raise ValueError("Not enough data for correlation calculation.")
|
| 55 |
|
| 56 |
price_matrix_df = pd.DataFrame(price_matrix, index=zip_list, columns=date_columns)
|
| 57 |
price_matrix_df = price_matrix_df.T.dropna()
|
| 58 |
|
| 59 |
+
# Calculate correlation matrix
|
| 60 |
corr_matrix = price_matrix_df.corr()
|
| 61 |
|
| 62 |
+
# Prepare 3D plot data
|
| 63 |
z_data = corr_matrix.values
|
| 64 |
x_data, y_data = np.meshgrid(zip_list, zip_list)
|
| 65 |
|
| 66 |
+
# Plot
|
| 67 |
fig = go.Figure(data=[go.Surface(z=z_data, x=x_data, y=y_data)])
|
|
|
|
| 68 |
fig.update_layout(
|
| 69 |
+
title=f'3D Correlation Matrix ({start_date} to {end_date})',
|
| 70 |
scene=dict(
|
| 71 |
xaxis_title='ZIP Code',
|
| 72 |
yaxis_title='ZIP Code',
|
|
|
|
| 79 |
|
| 80 |
iface = gr.Interface(
|
| 81 |
fn=plot_zip_code_correlation,
|
| 82 |
+
inputs=[
|
| 83 |
+
gr.Textbox(label="Enter comma-separated ZIP codes (e.g., 07001, 07002, 07003)"),
|
| 84 |
+
gr.Textbox(label="Start Date (YYYY-MM-DD) - No earlier than 2000"),
|
| 85 |
+
gr.Textbox(label="End Date (YYYY-MM-DD) - No earlier than 2000")
|
| 86 |
+
],
|
| 87 |
outputs=gr.Plot(),
|
| 88 |
+
title="ZIP Code 3D Correlation Matrix with Date Range"
|
| 89 |
)
|
| 90 |
|
| 91 |
+
iface.launch(share=False, debug=True)
|