nifty.cl.extra module#

assert_allclose(f1, f2, atol=0, rtol=1e-07)[source]#
check_linear_operator(op, domain_dtype=<class 'numpy.float64'>, target_dtype=<class 'numpy.float64'>, atol=1e-14, rtol=1e-14, only_r_linear=False, force_device_ids=[-1], assert_fixed_device=True, no_device_copies=True, _device_ids=None)[source]#

Checks an operator for algebraic consistency of its capabilities.

Checks whether times(), adjoint_times(), inverse_times() and adjoint_inverse_times() (if in capability list) is implemented consistently. Additionally, it checks whether the operator is linear.

By default, tests are performed on the CPU and if available on the GPU with device_id=0. Any device_ids that are passed through force_device_ids are added to that list.

Parameters:
  • op (LinearOperator) – Operator which shall be checked.

  • domain_dtype (dtype) – The data type of the random vectors in the operator’s domain. Default is np.float64.

  • target_dtype (dtype) – The data type of the random vectors in the operator’s target. Default is np.float64.

  • atol (float) – Absolute tolerance for the check. If rtol is specified, then satisfying any tolerance will let the check pass. Default: 0.

  • rtol (float) – Relative tolerance for the check. If atol is specified, then satisfying any tolerance will let the check pass. Default: 0.

  • only_r_linear (bool) – set to True if the operator is only R-linear, not C-linear. This will relax the adjointness test accordingly.

  • force_device_ids (list of int) – List of device ids on which the operator definitely shall be tested. Default: -1 (cpu).

  • assert_fixed_device (bool) – Determines if the test shall fail if input and output of the operator are on different devices. This tests only works if the domain and target of the operator are the same or if the whole input is stored on one device and the whole output is stored on one device. Default: True

check_operator(op, loc, tol=1e-12, ntries=100, perf_check=True, only_r_differentiable=True, metric_sampling=True, force_device_ids=[-1], assert_fixed_device=True, no_device_copies=True)[source]#

Performs various checks of the implementation of linear and nonlinear operators.

Computes the Jacobian with finite differences and compares it to the implemented Jacobian.

By default, tests are performed on the CPU and if available on the GPU with device_id=0. Any device_ids that are passed through force_device_ids are added to that list.

Parameters:
  • op (Operator) – Operator which shall be checked.

  • loc (nifty.cl.field.Field or nifty.cl.multi_field.MultiField) – An Field or MultiField instance which has the same domain as op. The location at which the gradient is checked

  • tol (float) – Tolerance for the check.

  • perf_check (Boolean) – Do performance check. May be disabled for very unimportant operators.

  • only_r_differentiable (Boolean) – Jacobians of C-differentiable operators need to be C-linear. Default: True

  • metric_sampling (Boolean) – If op is an EnergyOperator, metric_sampling determines whether the test shall try to sample from the metric or not.

  • force_device_ids (list of int) – List of device ids on which the operator definitely shall be tested. Default: -1 (cpu).

  • assert_fixed_device (bool) – Determines if the test shall fail if input and output of the operator are on different devices. This tests only works if the domain and target of the operator are the same or if the whole input is stored on one device and the whole output is stored on one device. Default: True

minisanity(likelihood_energy, samples, terminal_colors=True, return_values=False)[source]#

Log information about the current fit quality and prior compatibility.

Log a table with fitting information for the likelihood and the prior. Assume that the variables in energy.position.domain are standard-normal distributed a priori. The table contains the reduced chi^2 value, the mean and the number of degrees of freedom for every key of a MultiDomain. If the domain is a DomainTuple, the displayed key is <None>.

If everything is consistent the reduced chi^2 values should be close to one and the mean of the data residuals close to zero. If the reduced chi^2 value in latent space is significantly bigger than one and only one degree of freedom is present, the mean column gives an indication in which direction to change the respective hyper parameters.

Ignore all NaN entries in the target of modeldata_operator and in data. Print reduced chi-square values above 2 and 5 in orange and red, respectively.

Parameters:
  • likelihood_energy (LikelihoodEnergyOperator) – Likelihood energy of which the normalized residuals shall be computed.

  • samples (SampleListBase) – List of samples.

  • terminal_colors (bool, optional) – Setting this to false disables terminal colors. This may be useful if the output of minisanity is written to a file. Default: True

  • return_values (bool, optional) – If true, in addition to the table in string format, minisanity will return the computed values as a dictionary. Default: False.

Returns:

  • printable_table (string)

  • values (dictionary) – Only returned if return_values is True

Note

For computing the reduced chi^2 values and the normalized residuals, the metric of each individual sample is used.