alexandergagliano commited on
Commit
75c672a
·
1 Parent(s): c66cfda

Fix NoneType format error and move all imports to top

Browse files
Files changed (1) hide show
  1. app.py +15 -19
app.py CHANGED
@@ -7,6 +7,8 @@ from plotly.subplots import make_subplots
7
  import sys
8
  import os
9
  import io
 
 
10
  from PIL import Image
11
  import requests
12
  from pathlib import Path
@@ -17,8 +19,10 @@ sys.path.append(str(Path(__file__).parent.parent / "re-laiss" / "src"))
17
  import relaiss as rl
18
  from relaiss.constants import lc_features_const, host_features_const, anom_lc_features_const
19
  from relaiss.search import primer
20
- from relaiss.fetch import fetch_ps1_rgb_jpeg, fetch_ps1_cutout
 
21
  import antares_client
 
22
  from astropy.visualization import AsinhStretch, PercentileInterval
23
 
24
  # Page configuration
@@ -141,10 +145,6 @@ def initialize_relaiss():
141
  with st.spinner(status_msg):
142
  # Clear only index cache if we need to rebuild (preserve preprocessed dataset bank)
143
  if need_rebuild:
144
- from relaiss.utils import get_cache_dir
145
- import shutil
146
- from pathlib import Path
147
-
148
  cache_dir = Path(get_cache_dir())
149
  index_dir = cache_dir / 'indices'
150
 
@@ -182,7 +182,6 @@ def initialize_relaiss():
182
 
183
  def search_similar_transients(ztf_id, n_matches, lc_features, host_features):
184
  """Search for similar transients using reLAISS."""
185
- import time
186
  try:
187
  # Use the existing client (should be initialized by now)
188
  client = st.session_state.relaiss_client
@@ -334,9 +333,6 @@ def main():
334
  st.warning("No index built")
335
 
336
  if st.button("Clear Cache", help="Clear all cached data to fix index corruption issues"):
337
- from relaiss.utils import get_cache_dir
338
- import shutil
339
-
340
  cache_dir = get_cache_dir()
341
  if cache_dir.exists():
342
  try:
@@ -505,10 +501,6 @@ def main():
505
 
506
  # Fetch query object metadata
507
  try:
508
- from antares_client._api.models import Locus
509
- from relaiss.fetch import get_TNS_data
510
- import antares_client
511
-
512
  query_locus = antares_client.search.get_by_ztf_object_id(ztf_object_id=query_ztf_id)
513
  query_iau, query_spec, query_z = get_TNS_data(query_ztf_id)
514
  except Exception as e:
@@ -539,7 +531,7 @@ def main():
539
  </div>
540
  <div>
541
  <div style="font-size: 0.65rem; color: #a0aec0; margin-bottom: 0.15rem;">Redshift</div>
542
- <div style="font-weight: 500; font-size: 0.85rem;">{query_z if isinstance(query_z, str) else f'{query_z:.4f}' if query_z != 'N/A' else 'N/A'}</div>
543
  </div>
544
  </div>
545
  <div style="margin-top: 0.375rem;">
@@ -559,6 +551,14 @@ def main():
559
  spec = results['spec_cls'].iloc[i] if 'spec_cls' in results.columns else 'N/A'
560
  z = results['z'].iloc[i] if 'z' in results.columns else 'N/A'
561
 
 
 
 
 
 
 
 
 
562
  # Create a styled card for each match
563
  st.markdown(f"""
564
  <div style="background: linear-gradient(135deg, #2d3748 0%, #1a202c 100%);
@@ -584,7 +584,7 @@ def main():
584
  </div>
585
  <div>
586
  <div style="font-size: 0.65rem; color: #a0aec0; margin-bottom: 0.15rem;">Redshift</div>
587
- <div style="font-weight: 500; font-size: 0.85rem;">{z if isinstance(z, str) else f'{z:.4f}' if z != 'N/A' else 'N/A'}</div>
588
  </div>
589
  </div>
590
  <div style="margin-top: 0.375rem;">
@@ -603,8 +603,6 @@ def main():
603
  # Get primer data for the query object
604
  if st.session_state.relaiss_client:
605
  try:
606
- import antares_client
607
-
608
  primer_dict = primer(
609
  lc_ztf_id=ztf_id,
610
  dataset_bank_path=st.session_state.relaiss_client.bank_csv,
@@ -758,7 +756,6 @@ def main():
758
  stretch = AsinhStretch() + PercentileInterval(99.5)
759
  img_stretched = stretch(img_array)
760
  # Convert to PIL Image for display
761
- from PIL import Image
762
  img_pil = Image.fromarray((img_stretched * 255).astype(np.uint8))
763
  st.image(img_pil, use_container_width=True)
764
  except Exception:
@@ -768,7 +765,6 @@ def main():
768
  stretch = AsinhStretch() + PercentileInterval(99.5)
769
  img_stretched = stretch(img_array)
770
  # Convert to PIL Image for display
771
- from PIL import Image
772
  img_pil = Image.fromarray((img_stretched * 255).astype(np.uint8))
773
  st.image(img_pil, use_container_width=True)
774
  except Exception as e:
 
7
  import sys
8
  import os
9
  import io
10
+ import time
11
+ import shutil
12
  from PIL import Image
13
  import requests
14
  from pathlib import Path
 
19
  import relaiss as rl
20
  from relaiss.constants import lc_features_const, host_features_const, anom_lc_features_const
21
  from relaiss.search import primer
22
+ from relaiss.fetch import fetch_ps1_rgb_jpeg, fetch_ps1_cutout, get_TNS_data
23
+ from relaiss.utils import get_cache_dir
24
  import antares_client
25
+ from antares_client._api.models import Locus
26
  from astropy.visualization import AsinhStretch, PercentileInterval
27
 
28
  # Page configuration
 
145
  with st.spinner(status_msg):
146
  # Clear only index cache if we need to rebuild (preserve preprocessed dataset bank)
147
  if need_rebuild:
 
 
 
 
148
  cache_dir = Path(get_cache_dir())
149
  index_dir = cache_dir / 'indices'
150
 
 
182
 
183
  def search_similar_transients(ztf_id, n_matches, lc_features, host_features):
184
  """Search for similar transients using reLAISS."""
 
185
  try:
186
  # Use the existing client (should be initialized by now)
187
  client = st.session_state.relaiss_client
 
333
  st.warning("No index built")
334
 
335
  if st.button("Clear Cache", help="Clear all cached data to fix index corruption issues"):
 
 
 
336
  cache_dir = get_cache_dir()
337
  if cache_dir.exists():
338
  try:
 
501
 
502
  # Fetch query object metadata
503
  try:
 
 
 
 
504
  query_locus = antares_client.search.get_by_ztf_object_id(ztf_object_id=query_ztf_id)
505
  query_iau, query_spec, query_z = get_TNS_data(query_ztf_id)
506
  except Exception as e:
 
531
  </div>
532
  <div>
533
  <div style="font-size: 0.65rem; color: #a0aec0; margin-bottom: 0.15rem;">Redshift</div>
534
+ <div style="font-weight: 500; font-size: 0.85rem;">{'N/A' if query_z is None or query_z == 'N/A' else (query_z if isinstance(query_z, str) else f'{query_z:.4f}')}</div>
535
  </div>
536
  </div>
537
  <div style="margin-top: 0.375rem;">
 
551
  spec = results['spec_cls'].iloc[i] if 'spec_cls' in results.columns else 'N/A'
552
  z = results['z'].iloc[i] if 'z' in results.columns else 'N/A'
553
 
554
+ # Handle NaN values
555
+ if pd.isna(z):
556
+ z = 'N/A'
557
+ if pd.isna(iau_name):
558
+ iau_name = 'N/A'
559
+ if pd.isna(spec):
560
+ spec = 'N/A'
561
+
562
  # Create a styled card for each match
563
  st.markdown(f"""
564
  <div style="background: linear-gradient(135deg, #2d3748 0%, #1a202c 100%);
 
584
  </div>
585
  <div>
586
  <div style="font-size: 0.65rem; color: #a0aec0; margin-bottom: 0.15rem;">Redshift</div>
587
+ <div style="font-weight: 500; font-size: 0.85rem;">{'N/A' if z is None or z == 'N/A' else (z if isinstance(z, str) else f'{z:.4f}')}</div>
588
  </div>
589
  </div>
590
  <div style="margin-top: 0.375rem;">
 
603
  # Get primer data for the query object
604
  if st.session_state.relaiss_client:
605
  try:
 
 
606
  primer_dict = primer(
607
  lc_ztf_id=ztf_id,
608
  dataset_bank_path=st.session_state.relaiss_client.bank_csv,
 
756
  stretch = AsinhStretch() + PercentileInterval(99.5)
757
  img_stretched = stretch(img_array)
758
  # Convert to PIL Image for display
 
759
  img_pil = Image.fromarray((img_stretched * 255).astype(np.uint8))
760
  st.image(img_pil, use_container_width=True)
761
  except Exception:
 
765
  stretch = AsinhStretch() + PercentileInterval(99.5)
766
  img_stretched = stretch(img_array)
767
  # Convert to PIL Image for display
 
768
  img_pil = Image.fromarray((img_stretched * 255).astype(np.uint8))
769
  st.image(img_pil, use_container_width=True)
770
  except Exception as e: