def _register_cmap_clip(name, original_cmap, alpha): """Create a color map with "over" and "under" values.""" from matplotlib.colors import LinearSegmentedColormap cdict = plt.cm.datad[original_cmap] cmap = LinearSegmentedColormap(name, cdict) cmap.set_over([alpha * c + 1 - alpha for c in cmap(1.0)[:3]]) cmap.set_under([alpha * c + 1 - alpha for c in cmap(0.0)[:3]]) plt.cm.register_cmap(cmap=cmap)
def _register_cmap_clip(name, original_cmap, alpha): """Create a color map with "over" and "under" values.""" from matplotlib.colors import LinearSegmentedColormap cdata = _plt.cm.datad[original_cmap] if isinstance(cdata, dict): cmap = LinearSegmentedColormap(name, cdata) else: cmap = LinearSegmentedColormap.from_list(name, cdata) cmap.set_over([alpha * c + 1 - alpha for c in cmap(1.0)[:3]]) cmap.set_under([alpha * c + 1 - alpha for c in cmap(0.0)[:3]]) _plt.cm.register_cmap(cmap=cmap)
def _register_cmap_clip(name, original_cmap, alpha): """Create a color map with "over" and "under" values.""" from matplotlib.colors import LinearSegmentedColormap cdata = _plt.cm.datad[original_cmap] if isinstance(cdata, dict): cmap = LinearSegmentedColormap(name, cdata) else: cmap = LinearSegmentedColormap.from_list(name, cdata) cmap.set_over([alpha * c + 1 - alpha for c in cmap(1.0)[:3]]) cmap.set_under([alpha * c + 1 - alpha for c in cmap(0.0)[:3]]) _plt.cm.register_cmap(cmap=cmap)
def build_cmap(stops): # {{{ from matplotlib.colors import LinearSegmentedColormap ct = dict(red=[], blue=[], green=[]) for s in stops: if 'r2' in s: ct['red'].append((s['s'], s['r'], s['r2'])) ct['green'].append((s['s'], s['g'], s['g2'])) ct['blue'].append((s['s'], s['b'], s['b2'])) else: ct['red'].append((s['s'], s['r'], s['r'])) ct['green'].append((s['s'], s['g'], s['g'])) ct['blue'].append((s['s'], s['b'], s['b'])) cm = LinearSegmentedColormap('loaded_colourmap', ct, 256) cm.set_under((stops[ 0]['r'], stops[ 0]['g'], stops[ 0]['b'])) cm.set_over ((stops[-1]['r'], stops[-1]['g'], stops[-1]['b'])) return cm
def get_ctable(self, under=None, over=None): """カラーマップを作成する Parameters: ---------- under: str 下限を下回った場合の色 over: str 上限を上回った場合の色 ---------- Returns: ---------- cmap カラーマップ ---------- """ # カラーマップ作成 cmap = LinearSegmentedColormap('colormap_name', self._segment_data) if under is not None: cmap.set_under(under) # 下限を下回った場合の色を指定 if over is not None: cmap.set_over(over) # 上限を超えた場合の色を指定 return cmap
color_dict = {'red': ((0.00, 0.10, 0.10), (0.33, 0.10, 0.10), (0.67, 1.00, 1.00), (1.00, 1.00, 1.00)), 'green': ((0.00, 0.10, 0.10), (0.33, 0.10, 0.10), (0.67, 0.50, 0.50), (1.00, 1.00, 1.00)), 'blue': ((0.00, 0.10, 0.10), (0.33, 0.50, 0.50), (0.67, 0.10, 0.10), (1.00, 1.00, 1.00)) } amber_teal = LinearSegmentedColormap('OrangeTeal1', color_dict) amber_teal.set_under('#191919') amber_teal.set_over('#FFFFFF') colormap = 'RdPu' # Constrain the colorbar P_min = np.min( P[P>0.].cgs.value ) # Minimum (non-empty) value of sigma P_max = np.max( P.cgs.value ) # Maximum value of sigma #P_min = 10**(np.ceil( np.log10( np.max( P.cgs.value ) ) ) - 4.0 ) levels = np.linspace( np.floor(np.log10(P_min)), np.ceil(np.log10(P_max)), 100 ) # Levels of colorbar cbar_ticks = np.arange( np.floor(np.log10(P_min)), np.ceil(np.log10(P_max))+0.1, 0.5 ) # Ticks of colorbar # Create plot fig, ax = plt.subplots(subplot_kw=dict(projection='polar')) # Use polar coordinate system plt.subplots_adjust(bottom=0.25) # Create space at the bottom for the slider plot = ax.contourf(theta_cen, R_cen.to(u.AU), np.log10(P[ :, :].cgs.value), cmap=colormap, levels=levels, extend='both' ) # Filled contour plot collections_list = plot.collections[:] # Collect the collections....lol
def _shiftedColorMap(self, cmap, start=0, midpoint=0.5, stop=1.0, name='shiftedcmap'): ''' Taken from https://github.com/olgabot/prettyplotlib/blob/master/prettyplotlib/colors.py which makes beautiful plots by the way Function to offset the "center" of a colormap. Useful for data with a negative min and positive max and you want the middle of the colormap's dynamic range to be at zero Input ----- cmap : The matplotlib colormap to be altered start : Offset from lowest point in the colormap's range. Defaults to 0.0 (no lower ofset). Should be between 0.0 and `midpoint`. midpoint : The new center of the colormap. Defaults to 0.5 (no shift). Should be between 0.0 and 1.0. In general, this should be 1 - vmax/(vmax + abs(vmin)) For example if your data range from -15.0 to +5.0 and you want the center of the colormap at 0.0, `midpoint` should be set to 1 - 5/(5 + 15)) or 0.75 stop : Offset from highets point in the colormap's range. Defaults to 1.0 (no upper ofset). Should be between `midpoint` and 1.0. ''' from matplotlib.colors import LinearSegmentedColormap from matplotlib import cm cdict = {'red': [], 'green': [], 'blue': [], 'alpha': []} # regular index to compute the colors reg_index = np.linspace(start, stop, 257) # shifted index to match the data shift_index = np.hstack([ np.linspace(0.0, midpoint, 128, endpoint=False), np.linspace(midpoint, 1.0, 129, endpoint=True) ]) for ri, si in zip(reg_index, shift_index): r, g, b, a = cmap(ri) cdict['red'].append((si, r, r)) cdict['green'].append((si, g, g)) cdict['blue'].append((si, b, b)) cdict['alpha'].append((si, a, a)) newcmap = LinearSegmentedColormap(name, cdict) # add some overunders newcmap.set_bad(color='g', alpha=0.75) newcmap.set_over(color='m', alpha=0.75) newcmap.set_under(color='c', alpha=0.75) cm.register_cmap(cmap=newcmap) return newcmap
def test_problem_16(self): """ Schittkowski problem #16 """ cost = Problem16_Cost() problem = roboptim.core.PyProblem(cost) problem.startingPoint = numpy.array([ -2, 1., ]) problem.argumentBounds = numpy.array([[-2., 0.5], [-float("inf"), 1.]]) g1 = Problem16_G1() problem.addConstraint(g1, [ 0., float("inf"), ]) g2 = Problem16_G2() problem.addConstraint(g2, [ 0., float("inf"), ]) # Check starting value numpy.testing.assert_almost_equal(cost(problem.startingPoint)[0], 909.) # Initialize callback callback = IterCallback(problem) # Let the test fail if the solver does not exist. try: # Create solver log_dir = "/tmp/roboptim-core-python/problem_16" solver = roboptim.core.PySolver("ipopt", problem, log_dir=log_dir) # Add callback solver.addIterationCallback(callback) print(solver) solver.solve() r = solver.minimum() print(r) # Plot results plotter = Plotter2D([-2.1, 0.6], [0, 1.1]) plotter.x_res = 100 plotter.y_res = 100 plotter.plot(cost, plot_style=PlotStyle2D.PColorMesh, vmax=10, norm=LogNorm()) # Set up a colormap: cdict = { 'red': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)), 'green': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)), 'blue': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)), 'alpha': ((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)) } cstr_cmap = LinearSegmentedColormap('Mask', cdict) cstr_cmap.set_under('r', alpha=0) cstr_cmap.set_over('w', alpha=0) cstr_cmap.set_bad('g', alpha=0) plotter.plot(g1, plot_style=PlotStyle2D.Contourf, linewidth=10, alpha=None, cmap=cstr_cmap, vmax=0, fontsize=20) plotter.plot(g1, plot_style=PlotStyle2D.Contour, linewidth=10, alpha=None, levels=[0], vmax=0, fontsize=20, colors="k") plotter.plot(g2, plot_style=PlotStyle2D.Contourf, linewidth=10, alpha=None, cmap=cstr_cmap, vmax=0) # Print iterations X = zip(*callback.x)[0] Y = zip(*callback.x)[1] # Show evolution plotter.add_marker(X, Y, color="white", marker=".", markersize=5) # First point plotter.add_marker(X[0], Y[0], color="white", marker="o", markersize=10, markeredgewidth=2) # Final result plotter.add_marker(X[-1], Y[-1], color="white", marker="s", markersize=10, markeredgewidth=2) # Print actual global minimum plotter.add_marker(0.5, 0.25, color="black", marker="x", markersize=14, markeredgewidth=6) plotter.add_marker(0.5, 0.25, color="white", marker="x", markersize=10, markeredgewidth=3) plotter.show() except Exception as e: print("Error: %s" % e)
(0.968627450980392,0.78075390625,0.78075390625), (0.972549019607843,0.79254296875,0.79254296875), (0.976470588235294,0.80433203125,0.80433203125), (0.980392156862745,0.81612109375,0.81612109375), (0.984313725490196,0.82791015625,0.82791015625), (0.988235294117647,0.839703125,0.839703125), (0.992156862745098,0.8514921875,0.8514921875), (0.996078431372549,0.86328125,0.86328125), (1,0,0)), } califa = LinearSegmentedColormap('CALIFA', cdict) califa.set_bad(color='navy') califa.set_under(color='navy') califa.set_over(color='navy') register_cmap(cmap=califa) #plt.xkcd() hdus = fits.open('NGC4047.p_e.rad_SFR_lum_Mass.fits.gz') img = hdus[0].data fig=plt.figure() ax1 = fig.add_subplot(111) #img_mask= np.power(10, img) img_masked= img where_are_NaNs = np.isnan(img_masked) img_masked[where_are_NaNs] = 0 ticksy=np.linspace(0,2,9)
for pos, l, c, h in zip(np.linspace(0, 1, nsteps), l_vals, c_vals, h_vals): lch = [l, c, h] rgb = lch2rgb(lch) reds.append((pos, rgb[0], rgb[0])) greens.append((pos, rgb[1], rgb[1])) blues.append((pos, rgb[2], rgb[2])) alphas.append((pos, 1.0, 1.0)) red_blue = LinearSegmentedColormap('red_blue', { "red": reds, "green": greens, "blue": blues, "alpha": alphas }) red_blue.set_bad(gray_rgb, 1.0) red_blue.set_over(gray_rgb, 1.0) red_blue.set_under( gray_rgb, 1.0 ) # "under" is incorrectly used instead of "bad" in the scatter plot red_blue_no_bounds = LinearSegmentedColormap('red_blue_no_bounds', { "red": reds, "green": greens, "blue": blues, "alpha": alphas }) red_blue_transparent = LinearSegmentedColormap( 'red_blue_no_bounds', { "red": reds, "green": greens,
color_dict = {'red': ((0.00, 0.10, 0.10), (0.33, 0.10, 0.10), (0.67, 1.00, 1.00), (1.00, 1.00, 1.00)), 'green': ((0.00, 0.10, 0.10), (0.33, 0.10, 0.10), (0.67, 0.50, 0.50), (1.00, 1.00, 1.00)), 'blue': ((0.00, 0.10, 0.10), (0.33, 0.50, 0.50), (0.67, 0.10, 0.10), (1.00, 1.00, 1.00)) } amber_teal = LinearSegmentedColormap('OrangeTeal1', color_dict) amber_teal.set_under('#191919') amber_teal.set_over('#FFFFFF') colormap = 'jet' # Constrain the colorbar P_min = np.min( P[P>0.].cgs.value ) # Minimum (non-empty) value of sigma P_max = np.max( P.cgs.value ) # Maximum value of sigma levels = np.linspace( np.floor(np.log10(P_min)), np.ceil(np.log10(P_max)), 100 ) # Levels of colorbar cbar_ticks = np.arange( np.floor(np.log10(P_min)), np.ceil(np.log10(P_max))+0.1, 0.5 ) # Ticks of colorbar # Create plot fig, ax = plt.subplots(subplot_kw=dict(projection='polar')) # Use polar coordinate system plt.subplots_adjust(bottom=0.25) # Create space at the bottom for the slider plot = ax.contourf(theta_cen, R_cen.to(u.AU), np.log10(P[ :, :].cgs.value), cmap=colormap, levels=levels, extend='both' ) # Filled contour plot collections_list = plot.collections[:] # Collect the collections....lol #planet, = ax.plot(planet_theta[i_image], planet_R[i_image].to(u.AU), color='cyan', marker='o', markersize=8, markeredgecolor='black') # Cross at planet position
def test_problem_16(self): """ Schittkowski problem #16 """ cost = Problem16_Cost () problem = roboptim.core.PyProblem (cost) problem.startingPoint = numpy.array([-2, 1., ]) problem.argumentBounds = numpy.array([[-2., 0.5], [-float("inf"), 1.]]) g1 = Problem16_G1 () problem.addConstraint (g1, [0., float("inf"),]) g2 = Problem16_G2 () problem.addConstraint (g2, [0., float("inf"),]) # Check starting value numpy.testing.assert_almost_equal (cost (problem.startingPoint)[0], 909.) # Initialize callback callback = IterCallback (problem) # Let the test fail if the solver does not exist. try: # Create solver log_dir = "/tmp/roboptim-core-python/problem_16" solver = roboptim.core.PySolver ("ipopt", problem, log_dir = log_dir) # Add callback solver.addIterationCallback(callback) print (solver) solver.solve () r = solver.minimum () print (r) # Plot results plotter = Plotter2D([-2.1,0.6],[0,1.1]) plotter.x_res = 100 plotter.y_res = 100 plotter.plot(cost, plot_style = PlotStyle2D.PColorMesh, vmax=10, norm=LogNorm()) # Set up a colormap: cdict = {'red': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)), 'green': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)), 'blue': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)), 'alpha': ((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)) } cstr_cmap = LinearSegmentedColormap('Mask', cdict) cstr_cmap.set_under('r', alpha=0) cstr_cmap.set_over('w', alpha=0) cstr_cmap.set_bad('g', alpha=0) plotter.plot(g1, plot_style=PlotStyle2D.Contourf, linewidth=10, alpha=None, cmap=cstr_cmap, vmax=0, fontsize=20) plotter.plot(g1, plot_style=PlotStyle2D.Contour, linewidth=10, alpha=None, levels=[0], vmax=0, fontsize=20, colors="k") plotter.plot(g2, plot_style=PlotStyle2D.Contourf, linewidth=10, alpha=None, cmap=cstr_cmap, vmax=0) # Print iterations X = zip(*callback.x)[0] Y = zip(*callback.x)[1] # Show evolution plotter.add_marker(X, Y, color="white", marker=".", markersize=5) # First point plotter.add_marker(X[0], Y[0], color="white", marker="o", markersize=10, markeredgewidth=2) # Final result plotter.add_marker(X[-1], Y[-1], color="white", marker="s", markersize=10, markeredgewidth=2) # Print actual global minimum plotter.add_marker(0.5, 0.25, color="black", marker="x", markersize=14, markeredgewidth=6) plotter.add_marker(0.5, 0.25, color="white", marker="x", markersize=10, markeredgewidth=3) plotter.show() except Exception as e: print ("Error: %s" % e)
def create_cmap(start, end, mid=None, N=256, extend='neither', over=None, under=None, normalize=None): """ Function that creates matplotlib colormaps. Note that input colors must be RGB tuples within [0,1]. Parameters ---------- start : 3-tuple RGB-Tuple defining the start color of the color map. end : 3-tuple RGB-Tuple defining the end color of the color map. mid : list (optional, default = None) List of tuples (pos, col) specifying any colors in between start and end. In this context, 0. < pos < 1. specifiies the position of col (passed as RGB tuple) within the color map range. N : int (optional, default = 256) The number of rgb quantization levels. extend : string (optional, one of {*'neither'*, 'both', 'max', 'min'}) Extend the colorbar and indicate this by triangles at the respective ends. See eg. here: http://matplotlib.org/examples/pylab_examples/contourf_demo.html over : 3-tuple (optional, default = None) Color definition for everything out of the upper range of the colormap. This is only meaningful if you set the ``extend`` option correctly. Uses ``end`` by default. under : 3-tuple (optional, default = None) Same as ``over`` but for the lower limit. Uses ``start`` by default. Returns ------- cmap : LinearSegmentedColormap instance color map, ready to be used with matplotlib. Examples -------- If you want a colormap ranging from tumblue over white to tumorange, you call this function via >>> cmap = create_cmap(start = tumcolors['tumblue'], end = tumcolors['tumorange'], mid = [(0.5, tumcolors['white'])] ) You can also go straight from tumblue to tumorange: >>> cmap = create_cmap(start = tumcolors['tumblue'], end = tumcolors['tumorange']) Or you can add several colors in between: >>> cmap = create_cmap(start = tumcolors['tumblue'], end = tumcolors['tumorange'], mid = [(0.3, tumcolors['white']), (0.7, tumcolors['black'])] ) """ from matplotlib.colors import LinearSegmentedColormap cdict = dict() for i, channel in enumerate(['red', 'green', 'blue']): cdict_content = [[0.0, start[i], start[i]]] if mid is not None: try: for pos, col in mid: cdict_content.append([pos, col[i], col[i]]) except TypeError: pos, col = mid cdict_content.append([pos, col[i], col[i]]) cdict_content.append([1.0, end[i], end[i]]) cdict[channel] = cdict_content cmap = LinearSegmentedColormap('custom_cmap', cdict, N) # extend cmap.colorbar_extend = extend if under is None: under = start if over is None: over = end cmap.set_over(over) cmap.set_under(under) return cmap
'alpha': ((0.0, 1, 1), (0.5, 0.3, 0.3), (1.0, 1, 1)) }) #red_blue.set_bad("#777777") red_blue_solid = LinearSegmentedColormap( 'red_blue_solid', { 'red': ((0.0, 30. / 255, 30. / 255), (1.0, 255. / 255, 255. / 255)), 'green': ((0.0, 136. / 255, 136. / 255), (1.0, 13. / 255, 13. / 255)), 'blue': ((0.0, 229. / 255, 229. / 255), (1.0, 87. / 255, 87. / 255)), 'alpha': ((0.0, 1, 1), (0.5, 1, 1), (1.0, 1, 1)) }) red_blue_solid.set_bad("#777777", 1.0) red_blue_solid.set_over("#777777", 1.0) red_blue_solid.set_under( "#777777", 1.0 ) # "under" is incorrectly used instead of "bad" in the scatter plot colors = [] for l in np.linspace(1, 0, 100): colors.append((30. / 255, 136. / 255, 229. / 255, l)) for l in np.linspace(0, 1, 100): colors.append((255. / 255, 13. / 255, 87. / 255, l)) red_transparent_blue = LinearSegmentedColormap.from_list( "red_transparent_blue", colors) colors = [] for l in np.linspace(0, 1, 100): colors.append((30. / 255, 136. / 255, 229. / 255, l))
def generate_plot(self): # Get label label = self.generate_label() # Check all inputs if len(self.master.master.input_keys) == 0: messagebox.showerror( "No Input Data Sets", "No input data sets were found. Either there is no database " + "selected, or the selected database has no valid data. " + "You should really do something about that.") arr_name = self.arr_name.combox_input.get() if self.include_ll: ll_name = self.ll_name.combox_input.get() else: ll_name = None if arr_name == "" or ll_name == "": messagebox.showerror( "Missing Input", "Make sure all enabled output plots have a selected input.") # Get data from database analysis_db = AnalysisDatabase(self.db_path) z = analysis_db.get_val(self.input_key, arr_name) if z.shape[1] == 2: messagebox.showwarning( "Data Looks Weird", "The shape of this data makes it look like you may have " + "selected a histogram. Are you sure you didn't?") if self.include_ll: leak = analysis_db.get_val(self.input_key, ll_name) # Get noise threshold for heatmap by sampling outer edge of data noise_sample = np.concatenate( (z[0, :], z[-1, :], z[:, 0][1:-1], z[:, -1][1:-1])) noise_threshold = np.average(noise_sample) + 6 * np.std(noise_sample) # Setup heatmap x = np.arange(np.shape(z)[1]) y = np.arange(np.shape(z)[0]) z = z[:-1, :-1] if self.title == "Dominant Wavelength Heatmap": cmap = LinearSegmentedColormap('Spectrum', spec_dict_wl) max_val = 830 min_val = 380 cmap.set_under('white') cmap.set_over('gray') else: cmap = LinearSegmentedColormap('Rainbow', spec_dict_rb) max_val = np.max(z) min_val = noise_threshold plt.register_cmap(cmap=cmap) fig, (ax0) = plt.subplots(nrows=1) im = ax0.pcolormesh(x, y, z, cmap=cmap, norm=Normalize(vmin=min_val, vmax=max_val)) cbar = fig.colorbar(im, ax=ax0) if self.title == "Luminance Heatmap" or \ self.title == "Light Leakage Heatmap": cbar.set_label('Luminance [cd/m^2]', rotation=270, labelpad=15) elif self.title == "Uniformity Heatmap": cbar.set_label('Uniformity [cd/m^2/mm]', rotation=270, labelpad=15) elif self.title == "Dominant Wavelength Heatmap": cbar.set_label('Wavelength [nm]', rotation=270, labelpad=15) ax0.set_title(self.title + " - " + label) if self.include_ll: plt.text(int(z.shape[1] / 10), int(z.shape[0] / 10), "Leakage Ratio: " + str(leak)) fig.tight_layout() if self.return_fig: return fig
def mds(MATRIX_FILE, DIMENSIONS, N_ITERATIONS, IMAGES_EVERY, STATION_BLOCK_SIZE, N_NEARBY_STATIONS, DEBUG_OUTPUT, STATUS_FUNCTION, GRAPH_FUNCTION) : print 'Loading matrix...' npz = np.load(MATRIX_FILE) station_coords = npz['station_coords'] grid_dim = npz['grid_dim'] matrix = npz['matrix'].astype(np.int32) # EVERYTHING SHOULD BE IN FLOAT32 for ease of debugging. even times. # Matrix and others should be textures, arrays, or in constant memory, to do cacheing. # As it is, I'm doing explicit cacheing. # force OD matrix symmetry for test # THIS was responsible for the coordinate drift!!! # need to symmetrize it before copy to device matrix = (matrix + matrix.T) / 2 station_coords_int = station_coords.round().astype(np.int32) # to be removed when textures are working station_coords_gpu = gpuarray.to_gpu(station_coords_int) matrix_gpu = gpuarray.to_gpu(matrix) max_x, max_y = grid_dim n_gridpoints = int(max_x * max_y) n_stations = len(station_coords) cuda_grid_shape = ( int( math.ceil( float(max_x)/CUDA_BLOCK_SHAPE[0] ) ), int( math.ceil( float(max_y)/CUDA_BLOCK_SHAPE[1] ) ) ) print "\n----PARAMETERS----" print "Input file: ", MATRIX_FILE print "Number of stations: ", n_stations print "OD matrix shape: ", matrix.shape print "Station coords shape: ", station_coords_int.shape print "Station cache size: ", N_NEARBY_STATIONS print "Map dimensions: ", grid_dim print "Number of map points: ", n_gridpoints print "Target space dimensionality: ", DIMENSIONS print "CUDA block dimensions: ", CUDA_BLOCK_SHAPE print "CUDA grid dimensions: ", cuda_grid_shape assert station_coords.shape == (n_stations, 2) assert N_NEARBY_STATIONS <= n_stations #sys.exit() # Make and register custom color map for pylab graphs cdict = {'red': ((0.0, 0.0, 0.0), (0.2, 0.0, 0.0), (0.4, 0.9, 0.9), (1.0, 0.0, 0.0)), 'green': ((0.0, 0.0, 0.1), (0.05, 0.9, 0.9), (0.1, 0.0, 0.0), (0.4, 0.9, 0.9), (0.6, 0.0, 0.0), (1.0, 0.0, 0.0)), 'blue': ((0.0, 0.0, 0.0), (0.05, 0.0, 0.0), (0.2, 0.9, 0.9), (0.3, 0.0, 0.0), (1.0, 0.0, 0.0))} mymap = LinearSegmentedColormap('mymap', cdict) mymap.set_over( (1.0, 0.0, 1.0) ) mymap.set_bad ( (0.0, 0.0, 0.0) ) pl.plt.register_cmap(cmap=mymap) # set up arrays for calculations coords_gpu = gpuarray.to_gpu(np.random.random( (max_x, max_y, DIMENSIONS) ).astype(np.float32)) # initialize coordinates to random values in range 0...1 forces_gpu = gpuarray.zeros( (int(max_x), int(max_y), DIMENSIONS), dtype=np.float32 ) # 3D float32 accumulate forces over one timestep weights_gpu = gpuarray.zeros( (int(max_x), int(max_y)), dtype=np.float32 ) # 2D float32 cell error accumulation errors_gpu = gpuarray.zeros( (int(max_x), int(max_y)), dtype=np.float32 ) # 2D float32 cell error accumulation near_stations_gpu = gpuarray.zeros( (cuda_grid_shape[0], cuda_grid_shape[1], N_NEARBY_STATIONS), dtype=np.int32) debug_gpu = gpuarray.zeros( n_gridpoints, dtype = np.int32 ) debug_img_gpu = gpuarray.zeros_like( errors_gpu ) print "\n----COMPILATION----" # times could be merged into forces kernel, if done by pixel not station. # integrate kernel could be GPUArray operation; also helps clean up code by using GPUArrays. # DIM should be replaced by python script, so as not to define twice. src = open("unified_mds.cu").read() src = src.replace( 'N_NEARBY_STATIONS_PYTHON', str(N_NEARBY_STATIONS) ) src = src.replace( 'N_STATIONS_PYTHON', str(n_stations) ) src = src.replace( 'DIMENSIONS_PYTHON', str(DIMENSIONS) ) mod = SourceModule(src, options=["--ptxas-options=-v"]) stations_kernel = mod.get_function("stations" ) forces_kernel = mod.get_function("forces" ) integrate_kernel = mod.get_function("integrate") matrix_texref = mod.get_texref('tex_matrix') station_coords_texref = mod.get_texref('tex_station_coords') near_stations_texref = mod.get_texref('tex_near_stations') #ts_coords_texref = mod.get_texref('tex_ts_coords') could be a 4-channel 2 dim texture, or 3 dim texture. or just 1D. cuda.matrix_to_texref(matrix, matrix_texref, order="F") # copy directly to device with texref - made for 2D x 1channel textures cuda.matrix_to_texref(station_coords_int, station_coords_texref, order="F") # fortran ordering, because we will be accessing with texND() instead of C-style indices near_stations_gpu.bind_to_texref_ext(near_stations_texref) # note, cuda.In and cuda.Out are from the perspective of the KERNEL not the host app! stations_kernel(near_stations_gpu, block=CUDA_BLOCK_SHAPE, grid=cuda_grid_shape) autoinit.context.synchronize() #print "Near stations list:" #print near_stations_gpu print "\n----CALCULATION----" t_start = time.time() n_pass = 0 while (n_pass < N_ITERATIONS) : n_pass += 1 # Pay attention to grid sizes when testing: if you don't run the integrator on the coordinates connected to stations, # they don't move... so the whole thing stabilizes in a couple of cycles. # Stations are worked on in blocks to avoid locking up the GPU with one giant kernel. for subset_low in range(0, n_stations, STATION_BLOCK_SIZE) : subset_high = subset_low + STATION_BLOCK_SIZE if subset_high > n_stations : subset_high = n_stations sys.stdout.write( "\rpass %03i / station %04i of %04i / total runtime %03.1f min " % (n_pass, subset_high, n_stations, (time.time() - t_start) / 60.0) ) sys.stdout.flush() STATUS_FUNCTION(n_pass, subset_high, n_stations, (time.time() - t_start) / 60.0, (time.time() - t_start) / n_pass + (subset_low/n_stations)) # adding texrefs in kernel call seems to change nothing, leaving them out. # max_x and max_y could be #defined in kernel source, along with STATION_BLOCK_SIZE forces_kernel(np.int32(n_stations), np.int32(subset_low), np.int32(subset_high), max_x, max_y, coords_gpu, forces_gpu, weights_gpu, errors_gpu, debug_gpu, debug_img_gpu, block=CUDA_BLOCK_SHAPE, grid=cuda_grid_shape) autoinit.context.synchronize() # print coords_gpu, forces_gpu time.sleep(0.5) # let the user interface catch up. integrate_kernel(max_x, max_y, coords_gpu, forces_gpu, weights_gpu, block=CUDA_BLOCK_SHAPE, grid=cuda_grid_shape) autoinit.context.synchronize() print IMAGES_EVERY if (IMAGES_EVERY > 0) and (n_pass % IMAGES_EVERY == 0) : #print 'Kernel debug output:' #print debug_gpu velocities = np.sqrt(np.sum(forces_gpu.get() ** 2, axis = 2)) pl.imshow( velocities.T, cmap=mymap, origin='bottom', vmin=0, vmax=100 ) pl.title( 'Velocity ( sec / timestep) - step %03d' % n_pass ) pl.colorbar() pl.savefig( 'img/vel%03d.png' % n_pass ) pl.close() pl.imshow( (errors_gpu.get() / weights_gpu.get() / 60.0 ).T, cmap=mymap, origin='bottom', vmin=0, vmax=100 ) pl.title( 'Average absolute error (min) - step %03d' %n_pass ) pl.colorbar() pl.savefig( 'img/err%03d.png' % n_pass ) pl.close() pl.imshow( (debug_img_gpu.get() / 60.0).T, cmap=mymap, origin='bottom', vmin=0, vmax=100 ) pl.title( 'Debugging Output - step %03d' %n_pass ) pl.colorbar() pl.savefig( 'img/debug%03d.png' % n_pass ) pl.close() GRAPH_FUNCTION('img/err%03d.png' % n_pass) #INTERFACE.update sys.stdout.write( "/ avg pass time %02.1f sec" % ( (time.time() - t_start) / n_pass, ) ) sys.stdout.flush()
def _shiftedColorMap(self, cmap, start=0, midpoint=0.5, stop=1.0, name='shiftedcmap'): ''' Taken from https://github.com/olgabot/prettyplotlib/blob/master/prettyplotlib/colors.py which makes beautiful plots by the way Function to offset the "center" of a colormap. Useful for data with a negative min and positive max and you want the middle of the colormap's dynamic range to be at zero Input ----- cmap : The matplotlib colormap to be altered start : Offset from lowest point in the colormap's range. Defaults to 0.0 (no lower ofset). Should be between 0.0 and `midpoint`. midpoint : The new center of the colormap. Defaults to 0.5 (no shift). Should be between 0.0 and 1.0. In general, this should be 1 - vmax/(vmax + abs(vmin)) For example if your data range from -15.0 to +5.0 and you want the center of the colormap at 0.0, `midpoint` should be set to 1 - 5/(5 + 15)) or 0.75 stop : Offset from highets point in the colormap's range. Defaults to 1.0 (no upper ofset). Should be between `midpoint` and 1.0. ''' import matplotlib.pyplot as plt from matplotlib.colors import LinearSegmentedColormap cdict = { 'red': [], 'green': [], 'blue': [], 'alpha': [] } # regular index to compute the colors reg_index = np.linspace(start, stop, 257) # shifted index to match the data shift_index = np.hstack([ np.linspace(0.0, midpoint, 128, endpoint=False), np.linspace(midpoint, 1.0, 129, endpoint=True) ]) for ri, si in zip(reg_index, shift_index): r, g, b, a = cmap(ri) cdict['red'].append((si, r, r)) cdict['green'].append((si, g, g)) cdict['blue'].append((si, b, b)) cdict['alpha'].append((si, a, a)) newcmap = LinearSegmentedColormap(name, cdict) # add some overunders newcmap.set_bad(color='g', alpha=0.75) newcmap.set_over(color='m', alpha=0.75) newcmap.set_under(color='c', alpha=0.75) plt.register_cmap(cmap=newcmap) return newcmap
def run (self) : cuda.init() self.cuda_dev = cuda.Device(0) self.cuda_context = self.cuda_dev.make_context() # print 'Loading matrix...' # npz = np.load(self.MATRIX_FILE) # station_coords = npz['station_coords'] # grid_dim = npz['grid_dim'] # matrix = npz['matrix'] station_coords = self.station_coords grid_dim = self.grid_dim matrix = self.matrix nearby_stations = self.nearby_stations # EVERYTHING SHOULD BE IN FLOAT32 for ease of debugging. even times. # Matrix and others should be textures, arrays, or in constant memory, to do cacheing. # make matrix symmetric before converting to int32. this avoids halving the pseudo-infinity value. matrix = (matrix + matrix.T) / 2 #print np.where(matrix == np.inf) matrix[matrix == np.inf] = 99999999 # nan == nan is False because any operation involving nan is False ! # must use specific isnan function. however, inf works like a normal number. matrix[np.isnan(matrix)] = 99999999 #matrix[matrix >= 60 * 60 * 3] = 0 matrix = matrix.astype(np.int32) #matrix += 60 * 5 #matrix = np.nan_to_num(matrix) print matrix # force OD matrix symmetry for test # THIS was responsible for the coordinate drift!!! # need to symmetrize it before copy to device # matrix = (matrix + matrix.T) / 2 # print matrix #print np.any(matrix == np.nan) #print np.any(matrix == np.inf) station_coords_int = station_coords.round().astype(np.int32) # to be removed when textures are working station_coords_gpu = gpuarray.to_gpu(station_coords_int) matrix_gpu = gpuarray.to_gpu(matrix) max_x, max_y = grid_dim n_gridpoints = int(max_x * max_y) n_stations = len(station_coords) cuda_grid_shape = ( int( math.ceil( float(max_x)/CUDA_BLOCK_SHAPE[0] ) ), int( math.ceil( float(max_y)/CUDA_BLOCK_SHAPE[1] ) ) ) print "\n----PARAMETERS----" print "Input file: ", self.MATRIX_FILE print "Number of stations: ", n_stations print "OD matrix shape: ", matrix.shape print "Station coords shape: ", station_coords_int.shape print "Station cache size: ", self.N_NEARBY_STATIONS print "Map dimensions: ", grid_dim print "Number of map points: ", n_gridpoints print "Target space dimensionality: ", self.DIMENSIONS print "CUDA block dimensions: ", CUDA_BLOCK_SHAPE print "CUDA grid dimensions: ", cuda_grid_shape assert station_coords.shape == (n_stations, 2) assert self.N_NEARBY_STATIONS <= n_stations #sys.exit() # Make and register custom color map for pylab graphs cdict = {'red': ((0.0, 0.0, 0.0), (0.2, 0.0, 0.0), (0.4, 0.9, 0.9), (1.0, 0.0, 0.0)), 'green': ((0.0, 0.0, 0.1), (0.05, 0.9, 0.9), (0.1, 0.0, 0.0), (0.4, 0.9, 0.9), (0.6, 0.0, 0.0), (1.0, 0.0, 0.0)), 'blue': ((0.0, 0.0, 0.0), (0.05, 0.0, 0.0), (0.2, 0.9, 0.9), (0.3, 0.0, 0.0), (1.0, 0.0, 0.0))} mymap = LinearSegmentedColormap('mymap', cdict) mymap.set_over( (1.0, 0.0, 1.0) ) mymap.set_bad ( (0.0, 0.0, 0.0) ) #pl.plt.register_cmap(cmap=mymap) # set up arrays for calculations coords_gpu = gpuarray.to_gpu(np.random.random( (max_x, max_y, self.DIMENSIONS) ).astype(np.float32)) # initialize coordinates to random values in range 0...1 forces_gpu = gpuarray.zeros( (int(max_x), int(max_y), self.DIMENSIONS), dtype=np.float32 ) # 3D float32 accumulate forces over one timestep weights_gpu = gpuarray.zeros( (int(max_x), int(max_y)), dtype=np.float32 ) # 2D float32 cell error accumulation errors_gpu = gpuarray.zeros( (int(max_x), int(max_y)), dtype=np.float32 ) # 2D float32 cell error accumulation #near_stations_gpu = gpuarray.zeros( (int(max_x), int(max_y), self.N_NEARBY_STATIONS, 2), dtype=np.int32) # instead of using synthetic distances, use the network distance near stations lists. # rather than copying the array over to the GPU then binding to external texref, # could just use matrix_to_texref, but this function only seems to understand 2d arrays near_stations_gpu = gpuarray.to_gpu( nearby_stations ) debug_gpu = gpuarray.zeros( n_gridpoints, dtype = np.int32 ) debug_img_gpu = gpuarray.zeros_like( errors_gpu ) print "\n----COMPILATION----" # times could be merged into forces kernel, if done by pixel not station. # integrate kernel could be GPUArray operation; also helps clean up code by using GPUArrays. # DIM should be replaced by python script, so as not to define twice. replacements = [( 'N_NEAR_STATIONS', self.N_NEARBY_STATIONS ), ( 'N_STATIONS', n_stations ), ( 'DIM', self.DIMENSIONS)] src = preprocess_cu('unified_mds_stochastic.cu', replacements) #print src mod = SourceModule(src, options=["--ptxas-options=-v"]) stations_kernel = mod.get_function("stations" ) forces_kernel = mod.get_function("forces" ) integrate_kernel = mod.get_function("integrate") matrix_texref = mod.get_texref('tex_matrix') station_coords_texref = mod.get_texref('tex_station_coords') near_stations_texref = mod.get_texref('tex_near_stations') #ts_coords_texref = mod.get_texref('tex_ts_coords') could be a 4-channel 2 dim texture, or 3 dim texture. or just 1D. cuda.matrix_to_texref(matrix, matrix_texref, order="F") # copy directly to device with texref - made for 2D x 1channel textures cuda.matrix_to_texref(station_coords_int, station_coords_texref, order="F") # fortran ordering, because we will be accessing with texND() instead of C-style indices # again, matrix_to_texref is not used here because that function only understands 2D arrays near_stations_gpu.bind_to_texref_ext(near_stations_texref) # note, cuda.In and cuda.Out are from the perspective of the KERNEL not the host app! # stations_kernel disabled since true network distances are now being used #stations_kernel(near_stations_gpu, max_x, max_y, block=CUDA_BLOCK_SHAPE, grid=cuda_grid_shape) # autoinit.context.synchronize() #self.cuda_context.synchronize() #print "Near stations list:" #print near_stations_gpu #sys.exit() print "\n----CALCULATION----" t_start = time.time() n_pass = 0 active_cells = list(self.active_cells) # make a working list of map cells that are accessible print "N active cells:", len(active_cells) while (n_pass < self.N_ITERATIONS) : n_pass += 1 random.shuffle(active_cells) active_cells_gpu = gpuarray.to_gpu(np.array(active_cells).astype(np.int32)) # Pay attention to grid sizes when testing: if you don't run the integrator on the coordinates connected to stations, # they don't move... so the whole thing stabilizes in a couple of cycles. # Stations are worked on in blocks to avoid locking up the GPU with one giant kernel. # for subset_low in range(0, n_stations, self.STATION_BLOCK_SIZE) : for subset_low in range(1) : # changed to try integrating more often subset_high = subset_low + self.STATION_BLOCK_SIZE if subset_high > n_stations : subset_high = n_stations sys.stdout.write( "\rpass %03i / station %04i of %04i / total runtime %03.1f min " % (n_pass, subset_high, n_stations, (time.time() - t_start) / 60.0) ) sys.stdout.flush() self.emit(QtCore.SIGNAL( 'outputProgress(int, int, int, float, float)' ), n_pass, subset_high, n_stations, (time.time() - t_start) / 60.0, (time.time() - t_start) / n_pass + (subset_low/n_stations) ) # adding texrefs in kernel call seems to change nothing, leaving them out. # max_x and max_y could be #defined in kernel source, along with STATION_BLOCK_SIZE forces_kernel(np.int32(n_stations), np.int32(subset_low), np.int32(subset_high), max_x, max_y, active_cells_gpu, coords_gpu, forces_gpu, weights_gpu, errors_gpu, debug_gpu, debug_img_gpu, block=CUDA_BLOCK_SHAPE, grid=cuda_grid_shape) #autoinit.context.synchronize() self.cuda_context.synchronize() # show a sample of the results #print coords_gpu.get() [00:10,00:10] #print forces_gpu.get() [00:10,00:10] #print weights_gpu.get()[00:10,00:10] time.sleep(0.05) # let the OS GUI use the GPU for a bit. #pl.imshow( (debug_img_gpu.get() / 60.0).T, cmap=mymap, origin='bottom')#, vmin=0, vmax=100 ) #pl.title( 'Debugging Output - step %03d' %n_pass ) #pl.colorbar() #pl.savefig( 'img/debug%03d.png' % n_pass ) #pl.close() # why was this indented? shouldn't it be integrated only after all stations are taken into account? integrate_kernel(max_x, max_y, coords_gpu, forces_gpu, weights_gpu, block=CUDA_BLOCK_SHAPE, grid=cuda_grid_shape) self.cuda_context.synchronize() if (self.IMAGES_EVERY > 0) and (n_pass % self.IMAGES_EVERY == 0) : #print 'Kernel debug output:' #print debug_gpu # velocities = np.sqrt(np.sum(forces_gpu.get() ** 2, axis = 2)) # png_f = open('img/vel%03d.png' % n_pass, 'wb') # png_w = png.Writer(max_x, max_y, greyscale = True, bitdepth=8) # png_w.write(png_f, velocities / 1200) # png_f.close() # np.set_printoptions(threshold=np.nan) # print velocities.astype(np.int32) # pl.imshow( velocities.T, origin='bottom') #, vmin=0, vmax=100 ) # pl.title( 'Velocity ( sec / timestep) - step %03d' % n_pass ) # pl.colorbar() # pl.savefig( 'img/vel%03d.png' % n_pass ) # plt.close() # # pl.imshow( (errors_gpu.get() / weights_gpu.get() / 60.0 ).T, cmap=mymap, origin='bottom') #, vmin=0, vmax=100 ) # pl.title( 'Average absolute error (min) - step %03d' %n_pass ) # pl.colorbar() # pl.savefig( 'img/err%03d.png' % n_pass ) # pl.close() # pl.imshow( (debug_img_gpu.get() / 60.0).T, cmap=mymap, origin='bottom') #, vmin=0, vmax=100 ) # pl.title( 'Debugging Output - step %03d' %n_pass ) # pl.colorbar() # pl.savefig( 'img/debug%03d.png' % n_pass ) # pl.close() #self.emit( QtCore.SIGNAL( 'outputImage(QString)' ), QtCore.QString('img/err%03d.png' % n_pass) ) #self.emit( QtCore.SIGNAL( 'outputImage(QImage)' ), numpy2qimage( (errors_gpu.get() / weights_gpu.get() / 60.0 / 30 * 255 ).astype(np.uint8) ) ) velocities = np.sqrt(np.sum(forces_gpu.get() ** 2, axis = 2)) velocities /= 15. # out of 15 sec range velocities *= 255 np.clip(velocities, 0, 255, velocities) velImage = numpy2qimage(velocities.astype(np.uint8)).transformed(QtGui.QMatrix().rotate(-90)) # errors = np.sqrt(errors_gpu.get() / weights_gpu.get()) e = np.sum(np.nan_to_num(errors_gpu.get())) / np.sum(np.nan_to_num(weights_gpu.get())) print "average error (sec) over all active cells:", e errors = errors_gpu.get() / weights_gpu.get() # average instead of RMS error errors /= 60. errors /= 15. # out of 15 min range errors *= 255 np.clip(errors, 0, 255, errors) errImage = numpy2qimage(errors.astype(np.uint8)).transformed(QtGui.QMatrix().rotate(-90)) self.emit( QtCore.SIGNAL( 'outputImage(QImage, QImage)' ), errImage, velImage ) velImage.save('img/vel%03d.png' % n_pass, 'png' ) errImage.save('img/err%03d.png' % n_pass, 'png' ) sys.stdout.write( "/ avg pass time %02.1f sec" % ( (time.time() - t_start) / n_pass, ) ) sys.stdout.flush() #end of main loop np.save('result.npy', coords_gpu.get())