github-actions[bot] commited on
Commit ·
bc24113
1
Parent(s): 119e670
Deploy from GitHub Actions
Browse files- config.py +7 -1
- settings.py +74 -0
- utils/data_loading.py +65 -1
- utils/session.py +33 -1
config.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
-
from dataclasses import dataclass
|
| 3 |
from datetime import datetime
|
| 4 |
|
| 5 |
|
|
@@ -14,6 +14,12 @@ class AppConfig:
|
|
| 14 |
DATA_FILE_PATH: str = "data/master_data_file_1990_-_2024.parquet"
|
| 15 |
ADMIN_PASSWORD: str = ""
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
@classmethod
|
| 18 |
def from_env(cls) -> "AppConfig":
|
| 19 |
return cls(
|
|
|
|
| 1 |
import os
|
| 2 |
+
from dataclasses import dataclass, field
|
| 3 |
from datetime import datetime
|
| 4 |
|
| 5 |
|
|
|
|
| 14 |
DATA_FILE_PATH: str = "data/master_data_file_1990_-_2024.parquet"
|
| 15 |
ADMIN_PASSWORD: str = ""
|
| 16 |
|
| 17 |
+
# Default exclusions
|
| 18 |
+
DEFAULT_EXCLUDED_SECTORS: list[str] = field(
|
| 19 |
+
default_factory=lambda: ["FWC Oyster Reef Project"]
|
| 20 |
+
)
|
| 21 |
+
DEFAULT_EXCLUDED_STATIONS: list[str] = field(default_factory=lambda: ["3.8"])
|
| 22 |
+
|
| 23 |
@classmethod
|
| 24 |
def from_env(cls) -> "AppConfig":
|
| 25 |
return cls(
|
settings.py
CHANGED
|
@@ -147,6 +147,80 @@ with col1:
|
|
| 147 |
key="dataset_reporting_month",
|
| 148 |
on_change=on_reporting_month_change,
|
| 149 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
render_dataset_metadata(metadata, min_date, max_date)
|
| 152 |
render_records_by_year(st.session_state.data["raw_df"], reporting_month)
|
|
|
|
| 147 |
key="dataset_reporting_month",
|
| 148 |
on_change=on_reporting_month_change,
|
| 149 |
)
|
| 150 |
+
st.subheader("Data Exclusions")
|
| 151 |
+
exclusion_col1, exclusion_col2 = st.columns(2)
|
| 152 |
+
|
| 153 |
+
def on_sector_exclusion_change():
|
| 154 |
+
"""Callback for sector exclusion changes"""
|
| 155 |
+
if "excluded_sectors_widget" in st.session_state:
|
| 156 |
+
# Update the persistent storage with widget values
|
| 157 |
+
st.session_state.persistent_excluded_sectors = (
|
| 158 |
+
st.session_state.excluded_sectors_widget
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
# Reload data with new exclusions
|
| 162 |
+
st.session_state.data = st.session_state.data_manager.load_data(
|
| 163 |
+
start_date=st.session_state.get("start_date"),
|
| 164 |
+
end_date=st.session_state.get("end_date"),
|
| 165 |
+
reporting_month=st.session_state.get("reporting_month"),
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
def on_station_exclusion_change():
|
| 169 |
+
"""Callback for station exclusion changes"""
|
| 170 |
+
if "excluded_stations_widget" in st.session_state:
|
| 171 |
+
# Update the persistent storage with widget values
|
| 172 |
+
st.session_state.persistent_excluded_stations = (
|
| 173 |
+
st.session_state.excluded_stations_widget
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
# Reload data with new exclusions
|
| 177 |
+
st.session_state.data = st.session_state.data_manager.load_data(
|
| 178 |
+
start_date=st.session_state.get("start_date"),
|
| 179 |
+
end_date=st.session_state.get("end_date"),
|
| 180 |
+
reporting_month=st.session_state.get("reporting_month"),
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
# Initialize persistent storage if not exists
|
| 184 |
+
if "persistent_excluded_sectors" not in st.session_state:
|
| 185 |
+
st.session_state.persistent_excluded_sectors = []
|
| 186 |
+
if "persistent_excluded_stations" not in st.session_state:
|
| 187 |
+
st.session_state.persistent_excluded_stations = []
|
| 188 |
+
|
| 189 |
+
# Reload data if there are any exclusions
|
| 190 |
+
if st.session_state.get("persistent_excluded_sectors") or st.session_state.get(
|
| 191 |
+
"persistent_excluded_stations"
|
| 192 |
+
):
|
| 193 |
+
st.session_state.data = st.session_state.data_manager.load_data(
|
| 194 |
+
start_date=st.session_state.get("start_date"),
|
| 195 |
+
end_date=st.session_state.get("end_date"),
|
| 196 |
+
reporting_month=st.session_state.get("reporting_month"),
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
with exclusion_col1:
|
| 200 |
+
# Get complete list of sectors from data manager
|
| 201 |
+
all_sectors = st.session_state.data_manager.all_sectors
|
| 202 |
+
|
| 203 |
+
st.multiselect(
|
| 204 |
+
"Exclude Sectors",
|
| 205 |
+
options=all_sectors,
|
| 206 |
+
default=st.session_state.persistent_excluded_sectors,
|
| 207 |
+
help="Select sectors to exclude from all analyses",
|
| 208 |
+
key="excluded_sectors_widget",
|
| 209 |
+
on_change=on_sector_exclusion_change,
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
with exclusion_col2:
|
| 213 |
+
# Get complete list of stations from data manager
|
| 214 |
+
all_stations = st.session_state.data_manager.all_stations
|
| 215 |
+
|
| 216 |
+
st.multiselect(
|
| 217 |
+
"Exclude Stations",
|
| 218 |
+
options=all_stations,
|
| 219 |
+
default=st.session_state.persistent_excluded_stations,
|
| 220 |
+
help="Select stations to exclude from all analyses",
|
| 221 |
+
key="excluded_stations_widget",
|
| 222 |
+
on_change=on_station_exclusion_change,
|
| 223 |
+
)
|
| 224 |
|
| 225 |
render_dataset_metadata(metadata, min_date, max_date)
|
| 226 |
render_records_by_year(st.session_state.data["raw_df"], reporting_month)
|
utils/data_loading.py
CHANGED
|
@@ -27,7 +27,43 @@ class DataManager:
|
|
| 27 |
def __init__(self, config: AppConfig):
|
| 28 |
self.config = config
|
| 29 |
self._data_cache = None
|
| 30 |
-
self._metadata = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
@property
|
| 33 |
def metadata(self) -> DatasetMetadata | None:
|
|
@@ -69,6 +105,26 @@ class DataManager:
|
|
| 69 |
lambda x: get_reporting_year(x, reporting_month)
|
| 70 |
)
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
downloads = prepare_downloads(raw_df)
|
| 73 |
|
| 74 |
return {
|
|
@@ -103,6 +159,14 @@ class DataManager:
|
|
| 103 |
st.cache_data.clear()
|
| 104 |
|
| 105 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
return self._load_data_internal(
|
| 107 |
reporting_month=reporting_month
|
| 108 |
if reporting_month
|
|
|
|
| 27 |
def __init__(self, config: AppConfig):
|
| 28 |
self.config = config
|
| 29 |
self._data_cache = None
|
| 30 |
+
self._metadata: DatasetMetadata | None = None
|
| 31 |
+
self._all_sectors: list[str] | None = None
|
| 32 |
+
self._all_stations: list[str] | None = None
|
| 33 |
+
self._initialize_complete_lists()
|
| 34 |
+
|
| 35 |
+
def _initialize_complete_lists(self) -> None:
|
| 36 |
+
"""Initialize complete lists of sectors and stations from raw data"""
|
| 37 |
+
try:
|
| 38 |
+
raw_df = get_raw_data(self.config.DATA_FILE_PATH)
|
| 39 |
+
|
| 40 |
+
# Handle sectors
|
| 41 |
+
sectors = raw_df["Sector"].dropna().unique().tolist()
|
| 42 |
+
self._all_sectors = sorted(sectors)
|
| 43 |
+
|
| 44 |
+
# Handle stations - convert to float first to standardize numeric format
|
| 45 |
+
stations = raw_df["Station_Number"].dropna()
|
| 46 |
+
stations = stations.astype(float).astype(str).unique().tolist()
|
| 47 |
+
self._all_stations = sorted(stations, key=lambda x: float(x))
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
st.error(f"Failed to initialize complete lists: {str(e)}")
|
| 51 |
+
self._all_sectors = []
|
| 52 |
+
self._all_stations = []
|
| 53 |
+
|
| 54 |
+
@property
|
| 55 |
+
def all_sectors(self) -> list[str]:
|
| 56 |
+
"""Get complete list of all sectors in the dataset"""
|
| 57 |
+
if self._all_sectors is None:
|
| 58 |
+
self._initialize_complete_lists()
|
| 59 |
+
return self._all_sectors if self._all_sectors is not None else []
|
| 60 |
+
|
| 61 |
+
@property
|
| 62 |
+
def all_stations(self) -> list[str]:
|
| 63 |
+
"""Get complete list of all stations in the dataset"""
|
| 64 |
+
if self._all_stations is None:
|
| 65 |
+
self._initialize_complete_lists()
|
| 66 |
+
return self._all_stations if self._all_stations is not None else []
|
| 67 |
|
| 68 |
@property
|
| 69 |
def metadata(self) -> DatasetMetadata | None:
|
|
|
|
| 105 |
lambda x: get_reporting_year(x, reporting_month)
|
| 106 |
)
|
| 107 |
|
| 108 |
+
# Apply exclusion filters if they exist in session state
|
| 109 |
+
if (
|
| 110 |
+
"persistent_excluded_sectors" in st.session_state
|
| 111 |
+
and st.session_state.persistent_excluded_sectors
|
| 112 |
+
):
|
| 113 |
+
raw_df = raw_df[
|
| 114 |
+
~raw_df["Sector"].isin(st.session_state.persistent_excluded_sectors)
|
| 115 |
+
]
|
| 116 |
+
|
| 117 |
+
if (
|
| 118 |
+
"persistent_excluded_stations" in st.session_state
|
| 119 |
+
and st.session_state.persistent_excluded_stations
|
| 120 |
+
):
|
| 121 |
+
# Convert station numbers to standardized string format for comparison
|
| 122 |
+
df_stations = raw_df["Station_Number"].astype(float).astype(str)
|
| 123 |
+
excluded_stations = [
|
| 124 |
+
str(float(s)) for s in st.session_state.persistent_excluded_stations
|
| 125 |
+
]
|
| 126 |
+
raw_df = raw_df[~df_stations.isin(excluded_stations)]
|
| 127 |
+
|
| 128 |
downloads = prepare_downloads(raw_df)
|
| 129 |
|
| 130 |
return {
|
|
|
|
| 159 |
st.cache_data.clear()
|
| 160 |
|
| 161 |
try:
|
| 162 |
+
# Ensure we have the latest exclusions
|
| 163 |
+
excluded_sectors = st.session_state.get("persistent_excluded_sectors", [])
|
| 164 |
+
excluded_stations = st.session_state.get("persistent_excluded_stations", [])
|
| 165 |
+
|
| 166 |
+
# Update session state with current exclusions
|
| 167 |
+
st.session_state.persistent_excluded_sectors = excluded_sectors
|
| 168 |
+
st.session_state.persistent_excluded_stations = excluded_stations
|
| 169 |
+
|
| 170 |
return self._load_data_internal(
|
| 171 |
reporting_month=reporting_month
|
| 172 |
if reporting_month
|
utils/session.py
CHANGED
|
@@ -39,10 +39,42 @@ def init_session_state(config: AppConfig) -> None:
|
|
| 39 |
)
|
| 40 |
st.session_state.admin_initialized = True
|
| 41 |
|
| 42 |
-
# Initialize
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
if "data_manager" not in st.session_state:
|
| 44 |
st.session_state.data_manager = DataManager(config)
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
# Initialize data and date-related variables
|
| 47 |
if "data" not in st.session_state:
|
| 48 |
metadata = st.session_state.data_manager.metadata
|
|
|
|
| 39 |
)
|
| 40 |
st.session_state.admin_initialized = True
|
| 41 |
|
| 42 |
+
# Initialize exclusion settings
|
| 43 |
+
if "exclusions_initialized" not in st.session_state:
|
| 44 |
+
# Start with default exclusions from config
|
| 45 |
+
st.session_state.update(
|
| 46 |
+
{
|
| 47 |
+
"persistent_excluded_sectors": config.DEFAULT_EXCLUDED_SECTORS,
|
| 48 |
+
"persistent_excluded_stations": config.DEFAULT_EXCLUDED_STATIONS,
|
| 49 |
+
"exclusions_initialized": True,
|
| 50 |
+
}
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Initialize data manager if not already initialized
|
| 54 |
if "data_manager" not in st.session_state:
|
| 55 |
st.session_state.data_manager = DataManager(config)
|
| 56 |
|
| 57 |
+
# Ensure excluded values are valid using complete lists from data manager
|
| 58 |
+
if "data_manager" in st.session_state:
|
| 59 |
+
# Get valid options from data manager
|
| 60 |
+
valid_sectors = set(st.session_state.data_manager.all_sectors)
|
| 61 |
+
valid_stations = set(st.session_state.data_manager.all_stations)
|
| 62 |
+
|
| 63 |
+
# Filter out invalid values
|
| 64 |
+
if "persistent_excluded_sectors" in st.session_state:
|
| 65 |
+
st.session_state.persistent_excluded_sectors = [
|
| 66 |
+
sector
|
| 67 |
+
for sector in st.session_state.persistent_excluded_sectors
|
| 68 |
+
if sector in valid_sectors
|
| 69 |
+
]
|
| 70 |
+
|
| 71 |
+
if "persistent_excluded_stations" in st.session_state:
|
| 72 |
+
st.session_state.persistent_excluded_stations = [
|
| 73 |
+
station
|
| 74 |
+
for station in st.session_state.persistent_excluded_stations
|
| 75 |
+
if station in valid_stations
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
# Initialize data and date-related variables
|
| 79 |
if "data" not in st.session_state:
|
| 80 |
metadata = st.session_state.data_manager.metadata
|