icub-client
Classes | Functions

Utility class for functions required by both interactionSAMModel.py and trainSAMModel.py. More...

+ Collaboration diagram for SAM Utils:

Classes

class  SAM.SAM_Core.SAM_utils.TimeoutError
 Custom TimeoutError Exception. More...
 
class  SAM.SAM_Core.SAM_utils.InterruptableThread
 Class to launch a function inside of a separate thread. More...
 
class  SAM.SAM_Core.SAM_utils.timeout
 Class to terminate a function running inside of a separate thread. More...
 

Functions

def SAM.SAM_Core.SAM_utils.initialiseModels (argv, update, initMode='training')
 Initialise SAM Model data structure, training parameters and user parameters. More...
 
def SAM.SAM_Core.SAM_utils.varianceClass (varianceDirection, x, thresh)
 Utility function to perform threshold or range checking. More...
 
def SAM.SAM_Core.SAM_utils.plotKnownAndUnknown (varDict, colour, axlist, width=[0.2, factor=[(0, 0.6), plotRange=False)
 Utility function to plot variances of known and unknown as gaussian distributions. More...
 
def SAM.SAM_Core.SAM_utils.bhattacharyya_distance (mu1, mu2, var1, var2)
 Calculates a measure for the separability of two univariate gaussians. More...
 
def SAM.SAM_Core.SAM_utils.plot_confusion_matrix (cm, targetNames, title='Confusion matrix', cmap=plt.cm.inferno)
 Generate and display a confusion matrix. More...
 
def SAM.SAM_Core.SAM_utils.plotGaussFromList (mlist, vlist, rlist, colour, label, width, factor, axlist, plotRange=False)
 Plot multiple Gaussians from a list on the same plot. More...
 
def SAM.SAM_Core.SAM_utils.solve_intersections (m1, m2, std1, std2)
 Solve for the intersection/s of two Gaussian distributions. More...
 
def SAM.SAM_Core.SAM_utils.PfromHist (sample, hist, binWidth)
 Calulates the probability of a sample from a histogram. More...
 
def SAM.SAM_Core.SAM_utils.meanVar_varianceDistribution (dataList)
 Calculate list of means, variances and ranges for the data in the dataList. More...
 
def SAM.SAM_Core.SAM_utils.bhattacharyya_dict (m, v)
 Calculate bhattacharyya distances for each item in the dictionaries. More...
 
def SAM.SAM_Core.SAM_utils.smooth1D (x, window_len=11, window='hanning')
 Smooth the data using a window with a requested size. More...
 
def SAM.SAM_Core.SAM_utils.transformTimeSeriesToSeq (Y, timeWindow, offset=1, normalised=False, reduced=False, noY=False, doOffset=False)
 Utility function to convert a time series into multiple time windows with additional functionality. More...
 

Detailed Description

Utility class for functions required by both interactionSAMModel.py and trainSAMModel.py.


Class Documentation

◆ SAM::SAM_Core::SAM_utils::TimeoutError

class SAM::SAM_Core::SAM_utils::TimeoutError

Custom TimeoutError Exception.

Description
Class used to raise TimeoutError Exceptions.

Definition at line 414 of file SAM_utils.py.

+ Inheritance diagram for SAM.SAM_Core.SAM_utils.TimeoutError:

◆ SAM::SAM_Core::SAM_utils::InterruptableThread

class SAM::SAM_Core::SAM_utils::InterruptableThread

Class to launch a function inside of a separate thread.

Definition at line 420 of file SAM_utils.py.

+ Inheritance diagram for SAM.SAM_Core.SAM_utils.InterruptableThread:

Public Member Functions

def __init__ (self, func, args, kwargs)
 Initialise the interruptible thread. More...
 
def run (self)
 Run the function. More...
 
def result (self)
 

Constructor & Destructor Documentation

◆ __init__()

def SAM.SAM_Core.SAM_utils.InterruptableThread.__init__ (   self,
  func,
  args,
  kwargs 
)

Initialise the interruptible thread.

Definition at line 424 of file SAM_utils.py.

Member Function Documentation

◆ result()

def SAM.SAM_Core.SAM_utils.InterruptableThread.result (   self)

Definition at line 442 of file SAM_utils.py.

◆ run()

def SAM.SAM_Core.SAM_utils.InterruptableThread.run (   self)

Run the function.

Definition at line 434 of file SAM_utils.py.

◆ SAM::SAM_Core::SAM_utils::timeout

class SAM::SAM_Core::SAM_utils::timeout

Class to terminate a function running inside of a separate thread.

Definition at line 449 of file SAM_utils.py.

+ Inheritance diagram for SAM.SAM_Core.SAM_utils.timeout:

Public Member Functions

def __init__ (self, sec)
 Initialise the timeout function. More...
 
def __call__ (self, f)
 Initialise an interruptible thread and start the thread. More...
 

Constructor & Destructor Documentation

◆ __init__()

def SAM.SAM_Core.SAM_utils.timeout.__init__ (   self,
  sec 
)

Initialise the timeout function.

Definition at line 453 of file SAM_utils.py.

Member Function Documentation

◆ __call__()

def SAM.SAM_Core.SAM_utils.timeout.__call__ (   self,
  f 
)

Initialise an interruptible thread and start the thread.

Definition at line 459 of file SAM_utils.py.

Function Documentation

◆ bhattacharyya_dict()

def SAM.SAM_Core.SAM_utils.bhattacharyya_dict (   m,
  v 
)

Calculate bhattacharyya distances for each item in the dictionaries.

Parameters
mDictionary of means.
vDictionary of variances.
Returns
List of bhattacharyya distances for the input dictionaries.

Definition at line 677 of file SAM_utils.py.

◆ bhattacharyya_distance()

def SAM.SAM_Core.SAM_utils.bhattacharyya_distance (   mu1,
  mu2,
  var1,
  var2 
)

Calculates a measure for the separability of two univariate gaussians.

Returns the bhattacharyya distance that is used to optimise for separability between known and unknown classes when these are modelled as univariate gaussians.

Parameters
mu1Float with mean of distribution 1.
mu2Float with mean of distribution 2.
var1Float with variance of distribution 1.
var2Float with variance of distribution 2.
Returns
Returns a float with the bhattacharyya distance between the two distributions.

Definition at line 509 of file SAM_utils.py.

◆ initialiseModels()

def SAM.SAM_Core.SAM_utils.initialiseModels (   argv,
  update,
  initMode = 'training' 
)

Initialise SAM Model data structure, training parameters and user parameters.

This method starts by initialising the required Driver from the driver name in argv[3] if it exists in SAM_Drivers folder. The standard model parameters and the specific user parameters are then initialised and the data is read in by the SAMDriver.readData method to complete the model data structure. This method then replicates the model data structure for training with multiple models if it is required in the config file loaded by the Driver.

Parameters
argv_0dataPath containing the data that is to be trained on.
argv_1modelPath containing the path of where the model is to be stored.
argv_2driverName containing the name of the driver class that is to be loaded from SAM_Drivers folder.
updateString having either a value of 'update' or 'new'. 'new' will load the parameters as set in the config file of the driver being loaded present in the dataPath directory. This is used to train a new model from scratch. 'update' will check for an existing model in the modelPath directory and load the parameters from this model if it exists. This is used for retraining a model when new data becomes available.
initModeString having either a value of 'training' or 'interaction'. 'training' takes into consideration the value of update in loading the parameters. (Used by trainSAMModel.py) 'interaction' loads the parameters directly from the model if the model exists. (Used by interactionSAMModel.py)
Returns
The output is a list of SAMDriver models. The list is of length 1 when the config file requests a single model or a list of length n+1 for a config file requesting multiple models where n is the number of requested models. The number of models either depends on the number of directories present in the dataPath or from the length of textLabels returned from the SAMDriver.readData method.

Definition at line 59 of file SAM_utils.py.

◆ meanVar_varianceDistribution()

def SAM.SAM_Core.SAM_utils.meanVar_varianceDistribution (   dataList)

Calculate list of means, variances and ranges for the data in the dataList.

Parameters
dataListList of numpy arrays containing the data to check.
Returns
List of means, list of variances and list of ranges. One for each array in the dataList.

Definition at line 639 of file SAM_utils.py.

◆ PfromHist()

def SAM.SAM_Core.SAM_utils.PfromHist (   sample,
  hist,
  binWidth 
)

Calulates the probability of a sample from a histogram.

Parameters
sampleFloat with sample to be tested.
histNumpy array with normalised histogram probability values.
binWidthFloat indicating the width for each probability bin.
Returns
Probability ranging from 0 to 1 for sample with respect to the histogram.

Definition at line 621 of file SAM_utils.py.

◆ plot_confusion_matrix()

def SAM.SAM_Core.SAM_utils.plot_confusion_matrix (   cm,
  targetNames,
  title = 'Confusion matrix',
  cmap = plt.cm.inferno 
)

Generate and display a confusion matrix.

This method plots a formatted confusion matrix from the provided array and target names.

Parameters
cmSquare numpy array containing the values for the confusion matrix.
targetNameslabels for the different classes.
titleTitle of the plot.
cmapMatplotlib colourmap for the plot.
Returns
No return. Blocking call to matplotlib.plot.

Definition at line 531 of file SAM_utils.py.

◆ plotGaussFromList()

def SAM.SAM_Core.SAM_utils.plotGaussFromList (   mlist,
  vlist,
  rlist,
  colour,
  label,
  width,
  factor,
  axlist,
  plotRange = False 
)

Plot multiple Gaussians from a list on the same plot.

Parameters
mlistList of float means.
vlistList of float variances.
rlistList of float data ranges.
colourColour for the plots.
labelLabel for the plot.
widthWidth of line in the plot.
factorFactor for the height of the ranges to make them more aesthetic.
axlistList of axes.
plotRangeBoolean to plot ranges or not.
Returns
List of axes.

Definition at line 562 of file SAM_utils.py.

◆ plotKnownAndUnknown()

def SAM.SAM_Core.SAM_utils.plotKnownAndUnknown (   varDict,
  colour,
  axlist,
  width = [0.2,
  factor = [(0, 0.6),
  plotRange = False 
)

Utility function to plot variances of known and unknown as gaussian distributions.

Parameters
varDictDictionary containing the mean and variances of known and unknown for different sections of data.
colourList of strings with the colours to be used for each plot.
axlistPlot object to pass in and update.
widthList of floats with the linewidth for the plots.
factorList of tuples with factors for the plotting of ranges.
plotRangeBoolean to plot a range together with gaussian distributions or not.
Returns
Plot object for the generated plot.

Definition at line 484 of file SAM_utils.py.

◆ smooth1D()

def SAM.SAM_Core.SAM_utils.smooth1D (   x,
  window_len = 11,
  window = 'hanning' 
)

Smooth the data using a window with a requested size.

This method is based on the convolution of a scaled window with the signal. The signal is prepared by introducing reflected copies of the signal (with the window size) in both ends so that transient parts are minimized in the beginning and end part of the output signal.

input: x The input signal. window_len The dimension of the smoothing window; should be an odd integer. window The type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman' flat window will produce a moving average smoothing.

output: The smoothed signal.

Definition at line 710 of file SAM_utils.py.

◆ solve_intersections()

def SAM.SAM_Core.SAM_utils.solve_intersections (   m1,
  m2,
  std1,
  std2 
)

Solve for the intersection/s of two Gaussian distributions.

Parameters
m1Float Mean of Gaussian 1.
m2Float Mean of Gaussian 2.
std1Float Standard Deviation of Gaussian 1.
std2Float Standard Deviation of Gaussian 2.
Returns
Points of intersection for the two Gaussian distributions.

Definition at line 603 of file SAM_utils.py.

◆ transformTimeSeriesToSeq()

def SAM.SAM_Core.SAM_utils.transformTimeSeriesToSeq (   Y,
  timeWindow,
  offset = 1,
  normalised = False,
  reduced = False,
  noY = False,
  doOffset = False 
)

Utility function to convert a time series into multiple time windows with additional functionality.

Parameters
YTime series data.
timeWindowLength of the time window.
offsetNumber of non-overlapping frames between successive time windows.
normalisedBoolean to normalise time windows with respect to the starting frame.
reducedBoolean to remove the starting frame if normalisation is enabled since this frame contains 0.
noYBoolean to return time windows of labels together.
doOffsetIn future versions this parameter will enable skipping data points between sampled windows.
Returns
X numpy array of size (numberWindows x lengthOfWindow) containing the time series split up into windows.
Y numpy array of size (numberWindows x lengthOfWindow) containing the labels for each frame in the time series split into windows.

Definition at line 755 of file SAM_utils.py.

◆ varianceClass()

def SAM.SAM_Core.SAM_utils.varianceClass (   varianceDirection,
  x,
  thresh 
)

Utility function to perform threshold or range checking.

Parameters
varianceDirectionList of strings with the conditions to check.
xThe value to be checked.
threshThe threshold against which x is to be checked given the checks in varianceDirection.
Returns
Boolean True or False confirming arg x validates varianceDirection conditions for the threshold.

Definition at line 398 of file SAM_utils.py.