Function Analysis

This sub-package is used to analyze and especially compare functions. The current implementation supports 2D-functions and multiple methods for comparison either based on the function values itselves or by discretizing the corresponding 2D-plot of the function.

The usage of the discretization of 2D-plots and the corresponding fingerprint that is used for the actual comparison is more extensively described in the related examples.

A genereal comparison class is available which also wraps the aformentioned fingerpint and in addition the further function-value based methods.

[1]:
import numpy as np
import yaml

with open(
    "../../example_notebooks/files/function_analysis/example_energy_dos.yaml"
) as file:
    example_dos_energy, example_dos = np.array(yaml.safe_load(file))

Importing datasets

Data sets can be readily imported into the object using the import_data function:

[2]:
from aim2dat.fct import FunctionAnalysis

fa = FunctionAnalysis()

fa.import_data("test_set", example_dos_energy, example_dos)
fa.import_data(
    "test_set_shifted",
    example_dos_energy,
    example_dos + np.random.random(len(example_dos))
)

Supported comparison metrics

  • Pearson-correlation : calculate_correlation

  • Difference / distance: calculate_distance (multiple options to define the difference)
    • Euclidian norm

    • Cosine distance

    • Total difference (sum of the distances at each x-value)

    • Absolute difference (sum of the absolute distances at each x-value)

  • Comparison of areas: compare_areas

Example of the different methods for calculate_distance.

[3]:
for distance_method in fa.allowed_distance_methods:
    difference = fa.calculate_distance(
        "test_set", "test_set_shifted", distance_method
    )
    print(f"Method: {distance_method}: {difference:.2f}")
Method: euclidian: 20.13
Method: cosine: 0.18
Method: total: -615.77
Method: absolute: 615.77

Comparison fingerprint

A comparison between two functions can also be accomplished using the fingerprint introduced in doi:10.1038/s41597-022-01754-z which is either calculated via the calculate_discrete_fingerprint function or directly used for the comparison via the compare_functions_by_discrete_fingerprint function. Both methods require the definition of a grid. Please refer to the two examples (here and here) to see how to do this in detail.

[4]:
from aim2dat.fct import DiscretizedAxis

axis = DiscretizedAxis(
    axis_type="x", max=20, min=0, min_step=0.2, max_num_steps=1
)
axis.discretization_method = "uniform"
axis.discretize_axis()

axis2 = DiscretizedAxis(
    axis_type="y", max=0.3, min=0, min_step=0.003, max_num_steps=1
)
axis2.discretization_method = "uniform"
axis2.discretize_axis()

grid = (axis + axis2).create_grid()
(1, 101)
(101, 101)
[5]:
# the fingerprint, e.g. for further comparisons
fp = fa.calculate_discrete_fingerprint("test_set", grid)

# comparison of the two datasets on the specified grid
fa.compare_functions_by_discrete_fingerprint(
    "test_set", "test_set_shifted", grid
)
[5]:
0.258358