bilalsm commited on
Commit
c86d6eb
·
verified ·
1 Parent(s): cdf0030

Upload examples/basic_usage.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. examples/basic_usage.py +150 -0
examples/basic_usage.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Basic usage examples for DOM KNN models from Hugging Face Hub.
3
+
4
+ This script demonstrates how to load and use the models for formula assignment.
5
+ """
6
+
7
+ from transformers import AutoModel
8
+ import numpy as np
9
+
10
+
11
+ def example_1_default_model():
12
+ """Load the default best-performing model."""
13
+ print("=" * 60)
14
+ print("Example 1: Loading default model (21T, K=1, Euclidean)")
15
+ print("=" * 60)
16
+
17
+ model = AutoModel.from_pretrained(
18
+ "SaeedLab/dom-formula-assignment-using-knn",
19
+ trust_remote_code=True
20
+ )
21
+
22
+ print(f"[OK] Model loaded successfully")
23
+ print(f" Config: {model.config}")
24
+ print()
25
+
26
+
27
+ def example_2_variant_by_parameters():
28
+ """Load a specific variant using parameters."""
29
+ print("=" * 60)
30
+ print("Example 2: Loading variant by parameters")
31
+ print("=" * 60)
32
+
33
+ model = AutoModel.from_pretrained(
34
+ "SaeedLab/dom-formula-assignment-using-knn",
35
+ data_source="7T-21T",
36
+ k_neighbors=1,
37
+ metric="euclidean",
38
+ training_version="ver3",
39
+ trust_remote_code=True
40
+ )
41
+
42
+ print(f"[OK] Model loaded successfully")
43
+ print(f" Data source: 7T-21T")
44
+ print(f" K neighbors: 1")
45
+ print(f" Metric: euclidean")
46
+ print(f" Training version: ver3")
47
+ print()
48
+
49
+
50
+ def example_3_variant_by_name():
51
+ """Load a specific variant by direct name."""
52
+ print("=" * 60)
53
+ print("Example 3: Loading variant by name")
54
+ print("=" * 60)
55
+
56
+ model = AutoModel.from_pretrained(
57
+ "SaeedLab/dom-formula-assignment-using-knn",
58
+ variant="knn_21T_k3_manhattan",
59
+ trust_remote_code=True
60
+ )
61
+
62
+ print(f"[OK] Model loaded: knn_21T_k3_manhattan")
63
+ print(f" Config: {model.config}")
64
+ print()
65
+
66
+
67
+ def example_4_make_predictions():
68
+ """Make predictions with the model."""
69
+ print("=" * 60)
70
+ print("Example 4: Making predictions")
71
+ print("=" * 60)
72
+
73
+ # Load model
74
+ model = AutoModel.from_pretrained(
75
+ "SaeedLab/dom-formula-assignment-using-knn",
76
+ trust_remote_code=True
77
+ )
78
+
79
+ # Create sample data (replace with your actual mass spec features)
80
+ # Features might include: m/z, O/C ratio, H/C ratio, N/C ratio, etc.
81
+ X_sample = np.array([
82
+ [300.1234, 0.5, 1.2, 0.1], # Sample 1
83
+ [450.6789, 0.6, 1.5, 0.2], # Sample 2
84
+ [275.5432, 0.4, 1.1, 0.0], # Sample 3
85
+ ])
86
+
87
+ print(f"Input shape: {X_sample.shape}")
88
+ print(f"Making predictions...")
89
+
90
+ # Get predictions
91
+ predictions = model(X_sample)
92
+
93
+ print(f"[OK] Predictions shape: {predictions.shape}")
94
+ print(f" First 3 predictions: {predictions[:3]}")
95
+ print()
96
+
97
+
98
+ def example_5_compare_models():
99
+ """Compare predictions from different model variants."""
100
+ print("=" * 60)
101
+ print("Example 5: Comparing model variants")
102
+ print("=" * 60)
103
+
104
+ # Sample data
105
+ X_sample = np.array([[300.1234, 0.5, 1.2, 0.1]])
106
+
107
+ variants = [
108
+ ("21T K=1 Euclidean", {"variant": "knn_21T_k1_euclidean"}),
109
+ ("21T K=3 Manhattan", {"variant": "knn_21T_k3_manhattan"}),
110
+ ("7T-21T K=1 Euclidean", {
111
+ "data_source": "7T-21T",
112
+ "k_neighbors": 1,
113
+ "metric": "euclidean",
114
+ "training_version": "ver3"
115
+ }),
116
+ ]
117
+
118
+ for name, params in variants:
119
+ model = AutoModel.from_pretrained(
120
+ "SaeedLab/dom-formula-assignment-using-knn",
121
+ trust_remote_code=True,
122
+ **params
123
+ )
124
+ pred = model(X_sample)
125
+ print(f" {name}: {pred[0]}")
126
+
127
+ print()
128
+
129
+
130
+ if __name__ == "__main__":
131
+ print("\n" + "=" * 60)
132
+ print("DOM KNN Models - Usage Examples")
133
+ print("=" * 60 + "\n")
134
+
135
+ try:
136
+ example_1_default_model()
137
+ example_2_variant_by_parameters()
138
+ example_3_variant_by_name()
139
+ example_4_make_predictions()
140
+ example_5_compare_models()
141
+
142
+ print("=" * 60)
143
+ print("[OK] All examples completed successfully!")
144
+ print("=" * 60)
145
+
146
+ except Exception as e:
147
+ print(f"\n[ERROR] Error: {e}")
148
+ print("\nNote: These examples require the model to be uploaded to")
149
+ print(" Hugging Face Hub at: SaeedLab/dom-formula-assignment-using-knn")
150
+ print("\nFor local testing, replace the repo ID with a local path.")