direl-ts-tool-kit 0.4.9__py3-none-any.whl → 0.6.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- direl_ts_tool_kit/plot/plot_style.py +3 -0
- direl_ts_tool_kit/plot/plot_ts.py +263 -2
- {direl_ts_tool_kit-0.4.9.dist-info → direl_ts_tool_kit-0.6.0.dist-info}/METADATA +31 -1
- direl_ts_tool_kit-0.6.0.dist-info/RECORD +11 -0
- direl_ts_tool_kit-0.4.9.dist-info/RECORD +0 -11
- {direl_ts_tool_kit-0.4.9.dist-info → direl_ts_tool_kit-0.6.0.dist-info}/WHEEL +0 -0
- {direl_ts_tool_kit-0.4.9.dist-info → direl_ts_tool_kit-0.6.0.dist-info}/licenses/LICENCE +0 -0
- {direl_ts_tool_kit-0.4.9.dist-info → direl_ts_tool_kit-0.6.0.dist-info}/top_level.txt +0 -0
|
@@ -1,8 +1,17 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pandas as pd
|
|
1
3
|
from .plot_style import *
|
|
4
|
+
from scipy.stats import pearsonr
|
|
2
5
|
|
|
3
6
|
|
|
4
7
|
def plot_time_series(
|
|
5
|
-
df_ts,
|
|
8
|
+
df_ts,
|
|
9
|
+
variable,
|
|
10
|
+
units="",
|
|
11
|
+
color="BLUE_LINES",
|
|
12
|
+
time_unit="Year",
|
|
13
|
+
rot=90,
|
|
14
|
+
auto_format_label=True,
|
|
6
15
|
):
|
|
7
16
|
"""
|
|
8
17
|
Plots a time series with custom styling and dual-level grid visibility.
|
|
@@ -100,7 +109,7 @@ def plot_time_series(
|
|
|
100
109
|
if time_unit == "Day":
|
|
101
110
|
ax.xaxis.set_major_locator(mdates.DayLocator())
|
|
102
111
|
ax.xaxis.set_minor_locator(mdates.HourLocator())
|
|
103
|
-
|
|
112
|
+
|
|
104
113
|
if time_unit == "Hour":
|
|
105
114
|
ax.xaxis.set_major_locator(mdates.HourLocator())
|
|
106
115
|
ax.xaxis.set_minor_locator(mdates.MinuteLocator())
|
|
@@ -113,6 +122,159 @@ def plot_time_series(
|
|
|
113
122
|
return fig
|
|
114
123
|
|
|
115
124
|
|
|
125
|
+
def plot_interpolation_analysis(
|
|
126
|
+
df_original,
|
|
127
|
+
variable,
|
|
128
|
+
units="",
|
|
129
|
+
method="polynomial",
|
|
130
|
+
order=2,
|
|
131
|
+
imputation_se=None,
|
|
132
|
+
time_unit="Year",
|
|
133
|
+
rot=90,
|
|
134
|
+
):
|
|
135
|
+
"""
|
|
136
|
+
Performs interpolation on missing data (NaNs) in a specified column and
|
|
137
|
+
plots the result, highlighting the imputed points with confidence intervals
|
|
138
|
+
if the Imputation Standard Error (SE) is provided.
|
|
139
|
+
|
|
140
|
+
Parameters
|
|
141
|
+
----------
|
|
142
|
+
df_original : pd.DataFrame
|
|
143
|
+
The DataFrame containing the original time series data.
|
|
144
|
+
variable : str
|
|
145
|
+
The name of the column to interpolate and plot (e.g., 'LPUE').
|
|
146
|
+
units : str, optional
|
|
147
|
+
Units to display next to the variable name on the y-axis. Defaults to "".
|
|
148
|
+
method : str, optional
|
|
149
|
+
The interpolation method (e.g., 'linear', 'polynomial', 'spline').
|
|
150
|
+
Defaults to 'polynomial'.
|
|
151
|
+
order : int, optional
|
|
152
|
+
The order of the interpolation (required for 'polynomial' or 'spline').
|
|
153
|
+
Defaults to 2.
|
|
154
|
+
imputation_se : pd.Series, float, or None, optional
|
|
155
|
+
The Standard Error (SE) of the imputation. This must be a single value
|
|
156
|
+
or a Series aligned with the DataFrame's index. If None, confidence
|
|
157
|
+
intervals will NOT be plotted. Defaults to None.
|
|
158
|
+
time_unit : str, optional
|
|
159
|
+
The time granularity for x-axis tick locators. Defaults to "Year".
|
|
160
|
+
rot : int, optional
|
|
161
|
+
Rotation angle (in degrees) for the x-axis tick labels. Defaults to 90.
|
|
162
|
+
|
|
163
|
+
Returns
|
|
164
|
+
-------
|
|
165
|
+
matplotlib.figure.Figure
|
|
166
|
+
The generated Matplotlib figure object with the plot.
|
|
167
|
+
"""
|
|
168
|
+
|
|
169
|
+
imputed_mask = df_original[variable].isnull()
|
|
170
|
+
df_interpolated = df_original.copy()
|
|
171
|
+
df_interpolated[variable] = df_interpolated[variable].interpolate(
|
|
172
|
+
method=method, order=order
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
color1 = paper_colors["RED_LINES"]
|
|
176
|
+
color2 = paper_colors["GREEN_LINES"]
|
|
177
|
+
|
|
178
|
+
col = np.where(imputed_mask, color1, color2)
|
|
179
|
+
|
|
180
|
+
fig, ax = plt.subplots()
|
|
181
|
+
|
|
182
|
+
if imputation_se is not None:
|
|
183
|
+
df_imputed_only = df_interpolated.copy()
|
|
184
|
+
df_imputed_only.loc[~imputed_mask, variable] = np.nan
|
|
185
|
+
|
|
186
|
+
Z_80 = 1.282
|
|
187
|
+
Z_95 = 1.96
|
|
188
|
+
|
|
189
|
+
error_80 = Z_80 * imputation_se
|
|
190
|
+
error_95 = Z_95 * imputation_se
|
|
191
|
+
|
|
192
|
+
ax.fill_between(
|
|
193
|
+
df_imputed_only.index,
|
|
194
|
+
df_imputed_only[variable] - error_95,
|
|
195
|
+
df_imputed_only[variable] + error_95,
|
|
196
|
+
color=paper_colors["GRAY_BARS"],
|
|
197
|
+
alpha=0.2,
|
|
198
|
+
edgecolor="none",
|
|
199
|
+
label="95% confidence interval",
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
ax.fill_between(
|
|
203
|
+
df_imputed_only.index,
|
|
204
|
+
df_imputed_only[variable] - error_80,
|
|
205
|
+
df_imputed_only[variable] + error_80,
|
|
206
|
+
color=paper_colors["GRAY_BARS"],
|
|
207
|
+
alpha=0.4,
|
|
208
|
+
edgecolor="none",
|
|
209
|
+
label="80% confidence interval",
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
ax.plot(
|
|
213
|
+
df_interpolated[variable],
|
|
214
|
+
linestyle="-.",
|
|
215
|
+
linewidth=1,
|
|
216
|
+
color=paper_colors["BLUE_LINES"],
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
ax.scatter(
|
|
220
|
+
df_interpolated.index,
|
|
221
|
+
df_interpolated[variable],
|
|
222
|
+
color=col,
|
|
223
|
+
s=10,
|
|
224
|
+
linewidth=4,
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
ax.set(xlabel=f"{time_unit}", ylabel=f"{variable} {units}")
|
|
228
|
+
ax.ticklabel_format(style="sci", axis="y", scilimits=(0, 0))
|
|
229
|
+
|
|
230
|
+
if time_unit == "Year":
|
|
231
|
+
ax.xaxis.set_major_locator(mdates.YearLocator())
|
|
232
|
+
ax.xaxis.set_minor_locator(mdates.MonthLocator())
|
|
233
|
+
|
|
234
|
+
if time_unit == "Month":
|
|
235
|
+
ax.xaxis.set_major_locator(mdates.MonthLocator())
|
|
236
|
+
ax.xaxis.set_minor_locator(mdates.WeekdayLocator())
|
|
237
|
+
|
|
238
|
+
if time_unit == "Weekday":
|
|
239
|
+
ax.xaxis.set_major_locator(mdates.WeekdayLocator())
|
|
240
|
+
ax.xaxis.set_minor_locator(mdates.DayLocator())
|
|
241
|
+
|
|
242
|
+
if time_unit == "Day":
|
|
243
|
+
ax.xaxis.set_major_locator(mdates.DayLocator())
|
|
244
|
+
ax.xaxis.set_minor_locator(mdates.HourLocator())
|
|
245
|
+
|
|
246
|
+
if time_unit == "Hour":
|
|
247
|
+
ax.xaxis.set_major_locator(mdates.HourLocator())
|
|
248
|
+
ax.xaxis.set_minor_locator(mdates.MinuteLocator())
|
|
249
|
+
|
|
250
|
+
ax.tick_params(axis="x", rotation=rot)
|
|
251
|
+
ax.grid(which="both")
|
|
252
|
+
ax.grid(which="minor", alpha=0.6, linestyle=":")
|
|
253
|
+
ax.grid(which="major", alpha=0.8, linestyle="--")
|
|
254
|
+
|
|
255
|
+
legend_elements = [
|
|
256
|
+
Line2D(
|
|
257
|
+
[0],
|
|
258
|
+
[0],
|
|
259
|
+
marker="o",
|
|
260
|
+
color=color2,
|
|
261
|
+
label="Current data",
|
|
262
|
+
linestyle="none",
|
|
263
|
+
),
|
|
264
|
+
Line2D(
|
|
265
|
+
[0],
|
|
266
|
+
[0],
|
|
267
|
+
marker="o",
|
|
268
|
+
color=color1,
|
|
269
|
+
label="Imputed data",
|
|
270
|
+
linestyle="none",
|
|
271
|
+
),
|
|
272
|
+
]
|
|
273
|
+
ax.legend(handles=legend_elements, loc="upper right")
|
|
274
|
+
|
|
275
|
+
return fig
|
|
276
|
+
|
|
277
|
+
|
|
116
278
|
def save_figure(
|
|
117
279
|
fig,
|
|
118
280
|
file_name,
|
|
@@ -151,3 +313,102 @@ def save_figure(
|
|
|
151
313
|
fig.savefig(f"{base_name}.png")
|
|
152
314
|
fig.savefig(f"{base_name}.pdf")
|
|
153
315
|
fig.savefig(f"{base_name}.svg")
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def heat_map(X, y, colors="Blues"):
|
|
319
|
+
"""
|
|
320
|
+
Generates a correlation heatmap plot for a set of features and a target variable.
|
|
321
|
+
|
|
322
|
+
This function concatenates the feature DataFrame (X) and the target Series (y)
|
|
323
|
+
to compute and visualize the full pairwise correlation matrix using Seaborn.
|
|
324
|
+
|
|
325
|
+
Parameters
|
|
326
|
+
----------
|
|
327
|
+
X : pd.DataFrame
|
|
328
|
+
The DataFrame containing the feature variables.
|
|
329
|
+
y : pd.Series or pd.DataFrame
|
|
330
|
+
The target variable (must be concatenable with X).
|
|
331
|
+
colors : str or matplotlib.colors.Colormap, optional
|
|
332
|
+
The colormap to use for the heatmap, passed to the 'cmap' argument
|
|
333
|
+
in seaborn.heatmap. Defaults to "Blues".
|
|
334
|
+
|
|
335
|
+
Note: For standard correlation matrices (which include negative values),
|
|
336
|
+
a diverging colormap (e.g., "coolwarm", "vlag") is usually recommended.
|
|
337
|
+
|
|
338
|
+
Returns
|
|
339
|
+
-------
|
|
340
|
+
matplotlib.figure.Figure
|
|
341
|
+
The generated Matplotlib figure object containing the heatmap.
|
|
342
|
+
|
|
343
|
+
Notes
|
|
344
|
+
-----
|
|
345
|
+
The heatmap displays the Pearson correlation coefficient rounded to two
|
|
346
|
+
decimal places and includes annotations for improved readability.
|
|
347
|
+
"""
|
|
348
|
+
fig, ax = plt.subplots()
|
|
349
|
+
Z = pd.concat([X, y], axis=1)
|
|
350
|
+
|
|
351
|
+
ax = sns.heatmap(
|
|
352
|
+
Z.corr(),
|
|
353
|
+
cmap=colors,
|
|
354
|
+
annot=True,
|
|
355
|
+
linewidths=0.5,
|
|
356
|
+
fmt=".2f",
|
|
357
|
+
annot_kws={"size": 10},
|
|
358
|
+
)
|
|
359
|
+
|
|
360
|
+
return fig
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def corrfunc(x, y, ax=None, **kws):
|
|
364
|
+
"""Plot the correlation coefficient in the top left hand corner of a plot."""
|
|
365
|
+
r, _ = pearsonr(x, y)
|
|
366
|
+
ax = ax or plt.gca()
|
|
367
|
+
ax.annotate(f"R = {r:.2f}", xy=(0.1, 0.9), fontsize=25, xycoords=ax.transAxes)
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
def pair_plot(X, y):
|
|
371
|
+
"""
|
|
372
|
+
Generates a cornered pair plot (scatterplot matrix) to visualize relationships
|
|
373
|
+
between features and the target variable.
|
|
374
|
+
|
|
375
|
+
The function combines the feature DataFrame (X) and the target Series (y)
|
|
376
|
+
and uses seaborn.pairplot to create a matrix of scatter plots and histograms.
|
|
377
|
+
It focuses on the lower triangular part (corner=True) and includes a
|
|
378
|
+
regression line for trend visualization.
|
|
379
|
+
|
|
380
|
+
Parameters
|
|
381
|
+
----------
|
|
382
|
+
X : pd.DataFrame
|
|
383
|
+
The DataFrame containing the feature variables.
|
|
384
|
+
y : pd.Series or pd.DataFrame
|
|
385
|
+
The target variable (must be concatenable with X).
|
|
386
|
+
|
|
387
|
+
Returns
|
|
388
|
+
-------
|
|
389
|
+
matplotlib.figure.Figure
|
|
390
|
+
The generated Matplotlib Figure object containing the cornered pair plot.
|
|
391
|
+
|
|
392
|
+
Notes
|
|
393
|
+
-----
|
|
394
|
+
1. **Dependency:** This function requires a previously defined custom function
|
|
395
|
+
`corrfunc` to be available in the local namespace, as it is used via
|
|
396
|
+
`svm.map_lower()`. This custom function is typically used to display
|
|
397
|
+
correlation coefficients (e.g., Pearson's r) in the lower panel.
|
|
398
|
+
2. **Aesthetics:** Uses a regression line (`kind="reg"`) with custom color
|
|
399
|
+
(RED_LINES) to highlight linear relationships.
|
|
400
|
+
3. **Output:** The returned Figure object can be manipulated further
|
|
401
|
+
or saved using methods like `fig.savefig()`.
|
|
402
|
+
"""
|
|
403
|
+
Z = pd.concat([X, y], axis=1)
|
|
404
|
+
svm = sns.pairplot(
|
|
405
|
+
Z,
|
|
406
|
+
corner=True,
|
|
407
|
+
kind="reg",
|
|
408
|
+
plot_kws={"line_kws": {"color": paper_colors["RED_LINES"]}},
|
|
409
|
+
)
|
|
410
|
+
svm.map_lower(corrfunc)
|
|
411
|
+
|
|
412
|
+
fig = svm.fig
|
|
413
|
+
|
|
414
|
+
return fig
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: direl-ts-tool-kit
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Summary: A toolbox for time series analysis and visualization.
|
|
5
5
|
Home-page: https://gitlab.com/direl/direl_tool_kit
|
|
6
6
|
Author: Diego Restrepo-Leal
|
|
@@ -18,6 +18,8 @@ Requires-Dist: pandas>=1.0.0
|
|
|
18
18
|
Requires-Dist: numpy>=1.18.0
|
|
19
19
|
Requires-Dist: matplotlib>=3.0.0
|
|
20
20
|
Requires-Dist: openpyxl
|
|
21
|
+
Requires-Dist: seaborn
|
|
22
|
+
Requires-Dist: scipy
|
|
21
23
|
Dynamic: author
|
|
22
24
|
Dynamic: author-email
|
|
23
25
|
Dynamic: classifier
|
|
@@ -87,6 +89,14 @@ This function automatically sets major and minor time-based locators
|
|
|
87
89
|
on the x-axis based on the specified time unit, and formats the y-axis
|
|
88
90
|
to use scientific notation.
|
|
89
91
|
|
|
92
|
+
#### plot_interpolation_analysis
|
|
93
|
+
`plot_interpolation_analysis(df_original, variable, units="", method="polynomial", order=2, imputation_se=None, time_unit="Year", rot=90)`
|
|
94
|
+
|
|
95
|
+
Performs interpolation on missing data (NaNs) in a specified column and
|
|
96
|
+
plots the result, highlighting the imputed points with confidence intervals
|
|
97
|
+
if the Imputation Standard Error (SE) is provided.
|
|
98
|
+
|
|
99
|
+
|
|
90
100
|
#### save_figure
|
|
91
101
|
`save_figure(fig, file_name, variable_name="", path="./")`
|
|
92
102
|
|
|
@@ -95,6 +105,26 @@ Saves a Matplotlib figure in three common high-quality formats (PNG, PDF, SVG).
|
|
|
95
105
|
The function creates a consistent file name structure:
|
|
96
106
|
{path}/{file_name}_{variable_name}.{extension}.
|
|
97
107
|
|
|
108
|
+
#### heat_map
|
|
109
|
+
`heat_map(X, y, colors="Blues")`
|
|
110
|
+
|
|
111
|
+
Generates a correlation heatmap plot for a set of features and a target variable.
|
|
112
|
+
|
|
113
|
+
This function concatenates the feature DataFrame (X) and the target Series (y)
|
|
114
|
+
to compute and visualize the full pairwise correlation matrix using Seaborn.
|
|
115
|
+
|
|
116
|
+
#### pair_plot
|
|
117
|
+
`pair_plot(X, y)`
|
|
118
|
+
|
|
119
|
+
Generates a cornered pair plot (scatterplot matrix) to visualize relationships
|
|
120
|
+
between features and the target variable.
|
|
121
|
+
|
|
122
|
+
The function combines the feature DataFrame (X) and the target Series (y)
|
|
123
|
+
and uses seaborn.pairplot to create a matrix of scatter plots and histograms.
|
|
124
|
+
It focuses on the lower triangular part (corner=True) and includes a
|
|
125
|
+
regression line for trend visualization.
|
|
126
|
+
|
|
127
|
+
|
|
98
128
|
# Examples
|
|
99
129
|
- [Example 1](https://gitlab.com/direl/direl_tool_kit/-/blob/main/example/example_01.md?ref_type=heads)
|
|
100
130
|
- [Example 2](https://gitlab.com/direl/direl_tool_kit/-/blob/main/example/example_02.md?ref_type=heads)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
direl_ts_tool_kit/__init__.py,sha256=W99Wd3BeEFKOxT51TApURElbDJvqIjD8u_-qDoCYSJ0,94
|
|
2
|
+
direl_ts_tool_kit/plot/__init__.py,sha256=CMwyv-kiE74nwr3MJPL7gWIJmcfZ8UQCRu7mBGGQ4rI,49
|
|
3
|
+
direl_ts_tool_kit/plot/plot_style.py,sha256=5YxoLXlYvzleTnBEGPwCmHQIJ0S96KPJspq_n-qMvpw,1069
|
|
4
|
+
direl_ts_tool_kit/plot/plot_ts.py,sha256=D1doDV24eKOFD7vxZUE6QRx-kTRPPjmbxcGOnvdVjKQ,12982
|
|
5
|
+
direl_ts_tool_kit/utilities/__init__.py,sha256=jMtxYZUtwlhgI99sxe_8MMzsDnxtbTP7Ivh9tUOeIwQ,25
|
|
6
|
+
direl_ts_tool_kit/utilities/data_prep.py,sha256=k3eOwQEEd5mxy2DtT_Gdo7BhkzEmSQqvMJ89y8mH5CQ,6024
|
|
7
|
+
direl_ts_tool_kit-0.6.0.dist-info/licenses/LICENCE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
direl_ts_tool_kit-0.6.0.dist-info/METADATA,sha256=4G6DKqzMbNny1JE26bGSUuX5zpDqf-4VDuvjwgMZjLI,4883
|
|
9
|
+
direl_ts_tool_kit-0.6.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
10
|
+
direl_ts_tool_kit-0.6.0.dist-info/top_level.txt,sha256=vMCRudnGnsdRg_6fUftnG8PF2Y1m0bjBDMf3pCAp6bc,18
|
|
11
|
+
direl_ts_tool_kit-0.6.0.dist-info/RECORD,,
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
direl_ts_tool_kit/__init__.py,sha256=W99Wd3BeEFKOxT51TApURElbDJvqIjD8u_-qDoCYSJ0,94
|
|
2
|
-
direl_ts_tool_kit/plot/__init__.py,sha256=CMwyv-kiE74nwr3MJPL7gWIJmcfZ8UQCRu7mBGGQ4rI,49
|
|
3
|
-
direl_ts_tool_kit/plot/plot_style.py,sha256=vhzcDa3LzgkHuy-GnliofGZ8TDntkm3_1C5kgl2Gx3E,1010
|
|
4
|
-
direl_ts_tool_kit/plot/plot_ts.py,sha256=Fi_J-5qyDxcR-Z6sRHksn1e5QZcKqITGbpH3-bAX-Jw,5015
|
|
5
|
-
direl_ts_tool_kit/utilities/__init__.py,sha256=jMtxYZUtwlhgI99sxe_8MMzsDnxtbTP7Ivh9tUOeIwQ,25
|
|
6
|
-
direl_ts_tool_kit/utilities/data_prep.py,sha256=k3eOwQEEd5mxy2DtT_Gdo7BhkzEmSQqvMJ89y8mH5CQ,6024
|
|
7
|
-
direl_ts_tool_kit-0.4.9.dist-info/licenses/LICENCE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
-
direl_ts_tool_kit-0.4.9.dist-info/METADATA,sha256=CrRTdibENrgqBksrJVNTWR-Kt9C8uUAgpR5hwBK3bdI,3757
|
|
9
|
-
direl_ts_tool_kit-0.4.9.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
10
|
-
direl_ts_tool_kit-0.4.9.dist-info/top_level.txt,sha256=vMCRudnGnsdRg_6fUftnG8PF2Y1m0bjBDMf3pCAp6bc,18
|
|
11
|
-
direl_ts_tool_kit-0.4.9.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|