Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make DataContainer an attribute of Effect #482

Merged
merged 8 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
14 changes: 7 additions & 7 deletions scopesim/effects/data_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@

class DataContainer:
"""
A class to hold data files needed by all Effects objects.
A class to hold data (file(s)) needed by some Effects objects.

Parameters
----------
filename : str
filename : str, optional
Path to file containing data.
Accepted formats: ASCII table, FITS table, FITS image

table : astropy.Table
table : astropy.Table, optional
An astropy Table containing data

array_dict : dict
array_dict : dict, optional
A dictionary out of which an astropy.Table object can be constructed.

kwargs :
Expand All @@ -35,7 +35,7 @@ class DataContainer:

Notes
-----
If a table is to be generated from an ``array_dict`` parameter, column
If a table is to be generated from an `array_dict` parameter, column
units can be passed as keyword arguments (kwargs) using the following
format::

Expand All @@ -57,11 +57,11 @@ class DataContainer:

table : astropy.Table
If the file has a table format (ASCII of FITS) it is read in
immediately and stored in ``.table``
immediately and stored in `.table`

_file : HDUList pointer
If the file is a FITS image or cube, the data is only read in when
needed in order to save on memory usage. ``._file`` contains a pointer
needed in order to save on memory usage. `._file` contains a pointer
to the data open FITS file.

"""
Expand Down
45 changes: 39 additions & 6 deletions scopesim/effects/effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
"""Contains base class for effects."""

from pathlib import Path
from collections.abc import Mapping, MutableMapping
from dataclasses import dataclass, field, InitVar, fields
from typing import NewType

from ..effects.data_container import DataContainer
from .data_container import DataContainer
from .. import base_classes as bc
from ..utils import from_currsys, write_report
from ..reports.rst_utils import table_to_rst
Expand All @@ -12,7 +15,10 @@
# FIXME: This docstring is out-of-date for several reasons:
# - Effects can act on objects other than Source (eg FOV, IMP, DET)
# - fov_grid is outdated
class Effect(DataContainer):


# @dataclass(kw_only=True, eq=False)
class Effect:
"""
Base class for representing the effects (artifacts) in an optical system.

Expand Down Expand Up @@ -41,7 +47,11 @@
required_keys = set()

def __init__(self, filename=None, **kwargs):
super().__init__(filename=filename, **kwargs)
self.data_container = DataContainer(filename=filename, **kwargs)
self.meta = kwargs.get("meta", {})
self.cmds = kwargs.get("cmds")

self.meta.update(self.data_container.meta)
self.meta["z_order"] = []
self.meta["include"] = True
self.meta.update(kwargs)
Comment on lines +50 to 57
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that this last self.meta.update(kwargs) is (currently) superfluous due to the self.meta.update(self.data_container.meta). Nevertheless, I prefer to keep it as-is, because I would prefer that we, in due time, remove the **kwargs from self.data_container = DataContainer(filename=filename, **kwargs).

Expand Down Expand Up @@ -92,11 +102,34 @@
[um, arcsec, arcsec]

"""
self.update(**kwargs)
# self.update(**kwargs)
return []

def update(self, **kwargs):
self.meta.update(kwargs)
# *******************************************************************
# ported from DataContainer, previous base class
# *******************************************************************

@property
def table(self):
return self.data_container.table

@table.setter
def table(self, value):
self.data_container.table = value

@property
def data(self):
return self.data_container.data

@property
def _file(self):
return self.data_container._file

@_file.setter
def _file(self, value):
self.data_container._file = value

Check warning on line 130 in scopesim/effects/effects.py

View check run for this annotation

Codecov / codecov/patch

scopesim/effects/effects.py#L130

Added line #L130 was not covered by tests

# *******************************************************************

@property
def include(self) -> bool:
Expand Down
3 changes: 3 additions & 0 deletions scopesim/effects/effects_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
logger = get_logger(__name__)


# TODO: is this ever used anywhere??
def combine_surface_effects(surface_effects):
surflist_list = [eff for eff in surface_effects
if isinstance(eff, efs.SurfaceList)]
Expand All @@ -24,6 +25,8 @@
surflist_list = [empty_surface_list(name="combined_surface_list")]

new_surflist = copy(surflist_list[0])
new_surflist.data_container = copy(surflist_list[0].data_container)

Check warning on line 28 in scopesim/effects/effects_utils.py

View check run for this annotation

Codecov / codecov/patch

scopesim/effects/effects_utils.py#L28

Added line #L28 was not covered by tests

for surflist in surflist_list[1:]:
new_surflist.add_surface_list(surflist)

Expand Down
2 changes: 1 addition & 1 deletion scopesim/effects/metis_lms_trace_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@
# TODO: Refactor these _class_params?
self.meta = copy.copy(self._class_params)
assert "grat_spacing" in self.meta, "grat_spacing is missing from self.meta 1"
super().__init__(**kwargs)
super().__init__(**(kwargs | self.meta))

Check warning on line 568 in scopesim/effects/metis_lms_trace_list.py

View check run for this annotation

Codecov / codecov/patch

scopesim/effects/metis_lms_trace_list.py#L568

Added line #L568 was not covered by tests
assert "grat_spacing" in self.meta, "grat_spacing is missing from self.meta 2"

filename = find_file(self.meta["filename"])
Expand Down
2 changes: 1 addition & 1 deletion scopesim/effects/ter_curves.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def __init__(self, filename=None, **kwargs):
self.surface.meta.update(self.meta)
self._background_source = None

data = self.get_data()
data = self.data
if self.meta["ignore_wings"]:
data = add_edge_zeros(data, "wavelength")
if data is not None:
Expand Down
1 change: 1 addition & 0 deletions scopesim/optics/optics_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def fov_setup_effects(self):
# Working out where to set wave_min, wave_max
return self.get_z_order_effects(200)

# TODO: is this ever used anywhere??
@property
def surfaces_table(self):
"""Get combined surface table from effects with z_order = 100...199."""
Expand Down
2 changes: 1 addition & 1 deletion scopesim/tests/tests_effects/test_SpectralTraceList.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_initialises_with_nothing(self):
def test_initialises_with_a_hdulist(self, full_trace_list):
spt = SpectralTraceList(hdulist=full_trace_list)
assert isinstance(spt, SpectralTraceList)
assert spt.get_data(2, fits.BinTableHDU)
assert isinstance(spt.data_container._file[2], fits.BinTableHDU)
# next assert that dispersion axis determined correctly
assert list(spt.spectral_traces.values())[2].dispersion_axis == 'y'

Expand Down