astrocook.session.session module#

astrocook.session.session.load_session_from_file(archive_path: str, name: str, gui_context: Any, format_name: str) → Session | int[source]#

Orchestrate the loading of a session from a file or archive.

This function handles various input formats, including legacy Astrocook V1 archives (.acs), modern V2 archives (.acs2), and raw FITS files. It automatically handles V1-to-V2 migration of structures.

Parameters:
  • archive_path (str) – The full path to the file to load.

  • name (str) – The display name to assign to the new session.

  • gui_context (Any) – A reference to the main GUI window (for logging and dialog parenting), or a mock object if running in a script.

  • format_name (str) – A format identifier string (e.g., 'generic_spectrum', 'eso_midas') used to select the appropriate loader for raw files. Use 'auto' to attempt detection.

Returns:

The loaded Session object if successful. Returns 0 (integer) if the loading process failed.

Return type:

Session or int

Raises:
  • FileNotFoundError – If the archive path is invalid or internal files are missing.

  • RuntimeError – If the V1 archive unpacking fails.

class astrocook.session.session.Session(name: str, gui: Any, spec: Spectrum | None = None, systs: SystemList | None = None)[source]#

Bases: object

The central coordinator for an Astrocook analysis session.

This class encapsulates the state of a single spectrum and its associated models (systems, components). It serves as the primary entry point for high-level analysis workflows (“Recipes”).

Note

Immutability Pattern: In Astrocook V2, the Session object is designed to be treated as immutable within the undo/redo history stack. Operations that modify data (like smoothing flux or fitting a continuum) do not modify the object in-place; instead, they return a new Session instance containing the updated data structures.

name#

The display name of the session (e.g., filename without extension).

Type:

str

saved_with_version#

The version of Astrocook that saved the .acs2 archive this session was opened from; None if it was not opened from an .acs2 archive, or if the archive does not record it (versions up to 2.0.3).

Type:

str or None

systs#

The container for identified absorption systems and Voigt profile components.

Type:

astrocook.core.system_list.SystemList, optional

file#

Access to file import/export recipes (e.g., import_ascii, export_ascii).

Type:

astrocook.recipes.file.RecipeFile

edit#

Access to editing recipes (e.g., set_properties, convert_x_axis).

Type:

astrocook.recipes.edit.RecipeEdit

flux#

Access to flux manipulation recipes (e.g., smooth, rebin).

Type:

astrocook.recipes.flux.RecipeFlux

continuum#

Access to continuum estimation and fitting recipes.

Type:

astrocook.recipes.continuum.RecipeContinuum

absorbers#

Access to line identification and fitting recipes.

Type:

astrocook.recipes.absorbers.RecipeAbsorbers

__init__(name: str, gui: Any, spec: Spectrum | None = None, systs: SystemList | None = None)[source]#
property spec: Spectrum | None#

The main container for spectral data (flux, wavelength, error, etc.).

property lines#
classmethod open_new(file_path: str, name: str, gui_context: Any, format_name: str) → Session | int[source]#

Factory method: Create a new Session by loading data from a file.

Parameters:
  • file_path (str) – The system path to the file (FITS or ACS/ACS2 archive).

  • name (str) – The name to give the new session.

  • gui_context (Any) – The GUI parent object (used for logging/dialogs).

  • format_name (str) – Hint for the file format loader (e.g. 'generic_spectrum').

Returns:

A new Session instance, or 0 on failure.

Return type:

Session or int

with_new_spectrum(new_spectrum: Spectrum | None) → Session[source]#

Return a new Session instance with an updated spectrum.

This is used by recipes that modify spectral data (e.g., smoothing, continuum fitting) to ensure history preservation.

Parameters:

new_spectrum (Spectrum or None) – The new spectral data object (None: a session without spectrum is copied as it is).

Returns:

A shallow copy of the session pointing to the new spectrum.

Return type:

Session

with_new_system_list(new_systs: SystemList) → Session[source]#

Return a new Session instance with an updated system list.

Parameters:

new_systs (SystemList) – The new system list object.

Returns:

A shallow copy of the session pointing to the new system list.

Return type:

Session

save(file_path: str, initial_session: Session | None = None, models: bool = False)[source]#

Save the current session state to an .acs2 archive.

The archive contains the full session state, metadata, the session log and (if initial_session is provided) the original data. Saving in the V1 .acs format is no longer supported: V1 archives can still be opened, and Astrocook 1.9.0 can still write them.

Parameters:
  • file_path (str) – The destination path. .acs2 is appended if missing.

  • initial_session (Session, optional) – The state of the session before any modifications, saved in the archive so that it contains the original raw data for reproducibility.

  • models (bool, optional) – Unused; kept for backward compatibility of the signature.

Returns:

Returns 0 on success.

Return type:

int

Raises:

ValueError – If file_path has the V1 .acs extension.