Skip to content

Commit

Permalink
Further fixes to UninstantiatedColumn
Browse files Browse the repository at this point in the history
  • Loading branch information
smathot committed May 10, 2023
1 parent 5a18ca5 commit b836523
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 15 deletions.
39 changes: 25 additions & 14 deletions datamatrix/_datamatrix/_datamatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,7 @@ 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
self._instantiate()
return self._to_list(self._cols.items(), key=lambda col: col[0])

@property
Expand Down Expand Up @@ -340,6 +337,7 @@ def _slice(self, key):
type: DataMatrix.
"""

self._instantiate()
# A tuple of length two is interpreted by a SeriesColumn as a sample
# and row. Therefore, we turn tuples into lists.
if isinstance(key, tuple):
Expand Down Expand Up @@ -376,7 +374,7 @@ def _setlength(self, value):
desc: The new length.
type: int
"""

self._instantiate()
if value < len(self):
object.__setattr__(self, u'_rowid', self._rowid[:value])
for name, col in self._cols.items():
Expand Down Expand Up @@ -424,7 +422,7 @@ def _merge(self, other, _rowid):
returns:
type: DataMatrix.
"""

self._instantiate()
if self != other:
raise Exception('Can only merge related datamatrices')
dm = DataMatrix(len(_rowid))
Expand Down Expand Up @@ -495,7 +493,7 @@ def _getcolbyobject(self, key):
returns:
type: BaseColumn
"""

self._instantiate()
for col in self._cols.values():
if col is key:
return col
Expand All @@ -522,13 +520,7 @@ def _getcolbyname(self, key):
col = self._cols.get(key, None)
if col is None:
raise AttributeError(u'No column named "%s"' % key)
# If an UninstantiatedColumn is requested, then we turn it into an
# instantiated column, i.e. a copy of a larger column with a copy of
# the selected data.
if isinstance(col, UninstantiatedColumn):
col = col.instantiate()
self._cols[key] = col
return col
return self._instantiate_column(key, col)

def _getrow(self, key):

Expand Down Expand Up @@ -815,6 +807,7 @@ def __getitem__(self, key):

def __str__(self):

self._instantiate()
if len(self) > PRINT_MAX_ROWS:
return str(self[:PRINT_MAX_ROWS]) + u'\n(+ %d rows not shown)' \
% (len(self) - PRINT_MAX_ROWS)
Expand Down Expand Up @@ -876,6 +869,7 @@ def __array__(self, *args):
"""

import numpy as np
self._instantiate()
a = np.empty((len(self), len(self.columns)), dtype=object)
for i, col in enumerate(self._cols.values()):
if hasattr(col, 'depth'):
Expand All @@ -897,3 +891,20 @@ def __dataframe__(self):
from datamatrix import convert as cnv

return cnv.to_pandas(self)

def _instantiate(self):
"""Instantiates all uninstantiated columns"""
for name, col in self._cols.items():
if isinstance(col, UninstantiatedColumn):
col = col.instantiate()
self._cols[name] = col

def _instantiate_column(self, key, col):
"""Makes sure that a specific column is instantiated, and returns the
instantiated column.
"""
if not isinstance(col, UninstantiatedColumn):
return col
col = col.instantiate()
self._cols[key] = col
return col
3 changes: 2 additions & 1 deletion datamatrix/io/_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,15 @@ def writebin(dm, path):
~~~ .python
io.writebin(dm, 'data.dm')
~~~
*Version note:* New in 1.0.0
arguments:
dm: The DataMatrix to write.
path: The path to the binary file.
"""
dm._instantiate()
if np is None:
raise Exception(
'NumPy and SciPy are required, but not installed.')
Expand Down
1 change: 1 addition & 0 deletions datamatrix/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def stack(*dms):
start_index = 0
dm = DataMatrix(length=new_length)
for stackdm in dms:
stackdm._instantiate()
for name, col in stackdm._cols.items():
if name not in dm._cols:
if isinstance(col, _MultiDimensionalColumn):
Expand Down
37 changes: 37 additions & 0 deletions testcases/test_uninstantiated_column.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-

"""
This file is part of datamatrix.
datamatrix is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
datamatrix is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with datamatrix. If not, see <http://www.gnu.org/licenses/>.
"""

from datamatrix.py3compat import *
from datamatrix import DataMatrix, operations as ops, MultiDimensionalColumn


def test_uninstantiated_column():
# Minimal testing of operating on uninstantiated columns
dm = DataMatrix(length=4)
dm.c = 1, 1, 2, 2
dm.i = int
dm.f = float
dm.s = 's'
dm.m = MultiDimensionalColumn(shape=(2,))
for c, cdm in ops.split(dm.c):
cdm[:]
for c, cdm in ops.split(dm.c):
print(cdm)
for c, cdm in ops.split(dm.c):
len(cdm.i)

0 comments on commit b836523

Please sign in to comment.