MR

class metamorphic_relations.MR.MR(transforms, max_composite=1)[source]

Creates an object to represent the tree of all combinations of the transforms

Parameters:
  • transforms (list[Transform]) – a list of transforms

  • max_composite (int) – the maximum number of transformations that can be performed sequentially on each data element

add_composite(max_composite, used_indices, prev_y)[source]

Adds a branch to the tree of composite transforms

Parameters:
  • max_composite (int) – the maximum number of remaining transformations that can be performed sequentially given the previous transforms more shallow in the tree

  • used_indices (list[int]) – the transforms that have already been used in this branch

  • prev_y (int) – the final y value after the previous transform

Returns:

the tree from this point of possible combinations of transforms

Return type:

list[tuple[Transform, list]]

static for_all_labels(transform, label_current_indices=None, label_target_indices=None, name=None)[source]

Adds transforms for a given set of labels

Parameters:
  • transform (function) – the transformation function

  • label_current_indices (list[int] | None) – the indices of labels to use this transform on (default leads to all labels)

  • label_target_indices (list[int] | None) – the indices of labels to give after the transform (default leads to labels remaining the same)

  • name (str | None) – the name of the transform by default uses the function name

Returns:

a list of transforms

Return type:

list[Transform]

get_composite_list(MR_tree=None)[source]

Takes a tree of MRs and converts it to a list of paths through the tree

Parameters:

MR_tree (list[tuple[Transform, list]] | None) – the tree to convert

Returns:

a list of trees each of which has a single path from root to leaf

Return type:

list[tuple[Transform, list]]

get_composite_list_names()[source]

Gets the names of each of the composite MRs

Returns:

a list of string names

Return type:

list[str]

static get_composite_name(transform_list)[source]

Gets the name of a composite MR, by concatenating each Transform’s individual name

Parameters:

transform_list (tuple[Transform, list]) – the composite MR representation

Returns:

a string name

Return type:

str

get_composite_tree(max_composite)[source]

Gets the tree of composite transforms

Parameters:

max_composite (int) – the maximum number of transformations that can be performed sequentially on each data element

Returns:

The tree of composite transforms

Return type:

list[tuple[Transform, list]]

static perform_DSMR(transform, xs, indices)[source]

Performs the DSMR on the x data given by indices

Parameters:
  • transform (function) – the transformation function

  • xs (array) – numpy array of x data

  • indices (list[int]) – indexed labels of the y data this MR should be performed on

Returns:

the transformed data

Return type:

tuple[numpy.array, int]

static perform_GMR(transform, xs)[source]

Performs the GMR on all the x data

Parameters:
  • transform (function) – the transformation function

  • xs (array) – numpy array of x data

Returns:

the transformed data (the shape is the same as the input)

Return type:

array

static perform_MRs(transform_branch, xs, ys, groups)[source]

Performs a single branch of the MRs tree

Parameters:
  • transform_branch (tuple[Transform, list]) – the branch of MRs in the form (current_transform, [following_transforms])

  • xs (array) – x numpy array

  • ys (array) – y numpy array

  • groups (list[list[int]]) – indexed labels of the y data

Returns:

the transformed data and corresponding labels

Return type:

tuple[numpy.array, numpy.array]

static perform_MRs_list(MR_list, xs, ys, max_y)[source]

Performs the entire tree of MRs on the given data

Parameters:
  • MR_list (tuple[Transform, list]) – an element of a list of MRs

  • xs (array) – x numpy array

  • ys (array) – y numpy array

  • max_y (int) – the largest value y can be

Returns:

the transformed data and corresponding labels

Return type:

array

perform_MRs_tree(xs, ys, max_y)[source]

Performs the entire tree of MRs on the given data

Parameters:
  • xs (array) – x numpy array

  • ys (array) – y numpy array

  • max_y (int) – the largest value y can be

Returns:

the transformed data and corresponding labels

Return type:

array

static scale_values_transform(x, scale_func)[source]

Scales all the values of the input

Parameters:
  • x (array) – input data in the form of a numpy array

  • scale_func (function) – a function to be applied to all int values in the input

Returns:

the transformed data

Return type:

array

update_composite(max_composite)[source]

Updates the tree based on the current list and max_composite

Parameters:

max_composite (int) – the maximum number of transformations that can be performed sequentially on each data element

class metamorphic_relations.ImageMR.ImageMR(transforms, max_composite=1)[source]
Parameters:
  • transforms (list[Transform]) –

  • max_composite (int) –

static blur_transform(x, sigma)[source]

Blurs the input

Parameters:
  • x (array) – input data in the form of a numpy array

  • sigma (float) – a blurring factor (the larger sigma the more blurred the result)

Returns:

the transformed data

Return type:

array

static flip_horizontal_transform(x)[source]

Flips the input horizontally

Parameters:

x (array) – input data in the form of a numpy array

Returns:

the transformed data

Return type:

array

static flip_vertical_transform(x)[source]

Flips the input vertically

Parameters:

x (array) – input data in the form of a numpy array

Returns:

the transformed data

Return type:

array

static get_image_GMRs()[source]

Gives a list of generic metamorphic relations (GMRs) for images

Returns:

list of Transforms

Return type:

list[Transform]

static rotate_transform(x, angle)[source]

Rotates the input by an angle

Parameters:
  • x (array) – input data in the form of a numpy array

  • angle (int) – an angle in degrees

Returns:

the transformed data

Return type:

array

class metamorphic_relations.MRModel.MRModel(data, model, transform_x=None, transform_y=None, GMRs=None, DSMRs=None)[source]

Creates and MRModel object

Parameters:
  • data (Data) – the data to be used with the model

  • model (Model) – the ML model

  • transform_x (function) – the transform of the x from the data representation to the expected input to the model

  • transform_y (function) – the transform of the y from the data representation to the expected output from the model

  • GMRs (list[Transform]) – list of Generic Metamorphic Relations (GMRs)

  • DSMRs (list[Transform]) – list of Domain Specific Metamorphic Relations (DSMRs)

compare_MR_counts(max_composite=1, min_i=4)[source]

Trains the model on each MR individually using increasing proportions of the data

Parameters:
  • max_composite (int) – default = 1, determines the max number of MRs that can be applied consecutively to produce new training data

  • min_i (int) – the smallest set of data to test calculated as 2**min_i

Returns:

a Results object and list of the best model for each individual MR (in the same order as results)

Return type:

tuple[Results, list[keras.engine.training.Model]]

compare_MR_sets(max_composite=1, compare_sets=(True, True, True, True))[source]

Trains the model on each set of MRs using all the training data

Parameters:
  • max_composite (int) – default = 1, determines the max number of MRs that can be applied consecutively to produce new training data

  • compare_sets (tuple[bool, bool, bool, bool]) –

Return type:

tuple[Results, list[keras.engine.training.Model]]

:param compare_sets:the sets of results to compare of [“original”, “GMRs”, “DSMRs”, “all_MRs”]. E.g. [True, False, True, False] compares [“original”, “DSMRs”] :return: a Results object and list with the model for each set (in the same order as the input to compare_sets)

compare_MR_sets_counts(max_composite=1, min_i=4, compare_sets=(True, True, True, True))[source]

Trains the model on each set of MRs using increasing proportions of the data

Parameters:
  • max_composite (int) – determines the max number of MRs that can be applied consecutively to produce new training data

  • min_i (int) – the smallest set of data to test calculated as 2**min_i

  • compare_sets (tuple[bool, bool, bool, bool]) –

Return type:

tuple[Results, list[keras.engine.training.Model]]

:param compare_sets:the sets of results to compare of [“original”, “GMRs”, “DSMRs”, “all_MRs”]. E.g. [True, False, True, False] compares [“original”, “DSMRs”] :return: a Results object and list of the best model for each set (in the same order as the input to compare_sets)

compare_MRs(max_composite=1)[source]

Trains the model on each MR individually using all the training data

Parameters:

max_composite (int) – default = 1, determines the max number of MRs that can be applied consecutively to produce new training data

Returns:

a Results object and list of the best model for each individual MR (in the same order as results)

Return type:

tuple[Results, list[keras.engine.training.Model]]

static concat(x, y, new_x, new_y)[source]

Concatenates 2 arrays

Parameters:
  • x (array) – x data

  • y (array) – y data

  • new_x (array) – data to add to x

  • new_y (array) – data to add to y

Returns:

(x + new_x, y + new_y)

Return type:

tuple[numpy.array, numpy.array]

get_results(MR_obj=None, MR_list=None, i_vals=None)[source]

Returns the results of training the data on the model with the MRs

Parameters:
  • MR_obj (MR | None) – the MR object - this should be given if the MR tree should be used i.e. to get the results when using set of MRs

  • MR_list (tuple[Transform, list] | None) – a list of transforms to be performed compositely - this should be used when getting results for an individual (potentially composite) MR

  • i_vals (list[int] | None) – the intervals to get results for

Returns:

an Info object containing the results for this MR

Return type:

tuple[Info, keras.engine.training.Model]

test_model(test_x=None, test_y=None)[source]

Tests the model to find macro f1 scores

Parameters:
  • test_x (array | None) – the x data (default uses the data.train_x)

  • test_y (array | None) – the y data (default uses the data.train_y)

Returns:

the macro f1 score

Return type:

float

train_model(train_x, train_y, k=5)[source]

Trains the model and sets it to the best performing model of the k folds

Parameters:
  • train_x (array) – the x data

  • train_y (array) – the y data

  • k (int) – the number of folds for k fold validation

Returns:

the mean macro f1 score over the training folds

Return type:

float

transform_data(x, y)[source]

Transforms the data to be used in the ML model

Parameters:
  • x (array) – x data

  • y (array) – y data

Returns:

(new_x_data, new_y_data)

Return type:

tuple[numpy.array, numpy.array]

static y_1D_to_2D(y, max_y)[source]

Reshapes a 1D array to a 2D array containing the index of the highest value. E.g. [1, 0] -> [[0, 1], [1, 0]]

Parameters:
  • y (array) – the original array

  • max_y (int) – the largest possible value in the array

Returns:

the 2D array

Return type:

array

static y_2D_to_1D(y)[source]

Reshapes a 2D array to a 1D array containing the index of the highest value. E.g. [[0, 1], [1, 0]] -> [1, 0]

Parameters:

y (array) – the original array

Returns:

the 1D array

Return type:

array

class metamorphic_relations.Transform.Transform(func, current, target, name=None)[source]

Creates an object to be used for transforms

Parameters:
  • func (function) – the transformation function, it must take a numpy array to another numpy array of the same shape

  • current (int) – the label index of that the data originally has

  • target (int) – the label index of the data after the transform

  • name (str) – the name of the transform