Projects/laydi
Projects
/
laydi
Archived
7
0
Fork 0

Errors when identifers dont match shape, + whitespace

This commit is contained in:
Arnar Flatberg 2007-01-31 11:54:54 +00:00
parent 48bb47ec86
commit ed2848beb3
1 changed files with 51 additions and 47 deletions

View File

@ -42,22 +42,24 @@ class Dataset:
self._name = name
self._identifiers = identifiers
self._type = 'n'
try:
if len(array.shape)==1:
array = atleast_2d(asarray(array))
except:
print "Cant cast array as numpy-array"
return
# vectors are column vectors
if array.shape[0]==1:
array = array.T
self.shape = array.shape
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)
else:
self._identifiers = self._create_identifiers(self.shape, self._all_dims)
self._set_identifiers(self._identifiers, self._all_dims)
self._array = array
def __iter__(self):
@ -103,7 +105,6 @@ class Dataset:
all_dims.add(dim)
else:
raise ValueError, "Dimension names must be unique whitin dataset"
for pos, id in enumerate(ids):
pos_map[id] = pos
self._map[dim] = pos_map
@ -125,7 +126,6 @@ class Dataset:
"""Adds array as an ArrayType object.
A one-dim array is transformed to a two-dim array (row-vector)
"""
if self.shape!=array.shape:
raise ValueError, "Input array must be of similar dimensions as dataset"
self._array = atleast_2d(asarray(array))
@ -178,7 +178,6 @@ class Dataset:
You can optionally provide a list of identifiers to retrieve a
index subset.
Identifiers are the unique names (strings) for a variable in a
given dim. Index (Indices) are the Identifiers position in a
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)
adjacency matrix
(aka. restricted to square symmetric matrices)
(restricted to square symmetric matrices)
If the library NetworkX is installed, there is support for
representing the graph as a NetworkX.Graph, or NetworkX.XGraph structure.
@ -310,6 +309,7 @@ class GraphDataset(Dataset):
Dataset._all_dims = set()
class ReverseDict(dict):
"""
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()
if name in names:
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.close()
def from_file(filepath):
"""Read dataset from file """
"""Read dataset(s) from file """
data = shelve.open(filepath, flag='r')
out_data = []
for name in data.keys():
@ -360,6 +363,7 @@ def from_file(filepath):
return out_data
class Selection(dict):
"""Handles selected identifiers along each dimension of a dataset"""