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:
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 | 6.453459 | 12.845543 |
| 1 | 3.665404 | 25.092207 |
| 2 | 18.456522 | 15.515011 |
| 3 | 19.369122 | 10.219224 |
| 4 | 15.620123 | 8.727153 |
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 | 206.655109 | 19.299002 |
| 1 | 643.054056 | 28.757612 |
| 2 | 581.358767 | 33.971533 |
| 3 | 479.595420 | 29.588346 |
| 4 | 320.151425 | 24.347275 |
The process is similar for the V file:
V = compute_V([lambda X: sphere(X) - 2], X)
V.head()
| v0 | |
|---|---|
| 0 | 204.655109 |
| 1 | 641.054056 |
| 2 | 579.358767 |
| 3 | 477.595420 |
| 4 | 318.151425 |
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 | 0.598340 |
| 2 | 0.575984 | |
| 3 | 0.614037 | |
| 4 | 0.606185 | |
| 5 | 1.430009 |
The N file is equally straightforward; either specify a neighbourhood function of supply the literal "hilbert-curve" to use the Hilbert curve to efficiently generate neighbourhood information (see pyxla.sampling.hilbert_curve_neighbour_sampling()).
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 | 1 |
| 1 | 0 | 2 |
| 2 | 0 | 3 |
| 3 | 0 | 4 |
| 4 | 0 | 5 |