def cg(self,j11,m11,j22,m22,j,m): # calculates a clebsch-gordan coef (j1,m1,j2,m2,j,m). # uses general cg formula provided in many references. # assume qf is index for fstates and qj for ijstates. # fstates[qf] = sum_over_qj( C(qf,qj)*(ijstates[qj] ) fac = self.fac if j11 < j22: #if j1<j2 switch them j1 = j22 j2 = j11 m1 = m22 m2 = m11 else: j1 = j11 j2 = j22 m1 = m11 m2 = m22 N1 = fac(j1+j2-j)*fac(j+j1-j2)*fac(j+j2-j1)*(2*j+1) D1 = fac(j+j1+j2+1) N2 = fac(j1+m1)*fac(j1-m1)*fac(j2+m2)*fac(j2-m2)*fac(j+m)*fac(j-m) def D2(k): return fac(k)*fac(j1+j2-j-k)*fac(j1-m1-k)*fac(j2+m2-k)*fac(j-j2+m1+k)*fac(j-j1-m2+k) factor1 = self.delta(m1+m2-m)*scipy.sqrt(N1/float(D1)) #find range for k, largest k such that no factorials are negative klist = scipy.rint(scipy.asarray([j1+j2-j,j1-m1,j2+m2])) kmax = int(klist.min()) klist = scipy.rint(scipy.asarray([0,-(j-j2+m1),-(j-j1-m2)])) kmin = int(klist.max()) factor2 = 0 for k in range(kmin,kmax+1): factor2 += ((-1)**k)*scipy.sqrt(N2)/D2(k) return factor1*factor2
def coordinates_to_voxel_idx(coords_xyz, masker): # transform to homogeneous coordinates coords_h_xyz = sp.append(coords_xyz, ones([1,coords_xyz.shape[1]]),axis=0) # apply inverse affine transformation to get homogeneous coordinates in voxel space inv_transf = sp.linalg.inv(masker.volume.get_affine()) coords_h_voxel_space = inv_transf.dot(coords_h_xyz) coords_h_voxel_space = sp.rint(coords_h_voxel_space).astype(int) # remove homogeneous dimension coords_voxel_space = coords_h_voxel_space[0:-1,:] # convert coordinates to idcs in a flattened voxel space flattened_idcs = sp.ravel_multi_index(coords_voxel_space, masker.dims) # check if there is any study data for the flattened idcs voxel_idcs = sp.zeros((1,len(flattened_idcs)),dtype=int64) for i in range(0,len(flattened_idcs)): idcs = find(masker.in_mask == flattened_idcs[i]) if len(idcs > 0): voxel_idcs[0,i] = find(masker.in_mask == flattened_idcs[i]) else: voxel_idcs[0,i] = nan return voxel_idcs
def get_hand_props(self): """Initializes self.handarea [sqmeters], self.handrad [m], self.handslope [-], and self.handstage [ft].""" handc = self.hand_props.variables['COMID'] handslope = self.hand_props.variables['Slope'] # So handstage = self.hand_props.variables[ 'StageHeight'] # h values for Aw and Hr handarea = self.hand_props.variables['WetArea'] # Aw handrad = self.hand_props.variables['HydraulicRadius'] # Hr handlen = self.hand_props.variables['Length'] # Length if handc[self.hand_props_idx] == self.comid: self.handarea = handarea[self.hand_props_idx] * ( 3.28084**2) # Convert sqm to sqft self.handrad = handrad[ self.hand_props_idx] * 3.28084 # Convert m to ft self.handslope = handslope[self.hand_props_idx] # unitless self.handlen = handlen[ self.hand_props_idx] * 3.28084 # Convert m to ft handstage = scipy.array(handstage) * 3.28084 # Convert m to ft self.handstage = scipy.rint(handstage) # Round to nearest int
def get_handnetcdf(self): """Initializes self.handarea (sqmeters), self.handrad (m), self.handslope (-), and self.handstage (ft) """ handc = self.handnetcdf.variables['COMID'] handslope = self.handnetcdf.variables['Slope'] handarea = self.handnetcdf.variables['WetArea'] handrad = self.handnetcdf.variables['HydraulicRadius'] handstage = self.handnetcdf.variables['StageHeight'] if handc[self.handnetcdfidx] == self.comid: self.handarea = handarea[ self.handnetcdfidx] * 10.7639 # Convert sqm to sqft self.handrad = handrad[ self.handnetcdfidx] * 3.28084 # Convert m to ft self.handslope = handslope[self.handnetcdfidx] handstagenew = scipy.array([]) for i in handstage: handstagenew = scipy.append(handstagenew, handstage) self.handstage = handstagenew self.handstage = self.handstage[:49] * 3.28084 # Convert m to ft self.handstage = scipy.rint( self.handstage) # Round to nearest int, to clean up conversion
def get_radial_edges(rwidth, drange): nbins = scipy.rint((dmax - dmin) / rwidth).astype(int) return scipy.linspace(dmin, dmax, nbins + 1)
# perform kmeans clustering on pixel rgb values ar = ar.astype(float) codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS) # Getting main colors vecs, dist = scipy.cluster.vq.vq(ar, codes) counts, bins = scipy.histogram(vecs, len(codes)) # Get hexes, rgbs, names, pantones color_data = [] hexes = [] rgbs = [] c_names = [] cmyks = [] text_colors = [] pantone_names = [] codes = scipy.rint(codes).astype(int) # For each "primary" color for i in range(0, len(codes)): single_color_data = {} # hexes hexes.append(''.join(chr(c) for c in codes[i]).encode('hex')) # rgbs cur = "" for j in range(0, len(codes[i])): cur = cur + str(codes[i][j]) + ", " cur = cur[:-2] rgbs.append(cur) # Closest CSS3 name cur = tuple(map(int, cur.split(","))) c_names.append(get_colour_name(cur)) # CMYK
def mod(n, m): x = n - m * rint(n / m) if x < 0: return x + abs(m) return x
def get_radial_density(self, catalogue, iaddbins=None, rwidth=2., redges=None, normalize=True, density=True, power_weight=1): iaddbins = utils.tolist(iaddbins) normalize = utils.tolist(normalize, n=len(iaddbins), fill=-1) density = utils.tolist(density, n=len(iaddbins), fill=-1) power_weight = utils.tolist(power_weight, n=len(iaddbins), value=1) distance = catalogue.distance() dmin, dmax = distance.min(), distance.max() self.logger.info('Comoving distances: {:.1f} - {:.1f}.'.format( dmin, dmax)) if redges is not None: radialedges = scipy.array(redges) rwidth = scipy.mean(scipy.diff(radialedges)) rmin, rmax = radialedges.min(), radialedges.max() if (rmin > dmin) or (rmax < dmax): raise ValueError( 'Provided radial-edges ({:.1f} - {:.1f}) do not encompass the full survey ({:.1f} - {:.1f}).' .format(rmin, rmax, dmin, dmax)) self.logger.info( 'Provided radial-edges of width: {:.1f} and range: {:.1f} - {:.1f}.' .format(rwidth, rmin, rmax)) nbins = len(radialedges) - 1 else: self.logger.info('Provided radial-width: {:.1f}.'.format(rwidth)) nbins = scipy.rint((dmax - dmin) / rwidth).astype(int) radialedges = scipy.linspace(dmin, dmax + 1e-9, nbins + 1) self.logger.info( 'There are {:d} radial-bins with an average of {:.1f} objects.'. format(nbins, len(catalogue) * 1. / nbins)) def radial_density(distance, weight, normalize=True, density=True): toret = stats.binned_statistic(distance, values=weight, statistic='sum', bins=radialedges)[0] if density: toret /= radial_volume(radialedges) if normalize: toret /= toret.sum() return toret radial = (radialedges[:-1] + radialedges[1:]) / 2. densities, weights = [], [] for iaddbin_, normalize_, density_, power_ in zip( iaddbins, normalize, density, power_weight): if iaddbin_ is not None: mask = catalogue['iaddbin'] == iaddbin_ self.logger.info('Additional cut {:d} ({:d} objects).'.format( iaddbin_, mask.sum())) else: mask = catalogue.trues() weight = catalogue['Weight'][mask] if power_ != 1: self.logger.info( 'Raising weights to power {:.4f}.'.format(power_)) weight **= power_ densities.append( radial_density(distance[mask], weight, normalize=normalize_, density=density_)) weights.append(weight) self.params['catalogue'] = None return radial, densities, weights