pubchem-faiss-library / code /scripts /create_test_spectra.py
YinkaiW's picture
Upload folder using huggingface_hub
db32e07 verified
Raw
History Blame Contribute Delete
1.4 kB
import json
import os
import sys
# Assume running from Spec-RAG/ or using relative path
input_path = "data/val.jsonl"
output_path = "data/test_spectra.jsonl"
num_samples = 50
# If paths don't exist, try absolute
if not os.path.exists(input_path):
# Try relative to script
script_dir = os.path.dirname(os.path.abspath(__file__))
input_path = os.path.join(script_dir, "../data/val.jsonl")
output_path = os.path.join(script_dir, "../data/test_spectra.jsonl")
if not os.path.exists(input_path):
print(f"Error: {input_path} does not exist.")
sys.exit(1)
print(f"Reading from {input_path}...")
with open(input_path, "r") as f_in, open(output_path, "w") as f_out:
count = 0
for line in f_in:
line = line.strip()
if not line: continue
if count >= num_samples:
break
try:
data = json.loads(line)
# Keep only necessary fields for inference (peaks) and reference (smiles)
new_data = {
"spectrum_id": data.get("spectrum_id", f"spec_{count}"),
"peaks": data.get("peaks", []),
"smiles": data.get("smiles", "") # Keep ground truth for evaluation
}
f_out.write(json.dumps(new_data) + "\n")
count += 1
except json.JSONDecodeError:
continue
print(f"Created {output_path} with {count} samples.")