util.py#
The util module contains several useful shared classes and methods for the
camera spectral calibration suite.
- class util.QuietPrint(quiet=False)[source]#
Context manager class for suppressing
print()Replaces
builtins.printwith eitherprint_quietorprint_flushinside the context. Nesting is supported:with QuietPrint(True): print('This will not print') with QuietPrint(False): print('This will print with flush=True') print('This will not print') print('This will print normally')
- Parameters:
quiet (bool) – If
True,builtins.printis monkey-patched withprint_quietwithin the context, otherwise it usesprint_flush.
- print_original(*, sep=' ', end='\n', file=None, flush=False)#
Prints the values to a stream, or to sys.stdout by default.
- sep
string inserted between values, default a space.
- end
string appended after the last value, default a newline.
- file
a file-like object (stream); defaults to the current sys.stdout.
- flush
whether to forcibly flush the stream.
- class util.Settings(path)[source]#
Camera Spectral Calibration task settings.
This class loads image stack, file paths, and other data related to the spectral calibration workflow from a yaml file. The yaml file should be formatted as:
camera: Camera model name lens: Lens model name settings: Camera & lens settings (e.g. ISO & Aperture) label: Short name for dataset ND: 0.0 # ND filter value (or 0.0 if unused) crop: [x, y, w, h] # raw->tiff crop settings dcraw: -w # dcraw command-line settings roi: [x, y, w, h] # region of interest (in cropped image) for collecting stats non_negative: True # set True to use non-negative regression for spectral response source_spectra: monochromator data directory image_dir: image file directory image_name: DSC_{:04}.NEF #image filename pattern. the {} will be replaced with the image number images: 300: [1, 2, 3] 305: [4, 5, 6] ... wavelength: [image numbers]
The first line must be
camera: ...forcheck_validto accept the file, but the other items may appear in any order. Omitted items will be set toNone.- Parameters:
path – Path to the settings yaml file.
- camera: str#
Camera name/description.
- lens: str#
Lens name/description.
- settings: str#
Camera & lens settings.
- label: str#
Short name to identify the dataset.
- nd: float#
Neutral density filter value.
- crop_xywh: list[int]#
dcrawcrop settings [x,y,w,h]
- dcraw_args: str#
dcrawextra arguments
- roi: tuple[slice, slice]#
region of interest slices for image statistics
- non_negative: bool#
True to use non-negative regression methods
- image_dir: str#
Path to image files
- image_name: str#
Image filename pattern
- images: dict[float, list[int]]#
mapping wavelength to lists of image numbers
- samples_file: str#
path to image samples file
- spectra_dir: str#
illuminant spectra data directory
- spectra_file: str#
illuminant spectra data file
- response_file: str#
spectral response data file path
- plots_file: str#
spectral response plots file path
- excel_file: str#
spectral response excel file path
- class util.H5Storage[source]#
Base class for data storage backed by H5 files.
This class provides several methods that can be used by derived classes to semi-automatically load and save data from H5 files.
E.g. in the derived class:
class Foo(H5Storage): _h5members = [('x'),('y')] def __init__(self): self.x = numpy.arange(100) def save(self): with self._open('file.h5','w') as f: self._save(f)
The
membersargument to_loadand_saveis eitherNoneor an iterable where each item is(name, h5_name, attrs, dims, func). If themembersargument isNone, it defaults toself._h5members.h5_name,attrs,dims, andfuncare optional, but must be specified in order. If not specified, they will default toNone. ifh5_nameisNoneit will default toname. If bothnameandh5_nameareNone,h5_namewill be set to'/', representing the file’s root (e.g. to set file attributes). IffuncisNoneit will default tonumpy.array.Data will be loaded as
self.name = func(file[h5_name])and stored asfile.create_dataset(h5_name,data=self.name). If the dataset already exists, then it will be overwritten (requiring that the shape matches!), if the same data was previously written to a different dataset, then a hardlink will be created rather than copy the data.attrsis eitherNoneor an iterable of(attr_name, h5_attr_name)Attributes are loaded asself.attr_name = file[h5_name].attrs[h5_attr_name]and stored asfile[h5_name].attrs[h5_attr_name] = self.attr_name.To access file or group attributes, set
nametoNoneandh5_nameto either the group name or'/'to access file attributes.dimsis eitherNoneor an iterable ofh5_scale_nameor(dim_label, h5_scale_name). If onlyh5_scale_nameis given, it will also be used as the label. After all datasets have been written to the file, all datasets referred to byh5_scale_namewill havemake_scale()called on them and then attached as dimension scales to the associated datasets. If theh5_scale_namedataset does not exist aH5Storage.Warningwill be issued.- _open(path: str, mode: Literal['r', 'r+', 'w', 'w-', 'x', 'a']) h5py.File[source]#
Open and return an h5py File.
- _load(file: h5py.File, members: list[tuple[str, ...]] | None = None, warn_missing: bool = False)[source]#
Load the specified members from file.
- Parameters:
file – an open
h5py.File.members – list of class members to load.
warn_missing – if
True, issue a warning for missing items rather than raise an exception.