Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions roman_photoz/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# Import after logger setup to avoid circular imports
from .create_simulated_catalog import SimulatedCatalog
from .default_config_file import * # noqa: F403

# Import logger from the dedicated logger module
from .logger import logger, setup_logging
from .roman_catalog_handler import RomanCatalogHandler
from .roman_catalog_process import RomanCatalogProcess

__all__ = [
"RomanCatalogProcess",
"RomanCatalogHandler",
"SimulatedCatalog",
"setup_logging",
"logger",
]
12 changes: 9 additions & 3 deletions roman_photoz/create_roman_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from lephare.data_retrieval import get_auxiliary_data
from lephare.filter import Filter # type: ignore

from roman_photoz.logger import logger

# date of the file
DEFAULT_FILE_DATE = "20210614"

Expand Down Expand Up @@ -157,7 +159,7 @@ def create_path(filepath: str = "") -> Path:
# save files to custom path
path = Path(filepath).resolve()
# create path if they don't exist
print("Creating directory structure...")
logger.info("Creating directory structure...")
path.mkdir(parents=True, exist_ok=True)
return path

Expand Down Expand Up @@ -191,24 +193,28 @@ def run(input_filename: str = "", input_path: str = ""):
input_path : str
The path where the results will be saved.
"""
logger.info("Starting filter file creation...")
# create dataframe from file
data = read_effarea_file(filename=input_filename, header=1)

# get auxiliary data
logger.info("Getting auxiliary data...")
get_auxiliary_data()

# format and create the file for each filter
# containing lambda vs transmission
logger.info("Creating individual filter files...")
create_files(data=data, filepath=input_path)

# run filter command to create the filter
# file containing lambda vs transmission for all filters
# (merge all the filter files created in the previous call)
logger.info("Running filter command to merge filter files...")
run_filter_command(config_file_path=input_path)
logger.info("Filter file creation completed successfully")


if __name__ == "__main__":

# this module takes a filename as input containing
# the monochromatic effective area of each filter per column
# and creates one file for each filter as well as
Expand All @@ -221,4 +227,4 @@ def run(input_filename: str = "", input_path: str = ""):

run(input_filename=input_filename, input_path=input_path)

print("Done.")
logger.info("Done.")
16 changes: 11 additions & 5 deletions roman_photoz/create_simulated_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from roman_photoz import create_roman_filters
from roman_photoz.default_config_file import default_roman_config
from roman_photoz.logger import logger

ROMAN_DEFAULT_CONFIG = default_roman_config

Expand Down Expand Up @@ -116,12 +117,13 @@ def get_filters(self):
Path(self.lephare_config["FILTER_REP"], "roman"), "roman_"
)
if not filter_files_present:
logger.info("Filter files not found, generating them...")
create_roman_filters.run()

self.filter_lib = lp.FilterSvc.from_keymap(
lp.all_types_to_keymap(self.lephare_config)
)
print(
logger.info(
f"Created filter library using the filter files in {self.lephare_config['FILTER_REP']}/roman."
)

Expand All @@ -146,6 +148,7 @@ def create_simulated_data(self):
"LIB_ASCII": "YES",
}

logger.info("Preparing LePhare environment for simulated data generation...")
lp.prepare(
config=self.lephare_config,
star_config=star_overrides,
Expand All @@ -154,6 +157,7 @@ def create_simulated_data(self):
)

self.simulated_data_filename = gal_overrides.get("GAL_LIB_OUT")
logger.info("Simulated data generated successfully")

def create_simulated_input_catalog(
self,
Expand Down Expand Up @@ -300,9 +304,9 @@ def save_catalog(
"""
output_path = Path(output_path).as_posix()
output_filename = output_filename
logger.info(f"Saving catalog to {output_path}/{output_filename}...")
self.roman_catalog_template.save(output_filename, dir_path=output_path)

print(f"Saved simulated input catalog to {output_path}/{output_filename}.")
logger.info("Catalog saved successfully")

def add_ids(self, catalog):
"""
Expand Down Expand Up @@ -462,7 +466,7 @@ def process(
output_path=output_path,
)

print("DONE")
logger.info("DONE")


if __name__ == "__main__":
Expand All @@ -487,7 +491,9 @@ def parse_args():

args = parse_args()

logger.info("Starting simulated catalog creation...")
rcp = SimulatedCatalog()
rcp.process(args.output_path, args.output_filename)
logger.info("Simulated catalog creation completed successfully")

print("Done.")
logger.info("Done.")
37 changes: 37 additions & 0 deletions roman_photoz/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import logging


def setup_logging(log_file="roman_photoz.log", level=logging.INFO):
"""Set up logging configuration for roman_photoz module"""
# Create a logger specific to our module instead of configuring the root logger
logger = logging.getLogger("roman_photoz")

# Only configure if it hasn't been configured yet
if not logger.handlers:
# Set propagate to False to prevent messages from being passed to the root logger
logger.propagate = False

# Set the level
logger.setLevel(level)

# Create formatter
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)

# Create handlers
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)

file_handler = logging.FileHandler(log_file, mode="a")
file_handler.setFormatter(formatter)

# Add handlers to logger
logger.addHandler(console_handler)
logger.addHandler(file_handler)

return logger


# Create the default logger instance
logger = setup_logging()
11 changes: 6 additions & 5 deletions roman_photoz/roman_catalog_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from roman_datamodels import datamodels as rdm

from roman_photoz.default_config_file import default_roman_config
from roman_photoz.logger import logger


class RomanCatalogHandler:
Expand Down Expand Up @@ -32,7 +33,7 @@ def __init__(self, catname: str = ""):
self.cat_temp_filename = "cat_temp_file.csv"
# get Roman's filter names from default config file
filter_list = default_roman_config.get("FILTER_LIST")
if filter_list is not None:
if (filter_list is not None):
self.filter_names = (
filter_list.replace(".pb", "").replace("roman/", "").split(",")
)
Expand All @@ -43,7 +44,7 @@ def format_catalog(self):
"""
Format the catalog by appending necessary fields and columns.
"""
print("Formatting catalog...")
logger.info("Formatting catalog...")
# self.format_colnames()

self.catalog = self.cat_array[["id"]].copy()
Expand Down Expand Up @@ -79,19 +80,19 @@ def format_catalog(self):
self.cat_array["string_data"].astype(str),
usemask=False,
)
print("Done.")
logger.info("Catalog formatting completed")

def read_catalog(self):
"""
Read the catalog file and convert it to a numpy structured array.
"""
print(f"Parsing catalog {self.cat_name}...")
logger.info(f"Reading catalog {self.cat_name}...")
dm = rdm.open(self.cat_name)
self.cat_array = (
dm.source_catalog.as_array()
) # Convert Table to numpy structured array

print("Done.")
logger.info("Catalog read successfully")

def process(self):
"""
Expand Down
11 changes: 8 additions & 3 deletions roman_photoz/roman_catalog_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from roman_photoz.default_config_file import default_roman_config
from roman_photoz.roman_catalog_handler import RomanCatalogHandler
from roman_photoz.logger import logger

DS = RailStage.data_store
DS.__class__.allow_overwrite = True
Expand Down Expand Up @@ -124,6 +125,7 @@ def get_data(
"""
# full qualified path to the catalog file
filename = Path(input_path, input_filename).as_posix()
logger.info(f"Reading catalog from {filename}")

# read in catalog data
if Path(filename).suffix == ".asdf":
Expand All @@ -147,7 +149,7 @@ def format_data(self, cat_data: Table):
"""
# get information about Roman filters
bands = self.config["FILTER_LIST"].split(",")
print(len(bands))
logger.debug(f"Found {len(bands)} bands in filter list")

# loop over the filters we want to keep to get
# the number of the filter, n, and the name, b
Expand All @@ -164,6 +166,7 @@ def format_data(self, cat_data: Table):
self.data["string_data"] = np.array(cat_data[cat_data.colnames[-1]]).astype(
bytes
)
logger.info("Catalog data formatted successfully")

def create_informer_stage(self):
"""
Expand Down Expand Up @@ -254,13 +257,14 @@ def save_results(
if self.estimated is not None:
ancil_data = self.estimated.data.ancil
else:
logger.error("No results to save")
raise ValueError("No results to save.")

tree = {"roman_photoz_results": ancil_data}
with AsdfFile(tree) as af:
af.write_to(output_filename)

print(f"Results saved to {output_filename}")
logger.info(f"Results saved to {output_filename}")

def process(
self,
Expand Down Expand Up @@ -350,6 +354,7 @@ def main(argv=None):

args = parser.parse_args(argv)

logger.info("Starting Roman catalog processing")
rcp = RomanCatalogProcess(config_filename=args.config_filename)

rcp.process(
Expand All @@ -360,4 +365,4 @@ def main(argv=None):
save_results=args.save_results,
)

print("Done.")
logger.info("Roman catalog processing completed")
Loading