Skip to content

Commit

Permalink
Fix add_column header bug
Browse files Browse the repository at this point in the history
  • Loading branch information
gb119 committed Mar 5, 2016
1 parent 88b84ac commit a39f2c9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
18 changes: 10 additions & 8 deletions Stoner/Core.py
Expand Up @@ -718,7 +718,7 @@ def findtype(self, value):
elements.append(self.findtype(value[k]))
else:
for i,v in enumerate(value):
elements.append(self.findtype(v))
elements.append(self.findtype(v))
tt = ','
tt = tt.join(elements)
typ = 'Cluster (' + tt + ')'
Expand Down Expand Up @@ -1340,7 +1340,7 @@ class DataFile(object):

#mimetypes we match
mime_type=["text/plain"]

_conv_string = _np_.vectorize(lambda x: str(x))
_conv_float = _np_.vectorize(lambda x: float(x))

Expand Down Expand Up @@ -2580,7 +2580,7 @@ def add_column(self, column_data, header=None, index=None, func_args=None, repla
Like most :py:class:`DataFile` methods, this method operates in-place in that it also modifies
the original DataFile Instance as well as returning it."""
if index is None or isinstance(index,bool) and index:
index = len(self.column_headers)
index = self.shape[1]
replace = False
if header is None:
header = "Col{}".format(index)
Expand All @@ -2589,6 +2589,9 @@ def add_column(self, column_data, header=None, index=None, func_args=None, repla
if header is None:
header = self.column_headers[index]

if isinstance(column_data, list):
column_data = _np_.array(column_data)

if isinstance(column_data, _np_.ndarray):
if len(_np_.shape(column_data)) != 1:
raise ValueError('Column data must be 1 dimensional')
Expand All @@ -2600,8 +2603,6 @@ def add_column(self, column_data, header=None, index=None, func_args=None, repla
else:
new_data = [column_data(x) for x in self]
_np__data = _np_.array(new_data)
elif isinstance(column_data, list):
_np__data = _np_.array(column_data)
else:
return NotImplemented
#Sort out the sizes of the arrays
Expand All @@ -2621,7 +2622,8 @@ def add_column(self, column_data, header=None, index=None, func_args=None, repla
if replace:
self.data[:, index] = _np__data
else:
self.column_headers.insert(index, header)
newcols=copy.copy(self.column_headers)
newcols.insert(index, header)
if dc * dr == 0:
self.data = DataArray(_np_.transpose(_np_.atleast_2d(_np__data)),setas=self.data._setas)
else:
Expand All @@ -2633,7 +2635,7 @@ def add_column(self, column_data, header=None, index=None, func_args=None, repla
self.setas(setas)
self.column_headers=columns
#Finally sort out column headers
self.column_headers[index]= header
self.column_headers=newcols


return self
Expand Down Expand Up @@ -2984,7 +2986,7 @@ def load(self, filename=None, auto_load=True, filetype=None, *args, **kargs):
for cls in self.subclasses.values():
try:
if filemagic is not None and mimetype not in cls.mime_type: #short circuit for non-=matching mime-types
if self.debug: print("Skipping {} due to mismatcb mime type {}".format(cls.__name__,cls.mime_type))
if self.debug: print("Skipping {} due to mismatcb mime type {}".format(cls.__name__,cls.mime_type))
continue
test = cls()
if self.debug and filemagic is not None:
Expand Down
2 changes: 1 addition & 1 deletion Stoner/__init__.py
Expand Up @@ -10,6 +10,6 @@
from .Util import Data
from Stoner.Folders import DataFolder

__version_info__ = ('0', '6', '0rc1')
__version_info__ = ('0', '6', '0rc2')
__version__ = '.'.join(__version_info__)

7 changes: 7 additions & 0 deletions tests/Stoner/test_Core.py
Expand Up @@ -166,6 +166,13 @@ def test_methods(self):
d.insert_rows(10,np.zeros((2,2)))
self.assertEqual(len(d),102,"Failed to inert extra rows")
self.assertTrue(d[9,0]==10 and d[10,0]==0 and d[12,0]==11, "Failed to insert rows properly.")
d=self.d.clone
d.add_column(np.ones(len(d)),replace=False,header="added")
self.assertTrue(d.shape[1]==self.d.shape[1]+1,"Adding a column with replace=False did add a column.")
self.assertTrue(np.all(d.data[:,-1]==np.ones(len(d))),"Didn't add the new column to the end of the data.")
self.assertTrue(len(d.column_headers)==len(self.d.column_headers)+1,"Column headers isn't bigger by one")
self.assertTrue(d.column_headers==self.d.column_headers+["added",],"Column header not added correctly")


if __name__=="__main__": # Run some tests manually to allow debugging
test=Datatest("test_operators")
Expand Down

0 comments on commit a39f2c9

Please sign in to comment.