Skip to content

Commit

Permalink
matrix cleaning. tests coverage 99
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgorji committed Jun 17, 2024
1 parent c1cebe8 commit 07d4e7c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 162 deletions.
167 changes: 5 additions & 162 deletions musurgia/matrix/matrix.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from pprint import pprint
from typing import Optional, Any, TypeVar

from musurgia.musurgia_exceptions import MatrixIsEmptyError, SquareMatrixDataError, \
PermutationOrderMatrixDataError, MatrixIndexOutOfRangeError, MatrixIndexEndOfMatrixError, MatrixIndexEndOfRowError, \
PermutationOrderMatrixDataError, MatrixIndexEndOfMatrixError, MatrixIndexEndOfRowError, \
MatrixIndexControllerReadingDirectionError
from musurgia.musurgia_types import NonNegativeInteger, check_type, PositiveInteger, \
MatrixData, MatrixIndex, check_matrix_index_values, check_permutation_order_values, \
Expand Down Expand Up @@ -73,15 +72,8 @@ def get_element(self, element_index: MatrixIndex) -> Any:
argument_name='element_index')
check_matrix_index_values(element_index, number_of_rows=self.get_column_size(),
number_of_columns=self.get_row_size())
try:
return self.matrix_data[element_index[0] - 1][element_index[1] - 1]
except IndexError as err:
raise IndexError(f'{err}: element_index: {element_index} data: {self.matrix_data}')

#
# def get_transposed_matrix(self: 'T', mode: MatrixTransposeMode = 'regular') -> 'T':
# return MatrixTransposition.get_transposed_matrix(self, mode=mode)
#
return self.matrix_data[element_index[0] - 1][element_index[1] - 1]

def get_transposed_matrix(matrix: 'T', mode: MatrixTransposeMode = 'regular') -> 'T':
check_type(v=mode, t='MatrixTransposeMode', class_name='MatrixTransposition',
method_name='get_transposed_matrix', argument_name='mode')
Expand All @@ -91,39 +83,12 @@ def get_transposed_matrix(matrix: 'T', mode: MatrixTransposeMode = 'regular') ->
controller.reading_direction = 'vertical'
elif mode == 'diagonal':
controller.reading_direction = 'diagonal'
# indices = list(controller)
# print(f'matrix: {matrix.matrix_data} indices: {indices}')
# controller.reset()
matrix_data = []
for c in range(controller.number_of_columns):
matrix_data.append([matrix.get_element(next(controller)) for _ in range(controller.number_of_rows)])
return matrix.__class__(matrix_data=matrix_data)


#
#
# class MatrixTransposition:
# T = TypeVar('T', bound='SimpleMatrix')
#
# @staticmethod
# def get_transposed_matrix(matrix: 'T', mode: MatrixTransposeMode) -> 'T':
# check_type(v=mode, t='MatrixTransposeMode', class_name='MatrixTransposition',
# method_name='get_transposed_matrix', argument_name='mode')
# controller = MatrixIndexController(number_of_rows=matrix.get_column_size(),
# number_of_columns=matrix.get_row_size())
# if mode == 'regular':
# controller.reading_direction = 'vertical'
# elif mode == 'diagonal':
# controller.reading_direction = 'diagonal'
# # indices = list(controller)
# # print(f'matrix: {matrix.matrix_data} indices: {indices}')
# # controller.reset()
# matrix_data = []
# for c in range(controller.number_of_columns):
# matrix_data.append([matrix.get_element(next(controller)) for _ in range(controller.number_of_rows)])
# return matrix.__class__(matrix_data=matrix_data)


class Matrix(SimpleMatrix):
T = TypeVar('T', bound='Matrix')

Expand Down Expand Up @@ -259,8 +224,8 @@ def _convert_flatten_index_to_index(self, flatten_index: NonNegativeInteger) ->

elif self.reading_direction == 'vertical':
return self._convert_flatten_index_to_index_vertical(flatten_index)
else:
raise AttributeError(self.reading_direction)
# else:
# raise AttributeError(self.reading_direction)

def _convert_index_to_flatten_index(self, index: MatrixIndex) -> PositiveInteger:
r = index[0]
Expand All @@ -277,8 +242,6 @@ def _convert_index_to_flatten_index(self, index: MatrixIndex) -> PositiveInteger
elif self.reading_direction == 'vertical':
flatten_index = (r - 1) + (c - 1) * self.number_of_rows
return flatten_index
else:
raise AttributeError(self.reading_direction)

def _get_next_index(self):
return self._convert_flatten_index_to_index(self._flatten_index)
Expand Down Expand Up @@ -349,123 +312,3 @@ def __next__(self) -> MatrixIndex:
output = self._get_next_index()
self._flatten_index += 1
return output
# class PermutationOrderMatrixTransposition:
# def __init__(self, matrix: PermutationOrderMatrix):
# self._matrix: Optional[PermutationOrderMatrix] = None
# self.matrix = matrix
#
# @property
# def matrix(self) -> PermutationOrderMatrix:
# return self._matrix
#
# @matrix.setter
# def matrix(self, value: PermutationOrderMatrix) -> None:
# if not isinstance(value, PermutationOrderMatrix):
# raise TypeError(create_error_message())
# if self._matrix is None:
# self._matrix = value
# else:
# raise AttributeError('PermutationOrderMatrixTransposition: matrix cannot be set after initialization')
#
# @staticmethod
# def reorder_permutation_order_matrix_data_vertically(matrix_data: MatrixData) -> MatrixData:
# """
# No type checking takes place!
#
# >>> matrix_data = [[('a1', 'b1', 'c1'), ('d1', 'e1', 'f1'), ('g1', 'h1', 'i1')], [('a2', 'b2', 'c2'), ('d2', 'e2', 'f2'), ('g2', 'h2', 'i2')], [('a3', 'b3', 'c3'), ('d3', 'e3', 'f3'), ('g3', 'h3', 'i3')]]
# >>> pprint(matrix_data)
# [[('a1', 'b1', 'c1'), ('d1', 'e1', 'f1'), ('g1', 'h1', 'i1')],
# [('a2', 'b2', 'c2'), ('d2', 'e2', 'f2'), ('g2', 'h2', 'i2')],
# [('a3', 'b3', 'c3'), ('d3', 'e3', 'f3'), ('g3', 'h3', 'i3')]]
# >>> pprint(PermutationOrderMatrixTransposition.reorder_permutation_order_matrix_data_vertically(matrix_data))
# [[('a1', 'a2', 'a3'), ('b1', 'b2', 'b3'), ('c1', 'c2', 'c3')],
# [('d1', 'd2', 'd3'), ('e1', 'e2', 'e3'), ('f1', 'f2', 'f3')],
# [('g1', 'g2', 'g3'), ('h1', 'h2', 'h3'), ('i1', 'i2', 'i3')]]
# """
# output = []
# for column in range(len(matrix_data)):
# row_list = []
# for element in range(len(matrix_data)):
# tmp = []
# for row in range(len(matrix_data)):
# tmp.append(matrix_data[row][column][element])
# row_list.append(tuple(tmp))
# output.append(row_list)
# return output
#
# @staticmethod
# def reorder_permutation_order_matrix_data_half_diagonally(matrix_data: MatrixData) -> MatrixData:
# """
# No type checking takes place!
#
# >>> matrix_data = [[('a1', 'b1', 'c1'), ('d1', 'e1', 'f1'), ('g1', 'h1', 'i1')], [('a2', 'b2', 'c2'), ('d2', 'e2', 'f2'), ('g2', 'h2', 'i2')], [('a3', 'b3', 'c3'), ('d3', 'e3', 'f3'), ('g3', 'h3', 'i3')]]
# >>> pprint(matrix_data)
# [[('a1', 'b1', 'c1'), ('d1', 'e1', 'f1'), ('g1', 'h1', 'i1')],
# [('a2', 'b2', 'c2'), ('d2', 'e2', 'f2'), ('g2', 'h2', 'i2')],
# [('a3', 'b3', 'c3'), ('d3', 'e3', 'f3'), ('g3', 'h3', 'i3')]]
# >>> pprint(PermutationOrderMatrixTransposition.reorder_permutation_order_matrix_data_half_diagonally(matrix_data))
# [[('a1', 'b2', 'c3'), ('b1', 'c2', 'a3'), ('c1', 'a2', 'b3')],
# [('d1', 'e2', 'f3'), ('e1', 'f2', 'd3'), ('f1', 'd2', 'e3')],
# [('g1', 'h2', 'i3'), ('h1', 'i2', 'g3'), ('i1', 'g2', 'h3')]]
# """
# output = []
# for i in range(len(matrix_data)):
# row_list = []
# for j in range(len(matrix_data)):
# tmp = []
# for k in range(len(matrix_data)):
# row = k
# column = i
# element = (j + k) % len(matrix_data)
# tmp.append(matrix_data[row][column][element])
# row_list.append(tuple(tmp))
# output.append(row_list)
# return output
#
# @staticmethod
# def reorder_permutation_order_matrix_data_diagonally(matrix_data: MatrixData) -> MatrixData:
# """
# No type checking takes place!
#
# >>> matrix_data = [[('a1', 'b1', 'c1'), ('d1', 'e1', 'f1'), ('g1', 'h1', 'i1')], [('a2', 'b2', 'c2'), ('d2', 'e2', 'f2'), ('g2', 'h2', 'i2')], [('a3', 'b3', 'c3'), ('d3', 'e3', 'f3'), ('g3', 'h3', 'i3')]]
# >>> pprint(matrix_data)
# [[('a1', 'b1', 'c1'), ('d1', 'e1', 'f1'), ('g1', 'h1', 'i1')],
# [('a2', 'b2', 'c2'), ('d2', 'e2', 'f2'), ('g2', 'h2', 'i2')],
# [('a3', 'b3', 'c3'), ('d3', 'e3', 'f3'), ('g3', 'h3', 'i3')]]
# >>> pprint(PermutationOrderMatrixTransposition.reorder_permutation_order_matrix_data_diagonally(matrix_data))
# [[('a1', 'b2', 'c3'), ('b1', 'c2', 'd3'), ('c1', 'd2', 'e3')],
# [('d1', 'e2', 'f3'), ('e1', 'f2', 'g3'), ('f1', 'g2', 'h3')],
# [('g1', 'h2', 'i3'), ('h1', 'i2', 'a3'), ('i1', 'a2', 'b3')]]
# """
# output = []
# for i in range(len(matrix_data)):
# row_list = []
# for j in range(len(matrix_data)):
# tmp = []
# for k in range(len(matrix_data)):
# row = k
# column = (i + (j + k) // len(matrix_data)) % len(matrix_data)
# element = (j + k) % len(matrix_data)
# tmp.append(matrix_data[row][column][element])
# row_list.append(tuple(tmp))
# output.append(row_list)
# return output
#
# def reorder_vertically(self) -> None:
# self.matrix.matrix_data = self.reorder_permutation_order_matrix_data_vertically(self.matrix.matrix_data)
#
# def reorder_half_diagonally(self) -> None:
# self.matrix.matrix_data = self.reorder_permutation_order_matrix_data_half_diagonally(self.matrix.matrix_data)
#
# def reorder_diagonally(self) -> None:
# self.matrix.matrix_data = self.reorder_permutation_order_matrix_data_diagonally(self.matrix.matrix_data)
#
# def reorder(self, mode: PermutationOrderMatrixReorderMode) -> None:
# if mode == 'diagonally':
# self.reorder_diagonally()
# elif mode == 'half_diagonally':
# self.reorder_half_diagonally()
# elif mode == 'vertically':
# self.reorder_vertically()
# else:
# raise ValueError()
8 changes: 8 additions & 0 deletions musurgia/tests/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ def test_get_element(self):
assert self.big_mat.get_element((1, 2)) == (1, 2)
assert self.big_mat.get_element((3, 4)) == (3, 4)

with self.assertRaises(MatrixIndexOutOfRangeError):
self.mat.get_element((20, 30))

def test_transpose(self):
expected = [[(1, 1), (2, 1), (3, 1), (4, 1), (5, 1)],
[(1, 2), (2, 2), (3, 2), (4, 2), (5, 2)],
Expand Down Expand Up @@ -176,6 +179,11 @@ def test_generation(self):
[(2, 4, 1, 3), (3, 1, 4, 2), (1, 2, 3, 4), (4, 3, 2, 1)],
[(1, 2, 3, 4), (2, 4, 1, 3), (4, 3, 2, 1), (3, 1, 4, 2)],
[(4, 3, 2, 1), (1, 2, 3, 4), (3, 1, 4, 2), (2, 4, 1, 3)]]
pomg = PermutationOrderMatrixGenerator(
main_permutation_order=(3, 1, 4, 2))
assert pomg.main_permutation_order == (3, 1, 4, 2)
pomg.main_permutation_order = (1, 2)
assert pomg.main_permutation_order == (1, 2)


class TestPermutationOrderMatrix(TestCase):
Expand Down

0 comments on commit 07d4e7c

Please sign in to comment.