Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ training = [

# CASA tools for Measurement Set I/O
casa = [
"python-casacore; sys_platform != 'darwin'",
"casatools; sys_platform == 'darwin'",
"casatools>=6.5.0",
"casatasks>=6.5.0",
]

# Development tools
Expand Down
8 changes: 6 additions & 2 deletions rfi_toolbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
__email__ = "pjaganna@nrao.edu"

# Eager imports (required for multiprocessing pickle compatibility)
from . import datasets, preprocessing
import datasets as datasets
import preprocessing as preprocessing

# Lazy imports for other modules to avoid circular dependencies
def __getattr__(name):
"""Lazy import for optional modules."""
import importlib
print(f"[DEBUG __getattr__] Lazy loading: {name}")

# List of valid lazy-loaded modules
valid_modules = {
Expand All @@ -40,10 +42,12 @@ def __getattr__(name):
}

if name in valid_modules:
print(f"[DEBUG __getattr__] Calling importlib.import_module for: {name}")
# Use importlib to avoid triggering __getattr__ recursion
mod = importlib.import_module(f".{name}", __name__)
print(f"[DEBUG __getattr__] Successfully imported: {name}")
# Cache in globals to avoid repeated imports
globals()[name] = mod
return mod

raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
28 changes: 8 additions & 20 deletions rfi_toolbox/datasets/rfi_mask_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,12 @@
from tqdm import tqdm

try:
import casacore.tables as ct

use_casacore = True
from casatools import table
CASA_AVAILABLE = True
except ImportError:
try:
from casatools import table

use_casacore = False
CASA_AVAILABLE = True
except ImportError:
# CASA not available - class will fail at instantiation if use_ms=True
ct = None
table = None
use_casacore = False
CASA_AVAILABLE = False
# CASA not available - class will fail at instantiation if use_ms=True
table = None
CASA_AVAILABLE = False


class RFIMaskDataset(Dataset):
Expand Down Expand Up @@ -73,13 +63,11 @@ def __init__(
if not CASA_AVAILABLE:
raise ImportError(
"CASA is required for use_ms=True but is not installed.\n"
"Install with: pip install python-casacore OR pip install casatools"
"Install with: pip install rfi-toolbox[casa]"
)
if use_casacore:
self.tb = ct.table(ms_name, readonly=True)
else:
tb = table()
self.tb = tb.open(ms_name, readonly=True)
tb = table()
tb.open(ms_name)
self.tb = tb
self.num_antennas = self.tb.getcol("ANTENNA1").max() + 1
self.spw_array = np.unique(self.tb.getcol("DATA_DESC_ID"))

Expand Down
16 changes: 14 additions & 2 deletions rfi_toolbox/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,28 @@
Handles CASA measurement set I/O and synthetic data injection.
"""

print("[DEBUG io/__init__] Starting io module import")

print("[DEBUG io/__init__] Attempting to import MSLoader")
try:
from .ms_loader import MSLoader

print("[DEBUG io/__init__] MSLoader imported successfully")
_HAS_CASA = True
except ImportError:
except ImportError as e:
print(f"[DEBUG io/__init__] MSLoader import failed: {e}")
_HAS_CASA = False
MSLoader = None

print("[DEBUG io/__init__] Attempting to import inject_synthetic_data")
try:
from .ms_injection import inject_synthetic_data
except ImportError:

print("[DEBUG io/__init__] inject_synthetic_data imported successfully")
except ImportError as e:
print(f"[DEBUG io/__init__] inject_synthetic_data import failed: {e}")
inject_synthetic_data = None

print("[DEBUG io/__init__] io module import complete")

__all__ = ["MSLoader", "inject_synthetic_data"]
29 changes: 23 additions & 6 deletions rfi_toolbox/io/ms_injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@
import numpy as np
from tqdm import tqdm

print("[DEBUG ms_injection] Basic imports complete")

print("[DEBUG ms_injection] Attempting casatools import")
try:
from casatools import table

print("[DEBUG ms_injection] casatools.table imported successfully")
CASA_AVAILABLE = True
except ImportError:
except ImportError as e:
print(f"[DEBUG ms_injection] casatools import failed: {e}")
CASA_AVAILABLE = False

print("[DEBUG ms_injection] ms_injection module import complete")


def inject_synthetic_data(
template_ms_path,
Expand All @@ -44,14 +51,17 @@ def inject_synthetic_data(
"""
if not CASA_AVAILABLE:
raise ImportError(
"casatools is required for MS injection. " "Install with: pip install rfi-toolbox[casa]"
"casatools is required for MS injection. "
"Install with: pip install rfi-toolbox[casa]"
)

template_ms_path = Path(template_ms_path)

# Default output path
if output_ms_path is None:
output_ms_path = template_ms_path.parent / f"{template_ms_path.stem}.synthetic.ms"
output_ms_path = (
template_ms_path.parent / f"{template_ms_path.stem}.synthetic.ms"
)
else:
output_ms_path = Path(output_ms_path)

Expand Down Expand Up @@ -104,7 +114,10 @@ def inject_synthetic_data(
# For simplicity, assume all SPWs have same channel count
# and we're filling all SPWs with the same data
if len(set(channels_per_spw)) > 1:
print(" WARNING: MS has SPWs with different channel counts. " "Using first SPW only.")
print(
" WARNING: MS has SPWs with different channel counts. "
"Using first SPW only."
)

channels_in_spw = channels_per_spw[0]

Expand All @@ -130,7 +143,9 @@ def inject_synthetic_data(

for spw_idx in range(num_spw):
# Query this baseline + SPW
subtable = tb.query(f"DATA_DESC_ID=={spw_idx} && ANTENNA1=={ant1} && ANTENNA2=={ant2}")
subtable = tb.query(
f"DATA_DESC_ID=={spw_idx} && ANTENNA1=={ant1} && ANTENNA2=={ant2}"
)

if subtable.nrows() == 0:
print(f" WARNING: No rows for baseline ({ant1},{ant2}), SPW {spw_idx}")
Expand Down Expand Up @@ -251,7 +266,9 @@ def inject_synthetic_data(
subtable.putcell("DATA", row_idx, cell_val)
except Exception as e:
subtable.close()
raise RuntimeError(f"Failed to write DATA row {row_idx}: {e}") from e
raise RuntimeError(
f"Failed to write DATA row {row_idx}: {e}"
) from e

subtable.close()

Expand Down
Loading
Loading