-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstatistical.py
More file actions
581 lines (446 loc) · 18.2 KB
/
statistical.py
File metadata and controls
581 lines (446 loc) · 18.2 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
"""Statistical plotting functions for data analysis and visualization.
This module provides statistical visualization functions including histograms,
box plots, violin plots, Q-Q plots, correlation heatmaps, density plots,
ridge plots, ROC curves, precision-recall curves, residual plots, and leverage plots.
"""
from __future__ import annotations
from pathlib import Path
from typing import Any, Sequence
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib.axes import Axes
from metainformant.core.data import validation
from metainformant.core.io import paths
from metainformant.core.utils import logging
logger = logging.get_logger(__name__)
# Optional imports with graceful fallbacks
try:
from scipy import stats
HAS_SCIPY = True
except ImportError:
stats = None
HAS_SCIPY = False
try:
import seaborn as sns
HAS_SEABORN = True
except ImportError:
sns = None
HAS_SEABORN = False
try:
import sklearn.metrics as metrics
HAS_SKLEARN = True
except ImportError:
metrics = None
HAS_SKLEARN = False
def histogram(
data: np.ndarray, *, bins: int = 30, ax: Axes | None = None, output_path: str | Path | None = None, **kwargs
) -> Axes:
"""Create a histogram.
Args:
data: Data to plot
bins: Number of bins
ax: Optional matplotlib axes to plot on. Creates new if None.
output_path: Optional path to save the figure.
**kwargs: Additional arguments passed to matplotlib hist().
Returns:
matplotlib Axes object
Raises:
ValueError: If data is empty
"""
validation.validate_type(data, np.ndarray, "data")
if len(data) == 0:
raise ValueError("Data array cannot be empty")
if ax is None:
fig, ax = plt.subplots(figsize=kwargs.pop("figsize", (8, 6)))
ax.hist(data, bins=bins, **kwargs)
if output_path:
paths.ensure_directory(Path(output_path).parent)
plt.savefig(output_path, dpi=300, bbox_inches="tight")
logger.info(f"Histogram saved to {output_path}")
return ax
def box_plot(
data: list[np.ndarray], *, ax: Axes | None = None, output_path: str | Path | None = None, **kwargs
) -> Axes:
"""Create a box plot.
Args:
data: List of arrays to plot as box plots
ax: Optional matplotlib axes to plot on. Creates new if None.
output_path: Optional path to save the figure.
**kwargs: Additional arguments passed to matplotlib boxplot().
Returns:
matplotlib Axes object
Raises:
ValueError: If data is empty or contains invalid arrays
"""
validation.validate_type(data, list, "data")
if not data:
raise ValueError("Data list cannot be empty")
for i, arr in enumerate(data):
validation.validate_type(arr, np.ndarray, f"data[{i}]")
if len(arr) == 0:
raise ValueError(f"Data array at index {i} cannot be empty")
if ax is None:
fig, ax = plt.subplots(figsize=kwargs.pop("figsize", (8, 6)))
ax.boxplot(data, **kwargs)
if output_path:
paths.ensure_directory(Path(output_path).parent)
plt.savefig(output_path, dpi=300, bbox_inches="tight")
logger.info(f"Box plot saved to {output_path}")
return ax
def violin_plot(
data: list[np.ndarray], *, ax: Axes | None = None, output_path: str | Path | None = None, **kwargs
) -> Axes:
"""Create a violin plot.
Args:
data: List of arrays to plot as violin plots
ax: Optional matplotlib axes to plot on. Creates new if None.
output_path: Optional path to save the figure.
**kwargs: Additional arguments passed to matplotlib violinplot().
Returns:
matplotlib Axes object
Raises:
ValueError: If data is empty or contains invalid arrays
"""
validation.validate_type(data, list, "data")
if not data:
raise ValueError("Data list cannot be empty")
for i, arr in enumerate(data):
validation.validate_type(arr, np.ndarray, f"data[{i}]")
if len(arr) == 0:
raise ValueError(f"Data array at index {i} cannot be empty")
if ax is None:
fig, ax = plt.subplots(figsize=kwargs.pop("figsize", (8, 6)))
# Use seaborn if available, otherwise matplotlib
if HAS_SEABORN:
# Convert to DataFrame for seaborn
df_data = []
for i, arr in enumerate(data):
df_data.extend([(i + 1, val) for val in arr])
df = pd.DataFrame(df_data, columns=["group", "value"])
sns.violinplot(data=df, x="group", y="value", ax=ax, **kwargs)
else:
# Fallback to matplotlib boxplot with warning
logger.warning("Seaborn not available, using boxplot instead of violinplot")
ax.boxplot(data, **kwargs)
if output_path:
paths.ensure_directory(Path(output_path).parent)
plt.savefig(output_path, dpi=300, bbox_inches="tight")
logger.info(f"Violin plot saved to {output_path}")
return ax
def qq_plot(
data: np.ndarray,
*,
distribution: str = "norm",
ax: Axes | None = None,
output_path: str | Path | None = None,
**kwargs,
) -> Axes:
"""Create a Q-Q plot.
Args:
data: Data to plot
distribution: Distribution to compare against ("norm", "uniform", etc.)
ax: Optional matplotlib axes to plot on. Creates new if None.
output_path: Optional path to save the figure.
**kwargs: Additional arguments passed to matplotlib scatter().
Returns:
matplotlib Axes object
Raises:
ValueError: If data is empty or distribution is unsupported
"""
validation.validate_type(data, np.ndarray, "data")
if len(data) == 0:
raise ValueError("Data array cannot be empty")
if not HAS_SCIPY:
raise ImportError("scipy required for Q-Q plot")
if ax is None:
fig, ax = plt.subplots(figsize=kwargs.pop("figsize", (8, 6)))
# Calculate theoretical quantiles
data_sorted = np.sort(data)
n = len(data)
theoretical_quantiles = np.linspace(0.01, 0.99, n)
# Get distribution quantiles
if distribution == "norm":
sample_quantiles = stats.norm.ppf(theoretical_quantiles, loc=np.mean(data), scale=np.std(data))
elif distribution == "uniform":
sample_quantiles = stats.uniform.ppf(theoretical_quantiles, loc=np.min(data), scale=np.max(data) - np.min(data))
else:
raise ValueError(f"Unsupported distribution: {distribution}")
ax.scatter(sample_quantiles, data_sorted, **kwargs)
ax.plot(
[np.min(sample_quantiles), np.max(sample_quantiles)],
[np.min(sample_quantiles), np.max(sample_quantiles)],
"r--",
alpha=0.7,
)
ax.set_xlabel(f"Theoretical {distribution.title()} Quantiles")
ax.set_ylabel("Sample Quantiles")
if output_path:
paths.ensure_directory(Path(output_path).parent)
plt.savefig(output_path, dpi=300, bbox_inches="tight")
logger.info(f"Q-Q plot saved to {output_path}")
return ax
def correlation_heatmap(
data: pd.DataFrame, *, ax: Axes | None = None, output_path: str | Path | None = None, **kwargs
) -> Axes:
"""Create a correlation heatmap.
Args:
data: DataFrame with numeric columns
ax: Optional matplotlib axes to plot on. Creates new if None.
output_path: Optional path to save the figure.
**kwargs: Additional arguments passed to seaborn heatmap().
Returns:
matplotlib Axes object
Raises:
ValueError: If data contains no numeric columns
"""
validation.validate_type(data, pd.DataFrame, "data")
# Select only numeric columns
numeric_data = data.select_dtypes(include=[np.number])
if numeric_data.empty:
raise ValueError("DataFrame must contain numeric columns for correlation heatmap")
if ax is None:
fig, ax = plt.subplots(figsize=kwargs.pop("figsize", (10, 8)))
# Calculate correlation matrix
corr_matrix = numeric_data.corr()
# Use seaborn if available, otherwise matplotlib
if HAS_SEABORN:
# Extract specific kwargs to avoid duplicate keyword argument error
annot = kwargs.pop("annot", True)
cmap = kwargs.pop("cmap", "coolwarm")
sns.heatmap(corr_matrix, ax=ax, annot=annot, cmap=cmap, **kwargs)
else:
# Fallback to matplotlib imshow
logger.warning("Seaborn not available, using basic heatmap")
# Remove seaborn-specific kwargs that imshow doesn't understand
cmap = kwargs.pop("cmap", "coolwarm")
kwargs.pop("annot", None) # Remove annot if present
im = ax.imshow(corr_matrix.values, cmap=cmap, **kwargs)
plt.colorbar(im, ax=ax)
# Add text annotations
for i in range(len(corr_matrix)):
for j in range(len(corr_matrix)):
text = ax.text(j, i, f"{corr_matrix.iloc[i, j]:.2f}", ha="center", va="center", color="w")
if output_path:
paths.ensure_directory(Path(output_path).parent)
plt.savefig(output_path, dpi=300, bbox_inches="tight")
logger.info(f"Correlation heatmap saved to {output_path}")
return ax
def density_plot(data: np.ndarray, *, ax: Axes | None = None, output_path: str | Path | None = None, **kwargs) -> Axes:
"""Create a kernel density estimate plot.
Args:
data: Data to plot
ax: Optional matplotlib axes to plot on. Creates new if None.
output_path: Optional path to save the figure.
**kwargs: Additional arguments passed to matplotlib or seaborn.
Returns:
matplotlib Axes object
Raises:
ValueError: If data is empty
"""
validation.validate_type(data, np.ndarray, "data")
if len(data) == 0:
raise ValueError("Data array cannot be empty")
if ax is None:
fig, ax = plt.subplots(figsize=kwargs.pop("figsize", (8, 6)))
# Use seaborn if available, otherwise matplotlib histogram
if HAS_SEABORN:
sns.kdeplot(data=data, ax=ax, **kwargs)
else:
logger.warning("Seaborn not available, using histogram instead of density plot")
ax.hist(data, density=True, alpha=0.7, **kwargs)
if output_path:
paths.ensure_directory(Path(output_path).parent)
plt.savefig(output_path, dpi=300, bbox_inches="tight")
logger.info(f"Density plot saved to {output_path}")
return ax
def ridge_plot(
data: list[np.ndarray], *, ax: Axes | None = None, output_path: str | Path | None = None, **kwargs
) -> Axes:
"""Create a ridge plot (overlapping density plots).
Args:
data: List of arrays to plot as ridge plot
ax: Optional matplotlib axes to plot on. Creates new if None.
output_path: Optional path to save the figure.
**kwargs: Additional arguments passed to plotting functions.
Returns:
matplotlib Axes object
Raises:
ValueError: If data is empty or contains invalid arrays
"""
validation.validate_type(data, list, "data")
if not data:
raise ValueError("Data list cannot be empty")
for i, arr in enumerate(data):
validation.validate_type(arr, np.ndarray, f"data[{i}]")
if len(arr) == 0:
raise ValueError(f"Data array at index {i} cannot be empty")
if ax is None:
fig, ax = plt.subplots(figsize=kwargs.pop("figsize", (10, 6)))
# Simple ridge plot implementation
n_groups = len(data)
y_positions = np.linspace(0, n_groups - 1, n_groups)
for i, arr in enumerate(data):
if HAS_SEABORN:
# Use seaborn for better KDE
sns.kdeplot(data=arr, ax=ax, fill=True, alpha=0.7, label=f"Group {i+1}", **kwargs)
else:
# Fallback to histogram
ax.hist(arr, density=True, alpha=0.5, bins=20, label=f"Group {i+1}", **kwargs)
ax.legend()
if output_path:
paths.ensure_directory(Path(output_path).parent)
plt.savefig(output_path, dpi=300, bbox_inches="tight")
logger.info(f"Ridge plot saved to {output_path}")
return ax
def roc_curve(
y_true: np.ndarray, y_scores: np.ndarray, *, ax: Axes | None = None, output_path: str | Path | None = None, **kwargs
) -> Axes:
"""Create a ROC curve plot.
Args:
y_true: True binary labels
y_scores: Target scores (probabilities or confidence values)
ax: Optional matplotlib axes to plot on. Creates new if None.
output_path: Optional path to save the figure.
**kwargs: Additional arguments passed to matplotlib plot().
Returns:
matplotlib Axes object
Raises:
ValueError: If arrays have mismatched lengths or invalid values
"""
validation.validate_type(y_true, np.ndarray, "y_true")
validation.validate_type(y_scores, np.ndarray, "y_scores")
if len(y_true) != len(y_scores):
raise ValueError("y_true and y_scores must have same length")
if metrics is None:
raise ImportError("scikit-learn required for ROC curve")
if ax is None:
fig, ax = plt.subplots(figsize=kwargs.pop("figsize", (8, 6)))
fpr, tpr, _ = metrics.roc_curve(y_true, y_scores)
roc_auc = metrics.roc_auc_score(y_true, y_scores)
ax.plot(fpr, tpr, label=f"ROC curve (AUC = {roc_auc:.2f})", **kwargs)
ax.plot([0, 1], [0, 1], "k--", alpha=0.7)
ax.set_xlabel("False Positive Rate")
ax.set_ylabel("True Positive Rate")
ax.set_title("ROC Curve")
ax.legend()
if output_path:
paths.ensure_directory(Path(output_path).parent)
plt.savefig(output_path, dpi=300, bbox_inches="tight")
logger.info(f"ROC curve saved to {output_path}")
return ax
def precision_recall_curve(
y_true: np.ndarray, y_scores: np.ndarray, *, ax: Axes | None = None, output_path: str | Path | None = None, **kwargs
) -> Axes:
"""Create a precision-recall curve plot.
Args:
y_true: True binary labels
y_scores: Target scores (probabilities or confidence values)
ax: Optional matplotlib axes to plot on. Creates new if None.
output_path: Optional path to save the figure.
**kwargs: Additional arguments passed to matplotlib plot().
Returns:
matplotlib Axes object
Raises:
ValueError: If arrays have mismatched lengths or invalid values
"""
validation.validate_type(y_true, np.ndarray, "y_true")
validation.validate_type(y_scores, np.ndarray, "y_scores")
if len(y_true) != len(y_scores):
raise ValueError("y_true and y_scores must have same length")
if metrics is None:
raise ImportError("scikit-learn required for precision-recall curve")
if ax is None:
fig, ax = plt.subplots(figsize=kwargs.pop("figsize", (8, 6)))
precision, recall, _ = metrics.precision_recall_curve(y_true, y_scores)
pr_auc = metrics.average_precision_score(y_true, y_scores)
ax.plot(recall, precision, label=f"PR curve (AP = {pr_auc:.2f})", **kwargs)
ax.set_xlabel("Recall")
ax.set_ylabel("Precision")
ax.set_title("Precision-Recall Curve")
ax.legend()
if output_path:
paths.ensure_directory(Path(output_path).parent)
plt.savefig(output_path, dpi=300, bbox_inches="tight")
logger.info(f"Precision-recall curve saved to {output_path}")
return ax
def residual_plot(
y_true: np.ndarray, y_pred: np.ndarray, *, ax: Axes | None = None, output_path: str | Path | None = None, **kwargs
) -> Axes:
"""Create a residual plot.
Args:
y_true: True values
y_pred: Predicted values
ax: Optional matplotlib axes to plot on. Creates new if None.
output_path: Optional path to save the figure.
**kwargs: Additional arguments passed to matplotlib scatter().
Returns:
matplotlib Axes object
Raises:
ValueError: If arrays have mismatched lengths
"""
validation.validate_type(y_true, np.ndarray, "y_true")
validation.validate_type(y_pred, np.ndarray, "y_pred")
if len(y_true) != len(y_pred):
raise ValueError("y_true and y_pred must have same length")
if ax is None:
fig, ax = plt.subplots(figsize=kwargs.pop("figsize", (8, 6)))
residuals = y_true - y_pred
ax.scatter(y_pred, residuals, **kwargs)
ax.axhline(y=0, color="r", linestyle="--", alpha=0.7)
ax.set_xlabel("Predicted Values")
ax.set_ylabel("Residuals")
ax.set_title("Residual Plot")
if output_path:
paths.ensure_directory(Path(output_path).parent)
plt.savefig(output_path, dpi=300, bbox_inches="tight")
logger.info(f"Residual plot saved to {output_path}")
return ax
def leverage_plot(
X: np.ndarray, y: np.ndarray, *, ax: Axes | None = None, output_path: str | Path | None = None, **kwargs
) -> Axes:
"""Create a leverage plot for regression diagnostics.
Args:
X: Feature matrix
y: Target values
ax: Optional matplotlib axes to plot on. Creates new if None.
output_path: Optional path to save the figure.
**kwargs: Additional arguments passed to matplotlib scatter().
Returns:
matplotlib Axes object
Raises:
ValueError: If arrays have mismatched dimensions
ImportError: If sklearn is not available
"""
validation.validate_type(X, np.ndarray, "X")
validation.validate_type(y, np.ndarray, "y")
if len(X) != len(y):
raise ValueError("X and y must have same number of samples")
try:
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
except ImportError:
raise ImportError("scikit-learn required for leverage plot")
if ax is None:
fig, ax = plt.subplots(figsize=kwargs.pop("figsize", (8, 6)))
# Fit linear regression
model = LinearRegression()
model.fit(X, y)
y_pred = model.predict(X)
# Calculate leverage (hat values)
# For simplicity, use a basic approximation
leverage = np.sum(X**2, axis=1) / np.sum(X**2)
# Calculate standardized residuals
residuals = y - y_pred
residual_std = np.std(residuals)
standardized_residuals = residuals / residual_std
ax.scatter(leverage, standardized_residuals, **kwargs)
ax.axhline(y=0, color="r", linestyle="--", alpha=0.7)
ax.set_xlabel("Leverage")
ax.set_ylabel("Standardized Residuals")
ax.set_title("Leverage Plot")
if output_path:
paths.ensure_directory(Path(output_path).parent)
plt.savefig(output_path, dpi=300, bbox_inches="tight")
logger.info(f"Leverage plot saved to {output_path}")
return ax