Auxiliary Functions

Download this as a Jupyter notebook

This notebook covers the usage of the auxiliary function provided in pyXla.

A key feature of the pyXla framework is the separation of sampling and analysis. A set of functions are provided to support sampling. They are:

  1. pyxla.util.sample_X()

  2. pyxla.util.compute_F()

  3. pyxla.util.compute_V()

  4. pyxla.util.compute_D()

  5. pyxla.util.compute_N()

Each function corresponds to a given input file as indicated by it suffix.

When a sample is loaded declaratively via domain and function specification (method 3 in Loading and Sampling), these function are used under the hood. These function are available to the user for finer control over sampling.

The auxiliary functions are all imported from pyxla.util:

from pyxla.util import sample_X, compute_F, compute_V, compute_D, compute_N

One can generate an X file as below:

from pyxla.sampling import HilbertCurveSampler

sampler = HilbertCurveSampler(
    sample_size=100, dim=2, return_neighbourhood=True # will return an N file too
)

X, N = sample_X(sampler)
X.head()
x0 x1
0 7.447095 2.614344
1 14.974336 22.426519
2 20.351285 24.056034
3 21.557290 9.976241
4 19.596467 1.000000

Specifying return_neighbourhood=True generates an N file as well:

N.head()
id1 id2
0 0 1
1 1 2
2 2 3
3 3 4
4 4 5

The F file can be generated from the X file by specifying an objective function or multiple objective functions:

def sphere(X):
    return X[0]**2 + X[1]**2

def summation(X):
    return X.sum()

F = compute_F([sphere, summation], X)
F.head()
f0 f1
0 62.294016 10.061439
1 727.179492 37.400855
2 992.867611 44.407320
3 564.242123 31.533531
4 385.021524 20.596467

The process is similar for the V file:

V = compute_V([lambda X: sphere(X) - 2], X)
V.head()
v0
0 60.294016
1 725.179492
2 990.867611
3 562.242123
4 383.021524

Computing a D file is straightforward:

D = compute_D({"X": X}, metric='canberra') # you can define a function or specify any of scipy's distance functions
D.head()
d
id1 id2
0 1 1.126910
2 1.268158
3 1.071199
4 0.895901
5 0.996279

The N file is equally straightforward; either specify a neighbourhood function or supply the a literal: "hilbert-curve" or "X-index".

"hilbert-curve" uses the Hilbert curve to efficiently generate neighbourhood information (see pyxla.sampling.hilbert_curve_neighbour_sampling()).

"X-index" extracts neighbourhood from the default index of a DataFrame.

def randomly_neighbours(a, b):
    import random
    return random.choice([True, False])

N = compute_N({"X": X}, neighbourhood_func=randomly_neighbours)
N.head()
id1 id2
0 0 2
1 0 3
2 0 4
3 0 7
4 0 10