lightcurvelynx.utils.io_utils

Classes

SquashOutput

Context manager to temporarily squash all output to stdout and stderr.

SquashLogging

A context manager to temporarily squash all logging (below a certain level)

Functions

write_results_as_hats(base_catalog_path, results, *[, ...])

Write results to a HATS catalog.

read_numpy_data(file_path)

Read in a numpy array from different formats depending on the file extension.

write_numpy_data(file_path, data)

Write a numpy array to a file in a format determined by the file extension.

read_grid_data(input_file[, format, validate])

Read 2-d grid data from a text, csv, ecsv, or fits file.

read_lclib_data(input_file)

Read SNANA's LCLIB data from a text file.

read_sqlite_table(db_path[, table_name, sql_query])

Read a table from a SQLite database into a pandas DataFrame.

Module Contents

class SquashOutput(stdout_to_log=False, logger=None, log_level=logging.DEBUG)[source]

Context manager to temporarily squash all output to stdout and stderr.

Optionally, stdout can be redirected to a logger instead of being suppressed.

Parameters:
  • stdout_to_log (bool, optional) – If True, stdout and stderr will be redirected to the logger instead of being suppressed. Default is False.

  • logger (logging.Logger, optional) – The logger to which to redirect the output if stdout_to_log is True. If None, the root logger will be used. Default is None.

  • log_level (int, optional) – The logging level to use when redirecting stdout and stderr to the logger. Default is logging.DEBUG.

Examples

>>> with SquashOutput():
>>>     # Code that produces unwanted output
>>>     ...
>>> with SquashOutput(stdout_to_log=True):
>>>     # Printed output is sent to logger.debug
>>>     ...
__enter__()[source]
__exit__(exc_type, exc_value, traceback)[source]
class SquashLogging(logger, level=logging.ERROR)[source]

A context manager to temporarily squash all logging (below a certain level) to a logger.

Parameters:
  • logger (logging.Logger) – The logger to which to redirect the logging output.

  • level (int, optional) – The logging level below which to suppress logging output. Default: logging.ERROR

Examples

>>> with SquashLogging(logger=logging.getLogger(), level=logging.ERROR):
>>>     # Code that produces unwanted logging at any level below logging.ERROR
>>>     ...
__enter__()[source]
__exit__(exc_type, exc_value, traceback)[source]
write_results_as_hats(base_catalog_path, results, *, catalog_name=None, overwrite=False)[source]

Write results to a HATS catalog.

Parameters:
  • base_catalog_path (str or Path) – The base path to the output hats directory.

  • results (nested_pandas.NestedFrame) – The results to write, as a NestedFrame where each row is a sample.

  • catalog_name (str, optional) – The name of the catalog to write. If None, the name will be derived from the base_catalog_path. Default: None

  • overwrite (bool) – Whether to overwrite the output directory if it already exists. Default: False

read_numpy_data(file_path)[source]

Read in a numpy array from different formats depending on the file extension. Automatically detects and handles files in .npy, .npz, .csv, .ecsv, and .txt formats.

Parameters:

file_path (str) – The path to the file to read.

Returns:

data – The data read from the file.

Return type:

numpy.ndarray

write_numpy_data(file_path, data)[source]

Write a numpy array to a file in a format determined by the file extension. Automatically detects and handles files in .npy, .npz, .csv, .ecsv, and .txt formats.

Parameters:
  • file_path (str) – The path to the file to write.

  • data (numpy.ndarray) – The data to write to the file.

read_grid_data(input_file, format='ascii', validate=False)[source]

Read 2-d grid data from a text, csv, ecsv, or fits file.

Each line is of the form ‘x0 x1 value’ where x0 and x1 are the grid coordinates and value is the grid value. The rows should be sorted by increasing x0 and, within an x0 value, increasing x1.

Parameters:
  • input_file (str or file-like object) – The input data file.

  • format (str) – The file format. Should be one of the formats supported by astropy Tables such as ‘ascii’, ‘ascii.ecsv’, or ‘fits’. Default: ‘ascii’

  • validate (bool) – Perform additional validation on the input data. Default: False

Returns:

  • x0 (numpy.ndarray) – A 1-d array with the values along the x-axis of the grid.

  • x1 (numpy.ndarray) – A 1-d array with the values along the y-axis of the grid.

  • values (numpy.ndarray) – A 2-d array with the values at each point in the grid with shape (len(x0), len(x1)).

Raises:

ValueError – if any data validation fails.

read_lclib_data(input_file)[source]

Read SNANA’s LCLIB data from a text file.

Parameters:

input_file (str or Path) – The path to the SNANA LCLIB data file.

Returns:

curves – A list of Astropy Tables, each representing a light curve.

Return type:

list of astropy.table.Table

read_sqlite_table(db_path, table_name=None, sql_query=None)[source]

Read a table from a SQLite database into a pandas DataFrame.

Parameters:
  • db_path (str or Path) – The path to the SQLite database file.

  • table_name (str, optional) – The name of the table to read. If not provided, sql_query must be provided.

  • sql_query (str, optional) – A custom SQL query to execute. If provided, this query will be used instead of reading a table.

Returns:

df – The table data as a pandas DataFrame.

Return type:

pandas.DataFrame