iaode 0.2.7__tar.gz → 0.2.8__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: iaode
3
- Version: 0.2.7
3
+ Version: 0.2.8
4
4
  Summary: Interpretable Autoencoder with Ordinary Differential Equations for single-cell omics analysis
5
5
  Author-email: Zeyu Fu <fuzeyu99@126.com>
6
6
  License-Expression: MIT
@@ -37,7 +37,7 @@ Usage Example:
37
37
  >>> latent = model.get_latent()
38
38
  """
39
39
 
40
- __version__ = "0.2.7"
40
+ __version__ = "0.2.8"
41
41
  __author__ = "Zeyu Fu"
42
42
 
43
43
  # Import main classes and functions
@@ -5,7 +5,7 @@ import scanpy as sc # type: ignore
5
5
  from anndata import AnnData # type: ignore
6
6
  import numpy as np
7
7
  import torch
8
- import tqdm # type: ignore
8
+ from tqdm.auto import tqdm # type: ignore
9
9
  import time
10
10
  from typing import Literal, Optional
11
11
  from scipy.stats import norm # type: ignore
@@ -183,80 +183,113 @@ class agent(Env):
183
183
  val_every: int = 5,
184
184
  early_stop: bool = True,
185
185
  ):
186
- """Train model with early stopping."""
186
+ """Train model with early stopping and clean progress tracking."""
187
187
 
188
- # Initialize resource tracking
189
188
  use_cuda = torch.cuda.is_available()
190
189
  if use_cuda:
191
190
  torch.cuda.reset_peak_memory_stats()
192
191
  start_time = time.time()
193
192
 
194
- # Main progress bar (compact)
195
- with tqdm.tqdm(total=epochs, desc="Training", position=0) as pbar:
196
- # Secondary info bar (detailed metrics)
197
- with tqdm.tqdm(total=0, position=1, bar_format='{desc}') as pbar_info:
198
-
199
- for epoch in range(epochs):
200
-
201
- train_loss = self.train_epoch()
202
-
203
- if (epoch + 1) % val_every == 0 or epoch == 0:
204
- val_loss, val_score = self.validate()
205
- val_loss = float(val_loss)
206
-
207
- if early_stop:
208
- should_stop, improved = self.check_early_stopping(val_loss, patience)
209
-
210
- # Main bar: key metrics only
211
- pbar.set_postfix({
212
- "trn": f"{train_loss:.2f}",
213
- "val": f"{val_loss:.2f}",
214
- "pat": f"{self.patience_counter}/{patience}",
215
- "↑" if improved else "↓": ""
216
- })
217
-
218
- # Info bar: all metrics
219
- pbar_info.set_description_str(
220
- f"Metrics | ARI:{val_score[0]:.3f} NMI:{val_score[1]:.3f} "
221
- f"ASW:{val_score[2]:.3f} CAL:{val_score[3]:.3f} "
222
- f"DAV:{val_score[4]:.3f} COR:{val_score[5]:.3f} | "
223
- f"Best:{self.best_val_loss:.3f}"
224
- )
225
-
226
- if should_stop:
227
- self.actual_epochs = epoch + 1
228
- pbar.write(f"\nEarly stopping at epoch {epoch + 1}")
229
- break
230
- else:
231
- pbar.set_postfix({
232
- "trn": f"{train_loss:.2f}",
233
- "val": f"{val_loss:.2f}",
234
- })
235
- pbar_info.set_description_str(
236
- f"Metrics | ARI:{val_score[0]:.3f} NMI:{val_score[1]:.3f} "
237
- f"ASW:{val_score[2]:.3f} CAL:{val_score[3]:.3f} "
238
- f"DAV:{val_score[4]:.3f} COR:{val_score[5]:.3f}"
239
- )
240
-
241
- pbar.update(1)
242
- else:
243
- self.actual_epochs = epochs
193
+ # Prepare header & separator (but do NOT print yet)
194
+ header = (
195
+ f"{'Epoch':<6} | "
196
+ f"{'Train Loss':<10} | "
197
+ f"{'Val Loss':<10} | "
198
+ f"{'ARI':<6} {'NMI':<6} {'ASW':<6} | "
199
+ f"{'CAL':<6} {'DAV':<6} {'COR':<6} | "
200
+ f"{'Patience':<9}"
201
+ )
202
+ sep_line = "-" * len(header)
203
+
204
+ # Single unified progress bar
205
+ with tqdm(total=epochs, desc="Training", unit="epoch", dynamic_ncols=True) as pbar:
206
+ # Print header via pbar.write so that header & rows stay together
207
+ pbar.write(sep_line)
208
+ pbar.write(header)
209
+ pbar.write(sep_line)
210
+
211
+ for epoch in range(epochs):
212
+
213
+ # 1. Training step
214
+ train_loss = self.train_epoch()
215
+
216
+ # 2. Validation step
217
+ if (epoch + 1) % val_every == 0 or epoch == 0:
218
+ val_loss, val_score = self.validate()
219
+ val_loss = float(val_loss)
220
+
221
+ # val_score indices:
222
+ # 0: ARI, 1: NMI, 2: ASW, 3: CAL, 4: DAV, 5: COR
223
+ ari, nmi, asw, cal, dav, cor = val_score[:6]
224
+
225
+ if early_stop:
226
+ should_stop, _ = self.check_early_stopping(val_loss, patience)
227
+ pat_status = f"{self.patience_counter}/{patience}"
228
+ else:
229
+ should_stop = False
230
+ pat_status = "N/A"
231
+
232
+ # Log line with all 6 metrics
233
+ log_msg = (
234
+ f"{epoch+1:<6} | "
235
+ f"{train_loss:<10.4f} | "
236
+ f"{val_loss:<10.4f} | "
237
+ f"{ari:<6.3f} {nmi:<6.3f} {asw:<6.3f} | "
238
+ f"{cal:<6.3f} {dav:<6.3f} {cor:<6.3f} | "
239
+ f"{pat_status:<9}"
240
+ )
241
+
242
+ # This will appear just above the bar, under the header
243
+ pbar.write(log_msg)
244
+
245
+ # Compact info on the moving bar itself
246
+ postfix = {
247
+ "trn": f"{train_loss:.3f}",
248
+ "val": f"{val_loss:.3f}",
249
+ }
250
+ if early_stop:
251
+ postfix["best"] = f"{self.best_val_loss:.3f}"
252
+ pbar.set_postfix(postfix)
253
+
254
+ if should_stop and early_stop:
255
+ self.actual_epochs = epoch + 1
256
+ pbar.write(f"\n>>> Early stopping triggered at epoch {epoch + 1}")
257
+ break
258
+
259
+ # Update the bar each epoch
260
+ pbar.update(1)
261
+ else:
262
+ self.actual_epochs = epochs
244
263
 
245
264
  # Record resource usage
246
265
  self.train_time = time.time() - start_time
247
266
  self.peak_memory_gb = torch.cuda.max_memory_allocated() / 1e9 if use_cuda else 0.0
248
267
 
249
- print(f"\n{'='*70}")
250
- print("Training Complete")
251
- print(f"{'='*70}")
252
- print(f" Epochs: {self.actual_epochs}/{epochs}")
253
- print(f" Time: {self.train_time:.2f}s ({self.train_time/self.actual_epochs:.2f}s/epoch)")
254
- print(f" Peak GPU Memory: {self.peak_memory_gb:.3f} GB")
255
- if early_stop:
256
- print(f" Best Val Loss: {self.best_val_loss:.4f}")
257
- print(f"{'='*70}\n")
268
+ # Final summary (after bar is closed, plain prints are fine)
269
+ self._print_training_summary(early_stop)
258
270
 
259
271
  return self
272
+
273
+
274
+ def _print_training_summary(self, early_stop: bool):
275
+ """Helper to print a clean final summary."""
276
+ print(f"\n{'='*60}")
277
+ print(f"{'Training Summary':^60}")
278
+ print(f"{'='*60}")
279
+
280
+ rows = [
281
+ ("Total Epochs", f"{self.actual_epochs}"),
282
+ ("Total Time", f"{self.train_time:.2f}s"),
283
+ ("Time per Epoch", f"{self.train_time/self.actual_epochs:.3f}s"),
284
+ ("Peak GPU Memory", f"{self.peak_memory_gb:.3f} GB"),
285
+ ]
286
+ if early_stop:
287
+ rows.append(("Best Val Loss", f"{self.best_val_loss:.4f}"))
288
+
289
+ for label, value in rows:
290
+ print(f" {label:<20} : {value}")
291
+ print(f"{'='*60}\n")
292
+
260
293
  # ========================================================================
261
294
  # Representation Extraction
262
295
  # ========================================================================
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: iaode
3
- Version: 0.2.7
3
+ Version: 0.2.8
4
4
  Summary: Interpretable Autoencoder with Ordinary Differential Equations for single-cell omics analysis
5
5
  Author-email: Zeyu Fu <fuzeyu99@126.com>
6
6
  License-Expression: MIT
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "iaode"
7
- version = "0.2.7"
7
+ version = "0.2.8"
8
8
  description = "Interpretable Autoencoder with Ordinary Differential Equations for single-cell omics analysis"
9
9
  readme = "README.md"
10
10
  authors = [
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes