gotmap-benchmark 0.2.2__tar.gz → 0.2.3__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: gotmap-benchmark
3
- Version: 0.2.2
3
+ Version: 0.2.3
4
4
  Summary: Benchmark pipeline for gotmap (query-to-reference atlas mapping) vs baselines.
5
5
  Author: gotmap developers
6
6
  License: MIT
@@ -17,4 +17,4 @@ __all__ = [
17
17
  "split_reference_query",
18
18
  ]
19
19
 
20
- __version__ = "0.2.2"
20
+ __version__ = "0.2.3"
@@ -123,6 +123,10 @@ def run_gotmap_nn(ref, query, use_rep="X_pca", label_key=None,
123
123
  mapper.fit(query)
124
124
 
125
125
  projected = mapper.transform(query, method="nn", hidden_dim=hidden_dim, epochs=epochs)
126
+ # ensure the canonical key 'X_mapped' exists (gotmap writes X_gotmap / X_mapped)
127
+ if "X_mapped" not in projected.obsm:
128
+ projected.obsm["X_mapped"] = projected.obsm.get(
129
+ "X_gotmap", projected.X if projected.X is not None else projected.obsm["X_pca"])
126
130
  out = {
127
131
  "query_mapped": projected,
128
132
  "method": "gotmap_nn",
@@ -61,15 +61,29 @@ def evaluate_mapping(ref, query, result, use_rep="X_pca", label_key=None,
61
61
  mapped = result["query_mapped"]
62
62
  mapped_coords = mapped.obsm["X_mapped"]
63
63
  ref_coords = ref.obsm[use_rep]
64
- orig_query = query.obsm[use_rep][mapped.obs_names.argsort()]
64
+ n_query = query.n_obs
65
+
66
+ # robust alignment: if mapped.obs_names is a permutation of query.obs_names,
67
+ # use it to index; otherwise fall back to positional alignment (assumes the
68
+ # method preserved the original cell order, which gotmap and most wrappers do).
69
+ mapped_names = list(mapped.obs_names)
70
+ query_names = list(query.obs_names)
71
+ aligned_by_name = (
72
+ len(mapped_names) == n_query
73
+ and set(mapped_names) == set(query_names)
74
+ )
65
75
 
66
76
  metrics = {}
67
77
 
68
78
  # 1. Label transfer
69
79
  if label_key and label_key in ref.obs.columns and "predicted_labels" in result:
70
- y_true = query.obs[label_key].reindex(mapped.obs_names).to_numpy(str)
71
80
  y_pred = np.asarray(result["predicted_labels"]).astype(str)
72
- if len(y_true) == len(y_pred):
81
+ if aligned_by_name:
82
+ y_true = query.obs[label_key].reindex(mapped_names).to_numpy(str)
83
+ else:
84
+ # positional: mapped coords are already 1:1 with query cells
85
+ y_true = query.obs[label_key].to_numpy(str)[:len(y_pred)]
86
+ if len(y_true) == len(y_pred) and len(y_true) > 0:
73
87
  metrics["label_transfer_accuracy"] = float(accuracy_score(y_true, y_pred))
74
88
  metrics["weighted_f1"] = float(f1_score(y_true, y_pred,
75
89
  average="weighted", zero_division=0))
@@ -80,10 +94,14 @@ def evaluate_mapping(ref, query, result, use_rep="X_pca", label_key=None,
80
94
  metrics["label_transfer_accuracy"] = np.nan
81
95
  metrics["weighted_f1"] = np.nan
82
96
 
83
- # 2. kNN preservation
97
+ # 2. kNN preservation: needs original query coords aligned to mapped coords
84
98
  try:
99
+ if aligned_by_name:
100
+ orig_query = query.obsm[use_rep][query.obs_names.get_indexer(mapped_names)]
101
+ else:
102
+ orig_query = query.obsm[use_rep][:len(mapped_coords)]
85
103
  metrics["knn_preservation"] = compute_knn_preservation(
86
- query.obsm[use_rep], mapped_coords, ref_coords, k=k)
104
+ orig_query, mapped_coords, ref_coords, k=k)
87
105
  except Exception as e:
88
106
  if verbose:
89
107
  print(f" kNN preservation failed: {e}")
@@ -91,7 +109,10 @@ def evaluate_mapping(ref, query, result, use_rep="X_pca", label_key=None,
91
109
 
92
110
  # 3. Cell-type silhouette in mapped space
93
111
  if label_key and label_key in query.obs.columns:
94
- labs = query.obs[label_key].reindex(mapped.obs_names).to_numpy(str)
112
+ if aligned_by_name:
113
+ labs = query.obs[label_key].reindex(mapped_names).to_numpy(str)
114
+ else:
115
+ labs = query.obs[label_key].to_numpy(str)[:len(mapped_coords)]
95
116
  if len(np.unique(labs)) > 1 and len(labs) <= 20000:
96
117
  sample = min(2000, len(labs))
97
118
  idx = np.random.RandomState(42).choice(len(labs), sample, replace=False)
@@ -65,6 +65,7 @@ class BenchmarkPipeline:
65
65
  print(f"\n{'='*60}\n[benchmark] running: {method}\n{'='*60}")
66
66
 
67
67
  t0 = time.time()
68
+ result = None
68
69
  try:
69
70
  result = METHODS[method](
70
71
  self.ref_adata, self.query_adata,
@@ -78,6 +79,15 @@ class BenchmarkPipeline:
78
79
  traceback.print_exc()
79
80
  continue
80
81
 
82
+ # canonicalize: downstream metrics expect obsm['X_mapped']
83
+ _mapped = result.get("query_mapped")
84
+ if _mapped is not None and "X_mapped" not in _mapped.obsm:
85
+ _mapped.obsm["X_mapped"] = _mapped.obsm.get(
86
+ "X_gotmap",
87
+ _mapped.X if _mapped.X is not None else None)
88
+ if _mapped is None:
89
+ continue
90
+
81
91
  elapsed = time.time() - t0
82
92
  metrics = evaluate_mapping(
83
93
  self.ref_adata, self.query_adata, result,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: gotmap-benchmark
3
- Version: 0.2.2
3
+ Version: 0.2.3
4
4
  Summary: Benchmark pipeline for gotmap (query-to-reference atlas mapping) vs baselines.
5
5
  Author: gotmap developers
6
6
  License: MIT
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "gotmap-benchmark"
7
- version = "0.2.2"
7
+ version = "0.2.3"
8
8
  description = "Benchmark pipeline for gotmap (query-to-reference atlas mapping) vs baselines."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"