def watershed(self, a): m = self._markers #d = scipy.ndimage.distance_transform_edt(a) d = a.copy() q = [] w = m.copy() for y, x in scipy.argwhere(m != 0): heapq.heappush(q, (-d[y - 1, x - 1], (y - 1, x - 1))) heapq.heappush(q, (-d[y - 1, x], (y - 1, x))) heapq.heappush(q, (-d[y - 1, x + 1], (y - 1, x + 1))) heapq.heappush(q, (-d[y, x - 1], (y, x - 1))) heapq.heappush(q, (-d[y, x + 1], (y, x + 1))) heapq.heappush(q, (-d[y + 1, x - 1], (y + 1, x - 1))) heapq.heappush(q, (-d[y + 1, x], (y + 1, x))) heapq.heappush(q, (-d[y + 1, x + 1], (y + 1, x + 1))) while q: y, x = heapq.heappop(q)[1] l = scipy.unique(w[y - 1:y + 2, x - 1:x + 2]) l = l[l != 0] if len(l) == 1: w[y, x] = l[0] for ny, nx in scipy.argwhere(w[y - 1:y + 2, x - 1:x + 2] == 0): if (ny == 1) and (nx == 1): continue ny += y - 1 nx += x - 1 try: p = (-d[ny, nx], (ny, nx)) d[ny, nx] = 0 if p[0] != 0 and not p in q: heapq.heappush(q, p) except IndexError, e: print e
def watershed(self, a): m = self._markers #d = scipy.ndimage.distance_transform_edt(a) d = a.copy() q = [] w = m.copy() for y, x in scipy.argwhere(m != 0): heapq.heappush(q, (-d[y - 1, x - 1], (y - 1, x - 1))) heapq.heappush(q, (-d[y - 1, x], (y - 1, x))) heapq.heappush(q, (-d[y - 1, x + 1], (y - 1, x + 1))) heapq.heappush(q, (-d[y, x - 1], (y, x - 1))) heapq.heappush(q, (-d[y, x + 1], (y, x + 1))) heapq.heappush(q, (-d[y + 1, x - 1], (y + 1, x - 1))) heapq.heappush(q, (-d[y + 1, x], (y + 1 , x))) heapq.heappush(q, (-d[y + 1, x + 1], (y + 1, x + 1))) while q: y, x = heapq.heappop(q)[1] l = scipy.unique(w[y - 1: y + 2, x - 1: x + 2]) l = l[l != 0] if len(l) == 1: w[y, x] = l[0] for ny, nx in scipy.argwhere(w[y - 1: y + 2, x - 1: x + 2] == 0): if (ny == 1) and (nx == 1): continue ny += y - 1 nx += x - 1 try: p = (-d[ny, nx], (ny, nx)) d[ny, nx] = 0 if p[0] != 0 and not p in q: heapq.heappush(q, p) except IndexError, e: print e
def part_shape_bins(data, AreR, AspR, alpha, beta, sig_adj, Lbound, Rbound): top_bound = alpha * AreR + beta + sig_adj LRbound_pos01 = [ scipy.argwhere(AreR == j)[0][0] for j in AreR if j < Lbound ] LRbound_pos2 = [ scipy.argwhere(AreR == j)[0][0] for j in AreR if j >= Lbound and j <= Rbound ] LRbound_pos3 = [ scipy.argwhere(AreR == j)[0][0] for j in AreR if j > Rbound ] bins = [[], [], [], [], []] bins2 = np.array([0, 0, 0, 0]) for i in range(np.size(AspR)): cols0 = [k for k in LRbound_pos01 if AspR[i] > top_bound[k]] bins[0].append(np.sum(data[i, cols0])) cols1 = [k for k in LRbound_pos01 if AspR[i] < top_bound[k]] bins[1].append(np.sum(data[i, cols1])) bins[2].append(np.sum(data[i, LRbound_pos2])) bins[3].append(np.sum(data[i, LRbound_pos3])) bins2[0] = np.array(bins[0]).sum() bins2[1] = np.array(bins[1]).sum() bins2[2] = np.array(bins[2]).sum() bins2[3] = np.array(bins[3]).sum() if bins2[1] < bins2[0]: bins2[0] += bins2[1] bins2[1] = 0 else: bins2[1] -= bins2[0] bins2[0] *= 2 return (bins2)
def switch_triangles(self, triangle, neighbour, op1, op2): # print("switching", triangle.index, neighbour.index, # "with neighbours", triangle.neighbours, # neighbour.neighbours, self.triangles[29].neighbours) tn = triangle.neighbours.copy() nn = neighbour.neighbours.copy() # This escapes words, maybe a drawing will help? # https://imgur.com/ZvuKOZH # Basically we need to update the neighbours lists of the # triangles we're working with... triangle.neighbours[op1] = nn[sp.argwhere( neighbour.vertices == triangle.vertices[(op1 + 1) % 3])] triangle.neighbours[(op1 + 2) % 3] = neighbour.index neighbour.neighbours[op2] = tn[(op1 + 2) % 3] neighbour.neighbours[sp.argwhere( neighbour.vertices == triangle.vertices[(op1 + 1) % 3])] = triangle.index # ...as well as their neighbouring triangles (if they exist) if triangle.neighbours[op1] != -1: n_temp = self.triangles[triangle.neighbours[op1]].neighbours n_temp[sp.argwhere(n_temp == neighbour.index)] = triangle.index if neighbour.neighbours[op2] != -1: n_temp = self.triangles[neighbour.neighbours[op2]].neighbours n_temp[sp.argwhere(n_temp == triangle.index)] = neighbour.index # Now we just need to update the triangles' vertices triangle.vertices[(op1 + 1) % 3] = neighbour.vertices[op2] neighbour.vertices[sp.argwhere(neighbour.vertices == triangle.vertices[ (op1 + 2) % 3])] = triangle.vertices[op1] triangle.vert_coordinates = self.points[triangle.vertices] neighbour.vert_coordinates = self.points[neighbour.vertices] # Finally we change the status of the flat triangle, # as it is now sloped triangle.flat = False
def truncate(self, new_D, update=True, return_update_data=False): """Reduces the bond-dimensions by truncating the least-significant Schmidt vectors. The parameters must be in canonical form to ensure that the discarded parameters correspond to the smallest Schmidt coefficients. Always perform self.restore_RCF() first! Each bond-dimension can either be reduced or left unchanged. The resulting parameters self.A will not generally have canonical form after truncation. Parameters ---------- new_D : list or ndarray of int The new bond-dimensions in a vector of length N + 1. update : bool (True) Whether to call self.update() after truncation (turn off if you plan to do it yourself). return_update_data : bool Whether to return additional data needed to perform a minimal update. Returns ------- data : stuff Additional data needed by self._update_after_truncate() (if return_update_data == True). """ new_D = sp.array(new_D) assert new_D.shape == self.D.shape, "new_D must have same shape as self.D" assert sp.all( new_D <= self.D ), "new bond-dimensions must be less than or equal to current dimensions" last_trunc = sp.argwhere(self.D - new_D).max() first_trunc = sp.argwhere(self.D - new_D).min() tmp_A = self.A old_l = self.l old_r = self.r self.D = new_D self._init_arrays() for n in xrange(1, self.N + 1): self.A[n][:] = tmp_A[n][:, -self.D[n - 1]:, -self.D[n]:] if update: self.update() if return_update_data: return last_trunc, old_l, first_trunc, old_r
def pts_between_stdevs(AreR, AspR, alpha, beta, sig_adj): top_bound = alpha * AreR + beta + sig_adj bot_bound = alpha * AreR + beta - sig_adj accepted = [] for i in range(np.size(AreR)): # These are arrays that contain the position of points within AspR that fit # within the top and bot bound line top_accept = [ scipy.argwhere(AspR == j)[0][0] for j in AspR if j <= top_bound[i] ] bot_accept = [ scipy.argwhere(AspR == k)[0][0] for k in AspR if k >= bot_bound[i] ] accepted.append([val for val in top_accept if val in bot_accept]) return accepted
def makeinputh5(Iono,basedir): """This will make a h5 file for the IonoContainer that can be used as starting points for the fitter. The ionocontainer taken will be average over the x and y dimensions of space to make an average value of the parameters for each altitude. Inputs Iono - An instance of the Ionocontainer class that will be averaged over so it can be used for fitter starting points. basdir - A string that holds the directory that the file will be saved to. """ # Get the parameters from the original data Param_List = Iono.Param_List dataloc = Iono.Cart_Coords times = Iono.Time_Vector velocity = Iono.Velocity zlist,idx = sp.unique(dataloc[:,2],return_inverse=True) siz = list(Param_List.shape[1:]) vsiz = list(velocity.shape[1:]) datalocsave = sp.column_stack((sp.zeros_like(zlist),sp.zeros_like(zlist),zlist)) outdata = sp.zeros([len(zlist)]+siz) outvel = sp.zeros([len(zlist)]+vsiz) # Do the averaging across space for izn,iz in enumerate(zlist): arr = sp.argwhere(idx==izn) outdata[izn] = sp.mean(Param_List[arr],axis=0) outvel[izn] = sp.mean(velocity[arr],axis=0) Ionoout = IonoContainer(datalocsave,outdata,times,Iono.Sensor_loc,ver=0, paramnames=Iono.Param_Names, species=Iono.Species,velocity=outvel) Ionoout.saveh5(basedir/'startdata.h5')
def test_RSA_mask_edge_2d(self): im = sp.zeros([100, 100], dtype=int) im = ps.generators.RSA(im, radius=10, volume_fraction=0.5, mode='contained') coords = sp.argwhere(im == 2) assert ~sp.any(coords < 10) assert ~sp.any(coords > 90)
def test_RSA_mask_edge_3d(self): im = sp.zeros([50, 50, 50], dtype=int) im = ps.generators.RSA(im, radius=5, volume_fraction=0.5, mode='contained') coords = sp.argwhere(im == 2) assert ~sp.any(coords < 5) assert ~sp.any(coords > 45)
def degree_distrib(net, deg_type="total", node_list=None, use_weights=True, log=False, num_bins=30): ''' Computing the degree distribution of a network. Parameters ---------- net : :class:`~nngt.Graph` or subclass the network to analyze. deg_type : string, optional (default: "total") type of degree to consider ("in", "out", or "total"). node_list : list or numpy.array of ints, optional (default: None) Restrict the distribution to a set of nodes (default: all nodes). use_weights : bool, optional (default: True) use weighted degrees (do not take the sign into account: all weights are positive). log : bool, optional (default: False) use log-spaced bins. Returns ------- counts : :class:`numpy.array` number of nodes in each bin deg : :class:`numpy.array` bins ''' ia_node_deg = net.get_degrees(node_list, deg_type, use_weights) ra_bins = sp.linspace(ia_node_deg.min(), ia_node_deg.max(), num_bins) if log: ra_bins = sp.logspace(sp.log10(sp.maximum(ia_node_deg.min(),1)), sp.log10(ia_node_deg.max()), num_bins) counts,deg = sp.histogram(ia_node_deg, ra_bins) ia_indices = sp.argwhere(counts) return counts[ia_indices], deg[ia_indices]
def makeinputh5(Iono,basedir): basedir = Path(basedir).expanduser() Param_List = Iono.Param_List dataloc = Iono.Cart_Coords times = Iono.Time_Vector velocity = Iono.Velocity zlist,idx = sp.unique(dataloc[:,2],return_inverse=True) siz = list(Param_List.shape[1:]) vsiz = list(velocity.shape[1:]) datalocsave = sp.column_stack((sp.zeros_like(zlist),sp.zeros_like(zlist),zlist)) outdata = sp.zeros([len(zlist)]+siz) outvel = sp.zeros([len(zlist)]+vsiz) for izn,iz in enumerate(zlist): arr = sp.argwhere(idx==izn) outdata[izn]=sp.mean(Param_List[arr],axis=0) outvel[izn]=sp.mean(velocity[arr],axis=0) Ionoout = IonoContainer(datalocsave,outdata,times,Iono.Sensor_loc,ver=0, paramnames=Iono.Param_Names, species=Iono.Species,velocity=outvel) ofn = basedir/'startdata.h5' print('writing {}'.format(ofn)) Ionoout.saveh5(str(ofn))
def filter(molecule, filter_func): """ Beta version: Uses the filter_func which must return a boolean when being passed an individual region to filter out certain regions. !!!DO NOT FILTER LINKERS AND BUBBLES AT THE SAME TIME.!!! All positions that were removed are then interpolated by the surrounding values. """ filtered = [ Region(r.isbubble, r.start, r.end, len(r)) for r in molecule if not filter_func(r) ] filtered = Molecule.array(filtered, len(molecule)) #Selects all removed bases and attempts to interpolate their value from surrounding bases. #http://stackoverflow.com/questions/6518811/interpolate-nan-values-in-a-numpy-array nans = scipy.isnan(filtered) f = lambda x: x.nonzero()[0] filtered[nans] = scipy.interp(f(nans), f(~nans), filtered[~nans]) #After interpolation the regions are reconstructed an a new molecule is created with specific regions removed. pivots = scipy.argwhere(scipy.diff(filtered))[:, 0] + 1 pivots = scipy.concatenate(([0], pivots, [len(molecule)])) new_regions = [ Region(bool(filtered[start]), start, stop, stop - start) for start, stop in zip(pivots[:-1], pivots[1:]) ] return (Molecule(new_regions))
def makeinputh5(Iono, basedir): basedir = Path(basedir).expanduser() Param_List = Iono.Param_List dataloc = Iono.Cart_Coords times = Iono.Time_Vector velocity = Iono.Velocity zlist, idx = sp.unique(dataloc[:, 2], return_inverse=True) siz = list(Param_List.shape[1:]) vsiz = list(velocity.shape[1:]) datalocsave = sp.column_stack( (sp.zeros_like(zlist), sp.zeros_like(zlist), zlist)) outdata = sp.zeros([len(zlist)] + siz) outvel = sp.zeros([len(zlist)] + vsiz) for izn, iz in enumerate(zlist): arr = sp.argwhere(idx == izn) outdata[izn] = sp.mean(Param_List[arr], axis=0) outvel[izn] = sp.mean(velocity[arr], axis=0) Ionoout = IonoContainer(datalocsave, outdata, times, Iono.Sensor_loc, ver=0, paramnames=Iono.Param_Names, species=Iono.Species, velocity=outvel) ofn = basedir / 'startdata.h5' print('writing {}'.format(ofn)) Ionoout.saveh5(str(ofn))
def makeinputh5(Iono,basedir): """This will make a h5 file for the IonoContainer that can be used as starting points for the fitter. The ionocontainer taken will be average over the x and y dimensions of space to make an average value of the parameters for each altitude. Inputs Iono - An instance of the Ionocontainer class that will be averaged over so it can be used for fitter starting points. basdir - A string that holds the directory that the file will be saved to. """ # Get the parameters from the original data Param_List = Iono.Param_List dataloc = Iono.Cart_Coords times = Iono.Time_Vector velocity = Iono.Velocity zlist,idx = sp.unique(dataloc[:,2],return_inverse=True) siz = list(Param_List.shape[1:]) vsiz = list(velocity.shape[1:]) datalocsave = sp.column_stack((sp.zeros_like(zlist),sp.zeros_like(zlist),zlist)) outdata = sp.zeros([len(zlist)]+siz) outvel = sp.zeros([len(zlist)]+vsiz) # Do the averaging across space for izn,iz in enumerate(zlist): arr = sp.argwhere(idx==izn) outdata[izn]=sp.mean(Param_List[arr],axis=0) outvel[izn]=sp.mean(velocity[arr],axis=0) Ionoout = IonoContainer(datalocsave,outdata,times,Iono.Sensor_loc,ver=0, paramnames=Iono.Param_Names, species=Iono.Species,velocity=outvel) Ionoout.saveh5(os.path.join(basedir,'startdata.h5'))
def sample(self): #interesting idea, but, presumably, it only makes probabilistic sense #in the case of joint distributions cumulative_measures = self.beliefs.cumsum().reshape(self.beliefs.shape) random_measure = scipy.random.random() * self.beliefs.sum() indices = scipy.argwhere(cumulative_measures > random_measure)[0] return tuple(rvar[index] for (rvar, index) in zip(self.scope, indices))
def truncate(self, new_D, update=True, return_update_data=False): """Reduces the bond-dimensions by truncating the least-significant Schmidt vectors. The parameters must be in canonical form to ensure that the discarded parameters correspond to the smallest Schmidt coefficients. Always perform self.restore_RCF() first! Each bond-dimension can either be reduced or left unchanged. The resulting parameters self.A will not generally have canonical form after truncation. Parameters ---------- new_D : list or ndarray of int The new bond-dimensions in a vector of length N + 1. update : bool (True) Whether to call self.update() after truncation (turn off if you plan to do it yourself). return_update_data : bool Whether to return additional data needed to perform a minimal update. Returns ------- data : stuff Additional data needed by self._update_after_truncate() (if return_update_data == True). """ new_D = sp.array(new_D) assert new_D.shape == self.D.shape, "new_D must have same shape as self.D" assert sp.all(new_D <= self.D), "new bond-dimensions must be less than or equal to current dimensions" last_trunc = sp.argwhere(self.D - new_D).max() first_trunc = sp.argwhere(self.D - new_D).min() tmp_A = self.A old_l = self.l old_r = self.r self.D = new_D self._init_arrays() for n in xrange(1, self.N + 1): self.A[n][:] = tmp_A[n][:, -self.D[n - 1]:, -self.D[n]:] if update: self.update() if return_update_data: return last_trunc, old_l, first_trunc, old_r
def get_sloped_neighbour(self, tri): for i in range(3): # If the edge is long if self.long_edges[i]: n_index = self.neighbours[i] # If the neighbour exists and isn't flat, return its index if n_index != -1 and not tri.triangles[n_index].flat: neighbour = tri.triangles[n_index] return neighbour, i, int( sp.argwhere(neighbour.neighbours == self.index)) # If no neighbour found, return None three times, to make the return # length consistent return None, None, None
def quad_generator(angles, i_scan, no_steps, quadrature_ds): for i_step in range(no_steps): a = angles[i_step] indices = scipy.argwhere(a < a[0] + pi) i_min = 0 i_max = indices.max() + 1 try: assert (a[i_max] - a[i_min] > pi) except: logging.error("The phases seem not to cover a pi interval in scan %i, step %i. " "The following phases were found:", i_scan, i_step) logging.error(a) logging.error(indices.T.squeeze()) raise yield (a[i_min:i_max + 1], quadrature_ds[i_scan, i_step, i_min:i_max + 1, :])
def timeregister(self, self2): """ Create a cell array which shows the overlap between two instances of GeoData. Inputs self2 - A GeoData object. Outputs outcell - A cellarray of vectors the same length as the time vector in self. Each vector will have the time indecies from the second GeoData object which overlap with the time indicie of the first object.""" times1 = timerepair(self.times) times2 = timerepair(self2.times) outcell = [sp.array([])] * times1.shape[0] for k in range(times1.shape[0]): l = times1[k, :] list1 = sp.argwhere(l[0] > times2[:, 0]) list2 = sp.argwhere(l[1] < times2[:, 1]) if (list1.size == 0) or (list2.size == 0): continue ind1 = list1[-1][0] ind2 = list2[0][0] outcell[k] = sp.arange(ind1, ind2 + 1).astype('int64') return outcell
def timeregister(self,self2): """ Create a cell array which shows the overlap between two instances of GeoData. Inputs self2 - A GeoData object. Outputs outcell - A cellarray of vectors the same length as the time vector in self. Each vector will have the time indecies from the second GeoData object which overlap with the time indicie of the first object.""" times1 = timerepair(self.times) times2 = timerepair(self2.times) outcell = [sp.array([])]*times1.shape[0] for k in range(times1.shape[0]): l = times1[k,:] list1 = sp.argwhere(l[0]>times2[:,0]) list2 = sp.argwhere(l[1]<times2[:,1]) if (list1.size==0) or (list2.size==0): continue ind1 = list1[-1][0] ind2 = list2[0][0] outcell[k]=sp.arange(ind1,ind2+1).astype('int64') return outcell
def calc_volume(mesh): Xi = morphic.utils.grid(50, 3) Xm = mesh.elements[1].evaluate(Xi) tree = cKDTree(Xm) Xn = mesh.get_nodes() Xmin, Xmax = Xn.min(0), Xn.max(0) dX = Xmax - Xmin print dX N = 50 Xi = morphic.utils.grid(N, 3) dv = (dX / N).prod() dlimit = (dX / N).max() print dlimit, dv X = Xmin + dX * Xi d, i = tree.query(X.tolist()) ii = scipy.argwhere(d < dlimit) print ii.shape[0] * dv, ii.shape, X.shape
def quad_generator(angles, i_scan, no_steps, quadrature_ds): for i_step in range(no_steps): a = angles[i_step] indices = scipy.argwhere(a < a[0] + pi) i_min = 0 i_max = indices.max() + 1 try: assert (a[i_max] - a[i_min] > pi) except: logging.error( "The phases seem not to cover a pi interval in scan %i, step %i. " "The following phases were found:", i_scan, i_step) logging.error(a) logging.error(indices.T.squeeze()) raise yield (a[i_min:i_max + 1], quadrature_ds[i_scan, i_step, i_min:i_max + 1, :])
def findPeaks(data, threshold, size=3, mode="wrap"): """ Look for local maxima above a specified threshold. It works well for data that is not especially noisy or crowded. """ peaks = [] if (data.size == 0) or (data.max() < threshold): return peaks boolsVal = data > threshold maxFilter = np.maximum_filter(data, size=size, mode=mode) boolsMax = data == maxFilter boolsPeak = boolsVal & boolsMax indices = sp.argwhere(boolsPeak) # Position indices for True for position in indices: position = tuple(position) height = data[position] peak = Peak(position, data, height) peaks.append(peak) return peaks
def segment(self, mol, threshold, end): """ takes a list whos values are some score of whether that specific coordinate is a linker. this is typically used on the lists returned from partition but does not have to be. All streches of elements whos threshold is above or below some value are then determined. and a list of regions and there corresponding label are returned. It also ignores the last "end" coordinates of mol. regeions = [(0,end),(start,end),...,(start,end),(start,len(mol))] labels = [1,0,...1,0] 1 for greater then threshold 0 for below. """ mask = mol > threshold breaks = scipy.concatenate( ([0], scipy.argwhere(scipy.ediff1d(mask[:-end]))[:, 0] + 1, [len(mol)])) regions = scipy.array([(s, e) for s, e in zip(breaks[:-1], breaks[1:])]) labels = scipy.array([int(mask[s]) for s, e in regions]) return (regions, labels)
def plot_pct_fwds(self): """ Plot the pct of forward motion for each genotype. """ Nn = len(self.dirs_to_plot) for iD, dir in enumerate(self.dirs_to_plot): filename = os.path.join(dir, 'pct_fwd.txt') tmp_data = sp.loadtxt(filename) if self.data is None: self.data = sp.zeros((Nn, len(tmp_data))) self.data[iD, :] = tmp_data # Get average fwd_pcts with error bars (1 sem) self.avgs = sp.average(self.data, axis=1)*100 self.stds = sp.std(self.data, axis=1)*100 sort_idxs = sp.argsort(self.avgs)[::-1] # Make empty zero index if not yet (hacky!!) if sort_idxs[0] != 0: zero_idx = sp.argwhere(sort_idxs == 0)[0] change_idx = sort_idxs[zero_idx] sort_idxs[zero_idx] = sort_idxs[0] sort_idxs[0] = 0 sort_labels = self.genotypes[sort_idxs] sort_avgs = self.avgs[sort_idxs] sort_stds = self.stds[sort_idxs] # Plot for each genotype fig = plt.figure() fig.set_size_inches(3, 4) plt.errorbar(range(Nn), sort_avgs, sort_stds, lw=0, elinewidth=1.5, capsize=5, color='k') plt.scatter(range(Nn), sort_avgs, c=sp.arange(Nn), cmap=plt.cm.winter, zorder=100, s=30) plt.ylim(0, 105) plt.xticks(rotation=90) plt.xticks(range(Nn), sort_labels)
def degree_distrib(graph, deg_type="total", node_list=None, use_weights=False, log=False, num_bins=30): ''' Degree distribution of a graph. Parameters ---------- graph : :class:`~nngt.Graph` or subclass the graph to analyze. deg_type : string, optional (default: "total") type of degree to consider ("in", "out", or "total"). node_list : list or numpy.array of ints, optional (default: None) Restrict the distribution to a set of nodes (default: all nodes). use_weights : bool, optional (default: False) use weighted degrees (do not take the sign into account: all weights are positive). log : bool, optional (default: False) use log-spaced bins. Returns ------- counts : :class:`numpy.array` number of nodes in each bin deg : :class:`numpy.array` bins ''' ia_node_deg = graph.get_degrees(deg_type, node_list, use_weights) ra_bins = sp.linspace(ia_node_deg.min(), ia_node_deg.max(), num_bins) if log: ra_bins = sp.logspace(sp.log10(sp.maximum(ia_node_deg.min(), 1)), sp.log10(ia_node_deg.max()), num_bins) counts, deg = sp.histogram(ia_node_deg, ra_bins) ia_indices = sp.argwhere(counts) return counts[ia_indices], deg[ia_indices]
def makehistdata(params, maindir): """ This will make the histogram data for the statistics. Inputs params - A list of parameters that will have statistics created maindir - The directory that the simulation data is held. Outputs datadict - A dictionary with the data values in numpy arrays. The keys are param names. errordict - A dictionary with the data values in numpy arrays. The keys are param names. errdictrel - A dictionary with the error values in numpy arrays, normalized by the correct value. The keys are param names. """ maindir = Path(maindir) ffit = maindir.joinpath('Fitted', 'fitteddata.h5') inputfiledir = maindir.joinpath('Origparams') paramslower = [ip.lower() for ip in params] eparamslower = ['n' + ip.lower() for ip in params] # set up data dictionary errordict = {ip: [] for ip in params} errordictrel = {ip: [] for ip in params} #Read in fitted data Ionofit = IonoContainer.readh5(str(ffit)) times = Ionofit.Time_Vector dataloc = Ionofit.Sphere_Coords rng = dataloc[:, 0] rng_log = sp.logical_and(rng > 200., rng < 400) dataloc_out = dataloc[rng_log] pnames = Ionofit.Param_Names pnameslower = sp.array([ip.lower() for ip in pnames.flatten()]) p2fit = [ sp.argwhere(ip == pnameslower)[0][0] if ip in pnameslower else None for ip in paramslower ] datadict = { ip: Ionofit.Param_List[rng_log, :, p2fit[ipn]].flatten() for ipn, ip in enumerate(params) } ep2fit = [ sp.argwhere(ip == pnameslower)[0][0] if ip in pnameslower else None for ip in eparamslower ] edatadict = { ip: Ionofit.Param_List[rng_log, :, ep2fit[ipn]].flatten() for ipn, ip in enumerate(params) } # Determine which input files are to be used. dirlist = [str(i) for i in inputfiledir.glob('*.h5')] _, outime, filelisting, _, _ = IonoContainer.gettimes(dirlist) time2files = [] for itn, itime in enumerate(times): log1 = (outime[:, 0] >= itime[0]) & (outime[:, 0] < itime[1]) log2 = (outime[:, 1] > itime[0]) & (outime[:, 1] <= itime[1]) log3 = (outime[:, 0] <= itime[0]) & (outime[:, 1] > itime[1]) tempindx = sp.where(log1 | log2 | log3)[0] time2files.append(filelisting[tempindx]) curfilenum = -1 for iparam, pname in enumerate(params): curparm = paramslower[iparam] # Use Ne from input to compare the ne derived from the power. if curparm == 'nepow': curparm = 'ne' datalist = [] for itn, itime in enumerate(times): for filenum in time2files[itn]: filenum = int(filenum) if curfilenum != filenum: curfilenum = filenum datafilename = dirlist[filenum] Ionoin = IonoContainer.readh5(datafilename) if ('ti' in paramslower) or ('vi' in paramslower): Ionoin = maketi(Ionoin) pnames = Ionoin.Param_Names pnameslowerin = sp.array( [ip.lower() for ip in pnames.flatten()]) prmloc = sp.argwhere(curparm == pnameslowerin) if prmloc.size != 0: curprm = prmloc[0][0] # build up parameter vector bs the range values by finding the closest point in space in the input curdata = sp.zeros(len(dataloc_out)) for irngn, curcoord in enumerate(dataloc_out): tempin = Ionoin.getclosestsphere(curcoord, [itime])[0] Ntloc = tempin.shape[0] tempin = sp.reshape(tempin, (Ntloc, len(pnameslowerin))) curdata[irngn] = tempin[0, curprm] datalist.append(curdata) errordict[pname] = datadict[pname] - sp.hstack(datalist) errordictrel[pname] = 100. * errordict[pname] / sp.absolute( sp.hstack(datalist)) return datadict, errordict, errordictrel, edatadict
def plotacfs( coords, times, configfile, maindir, cartcoordsys=True, indisp=True, acfdisp=True, fitdisp=True, filetemplate="acf", suptitle="ACF Comparison", invacf="", ): """ This will create a set of images that compare the input ISR acf to the output ISR acfs from the simulator. Inputs coords - An Nx3 numpy array that holds the coordinates of the desired points. times - A numpy list of times in seconds. configfile - The name of the configuration file used. cartcoordsys - (default True)A bool, if true then the coordinates are given in cartisian if false then it is assumed that the coords are given in sphereical coordinates. specsfilename - (default None) The name of the file holding the input spectrum. acfname - (default None) The name of the file holding the estimated ACFs. filetemplate (default 'spec') This is the beginning string used to save the images. """ # indisp = specsfilename is not None # acfdisp = acfname is not None maindir = Path(maindir).expanduser() sns.set_style("whitegrid") sns.set_context("notebook") acfname = maindir.joinpath("ACF", "00lags.h5") ffit = maindir.joinpath("Fitted", "fitteddata.h5") specsfiledir = maindir.joinpath("Spectrums") (sensdict, simparams) = readconfigfile(configfile) simdtype = simparams["dtype"] npts = simparams["numpoints"] * 3.0 amb_dict = simparams["amb_dict"] if sp.ndim(coords) == 1: coords = coords[sp.newaxis, :] Nt = len(times) Nloc = coords.shape[0] sns.set_style("whitegrid") sns.set_context("notebook") pulse = simparams["Pulse"] ts = sensdict["t_s"] tau1 = sp.arange(pulse.shape[-1]) * ts if indisp: dirlist = [i.name for i in specsfiledir.glob("*.h5")] timelist = sp.array([float(i.split()[0]) for i in dirlist]) for itn, itime in enumerate(times): filear = sp.argwhere(timelist >= itime) if len(filear) == 0: filenum = len(timelist) - 1 else: filenum = filear[0][0] specsfilename = specsfiledir.joinpath(dirlist[filenum]) Ionoin = IonoContainer.readh5(str(specsfilename)) if itn == 0: specin = sp.zeros((Nloc, Nt, Ionoin.Param_List.shape[-1])).astype(Ionoin.Param_List.dtype) omeg = Ionoin.Param_Names npts = Ionoin.Param_List.shape[-1] for icn, ic in enumerate(coords): if cartcoordsys: tempin = Ionoin.getclosest(ic, times)[0] else: tempin = Ionoin.getclosestsphere(ic, times)[0] # if sp.ndim(tempin)==1: # tempin = tempin[sp.newaxis,:] specin[icn, itn] = tempin[0, :] / npts if acfdisp: Ionoacf = IonoContainer.readh5(str(acfname)) ACFin = sp.zeros((Nloc, Nt, Ionoacf.Param_List.shape[-1])).astype(Ionoacf.Param_List.dtype) omeg = sp.arange(-sp.ceil((npts + 1) / 2), sp.floor((npts + 1) / 2)) / ts / npts for icn, ic in enumerate(coords): if cartcoordsys: tempin = Ionoacf.getclosest(ic, times)[0] else: tempin = Ionoacf.getclosestsphere(ic, times)[0] if sp.ndim(tempin) == 1: tempin = tempin[sp.newaxis, :] ACFin[icn] = tempin # Determine the inverse ACF stuff if len(invacf) == 0: invacfbool = False else: invacfbool = True invfile = maindir.joinpath("ACFInv", "00lags" + invacf + ".h5") Ionoacfinv = IonoContainer.readh5(str(invfile)) ACFinv = sp.zeros((Nloc, Nt, Ionoacfinv.Param_List.shape[-1])).astype(Ionoacfinv.Param_List.dtype) for icn, ic in enumerate(coords): if cartcoordsys: tempin = Ionoacfinv.getclosest(ic, times)[0] else: tempin = Ionoacfinv.getclosestsphere(ic, times)[0] if sp.ndim(tempin) == 1: tempin = tempin[sp.newaxis, :] ACFinv[icn] = tempin if fitdisp: Ionofit = IonoContainer.readh5(str(ffit)) (omegfit, outspecsfit) = ISRspecmakeout( Ionofit.Param_List, sensdict["fc"], sensdict["fs"], simparams["species"], npts ) Ionofit.Param_List = outspecsfit Ionofit.Param_Names = omegfit specfit = sp.zeros((Nloc, Nt, npts)) for icn, ic in enumerate(coords): if cartcoordsys: tempin = Ionofit.getclosest(ic, times)[0] else: tempin = Ionofit.getclosestsphere(ic, times)[0] if sp.ndim(tempin) == 1: tempin = tempin[sp.newaxis, :] specfit[icn] = tempin / npts / npts nfig = int(sp.ceil(Nt * Nloc / 3.0)) imcount = 0 for i_fig in range(nfig): lines = [None] * 4 labels = [None] * 4 lines_im = [None] * 4 labels_im = [None] * 4 (figmplf, axmat) = plt.subplots(3, 2, figsize=(16, 12), facecolor="w") for ax in axmat: if imcount >= Nt * Nloc: break iloc = int(sp.floor(imcount / Nt)) itime = int(imcount - (iloc * Nt)) maxvec = [] minvec = [] if indisp: # apply ambiguity funciton to spectrum curin = specin[iloc, itime] (tau, acf) = spect2acf(omeg, curin) acf1 = scfft.ifftshift(acf)[: len(pulse)] * len(curin) rcs = acf1[0].real guess_acf = sp.dot(amb_dict["WttMatrix"], acf) guess_acf = guess_acf * rcs / guess_acf[0].real # fit to spectrums maxvec.append(guess_acf.real.max()) maxvec.append(guess_acf.imag.max()) minvec.append(acf1.real.min()) minvec.append(acf1.imag.min()) lines[0] = ax[0].plot(tau1 * 1e6, guess_acf.real, label="Input", linewidth=5)[0] labels[0] = "Input ACF With Ambiguity Applied" lines_im[0] = ax[1].plot(tau1 * 1e6, guess_acf.imag, label="Input", linewidth=5)[0] labels_im[0] = "Input ACF With Ambiguity Applied" if fitdisp: curinfit = specfit[iloc, itime] (taufit, acffit) = spect2acf(omegfit, curinfit) rcsfit = curinfit.sum() guess_acffit = sp.dot(amb_dict["WttMatrix"], acffit) guess_acffit = guess_acffit * rcsfit / guess_acffit[0].real lines[1] = ax[0].plot(tau1 * 1e6, guess_acffit.real, label="Input", linewidth=5)[0] labels[1] = "Fitted ACF" lines_im[1] = ax[1].plot(tau1 * 1e6, guess_acffit.imag, label="Input", linewidth=5)[0] labels_im[1] = "Fitted ACF" if acfdisp: lines[2] = ax[0].plot(tau1 * 1e6, ACFin[iloc, itime].real, label="Output", linewidth=5)[0] labels[2] = "Estimated ACF" lines_im[2] = ax[1].plot(tau1 * 1e6, ACFin[iloc, itime].imag, label="Output", linewidth=5)[0] labels_im[2] = "Estimated ACF" maxvec.append(ACFin[iloc, itime].real.max()) maxvec.append(ACFin[iloc, itime].imag.max()) minvec.append(ACFin[iloc, itime].real.min()) minvec.append(ACFin[iloc, itime].imag.min()) if invacfbool: lines[3] = ax[0].plot(tau1 * 1e6, ACFinv[iloc, itime].real, label="Output", linewidth=5)[0] labels[3] = "Reconstructed ACF" lines_im[3] = ax[1].plot(tau1 * 1e6, ACFinv[iloc, itime].imag, label="Output", linewidth=5)[0] labels_im[3] = "Reconstructed ACF" ax[0].set_xlabel(r"$\tau$ in $\mu$s") ax[0].set_ylabel("Amp") ax[0].set_title("Real Part") # Location {0}, Time {1}'.format(coords[iloc],times[itime])) ax[0].set_ylim(min(minvec), max(maxvec) * 1) ax[0].set_xlim([tau1.min() * 1e6, tau1.max() * 1e6]) ax[1].set_xlabel(r"$\tau$ in $\mu$s") ax[1].set_ylabel("Amp") ax[1].set_title("Imag Part") # Location {0}, Time {1}'.format(coords[iloc],times[itime])) ax[1].set_ylim(min(minvec), max(maxvec) * 1) ax[1].set_xlim([tau1.min() * 1e6, tau1.max() * 1e6]) imcount = imcount + 1 figmplf.suptitle(suptitle, fontsize=20) if None in labels: labels.remove(None) lines.remove(None) plt.figlegend(lines, labels, loc="lower center", ncol=5, labelspacing=0.0) fname = filetemplate + "_{0:0>3}.png".format(i_fig) plt.savefig(fname, dpi=300) plt.close(figmplf)
@author: Gape """ from collections import defaultdict import scipy as sp points = [] with open('day25.txt') as file: for row in file: points.append(tuple(int(i) for i in row.split(','))) dist = sp.spatial.distance.pdist(points, metric='cityblock') dist = sp.spatial.distance.squareform(dist) pairs = defaultdict(set) for p1, p2 in sp.argwhere(dist <= 3): if p1 != p2: pairs[p1].add(p2) for p in range(len(points)): values = pairs[p] joined = False for v in values: if v > p: pairs[v].update(values) joined = True if joined: pairs.pop(p) print(len(pairs))
def plotacfs(coords,times,configfile,maindir,cartcoordsys = True, indisp=True,acfdisp= True, fitdisp=True, filetemplate='acf',suptitle = 'ACF Comparison'): """ This will create a set of images that compare the input ISR acf to the output ISR acfs from the simulator. Inputs coords - An Nx3 numpy array that holds the coordinates of the desired points. times - A numpy list of times in seconds. configfile - The name of the configuration file used. cartcoordsys - (default True)A bool, if true then the coordinates are given in cartisian if false then it is assumed that the coords are given in sphereical coordinates. specsfilename - (default None) The name of the file holding the input spectrum. acfname - (default None) The name of the file holding the estimated ACFs. filetemplate (default 'spec') This is the beginning string used to save the images.""" # indisp = specsfilename is not None # acfdisp = acfname is not None acfname = os.path.join(maindir,'ACF','00lags.h5') ffit = os.path.join(maindir,'Fitted','fitteddata.h5') specsfiledir = os.path.join(maindir,'Spectrums') (sensdict,simparams) = readconfigfile(configfile) simdtype = simparams['dtype'] npts = simparams['numpoints']*3.0 amb_dict = simparams['amb_dict'] if sp.ndim(coords)==1: coords = coords[sp.newaxis,:] Nt = len(times) Nloc = coords.shape[0] sns.set_style("whitegrid") sns.set_context("notebook") pulse = simparams['Pulse'] ts = sensdict['t_s'] tau1 = sp.arange(pulse.shape[-1])*ts if indisp: dirlist = os.listdir(specsfiledir) timelist = sp.array([int(i.split()[0]) for i in dirlist]) for itn,itime in enumerate(times): filear = sp.argwhere(timelist>=itime) if len(filear)==0: filenum = len(timelist)-1 else: filenum = filear[0][0] specsfilename = os.path.join(specsfiledir,dirlist[filenum]) Ionoin = IonoContainer.readh5(specsfilename) if itn==0: specin = sp.zeros((Nloc,Nt,Ionoin.Param_List.shape[-1])).astype(Ionoin.Param_List.dtype) omeg = Ionoin.Param_Names npts = Ionoin.Param_List.shape[-1] for icn, ic in enumerate(coords): if cartcoordsys: tempin = Ionoin.getclosest(ic,times)[0] else: tempin = Ionoin.getclosestsphere(ic,times)[0] # if sp.ndim(tempin)==1: # tempin = tempin[sp.newaxis,:] specin[icn,itn] = tempin[0,:]/npts if acfdisp: Ionoacf = IonoContainer.readh5(acfname) ACFin = sp.zeros((Nloc,Nt,Ionoacf.Param_List.shape[-1])).astype(Ionoacf.Param_List.dtype) omeg = sp.arange(-sp.ceil((npts+1)/2),sp.floor((npts+1)/2))/ts/npts for icn, ic in enumerate(coords): if cartcoordsys: tempin = Ionoacf.getclosest(ic,times)[0] else: tempin = Ionoacf.getclosestsphere(ic,times)[0] if sp.ndim(tempin)==1: tempin = tempin[sp.newaxis,:] ACFin[icn] = tempin if fitdisp: Ionofit = IonoContainer.readh5(ffit) (omegfit,outspecsfit) =ISRspecmakeout(Ionofit.Param_List,sensdict['fc'],sensdict['fs'],simparams['species'],npts) Ionofit.Param_List= outspecsfit Ionofit.Param_Names = omegfit specfit = sp.zeros((Nloc,Nt,npts)) for icn, ic in enumerate(coords): if cartcoordsys: tempin = Ionofit.getclosest(ic,times)[0] else: tempin = Ionofit.getclosestsphere(ic,times)[0] if sp.ndim(tempin)==1: tempin = tempin[sp.newaxis,:] specfit[icn] = tempin/npts/npts nfig = int(sp.ceil(Nt*Nloc/6.0)) imcount = 0 for i_fig in range(nfig): lines = [None]*6 labels = [None]*6 (figmplf, axmat) = plt.subplots(2, 3,figsize=(24, 18), facecolor='w') axvec = axmat.flatten() for iax,ax in enumerate(axvec): if imcount>=Nt*Nloc: break iloc = int(sp.floor(imcount/Nt)) itime = int(imcount-(iloc*Nt)) maxvec = [] minvec = [] if indisp: # apply ambiguity funciton to spectrum curin = specin[iloc,itime] (tau,acf) = spect2acf(omeg,curin) acf1 = scfft.ifftshift(acf)[:len(pulse)] rcs=acf1[0].real guess_acf = sp.dot(amb_dict['WttMatrix'],acf) guess_acf = guess_acf*rcs/guess_acf[0].real # fit to spectrums maxvec.append(guess_acf.real.max()) maxvec.append(guess_acf.imag.max()) minvec.append(acf1.real.min()) minvec.append(acf1.imag.min()) lines[0]= ax.plot(tau1*1e6,guess_acf.real,label='Input',linewidth=5)[0] labels[0] = 'Input ACF With Ambiguity Applied Real' lines[1]= ax.plot(tau1*1e6,guess_acf.imag,label='Input',linewidth=5)[0] labels[1] = 'Input ACF With Ambiguity Applied Imag' if fitdisp: curinfit = specfit[iloc,itime] (taufit,acffit) = spect2acf(omegfit,curinfit) rcsfit=acffit[0].real guess_acffit = sp.dot(amb_dict['WttMatrix'],acffit) guess_acffit = guess_acffit*rcsfit/guess_acffit[0].real lines[2]= ax.plot(tau1*1e6,guess_acffit.real,label='Input',linewidth=5)[0] labels[2] = 'Fitted ACF real' lines[3]= ax.plot(tau1*1e6,guess_acffit.imag,label='Input',linewidth=5)[0] labels[3] = 'Fitted ACF Imag' if acfdisp: lines[4]=ax.plot(tau1*1e6,ACFin[iloc,itime].real,label='Output',linewidth=5)[0] labels[4] = 'Estimated ACF Real' lines[5]=ax.plot(tau1*1e6,ACFin[iloc,itime].imag,label='Output',linewidth=5)[0] labels[5] = 'Estimated ACF Imag' maxvec.append(ACFin[iloc,itime].real.max()) maxvec.append(ACFin[iloc,itime].imag.max()) minvec.append(ACFin[iloc,itime].real.min()) minvec.append(ACFin[iloc,itime].imag.min()) ax.set_xlabel('t in us') ax.set_ylabel('Amp') ax.set_title('Location {0}, Time {1}'.format(coords[iloc],times[itime])) ax.set_ylim(min(minvec),max(maxvec)*1) ax.set_xlim([tau1.min()*1e6,tau1.max()*1e6]) imcount=imcount+1 figmplf.suptitle(suptitle, fontsize=20) if None in labels: labels.remove(None) lines.remove(None) plt.figlegend( lines, labels, loc = 'lower center', ncol=5, labelspacing=0. ) fname= filetemplate+'_{0:0>3}.png'.format(i_fig) plt.savefig(fname) plt.close(figmplf)
def plotbeamparameters(times,configfile,maindir,params=['Ne'],indisp=True,fitdisp = True,filetemplate='params',suptitle = 'Parameter Comparison',werrors=False): """ """ sns.set_style("whitegrid") sns.set_context("notebook") # rc('text', usetex=True) ffit = os.path.join(maindir,'Fitted','fitteddata.h5') inputfiledir = os.path.join(maindir,'Origparams') (sensdict,simparams) = readconfigfile(configfile) paramslower = [ip.lower() for ip in params] Nt = len(times) Np = len(params) if fitdisp: Ionofit = IonoContainer.readh5(ffit) dataloc = Ionofit.Sphere_Coords pnames = Ionofit.Param_Names pnameslower = sp.array([ip.lower() for ip in pnames.flatten()]) p2fit = [sp.argwhere(ip==pnameslower)[0][0] if ip in pnameslower else None for ip in paramslower] time2fit = [None]*Nt for itn,itime in enumerate(times): filear = sp.argwhere(Ionofit.Time_Vector>=itime) if len(filear)==0: filenum = len(Ionofit.Time_Vector)-1 else: filenum = filear[0][0] time2fit[itn] = filenum angles = dataloc[:,1:] rng = sp.unique(dataloc[:,0]) b = np.ascontiguousarray(angles).view(np.dtype((np.void, angles.dtype.itemsize * angles.shape[1]))) _, idx, invidx = np.unique(b, return_index=True,return_inverse=True) beamlist = angles[idx] Nb = beamlist.shape[0] if indisp: dirlist = glob.glob(os.path.join(inputfiledir,'*.h5')) filesonly= [os.path.splitext(os.path.split(ifile)[-1])[0] for ifile in dirlist] timelist = sp.array([int(i.split()[0]) for i in filesonly]) time2file = [None]*Nt for itn,itime in enumerate(times): filear = sp.argwhere(timelist>=itime) if len(filear)==0: filenum = len(timelist)-1 else: filenum = filear[0][0] time2file[itn] = filenum nfig = int(sp.ceil(Nt*Nb*Np/9.0)) imcount = 0 curfilenum = -1 for i_fig in range(nfig): lines = [None]*2 labels = [None]*2 (figmplf, axmat) = plt.subplots(3, 3,figsize=(20, 15), facecolor='w') axvec = axmat.flatten() for iax,ax in enumerate(axvec): if imcount>=Nt*Nb*Np: break itime = int(sp.floor(imcount/Nb/Np)) iparam = int(imcount/Nb-Np*itime) ibeam = int(imcount-(itime*Np*Nb+iparam*Nb)) curbeam = beamlist[ibeam] altlist = sp.sin(curbeam[1]*sp.pi/180.)*rng if fitdisp: indxkep = np.argwhere(invidx==ibeam)[:,0] curfit = Ionofit.Param_List[indxkep,time2fit[itime],p2fit[iparam]] rng_fit= dataloc[indxkep,0] alt_fit = rng_fit*sp.sin(curbeam[1]*sp.pi/180.) errorexist = 'n'+paramslower[iparam] in pnameslower if errorexist and werrors: eparam = sp.argwhere( 'n'+paramslower[iparam]==pnameslower)[0][0] curerror = Ionofit.Param_List[indxkep,time2fit[itime],eparam] lines[1]=ax.errorbar(curfit, alt_fit, xerr=curerror,fmt='-.',c='g')[0] else: lines[1]= ax.plot(curfit,alt_fit,marker='.',c='g')[0] labels[1] = 'Fitted Parameters' if indisp: filenum = time2file[itime] if curfilenum!=filenum: curfilenum=filenum datafilename = dirlist[filenum] Ionoin = IonoContainer.readh5(datafilename) if 'ti' in paramslower: Ionoin = maketi(Ionoin) pnames = Ionoin.Param_Names pnameslowerin = sp.array([ip.lower() for ip in pnames.flatten()]) curparm = paramslower[iparam] if curparm == 'nepow': curparm = 'ne' prmloc = sp.argwhere(curparm==pnameslowerin) if prmloc.size !=0: curprm = prmloc[0][0] curcoord = sp.zeros(3) curcoord[1:] = curbeam curdata = sp.zeros(len(rng)) for irngn, irng in enumerate(rng): curcoord[0] = irng tempin = Ionoin.getclosestsphere(curcoord,times)[0] Ntloc = tempin.shape[0] tempin = sp.reshape(tempin,(Ntloc,len(pnameslowerin))) curdata[irngn] = tempin[0,curprm] lines[0]= ax.plot(curdata,altlist,marker='o',c='b')[0] labels[0] = 'Input Parameters' if curparm!='ne': ax.set(xlim=[0.25*sp.amin(curdata),2.5*sp.amax(curdata)]) if curparm=='ne': ax.set_xscale('log') ax.set_xlabel(params[iparam]) ax.set_ylabel('Alt km') ax.set_title('{0} vs Altitude, Time: {1}s Az: {2}$^o$ El: {3}$^o$'.format(params[iparam],times[itime],*curbeam)) imcount=imcount+1 figmplf.suptitle(suptitle, fontsize=20) if None in labels: labels.remove(None) lines.remove(None) plt.figlegend( lines, labels, loc = 'lower center', ncol=5, labelspacing=0. ) fname= filetemplate+'_{0:0>3}.png'.format(i_fig) plt.savefig(fname) plt.close(figmplf)
def beamvstime(configfile, maindir, params=['Ne'], filetemplate='AltvTime', suptitle='Alt vs Time'): """ This will create a altitude time image for the data for ionocontainer files that are in sphereical coordinates. Inputs Times - A list of times that will be plotted. configfile - The INI file with the simulation parameters that will be useds. maindir - The directory the images will be saved in. params - List of Parameter names that will be ploted. These need to match in the ionocontainer names. filetemplate - The first part of a the file names. suptitle - The supertitle for the plots. """ sns.set_style("whitegrid") sns.set_context("notebook") # rc('text', usetex=True) (sensdict, simparams) = readconfigfile(configfile) paramslower = [ip.lower() for ip in params] Np = len(params) maindir = Path(maindir) inputfile = str(maindir.joinpath('Fitted', 'fitteddata.h5')) Ionofit = IonoContainer.readh5(inputfile) times = Ionofit.Time_Vector Nt = len(times) dataloc = Ionofit.Sphere_Coords pnames = Ionofit.Param_Names pnameslower = sp.array([ip.lower() for ip in pnames.flatten()]) p2fit = [ sp.argwhere(ip == pnameslower)[0][0] if ip in pnameslower else None for ip in paramslower ] angles = dataloc[:, 1:] b = np.ascontiguousarray(angles).view( np.dtype((np.void, angles.dtype.itemsize * angles.shape[1]))) _, idx, invidx = np.unique(b, return_index=True, return_inverse=True) beamlist = angles[idx] Nb = beamlist.shape[0] newfig = True imcount = 0 ifig = -1 for iparam in range(Np): for ibeam in range(Nb): if newfig: (figmplf, axmat) = plt.subplots(3, 3, figsize=(20, 15), facecolor='w', sharex=True, sharey=True) axvec = axmat.flatten() newfig = False ix = 0 ifig += 1 ax = axvec[ix] curbeam = beamlist[ibeam] curparm = paramslower[iparam] if curparm == 'nepow': curparm = 'ne' indxkep = np.argwhere(invidx == ibeam)[:, 0] rng_fit = dataloc[indxkep, 0] rngargs = np.argsort(rng_fit) rng_fit = rng_fit[rngargs] alt_fit = rng_fit * sp.sin(curbeam[1] * sp.pi / 180.) curfit = Ionofit.Param_List[indxkep, :, p2fit[iparam]] curfit = curfit[rngargs] Tmat, Amat = np.meshgrid(times[:, 0], alt_fit) image = ax.pcolor(Tmat, Amat, curfit.real, cmap='viridis') if curparm == 'ne': image.set_norm(colors.LogNorm(vmin=1e9, vmax=5e12)) cbarstr = params[iparam] + ' m-3' else: image.set_norm(colors.PowerNorm(gamma=1., vmin=500, vmax=3e3)) cbarstr = params[iparam] + ' K' if ix > 5: ax.set_xlabel("Time in s") if sp.mod(ix, 3) == 0: ax.set_ylabel('Alt km') ax.set_title('{0} vs Altitude, Az: {1}$^o$ El: {2}$^o$'.format( params[iparam], *curbeam)) imcount = imcount + 1 ix += 1 if ix == 9 or ibeam + 1 == Nb: cbar_ax = figmplf.add_axes([.91, .3, .06, .4]) cbar = plt.colorbar(image, cax=cbar_ax) cbar.set_label(cbarstr) figmplf.suptitle(suptitle, fontsize=20) figmplf.tight_layout(rect=[0, .05, .9, .95]) fname = filetemplate + '_{0:0>3}.png'.format(ifig) plt.savefig(fname) plt.close(figmplf) newfig = True
def getFdof(self): fdof = np.argwhere(np.isnan(self.rvals)).transpose()[0] return fdof.tolist()
# convert strings and instructions to lower case RD['Ingredients and Instructions'] = RD['Ingredients and Instructions'].apply(lambda row: row.lower()) # remove the punctuation using the character deletion step of translate RD['Ingredients and Instructions'] = RD['Ingredients and Instructions'].apply(lambda row: row.translate(None, string.punctuation)) # calculate tf-idf tfidf = TfidfVectorizer(tokenizer=tokenize, stop_words='english') tfidf_mat = tfidf.fit_transform(RD['Ingredients and Instructions']) # Calculate the cosine similarity between two recipes as a sanity check cosine_similarity = pairwise_distances(tfidf_mat, metric='cosine') distances = 1-cosine_similarity cheeseballs_ind = argwhere(Recipes_Names == 'Bacon Cheese Puff Balls')[0][0] flan_ind = argwhere(Recipes_Names == 'Mexican Flan')[0][0] brussels_ind = argwhere(Recipes_Names == 'Buffalo Brussels Sprouts')[0][0] key_lime_bars_ind = argwhere(Recipes_Names == 'Key Lime Bars')[0][0] key_lime_cookies_ind = argwhere(Recipes_Names == 'Key Lime Cookies')[0][0] margarita_ind = argwhere(Recipes_Names == 'Cranberry Margarita')[0][0] print "similarity between bacon puff cheese balls and flan = ", distances[cheeseballs_ind, flan_ind] print "similarity between bacon puff cheese balls and buffalo brussels sprouts = ", distances[cheeseballs_ind, brussels_ind] print "similary between key lime bars and key lime cookies = ", distances[key_lime_bars_ind, key_lime_cookies_ind] print "similarity between key lime bars and cranberry margarita = ", distances[key_lime_bars_ind, margarita_ind] # Now find 5 Nearest Neighbors from sklearn.neighbors import NearestNeighbors neigh = NearestNeighbors(n_neighbors=5, algorithm='auto') neigh.fit(tfidf_mat) # print out the nearest neighbors for a few items
def make_amb(Fsorg,m_up,plen,nlags,nspec=128,winname = 'boxcar'): """ Make the ambiguity function dictionary that holds the lag ambiguity and range ambiguity. Uses a sinc function weighted by a blackman window. Currently only set up for an uncoded pulse. Inputs: Fsorg: A scalar, the original sampling frequency in Hertz. m_up: The upsampled ratio between the original sampling rate and the rate of the ambiguity function up sampling. plen: The length of the pulse in samples at the original sampling frequency. nlags: The number of lags used. Outputs: Wttdict: A dictionary with the keys 'WttAll' which is the full ambiguity function for each lag, 'Wtt' is the max for each lag for plotting, 'Wrange' is the ambiguity in the range with the lag dimension summed, 'Wlag' The ambiguity for the lag, 'Delay' the numpy array for the lag sampling, 'Range' the array for the range sampling and 'WttMatrix' for a matrix that will impart the ambiguity function on a pulses. """ # make the sinc nsamps = sp.floor(8.5*m_up) nsamps = nsamps-(1-sp.mod(nsamps,2)) nvec = sp.arange(-sp.floor(nsamps/2.0),sp.floor(nsamps/2.0)+1) pos_windows = ['boxcar', 'triang', 'blackman', 'hamming', 'hann', 'bartlett', 'flattop', 'parzen', 'bohman', 'blackmanharris', 'nuttall', 'barthann'] curwin = scisig.get_window(winname,nsamps) outsinc = curwin*sp.sinc(nvec/m_up) outsinc = outsinc/sp.sum(outsinc) dt = 1/(Fsorg*m_up) Delay = sp.arange(-(len(nvec)-1),m_up*(nlags+5))*dt t_rng = sp.arange(0,1.5*plen,dt) numdiff = len(Delay)-len(outsinc) outsincpad = sp.pad(outsinc,(0,numdiff),mode='constant',constant_values=(0.0,0.0)) (srng,d2d)=sp.meshgrid(t_rng,Delay) # envelop function envfunc = sp.zeros(d2d.shape) envfunc[(d2d-srng+plen-Delay.min()>=0)&(d2d-srng+plen-Delay.min()<=plen)]=1 envfunc = envfunc/sp.sqrt(envfunc.sum(axis=0).max()) #create the ambiguity function for everything Wtt = sp.zeros((nlags,d2d.shape[0],d2d.shape[1])) cursincrep = sp.tile(outsincpad[:,sp.newaxis],(1,d2d.shape[1])) Wt0 = Wta = cursincrep*envfunc Wt0fft = sp.fft(Wt0,axis=0) for ilag in sp.arange(nlags): cursinc = sp.roll(outsincpad,ilag*m_up) cursincrep = sp.tile(cursinc[:,sp.newaxis],(1,d2d.shape[1])) Wta = cursincrep*envfunc #do fft based convolution, probably best method given sizes Wtafft = scfft.fft(Wta,axis=0) if ilag==0: nmove = len(nvec)-1 else: nmove = len(nvec) Wtt[ilag] = sp.roll(scfft.ifft(Wtafft*sp.conj(Wt0fft),axis=0).real,nmove,axis=0) # make matrix to take # imat = sp.eye(nspec) # tau = sp.arange(-sp.floor(nspec/2.),sp.ceil(nspec/2.))/Fsorg # tauint = Delay # interpmat = spinterp.interp1d(tau,imat,bounds_error=0,axis=0)(tauint) # lagmat = sp.dot(Wtt.sum(axis=2),interpmat) # # triangle window tau = sp.arange(-sp.floor(nspec/2.),sp.ceil(nspec/2.))/Fsorg amb1d = plen-tau amb1d[amb1d<0]=0. amb1d[tau<0]=0. amb1d=amb1d/plen kp = sp.argwhere(amb1d>0).flatten() lagmat = sp.zeros((Wtt.shape[0],nspec)) lagmat.flat[sp.ravel_multi_index((sp.arange(Wtt.shape[0]),kp),lagmat.shape)]=amb1d[kp] Wttdict = {'WttAll':Wtt,'Wtt':Wtt.max(axis=0),'Wrange':Wtt.sum(axis=1),'Wlag':Wtt.sum(axis=2), 'Delay':Delay,'Range':v_C_0*t_rng/2.0,'WttMatrix':lagmat} return Wttdict
def makematPA(Sphere_Coords,timein,configfile): """Make a Ntimeout*Nbeam*Nrng x Ntime*Nloc matrix. The output space will have range repeated first, then beams then time. The coordinates will be [t0,b0,r0],[t0,b0,r1],[t0,b0,r2],... [t0,b1,r0],[t0,b1,r1], ... [t1,b0,r0],[t1,b0,r1],...[t1,b1,r0]...""" # (sensdict,simparams) = readconfigfile(configfile) timeout = simparams['Timevec'] Tint = simparams['Tint'] timeout = sp.column_stack((timeout,timeout+Tint)) fullmat = True rng_vec = simparams['Rangegates'] rng_bin=sensdict['t_s']*v_C_0/1000.0 sumrule = simparams['SUMRULE'] # minrgbin = -sumrule[0].min() maxrgbin = len(rng_vec)-sumrule[1].max() minrg = minrgbin*rng_bin maxrg = maxrgbin*rng_bin angles = simparams['angles'] Nbeams = len(angles) rho = Sphere_Coords[:,0] Az = Sphere_Coords[:,1] El = Sphere_Coords[:,2] rng_vec2 = simparams['Rangegatesfinal'] nrgout = len(rng_vec2) Nlocbeg = len(rho) Ntbeg = len(timein) Ntout = len(timeout) if fullmat: outmat = sp.matrix(sp.zeros((Ntout*Nbeams*nrgout,Nlocbeg*Ntbeg))) else: outmat = sp.sparse((Ntout*Nbeams*nrgout,Nlocbeg*Ntbeg),dype =sp.float64) weights = {ibn:sensdict['ArrayFunc'](Az,El,ib[0],ib[1],sensdict['Angleoffset']) for ibn, ib in enumerate(angles)} for iton,ito in enumerate(timeout): overlaps = sp.array([getOverlap(ito,x) for x in timein]) weights_time = overlaps/overlaps.sum() itpnts = sp.where(weights_time>0)[0] # usually the matrix size is nbeamsxnrange for ibn in range(Nbeams): print('\t\t Making Beam {0:d} of {1:d}'.format(ibn,Nbeams)) weight = weights[ibn] for isamp in range(nrgout): # make the row irow = isamp+ibn*nrgout+iton*nrgout*Nbeams range_g = rng_vec2[isamp] rnglims = [range_g-minrg,range_g+maxrg] rangelog = sp.argwhere((rho>=rnglims[0])&(rho<rnglims[1])) # This is a nearest neighbors interpolation for the spectrums in the range domain if sp.sum(rangelog)==0: minrng = sp.argmin(sp.absolute(range_g-rho)) rangelog[minrng] = True #create the weights and weight location based on the beams pattern. weight_cur =weight[rangelog[:,0]] weight_cur = weight_cur/weight_cur.sum() weight_loc = sp.where(rangelog[:,0])[0] w_loc_rep = sp.tile(weight_loc,len(itpnts)) t_loc_rep = sp.repeat(itpnts,len(weight_loc)) icols = t_loc_rep*Nlocbeg+w_loc_rep weights_final = weights_time[t_loc_rep]*weight_cur[w_loc_rep]*range_g**2/rho[w_loc_rep]**2 outmat[irow,icols] = weights_final return(outmat)
def plotspecs( coords, times, configfile, maindir, cartcoordsys=True, indisp=True, acfdisp=True, fitdisp=True, filetemplate="spec", suptitle="Spectrum Comparison", ): """ This will create a set of images that compare the input ISR spectrum to the output ISR spectrum from the simulator. Inputs coords - An Nx3 numpy array that holds the coordinates of the desired points. times - A numpy list of times in seconds. configfile - The name of the configuration file used. cartcoordsys - (default True)A bool, if true then the coordinates are given in cartisian if false then it is assumed that the coords are given in sphereical coordinates. specsfilename - (default None) The name of the file holding the input spectrum. acfname - (default None) The name of the file holding the estimated ACFs. filetemplate (default 'spec') This is the beginning string used to save the images. """ sns.set_style("whitegrid") sns.set_context("notebook") maindir = Path(maindir).expanduser() acfname = maindir.joinpath("ACF", "00lags.h5") ffit = maindir.joinpath("Fitted", "fitteddata.h5") specsfiledir = maindir.joinpath("Spectrums") (sensdict, simparams) = readconfigfile(configfile) simdtype = simparams["dtype"] npts = simparams["numpoints"] * 3.0 amb_dict = simparams["amb_dict"] if sp.ndim(coords) == 1: coords = coords[sp.newaxis, :] Nt = len(times) Nloc = coords.shape[0] sns.set_style("whitegrid") sns.set_context("notebook") if indisp: dirlist = [i.name for i in specsfiledir.glob("*.h5")] timelist = sp.array([float(i.split()[0]) for i in dirlist]) for itn, itime in enumerate(times): filear = sp.argwhere(timelist >= itime) if len(filear) == 0: filenum = len(timelist) - 1 else: filenum = filear[0][0] specsfilename = specsfiledir.joinpath(dirlist[filenum]) Ionoin = IonoContainer.readh5(str(specsfilename)) if itn == 0: specin = sp.zeros((Nloc, Nt, Ionoin.Param_List.shape[-1])).astype(Ionoin.Param_List.dtype) omeg = Ionoin.Param_Names npts = Ionoin.Param_List.shape[-1] for icn, ic in enumerate(coords): if cartcoordsys: tempin = Ionoin.getclosest(ic, times)[0] else: tempin = Ionoin.getclosestsphere(ic, times)[0] specin[icn, itn] = tempin[0, :] / npts / npts fs = sensdict["fs"] if acfdisp: Ionoacf = IonoContainer.readh5(str(acfname)) ACFin = sp.zeros((Nloc, Nt, Ionoacf.Param_List.shape[-1])).astype(Ionoacf.Param_List.dtype) ts = sensdict["t_s"] omeg = sp.arange(-sp.ceil((npts - 1.0) / 2.0), sp.floor((npts - 1.0) / 2.0) + 1) / ts / npts for icn, ic in enumerate(coords): if cartcoordsys: tempin = Ionoacf.getclosest(ic, times)[0] else: tempin = Ionoacf.getclosestsphere(ic, times)[0] if sp.ndim(tempin) == 1: tempin = tempin[sp.newaxis, :] ACFin[icn] = tempin specout = scfft.fftshift(scfft.fft(ACFin, n=npts, axis=-1), axes=-1) if fitdisp: Ionofit = IonoContainer.readh5(str(ffit)) (omegfit, outspecsfit) = ISRspecmakeout( Ionofit.Param_List, sensdict["fc"], sensdict["fs"], simparams["species"], npts ) Ionofit.Param_List = outspecsfit Ionofit.Param_Names = omegfit specfit = sp.zeros((Nloc, Nt, npts)) for icn, ic in enumerate(coords): if cartcoordsys: tempin = Ionofit.getclosest(ic, times)[0] else: tempin = Ionofit.getclosestsphere(ic, times)[0] if sp.ndim(tempin) == 1: tempin = tempin[sp.newaxis, :] specfit[icn] = tempin / npts / npts nfig = int(sp.ceil(Nt * Nloc / 6.0)) imcount = 0 for i_fig in range(nfig): lines = [None] * 3 labels = [None] * 3 (figmplf, axmat) = plt.subplots(2, 3, figsize=(16, 12), facecolor="w") axvec = axmat.flatten() for iax, ax in enumerate(axvec): if imcount >= Nt * Nloc: break iloc = int(sp.floor(imcount / Nt)) itime = int(imcount - (iloc * Nt)) maxvec = [] if fitdisp: curfitspec = specfit[iloc, itime] rcsfit = curfitspec.sum() (taufit, acffit) = spect2acf(omegfit, curfitspec) guess_acffit = sp.dot(amb_dict["WttMatrix"], acffit) guess_acffit = guess_acffit * rcsfit / guess_acffit[0].real spec_intermfit = scfft.fftshift(scfft.fft(guess_acffit, n=npts)) lines[1] = ax.plot(omeg * 1e-3, spec_intermfit.real, label="Fitted Spectrum", linewidth=5)[0] labels[1] = "Fitted Spectrum" if indisp: # apply ambiguity function to spectrum curin = specin[iloc, itime] rcs = curin.real.sum() (tau, acf) = spect2acf(omeg, curin) guess_acf = sp.dot(amb_dict["WttMatrix"], acf) guess_acf = guess_acf * rcs / guess_acf[0].real # fit to spectrums spec_interm = scfft.fftshift(scfft.fft(guess_acf, n=npts)) maxvec.append(spec_interm.real.max()) lines[0] = ax.plot(omeg * 1e-3, spec_interm.real, label="Input", linewidth=5)[0] labels[0] = "Input Spectrum With Ambiguity Applied" if acfdisp: lines[2] = ax.plot(omeg * 1e-3, specout[iloc, itime].real, label="Output", linewidth=5)[0] labels[2] = "Estimated Spectrum" maxvec.append(specout[iloc, itime].real.max()) ax.set_xlabel("f in kHz") ax.set_ylabel("Amp") ax.set_title("Location {0}, Time {1}".format(coords[iloc], times[itime])) ax.set_ylim(0.0, max(maxvec) * 1) ax.set_xlim([-fs * 5e-4, fs * 5e-4]) imcount = imcount + 1 figmplf.suptitle(suptitle, fontsize=20) if None in labels: labels.remove(None) lines.remove(None) plt.figlegend(lines, labels, loc="lower center", ncol=5, labelspacing=0.0) fname = filetemplate + "_{0:0>3}.png".format(i_fig) plt.savefig(fname) plt.close(figmplf)
def solve(A, maxiters=-1, tolerance=-1,randomTree=False,userTree=None,greedy=0, userX=None,procs=1,useres=1,tracker=0,fixediters=-1): print "Running Setup" n=A.shape[0] L=scipy.sparse.csgraph.laplacian(A) L=L.tocsr() n=L.shape[0] index=edgenumbers.edgenumbers(L) RB=kktmat.kktmat(L) R=RB['R'] B=RB['B'] m=R.shape[0] B=B.tocsr() if(userX!=None): x=userX else: x=scipy.random.rand(n) x=x-scipy.mean(x) b=L*x Adata=A.data if(userTree!=None): T=userTree else: if(randomTree): for i in range(0,len(Adata)): Adata[i]=Adata[i]*(1+scipy.rand()) T=-minimum_spanning_tree(-A) #print T Tdata=T.data for i in range(0,len(Tdata)): Tdata[i]=1 T=T+T.transpose() T=T.tocsc() tree_map=treemap.treemap(L,T.data,T.indices,T.indptr) minidx=scipy.argwhere(T.sum(0)==1)[0][0][1] info=treeinfo.treeinfo(T,minidx,index) parent = info['parent'] depth = info['depth'] gedge = info['gedge'] flow=scipy.zeros(m) cython_utils.initialize_flow(B.data,B.indptr,B.indices,b,parent,depth,gedge,m,flow) F=fundcycles.fundcycles(B.data,B.indptr,B.indices,tree_map,parent,depth,gedge,m) F=F.tocsc() if(greedy!=0): newF=local_greedy.find_basis(L,B,index,n,m,greedy) if(greedy!=0): F=scipy.sparse.hstack([F,newF]) tau = cython_utils.find_tau(F.data,F.indptr,F.indices,R) print "Starting Solve" #itersguess=scipy.ceil(tau*scipy.log2((tau*(tau-m+2*n-2))/(tolerance*tolerance))) if(maxiters < 0 and tolerance <0): maxiters=100000 results=[0,0,0,0,0,0,0,0,0,0,0,0,0,0] probs = cython_utils.get_probs(F.data,F.indptr,F.indices,R,1) temp_results=cython_utils.solve_wrapper(F.data,F.indices,F.indptr, B.data,B.indices,B.indptr, R,1.0, L.data,L.indices,L.indptr, b,probs,flow, parent,depth,gedge,tolerance, procs,x,useres,fixediters,tracker) for i in range(0,10): results[i]=temp_results[i] results[10]=temp_results[10] results[11]=results[10]-scipy.mean(results[10]) results[12]=tau results[13]=1-(1/tau) return results
def CATS2D(mol,PathLength = 10,scale = 3): """ ################################################################# The main program for calculating the CATS descriptors. CATS: chemically advanced template serach ----> CATS_DA0 .... Usage: result=CATS2D(mol,PathLength = 10,scale = 1) Input: mol is a molecule object. PathLength is the max topological distance between two atoms. scale is the normalization method (descriptor scaling method) scale = 1 indicates that no normalization. That is to say: the values of the vector represent raw counts ("counts"). scale = 2 indicates that division by the number of non-hydrogen atoms (heavy atoms) in the molecule. scale = 3 indicates that division of each of 15 possible PPP pairs by the added occurrences of the two respective PPPs. Output: result is a dict format with the definitions of each descritor. ################################################################# """ Hmol = Chem.RemoveHs(mol) AtomNum = Hmol.GetNumAtoms() atomtype = AssignAtomType(Hmol) DistanceMatrix = Chem.GetDistanceMatrix(Hmol) DM = scipy.triu(DistanceMatrix) tempdict = {} for PL in range(0,PathLength): if PL == 0: Index = [[k,k] for k in range(AtomNum)] else: Index1 = scipy.argwhere(DM == PL) Index = [[k[0],k[1]] for k in Index1] temp = [] for j in Index: temp.extend(MatchAtomType(j,atomtype)) tempdict.update({PL:temp}) CATSLabel = FormCATSLabel(PathLength) CATS1 = FormCATSDict(tempdict,CATSLabel) ####set normalization 3 AtomPair = ['DD','DA','DP','DN','DL','AA','AP', 'AN','AL','PP','PN','PL','NN','NL','LL'] temp = [] for i,j in tempdict.items(): temp.extend(j) AtomPairNum = {} for i in AtomPair: AtomPairNum.update({i:temp.count(i)}) ############################################ CATS = {} if scale == 1: CATS = CATS1 if scale == 2: for i in CATS1: CATS.update({i:round(CATS1[i]/(AtomNum+0.0),3)}) if scale == 3: for i in CATS1: if AtomPairNum[i[5:7]] == 0: CATS.update({i:round(CATS1[i],3)}) else: CATS.update({i:round(CATS1[i]/(AtomPairNum[i[5:7]]+0.0),3)}) return CATS
#It is probably good to sleep here, but the readStream will block sufficiently #it just depends on your processing # Range-Doppler Processing # strip off the header txVec = txVec[header_len:] rxBuffs = rxBuffs[header_len:] pulseCompressedTx = np.convolve(txProtoPulse[::-1].conjugate(),txVec) pulseCompressedRx = np.convolve(txProtoPulse[::-1].conjugate(),rxBuffs) ## sort data into slow time and fast time pulseCompressionPeak = np.max(abs(np.convolve(txProtoPulse[::-1], txProtoPulse.conjugate()))) zeroRangeBins = sp.argwhere(pulseCompressedTx==pulseCompressionPeak) assert(zeroRangeBins[1][0] - zeroRangeBins[0][0] == pulseLength) firstZeroRangeBin = zeroRangeBins[0][0] lastZeroRangeBin = zeroRangeBins[-1][0] rangeSamplesTx = np.reshape(pulseCompressedTx[firstZeroRangeBin:lastZeroRangeBin+pulseLength], [numPulses,pulseLength]) rangeSamplesRx = np.reshape(pulseCompressedRx[firstZeroRangeBin:lastZeroRangeBin+pulseLength], [numPulses,pulseLength]) ## TO DO: compute fft over slow time to realize the RD map print("\nDone reading and writing") waveFormFig, waveFormAxList = plt.subplots(2,1,sharex=True) waveFormAxList[0].plot(np.real(txVec),'b')
def makehistdata(params,maindir): """ This will make the histogram data for the statistics. Inputs params - A list of parameters that will have statistics created maindir - The directory that the simulation data is held. Outputs datadict - A dictionary with the data values in numpy arrays. The keys are param names. errordict - A dictionary with the data values in numpy arrays. The keys are param names. errdictrel - A dictionary with the error values in numpy arrays, normalized by the correct value. The keys are param names. """ maindir=Path(maindir) ffit = maindir.joinpath('Fitted','fitteddata.h5') inputfiledir = maindir.joinpath('Origparams') paramslower = [ip.lower() for ip in params] eparamslower = ['n'+ip.lower() for ip in params] # set up data dictionary errordict = {ip:[] for ip in params} errordictrel = {ip:[] for ip in params} #Read in fitted data Ionofit = IonoContainer.readh5(ffit) times=Ionofit.Time_Vector dataloc = Ionofit.Sphere_Coords pnames = Ionofit.Param_Names pnameslower = sp.array([ip.lower() for ip in pnames.flatten()]) p2fit = [sp.argwhere(ip==pnameslower)[0][0] if ip in pnameslower else None for ip in paramslower] datadict = {ip:Ionofit.Param_List[:,:,p2fit[ipn]].flatten() for ipn, ip in enumerate(params)} ep2fit = [sp.argwhere(ip==pnameslower)[0][0] if ip in pnameslower else None for ip in eparamslower] edatadict = {ip:Ionofit.Param_List[:,:,ep2fit[ipn]].flatten() for ipn, ip in enumerate(params)} # Determine which input files are to be used. dirlist = [str(i) for i in inputfiledir.glob('*.h5')] sortlist,outime,filelisting,timebeg,timelist_s = IonoContainer.gettimes(dirlist) time2files = [] for itn,itime in enumerate(times): log1 = (outime[:,0]>=itime[0]) & (outime[:,0]<itime[1]) log2 = (outime[:,1]>itime[0]) & (outime[:,1]<=itime[1]) log3 = (outime[:,0]<=itime[0]) & (outime[:,1]>itime[1]) tempindx = sp.where(log1|log2|log3)[0] time2files.append(filelisting[tempindx]) curfilenum=-1 for iparam,pname in enumerate(params): curparm = paramslower[iparam] # Use Ne from input to compare the ne derived from the power. if curparm == 'nepow': curparm = 'ne' datalist = [] for itn,itime in enumerate(times): for iplot,filenum in enumerate(time2files[itn]): filenum = int(filenum) if curfilenum!=filenum: curfilenum=filenum datafilename = dirlist[filenum] Ionoin = IonoContainer.readh5(datafilename) if ('ti' in paramslower) or ('vi' in paramslower): Ionoin = maketi(Ionoin) pnames = Ionoin.Param_Names pnameslowerin = sp.array([ip.lower() for ip in pnames.flatten()]) prmloc = sp.argwhere(curparm==pnameslowerin) if prmloc.size !=0: curprm = prmloc[0][0] # build up parameter vector bs the range values by finding the closest point in space in the input curdata = sp.zeros(len(dataloc)) for irngn, curcoord in enumerate(dataloc): tempin = Ionoin.getclosestsphere(curcoord,[itime])[0] Ntloc = tempin.shape[0] tempin = sp.reshape(tempin,(Ntloc,len(pnameslowerin))) curdata[irngn] = tempin[0,curprm] datalist.append(curdata) errordict[pname] = datadict[pname]-sp.hstack(datalist) errordictrel[pname] = 100.*errordict[pname]/sp.absolute(sp.hstack(datalist)) return datadict,errordict,errordictrel,edatadict
def plotspecs(coords, times, configfile, maindir, cartcoordsys=True, indisp=True, acfdisp=True, fitdisp=True, filetemplate='spec', suptitle='Spectrum Comparison'): """ This will create a set of images that compare the input ISR spectrum to the output ISR spectrum from the simulator. Inputs coords - An Nx3 numpy array that holds the coordinates of the desired points. times - A numpy list of times in seconds. configfile - The name of the configuration file used. cartcoordsys - (default True)A bool, if true then the coordinates are given in cartisian if false then it is assumed that the coords are given in sphereical coordinates. specsfilename - (default None) The name of the file holding the input spectrum. acfname - (default None) The name of the file holding the estimated ACFs. filetemplate (default 'spec') This is the beginning string used to save the images. """ sns.set_style("whitegrid") sns.set_context("notebook") maindir = Path(maindir).expanduser() acfname = maindir.joinpath('ACF', '00lags.h5') ffit = maindir.joinpath('Fitted', 'fitteddata.h5') specsfiledir = maindir.joinpath('Spectrums') (sensdict, simparams) = readconfigfile(configfile) simdtype = simparams['dtype'] npts = simparams['numpoints'] * 3.0 amb_dict = simparams['amb_dict'] if sp.ndim(coords) == 1: coords = coords[sp.newaxis, :] Nt = len(times) Nloc = coords.shape[0] sns.set_style("whitegrid") sns.set_context("notebook") if indisp: dirlist = [i.name for i in specsfiledir.glob('*.h5')] timelist = sp.array([float(i.split()[0]) for i in dirlist]) for itn, itime in enumerate(times): filear = sp.argwhere(timelist >= itime) if len(filear) == 0: filenum = len(timelist) - 1 else: filenum = filear[0][0] specsfilename = specsfiledir.joinpath(dirlist[filenum]) Ionoin = IonoContainer.readh5(str(specsfilename)) if itn == 0: specin = sp.zeros( (Nloc, Nt, Ionoin.Param_List.shape[-1])).astype( Ionoin.Param_List.dtype) omeg = Ionoin.Param_Names npts = Ionoin.Param_List.shape[-1] for icn, ic in enumerate(coords): if cartcoordsys: tempin = Ionoin.getclosest(ic, times)[0] else: tempin = Ionoin.getclosestsphere(ic, times)[0] specin[icn, itn] = tempin[0, :] / npts / npts fs = sensdict['fs'] if acfdisp: Ionoacf = IonoContainer.readh5(str(acfname)) ACFin = sp.zeros( (Nloc, Nt, Ionoacf.Param_List.shape[-1])).astype(Ionoacf.Param_List.dtype) ts = sensdict['t_s'] omeg = sp.arange(-sp.ceil((npts - 1.) / 2.), sp.floor((npts - 1.) / 2.) + 1) / ts / npts for icn, ic in enumerate(coords): if cartcoordsys: tempin = Ionoacf.getclosest(ic, times)[0] else: tempin = Ionoacf.getclosestsphere(ic, times)[0] if sp.ndim(tempin) == 1: tempin = tempin[sp.newaxis, :] ACFin[icn] = tempin specout = scfft.fftshift(scfft.fft(ACFin, n=npts, axis=-1), axes=-1) if fitdisp: Ionofit = IonoContainer.readh5(str(ffit)) (omegfit, outspecsfit) = ISRspecmakeout(Ionofit.Param_List, sensdict['fc'], sensdict['fs'], simparams['species'], npts) Ionofit.Param_List = outspecsfit Ionofit.Param_Names = omegfit specfit = sp.zeros((Nloc, Nt, npts)) for icn, ic in enumerate(coords): if cartcoordsys: tempin = Ionofit.getclosest(ic, times)[0] else: tempin = Ionofit.getclosestsphere(ic, times)[0] if sp.ndim(tempin) == 1: tempin = tempin[sp.newaxis, :] specfit[icn] = tempin / npts / npts nfig = int(sp.ceil(Nt * Nloc / 6.0)) imcount = 0 for i_fig in range(nfig): lines = [None] * 3 labels = [None] * 3 (figmplf, axmat) = plt.subplots(2, 3, figsize=(16, 12), facecolor='w') axvec = axmat.flatten() for iax, ax in enumerate(axvec): if imcount >= Nt * Nloc: break iloc = int(sp.floor(imcount / Nt)) itime = int(imcount - (iloc * Nt)) maxvec = [] if fitdisp: curfitspec = specfit[iloc, itime] rcsfit = curfitspec.sum() (taufit, acffit) = spect2acf(omegfit, curfitspec) guess_acffit = sp.dot(amb_dict['WttMatrix'], acffit) guess_acffit = guess_acffit * rcsfit / guess_acffit[0].real spec_intermfit = scfft.fftshift(scfft.fft(guess_acffit, n=npts)) lines[1] = ax.plot(omeg * 1e-3, spec_intermfit.real, label='Fitted Spectrum', linewidth=5)[0] labels[1] = 'Fitted Spectrum' if indisp: # apply ambiguity function to spectrum curin = specin[iloc, itime] rcs = curin.real.sum() (tau, acf) = spect2acf(omeg, curin) guess_acf = sp.dot(amb_dict['WttMatrix'], acf) guess_acf = guess_acf * rcs / guess_acf[0].real # fit to spectrums spec_interm = scfft.fftshift(scfft.fft(guess_acf, n=npts)) maxvec.append(spec_interm.real.max()) lines[0] = ax.plot(omeg * 1e-3, spec_interm.real, label='Input', linewidth=5)[0] labels[0] = 'Input Spectrum With Ambiguity Applied' if acfdisp: lines[2] = ax.plot(omeg * 1e-3, specout[iloc, itime].real, label='Output', linewidth=5)[0] labels[2] = 'Estimated Spectrum' maxvec.append(specout[iloc, itime].real.max()) ax.set_xlabel('f in kHz') ax.set_ylabel('Amp') ax.set_title('Location {0}, Time {1}'.format( coords[iloc], times[itime])) ax.set_ylim(0.0, max(maxvec) * 1) ax.set_xlim([-fs * 5e-4, fs * 5e-4]) imcount = imcount + 1 figmplf.suptitle(suptitle, fontsize=20) if None in labels: labels.remove(None) lines.remove(None) plt.figlegend(lines, labels, loc='lower center', ncol=5, labelspacing=0.) fname = filetemplate + '_{0:0>3}.png'.format(i_fig) plt.savefig(fname) plt.close(figmplf)
def beamvstime(configfile, maindir, params=["Ne"], filetemplate="AltvTime", suptitle="Alt vs Time"): """ This will create a altitude time image for the data for ionocontainer files that are in sphereical coordinates. Inputs Times - A list of times that will be plotted. configfile - The INI file with the simulation parameters that will be useds. maindir - The directory the images will be saved in. params - List of Parameter names that will be ploted. These need to match in the ionocontainer names. filetemplate - The first part of a the file names. suptitle - The supertitle for the plots. """ sns.set_style("whitegrid") sns.set_context("notebook") # rc('text', usetex=True) (sensdict, simparams) = readconfigfile(configfile) paramslower = [ip.lower() for ip in params] Np = len(params) maindir = Path(maindir) inputfile = str(maindir.joinpath("Fitted", "fitteddata.h5")) Ionofit = IonoContainer.readh5(inputfile) times = Ionofit.Time_Vector Nt = len(times) dataloc = Ionofit.Sphere_Coords pnames = Ionofit.Param_Names pnameslower = sp.array([ip.lower() for ip in pnames.flatten()]) p2fit = [sp.argwhere(ip == pnameslower)[0][0] if ip in pnameslower else None for ip in paramslower] angles = dataloc[:, 1:] b = np.ascontiguousarray(angles).view(np.dtype((np.void, angles.dtype.itemsize * angles.shape[1]))) _, idx, invidx = np.unique(b, return_index=True, return_inverse=True) beamlist = angles[idx] Nb = beamlist.shape[0] newfig = True imcount = 0 ifig = -1 for iparam in range(Np): for ibeam in range(Nb): if newfig: (figmplf, axmat) = plt.subplots(3, 3, figsize=(20, 15), facecolor="w", sharex=True, sharey=True) axvec = axmat.flatten() newfig = False ix = 0 ifig += 1 ax = axvec[ix] curbeam = beamlist[ibeam] curparm = paramslower[iparam] if curparm == "nepow": curparm = "ne" indxkep = np.argwhere(invidx == ibeam)[:, 0] rng_fit = dataloc[indxkep, 0] rngargs = np.argsort(rng_fit) rng_fit = rng_fit[rngargs] alt_fit = rng_fit * sp.sin(curbeam[1] * sp.pi / 180.0) curfit = Ionofit.Param_List[indxkep, :, p2fit[iparam]] curfit = curfit[rngargs] Tmat, Amat = np.meshgrid(times[:, 0], alt_fit) image = ax.pcolor(Tmat, Amat, curfit, cmap="jet") if curparm == "ne": image.set_norm(colors.LogNorm(vmin=1e9, vmax=5e12)) cbarstr = params[iparam] + " m-3" else: image.set_norm(colors.PowerNorm(gamma=1.0, vmin=500, vmax=3e3)) cbarstr = params[iparam] + " K" if ix > 5: ax.set_xlabel("Time in s") if sp.mod(ix, 3) == 0: ax.set_ylabel("Alt km") ax.set_title("{0} vs Altitude, Az: {1}$^o$ El: {2}$^o$".format(params[iparam], *curbeam)) imcount = imcount + 1 ix += 1 if ix == 9 or ibeam + 1 == Nb: cbar_ax = figmplf.add_axes([0.91, 0.3, 0.06, 0.4]) cbar = plt.colorbar(image, cax=cbar_ax) cbar.set_label(cbarstr) figmplf.suptitle(suptitle, fontsize=20) figmplf.tight_layout(rect=[0, 0.05, 0.9, 0.95]) fname = filetemplate + "_{0:0>3}.png".format(ifig) plt.savefig(fname) plt.close(figmplf) newfig = True
def RSA(im: array, radius: int, volume_fraction: int = 1, mode: str = 'extended'): r""" Generates a sphere or disk packing using Random Sequential Addition This which ensures that spheres do not overlap but does not guarantee they are tightly packed. Parameters ---------- im : ND-array The image into which the spheres should be inserted. By accepting an image rather than a shape, it allows users to insert spheres into an already existing image. To begin the process, start with an array of zero such as ``im = np.zeros([200, 200], dtype=bool)``. radius : int The radius of the disk or sphere to insert. volume_fraction : scalar The fraction of the image that should be filled with spheres. The spheres are addeds 1's, so each sphere addition increases the ``volume_fraction`` until the specified limit is reach. mode : string Controls how the edges of the image are handled. Options are: 'extended' - Spheres are allowed to extend beyond the edge of the image 'contained' - Spheres are all completely within the image 'periodic' - The portion of a sphere that extends beyond the image is inserted into the opposite edge of the image (Not Implemented Yet!) Returns ------- image : ND-array A copy of ``im`` with spheres of specified radius *added* to the background. Notes ----- Each sphere is filled with 1's, but the center is marked with a 2. This allows easy boolean masking to extract only the centers, which can be converted to coordinates using ``scipy.where`` and used for other purposes. The obtain only the spheres, use``im = im == 1``. This function adds spheres to the background of the received ``im``, which allows iteratively adding spheres of different radii to the unfilled space. References ---------- [1] Random Heterogeneous Materials, S. Torquato (2001) """ # Note: The 2D vs 3D splitting of this just me being lazy...I can't be # bothered to figure it out programmatically right now # TODO: Ideally the spheres should be added periodically print(78*'―') print('RSA: Adding spheres of size ' + str(radius)) d2 = len(im.shape) == 2 mrad = 2*radius if d2: im_strel = ps_disk(radius) mask_strel = ps_disk(mrad) else: im_strel = ps_ball(radius) mask_strel = ps_ball(mrad) if sp.any(im > 0): # Dilate existing objects by im_strel to remove pixels near them # from consideration for sphere placement mask = ps.tools.fftmorphology(im > 0, im_strel > 0, mode='dilate') mask = mask.astype(int) else: mask = sp.zeros_like(im) if mode == 'contained': mask = _remove_edge(mask, radius) elif mode == 'extended': pass elif mode == 'periodic': raise Exception('Periodic edges are not implemented yet') else: raise Exception('Unrecognized mode: ' + mode) vf = im.sum()/im.size free_spots = sp.argwhere(mask == 0) i = 0 while vf <= volume_fraction and len(free_spots) > 0: choice = sp.random.randint(0, len(free_spots), size=1) if d2: [x, y] = free_spots[choice].flatten() im = _fit_strel_to_im_2d(im, im_strel, radius, x, y) mask = _fit_strel_to_im_2d(mask, mask_strel, mrad, x, y) im[x, y] = 2 else: [x, y, z] = free_spots[choice].flatten() im = _fit_strel_to_im_3d(im, im_strel, radius, x, y, z) mask = _fit_strel_to_im_3d(mask, mask_strel, mrad, x, y, z) im[x, y, z] = 2 free_spots = sp.argwhere(mask == 0) vf = im.sum()/im.size i += 1 if vf > volume_fraction: print('Volume Fraction', volume_fraction, 'reached') if len(free_spots) == 0: print('No more free spots', 'Volume Fraction', vf) return im
import matplotlib.pyplot as plt import matplotlib.patches as mpatches # Opens the csv file containing edge & node information. link = sc.genfromtxt("../data/QMEE_Net_Mat_edges.csv", delimiter=",") node = sc.genfromtxt("../data/QMEE_Net_Mat_nodes.csv", delimiter=",", dtype=str) # Removes the location names links = link[1:, :] # Extracts the institution names from the node data. nodes = node[1:, 0] # Identifies the presence of links between collaborating sites and creates an array of these links adjacency = sc.argwhere(links > 1.) # Creates a tuple of connections between sites using the adjacency array. connect = () for i in adjacency: connect = connect + ((i[0], i[1]), ) # Creates a list of link weights using the links array. weights = [] for i in adjacency: weights.append((links[i[0], i[1]]) / 10) # Creates the variable NodSizs NodSizs = [] for i in node[1:, 2]: NodSizs.append(float(i) * 77)
def plotbeamparametersv2(times, configfile, maindir, fitdir='Fitted', params=['Ne'], filetemplate='params', suptitle='Parameter Comparison', werrors=False, nelog=True): """ This function will plot the desired parameters for each beam along range. The values of the input and measured parameters will be plotted Inputs Times - A list of times that will be plotted. configfile - The INI file with the simulation parameters that will be useds. maindir - The directory the images will be saved in. params - List of Parameter names that will be ploted. These need to match in the ionocontainer names. filetemplate - The first part of a the file names. suptitle - The supertitle for the plots. werrors - A bools that determines if the errors will be plotted. """ sns.set_style("whitegrid") sns.set_context("notebook") # rc('text', usetex=True) maindir = Path(maindir) ffit = maindir / fitdir / 'fitteddata.h5' inputfiledir = maindir / 'Origparams' (sensdict, simparams) = readconfigfile(configfile) paramslower = [ip.lower() for ip in params] Nt = len(times) Np = len(params) #Read in fitted data Ionofit = IonoContainer.readh5(str(ffit)) dataloc = Ionofit.Sphere_Coords pnames = Ionofit.Param_Names pnameslower = sp.array([ip.lower() for ip in pnames.flatten()]) p2fit = [ sp.argwhere(ip == pnameslower)[0][0] if ip in pnameslower else None for ip in paramslower ] time2fit = [None] * Nt # Have to fix this because of time offsets if times[0] == 0: times += Ionofit.Time_Vector[0, 0] for itn, itime in enumerate(times): filear = sp.argwhere(Ionofit.Time_Vector[:, 0] >= itime) if len(filear) == 0: filenum = len(Ionofit.Time_Vector) - 1 else: filenum = sp.argmin(sp.absolute(Ionofit.Time_Vector[:, 0] - itime)) time2fit[itn] = filenum times_int = [Ionofit.Time_Vector[i] for i in time2fit] # determine the beams angles = dataloc[:, 1:] rng = sp.unique(dataloc[:, 0]) b_arr = np.ascontiguousarray(angles).view( np.dtype((np.void, angles.dtype.itemsize * angles.shape[1]))) _, idx, invidx = np.unique(b_arr, return_index=True, return_inverse=True) beamlist = angles[idx] Nb = beamlist.shape[0] # Determine which imput files are to be used. dirlist = sorted(inputfiledir.glob('*.h5')) dirliststr = [str(i) for i in dirlist] sortlist, outime, outfilelist, timebeg, timelist_s = IonoContainer.gettimes( dirliststr) timelist = timebeg.copy() time2file = [None] * Nt time2intime = [None] * Nt # go through times find files and then times in files for itn, itime in enumerate(times): filear = sp.argwhere(timelist >= itime) if len(filear) == 0: filenum = [len(timelist) - 1] else: filenum = filear[0] flist1 = [] timeinflist = [] for ifile in filenum: filetimes = timelist_s[ifile] log1 = (filetimes[:, 0] >= times_int[itn][0]) & (filetimes[:, 0] < times_int[itn][1]) log2 = (filetimes[:, 1] > times_int[itn][0]) & (filetimes[:, 1] <= times_int[itn][1]) log3 = (filetimes[:, 0] <= times_int[itn][0]) & (filetimes[:, 1] > times_int[itn][1]) log4 = (filetimes[:, 0] > times_int[itn][0]) & (filetimes[:, 1] < times_int[itn][1]) curtimes1 = sp.where(log1 | log2 | log3 | log4)[0].tolist() flist1 = flist1 + [ifile] * len(curtimes1) timeinflist = timeinflist + curtimes1 time2intime[itn] = timeinflist time2file[itn] = flist1 nfig = int(sp.ceil(Nt * Nb)) imcount = 0 curfilenum = -1 # Loop for the figures for i_fig in range(nfig): lines = [None] * 2 labels = [None] * 2 (figmplf, axmat) = plt.subplots(int(sp.ceil(Np / 2)), 2, figsize=(20, 15), facecolor='w') axvec = axmat.flatten() # loop that goes through each axis loops through each parameter, beam # then time. for ax in axvec: if imcount >= Nt * Nb * Np: break imcount_f = float(imcount) itime = int(sp.floor(imcount_f / Nb / Np)) iparam = int(imcount_f / Nb - Np * itime) ibeam = int(imcount_f - (itime * Np * Nb + iparam * Nb)) curbeam = beamlist[ibeam] altlist = sp.sin(curbeam[1] * sp.pi / 180.) * rng curparm = paramslower[iparam] # Use Ne from input to compare the ne derived from the power. if curparm == 'nepow': curparm_in = 'ne' else: curparm_in = curparm curcoord = sp.zeros(3) curcoord[1:] = curbeam for iplot, filenum in enumerate(time2file[itime]): if curfilenum != filenum: curfilenum = filenum datafilename = dirlist[filenum] Ionoin = IonoContainer.readh5(str(datafilename)) if ('ti' in paramslower) or ('vi' in paramslower): Ionoin = maketi(Ionoin) pnames = Ionoin.Param_Names pnameslowerin = sp.array( [ip.lower() for ip in pnames.flatten()]) prmloc = sp.argwhere(curparm_in == pnameslowerin) if prmloc.size != 0: curprm = prmloc[0][0] # build up parameter vector bs the range values by finding the closest point in space in the input curdata = sp.zeros(len(rng)) for irngn, irng in enumerate(rng): curcoord[0] = irng tempin = Ionoin.getclosestsphere(curcoord)[0][ time2intime[itime]] Ntloc = tempin.shape[0] tempin = sp.reshape(tempin, (Ntloc, len(pnameslowerin))) curdata[irngn] = tempin[0, curprm] #actual plotting of the input data lines[0] = ax.plot(curdata, altlist, marker='o', c='b', linewidth=2)[0] labels[0] = 'Input Parameters' # Plot fitted data for the axis indxkep = np.argwhere(invidx == ibeam)[:, 0] curfit = Ionofit.Param_List[indxkep, time2fit[itime], p2fit[iparam]] rng_fit = dataloc[indxkep, 0] alt_fit = rng_fit * sp.sin(curbeam[1] * sp.pi / 180.) errorexist = 'n' + paramslower[iparam] in pnameslower if errorexist and werrors: eparam = sp.argwhere('n' + paramslower[iparam] == pnameslower)[0][0] curerror = Ionofit.Param_List[indxkep, time2fit[itime], eparam] lines[1] = ax.errorbar(curfit, alt_fit, xerr=curerror, fmt='-.', c='g', linewidth=2)[0] else: lines[1] = ax.plot(curfit, alt_fit, marker='o', c='g', linewidth=2)[0] labels[1] = 'Fitted Parameters' # get and plot the input data numplots = len(time2file[itime]) # set the limit for the parameter if curparm == 'vi': ax.set(xlim=[ -1.25 * sp.amax(sp.absolute(curfit)), 1.25 * sp.amax(sp.absolute(curfit)) ]) elif curparm_in != 'ne': ax.set(xlim=[ 0.75 * sp.amin(curfit), sp.minimum(1.25 * sp.amax(curfit), 8000.) ]) elif (curparm_in == 'ne') and nelog: ax.set_xscale('log') ax.set_xlabel(params[iparam]) ax.set_ylabel('Alt km') ax.set_title( '{0} vs Altitude, Time: {1}s Az: {2}$^o$ El: {3}$^o$'.format( params[iparam], times[itime], *curbeam)) imcount += 1 # save figure figmplf.suptitle(suptitle, fontsize=20) if None in labels: labels.remove(None) lines.remove(None) plt.figlegend(lines, labels, loc='lower center', ncol=5, labelspacing=0.) fname = filetemplate + '_{0:0>3}.png'.format(i_fig) plt.savefig(fname) plt.close(figmplf)
def CATS2D(mol, PathLength=10, scale=3): """ ################################################################# The main program for calculating the CATS descriptors. CATS: chemically advanced template serach ----> CATS_DA0 .... Usage: result=CATS2D(mol,PathLength = 10,scale = 1) Input: mol is a molecule object. PathLength is the max topological distance between two atoms. scale is the normalization method (descriptor scaling method) scale = 1 indicates that no normalization. That is to say: the values of the vector represent raw counts ("counts"). scale = 2 indicates that division by the number of non-hydrogen atoms (heavy atoms) in the molecule. scale = 3 indicates that division of each of 15 possible PPP pairs by the added occurrences of the two respective PPPs. Output: result is a dict format with the definitions of each descritor. ################################################################# """ Hmol = Chem.RemoveHs(mol) AtomNum = Hmol.GetNumAtoms() atomtype = AssignAtomType(Hmol) DistanceMatrix = Chem.GetDistanceMatrix(Hmol) DM = scipy.triu(DistanceMatrix) tempdict = {} for PL in range(0, PathLength): if PL == 0: Index = [[k, k] for k in range(AtomNum)] else: Index1 = scipy.argwhere(DM == PL) Index = [[k[0], k[1]] for k in Index1] temp = [] for j in Index: temp.extend(MatchAtomType(j, atomtype)) tempdict.update({PL: temp}) CATSLabel = FormCATSLabel(PathLength) CATS1 = FormCATSDict(tempdict, CATSLabel) ####set normalization 3 AtomPair = [ "DD", "DA", "DP", "DN", "DL", "AA", "AP", "AN", "AL", "PP", "PN", "PL", "NN", "NL", "LL", ] temp = [] for i, j in tempdict.items(): temp.extend(j) AtomPairNum = {} for i in AtomPair: AtomPairNum.update({i: temp.count(i)}) ############################################ CATS = {} if scale == 1: CATS = CATS1 if scale == 2: for i in CATS1: CATS.update({i: round(CATS1[i] / (AtomNum + 0.0), 3)}) if scale == 3: for i in CATS1: if AtomPairNum[i[5:7]] == 0: CATS.update({i: round(CATS1[i], 3)}) else: CATS.update( {i: round(CATS1[i] / (AtomPairNum[i[5:7]] + 0.0), 3)}) return CATS
def plotacfs(coords, times, configfile, maindir, cartcoordsys=True, indisp=True, acfdisp=True, fitdisp=True, filetemplate='acf', suptitle='ACF Comparison', invacf=''): """ This will create a set of images that compare the input ISR acf to the output ISR acfs from the simulator. Inputs coords - An Nx3 numpy array that holds the coordinates of the desired points. times - A numpy list of times in seconds. configfile - The name of the configuration file used. cartcoordsys - (default True)A bool, if true then the coordinates are given in cartisian if false then it is assumed that the coords are given in sphereical coordinates. specsfilename - (default None) The name of the file holding the input spectrum. acfname - (default None) The name of the file holding the estimated ACFs. filetemplate (default 'spec') This is the beginning string used to save the images. """ # indisp = specsfilename is not None # acfdisp = acfname is not None maindir = Path(maindir).expanduser() sns.set_style("whitegrid") sns.set_context("notebook") acfname = maindir.joinpath('ACF', '00lags.h5') ffit = maindir.joinpath('Fitted', 'fitteddata.h5') specsfiledir = maindir.joinpath('Spectrums') (sensdict, simparams) = readconfigfile(configfile) simdtype = simparams['dtype'] npts = simparams['numpoints'] * 3.0 amb_dict = simparams['amb_dict'] if sp.ndim(coords) == 1: coords = coords[sp.newaxis, :] Nt = len(times) Nloc = coords.shape[0] sns.set_style("whitegrid") sns.set_context("notebook") pulse = simparams['Pulse'] ts = sensdict['t_s'] tau1 = sp.arange(pulse.shape[-1]) * ts if indisp: dirlist = [i.name for i in specsfiledir.glob('*.h5')] timelist = sp.array([float(i.split()[0]) for i in dirlist]) for itn, itime in enumerate(times): filear = sp.argwhere(timelist >= itime) if len(filear) == 0: filenum = len(timelist) - 1 else: filenum = filear[0][0] specsfilename = specsfiledir.joinpath(dirlist[filenum]) Ionoin = IonoContainer.readh5(str(specsfilename)) if itn == 0: specin = sp.zeros( (Nloc, Nt, Ionoin.Param_List.shape[-1])).astype( Ionoin.Param_List.dtype) omeg = Ionoin.Param_Names npts = Ionoin.Param_List.shape[-1] for icn, ic in enumerate(coords): if cartcoordsys: tempin = Ionoin.getclosest(ic, times)[0] else: tempin = Ionoin.getclosestsphere(ic, times)[0] # if sp.ndim(tempin)==1: # tempin = tempin[sp.newaxis,:] specin[icn, itn] = tempin[0, :] / npts if acfdisp: Ionoacf = IonoContainer.readh5(str(acfname)) ACFin = sp.zeros( (Nloc, Nt, Ionoacf.Param_List.shape[-1])).astype(Ionoacf.Param_List.dtype) omeg = sp.arange(-sp.ceil((npts + 1) / 2), sp.floor( (npts + 1) / 2)) / ts / npts for icn, ic in enumerate(coords): if cartcoordsys: tempin = Ionoacf.getclosest(ic, times)[0] else: tempin = Ionoacf.getclosestsphere(ic, times)[0] if sp.ndim(tempin) == 1: tempin = tempin[sp.newaxis, :] ACFin[icn] = tempin # Determine the inverse ACF stuff if len(invacf) == 0: invacfbool = False else: invacfbool = True invfile = maindir.joinpath('ACFInv', '00lags' + invacf + '.h5') Ionoacfinv = IonoContainer.readh5(str(invfile)) ACFinv = sp.zeros((Nloc, Nt, Ionoacfinv.Param_List.shape[-1])).astype( Ionoacfinv.Param_List.dtype) for icn, ic in enumerate(coords): if cartcoordsys: tempin = Ionoacfinv.getclosest(ic, times)[0] else: tempin = Ionoacfinv.getclosestsphere(ic, times)[0] if sp.ndim(tempin) == 1: tempin = tempin[sp.newaxis, :] ACFinv[icn] = tempin if fitdisp: Ionofit = IonoContainer.readh5(str(ffit)) (omegfit, outspecsfit) = ISRspecmakeout(Ionofit.Param_List, sensdict['fc'], sensdict['fs'], simparams['species'], npts) Ionofit.Param_List = outspecsfit Ionofit.Param_Names = omegfit specfit = sp.zeros((Nloc, Nt, npts)) for icn, ic in enumerate(coords): if cartcoordsys: tempin = Ionofit.getclosest(ic, times)[0] else: tempin = Ionofit.getclosestsphere(ic, times)[0] if sp.ndim(tempin) == 1: tempin = tempin[sp.newaxis, :] specfit[icn] = tempin / npts / npts nfig = int(sp.ceil(Nt * Nloc / 3.)) imcount = 0 for i_fig in range(nfig): lines = [None] * 4 labels = [None] * 4 lines_im = [None] * 4 labels_im = [None] * 4 (figmplf, axmat) = plt.subplots(3, 2, figsize=(16, 12), facecolor='w') for ax in axmat: if imcount >= Nt * Nloc: break iloc = int(sp.floor(imcount / Nt)) itime = int(imcount - (iloc * Nt)) maxvec = [] minvec = [] if indisp: # apply ambiguity funciton to spectrum curin = specin[iloc, itime] (tau, acf) = spect2acf(omeg, curin) acf1 = scfft.ifftshift(acf)[:len(pulse)] * len(curin) rcs = acf1[0].real guess_acf = sp.dot(amb_dict['WttMatrix'], acf) guess_acf = guess_acf * rcs / guess_acf[0].real # fit to spectrums maxvec.append(guess_acf.real.max()) maxvec.append(guess_acf.imag.max()) minvec.append(acf1.real.min()) minvec.append(acf1.imag.min()) lines[0] = ax[0].plot(tau1 * 1e6, guess_acf.real, label='Input', linewidth=5)[0] labels[0] = 'Input ACF With Ambiguity Applied' lines_im[0] = ax[1].plot(tau1 * 1e6, guess_acf.imag, label='Input', linewidth=5)[0] labels_im[0] = 'Input ACF With Ambiguity Applied' if fitdisp: curinfit = specfit[iloc, itime] (taufit, acffit) = spect2acf(omegfit, curinfit) rcsfit = curinfit.sum() guess_acffit = sp.dot(amb_dict['WttMatrix'], acffit) guess_acffit = guess_acffit * rcsfit / guess_acffit[0].real lines[1] = ax[0].plot(tau1 * 1e6, guess_acffit.real, label='Input', linewidth=5)[0] labels[1] = 'Fitted ACF' lines_im[1] = ax[1].plot(tau1 * 1e6, guess_acffit.imag, label='Input', linewidth=5)[0] labels_im[1] = 'Fitted ACF' if acfdisp: lines[2] = ax[0].plot(tau1 * 1e6, ACFin[iloc, itime].real, label='Output', linewidth=5)[0] labels[2] = 'Estimated ACF' lines_im[2] = ax[1].plot(tau1 * 1e6, ACFin[iloc, itime].imag, label='Output', linewidth=5)[0] labels_im[2] = 'Estimated ACF' maxvec.append(ACFin[iloc, itime].real.max()) maxvec.append(ACFin[iloc, itime].imag.max()) minvec.append(ACFin[iloc, itime].real.min()) minvec.append(ACFin[iloc, itime].imag.min()) if invacfbool: lines[3] = ax[0].plot(tau1 * 1e6, ACFinv[iloc, itime].real, label='Output', linewidth=5)[0] labels[3] = 'Reconstructed ACF' lines_im[3] = ax[1].plot(tau1 * 1e6, ACFinv[iloc, itime].imag, label='Output', linewidth=5)[0] labels_im[3] = 'Reconstructed ACF' ax[0].set_xlabel(r'$\tau$ in $\mu$s') ax[0].set_ylabel('Amp') ax[0].set_title( 'Real Part' ) # Location {0}, Time {1}'.format(coords[iloc],times[itime])) ax[0].set_ylim(min(minvec), max(maxvec) * 1) ax[0].set_xlim([tau1.min() * 1e6, tau1.max() * 1e6]) ax[1].set_xlabel(r'$\tau$ in $\mu$s') ax[1].set_ylabel('Amp') ax[1].set_title( 'Imag Part' ) # Location {0}, Time {1}'.format(coords[iloc],times[itime])) ax[1].set_ylim(min(minvec), max(maxvec) * 1) ax[1].set_xlim([tau1.min() * 1e6, tau1.max() * 1e6]) imcount = imcount + 1 figmplf.suptitle(suptitle, fontsize=20) if None in labels: labels.remove(None) lines.remove(None) plt.figlegend(lines, labels, loc='lower center', ncol=5, labelspacing=0.) fname = filetemplate + '_{0:0>3}.png'.format(i_fig) plt.savefig(fname, dpi=300) plt.close(figmplf)
def plotbeamparametersv2( times, configfile, maindir, fitdir="Fitted", params=["Ne"], filetemplate="params", suptitle="Parameter Comparison", werrors=False, nelog=True, ): """ This function will plot the desired parameters for each beam along range. The values of the input and measured parameters will be plotted Inputs Times - A list of times that will be plotted. configfile - The INI file with the simulation parameters that will be useds. maindir - The directory the images will be saved in. params - List of Parameter names that will be ploted. These need to match in the ionocontainer names. filetemplate - The first part of a the file names. suptitle - The supertitle for the plots. werrors - A bools that determines if the errors will be plotted. """ sns.set_style("whitegrid") sns.set_context("notebook") # rc('text', usetex=True) maindir = Path(maindir) ffit = maindir / fitdir / "fitteddata.h5" inputfiledir = maindir / "Origparams" (sensdict, simparams) = readconfigfile(configfile) paramslower = [ip.lower() for ip in params] Nt = len(times) Np = len(params) # Read in fitted data Ionofit = IonoContainer.readh5(str(ffit)) dataloc = Ionofit.Sphere_Coords pnames = Ionofit.Param_Names pnameslower = sp.array([ip.lower() for ip in pnames.flatten()]) p2fit = [sp.argwhere(ip == pnameslower)[0][0] if ip in pnameslower else None for ip in paramslower] time2fit = [None] * Nt for itn, itime in enumerate(times): filear = sp.argwhere(Ionofit.Time_Vector >= itime) if len(filear) == 0: filenum = len(Ionofit.Time_Vector) - 1 else: filenum = filear[0][0] time2fit[itn] = filenum times_int = [Ionofit.Time_Vector[i] for i in time2fit] # determine the beams angles = dataloc[:, 1:] rng = sp.unique(dataloc[:, 0]) b = np.ascontiguousarray(angles).view(np.dtype((np.void, angles.dtype.itemsize * angles.shape[1]))) _, idx, invidx = np.unique(b, return_index=True, return_inverse=True) beamlist = angles[idx] Nb = beamlist.shape[0] # Determine which imput files are to be used. dirlist = sorted(inputfiledir.glob("*.h5")) dirliststr = [str(i) for i in dirlist] filesonly = [ifile.name for ifile in dirlist] sortlist, outime, outfilelist, timebeg, timelist_s = IonoContainer.gettimes(dirliststr) timelist = sp.array([float(i.split()[0]) for i in filesonly]) time2file = [None] * Nt time2intime = [None] * Nt # go through times find files and then times in files for itn, itime in enumerate(times): filear = sp.argwhere(timelist >= itime) if len(filear) == 0: filenum = [len(timelist) - 1] else: filenum = filear[0] flist1 = [] timeinflist = [] for ifile in filenum: filetimes = timelist_s[ifile] log1 = (filetimes[:, 0] >= times_int[itn][0]) & (filetimes[:, 0] < times_int[itn][1]) log2 = (filetimes[:, 1] > times_int[itn][0]) & (filetimes[:, 1] <= times_int[itn][1]) log3 = (filetimes[:, 0] <= times_int[itn][0]) & (filetimes[:, 1] > times_int[itn][1]) log4 = (filetimes[:, 0] > times_int[itn][0]) & (filetimes[:, 1] < times_int[itn][1]) curtimes1 = sp.where(log1 | log2 | log3 | log4)[0].tolist() flist1 = flist1 + [ifile] * len(curtimes1) timeinflist = timeinflist + curtimes1 time2intime[itn] = timeinflist time2file[itn] = flist1 nfig = int(sp.ceil(Nt * Nb)) imcount = 0 curfilenum = -1 # Loop for the figures for i_fig in range(nfig): lines = [None] * 2 labels = [None] * 2 (figmplf, axmat) = plt.subplots(int(sp.ceil(Np / 2)), 2, figsize=(20, 15), facecolor="w") axvec = axmat.flatten() # loop that goes through each axis loops through each parameter, beam # then time. for iax, ax in enumerate(axvec): if imcount >= Nt * Nb * Np: break itime = int(sp.floor(imcount / Nb / Np)) iparam = int(imcount / Nb - Np * itime) ibeam = int(imcount - (itime * Np * Nb + iparam * Nb)) curbeam = beamlist[ibeam] altlist = sp.sin(curbeam[1] * sp.pi / 180.0) * rng curparm = paramslower[iparam] # Use Ne from input to compare the ne derived from the power. if curparm == "nepow": curparm_in = "ne" else: curparm_in = curparm curcoord = sp.zeros(3) curcoord[1:] = curbeam for iplot, filenum in enumerate(time2file[itime]): if curfilenum != filenum: curfilenum = filenum datafilename = dirlist[filenum] Ionoin = IonoContainer.readh5(str(datafilename)) if ("ti" in paramslower) or ("vi" in paramslower): Ionoin = maketi(Ionoin) pnames = Ionoin.Param_Names pnameslowerin = sp.array([ip.lower() for ip in pnames.flatten()]) prmloc = sp.argwhere(curparm_in == pnameslowerin) if prmloc.size != 0: curprm = prmloc[0][0] # build up parameter vector bs the range values by finding the closest point in space in the input curdata = sp.zeros(len(rng)) for irngn, irng in enumerate(rng): curcoord[0] = irng tempin = Ionoin.getclosestsphere(curcoord)[0][time2intime[itime]] Ntloc = tempin.shape[0] tempin = sp.reshape(tempin, (Ntloc, len(pnameslowerin))) curdata[irngn] = tempin[0, curprm] # actual plotting of the input data lines[0] = ax.plot(curdata, altlist, marker="o", c="b", linewidth=2)[0] labels[0] = "Input Parameters" # Plot fitted data for the axis indxkep = np.argwhere(invidx == ibeam)[:, 0] curfit = Ionofit.Param_List[indxkep, time2fit[itime], p2fit[iparam]] rng_fit = dataloc[indxkep, 0] alt_fit = rng_fit * sp.sin(curbeam[1] * sp.pi / 180.0) errorexist = "n" + paramslower[iparam] in pnameslower if errorexist and werrors: eparam = sp.argwhere("n" + paramslower[iparam] == pnameslower)[0][0] curerror = Ionofit.Param_List[indxkep, time2fit[itime], eparam] lines[1] = ax.errorbar(curfit, alt_fit, xerr=curerror, fmt="-.", c="g", linewidth=2)[0] else: lines[1] = ax.plot(curfit, alt_fit, marker="o", c="g", linewidth=2)[0] labels[1] = "Fitted Parameters" # get and plot the input data numplots = len(time2file[itime]) # set the limit for the parameter if curparm_in != "ne": ax.set(xlim=[0.75 * sp.amin(curfit), 1.25 * sp.amax(curfit)]) if curparm == "vi": ax.set(xlim=[-1.25 * sp.amax(sp.absolute(curfit)), 1.25 * sp.amax(sp.absolute(curfit))]) if (curparm_in == "ne") and nelog: ax.set_xscale("log") ax.set_xlabel(params[iparam]) ax.set_ylabel("Alt km") ax.set_title( "{0} vs Altitude, Time: {1}s Az: {2}$^o$ El: {3}$^o$".format(params[iparam], times[itime], *curbeam) ) imcount = imcount + 1 # save figure figmplf.suptitle(suptitle, fontsize=20) if None in labels: labels.remove(None) lines.remove(None) plt.figlegend(lines, labels, loc="lower center", ncol=5, labelspacing=0.0) fname = filetemplate + "_{0:0>3}.png".format(i_fig) plt.savefig(fname) plt.close(figmplf)