File size: 1,398 Bytes
db32e07 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | 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.")
|