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.print
with eitherprint_quiet
orprint_flush
inside 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.print
is monkey-patched withprint_quiet
within 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_valid
to 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]#
dcraw
crop settings [x,y,w,h]
- dcraw_args: str#
dcraw
extra 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
members
argument to_load
and_save
is eitherNone
or an iterable where each item is(name, h5_name, attrs, dims, func)
. If themembers
argument isNone
, it defaults toself._h5members
.h5_name
,attrs
,dims
, andfunc
are optional, but must be specified in order. If not specified, they will default toNone
. ifh5_name
isNone
it will default toname
. If bothname
andh5_name
areNone
,h5_name
will be set to'/'
, representing the file’s root (e.g. to set file attributes). Iffunc
isNone
it 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.attrs
is eitherNone
or 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
name
toNone
andh5_name
to either the group name or'/'
to access file attributes.dims
is eitherNone
or an iterable ofh5_scale_name
or(dim_label, h5_scale_name)
. If onlyh5_scale_name
is given, it will also be used as the label. After all datasets have been written to the file, all datasets referred to byh5_scale_name
will havemake_scale()
called on them and then attached as dimension scales to the associated datasets. If theh5_scale_name
dataset does not exist aH5Storage.Warning
will 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.