def fast_CIC_deposit(x, mi, Ngrid=N, periodic=1): """cloud in cell density estimator """ if ((np.size(mi)) < (np.size(x))): m = x.copy() m[:] = mi else: m = mi dx = 1. / Ngrid rho = np.zeros(Ngrid) left = x - 0.5 * dx right = left + dx xi = np.int32(left / dx) frac = (1. + xi - left / dx) ind = pyl.where(left < 0.) frac[ind] = (-(left[ind] / dx)) xi[ind] = Ngrid - 1 xir = xi.copy() + 1 xir[xir == Ngrid] = 0 rho = pyl.bincount(xi, weights=frac * m, minlength=Ngrid) rho2 = pyl.bincount(xir, weights=(1. - frac) * m, minlength=Ngrid) rho += rho2 return rho * Ngrid
def OutToClass(outputs, targets): outs = [] trgs = [] for out, trg in zip(outputs, targets): # this is to have each output neruon responsible for a class # if False and len(out) == 3 and \ if len(out) == 1: outs.append(out[0]) trgs.append(trg[0]) # print out[0] elif len(out) == 3 and \ array([where(x in [1, 0], True, False) for x in out]).all() and \ bincount(out.astype('int'))[0] != 2: outs.append(-1) trgs.append(argmax(trg)) else: outs.append(argmax(out)) trgs.append(argmax(trg)) return outs, trgs
def testOutput(self): self.assertEqual(bincount(self.net2.ao.astype('int'))[0], 2) self.assertEqual(bincount(self.net2.ao.astype('int'))[1], 1)
def classify(self, x): d = self.X - tile(x.reshape(self.n, 1), self.N) dsq = sum(d*d, 0) neighbours = self.c[argpartition(dsq, self.k)[:self.k]] most_common = argmax(bincount(neighbours.astype(int))) return most_common
zspec = pylab.array(zspec) zphot = pylab.array(zphot) lmass = pylab.array(lmass) zlss = pylab.array(zlss) dzspec = (zspec - zlss) / (1 + zspec) dzphot = (zphot - zlss) / (1 + zphot) ### binning by mass digi_truepos = pylab.digitize(lmass[inds_truepos], lmassbins) digi_trueneg = pylab.digitize(lmass[inds_trueneg], lmassbins) digi_falsepos = pylab.digitize(lmass[inds_falsepos], lmassbins) digi_falseneg = pylab.digitize(lmass[inds_falseneg], lmassbins) bincount_truepos = pylab.bincount(digi_truepos, minlength=len(lmassbins)+1)[1:-1] bincount_trueneg = pylab.bincount(digi_trueneg, minlength=len(lmassbins)+1)[1:-1] bincount_falsepos = pylab.bincount(digi_falsepos, minlength=len(lmassbins)+1)[1:-1] bincount_falseneg = pylab.bincount(digi_falseneg, minlength=len(lmassbins)+1)[1:-1] n_truepos[i] += bincount_truepos n_trueneg[i] += bincount_trueneg n_falsepos[i] += bincount_falsepos n_falseneg[i] += bincount_falseneg print 'done with +/- 1.5 sigma_zphot' nhinlo_falsepos_tot = pylab.array([mypy.massfunc.confidence_interval(ni) for ni in n_falsepos.sum(axis=0)])
(f.cat.z_spec[f.inds_spatial] > zlo_spec) & (f.cat.z_spec[f.inds_spatial] < zhi_spec)) ### LSS candidates selected by zphot members_zphot = pylab.find((f.cat.use[f.inds_spatial] == 1) & (f.cat.z_spec[f.inds_spatial] < 0) & (dr_pkpc_min[f.inds_spatial] <= 1500.) & (f.fout.z[f.inds_spatial] > zlo_phot) & (f.fout.z[f.inds_spatial] < zhi_phot)) ### binning galaxies by stellar mass digi_mass_zspec = pylab.digitize(f.fout.lmass[f.inds_spatial][members_zspec], lmassbins) digi_mass_zphot = pylab.digitize(f.fout.lmass[f.inds_spatial][members_zphot], lmassbins) ngal_bins_zspec = pylab.bincount(digi_mass_zspec, minlength=len(lmassbins)+1)[1:-1] ngal_bins_zphot = pylab.bincount(digi_mass_zphot, minlength=len(lmassbins)+1)[1:-1] ngal_bins_zphot_corr = ngal_bins_zphot * corr_factor nfinal[i] += ngal_bins_zspec + ngal_bins_zphot_corr ################### ### STAR-FORMING ################### ### LSS members selected by zspec members_zspec_sf = pylab.find((f.cat.use[f.inds_spatial] == 1) &
def GetGS(image_name, scale, roi=None, show=True, min_size=500, exclude_zones=None, flipudflag=False): # Load image (Rappel pour convertir couleur to rgb color.rgb2gray) if opencv: original = cv2.imread(image_name) else: original = m.imread(image_name) if flipudflag: original = flipud(original) # manage exclusion zone put them to 1 i.e. bottom if exclude_zones != None: # create markers of background and gravel markers_zones = m.zeros_like(original[:, :, 0]) for zone in exclude_zones: xa = min(zone[0], zone[2]) xb = max(zone[0], zone[2]) ya = min(zone[1], zone[3]) yb = max(zone[1], zone[3]) markers_zones[ya:yb, xa:xb] = 1 if roi != None: # crop the image xa = min(roi[0], roi[2]) xb = max(roi[0], roi[2]) ya = min(roi[1], roi[3]) yb = max(roi[1], roi[3]) original = original[ya:yb, xa:xb] if exclude_zones != None: markers_zones = markers_zones[ya:yb, xa:xb] # transform to hsv if opencv: img = cv2.cvtColor(original, cv2.COLOR_BGR2HSV) else: img = color.rgb2hsv(original) # use s chanel img = img[:, :, 1] # div by 255 si en couleur thre = img if show: m.figure() m.imshow(original) # test filtre sobel (avoir les pentes) if opencv: elev = cv2.Laplacian(thre, cv2.CV_64F) else: elev = filters.sobel(thre) # compute an auto threshold thresh = filters.threshold_otsu(thre) # create markers of background and gravel markers = m.zeros_like(thre) if show: print thresh markers[thre < thresh] = 2 markers[thre > thresh] = 1 if exclude_zones != None: markers[markers_zones == 1] = 1 # use watershade transform (use markes as starting point) segmentation = morphology.watershed(elev, markers) # Clean small object if opencv: kernel = m.ones((2, 2), m.uint8) closing = cv2.morphologyEx(segmentation - 1, cv2.MORPH_CLOSE, kernel) else: closing = ndimage.binary_closing(segmentation - 1) tmp = ndimage.binary_fill_holes(closing) label_objects, nb_labels = ndimage.label(tmp) sizes = m.bincount(label_objects.ravel()) mask_sizes = sizes > min_size mask_sizes[0] = 0 segmentation_cleaned = mask_sizes[label_objects] # relabel cleaned version of segmentation label_objects_clean, nb_label_clean = ndimage.label(segmentation_cleaned) # plot contour of object if show: m.contour(ndimage.binary_fill_holes(label_objects_clean), linewidths=1.2, colors="y") # trouve les informations des objets # old version of scikit properties=measurement_types mes = measure.regionprops(label_objects_clean) granulo = [] for prop in mes: # Correct orientation ! car orientation prend pas en compte le cadrant !!! #: elements of the inertia tensor [a b; b c] Orientation = GetOrientation(prop["CentralMoments"]) x0 = prop["Centroid"][1] y0 = prop["Centroid"][0] x1 = x0 + m.cos(Orientation) * 0.5 * prop["MajorAxisLength"] y1 = y0 - m.sin(Orientation) * 0.5 * prop["MajorAxisLength"] x2 = x0 - m.sin(Orientation) * 0.5 * prop["MinorAxisLength"] y2 = y0 - m.cos(Orientation) * 0.5 * prop["MinorAxisLength"] if show: m.plot((x0, x1), (y0, y1), "-r", linewidth=2.5) m.plot((x0, x2), (y0, y2), "-r", linewidth=2.5) m.plot(x0, y0, ".g", markersize=15) granulo.append(prop["MinorAxisLength"] / scale) # minr, minc, maxr, maxc = prop['BoundingBox'] # bx = (minc, maxc, maxc, minc, minc) # by = (minr, minr, maxr, maxr, minr)[ # m.plot(bx, by, '-b', linewidth=2.5) return m.array(granulo), label_objects_clean, mes
### calculating MFs dm = 0.25 lmassbins = pylab.arange(9.5 - dm / 2., 11.5 + dm, dm) lmassbars = (lmassbins[1:] + lmassbins[:-1]) / 2. xlo, xhi = 25. * 100. / dx_map, 100 - 25. * 100. / dx_map ylo, yhi = 25. * 100. / dy_map, 100 - 25. * 100. / dy_map inds0 = pylab.find((overdens_arr > 0.0) & (overdens_arr < 0.5) & (simdata.x_cMpc > xlo) & (simdata.x_cMpc < xhi) & (simdata.y_cMpc > ylo) & (simdata.y_cMpc < yhi)) inds1 = pylab.find((overdens_arr > 0.5) & (overdens_arr < 1.0) & (simdata.x_cMpc > xlo) & (simdata.x_cMpc < xhi) & (simdata.y_cMpc > ylo) & (simdata.y_cMpc < yhi)) inds2 = pylab.find((overdens_arr > 1.0) & (overdens_arr < 1.5) & (simdata.x_cMpc > xlo) & (simdata.x_cMpc < xhi) & (simdata.y_cMpc > ylo) & (simdata.y_cMpc < yhi)) inds3 = pylab.find((overdens_arr > 1.5) & (overdens_arr < 2.0) & (simdata.x_cMpc > xlo) & (simdata.x_cMpc < xhi) & (simdata.y_cMpc > ylo) & (simdata.y_cMpc < yhi)) digi0 = pylab.digitize(pylab.log10(simdata.stellarMass[inds0]), lmassbins) digi1 = pylab.digitize(pylab.log10(simdata.stellarMass[inds1]), lmassbins) digi2 = pylab.digitize(pylab.log10(simdata.stellarMass[inds2]), lmassbins) digi3 = pylab.digitize(pylab.log10(simdata.stellarMass[inds3]), lmassbins) ngal0 = pylab.bincount(digi0, minlength=len(lmassbins) + 1)[1:-1] ngal1 = pylab.bincount(digi1, minlength=len(lmassbins) + 1)[1:-1] ngal2 = pylab.bincount(digi2, minlength=len(lmassbins) + 1)[1:-1] ngal3 = pylab.bincount(digi3, minlength=len(lmassbins) + 1)[1:-1]
& (f.fout.z[f.inds_spatial] < zhi)) subinds_massive = pylab.find((f.cat.use[f.inds_spatial] == 1) & (f.fout.z[f.inds_spatial] > zlo) & (f.fout.z[f.inds_spatial] < zhi) & (f.fout.lmass[f.inds_spatial] > 11)) if 0.19 < zlo < 0.76: for si in subinds_massive: s[fi] += ' mugshot_%05i_%s.pdf' % ( f.cat.id[f.inds_spatial][si], f.version) digi_mass = pylab.digitize(f.fout.lmass[f.inds_spatial][subinds], massbins) ngal_bins = pylab.bincount(digi_mass, minlength=len(massbins) + 1)[1:-1] nlo_poisson, nhi_poisson = [], [] for n in ngal_bins: nhi, nlo = mypy.massfunc.confidence_interval(n) nlo_poisson.append(nlo) nhi_poisson.append(nhi) nlo_poisson, nhi_poisson = pylab.array(nlo_poisson), pylab.array( nhi_poisson) phi_bins = ngal_bins * 1. / volume / dm ephi_lo = phi_bins * nlo_poisson / ngal_bins ephi_lo[pylab.isnan(ephi_lo)] = 0 ephi_hi = phi_bins * nhi_poisson / ngal_bins ephi_hi[pylab.isnan(ephi_hi)] = ( 1. / volume / dm) * nhi_poisson[pylab.isnan(ephi_hi)] / 1.
#! /usr/bin/env python import sys, pylab x = 5**5**5 t = '$5^{5^5}$' l = pylab.fromstring(str(x), dtype='uint8') - 48 #Remove ASCII 0 offset bin_var = pylab.arange(10) bin_cnt = pylab.bincount(l, minlength=10).astype('double') bin_cnt /= bin_cnt.sum() bin_cnt -= bin_cnt.mean() bin_srt = bin_cnt.argsort() pylab.figure(1) pylab.barh(bin_var, bin_cnt[bin_srt], align='center') pylab.yticks(bin_var, bin_srt) pylab.axis(ymin=-0.5, ymax=9.5) pylab.axvline(0) pylab.ylabel(r'Decimal Digits (sorted by frequency)') pylab.xlabel(r'Mean Centred Normalised Frequency') pylab.title(r'%s is %d decimal digits long' % (t, len(l))) pylab.grid(True) pylab.savefig('long.png') pylab.show()