def set_working_directory(self, wd): ''' Method for setting the working directory of the model interface. This method is used in case of running models in parallel. In this case, each worker process will have its own working directory, to avoid having to share files across processes. This requires the need to update the working directory to the new working directory. :param wd: The new working directory. ''' wd = os.path.abspath(wd) debug('setting working directory to '+ wd) self.workingDirectory = wd
def prim_one(x, y, box_init = None, peel_alpha = 0.05, paste_alpha = 0.01, mass_min = 0.05, threshold = None, pasting = False, threshold_type = 1): d = x.shape[1] n = x.shape[0] k_max = np.ceil(1/mass_min) info("k max: %s" %(k_max)) num_boxes = int(k_max) y_mean = np.mean(y) mass_init = y.shape[0]/n #should default to 1 if I read the code correctly if box_init == None: box_init = np.array([np.min(x, axis=0),np.max(x, axis=0)]) box_diff = box_init[1, :] - box_init[0, :] box_init[0,:] = box_init[0, :] - 10*paste_alpha*box_diff box_init[1,:] = box_init[1, :] + 10*paste_alpha*box_diff # find first box k = 1 a = x.shape[0] debug("remaing items: %s" % (a)) boxk = find_box(x=x, y=y, box=box_init, peel_alpha=peel_alpha, paste_alpha=paste_alpha, mass_min=mass_min, threshold=np.min(y)-0.1*np.abs(np.min(y)), d=d, n=n, pasting=pasting) b = boxk.x.shape[0] debug("removed items: %s" %b) if boxk == None: info("unable to find box 1") x_prim = Prim(x, threshold_type*y, y_mean=threshold_type*y_mean, box=box_init, box_mass=mass_init, threshold=np.mean(y)) return x_prim else: info("Found box %s: y_mean=%s, mass=%s" % (k, threshold_type*boxk.y_mean, boxk.box_mass)) boxes = [] boxes.append(boxk) # find subsequent boxes if num_boxes > 1: # data still under consideration x_out_ind_mat = np.empty(x.shape) for j in range(d): x_out_ind_mat[:, j] = (x[:, j] < boxk.box[0,j]) | (x[:,j] >boxk.box[1,j]) x_out_ind = np.any(x_out_ind_mat,axis=1) x_out = x[x_out_ind,:] y_out = y[x_out_ind] a = x_out.shape[0] debug("remaing items: %s" % (a)) while (y_out.shape[0] > 0) & (k < num_boxes): k = k+1 boxk = find_box(x=x_out, y=y_out, box=box_init, peel_alpha=peel_alpha, paste_alpha=paste_alpha, mass_min=mass_min, threshold=np.min(y)-0.1*np.abs(np.min(y)), d=d, n=n, pasting=pasting) if boxk == None: info("Bump "+str(k)+" includes all remaining data") boxk = Prim(x_out, y_out, np.mean(y_out), box_init, y_out.shape[0]/n) b += boxk.x.shape[0] debug("removed items: %s" %b) boxes.append(boxk) break else: b += boxk.x.shape[0] debug("removed items: %s" %b) # update x and y debug("Found box %s: y_mean=%s, mass=%s" % (k, threshold_type*boxk.y_mean, boxk.box_mass)) # data still under consideration x_out_ind_mat = np.empty(x.shape) for j in range(d): x_out_ind_mat[:, j] = (x[:, j] < boxk.box[0,j]) | (x[:,j] >boxk.box[1,j]) x_out_ind_mat = np.any(x_out_ind_mat,axis=1) x_out_ind = x_out_ind & x_out_ind_mat x_out = x[x_out_ind, :] y_out = y[x_out_ind] a = x_out.shape[0] debug("remaing items: %s" %(a)) boxes.append(boxk) # adjust for negative hdr for box in boxes: box.y = threshold_type*box.y box.y_mean = threshold_type*box.y_mean prim_res, num_hdr= prim_hdr(boxes, threshold, threshold_type) return prim_res, num_hdr
def show_boxes_together(boxes, results, uv=[], filter=True): ''' This functions visually shows the size of a list of prim boxes. Each box has its own color. The dump box is not shown. The size of the boxes is normalized, where 0 is the lowest sampled value for each uncertainty and 1 is the highest sampled value for each uncertainty. his is visualized using a light grey background. :param boxes: the list of prim objects as returned by :func:`perform_prim`. :param results: the results as returnd by :meth:`perform_experiments` :param uv: the uncertainties to show in the plot. Defaults to an empty list, meaning all the uncertainties will be shown. If the list is not empty only the uncertainties specified in uv will be plotted. :param filter: boolean, if True, the uncertainties for which all the boxes equal the size of the dump box are not visualized (default=True) :rtype: a `figure <http://matplotlib.sourceforge.net/api/figure_api.html>`_ instance ''' experiments, results = results boxes = [element.box for element in boxes] #determine minima and maxima boxes = __normalize(boxes, experiments) dump_box = boxes[-1] boxes = boxes[0:-1] uncertainties = [] for entry in experiments.dtype.descr: uncertainties.append(entry[0]) if not uv: uv = uncertainties #plot results figure = plt.figure() ax = figure.add_subplot(111) #iterate over uncertainties names = [] i = -1 for name in uncertainties: if name in uv: show_uncertainty = True if filter: show_uncertainty=False #determine whether to show for box in boxes: minimum = box[name][0] maximum = box[name][1] value = box.dtype.fields.get(name)[0] if value == 'object': debug("filtering name") debug(dump_box[name][0]) debug(minimum) a = dump_box[name][0] if len(minimum) == len(a): ans = np.all(np.equal(a, minimum)) else: ans = False if not ans: show_uncertainty = True break elif (minimum > dump_box[name][0]) or\ (maximum < dump_box[name][1]): show_uncertainty = True break if show_uncertainty: i+=1 names.append(name) #iterate over boxes for j, box in enumerate(boxes): if value == 'object': y = box[name][0] x = [i+0.1*(j+1) for entry in range(len(y))] ax.scatter(x,y,edgecolor=COLOR_LIST[j%len(COLOR_LIST)], facecolor=COLOR_LIST[j%len(COLOR_LIST)]) else: ax.plot([i+0.1*(j+1), i+0.1*(j+1)], box[name][:], c=COLOR_LIST[j%len(COLOR_LIST)]) rect = mpl.patches.Rectangle((-0.5, 0), i+1.5, 1, facecolor="#C0C0C0", alpha=0.25, edgecolor="#C0C0C0") ax.add_patch(rect) ax.set_xlim(xmin= -0.5, xmax=len(names)-0.5) ax.set_ylim(ymin=-0.2, ymax=1.2) ax.xaxis.set_ticks([x for x in range(len(names))]) xtickNames = plt.setp(ax, xticklabels = names) plt.setp(xtickNames, rotation=90, fontsize=12) ytickNames = ax.get_yticklabels() plt.setp(ytickNames, rotation=90, fontsize=12) ax.set_ylabel('normalized uncertainty bandwidth', rotation=90, fontsize=8) return figure