Errors when identifers dont match shape, + whitespace
This commit is contained in:
parent
48bb47ec86
commit
ed2848beb3
|
@ -42,22 +42,24 @@ class Dataset:
|
||||||
self._name = name
|
self._name = name
|
||||||
self._identifiers = identifiers
|
self._identifiers = identifiers
|
||||||
self._type = 'n'
|
self._type = 'n'
|
||||||
try:
|
|
||||||
|
if len(array.shape)==1:
|
||||||
array = atleast_2d(asarray(array))
|
array = atleast_2d(asarray(array))
|
||||||
except:
|
|
||||||
print "Cant cast array as numpy-array"
|
|
||||||
return
|
|
||||||
# vectors are column vectors
|
# vectors are column vectors
|
||||||
if array.shape[0]==1:
|
if array.shape[0]==1:
|
||||||
array = array.T
|
array = array.T
|
||||||
self.shape = array.shape
|
self.shape = array.shape
|
||||||
|
|
||||||
if identifiers!=None:
|
if identifiers!=None:
|
||||||
|
identifier_shape = [len(i[1]) for i in identifiers]
|
||||||
|
if len(identifier_shape)!=len(self.shape):
|
||||||
|
raise ValueError, "Identifier list length must equal array dims"
|
||||||
|
for ni, na in zip(identifier_shape, self.shape):
|
||||||
|
if ni!=na:
|
||||||
|
raise ValueError, "identifier-array mismatch in %s: (idents: %s, array: %s)" %(self._name, ni, na)
|
||||||
self._set_identifiers(identifiers, self._all_dims)
|
self._set_identifiers(identifiers, self._all_dims)
|
||||||
else:
|
else:
|
||||||
self._identifiers = self._create_identifiers(self.shape, self._all_dims)
|
self._identifiers = self._create_identifiers(self.shape, self._all_dims)
|
||||||
self._set_identifiers(self._identifiers, self._all_dims)
|
self._set_identifiers(self._identifiers, self._all_dims)
|
||||||
|
|
||||||
self._array = array
|
self._array = array
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
|
@ -103,7 +105,6 @@ class Dataset:
|
||||||
all_dims.add(dim)
|
all_dims.add(dim)
|
||||||
else:
|
else:
|
||||||
raise ValueError, "Dimension names must be unique whitin dataset"
|
raise ValueError, "Dimension names must be unique whitin dataset"
|
||||||
|
|
||||||
for pos, id in enumerate(ids):
|
for pos, id in enumerate(ids):
|
||||||
pos_map[id] = pos
|
pos_map[id] = pos
|
||||||
self._map[dim] = pos_map
|
self._map[dim] = pos_map
|
||||||
|
@ -125,7 +126,6 @@ class Dataset:
|
||||||
"""Adds array as an ArrayType object.
|
"""Adds array as an ArrayType object.
|
||||||
A one-dim array is transformed to a two-dim array (row-vector)
|
A one-dim array is transformed to a two-dim array (row-vector)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if self.shape!=array.shape:
|
if self.shape!=array.shape:
|
||||||
raise ValueError, "Input array must be of similar dimensions as dataset"
|
raise ValueError, "Input array must be of similar dimensions as dataset"
|
||||||
self._array = atleast_2d(asarray(array))
|
self._array = atleast_2d(asarray(array))
|
||||||
|
@ -178,7 +178,6 @@ class Dataset:
|
||||||
You can optionally provide a list of identifiers to retrieve a
|
You can optionally provide a list of identifiers to retrieve a
|
||||||
index subset.
|
index subset.
|
||||||
|
|
||||||
|
|
||||||
Identifiers are the unique names (strings) for a variable in a
|
Identifiers are the unique names (strings) for a variable in a
|
||||||
given dim. Index (Indices) are the Identifiers position in a
|
given dim. Index (Indices) are the Identifiers position in a
|
||||||
matrix in a given dim. If none of the input identifiers are
|
matrix in a given dim. If none of the input identifiers are
|
||||||
|
@ -254,7 +253,7 @@ class GraphDataset(Dataset):
|
||||||
|
|
||||||
A dataset class for representing graphs using an (weighted)
|
A dataset class for representing graphs using an (weighted)
|
||||||
adjacency matrix
|
adjacency matrix
|
||||||
(aka. restricted to square symmetric matrices)
|
(restricted to square symmetric matrices)
|
||||||
|
|
||||||
If the library NetworkX is installed, there is support for
|
If the library NetworkX is installed, there is support for
|
||||||
representing the graph as a NetworkX.Graph, or NetworkX.XGraph structure.
|
representing the graph as a NetworkX.Graph, or NetworkX.XGraph structure.
|
||||||
|
@ -310,6 +309,7 @@ class GraphDataset(Dataset):
|
||||||
|
|
||||||
Dataset._all_dims = set()
|
Dataset._all_dims = set()
|
||||||
|
|
||||||
|
|
||||||
class ReverseDict(dict):
|
class ReverseDict(dict):
|
||||||
"""
|
"""
|
||||||
A dictionary which can lookup values by key, and keys by value.
|
A dictionary which can lookup values by key, and keys by value.
|
||||||
|
@ -341,12 +341,15 @@ def to_file(filepath,dataset,name=None):
|
||||||
names = data.keys()
|
names = data.keys()
|
||||||
if name in names:
|
if name in names:
|
||||||
print "Data with name: %s overwritten" %dataset._name
|
print "Data with name: %s overwritten" %dataset._name
|
||||||
sub_data = {'array':dataset._array,'idents':dataset._identifiers,'type':dataset._type}
|
|
||||||
|
sub_data = {'array':dataset._array,
|
||||||
|
'idents':dataset._identifiers,
|
||||||
|
'type':dataset._type}
|
||||||
data[name] = sub_data
|
data[name] = sub_data
|
||||||
data.close()
|
data.close()
|
||||||
|
|
||||||
def from_file(filepath):
|
def from_file(filepath):
|
||||||
"""Read dataset from file """
|
"""Read dataset(s) from file """
|
||||||
data = shelve.open(filepath, flag='r')
|
data = shelve.open(filepath, flag='r')
|
||||||
out_data = []
|
out_data = []
|
||||||
for name in data.keys():
|
for name in data.keys():
|
||||||
|
@ -360,6 +363,7 @@ def from_file(filepath):
|
||||||
|
|
||||||
return out_data
|
return out_data
|
||||||
|
|
||||||
|
|
||||||
class Selection(dict):
|
class Selection(dict):
|
||||||
"""Handles selected identifiers along each dimension of a dataset"""
|
"""Handles selected identifiers along each dimension of a dataset"""
|
||||||
|
|
||||||
|
|
Reference in New Issue