def plot_Vs(self, vs_br): from scipy.spatial.distance import pdist vsfile = self.directory + '/Cs_gll_sem2d.tab' with open(vsfile, 'r') as v: vs_int = pd.read_csv(v, sep='\s+', names=['vs', 'x', 'z']) tmp = vs_int.drop_duplicates() self.vs_int = tmp.drop(tmp[tmp['vs'] == vs_br].index) #dx = pdist(self.vs_int['x'][:,None],'cityblock') #dx = np.min(dx[np.nonzero(dx)]) #dz = pdist(self.vs_int['z'][:,None],'cityblock') #dz = np.min(dz[np.nonzero(dz)]) minx, maxx = np.min(self.vs_int['x']), np.max(self.vs_int['x']) minz, maxz = np.min(self.vs_int['z']), np.max(self.vs_int['z']) l = len(self.vs_int['x']) xi, zi = np.linspace(minx, maxx, l), np.linspace(minz, maxz, l) Xi, Zi = np.meshgrid(xi, zi) #plt.scatter(self.vs_int['x'],self.vs_int['z'],c=self.vs_int['vs'],cmap='jet') #plt.show() x = self.vs_int['x'].values z = self.vs_int['z'].values vs = self.vs_int['vs'].values y = gd((x, z), vs, (Xi, Zi), method='nearest') plt.figure() plt.imshow(y, cmap='jet', aspect='auto') plt.show() db.set_trace()
def _plot_energy(self, num_samples=25, path_length=20): """ Plots the energy function of the network. num_samples The number of samples to be used in the computation of the energy function. The greater the number of samples, the higher the accuracy of the resultant plot. path_length The number of steps to compute in calculating each sample's path of convergence toward the network's attractors. """ attractors = self.training_data states = [[np.random.choice([-1, 1]) for i in range(self.num_neurons)] for j in range(num_samples)] pca = PCA(n_components=2) pca.fit(attractors) paths = [attractors] for i in range(path_length): states = self.learn(states, steps=1) paths.append(states) x = y = linspace(-1, 1, 100) X,Y = meshgrid(x, y) meshpts = array([[x, y] for x, y in zip(np.ravel(X), np.ravel(Y))]) mesh = pca.inverse_transform(meshpts) grid = vstack((mesh, vstack(paths))) energies = array([self.energy(point) for point in grid]) grid = pca.transform(grid) gmin, gmax = grid.min(), grid.max() xi, yi = np.mgrid[gmin:gmax:100j, gmin:gmax:100j] zi = gd(grid, energies, (xi, yi), method='nearest') self.energy_diagram.plot_surface(xi, yi, zi, cmap=cm.coolwarm, linewidth=1) self.contour_diagram.contour(xi, yi, zi)
def contour(x, y, z, ncontours = 50, colorbar=True, fig=None, ax=None, method='linear', zlim=None, cmap=None): import matplotlib.pylab as _plt from scipy.interpolate import griddata as gd # check input if ax is None: if fig is None: ax = _plt.gca() else: ax = fig.gca() # grid data points = _np.hstack([x[:,None],y[:,None]]) xi, yi = _np.mgrid[x.min():x.max():100j, y.min():y.max():100j] zi = gd(points, z, (xi, yi), method=method) # contour level levels if zlim is None: zlim = (z.min(), z.max()) eps = (zlim[1] - zlim[0]) / float(ncontours) levels = _np.linspace(zlim[0] - eps, zlim[1] + eps) # contour plot if cmap is None: cmap=_plt.cm.jet cf = ax.contourf(xi, yi, zi, ncontours, cmap=cmap, levels=levels) # color bar if requested if colorbar: _plt.colorbar(cf, ax=ax) return ax
def _plot_energy(self, num_samples=25, path_length=20): """ Plots the energy function of the network. num_samples The number of samples to be used in the computation of the energy function. The greater the number of samples, the higher the accuracy of the resultant plot. path_length The number of steps to compute in calculating each sample's path of convergence toward the network's attractors. """ attractors = self.training_data states = [[np.random.choice([-1, 1]) for i in range(self.num_neurons)] for j in range(num_samples)] pca = PCA(n_components=2) pca.fit(attractors) paths = [attractors] for i in range(path_length): states = self.learn(states, steps=1) paths.append(states) x = y = linspace(-1, 1, 100) X, Y = meshgrid(x, y) meshpts = array([[x, y] for x, y in zip(np.ravel(X), np.ravel(Y))]) mesh = pca.inverse_transform(meshpts) grid = vstack((mesh, vstack(paths))) energies = array([self.energy(point) for point in grid]) grid = pca.transform(grid) gmin, gmax = grid.min(), grid.max() xi, yi = np.mgrid[gmin:gmax:100j, gmin:gmax:100j] zi = gd(grid, energies, (xi, yi), method='nearest') self.energy_diagram.plot_surface(xi, yi, zi, cmap=cm.coolwarm, linewidth=1) self.contour_diagram.contour(xi, yi, zi)
def rinterp(x, y, z): # Set up a regular grid of interpolation points dy = y[1] - y[0] Nx, Ny = 2 * max(x), max(y) / dy xi, yi = np.linspace(min(x), max(x), Nx), np.linspace(0, max(y), Ny) Xi, Yi = np.meshgrid(xi, yi) zi = gd((x, y), z, (Xi, Yi), method='linear') return xi, yi, zi
def scatter_contour(x, y, z, ncontours = 50, colorbar=True, fig=None, ax=None, cmap=None, outfile=None): """Shows a contour plot on scattered data (x,y,z) and the plots the positions of the points (x,y) on top. Parameters ---------- x : ndarray(T) x-coordinates y : ndarray(T) y-coordinates z : ndarray(T) z-coordinates ncontours : int, optional, default = 50 number of contour levels fig : matplotlib Figure object, optional, default = None the figure to plot into. When set to None the default Figure object will be used ax : matplotlib Axes object, optional, default = None the axes to plot to. When set to None the default Axes object will be used. cmap : matplotlib colormap, optional, default = None the color map to use. None will use pylab.cm.jet. outfile : str, optional, default = None output file to write the figure to. When not given, the plot will be displayed Returns ------- ax : Axes object containing the plot """ # check input if (ax is None): if fig is None: ax = _plt.gca() else: ax = fig.gca() # grid data points = _np.hstack([x[:,None],y[:,None]]) xi, yi = _np.mgrid[x.min():x.max():100j, y.min():y.max():200j] zi = gd(points, z, (xi, yi), method='cubic') # contour level levels eps = (z.max() - z.min()) / float(ncontours) levels = _np.linspace(z.min() - eps, z.max() + eps) # contour plot if cmap is None: cmap=_plt.cm.jet cf = ax.contourf(xi, yi, zi, 15, cmap=cmap, levels=levels) # color bar if requested if colorbar: _plt.colorbar(cf, ax=ax) # scatter points ax.scatter(x,y,marker='o',c='b',s=5) # show or save #if outfile is None: # _plt.show() if outfile is not None: _plt.savefig(outfile) return ax
def plot_tf(tf, freq, xcoord, fmax, savefile=None, cmap='jet', **kwargs): # Interpolated array on 2d meshgrid xmin, xmax = np.min(xcoord), np.max(xcoord) ymax = np.max(freq) dy = freq[1] - freq[0] x1, y1 = np.meshgrid(xcoord[:tf.shape[0]], freq) xi, yi = np.linspace(xmin, xmax, int(2 * xmax)), np.linspace(0, ymax, int(ymax / dy)) X, Y = np.meshgrid(xi, yi) zi = gd((x1.ravel(order='F'), y1.ravel(order='F')), tf.ravel(), (X, Y)) # Plot fig = plt.figure(figsize=(8, 6)) if 'clim' in kwargs: cmin = kwargs['clim'][0] cmax = kwargs['clim'][1] else: cmin = 0 cmax = 7 ax = fig.add_subplot(111) im = ax.imshow(zi, cmap=cmap, aspect='auto', interpolation='bilinear',vmin=cmin, vmax=cmax, \ origin='lower', extent=[xmin,xmax, min(freq),ymax]) if 'xlim' in kwargs: ax.set_xlim(kwargs['xlim'][0], kwargs['xlim'][1]) if 'ylim' in kwargs: ax.set_ylim(kwargs['ylim'][0], kwargs['ylim'][1]) else: ax.set_ylim(0.1, fmax) if 'ylabel' in kwargs: ax.set_ylabel(kwargs['ylabel'], fontsize=16, color='black') else: ax.set_ylabel('Frequency [Hz]', fontsize=16, color='black') if 'xlabel' in kwargs: ax.set_xlabel(kwargs['xlabel'], fontsize=16, color='black') else: ax.set_xlabel('Horizontal profile [m]', fontsize=16, color='black') if 'title' in kwargs: ax.set_title(kwargs['title'], fontsize=20, color='black') ax.tick_params(axis='both', labelsize=12, color='black') cb = fig.colorbar(im, shrink=0.6, aspect=10, pad=0.02, ticks=np.linspace(cmin,cmax,cmax+1), \ boundaries=np.linspace(cmin,cmax,cmax+1)) cb.set_label('Amplification', labelpad=15, y=0.5, rotation=90, fontsize=16, color='black') cb.ax.yaxis.set_tick_params(color='black', labelsize=12) if savefile != None: fig.savefig(savefile, dpi=300) plt.show(block=False)
def plot_snapshot_tests(self, fname, interval, vmin=-1.e-10, vmax=1.e-10, save=False, outdir='./', show=False): ''' very slow... needs optimisation ! ''' print('Plotting snapshots...') filename = self.directory + fname print('reading ...', filename) field = self.readField(filename) coord = self.mdict["coord"] xcoord = coord[:, 0] zcoord = coord[:, 1] nbx = len(xcoord) / 4 nbz = len(zcoord) / 4 ext = [min(xcoord), max(xcoord), min(zcoord), max(zcoord)] # x,z = np.meshgrid(np.linspace(ext[0],ext[1],1000),np.linspace(ext[2],ext[3],1000)) x, z = np.meshgrid(np.linspace(ext[0], ext[1], 50), np.linspace(ext[2], ext[3], 50)) print('grid data ...') y = gd((xcoord, zcoord), field, (x, z), method='linear') print('flipud ...') y = np.flipud(y) print('Snapshots -- min and max:', min(field), max(field)) fig = plt.figure() sns.set_style('whitegrid') ax = fig.add_subplot(111) # Fault rupture outputs im = ax.imshow(y, extent=[min(xcoord), max(xcoord), min(zcoord), max(zcoord)], \ vmin=vmin, vmax=vmax, cmap='seismic') # Adding rectangle to restrain the fault area (optional) # ax.add_patch(Rectangle((-15.0, -1.5),30., 3.,alpha=1,linewidth=1,edgecolor='k',facecolor='none')) plt.ylabel('Width / $L_{c}$') plt.xlabel('Length / $L_{c}$') c = plt.colorbar(im, fraction=0.046, pad=0.1, shrink=0.4) c.set_clim(vmin, vmax) c.set_label('Amplitude') tit = 'Snapshot at t (s)= ' + str(interval) tit += ' Max vel. amplitude = ' + str('%.2f' % (max(abs(field)))) plt.title(tit) plt.tight_layout() if save: plt.savefig(fname + '.png', dpi=300) # save into current directory if show: plt.show() plt.close()
def _plot_energy(self, num_samples=4, path_length=20): """ Plots the energy function of the network. num_samples The number of samples to be used in the computation of the energy function. The greater the number of samples, the higher the accuracy of the resultant plot. path_length The number of steps to compute in calculating each sample's path of convergence toward the network's attractors. """ attractors = self.training_data states = [[np.random.choice([-1, 1]) for i in range(self.num_neurons)] for j in range(num_samples)] self.pca = PCA(n_components=2) self.pca.fit(attractors) paths = [attractors] for i in range(path_length): states = self.recall(states, steps=1) paths.append(states) x = y = np.linspace(-1, 1, 100) X,Y = np.meshgrid(x, y) meshpts = np.array([[x, y] for x, y in zip(np.ravel(X), np.ravel(Y))]) mesh = self.pca.inverse_transform(meshpts) grid = np.vstack((mesh, np.vstack(paths))) energies = np.array([self.energy(point) for point in grid]) grid = self.pca.transform(grid) gmin, gmax = grid.min(), grid.max() xi, yi = np.mgrid[gmin:gmax:100j, gmin:gmax:100j] zi = gd(grid, energies, (xi, yi), method='nearest') wireframe = self.energy_diagram.add_layer("wireframe") mesh_plot = self.energy_diagram.add_layer("mesh_plot") attracts = self.energy_diagram.add_layer("attractors") wireframe.add_data(xi, yi, zi) mesh_plot.add_data(xi, yi, zi) self.energy_diagram.build_layer(wireframe.name, plot=self.energy_diagram.plot_wireframe, colors=(0.5, 0.5, 0.5, 0.5), alpha=0.2) self.energy_diagram.build_layer(mesh_plot.name, plot=self.energy_diagram.plot_surface, cmap=cm.coolwarm) self.contour_diagram.contour(xi, yi, zi) grid = self.pca.transform(attractors) z = np.array([self.energy(state) for state in attractors]) attracts.add_data(grid[:,0], grid[:,1], z) self.energy_diagram.build_layer("attractors", plot=self.energy_diagram.scatter, s=80, c='g', marker='o') wireframe.hide() attracts.hide() def wireframe_click(event): wireframe.toggle_display() mesh_plot.toggle_display() def attractor_click(event): attracts.toggle_display() self.view_wfbutton.on_clicked(wireframe_click) self.view_attractbutton.on_clicked(attractor_click)
def interp(field, coord): """ Interpolates argument field over a meshgrid. Meshgrid size depends on argument coord. """ xcoord = coord[:, 0] zcoord = coord[:, 1] nbx = len(xcoord) nbz = len(zcoord) ext = [min(xcoord), max(xcoord), min(zcoord), max(zcoord)] x, z = np.meshgrid(np.linspace(ext[0], ext[1], 1000), np.linspace(ext[2], ext[3], 1000), sparse=True) y = gd((xcoord, zcoord), field, (x, z), method='linear') y = np.flipud(y) return y
def propellerStack_standalone( init_gpa=False, # Starts the gpa nScans = 1, # NEX larmorFreq = 3.07336e6, # Larmor frequency rfExAmp = 0.3, # rf excitation pulse amplitude rfReAmp = 0.3, # rf refocusing pulse amplitude rfExTime = 40e-6, # rf excitation pulse time rfReTime = 80e-6, # rf refocusing pulse time echoSpacing = 20e-3, # time between echoes repetitionTime = 300e-3, # TR inversionTime = 0e-3, # Inversion time for Inversino Recovery experiment fov = np.array([10e-2, 10e-2, 10e-2]), # FOV along readout, phase and slice dfov = np.array([0e-2, -10e-2, 0e-2]), # Displacement of fov center nPoints = np.array([20, 20, 20]), # Number of points along readout, phase and slice etl = 10, # Echo train length nLinesPerBlock = 1, # Number of lines for propeller undersampling = 1, # Angular undersampling acqTime = 2e-3, # Acquisition time (s) axes = np.array([0, 1, 2]), # 0->x, 1->y and 2->z defined as [rd,ph,sl] sweepMode = 1, # 0->k2k (T2), 1->02k (T1), 2->k20 (T2), 3->Niquist modulated (T2) phaseGradTime = 1000e-6, # Phase and slice dephasing time rdPreemphasis = 1.008, # Readout preemphasis factor (dephasing gradient is multiplied by this number) drfPhase = 0, # phase of the excitation pulse (in degrees) dummyPulses = 0, # Dummy pulses for T1 stabilization shimming = np.array([-80, -100, 20]), # Shimming along the X,Y and Z axes (a.u. *1e4) parAcqLines = 0 # Number of additional lines, Full sweep if 0 ): # rawData fields rawData = {} inputs = {} kSpace = {} auxiliar = {} # Miscellaneous blkTime = 10 # Deblanking time (us) larmorFreq = larmorFreq*1e-6 gradRiseTime = 400e-6 # Estimated gradient rise time gSteps = int(gradRiseTime*1e6/10)*0+1 # Gradient ramp steps gradDelay = 9 # Gradient amplifier delay addRdPoints = 10 # Initial rd points to avoid artifact at the begining of rd addRdGradTime = 1000 # Additional readout gradient time to avoid turn on/off effects on the Rx channel gammaB = 42.56e6 # Gyromagnetic ratio in Hz/T rfReAmp = rfExAmp rfReTime = 2*rfExTime shimming = shimming*1e-4 resolution = fov/nPoints kMax = 1/(2*resolution) dk = 1/fov oversamplingFactor = 6 auxiliar['resolution'] = resolution auxiliar['kMax'] = kMax auxiliar['dk'] = dk auxiliar['gradDelay'] = gradDelay*1e-6 auxiliar['gradRiseTime'] = gradRiseTime auxiliar['oversamplingFactor'] = oversamplingFactor auxiliar['addRdGradTime'] = addRdGradTime*1e-6 # Inputs for rawData inputs['nScans'] = nScans inputs['larmorFreq'] = larmorFreq # Larmor frequency inputs['rfExAmp'] = rfExAmp # rf excitation pulse amplitude inputs['rfReAmp'] = rfReAmp # rf refocusing pulse amplitude inputs['rfExTime'] = rfExTime # rf excitation pulse time inputs['rfReTime'] = rfReTime # rf refocusing pulse time inputs['echoSpacing'] = echoSpacing # time between echoes inputs['repetitionTime'] = repetitionTime # TR inputs['inversionTime'] = inversionTime # Inversion time for Inversion Recovery inputs['fov'] = fov # FOV along readout, phase and slice inputs['dfov'] = dfov # Displacement of fov center inputs['nPoints'] = nPoints # Number of points along readout, phase and slice inputs['etl'] = etl # Echo train length inputs['nLinesPerBlock'] = nLinesPerBlock # Number of lines for propeller inputs['undersampling'] = undersampling # Angular undersampling inputs['acqTime'] = acqTime # Acquisition time inputs['axes'] = axes # 0->x, 1->y and 2->z defined as [rd,ph,sl] inputs['sweepMode'] = sweepMode # 0->k2k (T2), 1->02k (T1), 2->k20 (T2), 3->Niquist modulated (T2) inputs['phaseGradTime'] = phaseGradTime # Phase and slice dephasing time inputs['rdPreemphasis'] = rdPreemphasis inputs['drfPhase'] = drfPhase inputs['dummyPulses'] = dummyPulses # Dummy pulses for T1 stabilization inputs['shimming'] = shimming # Calculate the acquisition bandwidth bandwidth = nPoints[2]/acqTime auxiliar['bandwidth'] = bandwidth # Oversampled BW BWov = bandwidth*oversamplingFactor samplingPeriod = 1/BWov*1e6 # Calculate the angles of each normalized k-space line phi = np.array([0]) while phi[-1]<np.pi: dPhi = np.array([(nPoints[1]+nPoints[0])/(nPoints[0]*nPoints[1])+np.abs(nPoints[1]-nPoints[0])/(nPoints[0]*nPoints[1])*np.cos(2*phi[-1])]) phi = np.concatenate((phi,phi[-1]+dPhi),axis=0) nBlocks = np.size(phi)-1 phi = phi[0:nBlocks] phiPropUnder = phi[0::nLinesPerBlock*undersampling] alpha = np.pi/(phiPropUnder[1]+phiPropUnder[-1]) phi = phiPropUnder*alpha nBlocks = np.size(phi) del alpha, dPhi, phiPropUnder # Calculate the number of readouts and acqTime as a function of phi nphx = nPoints[0]*np.abs(np.cos(phi)) nphy = nPoints[1]*np.abs(np.sin(phi)) nph = np.int32(np.round(np.sqrt(np.power(nphx,2)+np.power(nphy,2)))) del nphx, nphy # Calculate readout gradient and k-points (directions 2) rdGradAmp = bandwidth/(gammaB*fov[2]) kRd = np.linspace(-kMax[2], kMax[2], num = nPoints[2], endpoint = nPoints[2]%2) # Calculate the phase gradients (directions 0 and 1) phGradAmp = 1/(2*resolution*gammaB*(phaseGradTime+gradRiseTime)) phGradMax = np.sqrt(np.power(phGradAmp[0]*np.cos(phi), 2)+np.power(phGradAmp[1]*np.sin(phi), 2)) # Create k-space and get "phase" and "slice" gradients for each block ind = getIndex(etl, nPoints[0], sweepMode) slGradAmpBlock = {} phGradAmpBlock = {} kPropellerX = np.array([]) kPropellerY = np.array([]) kPropellerZ = np.array([]) dkPhiPar = np.sqrt(np.power(dk[0]*np.cos(phi), 2)+np.power(dk[1]*np.sin(phi), 2)) dkPhiPer = np.sqrt(np.power(dk[0]*np.sin(phi), 2)+np.power(dk[1]*np.cos(phi), 2)) for blockIndex in range(nBlocks): # Get dk for current propeller block dkPar = np.array([dkPhiPar[blockIndex]*np.cos(phi[blockIndex]), dkPhiPar[blockIndex]*np.sin(phi[blockIndex])]) dkPer = np.array([-dkPhiPer[blockIndex]*np.sin(phi[blockIndex]), dkPhiPer[blockIndex]*np.cos(phi[blockIndex])])*undersampling # Get "phase" gradients for current block phGradX = np.linspace(-phGradMax[blockIndex], phGradMax[blockIndex], num = nph[blockIndex], endpoint = nph[blockIndex]%2) phGradAmpBlock[blockIndex] = np.array([phGradX*np.cos(phi[blockIndex]), phGradX*np.sin(phi[blockIndex])]) phGradAmpBlock[blockIndex] = phGradAmpBlock[blockIndex][:,::-1] phGradAmpBlock[blockIndex] = phGradAmpBlock[blockIndex][:,ind] del phGradX # Get "slice" gradients for current block dGradPer = dkPer/(gammaB*(phaseGradTime+gradRiseTime)) ind0 = nLinesPerBlock-nLinesPerBlock%2 g0 = dGradPer*ind0/2 slGradAmpBlock[blockIndex] = np.array([g0[0]*np.linspace(-ind0/2, ind0/2, num=nLinesPerBlock, endpoint = nLinesPerBlock%2), g0[1]*np.linspace(-ind0/2, ind0/2, num=nLinesPerBlock, endpoint = nLinesPerBlock%2)]) del g0, ind0, dGradPer for lineIndex in range(nLinesPerBlock): kPlane = dkPer*((nLinesPerBlock-nLinesPerBlock%2)/2-lineIndex) for phIndex in range(nph[blockIndex]): kPhase = dkPar*((nph[blockIndex]-nph[blockIndex]%2)/2-phIndex) kLine = np.array([np.ones(nPoints[2])*(kPhase[0]-kPlane[0]), np.ones(nPoints[2])*(kPhase[1]-kPlane[1]), kRd]) # Save points into propeller k-points kPropellerX = np.concatenate((kPropellerX, kLine[0, :]), axis = 0) kPropellerY = np.concatenate((kPropellerY, kLine[1, :]), axis = 0) kPropellerZ = np.concatenate((kPropellerZ, kLine[2, :]), axis = 0) del kPlane, kPhase, kLine, dk, dkPar, dkPhiPar, dkPer, dkPhiPer # Generate cartesian k-points if nPoints[0]%2==1: kCartesianX = np.linspace(-kMax[0], kMax[0], num = nPoints[0], endpoint = True) else: kCartesianX = np.linspace(-kMax[0], kMax[0], num = nPoints[0], endpoint = False) if nPoints[1]%2==1: kCartesianY = np.linspace(-kMax[1], kMax[1], num = nPoints[1], endpoint = True) else: kCartesianY = np.linspace(-kMax[1], kMax[1], num = nPoints[1], endpoint = False) if nPoints[2]%2==1: kCartesianZ = np.linspace(-kMax[2], kMax[2], num = nPoints[2], endpoint = True) else: kCartesianZ = np.linspace(-kMax[2], kMax[2], num = nPoints[2], endpoint = False) kCartesianY, kCartesianZ, kCartesianX = np.meshgrid(kCartesianY, kCartesianZ, kCartesianX) kCartesianX = np.squeeze(np.reshape(kCartesianX, (1, nPoints[0]*nPoints[1]*nPoints[2]))) kCartesianY = np.squeeze(np.reshape(kCartesianY, (1, nPoints[0]*nPoints[1]*nPoints[2]))) kCartesianZ = np.squeeze(np.reshape(kCartesianZ, (1, nPoints[0]*nPoints[1]*nPoints[2]))) if nPoints[2]==1: kCartesianZ *=0 # plt.figure(2) # ax = plt.axes(projection='3d') ## ax.scatter3D(kPropellerX, kPropellerY, kPropellerZ, 'bo') # ax.scatter3D(kCartesianX, kCartesianY, kCartesianZ, 'ro') # plt.show() # Change gradient values to OCRA units gFactor = reorganizeGfactor(axes) rdGradAmp = rdGradAmp/gFactor[2]*1000/10 for blockIndex in range(nBlocks): phGradAmpBlock[blockIndex][0, :] = phGradAmpBlock[blockIndex][0, :]/gFactor[0]*1000/10 phGradAmpBlock[blockIndex][1, :] = phGradAmpBlock[blockIndex][1, :]/gFactor[1]*1000/10 slGradAmpBlock[blockIndex][0, :] = slGradAmpBlock[blockIndex][0, :]/gFactor[0]*1000/10 slGradAmpBlock[blockIndex][1, :] = slGradAmpBlock[blockIndex][1, :]/gFactor[1]*1000/10 # Create sequence def createSequence(): nRepetitions = int(nBlocks*nLinesPerBlock*nPoints[0]/etl)+dummyPulses scanTime = nRepetitions*repetitionTime # Set shimming iniSequence(20, shimming) slIndex = 0 phIndex = 0 blockIndex = 0 for repeIndex in range(nRepetitions): # Initialize time tRep = 20e3+inversionTime+repetitionTime*repeIndex # Inversion pulse if inversionTime!=0: t0 = tRep-inversionTime-rfReTime/2-blkTime rfPulse(t0,rfReTime,rfReAmp,0) # Excitation pulse t0 = tRep-rfExTime/2-blkTime rfPulse(t0,rfExTime,rfExAmp,drfPhase*np.pi/180) # Dephasing readout t0 = tRep+rfExTime/2-gradDelay if repeIndex>=dummyPulses: # This is to account for dummy pulses gradTrap(t0, nPoints[2]/bandwidth+2*addRdGradTime, rdGradAmp/2*rdPreemphasis, axes[2]) # Echo train for echoIndex in range(etl): tEcho = tRep+echoSpacing*(echoIndex+1) # Refocusing pulse t0 = tEcho-echoSpacing/2-rfReTime/2-blkTime rfPulse(t0, rfReTime, rfReAmp, np.pi/2) # Dephasing phase and slice gradients t0 = tEcho-echoSpacing/2+rfReTime/2-gradDelay if repeIndex>=dummyPulses: # This is to account for dummy pulses gradTrap(t0, phaseGradTime, phGradAmpBlock[blockIndex][0,phIndex]+slGradAmpBlock[blockIndex][0, slIndex], axes[0]) gradTrap(t0, phaseGradTime, phGradAmpBlock[blockIndex][1,phIndex]+slGradAmpBlock[blockIndex][1, slIndex], axes[1]) # Readout gradient if repeIndex>=dummyPulses: # This is to account for dummy pulses if nPoints[2]%2==0: t0 = tEcho-(nPoints[2]+1)/(2*bandwidth)-addRdGradTime-gradRiseTime-gradDelay gradTrap(t0, (nPoints[2]+1)/bandwidth+2*addRdGradTime, rdGradAmp, axes[2]) else: t0 = tEcho-nPoints[2]/(2*bandwidth)-addRdGradTime-gradRiseTime-gradDelay gradTrap(t0, nPoints[2]/bandwidth+2*addRdGradTime, rdGradAmp, axes[2]) # Rx gate if repeIndex>=dummyPulses: # This is to account for dummy pulses if nPoints[2]%2==0: t0 = tEcho-(nPoints[2]+1)/(2*bandwidth)-addRdPoints/bandwidth-1/(2*bandwidth) else: t0 = tEcho-(nPoints[2])/(2*bandwidth)-addRdPoints/bandwidth-1/(2*bandwidth) rxGate(t0, (nPoints[2]+2*addRdPoints)/bandwidth) # Rephasing phase and slice gradients t0 = tEcho+nPoints[2]/(2*bandwidth)+addRdGradTime+gradRiseTime if (echoIndex<etl-1 and repeIndex>=dummyPulses): gradTrap(t0, phaseGradTime, -phGradAmpBlock[blockIndex][0,phIndex]-slGradAmpBlock[blockIndex][0,slIndex], axes[0]) gradTrap(t0, phaseGradTime, -phGradAmpBlock[blockIndex][1,phIndex]-slGradAmpBlock[blockIndex][1,slIndex], axes[1]) # Update phase line phIndex += 1 # Update the block and slice index if repeIndex>=dummyPulses: if (phIndex==nph[blockIndex]) and (slIndex==nLinesPerBlock-1): phIndex = 0 slIndex = 0 blockIndex +=1 elif phIndex == nph[blockIndex]: phIndex = 0 slIndex += 1 if repeIndex == nRepetitions: endSequence(scanTime) # Frequency calibration function def createFreqCalSequence(): t0 = 20 # Shimming iniSequence(t0, shimming) # Excitation pulse rfPulse(t0,rfExTime,rfExAmp,drfPhase*np.pi/180) # Refocusing pulse t0 += rfExTime/2+echoSpacing/2-rfReTime/2 rfPulse(t0, rfReTime, rfReAmp, np.pi/2) # Rx t0 += blkTime+rfReTime/2+echoSpacing/2-acqTimeFreqCal/2-addRdPoints/bandwidth rxGate(t0, acqTimeFreqCal+2*addRdPoints/bandwidth) # Finalize sequence endSequence(repetitionTime) def rfPulse(tStart,rfTime,rfAmplitude,rfPhase): txTime = np.array([tStart+blkTime,tStart+blkTime+rfTime]) txAmp = np.array([rfAmplitude*np.exp(1j*rfPhase),0.]) txGateTime = np.array([tStart,tStart+blkTime+rfTime]) txGateAmp = np.array([1,0]) expt.add_flodict({ 'tx0': (txTime, txAmp), 'tx_gate': (txGateTime, txGateAmp) }) def rxGate(tStart,gateTime): rxGateTime = np.array([tStart,tStart+gateTime]) rxGateAmp = np.array([1,0]) expt.add_flodict({ 'rx0_en':(rxGateTime, rxGateAmp), 'rx_gate': (rxGateTime, rxGateAmp), }) def gradTrap(tStart, gTime, gAmp, gAxis): tUp = np.linspace(tStart, tStart+gradRiseTime, num=gSteps, endpoint=False) tDown = tUp+gradRiseTime+gTime t = np.concatenate((tUp, tDown), axis=0) dAmp = gAmp/gSteps aUp = np.linspace(dAmp, gAmp, num=gSteps) aDown = np.linspace(gAmp-dAmp, 0, num=gSteps) a = np.concatenate((aUp, aDown), axis=0) if gAxis==0: expt.add_flodict({'grad_vx': (t, a+shimming[0])}) elif gAxis==1: expt.add_flodict({'grad_vy': (t, a+shimming[1])}) elif gAxis==2: expt.add_flodict({'grad_vz': (t, a+shimming[2])}) def endSequence(tEnd): expt.add_flodict({ 'grad_vx': (np.array([tEnd]),np.array([0]) ), 'grad_vy': (np.array([tEnd]),np.array([0]) ), 'grad_vz': (np.array([tEnd]),np.array([0]) ), }) def iniSequence(tEnd, shimming): expt.add_flodict({ 'grad_vx': (np.array([tEnd]),np.array([shimming[0]]) ), 'grad_vy': (np.array([tEnd]),np.array([shimming[1]]) ), 'grad_vz': (np.array([tEnd]),np.array([shimming[2]]) ), }) # Changing time parameters to us rfExTime = rfExTime*1e6 rfReTime = rfReTime*1e6 echoSpacing = echoSpacing*1e6 repetitionTime = repetitionTime*1e6 gradRiseTime = gradRiseTime*1e6 phaseGradTime = phaseGradTime*1e6 inversionTime = inversionTime*1e6 bandwidth = bandwidth*1e-6 # Calibrate frequency expt = ex.Experiment(lo_freq=larmorFreq, rx_t=samplingPeriod, init_gpa=init_gpa, gpa_fhdo_offset_time=(1 / 0.2 / 3.1)) samplingPeriod = expt.get_rx_ts()[0] bandwidth = 1/samplingPeriod/oversamplingFactor acqTimeFreqCal = nPoints[2]/bandwidth # us auxiliar['bandwidth'] = bandwidth*1e6 createFreqCalSequence() rxd, msgs = expt.run() dataFreqCal = sig.decimate(rxd['rx0']*13.788, oversamplingFactor, ftype='fir', zero_phase=True) dataFreqCal = dataFreqCal[addRdPoints:nPoints[2]+addRdPoints] # Plot fid plt.figure(1) tVector = np.linspace(-acqTimeFreqCal/2, acqTimeFreqCal/2, num=nPoints[2],endpoint=True)*1e-3 plt.subplot(1, 2, 1) plt.plot(tVector, np.abs(dataFreqCal)) plt.title("Signal amplitude") plt.xlabel("Time (ms)") plt.ylabel("Amplitude (mV)") plt.subplot(1, 2, 2) angle = np.unwrap(np.angle(dataFreqCal)) plt.title("Signal phase") plt.xlabel("Time (ms)") plt.ylabel("Phase (rad)") plt.plot(tVector, angle) # Get larmor frequency dPhi = angle[-1]-angle[0] df = dPhi/(2*np.pi*acqTimeFreqCal) larmorFreq += df auxiliar['larmorFreq'] = larmorFreq*1e6 print("f0 = %s MHz" % (round(larmorFreq, 5))) # Plot sequence: # expt.plot_sequence() # plt.show() # Delete experiment: expt.__del__() # Create full sequence expt = ex.Experiment(lo_freq=larmorFreq, rx_t=samplingPeriod, init_gpa=init_gpa, gpa_fhdo_offset_time=(1 / 0.2 / 3.1)) samplingPeriod = expt.get_rx_ts()[0] bandwidth = 1/samplingPeriod/oversamplingFactor auxiliar['bandwidth'] = bandwidth*1e6 acqTime = nPoints[0]/bandwidth # us createSequence() # Plot sequence: # expt.plot_sequence() # plt.show() # Run the experiment seqTime = sum(nLinesPerBlock*nph/etl)*repetitionTime*1e-6*nScans print("Expected scan time = %s s" %(round(seqTime, 0))) print("Runing sequence...") tStart = time.time() dataFull = [] for ii in range(nScans): rxd, msgs = expt.run() rxd['rx0'] = rxd['rx0']*13.788 # Here I normalize to get the result in mV # Get data scanData = sig.decimate(rxd['rx0'], oversamplingFactor, ftype='fir', zero_phase=True) dataFull = np.concatenate((dataFull, scanData), axis = 0) print("scan done!") expt.__del__() tEnd = time.time() print("True scan time = %s s" %(round(tEnd-tStart, 0))) # Average data print("Averaging data...") nrdTotal = sum(nph*nLinesPerBlock*(nPoints[2]+2*addRdPoints)) dataProvA = np.reshape(dataFull, (nScans, nrdTotal)) dataProvA = np.average(dataProvA, axis=0) # Reorganize data according to sweep mode print("Reorganizing data according to sweep mode...") dataProvB = np.zeros(np.size(dataProvA), dtype=complex) n0 = 0 n1 = 0 for blockIndex in range(nBlocks): n1 += (nPoints[2]+2*addRdPoints)*nph[blockIndex]*nLinesPerBlock dataProvC = np.reshape(dataProvA[n0:n1], (nLinesPerBlock, nph[blockIndex], nPoints[2]+2*addRdPoints)) dataProvD = dataProvC*0 for phIndex in range(nph[blockIndex]): dataProvD[:, ind[phIndex], :] = dataProvC[:, phIndex, :] dataProvD = dataProvD[:, ::-1, :] dataProvD = np.reshape(dataProvD, (1, (nPoints[2]+2*addRdPoints)*nLinesPerBlock*nph[blockIndex])) dataProvB[n0:n1] = dataProvD n0 = n1 del dataProvC, dataProvD, n0, n1 # Delete the additional rd points and fix echo delay!! print("Deleting additional readout points...") dataPropeller = np.zeros(sum(nph*nLinesPerBlock*nPoints[2]), dtype=complex) n0 = 0 n1 = 0 n2 = 0 n3 = 0 for blockIndex in range(nBlocks): n1 += (nPoints[2]+2*addRdPoints)*nph[blockIndex]*nLinesPerBlock n3 += nPoints[2]*nph[blockIndex]*nLinesPerBlock dataProvC = np.reshape(dataProvB[n0:n1], (nLinesPerBlock, nph[blockIndex], nPoints[2]+2*addRdPoints)) k0Line = dataProvC[int((nLinesPerBlock-nLinesPerBlock%2)/2), int((nph[blockIndex]-nph[blockIndex]%2)/2), :] k0 = np.argmax(np.abs(k0Line)) dataProvC = dataProvC[:, :, k0-int((nPoints[2]-nPoints[2]%2)/2):k0+int((nPoints[2]+nPoints[2]%2)/2)] dataProvC = np.reshape(dataProvC, (1,nPoints[2]*nLinesPerBlock*nph[blockIndex])) dataPropeller[n2:n3] = dataProvC n0 = n1 n2 = n3 del dataProvA, dataProvB, dataProvC, n0, n1, n2, n3 # Fix the position of the sample according to dfov dPhase = np.exp(-2*np.pi*1j*(dfov[0]*kPropellerX+dfov[1]*kPropellerY+dfov[2]*kPropellerZ)) dataPropeller = dataPropeller*dPhase # Regridding to cartesian k-space print("Regridding to cartesian grid...") tStart = time.time() if nPoints[2]==1: dataCartesian = gd((kPropellerX, kPropellerY), dataPropeller, (kCartesianX, kCartesianY), method='linear', fill_value=0.) else: dataCartesian = gd((kPropellerX, kPropellerY, kPropellerZ), dataPropeller, (kCartesianX, kCartesianY, kCartesianZ), method='linear', fill_value=0.) tEnd = time.time() print("Regridding done in = %s s" %(round(tEnd-tStart, 0))) kPropeller = np.array([kPropellerX, kPropellerY, kPropellerZ, dataPropeller]) kCartesian = np.array([kCartesianX, kCartesianY, kCartesianZ, dataCartesian]) del kCartesianX, kCartesianY, kCartesianZ, kPropellerX, kPropellerY, kPropellerZ, dataPropeller auxiliar['kMax'] = kMax kSpace['sampled'] = np.transpose(kCartesian) kSpace['sampledProp'] = np.transpose(kPropeller) # Get image with FFT dataCartesian = np.reshape(dataCartesian, (nPoints[2], nPoints[1], nPoints[0])) img = np.fft.ifftshift(np.fft.ifftn(np.fft.ifftshift(dataCartesian))) kSpace['image'] = img # Save data dt = datetime.now() dt_string = dt.strftime("%Y.%m.%d.%H.%M.%S") dt2 = date.today() dt2_string = dt2.strftime("%Y.%m.%d") if not os.path.exists('experiments/acquisitions/%s' % (dt2_string)): os.makedirs('experiments/acquisitions/%s' % (dt2_string)) if not os.path.exists('experiments/acquisitions/%s/%s' % (dt2_string, dt_string)): os.makedirs('experiments/acquisitions/%s/%s' % (dt2_string, dt_string)) inputs['name'] = "%s.%s.mat" % ("TSE",dt_string) auxiliar['fileName'] = "%s.%s.mat" % ("PROPELLER STACK",dt_string) rawData['inputs'] = inputs rawData['auxiliar'] = auxiliar rawData['kSpace'] = kSpace rawdata = {} rawdata['rawData'] = rawData savemat("experiments/acquisitions/%s/%s/%s.%s.mat" % (dt2_string, dt_string, "PROPELLER STACK",dt_string), rawdata) # Plot k-space plt.figure(3) dataPlot = dataCartesian[round(nPoints[2]/2), :, :] # dataPlot = dataCartesian[:, :, round(nPoints[0]/2)] plt.subplot(121) plt.imshow(np.log(np.abs(dataPlot)),cmap='gray') plt.axis('off') # Plot image imgPlot = img[round(nPoints[2]/2), :, :] plt.subplot(122) plt.imshow(np.abs(imgPlot), cmap='gray') plt.axis('off') plt.title("PROPELLER_STACK.%s.mat" % (dt_string)) plt.show()
def main(): # constantes RUTA_CARPETA = "/home/jorge/Documents/Research/proyectoCaborca" # fecha actual fechaActual = strftime("%Y-%m-%d") # fecha -1 anio, mes, dia = generarDiaAnterior(fechaActual) # generar ruta de archivos # rutaCarpetaDeArchivos = "{}/estaciones/{}-{:02d}-{:02d}".format(RUTA_CARPETA, anio, mes, dia) # ruta carpeta rutaCarpetaEstacionesMapas = "******".format(RUTA_CARPETA, anio, mes, dia) # crear carpeta de mapas if not os.path.exists(rutaCarpetaEstacionesMapas): os.mkdir(rutaCarpetaEstacionesMapas) else: print("***** Carpeta ya existe") # datos de estaciones rutaArchivoEstaciones = "{}/estaciones/coor_est.csv".format(RUTA_CARPETA) dataEstaciones = pd.read_csv(rutaArchivoEstaciones) # ciclo # generar frame frames = [] for i in range(1,6,1): print("valor de i:", i) # datos a procesar rutaDatosEstaciones = "{}/estaciones/{}-{:02d}-{:02d}/{}.csv".format(RUTA_CARPETA, anio, mes, dia,i) print(rutaDatosEstaciones) dataTemp = pd.read_csv(rutaDatosEstaciones) print(dataTemp.head()) # valor de longitud # latTemp = dataEstaciones.loc[dataEstaciones["ID"]==i]["Lat"] latTemp = dataEstaciones.iloc[i-1]["Lat"] print("***** lat", latTemp) dataTemp["Lat"] = latTemp # valor de latitud # lonTemp = dataEstaciones.loc[dataEstaciones["ID"]==i]["Long"] lonTemp = dataEstaciones.iloc[i-1]["Long"] dataTemp["Lon"] = lonTemp print("***** Lon", lonTemp) frames.append(dataTemp) print(dataTemp.head()) # merge frames data = pd.concat(frames) # array columnas a evaluar arrayColumns = ["PM1_1M", "PM1_5M","PM1_15M","PM2P5_1M","PM2P5_5M","PM2P5_15M","PM2P5_1H","PM1_1H","PM10_1M","PM10_5M","PM10_15M","PM10_1H"] # tiempos a mapear tiempos = np.array(data["Time"]) tiempos = np.unique(tiempos) # ciclo de procesamiento for t in tiempos: # clasificar valores dataProcesamiento = data.loc[data["Time"] == t] for columna in arrayColumns: # determinar valores de x y y x_values = np.array(dataProcesamiento["Lon"]) y_values = np.array(dataProcesamiento["Lat"]) # ordenar valores x_values.sort() y_values.sort() # print print(x_values) print(y_values) # limites min y max LONG_MIN = x_values[0] LONG_MAX = x_values[-1] LAT_MIN = y_values[0] LAT_MAX = y_values[-1] # limites longitud > -115.65 y < -107.94 dataProcesamiento = dataProcesamiento.loc[dataProcesamiento['Lon'] > LONG_MIN] dataProcesamiento = dataProcesamiento.loc[dataProcesamiento['Lon'] < LONG_MAX] # limites latitud > 25.41 y < 33.06 dataProcesamiento = dataProcesamiento.loc[dataProcesamiento['Lat'] > LAT_MIN] dataProcesamiento = dataProcesamiento.loc[dataProcesamiento['Lat'] < LAT_MAX] # obtener valores de x, y lons = np.array(dataProcesamiento['Lon']) lats = np.array(dataProcesamiento['Lat']) # iniciar la gráfica #plt.clf() # agregar locación de estaciones xC = np.array(dataEstaciones['Long']) yC = np.array(dataEstaciones['Lat']) m = Basemap(projection='mill',llcrnrlat=LAT_MIN,urcrnrlat=LAT_MAX,llcrnrlon=LONG_MIN,urcrnrlon=LONG_MAX,resolution='h') # generar lats, lons x, y = m(lons, lats) # numero de columnas y filas numCols = len(x) numRows = len(y) # generar xi, yi xi = np.linspace(x.min(), x.max(), numCols) yi = np.linspace(y.min(), y.max(), numRows) # generar el meshgrid xi, yi = np.meshgrid(xi, yi) # generar zi z = np.array(dataProcesamiento[columna]) zi = gd((x,y), z, (xi,yi), method='cubic') # generar clevs stepVariable = 1 step = (z.max() - z.min()) / 10 # verificar el valor del intervalo if step <= 1: stepVariable = 1 clevs = np.linspace(z.min(), z.max() + stepVariable , 10) #clevs = [1,2,3,4,5,6,7,8,9,10] # contour plot cs = m.contourf(xi,yi,zi, clevs, zorder=5, alpha=0.5, cmap='PuBu') # agregar archivo shape de estados m.readshapefile('../shapes/Estados', 'Estados') # agregar puntos de estaciones m.scatter(xC, yC, latlon=True,s=1, marker='o', color='r', zorder=25) # colorbar cbar = m.colorbar(cs, location='right', pad="5%") cbar.set_label('mm') tituloTemporalParaElMapa = "{} {}-{:02d}-{:02d}".format(columna,anio,mes,dia) (tituloTemporalParaElMapa) # Mac /Users/jorgemauricio/Documents/Research/proyectoGranizo/Maps/{}_{}.png # Linux /home/jorge/Documents/Research/proyectoGranizo/Maps/{}_{}.png nombreTemporalParaElMapa = "{}/estaciones/maps/{}-{:02d}-{:02d}/{}.png".format(RUTA_CARPETA, anio, mes, dia, columna) plt.annotate('@2018 INIFAP', xy=(-109,29), xycoords='figure fraction', xytext=(0.45,0.45), color='g', zorder=50) plt.savefig(nombreTemporalParaElMapa, dpi=300) print('****** Genereate: {}'.format(nombreTemporalParaElMapa))
eeftab1[0]['EFF'], fill_value='extrapolate') arfcorr_aper[id_en] = coeff0 * eeffunc0( radius_arcmin) + coeff1 * eeffunc1(radius_arcmin) # Vignetting correction # starting at unity, compute if offaxis is not 0. arfcorr_vig = np.zeros(len(arftab), dtype=float) + 1. if offaxis != 0.: for i in progressbar(range(len(arfen))): energy = arfen[i] if (energy >= vig_elo[0]) and (energy <= vig_ehi[-1]): grid_e, grid_th = np.meshgrid(np.array([energy]), vig_theta) points = np.transpose( [np.tile(vig_emed, len(vig_theta)), \ np.repeat(vig_theta, len(vig_emed))] ) values = vig_vig.flatten() int_vig = gd(points, values, (grid_e, grid_th), method='nearest').flatten() # define a vignetting function which takes an off-axis angle in arcmin # and returns a vignetting fraction vigfunc = interp1d(vig_theta * 60., int_vig) arfcorr_vig[ i] = coeff0 * vigfunc(theta0) + coeff1 * vigfunc(theta1) arfhdu[1].data['SPECRESP'] = arftab['SPECRESP'] * arfcorr_vig * arfcorr_aper arfhdu[0].header.add_history( 'Applied aperture and off-axis corrections using martmkarf.py') arfhdu.writeto(out, overwrite=overwrite)
def _plot_energy(self, num_samples=4, path_length=20): """ Plots the energy function of the network. num_samples The number of samples to be used in the computation of the energy function. The greater the number of samples, the higher the accuracy of the resultant plot. path_length The number of steps to compute in calculating each sample's path of convergence toward the network's attractors. """ attractors = self.training_data states = [[np.random.choice([-1, 1]) for i in range(self.num_neurons)] for j in range(num_samples)] self.pca = PCA(n_components=2) self.pca.fit(attractors) paths = [attractors] for i in range(path_length): states = self.recall(states, steps=1) paths.append(states) x = y = np.linspace(-1, 1, 100) X, Y = np.meshgrid(x, y) meshpts = np.array([[x, y] for x, y in zip(np.ravel(X), np.ravel(Y))]) mesh = self.pca.inverse_transform(meshpts) grid = np.vstack((mesh, np.vstack(paths))) energies = np.array([self.energy(point) for point in grid]) grid = self.pca.transform(grid) gmin, gmax = grid.min(), grid.max() xi, yi = np.mgrid[gmin:gmax:100j, gmin:gmax:100j] zi = gd(grid, energies, (xi, yi), method='nearest') wireframe = self.energy_diagram.add_layer("wireframe") mesh_plot = self.energy_diagram.add_layer("mesh_plot") attracts = self.energy_diagram.add_layer("attractors") wireframe.add_data(xi, yi, zi) mesh_plot.add_data(xi, yi, zi) self.energy_diagram.build_layer( wireframe.name, plot=self.energy_diagram.plot_wireframe, colors=(0.5, 0.5, 0.5, 0.5), alpha=0.2) self.energy_diagram.build_layer(mesh_plot.name, plot=self.energy_diagram.plot_surface, cmap=cm.coolwarm) self.contour_diagram.contour(xi, yi, zi) grid = self.pca.transform(attractors) z = np.array([self.energy(state) for state in attractors]) attracts.add_data(grid[:, 0], grid[:, 1], z) self.energy_diagram.build_layer("attractors", plot=self.energy_diagram.scatter, s=80, c='g', marker='o') wireframe.hide() attracts.hide() def wireframe_click(event): wireframe.toggle_display() mesh_plot.toggle_display() def attractor_click(event): attracts.toggle_display() self.view_wfbutton.on_clicked(wireframe_click) self.view_attractbutton.on_clicked(attractor_click)
def interpolation(data): gridx, gridy = np.mgrid[xmin:xmax:resh, ymin:ymax:resh] gridz = gd(data[:, :2],data[:, 2], (gridx, gridy), method=interpolationmethod) return gridx, gridy, gridz
zold = Z[0] for zv in Z: if zv != zold: dzstep = abs(zold - zv) break print("Steps: ", dxstep, dzstep, dystep) #print(" ", X[1]-X[0], Y[1]-Y[0], Z[1]-Z[0]) #for i, x in enumerate(X): # print(x, Y[i], Z[i]) print("Before fit Min Max: ", min(s), max(s)) print("Interpolate...") S = gd((xs, ys, zs), s, (X, Y, Z), fill_value=0.0, method='nearest') #S = itp((X,Y,Z), s, (xs,ys,zs), fill_value=0.0, method='linear') print("After fit Min Max: ", min(S), max(S)) #for v in S: # print (v) print("") #s = np.sin(x*y*z)/(x*y*z) S = S.reshape(N, N, N) #print(S.shape, type(S)) g = Grid(S, origin=[min(X), min(Y), min(Z)], \ delta=[dxstep, dystep, dzstep])
def iniciarProcesamiento(): # Mac /Users/jorgemauricio/Documents/Research/proyectoCaborca # Linux /home/jorge/Documents/Research/proyectoCaborca URL_CARPETA = "/Users/jorgemauricio/Documents/Research/proyectoCaborca" # ruta para acceder a los archivos shapes# nombre de la ruta para shapes rutaParaArchivosShapes = '{}/shapes/Estados'.format(URL_CARPETA) # coordenadas estaciones dataEstaciones = pd.read_csv( "/Users/jorgemauricio/Documents/Research/proyectoCaborca/data/coordenadas_estaciones.csv" ) # fecha actual fechaActual = strftime("%Y-%m-%d") # fecha -1 anio, mes, dia = generarDiaAnterior(fechaActual) # nombre de la ruta para la descarga rutaDeCarpetaParaElProcesamiento = '{}/data/{}-{:02d}-{:02d}'.format( URL_CARPETA, anio, mes, dia) # constantes LONG_MIN = -115.65 LONG_MAX = -107.94 LAT_MIN = 25.41 LAT_MAX = 33.06 # archivos a procesar listaDeArchivos = [ x for x in os.listdir(rutaDeCarpetaParaElProcesamiento) if x.endswith('.nc') ] # ciclo de procesamiento for archivo in listaDeArchivos: # nombre del archivo # nombreArchivo = "GBBEPx.emis_so2.001.20180118.nc" arrayNombreArchivo = archivo.split(".") arrayComponente = arrayNombreArchivo[1].split("_") nombreParaMapa = arrayComponente[1] rutaArchivo = "{}/{}".format(rutaDeCarpetaParaElProcesamiento, archivo) # leer el archivo netcdf dataset = Dataset(rutaArchivo) # generar las arreglos de las variables biomass = dataset.variables['biomass'][:] Latitude = dataset.variables['Latitude'][:] Longitude = dataset.variables['Longitude'][:] # variable para generar CSV dataText = "Long,Lat,Biomass\n" # procesamiento de información for i in range(Longitude.shape[0]): for j in range(Latitude.shape[0]): tempText = "{},{},{}\n".format(Longitude[i], Latitude[j], biomass[0, j, i]) dataText += tempText # generar archivo temporal csv fileName = "{}/temp/{}-{:02d}-{:02d}/{}.csv".format( URL_CARPETA, anio, mes, dia, nombreParaMapa) textFile = open(fileName, "w") textFile.write(dataText) textFile.close() # leer el archivo temporal csv data = pd.read_csv(fileName) # limites longitud > -115.65 y < -107.94 data = data.loc[data['Long'] > LONG_MIN] data = data.loc[data['Long'] < LONG_MAX] # limites latitud > 25.41 y < 33.06 data = data.loc[data['Lat'] > LAT_MIN] data = data.loc[data['Lat'] < LAT_MAX] # ug/m3 a ppm data['Biomass'] = data['Biomass'] * 10000000000 # obtener valores de x, y lons = np.array(data['Long']) lats = np.array(data['Lat']) #%% iniciar la gráfica plt.clf() # agregar locación de estaciones xC = np.array(dataEstaciones['Long']) yC = np.array(dataEstaciones['Lat']) m = Basemap(projection='mill', llcrnrlat=LAT_MIN, urcrnrlat=LAT_MAX, llcrnrlon=LONG_MIN, urcrnrlon=LONG_MAX, resolution='h') # generar lats, lons x, y = m(lons, lats) # numero de columnas y filas numCols = len(x) numRows = len(y) # generar xi, yi xi = np.linspace(x.min(), x.max(), numCols) yi = np.linspace(y.min(), y.max(), numRows) # generar el meshgrid xi, yi = np.meshgrid(xi, yi) # generar zi z = np.array(data['Biomass']) zi = gd((x, y), z, (xi, yi), method='cubic') # generar clevs stepVariable = 1 step = (z.max() - z.min()) / 10 # verificar el valor del intervalo if step <= 1: stepVariable = 1 clevs = np.linspace(z.min(), z.max() + stepVariable, 10) #clevs = [1,2,3,4,5,6,7,8,9,10] # contour plot cs = m.contourf(xi, yi, zi, clevs, zorder=5, alpha=0.5, cmap='PuBu') # agregar archivo shape de estados m.readshapefile(rutaParaArchivosShapes, 'Estados') # agregar puntos de estaciones m.scatter(xC, yC, latlon=True, s=1, marker='o', color='r', zorder=25) # colorbar cbar = m.colorbar(cs, location='right', pad="5%") cbar.set_label('pm') tituloTemporalParaElMapa = "{} {}-{:02d}-{:02d}".format( nombreParaMapa, anio, mes, dia) plt.title(tituloTemporalParaElMapa) # Mac /Users/jorgemauricio/Documents/Research/proyectoGranizo/Maps/{}_{}.png # Linux /home/jorge/Documents/Research/proyectoGranizo/Maps/{}_{}.png nombreTemporalParaElMapa = "/Users/jorgemauricio/Documents/Research/proyectoCaborca/maps/{}-{:02d}-{:02d}/{}.png".format( anio, mes, dia, nombreParaMapa) plt.annotate('@2018 INIFAP', xy=(-109, 29), xycoords='figure fraction', xytext=(0.45, 0.45), color='g', zorder=50) plt.savefig(nombreTemporalParaElMapa, dpi=300) print('****** Genereate: {}'.format(nombreTemporalParaElMapa))
def interpolation(self, data): gridx, gridy = np.mgrid[1:self.front_num:50j, 1:self.end_num:50j] gridz = gd(data[:, :2], data[:, 2], (gridx, gridy), method=self.interpol_method) return gridx, gridy, gridz
x, y = m(lons,lats) #%% number of cols and rows numcols = len(x) numrows = len(y) #%% generate xi, yi xi = np.linspace(x.min(), x.max(), numcols) yi = np.linspace(y.min(), y.max(), numrows) #%% generate meshgrid xi, yi = np.meshgrid(xi,yi) #%% genate zi z = np.array(data['Rain']) zi = gd((x,y), z, (xi,yi), method='linear') #%% contour plot cs = m.contourf(xi,yi,zi, zorder=4, alpha=0.5, cmap='RdPu') #%% draw map details m.drawcoastlines() m.drawstates(linewidth=0.7) m.drawcountries() #m.drawmapscale(22, -103, 23, -102, 100, units='km', fontsize=14, yoffset=None, barstyle='fancy', labelstyle='simple', fillcolor1='w', fillcolor2='#000000',fontcolor='#000000', zorder=5) #%% # add colour bar and title cbar = m.colorbar(cs, location='right', pad="5%") cbar.set_label('mm') plt.title('Precipitación') plt.savefig('maps/precipitacion.png', dpi=300, transparent=True) plt.show()
def contour(coordinates, scores, world=False, filename="contour", do_contour=False, **kwargs): #with open('./data/coordinate_socres.pkl', 'wb') as fout: # pickle.dump((coordinates, scores), fout) with open('./data/coor_score_239.pkl', 'rb') as fin: coordinates, scores = pickle.load(fin) from matplotlib import rc rc('font', **{'family': 'sans-serif', 'sans-serif': ['Helvetica']}) ## for Palatino and other serif fonts use: #rc('font',**{'family':'serif','serif':['Palatino']}) rc('text', usetex=True) scores = np.array(scores) lllat = 24.396308 lllon = -124.848974 urlat = 49.384358 urlon = -66.885444 if world: lllat = -90 lllon = -180 urlat = 90 urlon = 180 fig = plt.figure(figsize=(2.5, 2)) grid_transform = kwargs.get('grid', False) ax = fig.add_subplot(111, axisbg='w', frame_on=False) grid_interpolation_method = 'nearest' #scores = np.log(scores) m = Basemap(llcrnrlat=lllat, urcrnrlat=urlat, llcrnrlon=lllon, urcrnrlon=urlon, resolution='i', projection='cyl') m.drawmapboundary(fill_color='white') #m.drawcoastlines(linewidth=0.2) m.drawcountries(linewidth=0.2) if world: m.drawstates(linewidth=0.2, color='lightgray') #m.fillcontinents(color='white', lake_color='#0000ff', zorder=2) #m.drawrivers(color='#0000ff') #m.drawlsmask(land_color='gray',ocean_color="#b0c4de", lakes=True) #m.drawcounties() shp_info = m.readshapefile('./data/us_states_st99/st99_d00', 'states', drawbounds=True, zorder=0) printed_names = [] ax = plt.gca() ax.xaxis.set_visible(False) ax.yaxis.set_visible(False) for spine in ax.spines.itervalues(): spine.set_visible(False) state_names_set = set(short_state_names.values()) mi_index = 0 wi_index = 0 for shapedict, state in zip(m.states_info, m.states): if world: break draw_state_name = True if shapedict['NAME'] not in state_names_set: continue short_name = short_state_names.keys()[short_state_names.values().index( shapedict['NAME'])] if short_name in printed_names and short_name not in ['MI', 'WI']: continue if short_name == 'MI': if mi_index != 3: draw_state_name = False mi_index += 1 if short_name == 'WI': if wi_index != 2: draw_state_name = False wi_index += 1 # center of polygon x, y = np.array(state).mean(axis=0) hull = ConvexHull(state) hull_points = np.array(state)[hull.vertices] x, y = hull_points.mean(axis=0) if short_name == 'MD': y = y - 0.5 x = x + 0.5 elif short_name == 'DC': y = y + 0.1 elif short_name == 'MI': x = x - 1 elif short_name == 'RI': x = x + 1 y = y - 1 #poly = MplPolygon(state,facecolor='lightgray',edgecolor='black') #x, y = np.median(np.array(state), axis=0) # You have to align x,y manually to avoid overlapping for little states if draw_state_name: plt.text(x + .1, y, short_name, ha="center", fontsize=4) #ax.add_patch(poly) #pdb.set_trace() printed_names += [ short_name, ] mlon, mlat = m(*(coordinates[:, 1], coordinates[:, 0])) # grid data if do_contour: numcols, numrows = 2000, 2000 xi = np.linspace(mlon.min(), mlon.max(), numcols) yi = np.linspace(mlat.min(), mlat.max(), numrows) xi, yi = np.meshgrid(xi, yi) # interpolate x, y, z = mlon, mlat, scores #pdb.set_trace() #zi = griddata(x, y, z, xi, yi) zi = gd((mlon, mlat), scores, (xi, yi), method=grid_interpolation_method, rescale=False) #Remove the lakes and oceans data = maskoceans(xi, yi, zi) con = m.contourf(xi, yi, data, cmap=plt.get_cmap('YlOrRd')) else: cmap = plt.get_cmap('YlOrRd') con = m.scatter(mlon, mlat, c=scores, s=3, cmap=cmap) #con = m.contour(xi, yi, data, 3, cmap=plt.get_cmap('YlOrRd'), linewidths=1) #con = m.contour(x, y, z, 3, cmap=plt.get_cmap('YlOrRd'), tri=True, linewidths=1) #conf = m.contourf(x, y, z, 3, cmap=plt.get_cmap('coolwarm'), tri=True) cbar = m.colorbar(con, location='right', pad="3%") #plt.setp(cbar.ax.get_yticklabels(), visible=False) #cbar.ax.tick_params(axis=u'both', which=u'both',length=0) #cbar.ax.set_yticklabels(['low', 'high']) #tick_locator = ticker.MaxNLocator(nbins=9) #cbar.locator = tick_locator #cbar.update_ticks() cbar.ax.tick_params(labelsize=6) cbar.ax.xaxis.set_tick_params(pad=0) cbar.ax.yaxis.set_tick_params(pad=0) cbar.set_label('error in km', size=8, labelpad=1) for line in cbar.lines: line.set_linewidth(20) #read countries for world dataset with more than 100 number of users with open('./data/country_count.json', 'r') as fin: top_countries = set(json.load(fin)) world_shp_info = m.readshapefile( './data/CNTR_2014_10M_SH/Data/CNTR_RG_10M_2014', 'world', drawbounds=False, zorder=100) for shapedict, state in zip(m.world_info, m.world): if not world: if shapedict['CNTR_ID'] not in ['CA', 'MX']: continue else: if shapedict['CNTR_ID'] in top_countries: continue poly = MplPolygon(state, facecolor='gray', edgecolor='gray') ax.add_patch(poly) #plt.title('term: ' + word ) plt.tight_layout() plt.savefig('./maps/' + filename + '.pdf', bbox_inches='tight') plt.close() del m
def mapasExtremos(): """ Función que permite generar los mapas de eventos extremos """ # ********** fecha pronóstico # fechaPronostico = fp fechaPronostico = "2018-02-21" # fechaPronostico = strftime("%Y-%m-%d") # ********** path # path server # path = "/home/jorge/Documents/work/autoPronosticoSonora" # os.chdir(path) # path local # ********* Lat y Long LONG_MAX = -86.1010 LONG_MIN = -118.2360 LAT_MAX = 33.5791 LAT_MIN = 12.37 # ********** Path path = "/home/jorge/Documents/Research/alermapweb" os.chdir(path) # ********** dict de análisis d = {"Rain" : '20/500', "Tmax": '30/60', "Tmin" : '-9/6', "Windpro" : '62/150'} # ********** array colores # generar fechas mediante función arrayFechas = generarFechas(fechaPronostico) # leer csv for j in range(1, 6, 1): pathFile = '{}/data/{}/d{}.txt'.format(path,fechaPronostico,j) data = pd.read_table(pathFile, sep=',') for key, i in d.items(): # comenzar con el proceso tiempoInicio = strftime("%Y-%m-%d %H:%M:%S") print("Empezar procesamiento {} {} tiempo: {}".format(key, i, tiempoInicio)) # obtener rangos vMin, vMax = i.split('/') vMin = int(vMin) vMax = int(vMax) # título temporal de la columna a procesar tituloTemporalColumna = key dataTemp = data #obtener valores de x y y lons = np.array(dataTemp['Long']) lats = np.array(dataTemp['Lat']) #%% set up plot plt.clf() fig = plt.figure(figsize=(8,4)) m = Basemap(projection='mill',llcrnrlat=LAT_MIN,urcrnrlat=LAT_MAX,llcrnrlon=LONG_MIN,urcrnrlon=LONG_MAX,resolution='h', area_thresh = 10000) # # # # # # # # # # # generar lats, lons x, y = m(lons, lats) # numero de columnas y filas numCols = len(x) numRows = len(y) # generar xi, yi xi = np.linspace(x.min(), x.max(), 1000) yi = np.linspace(y.min(), y.max(), 1000) # generar el meshgrid xi, yi = np.meshgrid(xi, yi) # generar zi z = np.array(dataTemp[key]) zi = gd((x,y), z, (xi,yi), method='cubic') #zi = gd((x,y), z, (xi,yi)) # agregar shape m.readshapefile('shapes/Estados', 'Estados') # clevs clevs = generarRangos(key) # contour plot cs = m.contourf(xi,yi,zi, clevs, zorder=25, alpha=0.7, cmap=generarMapaDeColores(key)) # colorbar cbar = m.colorbar(cs, location='right', pad="5%") # simbología simbologia = generarSimbologia(key) cbar.set_label(simbologia) # titulo del mapa tituloTemporalMapa = generarTexto(arrayFechas[j-1], key, vMin, vMax) plt.title(tituloTemporalMapa) tituloTemporalArchivo = "{}/data/{}/{}_{}_{}_{}.png".format(path,fechaPronostico,arrayFechas[j-1],key,vMin, vMax) # crear anotación latitudAnotacion = (LAT_MAX + LAT_MIN) / 2 longitudAnotacion = (LONG_MAX + LONG_MIN) / 2 plt.annotate('@2018 INIFAP', xy=(longitudAnotacion,latitudAnotacion), xycoords='figure fraction', xytext=(0.45,0.45), color='g', zorder=50) # guardar mapa plt.savefig(tituloTemporalArchivo, dpi=300) print('****** Genereate: {}'.format(tituloTemporalArchivo)) # finalizar con el proceso tiempoFinal = strftime("%Y-%m-%d %H:%M:%S") print("Terminar procesamiento {} {} tiempo: {}".format(key, i, tiempoInicio))
def map_words(coords, preds, vocab, map_dir, dataset_name): """ given the coords distributed over the map and the unigram distribution over vocabulary pred, contourf the logprob of a word over the map with interpolation. """ lllat = 24.396308 lllon = -124.848974 urlat = 49.384358 urlon = -66.885444 if dataset_name == 'world-final': lllat = -90 lllon = -180 urlat = 90 urlon = 180 grid_interpolation_method = 'cubic' logging.info('interpolation: ' + grid_interpolation_method) region_words = { "the north":['braht','breezeway','bubbler','clout','davenport','euchre','fridge','hotdish','paczki','pop','sack','soda','toboggan','Yooper'], "northeast":['brook','cellar','sneaker','soda'], "New England":['grinder','packie','rotary','wicked'], "Eastern New England":['bulkhead','Cabinet','frappe','hosey','intervale','jimmies','johnnycake','quahog','tonic'], "Northern New England":['ayuh','creemee','dooryard','logan','muckle'], "The Mid-Atlantic":['breezeway','hoagie','jawn','jimmies','parlor','pavement','shoobie','youze'], "New York City Area":['bodega','dungarees','potsy','punchball','scallion','stoop','wedge'], "The Midland":['hoosier'], "The South":['banquette','billfold','chuck','commode','lagniappe','yankee','yonder'], "The West":['davenport','Hella','snowmachine' ] } word_dialect = {} with open('./data/geodare.cleansed.filtered.json', 'r') as fin: for line in fin: line = line.strip() dialect_word = json.loads(line) word_dialect[dialect_word['word']] = dialect_word['dialect'] #if os.path.exists(map_dir): # shutil.rmtree(map_dir) try: os.mkdir(map_dir) except: logging.info('map_dir %s exists or can not be created.') #pick some words to map including some known dialect words #some DARE words and some words that are not evenly distributed topk_words = [] for words in region_words.values(): topk_words.extend(words) topk_words.extend(word_dialect.keys()) dialect_words = ['hella', 'yall', 'jawn', 'paczki', 'euchre', 'brat', 'toboggan', 'brook', 'grinder', 'yinz', 'youze', 'yeen'] topk_words.extend(dialect_words) custom_words = ['springfield', 'columbia', 'n***a', 'niqqa', 'bamma', 'cooter', 'britches', 'yapper', 'younguns', 'hotdish', 'schnookered', 'bubbler', 'betcha', 'dontcha'] topk_words.extend(custom_words) vocabset = set(vocab) dare_in_vocab = set(word_dialect.keys()) & vocabset logging.info('%d DARE words, %d in vocab' %(len(word_dialect), len(dare_in_vocab))) add_local_words = True if add_local_words: ne_file = './dumps/ne_' + dataset_name + '.json' with codecs.open(ne_file, 'r', encoding='utf-8') as fout: NEs = json.load(fout) NEs = NEs['nes'] local_words = get_local_words(preds, vocab, NEs=NEs, k=500) logging.info(local_words) topk_words.extend(local_words[0:20]) add_cities = False if add_cities: with open('./data/cities.json', 'r') as fin: cities = json.load(fin) cities = cities[0:100] for city in cities: name = city['city'].lower() topk_words.append(name) wi = 0 for word in topk_words: if word in vocabset: fig = plt.figure(figsize=(5, 4)) ax = fig.add_subplot(111, axisbg='w', frame_on=False) logging.info('%d mapping %s' %(wi, word)) wi += 1 index = vocab.index(word) scores = np.log(preds[:, index]) m = Basemap(llcrnrlat=lllat, urcrnrlat=urlat, llcrnrlon=lllon, urcrnrlon=urlon, resolution='i', projection='cyl') ''' m = Basemap(llcrnrlon=-119,llcrnrlat=22,urcrnrlon=-64,urcrnrlat=49, projection='lcc',lat_1=33,lat_2=45,lon_0=-95, resolution='i') ''' m.drawmapboundary(fill_color = 'white') #m.drawcoastlines(linewidth=0.2) m.drawcountries(linewidth=0.2) if dataset_name != 'world-fianl': m.drawstates(linewidth=0.2, color='lightgray') #m.fillcontinents(color='white', lake_color='#0000ff', zorder=2) #m.drawrivers(color='#0000ff') #m.drawlsmask(land_color='gray',ocean_color="#b0c4de", lakes=True) #m.drawcounties() shp_info = m.readshapefile('./data/us_states_st99/st99_d00','states',drawbounds=True, zorder=0) printed_names = [] ax = plt.gca() ax.xaxis.set_visible(False) ax.yaxis.set_visible(False) for spine in ax.spines.itervalues(): spine.set_visible(False) state_names_set = set(short_state_names.values()) mi_index = 0 wi_index = 0 for shapedict,state in zip(m.states_info, m.states): if dataset_name == 'world-final': break draw_state_name = True if shapedict['NAME'] not in state_names_set: continue short_name = short_state_names.keys()[short_state_names.values().index(shapedict['NAME'])] if short_name in printed_names and short_name not in ['MI', 'WI']: continue if short_name == 'MI': if mi_index != 3: draw_state_name = False mi_index += 1 if short_name == 'WI': if wi_index != 2: draw_state_name = False wi_index += 1 # center of polygon x, y = np.array(state).mean(axis=0) hull = ConvexHull(state) hull_points = np.array(state)[hull.vertices] x, y = hull_points.mean(axis=0) if short_name == 'MD': y = y - 0.5 x = x + 0.5 elif short_name == 'DC': y = y + 0.1 elif short_name == 'MI': x = x - 1 elif short_name == 'RI': x = x + 1 y = y - 1 #poly = MplPolygon(state,facecolor='lightgray',edgecolor='black') #x, y = np.median(np.array(state), axis=0) # You have to align x,y manually to avoid overlapping for little states if draw_state_name: plt.text(x+.1, y, short_name, ha="center", fontsize=8) #ax.add_patch(poly) #pdb.set_trace() printed_names += [short_name,] mlon, mlat = m(*(coords[:,1], coords[:,0])) # grid data numcols, numrows = 1000, 1000 xi = np.linspace(mlon.min(), mlon.max(), numcols) yi = np.linspace(mlat.min(), mlat.max(), numrows) xi, yi = np.meshgrid(xi, yi) # interpolate x, y, z = mlon, mlat, scores #pdb.set_trace() #zi = griddata(x, y, z, xi, yi) zi = gd( (mlon, mlat), scores, (xi, yi), method=grid_interpolation_method, rescale=False) #Remove the lakes and oceans data = maskoceans(xi, yi, zi) con = m.contourf(xi, yi, data, cmap=plt.get_cmap('YlOrRd')) #con = m.contour(xi, yi, data, 3, cmap=plt.get_cmap('YlOrRd'), linewidths=1) #con = m.contour(x, y, z, 3, cmap=plt.get_cmap('YlOrRd'), tri=True, linewidths=1) #conf = m.contourf(x, y, z, 3, cmap=plt.get_cmap('coolwarm'), tri=True) cbar = m.colorbar(con,location='right',pad="2%") #plt.setp(cbar.ax.get_yticklabels(), visible=False) #cbar.ax.tick_params(axis=u'both', which=u'both',length=0) #cbar.ax.set_yticklabels(['low', 'high']) tick_locator = ticker.MaxNLocator(nbins=9) cbar.locator = tick_locator cbar.update_ticks() cbar.ax.tick_params(labelsize=11) cbar.ax.yaxis.set_tick_params(pad=2) cbar.set_label('logprob', size=11) for line in cbar.lines: line.set_linewidth(10) #read countries for world dataset with more than 100 number of users with open('./data/country_count.json', 'r') as fin: top_countries = set(json.load(fin)) world_shp_info = m.readshapefile('./data/CNTR_2014_10M_SH/Data/CNTR_RG_10M_2014','world',drawbounds=False, zorder=100) for shapedict,state in zip(m.world_info, m.world): if dataset_name != 'world-final': if shapedict['CNTR_ID'] not in ['CA', 'MX']: continue else: if shapedict['CNTR_ID'] in top_countries: continue poly = MplPolygon(state,facecolor='gray',edgecolor='gray') ax.add_patch(poly) #plt.title('term: ' + word ) plt.tight_layout() filename = '{}{}_{}.pdf'.format(map_dir, word.encode('utf-8'), grid_interpolation_method) plt.savefig(filename, bbox_inches='tight') plt.close() del m
def main(): """ Función que genera los mapas de temperatura mínima """ startProcess = strftime("%Y-%m-%d %H:%M:%S") # #%% fecha del pronostico # # fechaPronostico = strftime("%Y-%m-%d") fechaPronostico = "2018-04-25" variables = ["Rain", "Tmax", "Tmin", "Tpro", "Hr", "Hrmin", "Hrmax"] LAT_MAX = 33.5791 LAT_MIN = 12.3782 LONG_MAX = -86.101 LONG_MIN = -118.236 #%% generate arrayFechas # Generate Days arrayFechas = [] tanio, tmes, tdia = fechaPronostico.split('-') anio = int(tanio) mes = int(tmes) dia = int(tdia) dirAnio = anio dirMes = mes dirDia = dia #%% generate arrayFechas for i in range(0, 5, 1): if i == 0: newDiaString = '{}'.format(dia) if len(newDiaString) == 1: newDiaString = '0' + newDiaString newMesString = '{}'.format(mes) if len(newMesString) == 1: newMesString = '0' + newMesString fecha = '{}'.format(anio) + "-" + newMesString + "-" + newDiaString arrayFechas.append(fecha) if i > 0: dia = dia + 1 if mes == 2 and anio % 4 == 0: diaEnElMes = 29 elif mes == 2 and anio % 4 != 0: diaEnElMes = 28 elif mes == 1 or mes == 3 or mes == 5 or mes == 7 or mes == 8 or mes == 10 or mes == 12: diaEnElMes = 31 elif mes == 4 or mes == 6 or mes == 9 or mes == 11: diaEnElMes = 30 if dia > diaEnElMes: mes = mes + 1 dia = 1 if mes > 12: anio = anio + 1 mes = 1 newDiaString = '{}'.format(dia) if len(newDiaString) == 1: newDiaString = '0' + newDiaString newMesString = '{}'.format(mes) if len(newMesString) == 1: newMesString = '0' + newMesString fecha = '{}'.format(anio) + "-" + newMesString + "-" + newDiaString arrayFechas.append(fecha) # path server path = "/home/jorge/Documents/Research/generar_boletin" # os.chdir("/home/jorge/Documents/work/autoPronosticoSonora") os.chdir(path) #%% read csvs pathFile1 = '{}/data/{}/d1.txt'.format(path, fechaPronostico) pathFile2 = '{}/data/{}/d2.txt'.format(path, fechaPronostico) pathFile3 = '{}/data/{}/d3.txt'.format(path, fechaPronostico) pathFile4 = '{}/data/{}/d4.txt'.format(path, fechaPronostico) pathFile5 = '{}/data/{}/d5.txt'.format(path, fechaPronostico) data1 = pd.read_table(pathFile1, sep=',') data2 = pd.read_table(pathFile2, sep=',') data3 = pd.read_table(pathFile3, sep=',') data4 = pd.read_table(pathFile4, sep=',') data5 = pd.read_table(pathFile5, sep=',') cols = [ "Long", "Lat", "Graupel", "Hail", "Rain", "Tmax", "Tmin", "Tpro", "Dpoint", "Hr", "Windpro", "WindDir", "Hrmin", "Hrmax", "TprSoil0_10", "TprSoil10_40", "WprSoil0_10", "WprSoil10_40" ] data1.columns = cols data2.columns = cols data3.columns = cols data4.columns = cols data5.columns = cols # ciclo para generar los mapas de cada una de las variables for variable in variables: #%% make one dataFrame data = data1.filter(items=['Long', 'Lat', variable]) data['{}1'.format(variable)] = data1[variable] data['{}2'.format(variable)] = data2[variable] data['{}3'.format(variable)] = data3[variable] data['{}4'.format(variable)] = data4[variable] data['{}5'.format(variable)] = data5[variable] #%% get values from Ags data = data.loc[data['Lat'] >= LAT_MIN] data = data.loc[data['Lat'] <= LAT_MAX] data = data.loc[data['Long'] >= LONG_MIN] data = data.loc[data['Long'] <= LONG_MAX] print(data.head()) #%% get x and y values lons = np.array(data['Long']) lats = np.array(data['Lat']) #%% loop diarios counterFecha = 0 for i in range(1, 6, 1): #%% set up plot plt.clf() #fig = plt.figure(figsize=(48,24)) m = Basemap(projection='mill', llcrnrlat=LAT_MIN, urcrnrlat=LAT_MAX, llcrnrlon=LONG_MIN, urcrnrlon=LONG_MAX, resolution='h') #%% generate lats, lons x, y = m(lons, lats) #%% number of cols and rows numcols = len(x) numrows = len(y) #%% generate xi, yi xi = np.linspace(x.min(), x.max(), 1000) yi = np.linspace(y.min(), y.max(), 1000) #%% generate meshgrid xi, yi = np.meshgrid(xi, yi) #%% genate zi tempTitleColumn = "{}{}".format(variable, i) z = np.array(data[tempTitleColumn]) zi = gd((x, y), z, (xi, yi), method='cubic') #%% generate clevs #generate clevs step = (z.max() - z.min()) / 15 if variable == "Rain": clevs = [ 1, 5, 10, 20, 30, 50, 70, 100, 150, 200, 250, 300, 350, 400, 500 ] else: clevs = np.linspace(z.min(), z.max(), 15) cmap_variable = colores_por_variable(variable) #%% contour plot cs = m.contourf(xi, yi, zi, clevs, zorder=4, alpha=0.5, cmap=cmap_variable) #%% draw map details # m.drawcoastlines() # m.drawstates(linewidth=0.7) # m.drawcountries() #%% read municipios shape file m.readshapefile('shapes/Estados', 'Estados') #m.drawmapscale(22, -103, 23, -102, 100, units='km', fontsize=14, yoffset=None, barstyle='fancy', labelstyle='simple', fillcolor1='w', fillcolor2='#000000',fontcolor='#000000', zorder=5) #%% colorbar cbar = m.colorbar(cs, location='right', pad="5%") # simbología colorbar # simbologia = generar_simbologia(variable) # cbar.set_label(simbologia) titulo_mapa = generarTexto(variable, arrayFechas[counterFecha]) plt.title(titulo_mapa) titulo_archivo = "{}/data/{}/{}_{}.png".format( path, fechaPronostico, variable, i) plt.annotate('@2018 INIFAP', xy=(-102, 22), xycoords='figure fraction', xytext=(0.45, 0.45), color='g') plt.savefig(titulo_archivo, dpi=300, bbox_inches='tight') counterFecha += 1 print('****** Genereate: {}'.format(titulo_archivo)) # generar pdf """ GENERAR GRÁFICA y1 = [] y2 = [] for k in range(1,6): y1.append(data["{}{}".format(variable, k)].max()) y2.append(data["{}{}".format(variable, k)].min()) ind = np.arange(5) fig, ax = plt.subplots() width = 0.35 rects1 = ax.bar(ind, y1, width, color='darkred') rects2 = ax.bar(ind + width, y2, width, color='darkblue') # add some text for labels, title and axes ticks # ax.set_ylabel(simbologia) # ax.set_title(variable) ax.set_xticks(ind + width / 2) ax.set_xticklabels(arrayFechas) # ax.legend((rects1[0], rects2[0]), ('Máximo', 'Mínimo'), loc=3) def autolabel(rects): # Attach a text label above each bar displaying its height for rect in rects: height = rect.get_height() ax.text(rect.get_x() + rect.get_width()/2., 1.05*height, '%d' % int(height), ha='center', va='bottom') autolabel(rects1) autolabel(rects2) titulo_grafica = "{}/data/{}/{}_grafica.png".format(path, fechaPronostico, variable) plt.savefig(titulo_grafica, dpi=600, bbox_inches='tight' ) """ nombre_archivo_pdf = "{}/data/{}/{}.pdf".format( path, fechaPronostico, variable) c = canvas.Canvas(nombre_archivo_pdf, pagesize=portrait(letter)) # logo inifap logo_inifap = "{}/images/inifap.jpg".format(path) c.drawImage(logo_inifap, 20, 700, width=100, height=100) # encabezado c.setFont("Helvetica", 20, leading=None) titulo_pdf = generar_titulo_pdf(variable) c.drawCentredString(350, 750, titulo_pdf) # imagen anuales imagen_1 = "{}/data/{}/{}_1.png".format(path, fechaPronostico, variable) imagen_2 = "{}/data/{}/{}_2.png".format(path, fechaPronostico, variable) imagen_3 = "{}/data/{}/{}_3.png".format(path, fechaPronostico, variable) imagen_4 = "{}/data/{}/{}_4.png".format(path, fechaPronostico, variable) imagen_5 = "{}/data/{}/{}_5.png".format(path, fechaPronostico, variable) imagen_6 = "{}/images/info_wrf.png".format(path) # draw images c.drawImage(imagen_1, 20, 475, width=260, height=172) c.drawImage(imagen_2, 325, 475, width=260, height=172) c.drawImage(imagen_3, 20, 250, width=260, height=172) c.drawImage(imagen_4, 325, 250, width=260, height=172) c.drawImage(imagen_5, 20, 25, width=260, height=172) c.drawImage(imagen_6, 325, 25, width=260, height=172) c.showPage() c.save() print(titulo_pdf) # generar boletin ruta_pdf = "{}/data/{}/".format(path, fechaPronostico) os.chdir(ruta_pdf) os.system( "pdftk Rain.pdf Tmax.pdf Tmin.pdf Tpro.pdf Hrmax.pdf Hrmin.pdf Hr.pdf cat output boletin.pdf" ) endProcess = strftime("%Y-%m-%d %H:%M:%S") print(startProcess) print(endProcess) access = claves() ftp = ftplib.FTP(access.ip) ftp.login(access.usr, access.pwd) #%% cambiar a directorio ftp.cwd('Content/documentos') # subir archivo fileName = "boletin.pdf" file = open(fileName, 'rb') comandoFTP = 'STOR boletin.pdf' # file to send ftp.storbinary(comandoFTP, file) # cerrar conexión file.close() # close file and FTP ftp.quit()
#%% number of cols and rows numcols = len(x) numrows = len(y) #%% generate xi, yi xi = np.linspace(x.min(), x.max(), numcols) yi = np.linspace(y.min(), y.max(), numrows) #%% generate meshgrid xi, yi = np.meshgrid(xi,yi) #%% genate zi tempTitleColumn = "Rain{}".format(i) z = np.array(data[tempTitleColumn]) zi = gd((x,y), z, (xi,yi), method='cubic') #%% generate clevs def generateClevs(minV, maxV): arrayValues = [] step = (maxV - minV) / 10 for i in range(10): rangeOfValue = int(step * i) arrayValues.append(rangeOfValue) return arrayValues clevs = generateClevs(z.min(), z.max()) #%% contour plot cs = m.contourf(xi,yi,zi, clevs, zorder=4, alpha=0.5, cmap='Spectral_r') #%% draw map details m.drawcoastlines()
data['projected_lon'], data['projected_lat'] = m(*(data.Lon.values, data.Lat.values)) # 生成经纬度的栅格数据 numcols, numrows = 1000, 1000 xi = np.linspace(data['projected_lon'].min(), data['projected_lon'].max(), numcols) yi = np.linspace(data['projected_lat'].min(), data['projected_lat'].max(), numrows) xi, yi = np.meshgrid(xi, yi) # 插值 x, y, z = data['projected_lon'].values, data[ 'projected_lat'].values, data.Z.values zi = gd((data[['projected_lon', 'projected_lat']]), data.Z.values, (xi, yi), method='cubic') # 设置地图细节 m.drawmapboundary(fill_color='white') m.fillcontinents(color='#C0C0C0', lake_color='#7093DB') m.drawcountries(linewidth=.75, linestyle='solid', color='#000073', antialiased=True, ax=ax, zorder=3) m.drawparallels(np.arange(lllat, urlat, 2.), color='black', linewidth=0.5,
Z = np.arange(ar_len, dtype=float) l = 0 for i in range(0, len(X1)): for j in range(0, len(Y1)): for k in range(0, len(Z1)): X[l] = X1[i] Y[l] = Y1[j] Z[l] = Z1[k] l = l + 1 print('time needed: ', time.clock() - start_time, ' seconds') print("") #interpolate "data.v" on new grid "X,Y,Z" print("Interpolate...") start_time = time.clock() V = gd((x, y, z), v, (X, Y, Z), method='linear') print('time needed: ', time.clock() - start_time, ' seconds') print("") #Plot original values fig1 = plt.figure() ax1 = fig1.gca(projection='3d') sc1 = ax1.scatter(x, y, z, c=v, cmap=plt.hot()) plt.colorbar(sc1) ax1.set_xlabel('X') ax1.set_ylabel('Y') ax1.set_zlabel('Z') #Plot interpolated values fig2 = plt.figure() ax2 = fig2.gca(projection='3d')
def interpolation(data): gridx, gridy = np.mgrid[5:10:50j, 0:1:50j] gridz = gd(data[:, :2],data[:, 2], (gridx, gridy), method=interpolationmethod) return gridx, gridy, gridz
def gen_mapas(df, fecha, cincodias): """Genera 5 mapas de cada respectivo día, y uno del pronóstico de los 5 días""" if not os.path.exists('mapas'): os.mkdir('mapas') os.chdir('mapas') if not os.path.exists('{}'.format(fecha)): os.mkdir('{}'.format(fecha)) os.chdir('{}'.format(fecha)) Long = np.array(df['Long']) Lat = np.array(df['Lat']) for i in range(1, 7): """Generación de los 5 mapas en base a las 5 columnas del DataFrame""" map = Basemap(projection='mill', resolution='c', llcrnrlon=Long.min(), llcrnrlat=Lat.min(), urcrnrlon=Long.max(), urcrnrlat=Lat.max()) if (i > 0 and i < 6): roya = df.loc[df['d{}'.format(i)] == '1'] x, y = map(np.array(roya['Long']), np.array(roya['Lat'])) map.scatter(x, y, marker='.', color='green', s=1) map.readshapefile('../../shapes/Estados', 'Mill') print('Generando mapa del dia {} ...'.format(cincodias[i - 1])) plt.title('Pronostico de ROYA \n del {}'.format(cincodias[i - 1])) plt.text(x=1.0536e+06, y=1.33233e+06, s=u' @ INIFAP', fontsize=15, color='green') plt.savefig('Pronostico_de_ROYA_del_{}.png'.format(cincodias[i - 1]), dpi=300) if (i == 6): """Generación de un mapa genérico en base al índice del DataFrame""" roya = df.loc[df['indice'] > 1] x, y = map(np.array(roya['Long']), np.array(roya['Lat'])) numCols = len(x) numRows = len(y) xi = np.linspace(x.min(), x.max(), numCols) yi = np.linspace(y.min(), y.max(), numRows) xi, yi = np.meshgrid(xi, yi) z = np.array(roya['indice']) zi = gd((x, y), z, (xi, yi), method='cubic') cs = map.contourf(xi, yi, zi, grado, cmap='RdYlGn_r') map.colorbar(cs) map.readshapefile('../../shapes/Estados', 'Mill') print('\nGenerando mapa del pronostico del {} al {} ...'.format( cincodias[0], cincodias[4])) plt.title('Pronostico de ROYA del {} al {}'.format( cincodias[0], cincodias[4])) plt.text(x=1.0536e+06, y=1.33233e+06, s=u' @ INIFAP', fontsize=15, color='green') plt.savefig('Pronostico_de_ROYA_del_{}_al_{}.png'.format( cincodias[0], cincodias[4]), dpi=300) plt.clf() os.chdir('../..')
ar_len = len(X1) * len(Y1) * len(Z1) X = np.arange(ar_len, dtype=float) Y = np.arange(ar_len, dtype=float) Z = np.arange(ar_len, dtype=float) l = 0 for i in range(0, len(X1)): for j in range(0, len(Y1)): for k in range(0, len(Z1)): X[l] = X1[i] Y[l] = Y1[j] Z[l] = Z1[k] l = l + 1 print("Interpolate...") S = gd((xs, ys, zs), s, (X, Y, Z), fill_value=0.0, method='linear') print("") print("After fit Min Max: ", min(S), max(S)) #s = np.sin(x*y*z)/(x*y*z) #print(S.shape, type(S)) #print(min(xs), max(xs)) #mlab.contour3d(s) if TYPES == 0: n_bins = 100 plt.hist(s, bins=n_bins) plt.show()
return X, Y X_PES, Y_PES, E_PES, XY_PES = PESreadin(FILE_PES) points = np.array(XY_PES) E = np.array(E_PES) X1 = np.array(X_PES) Y1 = np.array(Y_PES) #intepolate X_PES and Y_PES to get X-Y meshgrid X1 = np.linspace(X1.min(), X1.max(), 1000) Y1 = np.linspace(Y1.min(), Y1.max(), 1000) X1, Y1 = np.meshgrid(X1, Y1) #intepolate Z over X-Y meshgrid above Z = gd(points, E, (X1, Y1), method='cubic') X_MEP, Y_MEP = MEPreadin(FILE_MEP) if iniplot: X_ini, Y_ini = Inireadin(FILE_Ini) #draw the PES and MEP and initial guess path levels = np.arange(MIN, MAX, STEP) fig, ax = plt.subplots(figsize=(5, 5)) CS = ax.contour(X1, Y1, Z, levels, colors='k', linewidths=1,
def iniciarProcesamiento(): # constantes LONG_MIN = -115.65 LONG_MAX = -107.94 LAT_MIN = 25.41 LAT_MAX = 33.06 # archivos a procesar # listaDeArchivos = [x for x in os.listdir('') if x.endswith('')] # nombre del archivo nombreArchivo = "GBBEPx.emis_co.001.20180122.nc" arrayNombreArchivo = nombreArchivo.split(".") arrayComponente = arrayNombreArchivo[1].split("_") nombreParaMapa = arrayComponente[1] rutaArchivo = "../data/2018-01-22/{}".format(nombreArchivo) # leer el archivo netcdf dataset = Dataset(rutaArchivo) # generar las arreglos de las variables biomass = dataset.variables['biomass'][:] Latitude = dataset.variables['Latitude'][:] Longitude = dataset.variables['Longitude'][:] # variable para generar CSV dataText = "Long,Lat,Biomass\n" # procesamiento de información for i in range(Longitude.shape[0]): for j in range(Latitude.shape[0]): tempText = "{},{},{}\n".format(Longitude[i], Latitude[j], biomass[0, j, i]) dataText += tempText # generar archivo temporal csv fileName = '../temp/2018-01-22.csv' textFile = open(fileName, "w") textFile.write(dataText) textFile.close() # leer el archivo temporal csv data = pd.read_csv(fileName) # limites longitud > -115.65 y < -107.94 data = data.loc[data['Long'] > LONG_MIN] data = data.loc[data['Long'] < LONG_MAX] # limites latitud > 25.41 y < 33.06 data = data.loc[data['Lat'] > LAT_MIN] data = data.loc[data['Lat'] < LAT_MAX] # obtener valores de x, y lons = np.array(data['Long']) lats = np.array(data['Lat']) #%% iniciar la gráfica plt.clf() m = Basemap(projection='mill', llcrnrlat=LAT_MIN, urcrnrlat=LAT_MAX, llcrnrlon=LONG_MIN, urcrnrlon=LONG_MAX, resolution='h') # generar lats, lons x, y = m(lons, lats) # numero de columnas y filas numCols = len(x) numRows = len(y) # generar xi, yi xi = np.linspace(x.min(), x.max(), numCols) yi = np.linspace(y.min(), y.max(), numRows) # generar el meshgrid xi, yi = np.meshgrid(xi, yi) # generar zi z = np.array(data['Biomass']) zi = gd((x, y), z, (xi, yi), method='cubic') # generar clevs stepVariable = 1 step = (z.max() - z.min()) / 10 # verificar el valor del intervalo if step <= 1: stepVariable = 1 clevs = np.linspace(z.min(), z.max() + stepVariable, 10) #clevs = [1,2,3,4,5,6,7,8,9,10] #%% contour plot cs = m.contourf(xi, yi, zi, clevs, zorder=5, alpha=0.5, cmap='PuBu') m.readshapefile('../shapes/Estados', 'Estados') #%% colorbar cbar = m.colorbar(cs, location='right', pad="5%") cbar.set_label('mm') tituloTemporalParaElMapa = "{} {}".format(nombreParaMapa, "2018-01-17") plt.title(tituloTemporalParaElMapa) # Mac /Users/jorgemauricio/Documents/Research/proyectoGranizo/Maps/{}_{}.png # Linux /home/jorge/Documents/Research/proyectoGranizo/Maps/{}_{}.png nombreTemporalParaElMapa = "/Users/jorgemauricio/Documents/Research/proyectoCaborca/maps/{}_2018-01-22.png".format( nombreParaMapa) plt.annotate('@2018 INIFAP', xy=(-109, 29), xycoords='figure fraction', xytext=(0.45, 0.45), color='g', zorder=50) plt.savefig(nombreTemporalParaElMapa, dpi=300) print('****** Genereate: {}'.format(nombreTemporalParaElMapa))