def save(self, node, key, val, type='data'): ''' obj_key The variable that points to the object being stored obj_type The type of the object being stored id The configuration number being used keys The variables in the object vals and their values ''' # If a groupname is supplied, open/create it and enter that group if type == 'group' and pl.is_string_like(key): try: node = self.h5f.getNode(node, key) except: node = self.h5f.createGroup(node, key, filters=self.FILTERS) # If an id is supplied, make a group for it and enter that group elif type == 'id' and pl.is_numlike(key): try: node = self.h5f.getNode(node, "i%d" % key) except: node = self.h5f.createGroup(node, "i%d" % key, filters=self.FILTERS) # When data is supplied, add it (and overwrite any data that was there before) elif type == 'data': if key == None: key = 'None' elif not pl.is_string_like(key): error(self, 'This should not happen...') try: self.h5f.removeNode(node, key) except: pass if issubclass(val.__class__, np.ndarray): # Empty tuples not supported by pytables. Fix by reshaping to (1,) if val.shape == (): val = val.reshape((1, )) atom = tb.Atom.from_dtype(val.dtype) new_node = self.h5f.createCArray(node, key, atom, val.shape, filters=self.FILTERS) new_node[:] = val[:] else: self.h5f.createArray(node, key, val) else: error(self, 'Hmm? Either \'type\' or \'key\' is of shitty format.') node = self.h5f.root return node
def save(self, node, key, val, type='data'): ''' obj_key The variable that points to the object being stored obj_type The type of the object being stored id The configuration number being used keys The variables in the object vals and their values ''' # If a groupname is supplied, open/create it and enter that group if type=='group' and pl.is_string_like(key): try: node = self.h5f.getNode(node, key) except: node = self.h5f.createGroup(node, key, filters=self.FILTERS) # If an id is supplied, make a group for it and enter that group elif type=='id' and pl.is_numlike(key): try: node = self.h5f.getNode(node, "i%d"%key) except: node = self.h5f.createGroup(node, "i%d"%key, filters=self.FILTERS) # When data is supplied, add it (and overwrite any data that was there before) elif type=='data': if key == None: key = 'None' elif not pl.is_string_like(key): error(self, 'This should not happen...') try: self.h5f.removeNode(node, key) except: pass if issubclass(val.__class__, np.ndarray): # Empty tuples not supported by pytables. Fix by reshaping to (1,) if val.shape == (): val = val.reshape((1,)) atom = tb.Atom.from_dtype(val.dtype) new_node = self.h5f.createCArray(node, key, atom, val.shape, filters=self.FILTERS) new_node[:] = val[:] else: self.h5f.createArray(node, key, val) else: error(self, 'Hmm? Either \'type\' or \'key\' is of shitty format.') node = self.h5f.root return node
def save_nodes(self,filename,path='./results/'): """Saves the contents of a Nodes instance to a npz file.""" attribute = dir(self[0]) save_str = [] #Determine which attributes to be saved for attribute in dir(self[0]): if attribute[0]=='_': continue elif is_numlike(getattr(self[0],attribute)) or is_string_like(getattr(self[0],attribute)): save_str.append(attribute + '=' + 'np.array([self[i].'+attribute+' for i in np.arange(len(self))])') #Write save file eval('np.savez(path+filename,'+','.join(save_str)+')') print 'Saved nodes to file: ', path+filename sys.stdout.flush()
def save_nodes(self, filename, path="./results/"): """Saves the contents of a Nodes instance to a npz file.""" attribute = dir(self[0]) save_str = [] # Determine which attributes to be saved for attribute in dir(self[0]): if attribute[0] == "_": continue elif is_numlike(getattr(self[0], attribute)) or is_string_like(getattr(self[0], attribute)): save_str.append(attribute + "=" + "array([self[i]." + attribute + " for i in arange(len(self))])") # Write save file eval("np.savez(path+filename," + ",".join(save_str) + ")") print "Saved nodes to file: ", path + filename sys.stdout.flush()