Skip to content

Commit

Permalink
ImputerRpcaPcp and ImputerRpcaNoisy patched
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Roussel authored and Julien Roussel committed Jul 8, 2024
1 parent f94aafd commit fe411a7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
21 changes: 21 additions & 0 deletions examples/RPCA.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ from qolmat.imputations.rpca import rpca_utils
from qolmat.utils.data import generate_artificial_ts
```

```python
from qolmat.imputations.imputers import ImputerRpcaNoisy, ImputerRpcaPcp
```

**Generate synthetic data**

```python tags=[]
Expand All @@ -46,16 +50,33 @@ amp_noise = 0.1
X_true, A_true, E_true = generate_artificial_ts(n_samples, periods, amp_anomalies, ratio_anomalies, amp_noise)

signal = X_true + A_true + E_true
signal = 10 + signal * 40

# Adding missing data
signal[120:180] = np.nan
signal[:20] = np.nan
for i in range(10):
signal[i::365] = np.nan
# signal[80:220] = np.nan
# mask = np.random.choice(len(signal), round(len(signal) / 20))
# signal[mask] = np.nan

```

```python
import pandas as pd
df = pd.DataFrame({"signal": signal})
irn = ImputerRpcaPcp(period=100)
df_imp = irn.fit_transform(df)
```

```python
plt.plot(df_imp["signal"])
plt.plot(df["signal"])

plt.xlim(0, 200)
```

```python tags=[]
fig = plt.figure(figsize=(15, 8))
ax = fig.add_subplot(4, 1, 1)
Expand Down
6 changes: 1 addition & 5 deletions qolmat/imputations/imputers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1515,13 +1515,10 @@ def _transform_element(
D_scale = (D - means) / stds
M, A = model.decompose(D_scale, Omega)
M = M * stds + means
A = A * stds + means

M_final = utils.get_shape_original(M, X.shape)
A_final = utils.get_shape_original(A, X.shape)
X_imputed = M_final + A_final

df_imputed = pd.DataFrame(X_imputed, index=df.index, columns=df.columns)
df_imputed = pd.DataFrame(M_final, index=df.index, columns=df.columns)
df_imputed = df.where(~df.isna(), df_imputed)

return df_imputed
Expand Down Expand Up @@ -1705,7 +1702,6 @@ def _transform_element(
D_scale = (D - means) / stds
M, A = model.decompose_on_basis(D_scale, Omega, Q)
M = M * stds + means
A = A * stds + means

M_final = utils.get_shape_original(M, X.shape)

Expand Down

0 comments on commit fe411a7

Please sign in to comment.