def export_2d_data_for_Maxim_helper(self, x_axis, y_axis, data, path_and_filename, **kwargs): """ INPUT: - data: - x_range, y_range (optional) OUTPUT: CHANGELOG: 20160502-RB: started function """ if "x_range" in kwargs: x_range = kwargs["x_range"] else: x_range = [0,0] if "y_range" in kwargs: y_range = kwargs["y_range"] else: y_range = [0,-1] # determine the range to be plotted x_min, x_max, y_min, y_max = FU.find_axes(x_axis, y_axis, x_range, y_range, self.flag_verbose) # find the area to be plotted x_min_i, x_max_i = FU.find_axes_indices(x_axis, x_min, x_max) y_min_i, y_max_i = FU.find_axes_indices(y_axis, y_min, y_max) # truncate the data data, x_axis, y_axis = FU.truncate_data(data, y_axis, x_axis, y_min_i, y_max_i, x_min_i, x_max_i) n_x = len(x_axis) n_y = len(y_axis) with open(path_and_filename, 'w') as f: for x_i in range(n_x): for y_i in range(n_y): f.write("{y:4.3f}, {x:4.3f}, {z:.8f}\n".format(y = y_axis[y_i], x = x_axis[x_i], z = data[y_i, x_i])) # f.write("\n") f.close()
def test_c4_zm1(self): """ contours = 4, zlimit = -1 res == [-3,-1,1,3] """ res = FU.make_contours_2d(self.data, contours = 4, zlimit = -1, flag_verbose = self.flag_verbose) self.assertTrue(numpy.all(res == [-3,-1,1,3]))
def test_c3_zlist(self): """ contours = 3, zlimit = 2 res == [-2,0,2] """ res = FU.make_contours_2d(self.data, contours = 3, zlimit = [-3,1], flag_verbose = self.flag_verbose) self.assertTrue(numpy.all(res == [-3,-1,1]))
def test_c3_z0(self): """ contours = 3, zlimit = 0 res == [-3,-0.5,2] """ res = FU.make_contours_2d(self.data, contours = 3, zlimit = 0, flag_verbose = self.flag_verbose) self.assertTrue(numpy.all(res == [-3,-0.5,2]))
def test_c4_zlist(self): """ contours = 4, zlimit = [-4,2] res == [-4,-2,0,2] """ res = FU.make_contours_2d(self.data, contours = 4, zlimit = [-4,2], flag_verbose = self.flag_verbose) self.assertTrue(numpy.all(res == [-4,-2,0,2]))
def test_c4_zlist_reverse(self): """ list from high to low contours = 4, zlimit = [2,-4] res == [2,0,-2,-4] """ res = FU.make_contours_2d(self.data, contours = 4, zlimit = [2,-4], flag_verbose = self.flag_verbose) self.assertTrue(numpy.all(res == [2,0,-2,-4]))
def test_c4_z2(self): """ contours = 4, zlimit = 2.4 res == [-2.4,-0.8,0.8,2.4] using numpy.allclose() for rounding error in third element """ res = FU.make_contours_2d(self.data, contours = 4, zlimit = 2.4, flag_verbose = self.flag_verbose) self.assertTrue(numpy.allclose(res, [-2.4, -0.8 , 0.8 , 2.4]))
def test_c4_z0(self): """ contours = 4, zlimit = 0 res == [-3,-1.33333,0.33333,2] using numpy.allclose() for rounding error """ res = FU.make_contours_2d(self.data, contours = 4, zlimit = 0, flag_verbose = self.flag_verbose) self.assertTrue(numpy.allclose(res, [-3,-1.33333,0.33333,2]))
def test_c4_zm2(self): """ zlimit is negative, zlimit = -2 should give same result as zlimit = 2 contours = 4, zlimit = -2 res == [-3,-1,1,3] """ res = FU.make_contours_2d(self.data, contours = 4, zlimit = -2, flag_verbose = self.flag_verbose) self.assertTrue(numpy.allclose(res, [-2, -0.66666667, 0.66666667,2]))
def test_within_limits_and_xmax_and_ymax(self): x_min_i = 7 x_max_i = 19 y_min_i = 2 y_max_i = 9 data, x_axis, y_axis = FU.truncate_data(self.data, self.x_axis, self.y_axis, x_min_i, x_max_i, y_min_i, y_max_i, flag_verbose = self.flag_verbose) self.assertEqual(numpy.shape(data), (7,12)) self.assertTrue(numpy.all(x_axis == range(x_min_i,x_max_i))) self.assertTrue(numpy.all(y_axis == range(y_min_i,y_max_i)))
def test_within_limits_and_xm1_and_ym1(self): x_min_i = 7 x_max_i = -1 y_min_i = 2 y_max_i = -1 data, x_axis, y_axis = FU.truncate_data(self.data, self.x_axis, self.y_axis, x_min_i, x_max_i, y_min_i, y_max_i, flag_verbose = self.flag_verbose) self.assertEqual(numpy.shape(data), (8,13)) self.assertTrue(numpy.all(x_axis == range(x_min_i,20))) self.assertTrue(numpy.all(y_axis == range(y_min_i,10)))
def test_reverse_edges(self): """ The function does no sanity-checking. """ val_min = 27.0 val_max = 22.0 DEBUG.verbose("\nWarning is intentional", True) res = FU.find_axes_indices(self.axis, val_min, val_max) self.assertEqual(res, (7,4))
def make_overview_plot(self, **kwargs): """ Make a single figure with all plots. INPUT: - kwargs: - sp, sm, de, du, sc (lists, ndarray): lists that limit the range of spectra to be plotted. By default all spectra will be plotted. - aspect (str, 'equal'): the aspect ratio of the axes. - flip_spectrum (bool, False): by default (False) the x axis is w_3. If True, it will be w_1. Make sure the axes ranges are changed as well. OUTPUT: - - DESCRIPTION: - - CHANGELOG: 2016014/RB: started function """ pi, bish, sp, ds, sm, de, du, sc = self.multiplot_ranges(**kwargs) n_plots = len(sp) * len(sm) * len(de) * len(du) * len(sc) x, y = FU.find_subplots(n_plots, flag_verbose = self.flag_verbose) fig = plt.figure() ax = [0] * n_plots for ax_i in range(n_plots): ax[ax_i] = fig.add_subplot(y, x, ax_i + 1) if "aspect" in kwargs: if kwargs["aspect"] != False: ax[ax_i].set_aspect(kwargs["aspect"]) else: ax[ax_i].set_aspect("equal") ax_i = 0 for _sp in sp: for _sm in sm: for _de in de: for _du in du: for _sc in sc: title = "{name}\nsp {spx}, sm {smx}, de {dex} fs".format(name = self._basename, spx = self.s_axes[3][_sp], smx = "x",#self.s_axes[4][:,_sm], dex = self.s_axes[5][_de]) if "flip_spectrum" in kwargs and kwargs["flip_spectrum"]: PL.contourplot(self.s[:, :, 0, _sp, _sm, _de, _du, _sc], self.s_axes[1], self.s_axes[0], x_label = "w1 (cm-1)", y_label = "w3 (cm-1)", ax = ax[ax_i], title = title, **kwargs) else: PL.contourplot(self.s[:, :, 0, _sp, _sm, _de, _du, _sc].T, self.s_axes[0], self.s_axes[1], x_label = "w3 (cm-1)", y_label = "w1 (cm-1)", ax = ax[ax_i], title = title, **kwargs) ax_i += 1 return fig
def make_Z_table(self, x_range = [0,0], y_range = [0,-1], **kwargs): """ INPUT: - - OUTPUT: - - DESCRIPTION: - - CHANGELOG: 2016014/RB: started function """ print("sp sm de sc min max delta") # determine the range to be plotted x_min, x_max, y_min, y_max = FU.find_axes(self.s_axes[0], self.s_axes[1], x_range = x_range, y_range = y_range, flag_verbose = self.flag_verbose) # print(x_min, x_max, y_min, y_max) # find the area to be plotted x_min_i, x_max_i = FU.find_axes_indices(self.s_axes[0], x_min, x_max) y_min_i, y_max_i = FU.find_axes_indices(self.s_axes[1], y_min, y_max) # print(x_min_i, x_max_i, y_min_i, y_max_i) for _sp in range(self.s_n[2]): for _sm in range(self.s_n[4]): for _de in range(self.s_n[5]): for _sc in range(self.s_n[7]): data, x_axis, y_axis = FU.truncate_data(self.s[:,:,_sp,0,_sm,_de,0,_sc].T, self.s_axes[0], self.s_axes[1], x_min_i, x_max_i, y_min_i, y_max_i) # print(data) a = numpy.amin(data) b = numpy.amax(data) print(" {sp} {sm} {de} {sc} {min:5.1f} {max:5.1f} {delta:5.1f}".format(sp = _sp, sm = _sm, de = _de, sc = _sc, min = a, max = b, delta = (b - a)))
def test(self): par = [ [0,0,0], [1,1,1], [2,2,1], [3,2,2], [4,2,2], [5,3,2], [6,3,2], [7,3,3], [8,3,3], [9,3,3], [10,4,3], [11,4,3], [12,4,3], [13,4,4], [14,4,4], [15,4,4], [16,4,4], [17,5,4], [18,5,4], [19,5,4], [20,5,4], [21,5,5], [22,5,5], [23,5,5], [24,5,5], [25,5,5], ] for p in par: x, y = FU.find_subplots(p[0], flag_verbose = self.flag_verbose) print(p[0], x, y) self.assertEqual(x, p[1]) self.assertEqual(y, p[2])
def plot_overlap( ma, ax, la = [], x_range = [0,0], y_range = [0,-1], zlimit = -1, contours = 12, colors = ["b", "r"], x_label = "", y_label = "", title = "", diagonal_line = True, invert_colors = False, ma_linewidth = 1, la_linewidth = 2, flag_verbose = False): DEBUG.verbose("contour plot", flag_verbose) for i in range(2): # check for correct lengths y, x = numpy.shape(ma[i].s) if len(ma[i].s_axis[2]) != x and len(ma[i].s_axis[0]) != y: DEBUG.printError("The data should have the same shape as the axes, wrong for both axes", inspect.stack()) return False elif len(ma[i].s_axis[2]) != x: DEBUG.printError("The data should have the same shape as the axes, wrong for the x-axis", inspect.stack()) return False elif len(ma[i].s_axis[0]) != y: DEBUG.printError("The data should have the same shape as the axes, wrong for the y-axis", inspect.stack()) return False # invert colors if invert_colors: ma[i].s = ma[i].s x_axis = ma[0].s_axis[2] y_axis = ma[0].s_axis[0] # determine the range to be plotted x_min, x_max, y_min, y_max = FU.find_axes(x_axis, y_axis, x_range, y_range, flag_verbose) # find the area to be plotted x_min_i, x_max_i= FU.find_axes_indices(x_axis, x_min, x_max) y_min_i, y_max_i= FU.find_axes_indices(y_axis, y_min, y_max) x_axis = x_axis[x_min_i:x_max_i] y_axis = y_axis[y_min_i:y_max_i] for i in range(2): # truncate the data, this speeds up the plotting data = ma[i].s[y_min_i:y_max_i,x_min_i:x_max_i] # now make the actual contours V = FU.make_contours_2d(data, zlimit, contours, flag_verbose) # actually plot the thing ax.contour(x_axis, y_axis, data, V, linewidths = ma_linewidth, colors = colors[i]) if len(la) == 4: ax.plot(la[0],la[1], c = colors[0], lw = la_linewidth) ax.plot(la[2],la[3], c = colors[1], lw = la_linewidth) # the diagonal line if diagonal_line: ax.plot([x_axis[0]-100,x_axis[-1]+100], [x_axis[0]-100,x_axis[-1]+100], "k", linewidth = ma_linewidth) # we only want to see a certain part of the spectrum ax.set_xlim(x_min, x_max) ax.set_ylim(y_min, y_max) # add some text if x_label != "" and x_label != "no_label": ax.set_xlabel(x_label) if y_label != "" and y_label != "no_label": ax.set_ylabel(y_label) if title != "": ax.set_title(title) return True
def function(self, x_axis, y_axis, param): return FU.find_axes(x_axis, y_axis, param[0], param[1], flag_verbose = self.flag_verbose)
def contourplot(data, x_axis, y_axis, # ax = False, # x_range = [0,0], # y_range = [0,-1], # zlimit = -1, # contours = 12, # filled = True, # black_contour = True, # x_label = "", # y_label = "", # title = "", # diagonal_line = True, # invert_colors = False, # linewidth = 1, # flag_verbose = False, **kwargs): """ Make a contourplot. The defaults are for a plot with w3 as the x-axis. - data, x_axis, y_axis: data and axes - ax (bool (False) or matplotlib axes instance): if False, it will make a new figure, otherwise it will use the axes instance, allowing subplots etc. - x_label, y_label, title (string, default=''): the labels for the axes. If no label is set, it will use the default. Use 'no_label' or 'no_title' to show no label. - x_range, y_range (array with 2 elements, [0,0], [0,-1]): the range to be plotted. Possible cases: - [min, max]: plot range min to max - [0, 0]: plot the whole range - [0, -1]: use the range from the other axis. If both have this, it will plot both axes complete. (ie. it is identical to both having [0,0]) - zlimit (number or list, -1): the z-range that will be used Possible cases: zlimit = 0, show all, not don't care about centering around zero zlimit = -1, show all, centered around zero zlimit = all else, use that, centered around zero zlimit = [a,b], plot from a to b - contours (int, 16): number of contours to be used - invert_colors (BOOL, False): data = -data CHANGELOG: 201108xx/RB: started function 20130213/RB: moved some things out as separate functions 20160418/RB: replaced arguments with kwargs """ if "flag_verbose" in kwargs: flag_verbose = kwargs["flag_verbose"] else: flag_verbose = False DEBUG.verbose("contour plot", flag_verbose) y, x = numpy.shape(data) if len(x_axis) != x and len(y_axis) != y: DEBUG.printError("The data should have the same shape as the axes, wrong for both axes", inspect.stack()) return False elif len(x_axis) != x: DEBUG.printError("The data should have the same shape as the axes, wrong for the x-axis", inspect.stack()) return False elif len(y_axis) != y: DEBUG.printError("The data should have the same shape as the axes, wrong for the y-axis", inspect.stack()) return False if "invert_colors" in kwargs and kwargs["invert_colors"]: data = -data if "x_range" in kwargs: x_range = kwargs["x_range"] else: x_range = [0,0] if "y_range" in kwargs: y_range = kwargs["y_range"] else: y_range = [0,-1] # determine the range to be plotted x_min, x_max, y_min, y_max = FU.find_axes(x_axis, y_axis, x_range, y_range, flag_verbose) # find the area to be plotted x_min_i, x_max_i = FU.find_axes_indices(x_axis, x_min, x_max) y_min_i, y_max_i = FU.find_axes_indices(y_axis, y_min, y_max) # truncate the data, this speeds up the plotting data, x_axis, y_axis = FU.truncate_data(data, x_axis, y_axis, x_min_i, x_max_i, y_min_i, y_max_i) if "zlimit" in kwargs: zlimit = kwargs["zlimit"] else: zlimit = -1 if "contours" in kwargs: contours = kwargs["contours"] else: contours = 16 # now make the actual contours V = FU.make_contours_2d(data, zlimit, contours, flag_verbose) # make sure there is an axis-object if "ax" in kwargs: ax = kwargs["ax"] flag_show = False else: fig = plt.figure() ax = fig.add_subplot(111) flag_show = True # else: # flag_show = False if "linewidth" in kwargs: linewidth = kwargs["linewidth"] else: linewidth = 1 # actually plot the thing if "filled" in kwargs and kwargs["filled"] == False: pass else: ax.contourf(x_axis, y_axis, data, V, cmap = FU.rwb_cmap) if "black_contour" in kwargs and kwargs["black_contour"] == False: pass else: if "filled" in kwargs and kwargs["filled"] == False: ax.contour(x_axis, y_axis, data, V, linewidths = linewidth, colors = "k") else: ax.contour(x_axis, y_axis, data, V, linewidths = linewidth, linestyles = "solid", colors = "k") # the diagonal line if "diagonal_line" in kwargs and kwargs["diagonal_line"] == False: pass else: ax.plot([x_axis[0]-100,x_axis[-1]+100], [x_axis[0]-100,x_axis[-1]+100], "k", linewidth = linewidth) # we only want to see a certain part of the spectrum ax.set_xlim(x_min, x_max) ax.set_ylim(y_min, y_max) # add some text if "x_label" in kwargs and kwargs["x_label"] != "" and kwargs["x_label"] != "no_label": ax.set_xlabel(kwargs["x_label"] ) if "y_label" in kwargs and kwargs["y_label"] != "" and kwargs["y_label"] != "no_label": ax.set_ylabel(kwargs["y_label"]) if "title" in kwargs and kwargs["title"] != "": ax.set_title(kwargs["title"]) if flag_show: plt.show() return True
def test_upper_edge_2(self): self.axis = numpy.linspace(20,30,11) val_min = 22.5 val_max = 27.0 res = FU.find_axes_indices(self.axis, val_min, val_max, flag_verbose = self.flag_verbose) self.assertEqual(res, (2,9))
def test_lower_edge_over(self): val_min = 1.0 val_max = 27.2 res = FU.find_axes_indices(self.axis, val_min, val_max, flag_verbose = self.flag_verbose) self.assertEqual(res, (0,9))
def test_lower_edge_3(self): self.axis = numpy.linspace(20,30,11) val_min = 22.0 val_max = 27.5 res = FU.find_axes_indices(self.axis, val_min, val_max, flag_verbose = self.flag_verbose) self.assertEqual(res, (1,9))
def test_lower_edge_2(self): val_min = 22.7 val_max = 27.0 res = FU.find_axes_indices(self.axis, val_min, val_max, flag_verbose = self.flag_verbose) self.assertEqual(res, (2,9))
def test_upper_edge_over(self): val_min = 22.0 val_max = 37.3 res = FU.find_axes_indices(self.axis, val_min, val_max, flag_verbose = self.flag_verbose) self.assertEqual(res, (2,-1))