-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
75 lines (67 loc) · 2.45 KB
/
main.py
File metadata and controls
75 lines (67 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import streamlit as st
import pandas as pd
import numpy as np
import os
# ---------------------------
# Function: Interpolate Data
# ---------------------------
def iterative_interpolation(df):
"""
Interpolates missing values (NaN) using the average of non-diagonal neighbors.
"""
df = df.copy()
rows, cols = df.shape
for i in range(rows):
for j in range(cols):
if pd.isna(df.iat[i, j]):
# Collect neighbors (up, down, left, right)
neighbors = []
if i > 0 and not pd.isna(df.iat[i-1, j]):
neighbors.append(df.iat[i-1, j])
if i < rows-1 and not pd.isna(df.iat[i+1, j]):
neighbors.append(df.iat[i+1, j])
if j > 0 and not pd.isna(df.iat[i, j-1]):
neighbors.append(df.iat[i, j-1])
if j < cols-1 and not pd.isna(df.iat[i, j+1]):
neighbors.append(df.iat[i, j+1])
if neighbors:
df.iat[i, j] = np.mean(neighbors)
return df
# ---------------------------
# Streamlit App Layout
# ---------------------------
st.title("CSV Interpolation App")
st.write("""
Upload a CSV file or try the built-in sample dataset.
This app interpolates missing values using neighboring values,
previews the results, and lets you download the processed CSV.
""")
# ---------------------------
# File Upload + Sample Data
# ---------------------------
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
use_sample = st.checkbox("Use sample dataset")
df_input = None
if uploaded_file:
df_input = pd.read_csv(uploaded_file, header=None)
elif use_sample:
sample_path = os.path.join(os.path.dirname(__file__), "input_test_data.csv")
df_input = pd.read_csv(sample_path, header=None)
st.info("Loaded sample dataset: `input_test_data.csv`")
# ---------------------------
# Display & Interpolation
# ---------------------------
if df_input is not None:
st.subheader("Original Data")
st.dataframe(df_input)
if st.button("Interpolate Missing Values"):
df_result = iterative_interpolation(df_input)
st.subheader("Interpolated Data")
st.dataframe(df_result)
csv = df_result.to_csv(index=False, header=False).encode("utf-8")
st.download_button(
label="Download Interpolated CSV",
data=csv,
file_name="interpolated_results.csv",
mime="text/csv",
)