# *-* coding: utf-8 *-*"""Manage node value sets for a given grid. Node data assigns a numeric valueto each node of a grid. This could, for example, be potential distributions orsensitivities for single files."""importnumpyasnp
[docs]classNodeMan(object):"""manage one or more node value sets for a given :class:`crtomo.grid.crt_grid` object """
[docs]def__init__(self,grid_obj):""" Parameters ---------- grid_obj: crtomo.grid.crt_grid The FE grid that the parameter sets refer to """# check if grid is already initializedifgrid_obj.gridisNone:raiseException('Grid object is not initialized!')self.grid=grid_obj# we store the node value sets in hereself.nodevals={}# here we can store metadata for a node value setself.metadata={}# we assign indices to each data set stored in the manager. This index# should be unique over the life time of each instance. Therefore we# increase the counter for each added data set. We also ensure# conflicts when data sets are removed.self.index=-1
[docs]defadd_data(self,data):"""Add data to the node value sets Parameters ---------- data: numpy.ndarray one or more node value sets. It must either be 1D or 2D, with the first dimension the number of parameter sets (K), and the second the number of elements (Z): K x Z Examples -------- >>> # suppose that grid is a fully initialized grid oject with 50 nodes nodeman = NodeMan(grid) # one_data_set = np.ones(50) cid = nodeman.add_data(one_data_set) print(nodeman.parsets[cid]) two_data_sets = np.ones((2, 50)) cids = nodeman.add_data(two_data_sets) print(cids) [0, ] [1, 2] """subdata=np.atleast_2d(data)# we try to accommodate transposed inputifsubdata.shape[1]!=self.grid.nr_of_nodes:ifsubdata.shape[0]==self.grid.nr_of_nodes:subdata=subdata.Telse:raiseException('Number of values does not match the number of '+'nodes in the grid {0} grid nodes vs {1} data'.format(self.grid.nr_of_nodes,subdata.shape,))return_ids=[]fordatasetinsubdata:cid=self._get_next_index()self.nodevals[cid]=dataset.copy()return_ids.append(cid)iflen(return_ids)==1:returnreturn_ids[0]else:returnreturn_ids