Anton Bushuiev commited on
Commit
575d08b
·
1 Parent(s): 2af3eaa

Optimize data loading and image generation for subset to be rendered

Browse files
Files changed (1) hide show
  1. app.py +8 -6
app.py CHANGED
@@ -333,18 +333,20 @@ def _create_result_row(i, j, n, msdata, msdata_lib, sims, cos_sim, embs, calcula
333
  spec1 = msdata.get_spectra(i)
334
  spec2 = msdata_lib.get_spectra(j)
335
 
 
 
336
  # Base row data
337
  row_data = {
338
  'feature_id': i + 1,
339
  'precursor_mz': msdata.get_prec_mzs(i),
340
  'topk': n + 1,
341
  'library_j': j,
342
- 'library_SMILES': smiles_to_html_img(smiles),
343
  'library_SMILES_raw': smiles,
344
- 'Spectrum': spectrum_to_html_img(spec1, spec2),
345
  'Spectrum_raw': su.unpad_peak_list(spec1),
346
  'library_ID': msdata_lib.get_values('IDENTIFIER', j),
347
- 'DreaMS_similarity': sims[i, j],
348
  'i': i,
349
  'j': j,
350
  'DreaMS_embedding': embs[i],
@@ -453,7 +455,7 @@ def _predict_core(lib_pth, in_pth, calculate_modified_cosine, progress):
453
  try:
454
  # Load library data
455
  progress(0.1, desc="Loading library data...")
456
- msdata_lib = MSData.load(temp_lib_path)
457
  embs_lib = msdata_lib[DREAMS_EMBEDDING]
458
  print(f'Shape of the library embeddings: {embs_lib.shape}')
459
 
@@ -470,7 +472,7 @@ def _predict_core(lib_pth, in_pth, calculate_modified_cosine, progress):
470
  topk_cands = np.argsort(sims, axis=1)[:, -k:][:, ::-1]
471
 
472
  # Load query data for processing
473
- msdata = MSData.load(in_pth)
474
  print(f'Available columns: {msdata.columns()}')
475
 
476
  # Construct results DataFrame
@@ -611,7 +613,7 @@ def _create_gradio_interface():
611
  calculate_modified_cosine = gr.Checkbox(
612
  label="Calculate modified cosine similarity",
613
  value=False,
614
- info="Enable to calculate traditional modified cosine similarity scores (slower)"
615
  )
616
 
617
  # Prediction button
 
333
  spec1 = msdata.get_spectra(i)
334
  spec2 = msdata_lib.get_spectra(j)
335
 
336
+ dreams_similarity = sims[i, j]
337
+
338
  # Base row data
339
  row_data = {
340
  'feature_id': i + 1,
341
  'precursor_mz': msdata.get_prec_mzs(i),
342
  'topk': n + 1,
343
  'library_j': j,
344
+ 'library_SMILES': smiles_to_html_img(smiles) if dreams_similarity >= SIMILARITY_THRESHOLD else None,
345
  'library_SMILES_raw': smiles,
346
+ 'Spectrum': spectrum_to_html_img(spec1, spec2) if dreams_similarity >= SIMILARITY_THRESHOLD else None,
347
  'Spectrum_raw': su.unpad_peak_list(spec1),
348
  'library_ID': msdata_lib.get_values('IDENTIFIER', j),
349
+ 'DreaMS_similarity': dreams_similarity,
350
  'i': i,
351
  'j': j,
352
  'DreaMS_embedding': embs[i],
 
455
  try:
456
  # Load library data
457
  progress(0.1, desc="Loading library data...")
458
+ msdata_lib = MSData.load(temp_lib_path, in_mem=True)
459
  embs_lib = msdata_lib[DREAMS_EMBEDDING]
460
  print(f'Shape of the library embeddings: {embs_lib.shape}')
461
 
 
472
  topk_cands = np.argsort(sims, axis=1)[:, -k:][:, ::-1]
473
 
474
  # Load query data for processing
475
+ msdata = MSData.load(in_pth, in_mem=True)
476
  print(f'Available columns: {msdata.columns()}')
477
 
478
  # Construct results DataFrame
 
613
  calculate_modified_cosine = gr.Checkbox(
614
  label="Calculate modified cosine similarity",
615
  value=False,
616
+ info="Enable to also calculate traditional modified cosine similarity scores (a bit slower)"
617
  )
618
 
619
  # Prediction button