Skip to content

Commit

Permalink
UninstantiatedColumn now passes unit tests
Browse files Browse the repository at this point in the history
- Avoid creating many separate DataMatrix objects when selecting
  • Loading branch information
smathot committed May 5, 2023
1 parent 6c9f6f3 commit 5a18ca5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions datamatrix/_datamatrix/_basecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ def _getdatamatrixkey(self, key):
raise ValueError('Cannot slice column with a different DataMatrix')
return self[[self._rowid.index(_rowid) for _rowid in key._rowid]]

def _getrowidkey(self, key):
def _getrowidkey(self, key, dm=None):

"""
visible: False
Expand All @@ -599,7 +599,7 @@ def _getrowidkey(self, key):
BaseColunn
"""
seq = [self._seq[self._rowid.index(_rowid)] for _rowid in key]
return self._empty_col(rowid=key, seq=seq)
return self._empty_col(rowid=key, seq=seq, datamatrix=dm)

def _sortedrowid(self):

Expand Down
9 changes: 7 additions & 2 deletions datamatrix/_datamatrix/_datamatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ def empty(self):
@property
def columns(self):

for name, col in self._cols.items():
if isinstance(col, UninstantiatedColumn):
col = col.instantiate()
self._cols[name] = col
return self._to_list(self._cols.items(), key=lambda col: col[0])

@property
Expand Down Expand Up @@ -310,12 +314,13 @@ def _selectrowid(self, _rowid):
# By default we create new columns with a copy of the selected data
if not hasattr(self, '_instantiate_on_select') or \
self._instantiate_on_select:
dm._cols[name] = self._cols[name]._getrowidkey(_rowid)
dm._cols[name] = self._cols[name]._getrowidkey(_rowid, dm)
# Except when _instatiate_on_select is set to False, in which case
# we create an UninstantiatedColumn object which can be turned into
# an actual column when it is requested in _getcolbyname()
else:
dm._cols[name] = UninstantiatedColumn(self._cols[name], _rowid)
dm._cols[name] = UninstantiatedColumn(
name, self._cols[name], _rowid, dm)
dm._cols[name]._datamatrix = dm
return dm

Expand Down
5 changes: 3 additions & 2 deletions datamatrix/_datamatrix/_numericcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def _getintkey(self, key):

return self.dtype(self._seq[key])

def _getrowidkey(self, key):
def _getrowidkey(self, key, dm=None):

if isinstance(key, Index):
key = key._a
Expand All @@ -249,7 +249,8 @@ def _getrowidkey(self, key):
else:
selected_indices = selected_indices_cache[1]
return self._empty_col(rowid=self._rowid[selected_indices],
seq=self._seq[selected_indices])
seq=self._seq[selected_indices],
datamatrix=dm)

def _setdatamatrixkey(self, key, val):

Expand Down
14 changes: 10 additions & 4 deletions datamatrix/_datamatrix/_uninstantiatedcolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@ class UninstantiatedColumn:
Parameters
----------
name: str
parent_col: BaseColumn
rowid: Index
dm: DataMatrix
"""
def __init__(self, parent_col, rowid):
def __init__(self, name, parent_col, rowid, dm):
logger.debug(
f'creating uninstantiatedselection from {parent_col.name}')
f'creating uninstantiatedselection from {name}')
self._parent_col = parent_col
self._rowid = rowid
self._dm = dm
self._name = name

def instantiate(self):
logger.debug(f'instantiating selection from {self._parent_col.name}')
return self._parent_col._getrowidkey(self._rowid)
logger.debug(f'instantiating selection from {self._name}')
if isinstance(self._parent_col, UninstantiatedColumn):
self._parent_col = self._parent_col.instantiate()
return self._parent_col._getrowidkey(self._rowid, self._dm)

0 comments on commit 5a18ca5

Please sign in to comment.