flowgem 0.1.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.
flowgem/__init__.py ADDED
@@ -0,0 +1,18 @@
1
+ # read version from installed package
2
+ from importlib.metadata import version
3
+
4
+ __version__ = version("flowgem")
5
+
6
+ # public API
7
+ from flowgem.api import FlowGEM
8
+ from flowgem.core import sample_flowgem
9
+ from flowgem.optim import opt_wb
10
+ from flowgem.bandwidth import sigma_heuristic_pairs, evaluate_sigma
11
+
12
+ __all__ = [
13
+ "FlowGEM",
14
+ "sample_flowgem",
15
+ "opt_wb",
16
+ "sigma_heuristic_pairs",
17
+ "evaluate_sigma",
18
+ ]
flowgem/api.py ADDED
@@ -0,0 +1,253 @@
1
+ """Public, class-based API for FLOWGEM.
2
+
3
+ Wraps the core sampler in a scikit-learn-style fit/generate interface. The
4
+ user gives one incomplete array with NaN as missing entries. The
5
+ observation mask is built in fit(), and a fresh initial ensemble is built on
6
+ each generate() call.
7
+ """
8
+
9
+ import numpy as np
10
+ import torch
11
+
12
+ from flowgem.core import sample_flowgem
13
+
14
+
15
+ class FlowGEM:
16
+ """Generative sampler for missing at random (MAR) data via an approximate
17
+ Wasserstein gradient flow.
18
+
19
+ The user passes one incomplete array with NaN marking the missing entries.
20
+ The observation mask and the initial particle ensemble are built
21
+ internally. The interface follows the scikit-learn fit/generate pattern.
22
+
23
+ Each generate() call draws a fresh initial ensemble, so repeated calls
24
+ return different samples. With a fixed random_state the sequence of calls
25
+ is reproducible.
26
+
27
+ Parameters
28
+ ----------
29
+ T : int, default=1000
30
+ Number of gradient-flow (time) steps.
31
+ eta : float, default=0.01
32
+ Step size of the forward-Euler update.
33
+ sigma : float, list of float, or None, default=None
34
+ Kernel bandwidth. If None, a median heuristic picks it. A float fixes
35
+ the bandwidth. A list is cross-validated over the candidates.
36
+ init : {"mice", "sample"} or array-like, default="mice"
37
+ How the initial particles are built. "mice" imputes with the
38
+ hyperimpute MICE implementation. "sample" resamples observed values
39
+ column by column. An array is used directly as the initialisation.
40
+ grad_tol : float, default=0.01
41
+ Early-stopping tolerance on the relative gradient norm.
42
+ min_iter : int, default=10
43
+ Minimum number of steps before early stopping may trigger.
44
+ dtype : torch.dtype, default=torch.float64
45
+ Numerical precision used throughout.
46
+ random_state : int or None, default=None
47
+ Seed for the sequence of generate() calls, for reproducibility.
48
+
49
+ Attributes
50
+ ----------
51
+ mask_ : torch.Tensor
52
+ Observation mask built in fit(). A 1 marks an observed entry.
53
+ X_obs_ : torch.Tensor
54
+ Observed data with missing entries filled by zero.
55
+ X_incomplete_ : torch.Tensor
56
+ The incomplete data kept from fit(), used to build a fresh initial
57
+ ensemble on each generate() call.
58
+
59
+ Examples
60
+ --------
61
+ >>> import numpy as np
62
+ >>> from flowgem import FlowGEM
63
+ >>> X = np.random.default_rng(0).standard_normal((100, 3))
64
+ >>> X[0, 1] = np.nan # mark a missing value
65
+ >>> model = FlowGEM(T=1000, random_state=0)
66
+ >>> X_complete = model.fit(X).generate()
67
+ """
68
+
69
+ def __init__(self, T=1000, eta=0.01, sigma=None, init="mice",
70
+ grad_tol=0.01, min_iter=10, dtype=torch.float64,
71
+ random_state=None):
72
+ self.T = T
73
+ self.eta = eta
74
+ self.sigma = sigma
75
+ self.init = init
76
+ self.grad_tol = grad_tol
77
+ self.min_iter = min_iter
78
+ self.dtype = dtype
79
+ self.random_state = random_state
80
+
81
+ def fit(self, X_incomplete):
82
+ """Prepare the sampler from an incomplete dataset.
83
+
84
+ Parameters
85
+ ----------
86
+ X_incomplete : array-like of shape (n_samples, d_features)
87
+ Input data with missing entries marked as NaN.
88
+
89
+ Returns
90
+ -------
91
+ self : FlowGEM
92
+ The fitted instance, so fit(...).generate() can be chained.
93
+
94
+ Raises
95
+ ------
96
+ ValueError
97
+ If the input is not 2D, has no NaN (nothing marked missing), or
98
+ has infinite values.
99
+ """
100
+ # accept numpy arrays, lists, or tensors, in the chosen dtype
101
+ X = torch.as_tensor(X_incomplete, dtype=self.dtype)
102
+
103
+ if X.ndim != 2:
104
+ raise ValueError(
105
+ f"X_incomplete must be 2D (n_samples, n_features), got {X.ndim}D"
106
+ )
107
+ if not torch.isnan(X).any():
108
+ raise ValueError(
109
+ "X_incomplete has no NaN values; nothing is marked as missing. "
110
+ "Mark missing entries with NaN."
111
+ )
112
+ if torch.isinf(X).any():
113
+ raise ValueError(
114
+ "X_incomplete contains infinite values; only finite values "
115
+ "and NaN (for missing entries) are supported."
116
+ )
117
+
118
+ # observation mask: 1 = observed, 0 = missing (matches the core code)
119
+ self.mask_ = (~torch.isnan(X)).to(self.dtype)
120
+
121
+ # observed data, NaN filled with 0 as a placeholder
122
+ # (the sampler reads only the observed entries, through the mask)
123
+ self.X_obs_ = torch.nan_to_num(X, nan=0.0)
124
+
125
+ # keep the incomplete data: the initial ensemble is rebuilt on each
126
+ # generate() call, so repeated calls give different samples
127
+ self.X_incomplete_ = X
128
+
129
+ # persistent RNG for the sequence of generate() calls. With a fixed
130
+ # random_state the calls differ but the whole sequence is reproducible.
131
+ self._rng = np.random.default_rng(self.random_state)
132
+
133
+ return self
134
+
135
+ def _build_X0(self, X, seed):
136
+ """Build an initial particle ensemble from the incomplete data.
137
+
138
+ X is the incomplete tensor (with NaN). seed makes the construction
139
+ reproducible. Returns a complete X0 tensor.
140
+ """
141
+ init = self.init
142
+
143
+ # case 1: user supplied their own initialisation array (deterministic)
144
+ if not isinstance(init, str):
145
+ X0 = torch.as_tensor(init, dtype=self.dtype)
146
+ if X0.shape != X.shape:
147
+ raise ValueError(
148
+ f"Supplied init array has shape {tuple(X0.shape)}, "
149
+ f"expected {tuple(X.shape)}"
150
+ )
151
+ return X0
152
+
153
+ # case 2: MICE (default), via the hyperimpute package
154
+ if init == "mice":
155
+ from hyperimpute.plugins.imputers import Imputers
156
+
157
+ imputer = Imputers().get("mice", random_state=seed)
158
+ X0_np = imputer.fit_transform(X.cpu().numpy())
159
+ X0_np = np.asarray(X0_np, dtype=float).copy()
160
+ return torch.as_tensor(X0_np, dtype=self.dtype)
161
+
162
+ # case 3: resample observed values column by column (the paper's simulation init)
163
+ if init == "sample":
164
+ g = torch.Generator().manual_seed(seed)
165
+
166
+ X0 = X.clone()
167
+ _, d = X.shape
168
+ for j in range(d):
169
+ col = X[:, j]
170
+ missing = torch.isnan(col)
171
+ observed = col[~missing]
172
+ n_missing = int(missing.sum())
173
+ if n_missing > 0:
174
+ if observed.numel() == 0:
175
+ raise ValueError(f"Column {j} has no observed values to sample from")
176
+ idx = torch.randint(0, observed.numel(), (n_missing,), generator=g)
177
+ X0[missing, j] = observed[idx]
178
+ return X0
179
+
180
+ raise ValueError(
181
+ f"Unknown init {init!r}; use 'mice', 'sample', or supply an array"
182
+ )
183
+
184
+ def generate(self, return_trajectory=False):
185
+ """Generate a complete sample of the same size as the fitted data.
186
+
187
+ Each call builds a new initial ensemble Repeated calls on the
188
+ same model return different samples. With a fixed random_state the
189
+ sequence of calls is reproducible.
190
+
191
+ Parameters
192
+ ----------
193
+ return_trajectory : bool
194
+ If False (default), return only the final sample, of shape (n, d).
195
+ If True, return the full list of per step snapshots.
196
+
197
+ Returns
198
+ -------
199
+ numpy.ndarray or list of numpy.ndarray
200
+ The final sample of shape (n, d), or the full trajectory if
201
+ return_trajectory is True.
202
+
203
+ Raises
204
+ ------
205
+ RuntimeError
206
+ If called before fit().
207
+
208
+ Notes
209
+ -----
210
+ With init="mice" each call re runs MICE, which can be slow.
211
+ """
212
+ if not hasattr(self, "X_incomplete_"):
213
+ raise RuntimeError("Call fit(...) before generate().")
214
+
215
+ # fresh per-call seed drawn from the persistent RNG
216
+ call_seed = int(self._rng.integers(0, 2**31 - 1))
217
+
218
+ # sigma handling: None -> heuristic (sigma_fix=None, sigma_vals=None)
219
+ # float -> fixed
220
+ # list -> cross-validation over candidates
221
+ sigma_fix = None
222
+ sigma_vals = None
223
+ if isinstance(self.sigma, (int, float)):
224
+ sigma_fix = float(self.sigma)
225
+ elif self.sigma is not None:
226
+ sigma_vals = self.sigma
227
+
228
+ # build a fresh initial ensemble for this call, and seed torch so the
229
+ # bandwidth heuristic is reproducible. Save and restore the global RNG
230
+ # state so the user's other torch code is not disturbed.
231
+ rng_state = torch.random.get_rng_state()
232
+ try:
233
+ torch.manual_seed(call_seed)
234
+ X0 = self._build_X0(self.X_incomplete_, call_seed)
235
+ Xhats = sample_flowgem(
236
+ X0,
237
+ self.X_obs_,
238
+ self.mask_,
239
+ T=self.T,
240
+ eta=self.eta,
241
+ grad_tol=self.grad_tol,
242
+ min_iter=self.min_iter,
243
+ sigma_fix=sigma_fix,
244
+ sigma_vals=sigma_vals,
245
+ dtype=self.dtype,
246
+ )
247
+ finally:
248
+ torch.random.set_rng_state(rng_state)
249
+
250
+ # return the full trajectory or just the final snapshot (as numpy arrays)
251
+ if return_trajectory:
252
+ return [np.asarray(x) for x in Xhats]
253
+ return np.asarray(Xhats[-1])
flowgem/bandwidth.py ADDED
@@ -0,0 +1,48 @@
1
+ """Bandwidth (sigma) selection utilities for FLOWGEM.
2
+
3
+ Everything related to choosing the RBF kernel bandwidth sigma can be found here.
4
+ It includes computing pairwise distances, median heuristic for a quick data-adaptive
5
+ estimate, and CV (cross validation) scoring of candidate sigma.
6
+ These functions are internally used by :class:`flowgem.FlowGEM`. you do not need to call them directly.
7
+ """
8
+
9
+ import torch
10
+
11
+ from flowgem.optim import opt_wb
12
+
13
+
14
+ def comp_pairwise_dist(x, y):
15
+ # in case x and y are not vectors
16
+ x = x.view(x.shape[0], -1)
17
+ y = y.view(y.shape[0], -1)
18
+
19
+ t1 = torch.tile(torch.sum(x**2, dim=1, keepdim=True), (1, y.shape[0]))
20
+ t2 = -2 * torch.matmul(x, y.T)
21
+ t3 = torch.tile(torch.sum(y**2, dim=1, keepdim=True).T, (x.shape[0], 1))
22
+
23
+ return t1 + t2 + t3
24
+
25
+
26
+ def sigma_heuristic_pairs(X, num_pairs=1000000):
27
+ n = X.shape[0]
28
+
29
+ i = torch.randint(0, n, (num_pairs,))
30
+ j = torch.randint(0, n, (num_pairs,))
31
+
32
+ dvals = torch.norm(X[i] - X[j], dim=1)
33
+
34
+ return (0.5 * dvals.median()).sqrt().item()
35
+
36
+
37
+ def evaluate_sigma(xpi, xrho, sigma, splits_pi, splits_rho):
38
+ # See thesis: intentional discrepancy with paper's Remark 1; preserved as-is.
39
+ psicon = lambda d: 0.5*d*d + d
40
+
41
+ obj = 0
42
+
43
+ for (train_pi, val_pi), (train_rho, val_rho) in zip(splits_pi, splits_rho):
44
+ w, b = opt_wb(xpi[train_pi, :], xrho[train_rho, :], torch.cat([xpi[val_pi, :], xrho[val_rho, :]], dim=0), sigma)
45
+ obj += torch.mean((w[:len(val_pi), :] * xpi[val_pi, :]).sum(dim=1) + b[:len(val_pi)].T).item()
46
+ obj -= torch.mean(psicon((w[len(val_pi):, :] * xrho[val_rho, :]).sum(dim=1) + b[len(val_pi):].T)).item()
47
+
48
+ return obj
flowgem/core.py ADDED
@@ -0,0 +1,140 @@
1
+ """Core FLOWGEM sampler: the discretised Wasserstein gradient flow.
2
+
3
+ This module performs the whole method. Extracting missingness patterns,
4
+ selecting bandwidths, and evolving the particle ensemble over time as well as using the
5
+ solver from `optim` and the bandwidth utilities from `bandwidth`.
6
+ """
7
+
8
+ import logging
9
+ import os
10
+
11
+ import numpy as np
12
+ import torch
13
+ from joblib import Parallel, delayed
14
+ from sklearn.model_selection import KFold
15
+ from tqdm.auto import tqdm
16
+
17
+ from flowgem.optim import opt_wb
18
+ from flowgem.bandwidth import sigma_heuristic_pairs, evaluate_sigma
19
+
20
+ logger = logging.getLogger(__name__)
21
+
22
+ def sample_flowgem(X0, X_obs, M, T=1000, eta=0.01, grad_tol=0.01, min_iter=10, sigma_fix=None, sigma_vals=None, cv_every=10, dtype=torch.float64):
23
+ """Evolve a particle cloud along the approximate Wasserstein gradient flow.
24
+
25
+ This is the core sampler. It first extracts the distinct missingness patterns.
26
+ Then it selects the kernel bandwidth, and iteratively updates the particles using
27
+ local linear velocity estimate on each pattern. It does so until the final
28
+ time ``T`` or early stopping is reached.
29
+
30
+ Parameters
31
+ ----------
32
+ X0 : torch.Tensor of shape (n, d)
33
+ Initial particle ensemble/cloud (a complete array, no NaN).
34
+ X_obs : torch.Tensor of shape (n, d)
35
+ Observed data, with missing entries filled by zero.
36
+ M : torch.Tensor of shape (n, d)
37
+ Observation mask, 1 marks an observed entry, 0 as a missing one.
38
+ T : int, default=1000
39
+ Maximum number of gradient flow steps.
40
+ eta : float, default=0.01
41
+ Step size of the forward Euler update.
42
+ grad_tol : float, default=0.01
43
+ Early stopping tolerance on the relative gradient norm.
44
+ min_iter : int, default=10
45
+ Minimum number of steps before early stopping may trigger.
46
+ sigma_fix : float or None, default=None
47
+ Fixed kernel bandwidth. If None, a median heuristic is used (unless
48
+ ``sigma_vals`` is given).
49
+ sigma_vals : list of float or None, default=None
50
+ Candidate bandwidths for cross validation. If given, the bandwidth is
51
+ re-selected by CV, every ``cv_every`` steps.
52
+ cv_every : int, default=10
53
+ How often (in steps) to re run bandwidth cross validation.
54
+ dtype : torch.dtype, default=torch.float64
55
+ Numerical precision used throughout.
56
+
57
+ Returns
58
+ -------
59
+ list of numpy.ndarray
60
+ The snapshots of the particle ensemble at every step. The last element is
61
+ the final generated sample.
62
+ """
63
+ # device and dtype are taken from / controlled by the caller,
64
+ ...
65
+ device = X0.device
66
+
67
+ Xt = X0.clone().to(dtype = dtype, device = device)
68
+ M = M.clone().to(dtype = dtype, device = device)
69
+ X = X_obs.to(dtype = dtype, device = device)
70
+
71
+ # determine all possible missingness patterns M
72
+ unique_M, inverse = torch.unique(M, dim=0, return_inverse=True)
73
+
74
+ # precompute observed indices for each m (improves computation efficiency)
75
+ mask_idx = {tuple(m.tolist()): (m == 1).nonzero(as_tuple=True)[0] for m in unique_M}
76
+
77
+ # samples drawn from p, i.e. conditional distribution X^(m)|M=m for different values of m
78
+ zpi = {m: X[inverse == i][:, mask_idx[m]].clone().to(dtype = dtype, device = device) for i,m in enumerate(mask_idx)}
79
+
80
+ # prepare cross validation for sigma
81
+ if sigma_vals is not None:
82
+ n_jobs = min(len(sigma_vals), os.cpu_count()-1)
83
+ kf = KFold(n_splits=3, shuffle=True, random_state=42)
84
+ splits_pi = {m: list(kf.split(zpi[m])) for m in mask_idx}
85
+ splits_rho = list(kf.split(Xt))
86
+
87
+ grads = {}
88
+
89
+ # store current sigma for each pattern m
90
+ if sigma_fix is None:
91
+ sigma_heur = sigma_heuristic_pairs(Xt)
92
+ sigma_current = {m: sigma_heur for m in mask_idx}
93
+ logger.info("Heuristic bandwidth sigma: %s", sigma_heur)
94
+ else:
95
+ sigma_current = {m: sigma_fix for m in mask_idx}
96
+
97
+ mean_grad_prev = torch.inf
98
+
99
+ Xhats = []
100
+ for t in tqdm(range(T), desc="WGF iterations"):
101
+
102
+ run_cv = sigma_vals is not None and t % cv_every == 0
103
+
104
+ grad_sum = 0
105
+ for m in mask_idx:
106
+ if run_cv:
107
+ # choose sigma by cross validation (see App. I.1 in MIRI paper)
108
+ logger.debug("Running CV: pattern=%s, size=%s", m, zpi[m].shape[0])
109
+ logger.debug("Candidate sigmas: %s", sigma_vals)
110
+ sigma_scores = Parallel(n_jobs=n_jobs)(
111
+ delayed(evaluate_sigma)(zpi[m], Xt[:, mask_idx[m]], sig, splits_pi[m], splits_rho)
112
+ for sig in sigma_vals
113
+ )
114
+ sigma_current[m] = sigma_vals[np.argmax(sigma_scores)]
115
+ logger.debug("Chosen sigma: %s", sigma_current[m])
116
+
117
+ # optimize (w,b) according to the objective in equation (8)
118
+ grads[m] = opt_wb(zpi[m], Xt[:, mask_idx[m]], Xt[:, mask_idx[m]], sigma_current[m], dtype=dtype)[0]
119
+
120
+ # enlarge grad to R^d
121
+ grad_full = torch.zeros_like(Xt)
122
+ grad_full.index_copy_(1, mask_idx[m], grads[m])
123
+ grad_sum += (zpi[m].shape[0]/X.shape[0]) * grad_full
124
+
125
+ # update all particles according to WGF
126
+ Xt = Xt + eta * grad_sum
127
+
128
+ Xhats.append(Xt.clone().cpu().numpy())
129
+
130
+ # early stopping and eta halving dependent on mean grad value
131
+ mean_grad = (torch.mean(torch.norm(grad_sum, dim=1)) / torch.mean(torch.norm(Xt, dim=1))).item()
132
+ if mean_grad < grad_tol and t+1 > min_iter:
133
+ logger.info("Stopped early after %d iterations (mean grad %.4f < grad_tol %s).", t+1, mean_grad, grad_tol)
134
+ break
135
+ if mean_grad > mean_grad_prev:
136
+ eta = eta*0.5
137
+ logger.info("Mean gradient increased; halving step size eta to %s.", eta)
138
+ mean_grad_prev = mean_grad
139
+
140
+ return Xhats
flowgem/optim.py ADDED
@@ -0,0 +1,77 @@
1
+ """Local linear system solver for the FLOWGEM velocity field.
2
+
3
+ This module is used internally by :class:`flowgem.FlowGEM`. It computes transport
4
+ velocity at each gradient-flow step. You normally do not need to call it directly.
5
+ """
6
+
7
+ import torch
8
+
9
+
10
+ def opt_wb(xpi, xrho, x, sigma, block_size=1000, dtype=torch.float64):
11
+ # cast inputs to the requested dtype so all internal computations are consistent
12
+ xpi = xpi.to(dtype)
13
+ xrho = xrho.to(dtype)
14
+ x = x.to(dtype)
15
+
16
+ n, d = x.shape
17
+ n_pi = xpi.shape[0]
18
+ n_rho = xrho.shape[0]
19
+
20
+ device = x.device
21
+
22
+ w = torch.empty(n, d, device=device, dtype=dtype)
23
+ b = torch.empty(n, 1, device=device, dtype=dtype)
24
+
25
+ xpi_norm = (xpi**2).sum(dim=1, keepdim=True).T # (1,n_pi)
26
+ xrho_norm = (xrho**2).sum(dim=1, keepdim=True).T # (1,n_rho)
27
+
28
+ # solve systems blockwise to ensure memory usage linear in sample size
29
+ # (otherwise it might crash for large n)
30
+ for start in range(0, n, block_size):
31
+ end = min(start + block_size, n)
32
+ xb = x[start:end]
33
+ bs = xb.shape[0]
34
+
35
+ xb_norm = (xb**2).sum(dim=1, keepdim=True)
36
+
37
+ # ---------- xpi kernel block ----------
38
+ dist2_pi = xb_norm - 2 * xb @ xpi.T + xpi_norm
39
+ kpix = torch.exp(-dist2_pi / (2 * sigma**2))
40
+
41
+ kpix_mean = kpix.mean(dim=1, keepdim=True)
42
+ Xpikpix = (kpix @ xpi) / n_pi
43
+
44
+ # ---------- xrho kernel block ----------
45
+ dist2_rho = xb_norm - 2 * xb @ xrho.T + xrho_norm
46
+ krhox = torch.exp(-dist2_rho / (2 * sigma**2))
47
+
48
+ krhox_mean = krhox.mean(dim=1, keepdim=True)
49
+ Xrhokrhox = (krhox @ xrho) / n_rho
50
+
51
+ # ---------- build c ----------
52
+ c1 = Xpikpix - Xrhokrhox
53
+ c2 = kpix_mean - krhox_mean
54
+ c = torch.cat([c1, c2], dim=1)
55
+
56
+ # ---------- build A ----------
57
+ ## A11 = 1/n_rho sum_{i,j} k(x_i,xrho_j) xrho_j xrho_j^T
58
+ A11 = torch.einsum('bk,kd,ke->bde', krhox, xrho, xrho) / n_rho
59
+ A12 = Xrhokrhox
60
+ A22 = krhox_mean.squeeze(1)
61
+
62
+ A = torch.zeros(bs, d+1, d+1, dtype=dtype, device=device)
63
+ A[:, :d, :d] = A11
64
+ A[:, :d, d] = A12
65
+ A[:, d, :d] = A12
66
+ A[:, d, d] = A22
67
+
68
+ # ---------- solve ----------
69
+ # regularize to avoid (near-)singular system matrices A
70
+ eps = 1e-5
71
+ A += eps * torch.eye(d+1, device=A.device)
72
+ res = torch.linalg.solve(A, c)
73
+
74
+ w[start:end] = res[:, :d]
75
+ b[start:end] = res[:, d:]
76
+
77
+ return w, b
@@ -0,0 +1,116 @@
1
+ Metadata-Version: 2.5
2
+ Name: flowgem
3
+ Version: 0.1.0
4
+ Summary: Nonparametric generative sampling for MAR missing data via approximate Wasserstein gradient flows
5
+ Project-URL: Homepage, https://github.com/gkremling/FLOWGEM-package
6
+ Project-URL: Repository, https://github.com/gkremling/FLOWGEM-package
7
+ Project-URL: Documentation, https://flowgem.readthedocs.io
8
+ Author: Muhammad Haris Mumtaz
9
+ Author-email: Gitte Kremling <gitte.kremling@uni-hamburg.de>
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Requires-Python: >=3.12
13
+ Requires-Dist: hyperimpute>=0.1.17
14
+ Requires-Dist: joblib>=1.5.3
15
+ Requires-Dist: numpy>=2.5.1
16
+ Requires-Dist: scikit-learn>=1.9.0
17
+ Requires-Dist: torch>=2.13.0
18
+ Requires-Dist: tqdm>=4.68.4
19
+ Description-Content-Type: text/markdown
20
+
21
+ # FLOWGEM
22
+
23
+ **Generative sampling for missing data under MAR, via approximate Wasserstein gradient flows.**
24
+
25
+ [![Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen)](https://github.com/gkremling/FLOWGEM-package)
26
+ [![Python](https://img.shields.io/badge/python-3.12-blue)](https://www.python.org/)
27
+ [![License](https://img.shields.io/badge/license-MIT-green)](https://github.com/gkremling/FLOWGEM-package/blob/main/LICENSE)
28
+
29
+ [![Tests](https://github.com/gkremling/FLOWGEM-package/actions/workflows/ci.yml/badge.svg)](https://github.com/gkremling/FLOWGEM-package/actions)
30
+
31
+ ## What is FLOWGEM?
32
+
33
+ FLOWGEM receives a dataset with missing values and generates a complete dataset sampled from the same distribution. It recovers the complete distribution of the data while preserving variances and correlations which keeps downstream analyses like means, quantiles, and correlations unbiased.
34
+
35
+ It targets data which is **Missing at Random (MAR)**, including non-monotone MAR. It is nonparametric and makes no assumption about the shape of your data. Under the hood it uses approximate Wasserstein gradient flow which iteratively transports a particle ensemble towards target distribution.
36
+
37
+ > **Note:** FLOWGEM currently supports **continuous features only**.
38
+ > Support for categorical and mixed type data is future work.
39
+
40
+ ## Installation
41
+
42
+ Install the released version from PyPI:
43
+
44
+ ```bash
45
+ pip install flowgem
46
+ ```
47
+
48
+ Or install the latest development version from GitHub:
49
+
50
+ ```bash
51
+ pip install git+https://github.com/gkremling/FLOWGEM-package.git
52
+ ```
53
+
54
+ ## Quick start
55
+
56
+ ```python
57
+ import numpy as np
58
+ from flowgem import FlowGEM
59
+
60
+ # Your data, with missing values marked as NaN
61
+ X = np.array([
62
+ [1.2, 0.8, 3.1],
63
+ [0.5, np.nan, 2.7],
64
+ [np.nan, 1.4, 2.9],
65
+ [2.1, 0.3, np.nan],
66
+ ])
67
+
68
+ # Fit and generate a complete sample from the same distribution
69
+ model = FlowGEM(T=1000)
70
+ X_complete = model.fit(X).generate()
71
+ ```
72
+
73
+ `X_complete` is a fully observed array (without missing value) which is drawn from estimated data distribution.
74
+
75
+ ## Features
76
+
77
+ - **Generative**:- generate entire dataset from the full data distribution while preserving variances and correlations.
78
+ - **Handles non-monotone MAR**:- works with arbitrary, scattered missingness patterns.
79
+ - **Nonparametric** :- No assumption about the shape of your data.
80
+ - **scikit-learn-style API**:- familiar `fit` / `generate` interface.
81
+ - **Tuning-lean**:- sensible defaults; bandwidth is set from a single heuristic
82
+
83
+ ## Key parameters
84
+
85
+ | Parameter | What it does | Default |
86
+ |-----------|--------------|---------|
87
+ | `T` | number of gradient-flow steps (more = better, slower) | `1000` |
88
+ | `eta` | step size η (smaller = finer, slower) | `0.01` |
89
+ | `sigma` | kernel bandwidth (`None` → chosen by heuristic) | `None` |
90
+
91
+ ## Documentation
92
+
93
+ Comprehensive and detailed documentation is available at [Read the Docs](https://flowgem.readthedocs.io).
94
+
95
+ ## Citation
96
+
97
+ The FLOWGEM method is based on:
98
+
99
+ > Kremling, G., Näf, J., & Lederer, J. (2026). *Generative Modeling under
100
+ > Non-Monotonic MAR Missingness via Approximate Wasserstein Gradient Flows.*
101
+
102
+ While using the package in course of your research, please cite this paper:
103
+
104
+ ```bibtex
105
+ @article{kremling2026flowgem,
106
+ title = {Generative Modeling under Non-Monotonic MAR Missingness
107
+ via Approximate Wasserstein Gradient Flows},
108
+ author = {Kremling, Gitte and N\"af, Jeffrey and Lederer, Johannes},
109
+ journal = {arXiv preprint arXiv:2604.04567},
110
+ year = {2026}
111
+ }
112
+ ```
113
+
114
+ ## License
115
+
116
+ This project is licensed under the MIT License — see the [LICENSE](https://github.com/gkremling/FLOWGEM-package/blob/main/LICENSE) file.
@@ -0,0 +1,9 @@
1
+ flowgem/__init__.py,sha256=JOa4f3f5KWX31pz0SeXZPKDsG_sbMDQVXCdRW9c1cXU,414
2
+ flowgem/api.py,sha256=lr-OOq3ieWVH8NvDjTuECyHrC_eCIV48fPwFnfj9Egk,9557
3
+ flowgem/bandwidth.py,sha256=bCHVcAi4ZrUgQKJrHNNxRnLwDjJtlywHtJ2AADeu5Ko,1660
4
+ flowgem/core.py,sha256=n_LmuktsMEwTZeCE5Efbl0GCpmpHXtUIMWCN1Hv4ofk,5819
5
+ flowgem/optim.py,sha256=qK1txg294R4J68-dHZLSD66U60t00fj2afBu90gDgNE,2476
6
+ flowgem-0.1.0.dist-info/METADATA,sha256=14MeGJLOUncnIM_NgF-gAFuTDbkApIKgE1JpkshUFAY,4391
7
+ flowgem-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
8
+ flowgem-0.1.0.dist-info/licenses/LICENSE,sha256=f8Hkr6vJgvmoz3XQpgGvGUefpTPpfXSkpcD7UGdZ1GA,1096
9
+ flowgem-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026, Muhammad Haris Mumtaz, Gitte Kremling
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+