-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.py
More file actions
312 lines (242 loc) · 8.93 KB
/
data_loader.py
File metadata and controls
312 lines (242 loc) · 8.93 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""
Data Loading Module for Weyland-Yutani Mining Ops Dashboard
Handles loading data from Google Sheets and generating demo data
"""
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from typing import Optional, Dict, Any
import re
def load_data_from_sheets(
spreadsheet_url: str,
credentials: Optional[Dict[str, Any]] = None,
sheet_name: str = "Data"
) -> pd.DataFrame:
"""
Load data from Google Sheets
Args:
spreadsheet_url: Full URL or spreadsheet ID
credentials: Service account credentials dict
sheet_name: Name of the sheet to load
Returns:
DataFrame with mining data
"""
try:
import gspread
from google.oauth2.service_account import Credentials
except ImportError:
raise ImportError(
"Please install gspread and google-auth: "
"pip install gspread google-auth"
)
# Extract spreadsheet ID from URL if needed
spreadsheet_id = extract_spreadsheet_id(spreadsheet_url)
# Setup credentials
scopes = [
'https://www.googleapis.com/auth/spreadsheets.readonly',
'https://www.googleapis.com/auth/drive.readonly'
]
if credentials:
creds = Credentials.from_service_account_info(credentials, scopes=scopes)
else:
try:
creds = Credentials.from_service_account_file(
'credentials.json',
scopes=scopes
)
except FileNotFoundError:
return load_public_sheet(spreadsheet_id, sheet_name)
client = gspread.authorize(creds)
try:
spreadsheet = client.open_by_key(spreadsheet_id)
worksheet = spreadsheet.worksheet(sheet_name)
except gspread.exceptions.SpreadsheetNotFound:
raise ValueError(f"Spreadsheet not found: {spreadsheet_id}")
except gspread.exceptions.WorksheetNotFound:
raise ValueError(f"Worksheet not found: {sheet_name}")
data = worksheet.get_all_values()
if not data:
raise ValueError("No data found in spreadsheet")
df = pd.DataFrame(data[1:], columns=data[0])
df = process_mining_data(df)
return df
def load_public_sheet(spreadsheet_id: str, sheet_name: str) -> pd.DataFrame:
"""
Load data from a publicly accessible Google Sheet via CSV export
Args:
spreadsheet_id: The spreadsheet ID
sheet_name: Name of the sheet (used to get gid)
Returns:
DataFrame with mining data
"""
urls_to_try = [
f"https://docs.google.com/spreadsheets/d/{spreadsheet_id}/gviz/tq?tqx=out:csv&sheet={sheet_name}",
f"https://docs.google.com/spreadsheets/d/{spreadsheet_id}/export?format=csv&sheet={sheet_name}",
]
for url in urls_to_try:
try:
df = pd.read_csv(url)
df = process_mining_data(df)
return df
except Exception:
continue
raise ValueError(
f"Could not load spreadsheet. Make sure it's publicly accessible "
f"or provide service account credentials."
)
def extract_spreadsheet_id(url_or_id: str) -> str:
"""
Extract spreadsheet ID from URL or return as-is if already an ID
Args:
url_or_id: Full Google Sheets URL or just the ID
Returns:
Spreadsheet ID
"""
if 'docs.google.com' in url_or_id or 'spreadsheets' in url_or_id:
patterns = [
r'/spreadsheets/d/([a-zA-Z0-9-_]+)',
r'/d/([a-zA-Z0-9-_]+)',
r'id=([a-zA-Z0-9-_]+)',
]
for pattern in patterns:
match = re.search(pattern, url_or_id)
if match:
return match.group(1)
raise ValueError(f"Could not extract spreadsheet ID from URL: {url_or_id}")
return url_or_id
def process_mining_data(df: pd.DataFrame) -> pd.DataFrame:
"""
Process raw mining data from Google Sheets
Args:
df: Raw DataFrame from Google Sheets
Returns:
Processed DataFrame with proper types
"""
df = df.copy()
df = df.dropna(how='all')
date_col = df.columns[0]
try:
df[date_col] = pd.to_datetime(df[date_col])
except Exception:
for fmt in ['%Y-%m-%d', '%m/%d/%Y', '%d/%m/%Y', '%Y/%m/%d']:
try:
df[date_col] = pd.to_datetime(df[date_col], format=fmt)
break
except Exception:
continue
for col in df.columns[1:]:
if col.lower() in ['dayofweek', 'day']:
continue
try:
if df[col].dtype == 'object':
df[col] = df[col].astype(str).str.replace(',', '.', regex=False)
df[col] = pd.to_numeric(df[col], errors='coerce')
except Exception:
pass
df = df.sort_values(date_col).reset_index(drop=True)
return df
def load_demo_data(
num_days: int = 40,
num_mines: int = 3,
start_date: Optional[datetime] = None,
seed: Optional[int] = None
) -> pd.DataFrame:
"""
Generate realistic demo mining data
Args:
num_days: Number of days to generate
num_mines: Number of mines
start_date: Starting date (default: 2099-11-02)
seed: Random seed for reproducibility
Returns:
DataFrame with demo mining data
"""
if seed is not None:
np.random.seed(seed)
if start_date is None:
start_date = datetime(2099, 11, 2)
mine_names = ['LV-426', 'Origae-6', 'Fiorina 151', 'Hadley\'s Hope', 'Fury 161', 'Prometheus'][:num_mines]
base_outputs = [1.0, 1.0, 1.0, 0.8, 0.6, 1.2][:num_mines]
day_factors = [1.0, 1.0, 1.0, 1.0, 1.0, 0.8, 0.6]
mean = 50
std_dev = 20
correlation = 0.3
daily_growth = 0.02
events = [
(8, 3, 0.4, 1.0),
(18, 1, 1.4, 0.5),
(25, 2, 0.6, 0.8),
(32, 2, 1.3, 0.7),
]
event_occurs = [np.random.random() < prob for _, _, _, prob in events]
dates = []
day_names = []
mine_data = {name: [] for name in mine_names}
prev_values = [mean] * num_mines
for day in range(num_days):
current_date = start_date + timedelta(days=day)
dates.append(current_date)
dow = current_date.weekday()
day_names.append(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'][dow])
day_factor = day_factors[dow]
trend_factor = (1 + daily_growth) ** day
event_factor = 1.0
for i, (offset, duration, factor, _) in enumerate(events):
if event_occurs[i] and offset <= day < offset + duration:
position = (day - offset) / (duration - 1) if duration > 1 else 0.5
bell = np.exp(-((position - 0.5) * 3) ** 2)
event_factor *= 1 + (factor - 1) * bell
for m, mine_name in enumerate(mine_names):
random_value = np.random.normal(mean, std_dev)
smoothed = correlation * prev_values[m] + (1 - correlation) * random_value
value = smoothed * base_outputs[m] * day_factor * trend_factor * event_factor
value = max(0.01, value)
mine_data[mine_name].append(round(value, 2))
prev_values[m] = smoothed
df = pd.DataFrame({
'Date': dates,
'DayOfWeek': day_names,
**mine_data
})
df['Total'] = df[mine_names].sum(axis=1).round(2)
return df
def validate_data(df: pd.DataFrame) -> Dict[str, Any]:
"""
Validate loaded data and return validation results
Args:
df: DataFrame to validate
Returns:
Dictionary with validation results
"""
results = {
'valid': True,
'warnings': [],
'errors': [],
'info': {}
}
if df.empty:
results['valid'] = False
results['errors'].append("DataFrame is empty")
return results
date_col = df.columns[0]
if not pd.api.types.is_datetime64_any_dtype(df[date_col]):
results['warnings'].append(f"First column '{date_col}' is not datetime type")
numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()
if len(numeric_cols) == 0:
results['valid'] = False
results['errors'].append("No numeric columns found")
return results
missing = df.isnull().sum()
if missing.any():
cols_with_missing = missing[missing > 0].to_dict()
results['warnings'].append(f"Missing values found: {cols_with_missing}")
for col in numeric_cols:
if (df[col] < 0).any():
results['warnings'].append(f"Negative values found in column '{col}'")
results['info'] = {
'rows': len(df),
'columns': len(df.columns),
'date_range': f"{df[date_col].min()} to {df[date_col].max()}" if pd.api.types.is_datetime64_any_dtype(df[date_col]) else "N/A",
'numeric_columns': numeric_cols
}
return results