Skip to content

Commit

Permalink
rfctr: move Assemblers to matrix module
Browse files Browse the repository at this point in the history
* An Assembler object is really a matrix that has transforms applied.
  The interface is a matrix interface and it acts like any other matrix.
  Rename `_Assembler` to `TransformedMatrix` and move it to the matrix
  module. Similarly, rename `_StrandAssembler` to `TransformedStripe`
  and move it.

* Rename `._assembler` item in Slice and Strand to `._matrix` and
  `._stripe` respectively. Those objects have no need to access the base
  matrix/stripe so there is no reason to distinguish.
  • Loading branch information
scanny committed May 29, 2019
1 parent 95dd525 commit cab87ec
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 196 deletions.
102 changes: 83 additions & 19 deletions src/cr/cube/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,85 @@
)


class _BaseTransformedMatrix(object):
"""Base class for late-stage matrices that transform a base matrix."""
class TransformedMatrix(object):
"""Matrix reflection application of all transforms."""

def __init__(self, base_matrix):
self._base_matrix = base_matrix
def __init__(self, matrix, ordering):
self._matrix = matrix
self._ordering = ordering

@lazyproperty
def columns_dimension(self):
return self._base_matrix.columns_dimension
def columns(self):
return self._transformed_matrix.columns

@lazyproperty
def rows_dimension(self):
return self._base_matrix.rows_dimension
def rows(self):
return self._transformed_matrix.rows

@lazyproperty
def table_base(self):
return self._base_matrix.table_base
return self._transformed_matrix.table_base

@lazyproperty
def table_base_unpruned(self):
return self._transformed_matrix.table_base_unpruned

@lazyproperty
def table_margin(self):
return self._base_matrix.table_margin
return self._transformed_matrix.table_margin

@lazyproperty
def table_margin_unpruned(self):
return self._transformed_matrix.table_margin_unpruned

@lazyproperty
def _transformed_matrix(self):
"""Apply all transforms sequentially."""
matrix = _OrderedMatrix(self._matrix, self._ordering)
matrix = _MatrixWithInsertions(matrix)
matrix = _MatrixWithHidden(matrix)
return matrix


class TransformedStripe(object):
"""Stripe reflecting application of all transforms."""

def __init__(self, stripe, ordering):
self._stripe = stripe
self._ordering = ordering

@lazyproperty
def rows(self):
"""Sequence of post-transformation row vectors."""
return self._transformed_stripe.rows

@lazyproperty
def table_base_unpruned(self):
"""Hmm, weird 1D ndarray with same int value repeated for each row."""
return self._transformed_stripe.table_base_unpruned

@lazyproperty
def table_margin_unpruned(self):
"""Hmm, weird 1D ndarray with same float value repeated for each row."""
return self._transformed_stripe.table_margin_unpruned

@lazyproperty
def _transformed_stripe(self):
"""Apply all transforms sequentially."""
stripe = _OrderedMatrix(self._stripe, self._ordering)
stripe = _StripeWithInsertions(stripe)
stripe = _StripeWithHidden(stripe)
return stripe

class OrderedMatrix(_BaseTransformedMatrix):

class _OrderedMatrix(object):
"""Result of the ordering transform.
In charge of indexing rows and columns properly.
"""

def __init__(self, base_matrix, ordering):
super(OrderedMatrix, self).__init__(base_matrix)
self._base_matrix = base_matrix
self._ordering = ordering

@lazyproperty
Expand All @@ -71,15 +119,31 @@ def columns(self):
)
)

@lazyproperty
def columns_dimension(self):
return self._base_matrix.columns_dimension

@lazyproperty
def rows(self):
return tuple(
OrderedVector(row, self._ordering.column_order)
for row in tuple(np.array(self._base_matrix.rows)[self._ordering.row_order])
)

@lazyproperty
def rows_dimension(self):
return self._base_matrix.rows_dimension

@lazyproperty
def table_base(self):
return self._base_matrix.table_base

class MatrixWithHidden(_BaseTransformedMatrix):
@lazyproperty
def table_margin(self):
return self._base_matrix.table_margin


class _MatrixWithHidden(object):
"""Matrix with hidden vectors removed.
A vector can be hidden explicitly by the user, or it can be automatically hidden
Expand All @@ -92,7 +156,7 @@ class MatrixWithHidden(_BaseTransformedMatrix):
# ---removing an element from each column-vector in `.columns`.

def __init__(self, base_matrix):
super(MatrixWithHidden, self).__init__(base_matrix)
self._base_matrix = base_matrix

@lazyproperty
def columns(self):
Expand Down Expand Up @@ -139,7 +203,7 @@ def table_margin_unpruned(self):
return self._base_matrix.table_margin


class StripeWithHidden(object):
class _StripeWithHidden(object):
"""Stripe with hidden rows removed.
A row can be hidden explicitly by the user, or it can be automatically hidden when
Expand Down Expand Up @@ -257,11 +321,11 @@ def _rows_inserted_at_bottom(self):
return tuple(row for row in self._all_inserted_rows if row.anchor == "bottom")


class MatrixWithInsertions(_BasePartitionWithInsertions):
class _MatrixWithInsertions(_BasePartitionWithInsertions):
"""Represents slice with both normal and inserted bits."""

def __init__(self, base_matrix):
super(MatrixWithInsertions, self).__init__(base_matrix)
super(_MatrixWithInsertions, self).__init__(base_matrix)
self._base_matrix = base_matrix

@lazyproperty
Expand Down Expand Up @@ -320,11 +384,11 @@ def _iter_inserted_columns_anchored_at(self, anchor):
)


class StripeWithInsertions(_BasePartitionWithInsertions):
class _StripeWithInsertions(_BasePartitionWithInsertions):
"""Represents stripe with both base and inserted row vectors."""

def __init__(self, base_stripe):
super(StripeWithInsertions, self).__init__(base_stripe)
super(_StripeWithInsertions, self).__init__(base_stripe)
self._base_stripe = base_stripe

@lazyproperty
Expand Down
Loading

0 comments on commit cab87ec

Please sign in to comment.