Slab simulation

This is the documentation for the slab simulation module.

This package contains modules related to the slab model of HyperBlend.

There are three ways to convert reflectance and transmittance pairs (RT pairs) into renderable slab material parameters:

  1. opt: original optimization method that is very slow

  2. material_param_surface: surface fitting method that is fast but rather inaccurate

  3. material_param_neural: neural network method that is fast and slightly less accurate than optimization but much

    more accurate than surface fit. This is the recommended method.

Interface

Interface for the slab model. Most common needs can be called from here. You should avoid calling slab simulation related code directly unless it cannot be avoided. It just keeps the hierarchy more clear.

generate_prospect_leaf(slab_sim_name, signal_id=0, n=None, ab=None, ar=None, brown=None, w=None, m=None, ant=None)

Run prospect simulation with given arguments.

Calling this is the same as calling prospect.interface.make_leaf_target(). See explanation of the arguments there. If any of the values are not provided, default values are used. You get the default PROSPECT leaf by calling without any of the optional arguments.

Parameters:
  • slab_sim_name – See slab_sim_name.

  • signal_id – Signal id for this target. Default is 0. Overwrites existing targets if existing id is given.

  • n – PROSPECT N parameter [unitless]

  • ab – chlorophyll a + b concentration [ug / cm^2]

  • ar – carotenoid content [ug / cm^2]

  • brown – brown pigment [unitless]

  • w – equivalent water thickness [cm]

  • m – dry mater content [g / cm^2]

  • ant – anthocyanin content [ug / cm^2]

generate_prospect_leaf_random(slab_sim_name, leaf_count=1)

Generate one or more random PROSPECT leaves.

Calling this is the same as calling prospect.interface.make_random_leaf_targets().

Parameters:
  • slab_sim_name – See slab_sim_name.

  • leaf_count – How many target leaves will be generated.

iterative_train(runtime: RuntimeEnvironment, iterations=8, train_points_per_dim=200, dry_run=False)

Iteratively train the slab models several times.

Note

This method has many hard-coded values that are passed to slab_simulation.interface.train_models(). You may want to modify them to your needs.

Parameters:
resample_slab_sim_target(slab_sim_name: str, wls: list[int] | ndarray = None, range_start: int = None, range_end: int = None, resolution: int = None)

Runs a spectral resampling of all target signals in this slab simulation.

After this, you must solve leaf material parameters (for rendering) again by calling solve_leaf_material_parameters(). Reads the old sampling information from sampling.toml in targets directory and rewrites it before calling the actual resampling from leaf_sampling.resample().

Parameters:
  • slab_sim_name – See slab_sim_name.

  • wls – List of new wavelengths. If given, this overrides range_start, range_end and resolution.

  • range_start – Start of the wavelength range to be resampled (inclusive).

  • range_end – End of the wavelength range to be resampled (inclusive).

  • resolution – Resolution of the sampling in nm.

Raises:

AttributeError – If neither wls nor range_start, range_end and resolution are given. This error is also raised if range_start is less than the minimum wavelength or range_end is greater than the maximum wavelength in the target data.

solve_slab_material_parameters(runtime: RuntimeEnvironment, slab_sim_name: str, range_start: int = None, range_end: int = None, resolution=None, wls: list[int] | ndarray = None, solver='nn', clear_old_results=False, solver_dirname: str = None, copyof: str | None = None)

Solves leaf material parameters that are needed for rendering.

The result is saved to disk (the actual data and plots).

If any of wls, range_start, range_end and resolution are given, spectral resampling is called before solving the slab parameters. See documentation of these arguments from slab_simulation.interface.resample_slab_sim_target().

Note

Solvers ‘surf’ and ‘nn’ need a trained model to work. Pre-trained model are included in the Git repository, but you can train your own using slab_simulation.interface.train_models() method. Solver ‘opt’ does not need prior training, but it is slow. In case you have several trained models (whether surf or nn), you can also provide the solver_dirname to specify which solver to use.

Parameters:
  • runtime – See runtime.

  • slab_sim_name – See slab_sim_name.

  • solver – Solving method either ‘opt’, ‘surf’ or ‘nn’. Opt is slowest and most accurate (the original method). Surf is fast but not very accurate. NN is fast and fairly accurate. Surf and NN are roughly 200 times faster than opt. Recommended solver is the default ‘nn’.

  • clear_old_results – If True, clear old results of the slab simulation. This is handy for redoing the same slab simulation with different method, for example. When False, the old results are not overwritten and solver just skips the signals that are already solved.

  • solver_dirname – Name of the (directory of the) solver to be used. If None, the default solver is used.

  • copyof – Name of the slab simulation to copy. Copies target from existing slab simulation (walengths, reflectances, and transmittances).

train_models(runtime: RuntimeEnvironment, slab_sim_name_for_training='training_data', generate_data=False, data_generation_diff_step=0.01, starting_guess_type='curve', similarity_rt=0.25, train_surf=True, train_nn=True, layer_count=5, layer_width=1000, epochs=300, batch_size=32, learning_rate=0.01, patience=30, split=0.1, train_points_per_dim=20, dry_run=False, show_plot=False, solver_name_to_save=None, solver_name_to_use=None)

Train surface model and neural network.

If training data does not yet exist, it must be created by setting generate_data=True. Note that this will take a lot of time as the data generation uses the original optimization method explained in

[RRPolonen22]. Depending on value of train_points_per_dim the generation time varies from tens of minutes to several days. You should generate a few thousand points (which equals to train_points_per_dim\(^{2}\)) at least for any accuracy. Models in the repository were trained with 40 000 points (4 days generation time). Use dry_run=True just to print the number of points that would have been generated.

You can select to train surface model (train_surf) and neural network (train_nn) separately or just generate the points by setting both to False.

The plots are saved to the disk even if show_plot is set to False.

Parameters:
  • runtime – See runtime.

  • slab_sim_name_for_training – The name of the slab simulation that contains or will contain the training data. New training data is generated with this name if generate_data=True. Otherwise, existing data with this name is used.

  • generate_data – If True, new training data is generated with given slab_sim_name_for_training. Default is False. The training data must exist in order to train the models.

  • data_generation_diff_step – Used in slab_simulation.opt.Optimization as a stepsize for finite difference Jacobian estimation. Smaller step gives better results, but the variables look cloudy. Big step is faster and variables smoother but there will be outliers in the results. Good stepsize is between 0.001 and 0.01.

  • starting_guess_type – String, one of ‘hard-coded’, ‘curve’, ‘surf’ in order of increasing complexity. Hard-coded is only needed if training the other methods from absolute scratch (for example if leaf material parameter count or bounds change in future development). Curve fitting ‘curve’ is the method presented in the first HyperBlend paper [RRPolonen22]. It will only work in cases where R and T are relatively close to each other (around +- 0.2). Surface fitting method ‘surf’ can be used after the first training iteration has been carried out. It can more robustly adapt to situations where R and T are dissimilar.

  • similarity_rt – Controls the symmetry of generated pairs, i.e., how much each R value can differ from respective T value. Using greater than 0.25 will cause generating a lot of points that will fail to be optimized properly (and will be pruned before training). This wastes computational resources. Good results were obtained in [RLC+25] by training multiple times and gradually loosening the similarity requirement.

  • train_surf – If True, train the surface model. Default is True.

  • train_nn – If True, train the neural network. Default is True.

  • layer_count – Number of hidden layers in neural network. Omitted if train_nn=False.

  • layer_width – Width (in number of nodes) of hidden layers in neural network. Omitted if train_nn=False.

  • epochs – Maximum number of epochs the neural network is trained. Omitted if train_nn=False.

  • batch_size – Batch size when training neural network. Omitted if train_nn=False. Smaller values (e.g. 2) yield better accuracy while bigger values (e.g. 32) train faster.

  • learning_rate – Learning rate of the Adam optimizer. Default value of 0.001 is good and this has very little effect on training results. Feel free to test different values. Omitted if train_nn=False.

  • patience – Stop NN training if the loss has not improved in this many epochs. Omitted if train_nn=False.

  • split – Percentage [0,1] of data reserved for testing between epochs. Value between 0.1 and 0.2 is usually sufficient. Omitted if train_nn=False.

  • train_points_per_dim – Into how many parts each dimension (R,T) are cut in interval [0,1]. Greater value results in more training points. Good values from 100 to 500. For testing purposes, low values, e.g., 20 can be used. Omitted if generate_data=False.

  • dry_run – Print the number of points that would have been generated, but does not really generate the training points. Omitted if generate_data=False.

  • show_plot – If True, shows interactive plots to user (which halts execution until window is closed). Regardless of this value, the plots are saved to disk. Default is False.

  • solver_name_to_save – Solver used to save the generated training data and used to train the models if any.

  • solver_name_to_use – Name of the solver to be used. If None, default solver name is used. For iterative training, this should be the name of the previous iteration’s solver.

visualize_slab_model_training(training_slab_sim_name: str, show_plot=False, plot_surf=True, plot_nn=True, plot_points=True, solver_name=None)

Visualize trained surface and neural network model against training data.

The plot is always saved to disk regardless of show_plot attribute.

Parameters:
  • training_slab_sim_name – Name of the slab simulation that was used as training data.

  • show_plot – If True, show interactive plot. Default is false.

  • plot_surf – If True, plot surface model against training data. Default is False.

  • plot_nn – If True, plot neural network model against training data. Default is True.

  • plot_points – If True, plot training data points. Default is True.

  • solver_name – Name of the solver to be plotted. Default is None and plots the default solver.

Commons

Shared functionality that is used by all slab models: optimization, surface, and neural network.

_build_sample_res_dict(wls, r, r_m, re, t, t_m, te, ad_raw, sd_raw, ai_raw, mf_raw, elapsed_process_min, elapsed_wall_clock_min)

Builds result dictionary to be saved on disk.

Parameters:
  • wls – Wavelengths.

  • r – Modeled reflectances.

  • r_m – Measured reflectances.

  • re – Error of modeled reflectances.

  • t – Modeled transmittances.

  • t_m – Measured transmittances.

  • te – Error of modeled transmittances.

  • ad_raw – Numpy array absorption particle density [0,1].

  • sd_raw – Numpy array scattering particle density [0,1].

  • ai_raw – Numpy array scattering anisotropy [0,1].

  • mf_raw – Numpy array mix factor [0,1].

  • elapsed_process_min – Elapsed time of the processes. Applicable only for optimization method. For surface and NN method this will be the same as elapsed_wall_clock_min.

  • elapsed_wall_clock_min – Elapsed wall clock time.

Returns:

Returns built dictionary.

_convert_raw_params_to_renderable(ad_raw, sd_raw, ai_raw, mf_raw)

Convert machine learning parameters in range [0,1] to rendering parameters (scaling and re-centering).

Parameters:
  • ad_raw – Numpy array absorption particle density [0,1].

  • sd_raw – Numpy array scattering particle density [0,1].

  • ai_raw – Numpy array scattering anisotropy [0,1].

  • mf_raw – Numpy array mix factor [0,1].

Returns:

Returns corresponding (ad, sd, ai, mf) that can be fed to rendering script.

_material_params_to_RT(runtime: RuntimeEnvironment, slab_sim_name: str, signal_id: int, wls, ad, sd, ai, mf)

Material parameters are converted to reflectance and transmittance by rendering the slab model.

Parameters:
  • slab_sim_name – Name of the slab simulation.

  • signal_id – Signal ID.

  • wls – Numpy array wavelengths.

  • ad – Numpy array absorption particle density.

  • sd – Numpy array scattering particle density.

  • ai – Numpy array scattering anisotropy.

  • mf – Numpy array mix factor.

Returns:

Returns (r,t) lists of reflectances and transmittances, respectively.

_render(args)

Internal render function to be called from parallel code.

Unpacks given arguments. NOTE that they must be given in correct order so this is sensitive to refactoring.

density_scale = 3000

Control how much density variables (absorption and scattering density) are scaled for rendering. Value of 1000 cannot produce r = 0 or t = 0. Produced values do not significantly change when greater than 3000.

initialize_directories(slab_sim_name, clear_old_results=False)

Create necessary directories.

Optionally, one can wipe out old results of the same slab simulation by setting clear_old_results=True.

Parameters:
  • slab_sim_name – Slab simulation name.

  • clear_old_results – If True, old results are deleted.

Sampling

check_sampling(set_name: str) bool

Check that leaf material parameters are solved with current sampling.

TODO: This is not currently used in anywhere. Check if this is really necessary.

Parameters:

set_name

Returns:

Returns True if either, sampling information file does not contain resampling wavelengths or if the sample results and set results have the same wavelengths as the sampling information file. Otherwise, return False.

resample(slab_sim_name: str, plot_resampling=True)

Resamples leaf spectra to lower resolution as defined in /sample_target/sampling.toml.

Only rewrites target data. You must solve renderable leaf parameters again after resampling.

sampling_empty(set_name: str) bool

Optimization

Optimization class and related methods.

LOWER_BOUND = [0.0, 0.0, 0.0, 0.0]

Lower constraints of the minimization problem. Absorption and scattering particle density cannot be exactly zero as it may cause problems in rendering.

class Optimization(runtime: RuntimeEnvironment, set_name: str, ftol=0.01, ftol_abs=1.0, xtol=1e-05, diffstep=0.01, starting_guess_type='curve', clear_old_results=False, solver_name=None)

Optimization class runs a least squares optimization of the HyperBlend leaf spectral model against a set of measured leaf spectra.

run_optimization(use_threads=True, use_basin_hopping=False, resampled=True)

Runs the optimization for each sample in the set.

It is safe to interrupt this method at any point as intermediate results are saved to disk and existing results are not optimized again.

Loops through target toml files in set’s target folder.

Parameters:
  • use_threads – If True, use parallel computation on CPU.

  • use_basin_hopping – If True, use basin hopping algorithm on top of the default least squares method. It helps in not getting stuck to local optima, but is significantly slower.

  • resampled – If False, ignore sampling and use maximum available spectral resolution. Default is True. This is ignored in training data generation where we have fake wavelengths.

UPPER_BOUND = [1.0, 1.0, 1.0, 1.0]

Upper limit of the minimization problem.

get_starting_guess(absorption: float, solver_name: str = None) tuple

Gives starting guess for given absorption.

This is only used with the optimization method–not nn or surface fitting.

hard_coded_starting_guess = [0.28, 0.43, 0.55, 0.28]

This should be used only if the starting guess based on polynomial fitting is not available. Will produce worse results and is slower.

optimize_single_wl(wl: float, r_m: float, t_m: float, set_name: str, diffstep, ftol, xtol, bounds, density_scale, optimizer_verbosity, use_basin_hopping: bool, sample_id: int, ftol_abs, starting_guess_type, solver_name, runtime: RuntimeEnvironment)

Optimize single wavelength to given reflectance and transmittance.

Result is saved in a .toml file and plotted as an image.

param wl:

Wavelength to be optimized.

param r_m:

Measured reflectance.

param t_m:

Measured transmittance.

param set_name:

Set name (name of the working folder).

param diffstep:

Stepsize for finite difference Jacobian estimation. Smaller step gives better results, but the variables look cloudy. Big step is faster and variables smoother but there will be outliers in the results. Good stepsize is between 0.001 and 0.01.

Parameters:

ftol

Function value (difference between measured and modeled) change between

iterations considered as ‘still converging’. This is a stop criterion for the optimizer. Smaller value leads to more accurate result, but increases the optimization time. Works in tandem with xtol, so whichever value is reached first will stop the optimization for that wavelength.

param xtol:

Controls how much the leaf material parameters need to change between i terations to be considered ‘progressing’. Greater value stops the optimization earlier.

param bounds:

Bounds of the optimization problem. A tuple ([l1,l2,..], [h1,h2,…]).

param density_scale:

Scaling parameter for absorption and scattering density. The values for rendering are much higher than the ones used in optimization.

param optimizer_verbosity:

Optimizer verbosity: 0 least verbose, 2 very verbose.

param use_basin_hopping:

If True, use basin hopping algorithm to escape local minima (reduce outliers). Using this considerably slows down the optimization (nearly two-fold).

param sample_id:

Sample id.

param ftol_abs:

Absolute termination condition for basin hopping. There will be no more basin hopping iterations if reached function value is smaller than this value. Only used if run with basin hopping algorithm, which can help if optimization gets caught in local minima. Basin hopping can be turned on when Optimization.run() is called.

param starting_guess_type:

One of ‘hard-coded’, ‘curve’, ‘surf’ in order of increasing complexity. Hard-coded ‘hard-coded’ is only needed if training the other methods from absolute scratch (for example if leaf material parameter count or bounds change in future development). Curve fitting ‘curve’ is the method presented in the first HyperBlend paper. It will only work in cases where R and T are relatively close to each other (around +- 0.2). Surface fitting method ‘surf’ can be used after the first training iteration has been carried out. It can more robustly adapt to situations where R and T are dissimilar.

param solver_name:

The name of the solver to be used. If None given, the default one will be used.

optimize_single_wl_threaded(args)

Unpacks arguments from pool.map call.

Surface fitting model

Slab simulation surface model implementation.

exists(solver_dirname=None)

Checks whether surface model parameters exist.

Parameters:

solver_dirname

Returns:

Returns True if surface model parameters exist, False otherwise.

predict(target_refl, target_tran, solver_dirname: str)

Predicts the surface model parameters for given r_m and t_m.

Parameters:
  • target_refl – Target reflectance.

  • target_tran – Target transmittance.

  • solver_dirname – Name of the (directory of the) surface model to be used. If None, the default surface model is used.

Returns:

Lists ad, sd, ai, mf (absorption density, scattering density, scattering anisotropy, and mixing factor). Use <slab_commons._convert_raw_params_to_renderable()>() before passing them to rendering method.

train(training_sim_name='training_data', solver_save_name: str = None)

Train surface model.

Parameters:

training_sim_name – Name of the training data loaded for slab simulation. A new solver will be saved with this name. Note that the training data actually is another slab simulation; just a special kind where we generate the training data points and solved their material parameters with the optimization method. No need to change the default name unless you generated the data with custom name.

Functions for the Surface fitting model

function_exp(data, a, b, c, d)

Function used in fitting parameter surface to reflectance-transmittance value pairs.

Used in fitting and in retrieving Blender parameter values.

function_log(data, a, b, c, d)

Function used in fitting parameter surface to reflectance-transmittance value pairs.

Used in fitting and in retrieving Blender parameter values.

function_polynomial(data, a, b, c, d)

Function used in fitting parameter surface to reflectance-transmittance value pairs.

Used in fitting and in retrieving Blender parameter values.

get_x0()
safe_log(x)

Neural network

Slab model implementation as a neural network.

Greatly accelerates prediction time compared to the original optimization method with some loss to accuracy.

class Slabnet(layer_count=5, layer_width=1000)

Neural network implementation.

forward(x)

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class TrainingData(training_sim_name: str)

Handles catering the training data from disk to NN.

_load_model(solver_dirname: str)

Loads the NN from disk.

Parameters:

solver_dirname – Name of the (directory of the) neural network to be used. If None, the default is used.

Returns:

Returns loaded NN.

Raises:

ModuleNotFoundError – If PyTorch cannot load the requested neural network.

exists(solver_mame: str = None)

Checks whether NN with given name exists.

Returns:

True if found, False otherwise.

predict(target_refl, target_tran, solver_dirname: str = None)

Use neural network to predict HyperBlend slab model parameters from target reflectance and transmittance.

Parameters:
  • target_refl – Target reflectance.

  • target_tran – Target transmittance.

  • solver_dirname – Name of the (directory of the) neural network to be used. If None, the default is used.

Returns:

Lists ad, sd, ai, mf (absorption density, scattering density, scattering anisotropy, and mixing factor). Use slab_commons._convert_raw_params_to_renderable() before passing them to rendering method.

train(show_plot=False, layer_count=10, layer_width=1000, epochs=300, batch_size=2, learning_rate=0.001, patience=30, split=0.1, training_sim_name='training_data', solver_name=None)

Train the neural network with given parameters.

Saves the best performing model onto disk with generated name (according to NN architecture and some training parameters). You can manually change the name later to ‘nn_default.pt’ if you want to replace the old default network that comes from the Git repository.

Parameters:
  • show_plot – Show training history at the end of training. Set False when doing multiple runs. The plot is always saved to disk, even if not shown. Default is False.

  • layer_count – Number of hidden layers.

  • layer_width – Width of hidden layers.

  • epochs – Maximum epochs.

  • batch_size – Batch size. Best results with small batches (2). Bigger batches (e.g. 32) train faster but reduce accuracy.

  • learning_rate – Learning rate for Adam optimizer. Default 0.001 usually performs best.

  • patience – Early stop training if test loss has not improved in this many epochs.

  • split – Percentage [0,1] of data reserved for testing between epochs. Value between 0.1 and 0.2 is usually sufficient.

  • training_sim_name – Name of the training data slab simulation. A new solver will be saved with this name. Note that the training data actually is another slab simulation; just a special kind where we generate the training data points and solved their material parameters with the optimization method. No need to change the default name unless you generated the data with custom name.

  • solver_name – Trained solver is saved with this name.

Returns:

Returns the best loss for hyperparameter tuning loops.

Training data

Functionality regarding training data generation for surface model and neural network training.

This is somewhat specific functionality so it is not included in the leaf model interface script.

_make_target(set_name: str, wls, r_m, t_m, sample_id=None)

Write target reflectances and transmittances to disk.

Parameters:
  • set_name – Set name to be used.

  • wls – A list of wavelengths to be saved.

  • r_m – A list of measured reflectances to be saved.

  • t_m – A list of measured transmittances to be saved.

Returns:

None

fit_starting_guess_coefficients(degree=4, slab_sim_name: str = None, solver_name: str = None)

Fits polynomial coefficients to linear test run that are used as a starting guess for the optimization.

NOTE: One run of this is already stored in the code repo, so this only needs to be done if they corrupt somehow.

generate_starting_guess() must be run before this method can be run.

Parameters:
  • degree – Degree of the polynomial to fit.

  • slab_sim_name – Custom set name to fetch the data from. If not given, default set name variable ‘starting_guess_set_name’ stored in constants.py is used.

  • solver_name – If not given, default solver name is used.

Returns:

None.

generate_starting_guess(runtime: RuntimeEnvironment, slab_sim_name: str = None, solver_name: str = None, step=None)

Generates starting guess to be used later on real data.

Starting guess is generated by running the optimization procedure on a test target that linearly decreases absorption. The result is saved to root folder of the project.

Note

This does not need to be done unless the starting guess in the code repository is corrupt or missing.

Parameters:
  • slab_sim_name – Name of the slab simulation to be used to store the starting guess data.

  • solver_name – Name of the solver to be used. If None, default solver name is used.

  • step – The interval between the wavelengths. Default is 1 nm if None given. One can use longer step size for testing.

generate_train_data(runtime: RuntimeEnvironment, slab_sim_name='training_data', dry_run=True, train_points_per_dim=10, similarity_rt=0.25, starting_guess_type='curve', data_generation_diff_step=0.01, solver_name_to_use: str = None, solver_name_to_save: str = None)

Generate reflectance-transmittance pairs as training data for surface fitting and neural network.

Generated data will have fake wavelengths attached to them. They run from 1 to the number of generated points.

If dry_run=True, only pretends to generate the points. This is useful for testing how different train_points_per_dim values affect the actual point count.

Data visualization is saved to disk when the data has been generated.

Parameters:
  • data_generation_diff_step – Used in slab_simulation.opt.Optimization as a stepsize for finite difference Jacobian estimation. Smaller step gives better results, but the variables look cloudy. Big step is faster and variables smoother but there will be outliers in the results. Good stepsize is between 0.001 and 0.01.

  • slab_sim_name – Optionally change the slab_sim_name that is used for destination directory. If other than default is used, it must be taken into account when training, i.e., pass the same name for training method.

  • dry_run – If true, just prints how many points would have been generated. Note that it is not the same as cuts_per_dim ^2 because parts of the space are not usable and will be cut out.

  • train_points_per_dim – Into how many parts each dimension (R,T) are cut in interval [0,1].

  • similarity_rt – Controls the symmetry of generated pairs, i.e., how much each R value can differ from respective T value. Using greater than 0.25 will cause generating a lot of points that will fail to be optimized properly (and will be pruned before training).

  • starting_guess_type – One of ‘hard-coded’, ‘curve’, ‘surf’ in order of increasing complexity. Hard-coded ‘hard-coded’ is only needed if training the other methods from absolute scratch (for example if leaf material parameter count or bounds change in future development). Curve fitting ‘curve’ is the method presented in the first HyperBlend paper. It will only work in cases where R and T are relatively close to each other (around +- 0.2). Surface fitting method ‘surf’ can be used after the first training iteration has been carried out. It can more robustly adapt to situations where R and T are dissimilar.

  • solver_name_to_use – Name of the solver to be used. If None, default solver name is used.

  • solver_name_to_save – Solver used to save the generated training data.

make_linear_test_target(set_name: str, step: int = None)

Creates a test target where reflectance and transmittance grow linearly from 0 to 0.5.

Wavelength ranges from 400 to 2500.

Parameters:
  • set_name – Set name to be used (such as ‘linearity_test’).

  • step – The interval between the wavelengths. Default is 1 nm. One can use longer step size for testing purposes.

Returns:

None

visualize_training_data_pruning(set_name='training_data', show=False, save=True, solver_name=None)

Visualizes training data. Can be saved to disk or shown directly (or both).

Parameters:
  • solver_name

  • set_name – Name of the training data set. Change only if custom name was used in data generation.

  • show – Show interactive plot to user. Default is `False`.

  • save – Save plot to disk. Default is `True`.

Some common training utilities for the slab model for avoiding circular imports.

get_starting_guess_points(slab_sim_name: str = None)

Get starting guess points.

NOTE: Points where reflectance or transmittance error exceeds 0.002 are deleted.

Parameters:

slab_sim_name – Custom set name to fetch the data from. If not given, default set name variable ‘starting_guess_set_name’ stored in constants.py is used.

Returns:

a_list, ad_list, sd_list, ai_list, mf_list

get_training_data(training_sim_name: str)

Returns training data.

Parameters:

training_sim_name – Name of the training data slab simulation. Note that the training data actually is another slab simulation; just a special kind where we generate the training data points and solved their material parameters with the optimization method.

Returns:

Returns ad, sd, ai, mf, r, t, re, te Numpy arrays (vector).

prune_training_data(ad, sd, ai, mf, r, t, re, te, invereted=False)

Prune bad datapoints from training data.

Data point is considered bad if either reflectance or transmittance error is more than 1%.

Parameters:
  • ad – Numpy array absorption particle density.

  • sd – Numpy array scattering particle density.

  • ai – Numpy array scattering anisotropy.

  • mf – Numpy array mix factor.

  • r – Numpy array reflectance.

  • t – Numpy array transmittance.

  • re – Numpy array reflectance error.

  • te – Numpy array transmittance error.

  • invereted – If true, instead of good points, the bad points will be returned.

Returns:

Pruned ad,sd,ai,mf,r,t corresponding to arguments.