ModelContainer¶
- class jwst.datamodels.ModelContainer(init=None, asn_exptypes=None, asn_n_members=None, iscopy=False, **kwargs)[source]¶
Bases:
JwstDataModel
,Sequence
A container for holding DataModels.
This functions like a list for holding DataModel objects. It can be iterated through like a list, DataModels within the container can be addressed by index, and the datamodels can be grouped into a list of lists for grouped looping, useful for NIRCam where grouping together all detectors of a given exposure is useful for some pipeline steps.
- Parameters
init (file path, list of DataModels, or None) –
file path: initialize from an association table
list: a list of DataModels of any type
None: initializes an empty
ModelContainer
instance, to which DataModels can be added via theappend()
method.
asn_exptypes (str) – list of exposure types from the asn file to read into the ModelContainer, if None read all the given files.
asn_n_members (int) – Open only the first N qualifying members.
iscopy (bool) – Presume this model is a copy. Members will not be closed when the model is closed/garbage-collected.
Examples
>>> container = ModelContainer('example_asn.json') >>> for model in container: ... print(model.meta.filename)
Say the association was a NIRCam dithered dataset. The
models_grouped
attribute is a list of lists, the first index giving the list of exposure groups, with the second giving the individual datamodels representing each detector in the exposure (2 or 8 in the case of NIRCam).>>> total_exposure_time = 0.0 >>> for group in container.models_grouped: ... total_exposure_time += group[0].meta.exposure.exposure_time
>>> c = ModelContainer() >>> m = datamodels.open('myfile.fits') >>> c.append(m)
Notes
The optional paramters
save_open
andreturn_open
can be provided to control how theDataModel
are used by theModelContainer
. Ifsave_open
is set toFalse
, each inputDataModel
instance ininit
will be written out to disk and closed, then only the filename for theDataModel
will be used to initialize theModelContainer
object. Subsequent access of each member will then open theDataModel
file to work with it. Ifreturn_open
is alsoFalse
, then theDataModel
will be closed when access to theDataModel
is completed. The use of these parameters can minimize the amount of memory used by this object during processing, with these parameters being used byOutlierDetectionStep
.When ASN table’s members contain attributes listed in
RECOGNIZED_MEMBER_FIELDS
,ModelContainer
will read those attribute values and update the corresponding attributes in themeta
of input models.Example of ASN table with additional model attributes to supply custom catalogs.¶"products": [ { "name": "resampled_image", "members": [ { "expname": "input_image1_cal.fits", "exptype": "science", "tweakreg_catalog": "custom_catalog1.ecsv" }, { "expname": "input_image2_cal.fits", "exptype": "science", "tweakreg_catalog": "custom_catalog2.ecsv" } ] } ]
Warning
Input files will be updated in-place with new
meta
attribute values when ASN table’s members contain additional attributes.- Parameters
init (str, tuple,
HDUList
, ndarray, dict, None) –None : Create a default data model with no shape.
tuple : Shape of the data array. Initialize with empty data array with shape specified by the.
file path: Initialize from the given file (FITS or ASDF)
readable file object: Initialize from the given file object
HDUList
: Initialize from the givenHDUList
.A numpy array: Used to initialize the data array
dict: The object model tree for the data model
schema (dict, str (optional)) – Tree of objects representing a JSON schema, or string naming a schema. The schema to use to understand the elements on the model. If not provided, the schema associated with this class will be used.
memmap (bool) – Turn memmap of FITS file on or off. (default: False). Ignored for ASDF files.
pass_invalid_values (bool or None) – If
True
, values that do not validate the schema will be added to the metadata. IfFalse
, they will be set toNone
. IfNone
, value will be taken from the environmental PASS_INVALID_VALUES. Otherwise the default value isFalse
.strict_validation (bool or None) – If
True
, schema validation errors will generate an exception. IfFalse
, they will generate a warning. IfNone
, value will be taken from the environmental STRICT_VALIDATION. Otherwise, the default value isFalse
.validate_on_assignment (bool or None) – Defaults to ‘None’. If
None
, value will be taken from the environmental VALIDATE_ON_ASSIGNMENT, defaulting to ‘True’ if no environment variable is set. If ‘True’, attribute assignments are validated at the time of assignment. Validation errors generate warnings and values will be set toNone
. If ‘False’, schema validation occurs only once at the time of write. Validation errors generate warnings.cast_fits_arrays (bool) – If
True
, arrays will be cast to the dtype described by the schema when read from a FITS file. IfFalse
, arrays will be read without casting.validate_arrays (bool) – If
True
, arrays will be validated against ndim, max_ndim, and datatype validators in the schemas.ignore_missing_extensions (bool) – When
False
, raise warnings when a file is read that contains metadata about extensions that are not available. Defaults toTrue
.kwargs (dict) –
Additional keyword arguments passed to lower level functions. These arguments are generally file format-specific. Arguments of note are:
Attributes Summary
Get the CRDS observatory for this container.
Return list of names for the DataModel groups by exposure.
Returns a list of a list of datamodels grouped by exposure.
The schema URI to validate the model against.
Methods Summary
append
(model)close
()Close all datamodels.
copy
([memo])Returns a deep copy of the models in this model container.
extend
(model)from_asn
(asn_data[, asn_file_path])Load fits files from a JWST association file.
Get CRDS parameters for this container.
Iterator to return the sections from all members of the container.
ind_asn_type
(asn_exptype)Determine the indices of models corresponding to
asn_exptype
.insert
(index, model)pop
([index])read_asn
(filepath)Load fits files from a JWST association file.
save
([path, dir_path, save_model_func])Write out models in container to FITS or ASDF.
set_buffer
(buffer_size[, overlap])Set buffer size for scrolling section-by-section access.
Attributes Documentation
- crds_observatory¶
Get the CRDS observatory for this container. Used when selecting step/pipeline parameter files when the container is a pipeline input.
- Return type
- group_names¶
Return list of names for the DataModel groups by exposure.
- models_grouped¶
Returns a list of a list of datamodels grouped by exposure. Assign an ID grouping by exposure.
Data from different detectors of the same exposure will have the same group id, which allows grouping by exposure. The following metadata is used for grouping:
meta.observation.program_number meta.observation.observation_number meta.observation.visit_number meta.observation.visit_group meta.observation.sequence_id meta.observation.activity_id meta.observation.exposure_number
- schema_url = None¶
The schema URI to validate the model against. If None, only basic validation of required metadata properties (filename, model_type) will occur.
Methods Documentation
- from_asn(asn_data, asn_file_path=None)[source]¶
Load fits files from a JWST association file.
- Parameters
asn_data (Association) – An association dictionary
asn_file_path (str) – Filepath of the association, if known.
- get_crds_parameters()[source]¶
Get CRDS parameters for this container. Used when selecting step/pipeline parameter files when the container is a pipeline input.
- Return type
- static read_asn(filepath)[source]¶
Load fits files from a JWST association file.
- Parameters
filepath (str) – The path to an association file.
- save(path=None, dir_path=None, save_model_func=None, **kwargs)[source]¶
Write out models in container to FITS or ASDF.
- Parameters
path (str or func or None) –
If None, the
meta.filename
is used for each model.If a string, the string is used as a root and an index is appended.
If a function, the function takes the two arguments: the value of model.meta.filename and the
idx
index, returning constructed file name.
dir_path (str) – Directory to write out files. Defaults to current working dir. If directory does not exist, it creates it. Filenames are pulled from
meta.filename
of each datamodel in the container.save_model_func (func or None) – Alternate function to save each model instead of the models
save
method. Takes one argument, the model, and keyword argumentidx
for an index.
- Returns
output_paths – List of output file paths of where the models were saved.
- Return type
[str[, …]]