Utils

This module contains various utility functions and classes used throughout the package.

This module contains utility functions for the project.

Testing sphinx syntax

Hint

This is a test hint for the sphinx documentation.

Data utilities

Reading and manipulating image data.

get_relative_refl_or_tran(imaging_type: str, wl: float, base_path: str) float

Returns leaf reflectance (or transmittance) divided by reference reflectance (or transmittance).

Parameters:
  • imaging_type – String, either ‘refl’ for reflectance or ‘tran’ for transmittance.

  • wl – Wavelength.

  • base_path – Top level path (working temp directory).

Returns:

Relative reflectance or transmittance as a single float.

get_rend_as_mean(image_file_path: str) float

Read image from given path and return mean of the pixels.

get_rend_as_ndarray_wl(image_file_path: str)

Read an image in given path into a Numpy array.

pack_target(wls: list[int | float], refls: list[float], trans: list[float])

Pack lists of wavelengths, reflectances and transmittances into a numpy array of tuples.

This is reverse of utils.data_utils.unpack_target(). This method can be used to pack lists of data in a form that can be passed to data.toml_handling.write_target() method.

Parameters:
  • wls – List of wavelengths.

  • refls – List of reflectances.

  • trans – List of transmittances.

Returns:

Numpy array of tuples (wl, reflectance, transmittance), which is the format for target.

unpack_target(target) tuple[list[int | float], list[float], list[float]]

Unpacks target as given by toml_handling into separate lists.

Function data.toml_handling.read_target() returns wavelengths, reflectances and transmittances as a list of tuples. In many cases it is beneficial to handle them as three separate lists. This is reverse of utils.data_utils.pack_target().

Parameters:

target – Target to be unpacked.

Returns:

3 lists: wls, refls, trans

General utilities

Utility functions for all scripts in the project.

chunks(lst, n)

Yield successive n-sized chunks from lst.

Example

original_list = [0,1,2,3,4,5,6,7,8,9] chunked_list = list(chunks(original_list, 3)) -> [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

fit_poly(x, y, degree)

Fit a plynomial function to data.

Parameters:
  • x – Data x values.

  • y – Data y values.

  • degree – Degree of the fuction to fit.

Returns:

A list of coefficients.

Spectral utilities

This file contains methods for creating target reflectances and transmittances in a form that the rest of the project code can use it.

find_nearest_idx(array, value)

Find nearest index of value in array.

Parameters:
  • array – Array to be searched for.

  • value – Value that is searched.

Returns:

Index of an element that is closest to value.

is_in_visible(wls)

Check if given wavelengths are on visible range.

Parameters:

wls – List of wavelengths to check.

Returns:

False if all wavelengths are either greater than 700 nm or lower than 380 nm. Otherwise, returns True, i.e., it is enough that even some part of the spectrum is on the visible.

resample(original_wl, original_val, new_wl)

Resample spectra to lower resolution.

Parameters:
  • original_wl – List or 1-D numpy array of wavelengths in the original spectrum.

  • original_val – List or numpy array of intensities corresponding to original_wl. If multidimensional numpy array is provided, the last dimension must match the length of original_wl. Other dimensions can be used to include multiple spectra at the same run.

  • new_wl – List or 1-D numpy array of new wavelengths to be resampled to.

Returns:

Returns resampled spectra as a numpy array. The first dimension is the same length as new_wl and other dimensions the same as in original_val.

spectra_to_rgb(wls, value)

Creates at least some sort of RGB presentation of given spectrum.

Expects value`s to be reflectance no greater than 1. If greater values are found, the whole `value list is normalized to 1.

If wavelengths are completely outside of visible range the edges and the middle point of the spectrum are used. If they are at least partially inside the visible spectrum, closest values to red, green and blue are returned. There is no guarantee that these are different values, e.g., blue and green may be the same value if not enough wavelengths exist in visible range. If only 3 wavelengths are provided they are returned. For less than 3 wavelengths, a default color is returned.

Parameters:
  • wls – Wavelengths as a list of floats.

  • value – Reflectance values as a list of floats.

Returns:

Returns an rgb value as a list of floats.

Raises:

ValueError – If given lists are different in length.