muteria.common.matrices module

class muteria.common.matrices.ExecutionMatrix(filename=None, non_key_col_list=None)[source]

Bases: RawExecutionMatrix

This class is the default extension of the class RawExecutionMatrix.

class muteria.common.matrices.OutputLogData(filename=None)[source]

Bases: object

Dat_Keys = {'OUTLOG_HASH', 'OUTLOG_LEN', 'RETURN_CODE', 'TIMEDOUT'}
OUTLOG_HASH = 'OUTLOG_HASH'
OUTLOG_LEN = 'OUTLOG_LEN'
RETURN_CODE = 'RETURN_CODE'
TIMEDOUT = 'TIMEDOUT'
UNCERTAIN_TEST_OUTLOGDATA = {'OUTLOG_HASH': None, 'OUTLOG_LEN': None, 'RETURN_CODE': None, 'TIMEDOUT': None}
add_data(data_dict, check_all=True, override_existing=False, ask_confirmation_with_exist_missing=False, serialize=False)[source]
get_store_filename()[source]

Get the name of the storing file

get_zip_objective_and_data()[source]
is_empty()[source]
classmethod outlogdata_equiv(outlogdata1, outlogdata2)[source]
serialize()[source]

Serialize the matrix to its corresponding file if not None

update_with_other(other_execoutput, override_existing=False, ask_confirmation_with_exist_missing=False, serialize=False)[source]
class muteria.common.matrices.RawExecutionMatrix(filename=None, key_column_name='MUTERIA_MATRIX_KEY_COL', non_key_col_list=None, active_cell_default_val=[1], inactive_cell_vals=[0], uncertain_cell_default_val=[-1], is_active_cell_func=<function RawExecutionMatrix.<lambda>>, is_inactive_cell_func=<function RawExecutionMatrix.<lambda>>, is_uncertain_cell_func=<function RawExecutionMatrix.<lambda>>, cell_dtype=<class 'int'>)[source]

Bases: object

A 2D matrix representation of the execution of entities by test cases. Each entity is represented as a ‘key’ (The keys are unique) on each row, and, the test cases represented as columns. The behavior of each entity with regard to each test case (affected or not) is represented as active or not. The cell of the matrix with row key E and column test case T represents whether T affects E.

This class represent a configurable implementation of the Matrix.

Parameters:
  • filename (file system path string representing the filename where the) – underlying pandas dataframe of the matrix is stored

  • key_column_name (The string representing the pandas dataframe column) – name that will be used to represent the entity (‘key’)

  • non_key_col_list (list containing the list of strings representing) – the test cases (pandas dataframe column that are not used as key)

  • active_cell_default_val (default value of the matrix cell among) – those considered as active

  • inactive_cell_vals (list containing the matrix cell values that are) – considered as inactive

  • uncertain_cell_default_val (default value of the matrix cell among) – those considered as uncertain

  • is_active_cell_func (function that check whether a cell is active.) – It takes the cell value as single parameter and returns True if active and False otherwise

  • is_inactive_cell_func (function that check whether a cell is inactive.) – It takes the cell value as single parameter and returns True if active and False otherwise

  • is_uncertain_cell_func (function that check whether a cell is N/A.) – It takes the cell value as simgle parameter and returns True if uncertain (N/A) and False otherwise

  • cell_dtype (data type that will be used in underlying pandas dataframe) – to represent a cell.

Note

To ensure consistency in cell type and others, define an extension of this class with fixed values except filename and non_key_col_list see for example ExecutionMatrix below.

add_row_by_key(key, values, serialize=True)[source]

add a row to the matrix :param key: The key to add :param values: (list or dict) The values for the given key.

Ordered following non key columns ordering if list if dict, it is mapping from column name to value

Parameters:

serialize – (bool) decide whether to serialize the matrix to file after adding

Returns:

nothing

Example:

>>> nc = ['a', 'b', 'c']
>>> mat = ExecutionMatrix(non_key_col_list=nc)
>>> mat.add_row_by_key('k', [1, 2, 3])
>>> mat._get_key_values_dict(['k']) == {'k': {'a':1,'b':2,'c':3}}
True
>>> mat.add_row_by_key('r', {'a':3, 'c':2, 'b':3})
>>> mat._get_key_values_dict(['r']) == {'r': {'a':3,'b':3,'c':2}}
True
>>> len(mat.get_keys())
2
clear_cells_to_value(value)[source]

clear the matrix values to a given value :param value: cell value type :return: nothing

Example:

>>> nc = ['a', 'b', 'c']
>>> mat = ExecutionMatrix(non_key_col_list=nc)
>>> mat.add_row_by_key('k', [1, 2, 3])
>>> mat.add_row_by_key('r', [3, 2, 3])
>>> mat.add_row_by_key('w', [1, 0, 3])
>>> mat.clear_cells_to_value(0)
>>> mat._get_key_values_dict(['k']) == {'k': {'a':0,'b':0,'c':0}}
True
>>> mat._get_key_values_dict(['r']) == {'r': {'a':0,'b':0,'c':0}}
True
>>> mat._get_key_values_dict(['w']) == {'w': {'a':0,'b':0,'c':0}}
True
delete_rows_by_key(key_list, serialize=True)[source]

delete the rows for the given keys :param key_list: collection of key whose rows to delete :param serialize: (bool) decide whether to serialize the matrix

to file after deletion

Returns:

nothing

Example:

>>> nc = ['a', 'b', 'c']
>>> mat = ExecutionMatrix(non_key_col_list=nc)
>>> mat.add_row_by_key('k', [1, 2, 3])
>>> mat.add_row_by_key('r', [3, 2, 3])
>>> mat.add_row_by_key('w', [1, 0, 3])
>>> mat.delete_rows_by_key(['k','w'])
>>> mat_keys = mat.get_keys()
>>> len(mat_keys)
1
>>> mat_keys[0] == 'r'
True
extract_by_column(non_key_col_list, out_filename=None)[source]
get the sub-matrix with the columns corresponding to the

values in the passed list. Note that the order is maintained if a list is passed

Parameters:
  • non_key_col_list – collection of columns to extract. Must not be empty and every column must exist

  • out_filename – Optional filename to use as storage for the extracted matrix. Default is None (no storage)

Returns:

matrix which is sub matrix of this

Example:

>>> nc = ['a', 'b', 'c']
>>> mat = ExecutionMatrix(non_key_col_list=nc)
>>> mat.add_row_by_key('k', [1, 2, 3])
>>> mat.add_row_by_key('r', [3, 2, 3])
>>> mat.add_row_by_key('w', [1, 0, 3])
>>> mat2 = mat.extract_by_column(['a', 'c'])
>>> list(mat2.get_keys()) == ['k', 'r', 'w']
True
>>> mat2.get_nonkey_colname_list() == ['a', 'c']
True
extract_by_rowkey(row_key_list, out_filename=None)[source]
get the sub-matrix with the rows keys corresponding to the

values in the passed list. Note that the order of keys in row_key_list is not guaranted

Parameters:
  • row_key_list – collection of row keys to extract. Must not be empty and every key must exist

  • out_filename – Optional filename to use as storage for the extracted matrix. Default is None (no storage)

Returns:

matrix which is sub matrix of this

Example:

>>> nc = ['a', 'b', 'c']
>>> mat = ExecutionMatrix(non_key_col_list=nc)
>>> mat.add_row_by_key('k', [1, 2, 3])
>>> mat.add_row_by_key('r', [3, 2, 3])
>>> mat.add_row_by_key('w', [1, 0, 3])
>>> mat2 = mat.extract_by_rowkey(['r', 'k'])
>>> list(mat2.get_keys()) == ['k', 'r']
True
>>> mat2.get_nonkey_colname_list() == nc
True
getActiveCellDefaultVal()[source]

Get a value that represent an active cell value

getInactiveCellVal()[source]

Get a value that represent an inactive cell value

getUncertainCellDefaultVal()[source]

Get a value that represent an uncertain cell value

get_a_deepcopy(new_filename=None, serialize=True)[source]
get a copy of the current matrix. The new filename will be

used fo storage.

Parameters:
  • new_filename – filename to store the copy

  • serialize – (bool) decide whether to serialize the copy to file upon creation

Returns:

a deep copy of this matrix

Example:

>>> nc = ['a', 'b', 'c']
>>> mat = ExecutionMatrix(non_key_col_list=nc)
>>> mat.add_row_by_key('k', [1, 2, 3])
>>> c_mat = mat.get_a_deepcopy()
>>> mat.dataframe.equals(c_mat.dataframe)
True
>>> mat.add_row_by_key('r', [1, 2, 3])
>>> mat.dataframe.equals(c_mat.dataframe)
False
get_key_colname()[source]

get the name of the column representing the keys

get_keys()[source]

get a pandas serie of the keys (example mutant ids) :Example: >>> nc = [‘a’, ‘b’, ‘c’] >>> mat = ExecutionMatrix(non_key_col_list=nc) >>> type(mat.get_keys()) == pd.core.series.Series True >>> len(mat.get_keys()) 0 >>> mat.add_row_by_key(‘k’, [1, 2, 3]) >>> list(mat.get_keys()) [‘k’]

get_nonkey_colname_list()[source]

get list of non key columns :Example: >>> nc = [‘a’, ‘b’, ‘c’] >>> mat = ExecutionMatrix(non_key_col_list=nc) >>> mat.get_nonkey_colname_list() == nc True >>> mat.add_row_by_key(‘k’, [1, 2, 3]) >>> mat.get_nonkey_colname_list() == nc True

get_store_filename()[source]

Get the name of the storing file

is_empty()[source]

Check that the matrix have no row (all columns have no row) :Example: >>> nc = [‘a’, ‘b’, ‘c’] >>> mat = ExecutionMatrix(non_key_col_list=nc) >>> mat.is_empty() True >>> mat.add_row_by_key(‘k’, [1, 2, 3]) >>> mat.is_empty() False

query_active_columns_of_rows(row_key_list=None)[source]

return a dict in the form row2cols :param row_key_list: list of rows to query for :return: a dict representing a mapping between the passed rows

and the list of columns active for those rows

Example:

>>> nc = ['a', 'b', 'c']
>>> mat = ExecutionMatrix(non_key_col_list=nc)
>>> act = mat.getActiveCellDefaultVal()
>>> inact = mat.getInactiveCellVal()
>>> uncert = mat.getUncertainCellDefaultVal()
>>> mat.add_row_by_key('k', [inact, uncert, act])
>>> mat.query_active_columns_of_rows() == {'k': ['c']}
True
query_active_rows_of_columns(non_key_col_list=None)[source]

return a dict in the form col2rows :param non_key_col_list: list of columns to query for :return: a dict representing a mapping between the passed columns

and the list of rows active for those columns

Example:

>>> nc = ['a', 'b', 'c']
>>> mat = ExecutionMatrix(non_key_col_list=nc)
>>> act = mat.getActiveCellDefaultVal()
>>> inact = mat.getInactiveCellVal()
>>> uncert = mat.getUncertainCellDefaultVal()
>>> mat.add_row_by_key('k', [inact, uncert, act])
>>> mat.query_active_rows_of_columns() == {'a':[], 'b':[], 'c':['k']}
True
query_inactive_columns_of_rows(row_key_list=None)[source]

return a dict in the form row2cols :param row_key_list: list of rows to query for :return: a dict representing a mapping between the passed rows

and the list of columns inactive for those rows

Example:

>>> nc = ['a', 'b', 'c']
>>> mat = ExecutionMatrix(non_key_col_list=nc)
>>> act = mat.getActiveCellDefaultVal()
>>> inact = mat.getInactiveCellVal()
>>> uncert = mat.getUncertainCellDefaultVal()
>>> mat.add_row_by_key('k', [inact, uncert, act])
>>> mat.query_inactive_columns_of_rows() == {'k': ['a']}
True
query_inactive_rows_of_columns(non_key_col_list=None)[source]

return a dict in the form col2rows :param non_key_col_list: list of columns to query for :return: a dict representing a mapping between the passed columns

and the list of rows inactive for those columns

Example:

>>> nc = ['a', 'b', 'c']
>>> mat = ExecutionMatrix(non_key_col_list=nc)
>>> act = mat.getActiveCellDefaultVal()
>>> inact = mat.getInactiveCellVal()
>>> uncert = mat.getUncertainCellDefaultVal()
>>> mat.add_row_by_key('k', [inact, uncert, act])
>>> mat.query_inactive_rows_of_columns() == {'a':['k'], 'b':[], 'c':[]}
True
query_uncertain_columns_of_rows(row_key_list=None)[source]

return a dict in the form row2cols :param row_key_list: list of rows to query for :return: a dict representing a mapping between the passed rows

and the list of columns uncertain for those rows

Example:

>>> nc = ['a', 'b', 'c']
>>> mat = ExecutionMatrix(non_key_col_list=nc)
>>> act = mat.getActiveCellDefaultVal()
>>> inact = mat.getInactiveCellVal()
>>> uncert = mat.getUncertainCellDefaultVal()
>>> mat.add_row_by_key('k', [inact, uncert, act])
>>> mat.query_uncertain_columns_of_rows() == {'k': ['b']}
True
query_uncertain_rows_of_columns(non_key_col_list=None)[source]

return a dict in the form col2rows :param non_key_col_list: list of columns to query for :return: a dict representing a mapping between the passed columns

and the list of rows inactive for those columns

Example:

>>> nc = ['a', 'b', 'c']
>>> mat = ExecutionMatrix(non_key_col_list=nc)
>>> act = mat.getActiveCellDefaultVal()
>>> inact = mat.getInactiveCellVal()
>>> uncert = mat.getUncertainCellDefaultVal()
>>> mat.add_row_by_key('k', [inact, uncert, act])
>>> mat.query_uncertain_rows_of_columns() == {'a':[],'b':['k'],'c':[]}
True
serialize()[source]

Serialize the matrix to its corresponding file if not None

to_pandas_df()[source]
return the matrix as a pandas dataframe

(just return a copy of the underlying pandas dataframe)

Returns:

a deep copy of the underlying pandas dataframe (modifying the dataframe do ot affect the matrix)

Example:

>>> nc = ['a', 'b', 'c']
>>> mat = ExecutionMatrix(non_key_col_list=nc)
>>> mat.add_row_by_key('k', [1, 2, 3])
>>> df2 = mat.to_pandas_df()
>>> mat.add_row_by_key('r', [3, 2, 3])
>>> len(df2)
1
>>> len(mat.get_keys())
2
update_cells(key, values)[source]

Update the values for the key with the values :param key: key whose values to update :param values: dict representing the new cell values by column name :return: nothing

Example:

>>> nc = ['a', 'b', 'c']
>>> mat = ExecutionMatrix(non_key_col_list=nc)
>>> mat.add_row_by_key('k', [1, 2, 3])
>>> mat.add_row_by_key('r', [1, 2, 3])
>>> mat.update_cells('k', {'a':0, 'c':4})
>>> mat._get_key_values_dict(['k']) == {'k': {'a':0, 'b':2, 'c':4}}
True
update_with_other_matrix(other_matrix, override_existing=False, allow_missing=False, ask_confirmation_with_exist_missing=False, serialize=False)[source]

Update this matrix using the other matrix :param other_matrix: The matrix to use to update this matrix :param override_existing: (bool) decide whether existing cell’s

value (row, column) of this matrix should be overrided by the update. Note that if this is disabled, no cell (row, col) of this matrix must exist in other_matrix, or this will fail.

Parameters:
  • allow_missing – (bool) decide whether missing cell are allowed when merging (cells that only belong to one matrix while thre are other cells on same row or same column that belong to the two matrices). Note that if this is disable. there should not be any such cell during merging or this will fail.

  • serialize – (bool) decides whether to serialize the updated matrix (this matrix), after update, into its file.

Returns:

nothing

Example:

>>> nc = ['a', 'b', 'c']
>>> mat = ExecutionMatrix(non_key_col_list=nc)
>>> mat.add_row_by_key('k', [1, 2, 3])
>>> mat.add_row_by_key('r', [4, 0, 1])
>>> nc_o = ['b', 'e']
>>> mat_other = ExecutionMatrix(non_key_col_list=nc_o)
>>> mat_other.add_row_by_key('h', [200, 100])
>>> mat_other.add_row_by_key('r', [10, 11])
>>> mat.update_with_other_matrix(mat_other, override_existing=True,                                                             allow_missing=True)
>>> list(mat.get_keys())
['k', 'r', 'h']
>>> list(mat.get_nonkey_colname_list())
['a', 'b', 'c', 'e']
>>> k_v_d = mat._get_key_values_dict()
>>> uncert = mat.getUncertainCellDefaultVal()
>>> k_v_d['k'] == {'a': 1, 'b': 2, 'c': 3, 'e': uncert}
True
>>> k_v_d['r'] == {'a': 4, 'b': 10, 'c': 1, 'e': 11}
True
>>> k_v_d['h'] == {'a': uncert, 'b': 200, 'c': uncert, 'e': 100}
True